import { flatMorph, hasDomain, hasOwn, printable, serializePrimitive, throwParseError, type Entry, type Json, type JsonObject, type JsonStructure, type KeySet, type arrayIndexOf, type entryOf, type keySetOf, type listable, type requireKeys, type show } from "@ark/util" import type { NodeConfig, ResolvedUnknownNodeConfig } from "../config.ts" import type { Declaration, nodeOfKind } from "../kinds.ts" import type { BaseNode } from "../node.ts" import type { NodeId } from "../parse.ts" import type { BaseRoot, schemaKindOrRightOf, schemaKindRightOf } from "../roots/root.ts" import type { BaseScope, ResolvedScopeConfig } from "../scope.ts" import type { Structure } from "../structure/structure.ts" import { compileSerializedValue } from "./compile.ts" import type { ArkEnv, BaseNodeDeclaration } from "./declare.ts" import type { Disjoint } from "./disjoint.ts" import { errorWriters, errorWritersByCode } from "./errorWriters.ts" import { namedReference } from "./registry.ts" import { isNode, type makeRootAndArrayPropertiesMutable } from "./utils.ts" export const basisKinds = ["unit", "proto", "domain"] as const export type BasisKind = (typeof basisKinds)[number] export const structuralKinds = [ "required", "optional", "index", "sequence" ] as const export type StructuralKind = (typeof structuralKinds)[number] export const prestructuralKinds = [ "pattern", "divisor", "exactLength", "max", "min", "maxLength", "minLength", "before", "after", "notEqual", "notEqualLength" ] as const export type PrestructuralKind = (typeof prestructuralKinds)[number] export const refinementKinds = [ ...prestructuralKinds, "structure", "predicate" ] as const export type RefinementKind = (typeof refinementKinds)[number] export const constraintKinds = [...refinementKinds, ...structuralKinds] as const export type ConstraintKind = (typeof constraintKinds)[number] export const rootKinds = [ "alias", "union", "morph", "unit", "intersection", "proto", "domain" ] as const export type RootKind = (typeof rootKinds)[number] export type NodeKind = RootKind | ConstraintKind type orderedNodeKinds = [...typeof rootKinds, ...typeof constraintKinds] export const nodeKinds: orderedNodeKinds = [...rootKinds, ...constraintKinds] export type OpenNodeKind = { [k in NodeKind]: Declaration["stacks"] extends true ? k : never }[NodeKind] export type ClosedNodeKind = Exclude export type PrimitiveKind = Exclude export type CompositeKind = Exclude export type OrderedNodeKinds = typeof nodeKinds export const constraintKeys: KeySet = flatMorph( constraintKinds, (i, kind) => [kind, 1] as const ) export const structureKeys: keySetOf = flatMorph( [...structuralKinds, "undeclared", "order"], (i, k) => [k, 1] as const ) type RightsByKind = accumulateRightKinds export type kindOrRightOf = kind | kindRightOf export type kindLeftOf = Exclude< NodeKind, kindOrRightOf > export type kindOrLeftOf = kind | kindLeftOf type accumulateRightKinds = remaining extends ( readonly [infer head extends NodeKind, ...infer tail extends NodeKind[]] ) ? accumulateRightKinds : result export interface InternalIntersectionOptions { pipe: boolean } export interface IntersectionContext extends InternalIntersectionOptions { $: BaseScope invert: boolean } export type ConstraintIntersection< lKind extends ConstraintKind, rKind extends kindOrRightOf > = ( l: nodeOfKind, r: nodeOfKind, ctx: IntersectionContext ) => BaseNode | Disjoint | null export type ConstraintIntersectionMap = show< { [_ in kind]: ConstraintIntersection } & { [rKind in kindRightOf]?: ConstraintIntersection } > export type RootIntersection< lKind extends RootKind, rKind extends schemaKindOrRightOf > = ( l: nodeOfKind, r: nodeOfKind, ctx: IntersectionContext ) => BaseRoot | Disjoint export type TypeIntersectionMap = { [rKind in schemaKindOrRightOf]: RootIntersection } export type IntersectionMap = kind extends RootKind ? TypeIntersectionMap : ConstraintIntersectionMap export type UnknownIntersectionMap = { [k in NodeKind]?: ( l: BaseNode, r: BaseNode, ctx: IntersectionContext ) => UnknownIntersectionResult } export type UnknownIntersectionResult = BaseNode | Disjoint | null type PrecedenceByKind = { [i in arrayIndexOf as OrderedNodeKinds[i]]: i } export const precedenceByKind: PrecedenceByKind = flatMorph( nodeKinds, (i, kind) => [kind, i] as entryOf ) export const isNodeKind = (value: unknown): value is NodeKind => typeof value === "string" && hasOwn(precedenceByKind, value) export function assertNodeKind( value: BaseNode, kind: kind ): asserts value is nodeOfKind { const valueIsNode = isNode(value) if (!valueIsNode || value.kind !== kind) { throwParseError( `Expected node of kind ${kind} (was ${ valueIsNode ? `${value.kind} node` : printable(value) })` ) } } export type precedenceOfKind = PrecedenceByKind[kind] export const precedenceOfKind = ( kind: kind ): precedenceOfKind => precedenceByKind[kind] export type kindRightOf = RightsByKind[kind] export const schemaKindsRightOf = ( kind: kind ): schemaKindRightOf[] => rootKinds.slice(precedenceOfKind(kind) + 1) as never export const unionChildKinds = /* @__PURE__ */ (() => [...schemaKindsRightOf("union"), "alias"] as const)() export type UnionChildKind = (typeof unionChildKinds)[number] export const morphChildKinds = /* @__PURE__ */ (() => [...schemaKindsRightOf("morph"), "alias"] as const)() export type MorphChildKind = (typeof morphChildKinds)[number] export type keyImplementationsOf = { [k in keyof d["inner"]]-?: NodeKeyImplementation } // a string that reads like another value's serialization is quoted const ambiguousStringPattern = /^(?:"|\$ark\d*\.|(?:NaN|-?Infinity|-0|-?\d+n|undefined|Invalid Date|[+-]?\d{4,6}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z)$)/ export const defaultValueSerializer = (v: unknown): Json => { if (typeof v === "string") return ambiguousStringPattern.test(v) ? JSON.stringify(v) : v if (typeof v === "boolean" || v === null) return v if (typeof v === "number") { if (Number.isNaN(v)) return "NaN" if (v === Number.POSITIVE_INFINITY) return "Infinity" if (v === Number.NEGATIVE_INFINITY) return "-Infinity" if (Object.is(v, -0)) return "-0" return v } // registering would keep the value alive, so only values compiled code reads are registered return hasDomain(v, "object") || typeof v === "symbol" ? namedReference(v) : serializePrimitive(v as never) } export const metaJsonOf = (meta: ArkEnv.meta): JsonObject => flatMorph(meta, (k, v) => [ k, k === "examples" ? (v as unknown[]).map(defaultValueSerializer) : defaultValueSerializer(v) ]) export type NodeKeyImplementation< d extends BaseNodeDeclaration, k extends keyof d["inner"], instantiated = Exclude > = requireKeys< { /** whether an undefined value is kept as the key's value rather than dropped */ keepUndef?: true child?: boolean | ((value: instantiated) => BaseNode[]) serialize?: (schema: instantiated) => Json // a structural value defers an alias it holds elsewhere to an alias of its own structural?: true // written to no json, so nodes it alone tells apart are equal unserialized?: true reduceIo?: ( ioKind: "in" | "out", inner: makeRootAndArrayPropertiesMutable, value: d["inner"][k], // the node whose io it is node: BaseNode ) => void }, // require keys containing children specify it, although it can be false in cases like // declaredOut where we don't want to treat the node as a child [instantiated] extends [listable] ? "child" : never > interface CommonNodeImplementationInput { kind: d["kind"] keys: keyImplementationsOf applyConfig?: (inner: d["inner"], config: ResolvedScopeConfig) => d["inner"] /** whether it reports its own error when data fails it, rather than its children's */ ownErr: d["errorCtx"] extends null ? false : true /** rewrites its inner JSON once it's written, e.g. to collapse a key */ fixJson?: (json: { [k in keyof d["inner"]]: Json }) => JsonStructure /** the key whose value stands for its whole json when it's all the json holds */ bareKey?: keyof d["inner"] /** whether its description says what its basis would, so the basis isn't described */ coversNoun?: d["kind"] extends RefinementKind ? true : never /** whether its expression says what its basis would, so the basis isn't written */ coversExpr?: d["kind"] extends RefinementKind ? true : never } export interface UnknownNodeImplementation extends CommonNodeImplementationInput { defaults: ResolvedUnknownNodeConfig /** whether one intersection can hold several of its kind, as narrows and `!=` can */ stacks: boolean keys: Record> } export const compileObjectLiteral = (ctx: object): string => { let result = "{ " for (const [k, v] of Object.entries(ctx)) result += `${k}: ${compileSerializedValue(v)}, ` return result + " }" } export type nodeImplementationOf = nodeImplementationInputOf & { stacks: d["stacks"] defaults: Required> & { description: DescriptionWriter } } export type nodeImplementationInputOf = CommonNodeImplementationInput & { defaults: { description: DescriptionWriter } } & (d["stacks"] extends true ? { stacks: true } : {}) export type DescriptionConfig = | string | DescriptionWriter export type DescriptionWriter = ( node: nodeOfKind ) => string export interface UnknownAttachments { readonly kind: NodeKind readonly impl: UnknownNodeImplementation readonly id: NodeId readonly inner: Record readonly kv: readonly Entry[] readonly meta: ArkEnv.meta readonly metaJson: object readonly children: BaseNode[] } // what createNode passes; the rest is computed when read export interface UnknownAttachmentsInput extends Omit { readonly metaJson: object | undefined readonly shell?: { fn: Function; c: object } | undefined } export interface NarrowedAttachments extends UnknownAttachments { kind: d["kind"] inner: d["inner"] children: nodeOfKind[] } export const implementNode = ( _: nodeImplementationInputOf ): nodeImplementationOf => { const implementation: UnknownNodeImplementation = _ as never if (implementation.ownErr) { Object.assign( implementation.defaults, errorWriters, errorWritersByCode[implementation.kind as never] ) implementation.defaults.expected ??= ctx => "description" in ctx ? (ctx.description as string) : implementation.defaults.description(ctx as never) } return implementation as never }