arktype 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +7 -0
- package/README.md +106 -0
- package/dist/api.cjs +17 -0
- package/dist/api.d.ts +2 -0
- package/dist/api.mjs +3 -0
- package/dist/cjs/main.js +435 -0
- package/dist/cjs/nodes/branches.js +87 -0
- package/dist/cjs/nodes/compose.js +142 -0
- package/dist/cjs/nodes/discriminate.js +201 -0
- package/dist/cjs/nodes/functors.js +37 -0
- package/dist/cjs/nodes/infer.js +6 -0
- package/dist/cjs/nodes/node.js +110 -0
- package/dist/cjs/nodes/predicate.js +89 -0
- package/dist/cjs/nodes/rules/class.js +23 -0
- package/dist/cjs/nodes/rules/collapsibleSet.js +51 -0
- package/dist/cjs/nodes/rules/divisor.js +35 -0
- package/dist/cjs/nodes/rules/props.js +74 -0
- package/dist/cjs/nodes/rules/range.js +111 -0
- package/dist/cjs/nodes/rules/regex.js +70 -0
- package/dist/cjs/nodes/rules/rules.js +158 -0
- package/dist/cjs/nodes/rules/subdomain.js +80 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/parse/definition.js +35 -0
- package/dist/cjs/parse/record.js +39 -0
- package/dist/cjs/parse/string/ast.js +38 -0
- package/dist/cjs/parse/string/reduce/dynamic.js +152 -0
- package/dist/cjs/parse/string/reduce/shared.js +25 -0
- package/dist/cjs/parse/string/reduce/static.js +6 -0
- package/dist/cjs/parse/string/shift/operand/enclosed.js +56 -0
- package/dist/cjs/parse/string/shift/operand/operand.js +13 -0
- package/dist/cjs/parse/string/shift/operand/unenclosed.js +55 -0
- package/dist/cjs/parse/string/shift/operator/bounds.js +89 -0
- package/dist/cjs/parse/string/shift/operator/divisor.js +37 -0
- package/dist/cjs/parse/string/shift/operator/operator.js +28 -0
- package/dist/cjs/parse/string/shift/scanner.js +101 -0
- package/dist/cjs/parse/string/string.js +45 -0
- package/dist/cjs/parse/tuple/keyof.js +48 -0
- package/dist/cjs/parse/tuple/morph.js +58 -0
- package/dist/cjs/parse/tuple/narrow.js +17 -0
- package/dist/cjs/parse/tuple/tuple.js +87 -0
- package/dist/cjs/parse/tuple/utils.js +50 -0
- package/dist/cjs/traverse/problems.js +251 -0
- package/dist/cjs/traverse/traverse.js +270 -0
- package/dist/cjs/utils/chainableNoOpProxy.js +25 -0
- package/dist/cjs/utils/domains.js +69 -0
- package/dist/cjs/utils/errors.js +28 -0
- package/dist/cjs/utils/freeze.js +18 -0
- package/dist/cjs/utils/generics.js +38 -0
- package/dist/cjs/utils/numericLiterals.js +86 -0
- package/dist/cjs/utils/paths.js +34 -0
- package/dist/cjs/utils/serialize.js +100 -0
- package/dist/cjs/utils/unionToTuple.js +6 -0
- package/dist/mjs/main.js +415 -0
- package/dist/mjs/nodes/branches.js +73 -0
- package/dist/mjs/nodes/compose.js +120 -0
- package/dist/mjs/nodes/discriminate.js +186 -0
- package/dist/mjs/nodes/functors.js +29 -0
- package/dist/mjs/nodes/infer.js +3 -0
- package/dist/mjs/nodes/node.js +92 -0
- package/dist/mjs/nodes/predicate.js +71 -0
- package/dist/mjs/nodes/rules/class.js +9 -0
- package/dist/mjs/nodes/rules/collapsibleSet.js +37 -0
- package/dist/mjs/nodes/rules/divisor.js +21 -0
- package/dist/mjs/nodes/rules/props.js +59 -0
- package/dist/mjs/nodes/rules/range.js +92 -0
- package/dist/mjs/nodes/rules/regex.js +55 -0
- package/dist/mjs/nodes/rules/rules.js +140 -0
- package/dist/mjs/nodes/rules/subdomain.js +65 -0
- package/dist/mjs/package.json +3 -0
- package/dist/mjs/parse/definition.js +19 -0
- package/dist/mjs/parse/record.js +31 -0
- package/dist/mjs/parse/string/ast.js +21 -0
- package/dist/mjs/parse/string/reduce/dynamic.js +144 -0
- package/dist/mjs/parse/string/reduce/shared.js +8 -0
- package/dist/mjs/parse/string/reduce/static.js +3 -0
- package/dist/mjs/parse/string/shift/operand/enclosed.js +41 -0
- package/dist/mjs/parse/string/shift/operand/operand.js +5 -0
- package/dist/mjs/parse/string/shift/operand/unenclosed.js +38 -0
- package/dist/mjs/parse/string/shift/operator/bounds.js +72 -0
- package/dist/mjs/parse/string/shift/operator/divisor.js +23 -0
- package/dist/mjs/parse/string/shift/operator/operator.js +13 -0
- package/dist/mjs/parse/string/shift/scanner.js +93 -0
- package/dist/mjs/parse/string/string.js +30 -0
- package/dist/mjs/parse/tuple/keyof.js +40 -0
- package/dist/mjs/parse/tuple/morph.js +44 -0
- package/dist/mjs/parse/tuple/narrow.js +9 -0
- package/dist/mjs/parse/tuple/tuple.js +73 -0
- package/dist/mjs/parse/tuple/utils.js +36 -0
- package/dist/mjs/traverse/problems.js +231 -0
- package/dist/mjs/traverse/traverse.js +254 -0
- package/dist/mjs/utils/chainableNoOpProxy.js +17 -0
- package/dist/mjs/utils/domains.js +50 -0
- package/dist/mjs/utils/errors.js +12 -0
- package/dist/mjs/utils/freeze.js +10 -0
- package/dist/mjs/utils/generics.js +17 -0
- package/dist/mjs/utils/numericLiterals.js +73 -0
- package/dist/mjs/utils/paths.js +20 -0
- package/dist/mjs/utils/serialize.js +83 -0
- package/dist/mjs/utils/unionToTuple.js +3 -0
- package/dist/tsconfig.temp.tsbuildinfo +1 -0
- package/dist/types/main.d.ts +247 -0
- package/dist/types/nodes/branches.d.ts +14 -0
- package/dist/types/nodes/compose.d.ts +92 -0
- package/dist/types/nodes/discriminate.d.ts +27 -0
- package/dist/types/nodes/functors.d.ts +6 -0
- package/dist/types/nodes/infer.d.ts +34 -0
- package/dist/types/nodes/node.d.ts +42 -0
- package/dist/types/nodes/predicate.d.ts +52 -0
- package/dist/types/nodes/rules/class.d.ts +3 -0
- package/dist/types/nodes/rules/collapsibleSet.d.ts +4 -0
- package/dist/types/nodes/rules/divisor.d.ts +2 -0
- package/dist/types/nodes/rules/props.d.ts +20 -0
- package/dist/types/nodes/rules/range.d.ts +33 -0
- package/dist/types/nodes/rules/regex.d.ts +14 -0
- package/dist/types/nodes/rules/rules.d.ts +44 -0
- package/dist/types/nodes/rules/subdomain.d.ts +15 -0
- package/dist/types/parse/definition.d.ts +29 -0
- package/dist/types/parse/record.d.ts +24 -0
- package/dist/types/parse/string/ast.d.ts +42 -0
- package/dist/types/parse/string/reduce/dynamic.d.ts +34 -0
- package/dist/types/parse/string/reduce/shared.d.ts +24 -0
- package/dist/types/parse/string/reduce/static.d.ts +115 -0
- package/dist/types/parse/string/shift/operand/enclosed.d.ts +28 -0
- package/dist/types/parse/string/shift/operand/operand.d.ts +9 -0
- package/dist/types/parse/string/shift/operand/unenclosed.d.ts +18 -0
- package/dist/types/parse/string/shift/operator/bounds.d.ts +20 -0
- package/dist/types/parse/string/shift/operator/divisor.d.ts +9 -0
- package/dist/types/parse/string/shift/operator/operator.d.ts +13 -0
- package/dist/types/parse/string/shift/scanner.d.ts +89 -0
- package/dist/types/parse/string/string.d.ts +27 -0
- package/dist/types/parse/tuple/keyof.d.ts +2 -0
- package/dist/types/parse/tuple/morph.d.ts +15 -0
- package/dist/types/parse/tuple/narrow.d.ts +13 -0
- package/dist/types/parse/tuple/tuple.d.ts +37 -0
- package/dist/types/parse/tuple/utils.d.ts +19 -0
- package/dist/types/traverse/problems.d.ts +123 -0
- package/dist/types/traverse/traverse.d.ts +36 -0
- package/dist/types/utils/chainableNoOpProxy.d.ts +14 -0
- package/dist/types/utils/domains.d.ts +58 -0
- package/dist/types/utils/errors.d.ts +6 -0
- package/dist/types/utils/freeze.d.ts +2 -0
- package/dist/types/utils/generics.d.ts +122 -0
- package/dist/types/utils/numericLiterals.d.ts +28 -0
- package/dist/types/utils/paths.d.ts +7 -0
- package/dist/types/utils/serialize.d.ts +38 -0
- package/dist/types/utils/unionToTuple.d.ts +11 -0
- package/package.json +101 -2
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Subdomain } from "../../utils/domains.js";
|
|
2
|
+
import type { Dict } from "../../utils/generics.js";
|
|
3
|
+
import type { TraversalNode, TypeNode } from "../node.js";
|
|
4
|
+
import type { FlattenAndPushRule } from "./rules.js";
|
|
5
|
+
export type SubdomainRule<$ = Dict> = Subdomain | readonly ["Array", TypeNode<$>] | readonly ["Array", TypeNode<$>] | readonly ["Set", TypeNode<$>] | readonly ["Map", TypeNode<$>, TypeNode<$>];
|
|
6
|
+
export type TraversalSubdomainRule = Subdomain | readonly ["Array", TraversalNode] | readonly ["Array", TraversalNode] | readonly ["Set", TraversalNode] | readonly ["Map", TraversalNode, TraversalNode];
|
|
7
|
+
export declare const subdomainIntersection: import("../compose.js").Intersector<SubdomainRule<Dict> | undefined>;
|
|
8
|
+
export declare const subdomainPathSegments: {
|
|
9
|
+
readonly "${number}": true;
|
|
10
|
+
readonly "${item}": true;
|
|
11
|
+
readonly "${key}": true;
|
|
12
|
+
readonly "${value}": true;
|
|
13
|
+
};
|
|
14
|
+
export type SubdomainPathSegment = keyof typeof subdomainPathSegments;
|
|
15
|
+
export declare const flattenSubdomain: FlattenAndPushRule<SubdomainRule>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Type } from "../main.js";
|
|
2
|
+
import type { TypeNode } from "../nodes/node.js";
|
|
3
|
+
import type { Primitive, Subdomain } from "../utils/domains.js";
|
|
4
|
+
import { subdomainOf } from "../utils/domains.js";
|
|
5
|
+
import type { Dict, evaluate, isAny, isUnknown, List } from "../utils/generics.js";
|
|
6
|
+
import type { Path } from "../utils/paths.js";
|
|
7
|
+
import type { inferRecord } from "./record.js";
|
|
8
|
+
import type { inferString, validateString } from "./string/string.js";
|
|
9
|
+
import type { inferTuple, TupleExpression, validateTupleExpression } from "./tuple/tuple.js";
|
|
10
|
+
export type ParseContext = {
|
|
11
|
+
type: Type;
|
|
12
|
+
path: Path;
|
|
13
|
+
};
|
|
14
|
+
export declare const parseDefinition: (def: unknown, ctx: ParseContext) => TypeNode;
|
|
15
|
+
export type inferDefinition<def, $> = isAny<def> extends true ? never : def extends inferred<infer t> ? t : def extends string ? inferString<def, $> : def extends List ? inferTuple<def, $> : def extends RegExp ? string : def extends Dict ? inferRecord<def, $> : never;
|
|
16
|
+
export type validateDefinition<def, $> = def extends Terminal ? def : def extends string ? validateString<def, $> : def extends TupleExpression ? validateTupleExpression<def, $> : def extends BadDefinitionType ? writeBadDefinitionTypeMessage<subdomainOf<def>> : isUnknown<def> extends true ? unknownDefinitionMessage : evaluate<{
|
|
17
|
+
[k in keyof def]: validateDefinition<def[k], $>;
|
|
18
|
+
}>;
|
|
19
|
+
export declare const inferred: unique symbol;
|
|
20
|
+
export type inferred<t> = {
|
|
21
|
+
[inferred]?: t;
|
|
22
|
+
};
|
|
23
|
+
export declare const unknownDefinitionMessage = "Cannot statically parse a definition inferred as unknown. Consider using 'as inferred<...>' to cast it.";
|
|
24
|
+
export type unknownDefinitionMessage = typeof unknownDefinitionMessage;
|
|
25
|
+
type Terminal = RegExp | inferred<unknown>;
|
|
26
|
+
type BadDefinitionType = Exclude<Primitive, string> | Function;
|
|
27
|
+
export declare const writeBadDefinitionTypeMessage: <actual extends Subdomain>(actual: actual) => `Type definitions must be strings or objects (was ${actual})`;
|
|
28
|
+
type writeBadDefinitionTypeMessage<actual extends Subdomain> = `Type definitions must be strings or objects (was ${actual})`;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ResolvedNode } from "../nodes/node.js";
|
|
2
|
+
import type { Dict, evaluate } from "../utils/generics.js";
|
|
3
|
+
import type { inferDefinition, ParseContext } from "./definition.js";
|
|
4
|
+
import { Scanner } from "./string/shift/scanner.js";
|
|
5
|
+
export declare const parseRecord: (def: Dict, ctx: ParseContext) => ResolvedNode;
|
|
6
|
+
type withPossiblePreviousEscapeCharacter<k> = k extends `${infer name}?` ? `${name}${Scanner.EscapeToken}?` : k;
|
|
7
|
+
export type inferRecord<def extends Dict, $> = evaluate<{
|
|
8
|
+
[requiredKeyName in requiredKeyOf<def>]: inferDefinition<def[withPossiblePreviousEscapeCharacter<requiredKeyName>], $>;
|
|
9
|
+
} & {
|
|
10
|
+
[optionalKeyName in optionalKeyOf<def>]?: inferDefinition<def[`${optionalKeyName}?`], $>;
|
|
11
|
+
}>;
|
|
12
|
+
type KeyParseResult<name extends string, isOptional extends boolean> = [
|
|
13
|
+
name,
|
|
14
|
+
isOptional
|
|
15
|
+
];
|
|
16
|
+
type parseKey<k> = k extends optionalKeyWithName<infer name> ? name extends `${infer baseName}${Scanner.EscapeToken}` ? [`${baseName}?`, false] : [name, true] : [k, false];
|
|
17
|
+
type optionalKeyWithName<name extends string = string> = `${name}?`;
|
|
18
|
+
type optionalKeyOf<def> = {
|
|
19
|
+
[k in keyof def]: parseKey<k> extends KeyParseResult<infer name, true> ? name : never;
|
|
20
|
+
}[keyof def];
|
|
21
|
+
type requiredKeyOf<def> = {
|
|
22
|
+
[k in keyof def]: parseKey<k> extends KeyParseResult<infer name, false> ? name : never;
|
|
23
|
+
}[keyof def];
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { asIn, resolve } from "../../main.js";
|
|
2
|
+
import type { DisjointsByPath } from "../../nodes/compose.js";
|
|
3
|
+
import type { SizedData, subdomainOf } from "../../utils/domains.js";
|
|
4
|
+
import type { asConst, castOnError, Dict, equals, error, evaluate, extractValues, isAny, List, Literalable, RegexLiteral, requiredKeyOf, stringKeyOf, tryCatch } from "../../utils/generics.js";
|
|
5
|
+
import type { Path, pathToString } from "../../utils/paths.js";
|
|
6
|
+
import type { Out, ParsedMorph } from "../tuple/morph.js";
|
|
7
|
+
import type { StringLiteral } from "./shift/operand/enclosed.js";
|
|
8
|
+
import type { Scanner } from "./shift/scanner.js";
|
|
9
|
+
export type inferAst<ast, $> = ast extends List ? ast[1] extends "[]" ? inferAst<ast[0], $>[] : ast[1] extends "|" ? inferUnion<inferAst<ast[0], $>, inferAst<ast[2], $>> extends infer result ? castOnError<result, never> : never : ast[1] extends "&" ? inferIntersection<inferAst<ast[0], $>, inferAst<ast[2], $>> extends infer result ? castOnError<result, never> : never : ast[1] extends Scanner.Comparator ? ast[0] extends number ? inferAst<ast[2], $> : inferAst<ast[0], $> : ast[1] extends "%" ? inferAst<ast[0], $> : never : inferTerminal<ast, $>;
|
|
10
|
+
export type validateAstSemantics<ast, $> = ast extends string ? undefined : ast extends [infer child, unknown] ? validateAstSemantics<child, $> : ast extends [infer l, infer token, infer r] ? token extends "&" ? tryCatch<inferIntersection<inferAst<l, $>, inferAst<r, $>>, validateBinary<l, r, $>> : token extends "|" ? tryCatch<inferUnion<inferAst<l, $>, inferAst<r, $>>, validateBinary<l, r, $>> : token extends Scanner.Comparator ? l extends number ? validateAstSemantics<r, $> : isBoundable<inferAst<l, $>> extends true ? validateAstSemantics<l, $> : error<writeUnboundableMessage<astToString<ast[0]>>> : token extends "%" ? isDivisible<inferAst<l, $>> extends true ? validateAstSemantics<l, $> : error<writeIndivisibleMessage<astToString<ast[0]>>> : validateAstSemantics<l, $> : undefined;
|
|
11
|
+
type validateBinary<l, r, $> = tryCatch<validateAstSemantics<l, $>, tryCatch<validateAstSemantics<r, $>, undefined>>;
|
|
12
|
+
export type inferIntersection<l, r> = inferIntersectionRecurse<l, r, []>;
|
|
13
|
+
type inferIntersectionRecurse<l, r, path extends string[]> = path["length"] extends 10 ? l & r : l extends never ? never : r extends never ? never : l & r extends never ? error<writeImplicitNeverMessage<path>> : isAny<l | r> extends true ? any : l extends ParsedMorph<infer lIn, infer lOut> ? r extends ParsedMorph ? error<writeImplicitNeverMessage<path, "of morphs">> : (In: evaluate<lIn & r>) => Out<lOut> : r extends ParsedMorph<infer rIn, infer rOut> ? (In: evaluate<rIn & l>) => Out<rOut> : [l, r] extends [Dict, Dict] ? bubblePropErrors<evaluate<{
|
|
14
|
+
[k in stringKeyOf<l>]: k extends keyof r ? inferIntersectionRecurse<l[k], r[k], [...path, k]> : l[k];
|
|
15
|
+
} & Omit<r, keyof l>>> : [l, r] extends [List<infer lItem>, List<infer rItem>] ? inferIntersectionRecurse<lItem, rItem, [
|
|
16
|
+
...path,
|
|
17
|
+
"${number}"
|
|
18
|
+
]> extends infer result ? tryCatch<result, result[]> : never : l & r;
|
|
19
|
+
type bubblePropErrors<o> = extractValues<o, error> extends never ? o : extractValues<o, error>;
|
|
20
|
+
export type inferUnion<l, r> = isAny<l | r> extends true ? any : [l] extends [never] ? r : [r] extends [never] ? l : [asIn<l>, asIn<r>] extends [infer lIn, infer rIn] ? [equals<l, lIn>, equals<r, rIn>] extends [true, true] ? l | r : discriminatable<lIn, rIn> extends true ? l | r : error<undiscriminatableMorphUnionMessage> : never;
|
|
21
|
+
type discriminatable<l, r> = discriminatableRecurse<l, r, []> extends never ? false : true;
|
|
22
|
+
type discriminatableRecurse<l, r, path extends string[]> = path["length"] extends 10 ? never : l & r extends never ? path : subdomainOf<l> & subdomainOf<r> extends never ? path : [subdomainOf<l>, subdomainOf<r>] extends ["object", "object"] ? extractValues<{
|
|
23
|
+
[k in requiredKeyOf<l>]: k extends requiredKeyOf<r> ? discriminatableRecurse<l[k], r[k], [...path, k & string]> : never;
|
|
24
|
+
}, string[]> : never;
|
|
25
|
+
export declare const compileDisjointReasonsMessage: (disjoints: DisjointsByPath) => string;
|
|
26
|
+
export declare const writeImplicitNeverMessage: <path extends [] | Path, description extends string = "">(path: asConst<path>, description?: description | undefined) => `${path extends [] ? "" : `At ${pathToString<path, "/">}: `}Intersection ${description extends "" ? "" : `${description} `}results in an unsatisfiable type`;
|
|
27
|
+
type writeImplicitNeverMessage<path extends string[], description extends string = ""> = `${path extends [] ? "" : `At ${pathToString<path>}: `}Intersection ${description extends "" ? "" : `${description} `}results in an unsatisfiable type`;
|
|
28
|
+
export declare const writeUndiscriminatableMorphUnionMessage: <path extends string>(path: path) => string;
|
|
29
|
+
type undiscriminatableMorphUnionMessage = `A union including one or more morphs must be discriminatable`;
|
|
30
|
+
type isDivisible<data> = isAny<data> extends true ? false : [data] extends [number] ? true : false;
|
|
31
|
+
type isBoundable<data> = isAny<data> extends true ? false : [data] extends [SizedData] ? true : false;
|
|
32
|
+
export type inferTerminal<token, $> = token extends keyof $ ? resolve<token, $> : token extends StringLiteral<infer Text> ? Text : token extends RegexLiteral ? string : token extends number | bigint ? token : never;
|
|
33
|
+
export type astToString<ast> = `'${astToStringRecurse<ast, "">}'`;
|
|
34
|
+
type astToStringRecurse<ast, result extends string> = ast extends [
|
|
35
|
+
infer head,
|
|
36
|
+
...infer tail
|
|
37
|
+
] ? astToStringRecurse<tail, `${result}${astToStringRecurse<head, "">}`> : ast extends Literalable ? `${result}${ast extends bigint ? `${ast}n` : ast}` : "...";
|
|
38
|
+
export declare const writeIndivisibleMessage: <root extends string>(root: root) => `Divisibility operand ${root} must be a number`;
|
|
39
|
+
type writeIndivisibleMessage<root extends string> = `Divisibility operand ${root} must be a number`;
|
|
40
|
+
export declare const writeUnboundableMessage: <root extends string>(root: root) => `Bounded expression ${root} must be a number, string or array`;
|
|
41
|
+
type writeUnboundableMessage<root extends string> = `Bounded expression ${root} must be a number, string or array`;
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { TypeNode } from "../../../nodes/node.js";
|
|
2
|
+
import type { LowerBound } from "../../../nodes/rules/range.js";
|
|
3
|
+
import type { ParseContext } from "../../definition.js";
|
|
4
|
+
import { Scanner } from "../shift/scanner.js";
|
|
5
|
+
export declare class DynamicState {
|
|
6
|
+
readonly ctx: ParseContext;
|
|
7
|
+
readonly scanner: Scanner;
|
|
8
|
+
private root;
|
|
9
|
+
private branches;
|
|
10
|
+
private groups;
|
|
11
|
+
constructor(def: string, ctx: ParseContext);
|
|
12
|
+
error(message: string): never;
|
|
13
|
+
hasRoot(): boolean;
|
|
14
|
+
resolveRoot(): import("../../../nodes/node.js").ResolvedNode;
|
|
15
|
+
rootToString(): string;
|
|
16
|
+
ejectRootIfLimit(): number | undefined;
|
|
17
|
+
ejectRangeIfOpen(): LowerBound | undefined;
|
|
18
|
+
private assertHasRoot;
|
|
19
|
+
private assertUnsetRoot;
|
|
20
|
+
setRoot(node: TypeNode): void;
|
|
21
|
+
rootToArray(): void;
|
|
22
|
+
intersect(node: TypeNode): void;
|
|
23
|
+
private ejectRoot;
|
|
24
|
+
ejectFinalizedRoot(): TypeNode;
|
|
25
|
+
finalize(): undefined;
|
|
26
|
+
reduceLeftBound(limit: number, comparator: Scanner.Comparator): undefined;
|
|
27
|
+
finalizeBranches(): void;
|
|
28
|
+
finalizeGroup(): undefined;
|
|
29
|
+
pushRootToBranch(token: Scanner.BranchToken): void;
|
|
30
|
+
private assertRangeUnset;
|
|
31
|
+
reduceGroupOpen(): void;
|
|
32
|
+
previousOperator(): "|" | "&" | undefined;
|
|
33
|
+
shiftedByOne(): this;
|
|
34
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { MinComparator } from "../../../nodes/rules/range.js";
|
|
2
|
+
import { Scanner } from "../shift/scanner.js";
|
|
3
|
+
export declare const writeUnmatchedGroupCloseMessage: <unscanned extends string>(unscanned: unscanned) => `Unmatched )${unscanned extends "" ? "" : ` before ${unscanned}`}`;
|
|
4
|
+
export type writeUnmatchedGroupCloseMessage<unscanned extends string> = `Unmatched )${unscanned extends "" ? "" : ` before ${unscanned}`}`;
|
|
5
|
+
export declare const unclosedGroupMessage = "Missing )";
|
|
6
|
+
export type unclosedGroupMessage = typeof unclosedGroupMessage;
|
|
7
|
+
export declare const writeOpenRangeMessage: <min extends number, comparator extends ">" | ">=">(min: min, comparator: comparator) => `Left bounds are only valid when paired with right bounds (try ...${comparator}${min})`;
|
|
8
|
+
export type writeOpenRangeMessage<min extends number, comparator extends MinComparator> = `Left bounds are only valid when paired with right bounds (try ...${comparator}${min})`;
|
|
9
|
+
export type writeUnpairableComparatorMessage<comparator extends Scanner.Comparator> = `Left-bounded expressions must specify their limits using < or <= (was ${comparator})`;
|
|
10
|
+
export declare const writeUnpairableComparatorMessage: <comparator extends ">" | "<" | ">=" | "<=" | "==">(comparator: comparator) => `Left-bounded expressions must specify their limits using < or <= (was ${comparator})`;
|
|
11
|
+
export declare const writeMultipleLeftBoundsMessage: <openLimit extends number, openComparator extends ">" | ">=", limit extends number, comparator extends ">" | ">=">(openLimit: openLimit, openComparator: openComparator, limit: limit, comparator: comparator) => `An expression may have at most one left bound (parsed ${openLimit}${{
|
|
12
|
+
readonly "<": ">";
|
|
13
|
+
readonly ">": "<";
|
|
14
|
+
readonly "<=": ">=";
|
|
15
|
+
readonly ">=": "<=";
|
|
16
|
+
readonly "==": "==";
|
|
17
|
+
}[openComparator]}, ${limit}${{
|
|
18
|
+
readonly "<": ">";
|
|
19
|
+
readonly ">": "<";
|
|
20
|
+
readonly "<=": ">=";
|
|
21
|
+
readonly ">=": "<=";
|
|
22
|
+
readonly "==": "==";
|
|
23
|
+
}[comparator]})`;
|
|
24
|
+
export type writeMultipleLeftBoundsMessage<openLimit extends number, openComparator extends MinComparator, limit extends number, comparator extends MinComparator> = `An expression may have at most one left bound (parsed ${openLimit}${Scanner.InvertedComparators[openComparator]}, ${limit}${Scanner.InvertedComparators[comparator]})`;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import type { LowerBound, MinComparator } from "../../../nodes/rules/range.js";
|
|
2
|
+
import type { defined, error } from "../../../utils/generics.js";
|
|
3
|
+
import type { astToString } from "../ast.js";
|
|
4
|
+
import type { Scanner } from "../shift/scanner.js";
|
|
5
|
+
import type { unclosedGroupMessage, writeMultipleLeftBoundsMessage, writeOpenRangeMessage, writeUnmatchedGroupCloseMessage, writeUnpairableComparatorMessage } from "./shared.js";
|
|
6
|
+
export type StaticState = {
|
|
7
|
+
root: unknown;
|
|
8
|
+
branches: BranchState;
|
|
9
|
+
groups: BranchState[];
|
|
10
|
+
unscanned: string;
|
|
11
|
+
};
|
|
12
|
+
type BranchState = {
|
|
13
|
+
range: LowerBound | undefined;
|
|
14
|
+
"&": unknown;
|
|
15
|
+
"|": unknown;
|
|
16
|
+
};
|
|
17
|
+
export declare namespace state {
|
|
18
|
+
export type initialize<def extends string> = from<{
|
|
19
|
+
root: undefined;
|
|
20
|
+
branches: initialBranches;
|
|
21
|
+
groups: [];
|
|
22
|
+
unscanned: def;
|
|
23
|
+
}>;
|
|
24
|
+
type initialBranches = branchesFrom<{
|
|
25
|
+
range: undefined;
|
|
26
|
+
"&": undefined;
|
|
27
|
+
"|": undefined;
|
|
28
|
+
}>;
|
|
29
|
+
export type setRoot<s extends StaticState, root, unscanned extends string> = from<{
|
|
30
|
+
root: root;
|
|
31
|
+
branches: s["branches"];
|
|
32
|
+
groups: s["groups"];
|
|
33
|
+
unscanned: unscanned;
|
|
34
|
+
}>;
|
|
35
|
+
export type reduceBranch<s extends StaticState, token extends Scanner.BranchToken, unscanned extends string> = s["branches"]["range"] extends {} ? openRangeError<s["branches"]["range"]> : from<{
|
|
36
|
+
root: undefined;
|
|
37
|
+
branches: {
|
|
38
|
+
range: undefined;
|
|
39
|
+
"&": token extends "&" ? mergeToIntersection<s> : undefined;
|
|
40
|
+
"|": token extends "|" ? mergeToUnion<s> : s["branches"]["|"];
|
|
41
|
+
};
|
|
42
|
+
groups: s["groups"];
|
|
43
|
+
unscanned: unscanned;
|
|
44
|
+
}>;
|
|
45
|
+
export type reduceLeftBound<s extends StaticState, limit extends number, comparator extends Scanner.Comparator, unscanned extends string> = comparator extends "<" | "<=" ? s["branches"]["range"] extends {} ? error<writeMultipleLeftBoundsMessage<s["branches"]["range"]["limit"], s["branches"]["range"]["comparator"], limit, Scanner.InvertedComparators[comparator]>> : from<{
|
|
46
|
+
root: undefined;
|
|
47
|
+
branches: {
|
|
48
|
+
range: {
|
|
49
|
+
limit: limit;
|
|
50
|
+
comparator: Scanner.InvertedComparators[comparator];
|
|
51
|
+
};
|
|
52
|
+
"&": s["branches"]["&"];
|
|
53
|
+
"|": s["branches"]["|"];
|
|
54
|
+
};
|
|
55
|
+
groups: s["groups"];
|
|
56
|
+
unscanned: unscanned;
|
|
57
|
+
}> : error<writeUnpairableComparatorMessage<comparator>>;
|
|
58
|
+
export type reduceRange<s extends StaticState, minLimit extends number, minComparator extends MinComparator, maxComparator extends Scanner.Comparator, maxLimit extends number, unscanned extends string> = state.from<{
|
|
59
|
+
root: [minLimit, minComparator, [s["root"], maxComparator, maxLimit]];
|
|
60
|
+
branches: {
|
|
61
|
+
range: undefined;
|
|
62
|
+
"&": s["branches"]["&"];
|
|
63
|
+
"|": s["branches"]["|"];
|
|
64
|
+
};
|
|
65
|
+
groups: s["groups"];
|
|
66
|
+
unscanned: unscanned;
|
|
67
|
+
}>;
|
|
68
|
+
export type reduceSingleBound<s extends StaticState, comparator extends Scanner.Comparator, limit extends number, unscanned extends string> = state.from<{
|
|
69
|
+
root: [s["root"], comparator, limit];
|
|
70
|
+
branches: {
|
|
71
|
+
range: undefined;
|
|
72
|
+
"&": s["branches"]["&"];
|
|
73
|
+
"|": s["branches"]["|"];
|
|
74
|
+
};
|
|
75
|
+
groups: s["groups"];
|
|
76
|
+
unscanned: unscanned;
|
|
77
|
+
}>;
|
|
78
|
+
type mergeToUnion<s extends StaticState> = s["branches"]["|"] extends undefined ? mergeToIntersection<s> : [s["branches"]["|"], "|", mergeToIntersection<s>];
|
|
79
|
+
type mergeToIntersection<s extends StaticState> = s["branches"]["&"] extends undefined ? s["root"] : [s["branches"]["&"], "&", s["root"]];
|
|
80
|
+
type popGroup<stack extends BranchState[], top extends BranchState> = [
|
|
81
|
+
...stack,
|
|
82
|
+
top
|
|
83
|
+
];
|
|
84
|
+
export type finalizeGroup<s extends StaticState, unscanned extends string> = s["groups"] extends popGroup<infer stack, infer top> ? from<{
|
|
85
|
+
groups: stack;
|
|
86
|
+
branches: top;
|
|
87
|
+
root: mergeToUnion<s>;
|
|
88
|
+
unscanned: unscanned;
|
|
89
|
+
}> : error<writeUnmatchedGroupCloseMessage<unscanned>>;
|
|
90
|
+
export type reduceGroupOpen<s extends StaticState, unscanned extends string> = from<{
|
|
91
|
+
groups: [...s["groups"], s["branches"]];
|
|
92
|
+
branches: initialBranches;
|
|
93
|
+
root: undefined;
|
|
94
|
+
unscanned: unscanned;
|
|
95
|
+
}>;
|
|
96
|
+
export type finalize<s extends StaticState> = s["groups"] extends [] ? s["branches"]["range"] extends {} ? openRangeError<s["branches"]["range"]> : from<{
|
|
97
|
+
root: mergeToUnion<s>;
|
|
98
|
+
groups: s["groups"];
|
|
99
|
+
branches: initialBranches;
|
|
100
|
+
unscanned: Scanner.finalized;
|
|
101
|
+
}> : error<unclosedGroupMessage>;
|
|
102
|
+
type openRangeError<range extends defined<BranchState["range"]>> = error<writeOpenRangeMessage<range["limit"], range["comparator"]>>;
|
|
103
|
+
export type previousOperator<s extends StaticState> = s["branches"]["range"] extends {} ? s["branches"]["range"]["comparator"] : s["branches"]["&"] extends {} ? "&" : s["branches"]["|"] extends {} ? "|" : undefined;
|
|
104
|
+
export type scanTo<s extends StaticState, unscanned extends string> = from<{
|
|
105
|
+
root: s["root"];
|
|
106
|
+
branches: s["branches"];
|
|
107
|
+
groups: s["groups"];
|
|
108
|
+
unscanned: unscanned;
|
|
109
|
+
}>;
|
|
110
|
+
export type toString<s extends StaticState> = astToString<mergeToUnion<s>>;
|
|
111
|
+
export type from<s extends StaticState> = s;
|
|
112
|
+
export type branchesFrom<b extends BranchState> = b;
|
|
113
|
+
export {};
|
|
114
|
+
}
|
|
115
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { error, tailOfString } from "../../../../utils/generics.js";
|
|
2
|
+
import type { DynamicState } from "../../reduce/dynamic.js";
|
|
3
|
+
import type { state, StaticState } from "../../reduce/static.js";
|
|
4
|
+
import type { Scanner } from "../scanner.js";
|
|
5
|
+
export type StringLiteral<Text extends string = string> = DoubleQuotedStringLiteral<Text> | SingleQuotedStringLiteral<Text>;
|
|
6
|
+
export type DoubleQuotedStringLiteral<Text extends string = string> = `"${Text}"`;
|
|
7
|
+
export type SingleQuotedStringLiteral<Text extends string = string> = `'${Text}'`;
|
|
8
|
+
export declare const parseEnclosed: (s: DynamicState, enclosing: EnclosingChar) => undefined;
|
|
9
|
+
export type parseEnclosed<s extends StaticState, enclosing extends EnclosingChar, unscanned extends string> = Scanner.shiftUntil<unscanned, enclosing> extends Scanner.shiftResult<infer scanned, infer nextUnscanned> ? nextUnscanned extends "" ? error<writeUnterminatedEnclosedMessage<scanned, enclosing>> : state.setRoot<s, `${enclosing}${scanned}${enclosing}`, tailOfString<nextUnscanned>> : never;
|
|
10
|
+
export declare const enclosingChar: {
|
|
11
|
+
"'": number;
|
|
12
|
+
'"': number;
|
|
13
|
+
"/": number;
|
|
14
|
+
};
|
|
15
|
+
export type EnclosingChar = keyof typeof enclosingChar;
|
|
16
|
+
declare const enclosingCharDescriptions: {
|
|
17
|
+
readonly '"': "double-quote";
|
|
18
|
+
readonly "'": "single-quote";
|
|
19
|
+
readonly "/": "forward slash";
|
|
20
|
+
};
|
|
21
|
+
type enclosingCharDescriptions = typeof enclosingCharDescriptions;
|
|
22
|
+
export declare const writeUnterminatedEnclosedMessage: <fragment extends string, enclosing extends "/" | "'" | "\"">(fragment: fragment, enclosing: enclosing) => `${enclosing}${fragment} requires a closing ${{
|
|
23
|
+
readonly '"': "double-quote";
|
|
24
|
+
readonly "'": "single-quote";
|
|
25
|
+
readonly "/": "forward slash";
|
|
26
|
+
}[enclosing]}`;
|
|
27
|
+
type writeUnterminatedEnclosedMessage<fragment extends string, enclosing extends EnclosingChar> = `${enclosing}${fragment} requires a closing ${enclosingCharDescriptions[enclosing]}`;
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { error } from "../../../../utils/generics.js";
|
|
2
|
+
import type { DynamicState } from "../../reduce/dynamic.js";
|
|
3
|
+
import type { state, StaticState } from "../../reduce/static.js";
|
|
4
|
+
import type { Scanner } from "../scanner.js";
|
|
5
|
+
import type { EnclosingChar } from "./enclosed.js";
|
|
6
|
+
import { parseEnclosed } from "./enclosed.js";
|
|
7
|
+
import { parseUnenclosed, writeMissingOperandMessage } from "./unenclosed.js";
|
|
8
|
+
export declare const parseOperand: (s: DynamicState) => void;
|
|
9
|
+
export type parseOperand<s extends StaticState, $> = s["unscanned"] extends Scanner.shift<infer lookahead, infer unscanned> ? lookahead extends "(" ? state.reduceGroupOpen<s, unscanned> : lookahead extends EnclosingChar ? parseEnclosed<s, lookahead, unscanned> : parseUnenclosed<s, $> : error<writeMissingOperandMessage<s>>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { error, stringKeyOf } from "../../../../utils/generics.js";
|
|
2
|
+
import type { BigintLiteral, NumberLiteral, writeMalformedNumericLiteralMessage } from "../../../../utils/numericLiterals.js";
|
|
3
|
+
import type { DynamicState } from "../../reduce/dynamic.js";
|
|
4
|
+
import type { state, StaticState } from "../../reduce/static.js";
|
|
5
|
+
import type { Scanner } from "../scanner.js";
|
|
6
|
+
export declare const parseUnenclosed: (s: DynamicState) => void;
|
|
7
|
+
export type parseUnenclosed<s extends StaticState, $> = Scanner.shiftUntilNextTerminator<s["unscanned"]> extends Scanner.shiftResult<infer scanned, infer nextUnscanned> ? tryResolve<s, scanned, $> extends infer result ? result extends error<infer message> ? error<message> : state.setRoot<s, result, nextUnscanned> : never : never;
|
|
8
|
+
export type isResolvableIdentifier<token, $> = token extends stringKeyOf<$> ? true : false;
|
|
9
|
+
type tryResolve<s extends StaticState, token extends string, $> = isResolvableIdentifier<token, $> extends true ? token : token extends NumberLiteral<infer value> ? number extends value ? error<writeMalformedNumericLiteralMessage<token, "number">> : value : token extends BigintLiteral<infer value> ? bigint extends value ? error<writeMalformedNumericLiteralMessage<token, "bigint">> : value : error<token extends "" ? writeMissingOperandMessage<s> : writeUnresolvableMessage<token>>;
|
|
10
|
+
export declare const writeUnresolvableMessage: <token extends string>(token: token) => `'${token}' is unresolvable`;
|
|
11
|
+
type writeUnresolvableMessage<token extends string> = `'${token}' is unresolvable`;
|
|
12
|
+
export declare const writeMissingOperandMessage: <s extends DynamicState>(s: s) => `Token '|' requires a right operand before '${string}'` | `Token '&' requires a right operand before '${string}'` | `Expected an expression before '${string}'`;
|
|
13
|
+
export type writeMissingOperandMessage<s extends StaticState, operator extends Scanner.InfixToken | undefined = state.previousOperator<s>> = operator extends {} ? writeMissingRightOperandMessage<operator, s["unscanned"]> : writeExpressionExpectedMessage<s["unscanned"]>;
|
|
14
|
+
export type writeMissingRightOperandMessage<token extends Scanner.InfixToken, unscanned extends string> = `Token '${token}' requires a right operand${unscanned extends "" ? "" : ` before '${unscanned}'`}`;
|
|
15
|
+
export declare const writeMissingRightOperandMessage: <token extends Scanner.InfixToken, unscanned extends string>(token: token, unscanned: unscanned) => `Token '${token}' requires a right operand${unscanned extends "" ? "" : ` before '${unscanned}'`}`;
|
|
16
|
+
export declare const writeExpressionExpectedMessage: <unscanned extends string>(unscanned: unscanned) => `Expected an expression${unscanned extends "" ? "" : ` before '${unscanned}'`}`;
|
|
17
|
+
export type writeExpressionExpectedMessage<unscanned extends string> = `Expected an expression${unscanned extends "" ? "" : ` before '${unscanned}'`}`;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { MaxComparator, Range } from "../../../../nodes/rules/range.js";
|
|
2
|
+
import type { error } from "../../../../utils/generics.js";
|
|
3
|
+
import { tryParseWellFormedNumber } from "../../../../utils/numericLiterals.js";
|
|
4
|
+
import type { DynamicState } from "../../reduce/dynamic.js";
|
|
5
|
+
import { writeUnpairableComparatorMessage } from "../../reduce/shared.js";
|
|
6
|
+
import type { state, StaticState } from "../../reduce/static.js";
|
|
7
|
+
import { Scanner } from "../scanner.js";
|
|
8
|
+
export declare const parseBound: (s: DynamicState, start: Scanner.ComparatorStartChar) => void;
|
|
9
|
+
export type parseBound<s extends StaticState, start extends Scanner.ComparatorStartChar, unscanned extends string> = shiftComparator<start, unscanned> extends infer shiftResultOrError ? shiftResultOrError extends Scanner.shiftResult<infer comparator extends Scanner.Comparator, infer nextUnscanned> ? s["root"] extends number ? state.reduceLeftBound<s, s["root"], comparator, nextUnscanned> : parseRightBound<s, comparator, nextUnscanned> : shiftResultOrError : never;
|
|
10
|
+
declare const shiftComparator: (s: DynamicState, start: Scanner.ComparatorStartChar) => Scanner.Comparator;
|
|
11
|
+
type shiftComparator<start extends Scanner.ComparatorStartChar, unscanned extends string> = unscanned extends `=${infer nextUnscanned}` ? [`${start}=`, nextUnscanned] : start extends Scanner.OneCharComparator ? [start, unscanned] : error<singleEqualsMessage>;
|
|
12
|
+
export declare const singleEqualsMessage = "= is not a valid comparator. Use == to check for equality";
|
|
13
|
+
type singleEqualsMessage = typeof singleEqualsMessage;
|
|
14
|
+
export declare const parseRightBound: (s: DynamicState, comparator: Scanner.Comparator) => void;
|
|
15
|
+
export type parseRightBound<s extends StaticState, comparator extends Scanner.Comparator, unscanned extends string> = Scanner.shiftUntilNextTerminator<unscanned> extends Scanner.shiftResult<infer scanned, infer nextUnscanned> ? tryParseWellFormedNumber<scanned, writeInvalidLimitMessage<comparator, scanned>> extends infer limit ? limit extends number ? s["branches"]["range"] extends {} ? comparator extends MaxComparator ? state.reduceRange<s, s["branches"]["range"]["limit"], s["branches"]["range"]["comparator"], comparator, limit, nextUnscanned> : error<writeUnpairableComparatorMessage<comparator>> : state.reduceSingleBound<s, comparator, limit, nextUnscanned> : error<limit & string> : never : never;
|
|
16
|
+
export declare const writeInvalidLimitMessage: <comparator extends ">" | "<" | ">=" | "<=" | "==", limit extends string>(comparator: comparator, limit: limit) => `Comparator ${comparator} must be followed by a number literal (was '${limit}')`;
|
|
17
|
+
export type writeInvalidLimitMessage<comparator extends Scanner.Comparator, limit extends string> = `Comparator ${comparator} must be followed by a number literal (was '${limit}')`;
|
|
18
|
+
export declare const writeEmptyRangeMessage: (range: Range) => string;
|
|
19
|
+
export type BoundableDomain = "string" | "number" | "object";
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { error } from "../../../../utils/generics.js";
|
|
2
|
+
import { tryParseWellFormedInteger } from "../../../../utils/numericLiterals.js";
|
|
3
|
+
import type { DynamicState } from "../../reduce/dynamic.js";
|
|
4
|
+
import type { state, StaticState } from "../../reduce/static.js";
|
|
5
|
+
import type { Scanner } from "../scanner.js";
|
|
6
|
+
export declare const parseDivisor: (s: DynamicState) => void;
|
|
7
|
+
export type parseDivisor<s extends StaticState, unscanned extends string> = Scanner.shiftUntilNextTerminator<unscanned> extends Scanner.shiftResult<infer scanned, infer nextUnscanned> ? tryParseWellFormedInteger<scanned, writeInvalidDivisorMessage<scanned>> extends infer result ? result extends number ? result extends 0 ? error<writeInvalidDivisorMessage<0>> : state.setRoot<s, [s["root"], "%", result], nextUnscanned> : error<result & string> : never : never;
|
|
8
|
+
export declare const writeInvalidDivisorMessage: <divisor extends string | number>(divisor: divisor) => `% operator must be followed by a non-zero integer literal (was ${divisor})`;
|
|
9
|
+
export type writeInvalidDivisorMessage<divisor extends string | number> = `% operator must be followed by a non-zero integer literal (was ${divisor})`;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { error } from "../../../../utils/generics.js";
|
|
2
|
+
import type { DynamicState } from "../../reduce/dynamic.js";
|
|
3
|
+
import type { state, StaticState } from "../../reduce/static.js";
|
|
4
|
+
import { Scanner } from "../scanner.js";
|
|
5
|
+
import { parseBound } from "./bounds.js";
|
|
6
|
+
import { parseDivisor } from "./divisor.js";
|
|
7
|
+
export declare const parseOperator: (s: DynamicState) => void;
|
|
8
|
+
export type parseOperator<s extends StaticState> = s["unscanned"] extends Scanner.shift<infer lookahead, infer unscanned> ? lookahead extends "[" ? unscanned extends Scanner.shift<"]", infer nextUnscanned> ? state.setRoot<s, [s["root"], "[]"], nextUnscanned> : error<incompleteArrayTokenMessage> : lookahead extends Scanner.BranchToken ? state.reduceBranch<s, lookahead, unscanned> : lookahead extends ")" ? state.finalizeGroup<s, unscanned> : lookahead extends Scanner.ComparatorStartChar ? parseBound<s, lookahead, unscanned> : lookahead extends "%" ? parseDivisor<s, unscanned> : lookahead extends " " ? parseOperator<state.scanTo<s, unscanned>> : error<writeUnexpectedCharacterMessage<lookahead>> : state.finalize<s>;
|
|
9
|
+
export declare const writeUnexpectedCharacterMessage: <char extends string>(char: char) => `Unexpected character '${char}'`;
|
|
10
|
+
type writeUnexpectedCharacterMessage<char extends string> = `Unexpected character '${char}'`;
|
|
11
|
+
export declare const incompleteArrayTokenMessage = "Missing expected ']'";
|
|
12
|
+
type incompleteArrayTokenMessage = typeof incompleteArrayTokenMessage;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { Dict } from "../../../utils/generics.js";
|
|
2
|
+
export declare class Scanner<Lookahead extends string = string> {
|
|
3
|
+
private chars;
|
|
4
|
+
private i;
|
|
5
|
+
finalized: boolean;
|
|
6
|
+
constructor(def: string);
|
|
7
|
+
/** Get lookahead and advance scanner by one */
|
|
8
|
+
shift(): Lookahead;
|
|
9
|
+
get lookahead(): Lookahead;
|
|
10
|
+
shiftUntil(condition: Scanner.UntilCondition): string;
|
|
11
|
+
shiftUntilNextTerminator(): string;
|
|
12
|
+
get unscanned(): string;
|
|
13
|
+
lookaheadIs<Char extends Lookahead>(char: Char): this is Scanner<Char>;
|
|
14
|
+
lookaheadIsIn<Tokens extends Dict>(tokens: Tokens): this is Scanner<Extract<keyof Tokens, string>>;
|
|
15
|
+
}
|
|
16
|
+
export declare namespace Scanner {
|
|
17
|
+
type UntilCondition = (scanner: Scanner, shifted: string) => boolean;
|
|
18
|
+
type OnInputEndFn = (scanner: Scanner, shifted: string) => string;
|
|
19
|
+
type ShiftUntilOptions = {
|
|
20
|
+
onInputEnd?: OnInputEndFn;
|
|
21
|
+
};
|
|
22
|
+
const lookaheadIsTerminator: UntilCondition;
|
|
23
|
+
const lookaheadIsNotWhitespace: UntilCondition;
|
|
24
|
+
const comparatorStartChars: {
|
|
25
|
+
readonly "<": true;
|
|
26
|
+
readonly ">": true;
|
|
27
|
+
readonly "=": true;
|
|
28
|
+
};
|
|
29
|
+
const terminatingChars: {
|
|
30
|
+
readonly "|": true;
|
|
31
|
+
readonly "&": true;
|
|
32
|
+
readonly ")": true;
|
|
33
|
+
readonly "[": true;
|
|
34
|
+
readonly "%": true;
|
|
35
|
+
readonly " ": true;
|
|
36
|
+
readonly "<": true;
|
|
37
|
+
readonly ">": true;
|
|
38
|
+
readonly "=": true;
|
|
39
|
+
};
|
|
40
|
+
type TerminatingChar = keyof typeof Scanner.terminatingChars;
|
|
41
|
+
const comparators: {
|
|
42
|
+
readonly "<": true;
|
|
43
|
+
readonly ">": true;
|
|
44
|
+
readonly "<=": true;
|
|
45
|
+
readonly ">=": true;
|
|
46
|
+
readonly "==": true;
|
|
47
|
+
};
|
|
48
|
+
type Comparator = keyof typeof comparators;
|
|
49
|
+
type ComparatorStartChar = keyof typeof comparatorStartChars;
|
|
50
|
+
const oneCharComparators: {
|
|
51
|
+
readonly "<": true;
|
|
52
|
+
readonly ">": true;
|
|
53
|
+
};
|
|
54
|
+
type OneCharComparator = keyof typeof oneCharComparators;
|
|
55
|
+
const comparatorDescriptions: {
|
|
56
|
+
readonly "<": "less than";
|
|
57
|
+
readonly ">": "greater than";
|
|
58
|
+
readonly "<=": "at most";
|
|
59
|
+
readonly ">=": "at least";
|
|
60
|
+
readonly "==": "exactly";
|
|
61
|
+
};
|
|
62
|
+
const invertedComparators: {
|
|
63
|
+
readonly "<": ">";
|
|
64
|
+
readonly ">": "<";
|
|
65
|
+
readonly "<=": ">=";
|
|
66
|
+
readonly ">=": "<=";
|
|
67
|
+
readonly "==": "==";
|
|
68
|
+
};
|
|
69
|
+
type InvertedComparators = typeof invertedComparators;
|
|
70
|
+
const branchTokens: {
|
|
71
|
+
readonly "|": true;
|
|
72
|
+
readonly "&": true;
|
|
73
|
+
};
|
|
74
|
+
type BranchToken = keyof typeof branchTokens;
|
|
75
|
+
type InfixToken = BranchToken | Comparator | "%" | ":" | "=>";
|
|
76
|
+
type PostfixToken = "[]";
|
|
77
|
+
type OperatorToken = InfixToken | PostfixToken;
|
|
78
|
+
const escapeToken = "\\";
|
|
79
|
+
type EscapeToken = typeof escapeToken;
|
|
80
|
+
const whiteSpaceToken = " ";
|
|
81
|
+
type WhiteSpaceToken = typeof whiteSpaceToken;
|
|
82
|
+
type finalized = "{done}";
|
|
83
|
+
type shift<Lookahead extends string, Unscanned extends string> = `${Lookahead}${Unscanned}`;
|
|
84
|
+
type shiftUntil<unscanned extends string, terminator extends string, scanned extends string = ""> = unscanned extends Scanner.shift<infer lookahead, infer nextUnscanned> ? lookahead extends terminator ? scanned extends `${infer base}${EscapeToken}` ? shiftUntil<nextUnscanned, terminator, `${base}${lookahead}`> : [scanned, unscanned] : shiftUntil<nextUnscanned, terminator, `${scanned}${lookahead}`> : [scanned, ""];
|
|
85
|
+
type shiftUntilNot<unscanned extends string, nonTerminator extends string, scanned extends string = ""> = unscanned extends Scanner.shift<infer lookahead, infer nextUnscanned> ? lookahead extends nonTerminator ? shiftUntilNot<nextUnscanned, nonTerminator, `${scanned}${lookahead}`> : [scanned, unscanned] : [scanned, ""];
|
|
86
|
+
type shiftUntilNextTerminator<unscanned extends string> = shiftUntil<skipWhitespace<unscanned>, TerminatingChar>;
|
|
87
|
+
type skipWhitespace<unscanned extends string> = shiftUntilNot<unscanned, WhiteSpaceToken>[1];
|
|
88
|
+
type shiftResult<scanned extends string, unscanned extends string> = [scanned, unscanned];
|
|
89
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { error } from "../../utils/generics.js";
|
|
2
|
+
import type { ParseContext } from "../definition.js";
|
|
3
|
+
import type { inferAst, validateAstSemantics } from "./ast.js";
|
|
4
|
+
import { DynamicState } from "./reduce/dynamic.js";
|
|
5
|
+
import type { state, StaticState } from "./reduce/static.js";
|
|
6
|
+
import { parseOperand } from "./shift/operand/operand.js";
|
|
7
|
+
import type { isResolvableIdentifier } from "./shift/operand/unenclosed.js";
|
|
8
|
+
import { parseOperator } from "./shift/operator/operator.js";
|
|
9
|
+
import type { Scanner } from "./shift/scanner.js";
|
|
10
|
+
export declare const parseString: (def: string, ctx: ParseContext) => import("../../nodes/node.js").TypeNode;
|
|
11
|
+
export type parseString<def extends string, $> = maybeNaiveParse<def, $>;
|
|
12
|
+
export type inferString<def extends string, $> = inferAst<parseString<def, $>, $>;
|
|
13
|
+
export type validateString<def extends string, $> = parseString<def, $> extends infer ast ? ast extends error<infer message> ? message : validateAstSemantics<ast, $> extends error<infer message> ? message : def : never;
|
|
14
|
+
/**
|
|
15
|
+
* Try to parse the definition from right to left using the most common syntax.
|
|
16
|
+
* This can be much more efficient for simple definitions.
|
|
17
|
+
*/
|
|
18
|
+
type maybeNaiveParse<def extends string, $> = def extends `${infer child}[]` ? isResolvableIdentifier<child, $> extends true ? [child, "[]"] : fullStringParse<def, $> : isResolvableIdentifier<def, $> extends true ? def : fullStringParse<def, $>;
|
|
19
|
+
export declare const maybeNaiveParse: (def: string, ctx: ParseContext) => string | import("../../nodes/node.js").ResolvedNode | undefined;
|
|
20
|
+
export declare const fullStringParse: (def: string, ctx: ParseContext) => import("../../nodes/node.js").TypeNode;
|
|
21
|
+
type fullStringParse<def extends string, $> = loop<state.initialize<def>, $>;
|
|
22
|
+
declare const loop: (s: DynamicState) => import("../../nodes/node.js").TypeNode;
|
|
23
|
+
type loop<s extends StaticState | error, $> = s extends StaticState ? loopValid<s, $> : s;
|
|
24
|
+
type loopValid<s extends StaticState, $> = s["unscanned"] extends Scanner.finalized ? s["root"] : loop<next<s, $>, $>;
|
|
25
|
+
declare const next: (s: DynamicState) => void;
|
|
26
|
+
type next<s extends StaticState, $> = s["root"] extends undefined ? parseOperand<s, $> : parseOperator<s>;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { asOut } from "../../main.js";
|
|
2
|
+
import type { nominal } from "../../utils/generics.js";
|
|
3
|
+
import type { inferDefinition, validateDefinition } from "../definition.js";
|
|
4
|
+
import type { PostfixParser, TupleExpression } from "./tuple.js";
|
|
5
|
+
export declare const parseMorphTuple: PostfixParser<"=>">;
|
|
6
|
+
export type Out<t = {}> = nominal<t, "out">;
|
|
7
|
+
export type validateMorphTuple<def extends TupleExpression, $> = readonly [
|
|
8
|
+
_: validateDefinition<def[0], $>,
|
|
9
|
+
_: "=>",
|
|
10
|
+
_: Morph<asOut<inferDefinition<def[0], $>>, "3" extends keyof def ? inferDefinition<def[3], $> : unknown>,
|
|
11
|
+
_?: validateDefinition<def[3], $>
|
|
12
|
+
];
|
|
13
|
+
export type Morph<i = any, o = unknown> = (In: i) => o;
|
|
14
|
+
export type ParsedMorph<i = any, o = unknown> = (In: i) => Out<o>;
|
|
15
|
+
export declare const writeMalformedMorphExpressionMessage: (value: unknown) => string;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { asIn } from "../../main.js";
|
|
2
|
+
import type { Problems } from "../../traverse/problems.js";
|
|
3
|
+
import type { inferDefinition, validateDefinition } from "../definition.js";
|
|
4
|
+
import type { PostfixParser, TupleExpression } from "./tuple.js";
|
|
5
|
+
import type { distributable } from "./utils.js";
|
|
6
|
+
export declare const parseNarrowTuple: PostfixParser<":">;
|
|
7
|
+
export type Narrow<data = any> = (data: data, problems: Problems) => boolean;
|
|
8
|
+
export type validateNarrowTuple<def extends TupleExpression, $> = readonly [
|
|
9
|
+
_: validateDefinition<def[0], $>,
|
|
10
|
+
_: ":",
|
|
11
|
+
_: distributable<Narrow<asIn<inferDefinition<def[0], $>>>>,
|
|
12
|
+
_?: validateDefinition<def[3], $>
|
|
13
|
+
];
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { asIn, asOut } from "../../main.js";
|
|
2
|
+
import type { inferNode } from "../../nodes/infer.js";
|
|
3
|
+
import type { ResolvedNode, TypeNode } from "../../nodes/node.js";
|
|
4
|
+
import type { conform, constructor, error, evaluate, List, returnOf } from "../../utils/generics.js";
|
|
5
|
+
import type { inferDefinition, ParseContext, validateDefinition } from "../definition.js";
|
|
6
|
+
import type { inferIntersection, inferUnion } from "../string/ast.js";
|
|
7
|
+
import { writeMissingRightOperandMessage } from "../string/shift/operand/unenclosed.js";
|
|
8
|
+
import type { Scanner } from "../string/shift/scanner.js";
|
|
9
|
+
import type { Out, validateMorphTuple } from "./morph.js";
|
|
10
|
+
import type { validateNarrowTuple } from "./narrow.js";
|
|
11
|
+
export declare const parseTuple: (def: List, ctx: ParseContext) => TypeNode;
|
|
12
|
+
export type validateTupleExpression<def extends TupleExpression, $> = def[1] extends "=>" ? validateMorphTuple<def, $> : def[1] extends ":" ? validateNarrowTuple<def, $> : def[1] extends Scanner.BranchToken ? def[2] extends undefined ? [def[0], error<writeMissingRightOperandMessage<def[1], "">>] : conform<def, readonly [
|
|
13
|
+
validateDefinition<def[0], $>,
|
|
14
|
+
def[1],
|
|
15
|
+
validateDefinition<def[2], $>
|
|
16
|
+
]> : def[1] extends "[]" ? conform<def, readonly [validateDefinition<def[0], $>, "[]"]> : def[0] extends "===" ? conform<def, readonly ["===", unknown]> : def[0] extends "instanceof" ? conform<def, readonly ["instanceof", constructor]> : def[0] extends "node" ? conform<def, readonly ["node", ResolvedNode<$>]> : def[0] extends "keyof" ? ["keyof", validateDefinition<def[1], $>] : never;
|
|
17
|
+
export type inferTuple<def extends List, $> = def extends TupleExpression ? inferTupleExpression<def, $> : {
|
|
18
|
+
[i in keyof def]: inferDefinition<def[i], $>;
|
|
19
|
+
};
|
|
20
|
+
type inferTupleExpression<def extends TupleExpression, $> = def[1] extends ":" ? "3" extends keyof def ? inferDefinition<def[3], $> : inferDefinition<def[0], $> : def[1] extends "=>" ? (In: asIn<inferDefinition<def[0], $>>) => Out<"3" extends keyof def ? asOut<inferDefinition<def[3], $>> : returnOf<def[2]>> : def[1] extends "&" ? inferIntersection<inferDefinition<def[0], $>, inferDefinition<def[2], $>> : def[1] extends "|" ? inferUnion<inferDefinition<def[0], $>, inferDefinition<def[2], $>> : def[1] extends "[]" ? inferDefinition<def[0], $>[] : def[0] extends "===" ? def[1] : def[0] extends "instanceof" ? def[1] extends constructor<infer t> ? t : never : def[0] extends "node" ? def[1] extends ResolvedNode<$> ? inferNode<def[1], $> : never : def[0] extends "keyof" ? evaluate<keyof inferDefinition<def[1], $>> : never;
|
|
21
|
+
export type PostfixParser<token extends PostfixToken> = (def: PostfixExpression<token>, ctx: ParseContext) => TypeNode;
|
|
22
|
+
export type PrefixParser<token extends PrefixToken> = (def: PrefixExpression<token>, ctx: ParseContext) => TypeNode;
|
|
23
|
+
export type TupleExpression = PrefixExpression | PostfixExpression;
|
|
24
|
+
export declare const writeMalformedFunctionalExpressionMessage: (operator: "=>" | ":", rightDef: unknown) => string;
|
|
25
|
+
export type TupleExpressionToken = PrefixToken | PostfixToken;
|
|
26
|
+
type PostfixToken = "[]" | "&" | "|" | ":" | "=>";
|
|
27
|
+
type PostfixExpression<token extends PostfixToken = PostfixToken> = readonly [
|
|
28
|
+
unknown,
|
|
29
|
+
token,
|
|
30
|
+
...unknown[]
|
|
31
|
+
];
|
|
32
|
+
type PrefixToken = "keyof" | "instanceof" | "===" | "node";
|
|
33
|
+
type PrefixExpression<token extends PrefixToken = PrefixToken> = readonly [
|
|
34
|
+
token,
|
|
35
|
+
...unknown[]
|
|
36
|
+
];
|
|
37
|
+
export {};
|