import { BaseScope, type AliasDefEntry, type ArkSchemaRegistry, type ArkSchemaScopeConfig, type BaseNode, type BaseParseContext, type BaseParseContextInput, type BaseParseOptions, type BaseRoot, type GenericAst, type GenericParamAst, type GenericParamDef, type NodeKind, type NodeSchema, type PreparsedNodeResolution, type PrivateDeclaration, type RootKind, type RootSchema, type arkKind, type exportedNameOf, type nodeOfKind, type reducibleKindOf, type toInternalScope, type writeDuplicateAliasError } from "@ark/schema"; import { type Brand, type ErrorType, type JsonStructure, type anyOrNever, type array, type flattenListable, type noSuggest } from "@ark/util"; import { type GenericDeclaration, type GenericParser, type ParameterString, type baseGenericConstraints, type parseGenericParams, type parseValidGenericParams } from "./generic.ts"; import type { Ark, type } from "./keywords/keywords.ts"; import { InternalMatchParser, type MatchParser } from "./match.ts"; import type { BoundModule, Module, Submodule, exportScope, instantiateExport } from "./module.ts"; import type { NaryIntersectionParser, NaryMergeParser, NaryPipeParser, NaryUnionParser } from "./nary.ts"; import type { DefAst, InferredAst } from "./parser/ast/infer.ts"; import { type inferDefinition } from "./parser/definition.ts"; import type { ParsedOptionalProperty } from "./parser/property.ts"; import type { ParsedDefaultableProperty } from "./parser/shift/operator/default.ts"; import { InternalTypeParser, type DeclarationParser, type DefinitionParser, type EnumeratedTypeParser, type InstanceOfTypeParser, type SchemaParser, type TypeParser, type UnitTypeParser, type ValueOfTypeParser } from "./type.ts"; /** The convenience properties attached to `scope` */ export type ScopeParserAttachments = Omit; export interface ArkScopeConfig extends ArkSchemaScopeConfig { } export interface ScopeParser { (def: scope.validate, config?: ArkScopeConfig): Scope>; define: (def: scope.validate) => def; } export type ModuleParser = (def: scope.validate, config?: ArkScopeConfig) => scope.infer extends infer $ ? Module<{ [k in exportedNameOf<$>]: $[k]; }> : never; export type bindThis = { this: Def; }; /** nominal type for an unparsed definition used during scope bootstrapping */ type Def = Brand; /** sentinel indicating a scope that will be associated with a generic has not yet been parsed */ export type UnparsedScope = "$"; /** These are legal as values of a scope but not as definitions in other contexts */ type PreparsedResolution = PreparsedNodeResolution; type bootstrapAliases = { [k in Exclude]: def[k] extends (PreparsedResolution) ? def[k] extends { t: infer g extends GenericAst; } ? g : def[k] extends Module | BoundModule ? Submodule<$> : def[k] : def[k] extends (() => infer thunkReturn extends PreparsedResolution) ? thunkReturn extends { t: infer g extends GenericAst; } ? g : thunkReturn extends Module | BoundModule ? Submodule<$> : thunkReturn : Def; } & { [k in keyof def & GenericDeclaration as extractGenericName]: GenericAst, bootstrapAliases>, def[k], UnparsedScope>; }; type inferBootstrapped<$> = { [name in keyof $]: $[name] extends Def ? inferDefinition : $[name] extends { t: infer g extends GenericAst; } ? bindGenericToScope : $[name]; } & unknown; export type bindGenericToScope = GenericAst; type extractGenericName = k extends GenericDeclaration ? name : never; type extractGenericParameters = k extends `${string}<${infer params}>` ? ParameterString : never; export type resolutionToAst = [ resolution ] extends [anyOrNever] ? InferredAst : resolution extends Def ? DefAst : resolution extends { [arkKind]: "module"; root: infer root; } ? InferredAst : resolution extends GenericAst ? resolution : InferredAst; export type moduleKeyOf<$> = { [k in keyof $]: $[k] extends { [arkKind]: "module"; } ? [ $[k] ] extends [anyOrNever] ? never : k & string : never; }[keyof $]; export interface ArkTypeRegistry extends ArkSchemaRegistry { typeAttachments?: Ark.boundTypeAttachments; ambient: exportScope; } export declare const $arkTypeRegistry: ArkTypeRegistry; export interface InternalScope { constructor: typeof InternalScope; } export declare class InternalScope<$ extends {} = {}> extends BaseScope<$> { get ambientAttachments(): Ark.boundTypeAttachments<$> | undefined; protected preparseOwnAliasEntry(alias: string, def: unknown): AliasDefEntry; parseGenericParams(def: string, opts: BaseParseOptions): array; protected normalizeRootScopeValue(resolution: unknown): unknown; protected preparseOwnDefinitionFormat(def: unknown, opts: BaseParseOptions): BaseRoot | BaseParseContextInput; parseOwnDefinitionFormat(def: unknown, ctx: BaseParseContext): BaseRoot; unit: UnitTypeParser<$>; valueOf: ValueOfTypeParser<$>; enumerated: EnumeratedTypeParser<$>; instanceOf: InstanceOfTypeParser<$>; or: NaryUnionParser<$>; and: NaryIntersectionParser<$>; merge: NaryMergeParser<$>; pipe: NaryPipeParser<$>; match: InternalMatchParser; declare: () => { type: InternalTypeParser; }; define(def: def): def; type: InternalTypeParser; static scope: ScopeParser; static module: ModuleParser; } export declare const scope: ScopeParser; export declare namespace scope { type validate = { [k in keyof def]: k extends noSuggest ? unknown : parseScopeKey["params"] extends infer params ? params extends array ? params["length"] extends 0 ? def[k] extends type.Any | PreparsedResolution ? def[k] : k extends (PrivateDeclaration) ? ErrorType> : type.validate, {}> : type.validate, baseGenericConstraints> : params : never; }; type infer = inferBootstrapped>; } export interface Scope<$ = {}> { t: $; [arkKind]: "scope"; config: ArkScopeConfig; references: readonly BaseNode[]; json: JsonStructure; exportedNames: array>; /** The set of names defined at the root-level of the scope mapped to their * corresponding definitions.**/ aliases: Record; internal: toInternalScope<$>; defineSchema(schema: def): def; node>(kinds: kinds, schema: NodeSchema>, opts?: BaseParseOptions): nodeOfKind>>; unit: UnitTypeParser<$>; enumerated: EnumeratedTypeParser<$>; valueOf: ValueOfTypeParser<$>; instanceOf: InstanceOfTypeParser<$>; type: TypeParser<$>; match: MatchParser<$>; declare: DeclarationParser<$>; define: DefinitionParser<$>; generic: GenericParser<$>; schema: SchemaParser<$>; import(): Module<{ [k in exportedNameOf<$> as PrivateDeclaration]: $[k]; }>; import[]>(...names: names): BoundModule<{ [k in names[number] as PrivateDeclaration]: $[k]; } & unknown, $>; export(): Module<{ [k in exportedNameOf<$>]: $[k]; }>; export[]>(...names: names): BoundModule<{ [k in names[number]]: $[k]; } & unknown, $>; resolve>(name: name): instantiateExport<$[name], $>; } export interface ScopeConstructor { new <$ = {}>(...args: ConstructorParameters): Scope<$>; scope: ScopeParser; module: ModuleParser; } export declare const Scope: ScopeConstructor; export type parseScopeKey = k extends `${infer name}<${infer params}>` ? parseGenericScopeKey : { name: k; params: []; }; type parseGenericScopeKey = { name: name; params: parseGenericParams>; }; export type InnerParseResult = BaseRoot | ParsedOptionalProperty | ParsedDefaultableProperty; export {};