arktype 1.0.16-alpha → 1.0.17-alpha
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/README.md +3 -3
- package/dist/cjs/src/main.js +38 -0
- package/dist/cjs/src/nodes/branch.js +140 -0
- package/dist/cjs/src/nodes/compose.js +157 -0
- package/dist/{nodes → cjs/src/nodes}/discriminate.js +92 -100
- package/dist/cjs/src/nodes/node.js +139 -0
- package/dist/cjs/src/nodes/predicate.js +86 -0
- package/dist/cjs/src/nodes/rules/class.js +25 -0
- package/dist/cjs/src/nodes/rules/collapsibleSet.js +48 -0
- package/dist/cjs/src/nodes/rules/divisor.js +29 -0
- package/dist/cjs/src/nodes/rules/props.js +160 -0
- package/dist/cjs/src/nodes/rules/range.js +105 -0
- package/dist/cjs/src/nodes/rules/regex.js +26 -0
- package/dist/cjs/src/nodes/rules/rules.js +123 -0
- package/dist/cjs/src/parse/ast/ast.js +4 -0
- package/dist/cjs/src/parse/ast/bound.js +16 -0
- package/dist/cjs/src/parse/ast/config.js +13 -0
- package/dist/cjs/src/parse/ast/distributableFunction.js +48 -0
- package/dist/cjs/src/parse/ast/divisor.js +9 -0
- package/dist/cjs/src/parse/ast/intersection.js +30 -0
- package/dist/cjs/src/parse/ast/keyof.js +111 -0
- package/dist/cjs/src/parse/ast/morph.js +58 -0
- package/dist/cjs/src/parse/ast/narrow.js +22 -0
- package/dist/cjs/src/parse/ast/node.js +4 -0
- package/dist/cjs/src/parse/ast/tuple.js +94 -0
- package/dist/cjs/src/parse/ast/union.js +9 -0
- package/dist/cjs/src/parse/ast/utils.js +4 -0
- package/dist/cjs/src/parse/definition.js +61 -0
- package/dist/cjs/src/parse/record.js +37 -0
- package/dist/cjs/src/parse/string/reduce/dynamic.js +167 -0
- package/dist/cjs/src/parse/string/reduce/shared.js +23 -0
- package/dist/cjs/src/parse/string/reduce/static.js +4 -0
- package/dist/cjs/src/parse/string/shift/operand/enclosed.js +54 -0
- package/dist/cjs/src/parse/string/shift/operand/operand.js +11 -0
- package/dist/cjs/src/parse/string/shift/operand/unenclosed.js +53 -0
- package/dist/cjs/src/parse/string/shift/operator/bounds.js +87 -0
- package/dist/cjs/src/parse/string/shift/operator/divisor.js +35 -0
- package/dist/cjs/src/parse/string/shift/operator/operator.js +26 -0
- package/dist/cjs/src/parse/string/shift/scanner.js +114 -0
- package/dist/cjs/src/parse/string/string.js +43 -0
- package/dist/cjs/src/scopes/ark.js +45 -0
- package/dist/cjs/src/scopes/cache.js +52 -0
- package/dist/cjs/src/scopes/expressions.js +29 -0
- package/dist/cjs/src/scopes/jsObjects.js +93 -0
- package/dist/cjs/src/scopes/scope.js +323 -0
- package/dist/cjs/src/scopes/tsKeywords.js +113 -0
- package/dist/cjs/src/scopes/type.js +50 -0
- package/dist/cjs/src/scopes/validation/creditCard.js +47 -0
- package/dist/cjs/src/scopes/validation/date.js +62 -0
- package/dist/cjs/src/scopes/validation/validation.js +108 -0
- package/dist/cjs/src/traverse/problems.js +281 -0
- package/dist/cjs/src/traverse/traverse.js +390 -0
- package/dist/cjs/src/utils/chainableNoOpProxy.js +23 -0
- package/dist/cjs/src/utils/data.js +53 -0
- package/dist/cjs/src/utils/domains.js +30 -0
- package/dist/cjs/src/utils/errors.js +26 -0
- package/dist/cjs/src/utils/freeze.js +15 -0
- package/dist/cjs/src/utils/generics.js +63 -0
- package/dist/cjs/src/utils/numericLiterals.js +75 -0
- package/dist/cjs/src/utils/objectKinds.js +69 -0
- package/dist/cjs/src/utils/paths.js +32 -0
- package/dist/cjs/src/utils/serialize.js +74 -0
- package/dist/cjs/src/utils/unionToTuple.js +4 -0
- package/dist/mjs/package.json +3 -0
- package/dist/mjs/src/main.js +8 -0
- package/dist/mjs/src/nodes/branch.js +123 -0
- package/dist/mjs/src/nodes/compose.js +135 -0
- package/dist/mjs/src/nodes/discriminate.js +247 -0
- package/dist/mjs/src/nodes/node.js +117 -0
- package/dist/mjs/src/nodes/predicate.js +69 -0
- package/dist/mjs/src/nodes/rules/class.js +11 -0
- package/dist/mjs/src/nodes/rules/collapsibleSet.js +34 -0
- package/dist/mjs/src/nodes/rules/divisor.js +15 -0
- package/dist/mjs/src/nodes/rules/props.js +142 -0
- package/dist/mjs/src/nodes/rules/range.js +86 -0
- package/dist/mjs/src/nodes/rules/regex.js +11 -0
- package/dist/mjs/src/nodes/rules/rules.js +106 -0
- package/dist/mjs/src/parse/ast/ast.js +1 -0
- package/dist/mjs/src/parse/ast/bound.js +2 -0
- package/dist/mjs/src/parse/ast/config.js +9 -0
- package/dist/mjs/src/parse/ast/distributableFunction.js +34 -0
- package/dist/mjs/src/parse/ast/divisor.js +1 -0
- package/dist/mjs/src/parse/ast/intersection.js +16 -0
- package/dist/mjs/src/parse/ast/keyof.js +103 -0
- package/dist/mjs/src/parse/ast/morph.js +44 -0
- package/dist/mjs/src/parse/ast/narrow.js +14 -0
- package/dist/mjs/src/parse/ast/node.js +1 -0
- package/dist/mjs/src/parse/ast/tuple.js +80 -0
- package/dist/mjs/src/parse/ast/union.js +1 -0
- package/dist/mjs/src/parse/ast/utils.js +1 -0
- package/dist/mjs/src/parse/definition.js +46 -0
- package/dist/mjs/src/parse/record.js +29 -0
- package/dist/mjs/src/parse/string/reduce/dynamic.js +159 -0
- package/dist/mjs/src/parse/string/reduce/shared.js +6 -0
- package/dist/mjs/src/parse/string/reduce/static.js +1 -0
- package/dist/mjs/src/parse/string/shift/operand/enclosed.js +39 -0
- package/dist/mjs/src/parse/string/shift/operand/operand.js +3 -0
- package/dist/mjs/src/parse/string/shift/operand/unenclosed.js +36 -0
- package/dist/mjs/src/parse/string/shift/operator/bounds.js +70 -0
- package/dist/mjs/src/parse/string/shift/operator/divisor.js +21 -0
- package/dist/mjs/src/parse/string/shift/operator/operator.js +13 -0
- package/dist/mjs/src/parse/string/shift/scanner.js +106 -0
- package/dist/mjs/src/parse/string/string.js +28 -0
- package/dist/mjs/src/scopes/ark.js +28 -0
- package/dist/mjs/src/scopes/cache.js +38 -0
- package/dist/{scopes → mjs/src/scopes}/expressions.js +9 -20
- package/dist/mjs/src/scopes/jsObjects.js +82 -0
- package/dist/mjs/src/scopes/scope.js +303 -0
- package/dist/mjs/src/scopes/tsKeywords.js +116 -0
- package/dist/mjs/src/scopes/type.js +35 -0
- package/dist/{scopes → mjs/src/scopes}/validation/creditCard.js +11 -15
- package/dist/{scopes → mjs/src/scopes}/validation/date.js +16 -21
- package/dist/mjs/src/scopes/validation/validation.js +112 -0
- package/dist/mjs/src/traverse/problems.js +260 -0
- package/dist/mjs/src/traverse/traverse.js +373 -0
- package/dist/{utils → mjs/src/utils}/chainableNoOpProxy.js +2 -6
- package/dist/mjs/src/utils/data.js +38 -0
- package/dist/mjs/src/utils/domains.js +15 -0
- package/dist/mjs/src/utils/errors.js +10 -0
- package/dist/mjs/src/utils/freeze.js +7 -0
- package/dist/mjs/src/utils/generics.js +38 -0
- package/dist/mjs/src/utils/numericLiterals.js +72 -0
- package/dist/mjs/src/utils/objectKinds.js +55 -0
- package/dist/{utils → mjs/src/utils}/paths.js +3 -8
- package/dist/mjs/src/utils/serialize.js +58 -0
- package/dist/mjs/src/utils/unionToTuple.js +1 -0
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/package.json +13 -5
- package/dist/main.js +0 -29
- package/dist/nodes/branch.js +0 -135
- package/dist/nodes/compose.js +0 -99
- package/dist/nodes/node.js +0 -134
- package/dist/nodes/predicate.js +0 -88
- package/dist/nodes/rules/class.js +0 -23
- package/dist/nodes/rules/collapsibleSet.js +0 -34
- package/dist/nodes/rules/divisor.js +0 -19
- package/dist/nodes/rules/props.js +0 -130
- package/dist/nodes/rules/range.js +0 -108
- package/dist/nodes/rules/regex.js +0 -17
- package/dist/nodes/rules/rules.js +0 -102
- package/dist/parse/ast/ast.js +0 -2
- package/dist/parse/ast/bound.js +0 -7
- package/dist/parse/ast/config.js +0 -14
- package/dist/parse/ast/distributableFunction.js +0 -40
- package/dist/parse/ast/divisor.js +0 -5
- package/dist/parse/ast/intersection.js +0 -21
- package/dist/parse/ast/keyof.js +0 -107
- package/dist/parse/ast/morph.js +0 -50
- package/dist/parse/ast/narrow.js +0 -17
- package/dist/parse/ast/node.js +0 -2
- package/dist/parse/ast/tuple.js +0 -72
- package/dist/parse/ast/union.js +0 -5
- package/dist/parse/ast/utils.js +0 -2
- package/dist/parse/definition.js +0 -47
- package/dist/parse/record.js +0 -31
- package/dist/parse/string/reduce/dynamic.js +0 -159
- package/dist/parse/string/reduce/shared.js +0 -13
- package/dist/parse/string/reduce/static.js +0 -2
- package/dist/parse/string/shift/operand/enclosed.js +0 -37
- package/dist/parse/string/shift/operand/operand.js +0 -15
- package/dist/parse/string/shift/operand/unenclosed.js +0 -45
- package/dist/parse/string/shift/operator/bounds.js +0 -83
- package/dist/parse/string/shift/operator/divisor.js +0 -23
- package/dist/parse/string/shift/operator/operator.js +0 -34
- package/dist/parse/string/shift/scanner.js +0 -97
- package/dist/parse/string/string.js +0 -35
- package/dist/scopes/ark.js +0 -27
- package/dist/scopes/cache.js +0 -28
- package/dist/scopes/jsObjects.js +0 -20
- package/dist/scopes/scope.js +0 -184
- package/dist/scopes/tsKeywords.js +0 -49
- package/dist/scopes/type.js +0 -40
- package/dist/scopes/validation/validation.js +0 -74
- package/dist/traverse/problems.js +0 -234
- package/dist/traverse/traverse.js +0 -330
- package/dist/utils/data.js +0 -39
- package/dist/utils/domains.js +0 -27
- package/dist/utils/errors.js +0 -17
- package/dist/utils/freeze.js +0 -16
- package/dist/utils/generics.js +0 -53
- package/dist/utils/numericLiterals.js +0 -88
- package/dist/utils/objectKinds.js +0 -69
- package/dist/utils/serialize.js +0 -71
- package/dist/utils/unionToTuple.js +0 -2
- /package/dist/{package.json → cjs/package.json} +0 -0
- /package/dist/{main.d.ts → types/main.d.ts} +0 -0
- /package/dist/{nodes → types/nodes}/branch.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/compose.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/discriminate.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/node.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/predicate.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/class.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/collapsibleSet.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/divisor.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/props.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/range.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/regex.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/rules.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/ast.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/bound.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/config.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/distributableFunction.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/divisor.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/intersection.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/keyof.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/morph.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/narrow.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/node.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/tuple.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/union.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/utils.d.ts +0 -0
- /package/dist/{parse → types/parse}/definition.d.ts +0 -0
- /package/dist/{parse → types/parse}/record.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/reduce/dynamic.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/reduce/shared.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/reduce/static.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operand/enclosed.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operand/operand.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operand/unenclosed.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operator/bounds.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operator/divisor.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operator/operator.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/scanner.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/string.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/ark.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/cache.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/expressions.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/jsObjects.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/scope.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/tsKeywords.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/type.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/validation/creditCard.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/validation/date.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/validation/validation.d.ts +0 -0
- /package/dist/{traverse → types/traverse}/problems.d.ts +0 -0
- /package/dist/{traverse → types/traverse}/traverse.d.ts +0 -0
- /package/dist/{utils → types/utils}/chainableNoOpProxy.d.ts +0 -0
- /package/dist/{utils → types/utils}/data.d.ts +0 -0
- /package/dist/{utils → types/utils}/domains.d.ts +0 -0
- /package/dist/{utils → types/utils}/errors.d.ts +0 -0
- /package/dist/{utils → types/utils}/freeze.d.ts +0 -0
- /package/dist/{utils → types/utils}/generics.d.ts +0 -0
- /package/dist/{utils → types/utils}/numericLiterals.d.ts +0 -0
- /package/dist/{utils → types/utils}/objectKinds.d.ts +0 -0
- /package/dist/{utils → types/utils}/paths.d.ts +0 -0
- /package/dist/{utils → types/utils}/serialize.d.ts +0 -0
- /package/dist/{utils → types/utils}/unionToTuple.d.ts +0 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { isType } from "../scopes/type.js";
|
|
2
|
+
import { domainOf } from "../utils/domains.js";
|
|
3
|
+
import { throwParseError } from "../utils/errors.js";
|
|
4
|
+
import { objectKindOf } from "../utils/objectKinds.js";
|
|
5
|
+
import { stringify } from "../utils/serialize.js";
|
|
6
|
+
import { parseTuple } from "./ast/tuple.js";
|
|
7
|
+
import { parseRecord } from "./record.js";
|
|
8
|
+
import { parseString } from "./string/string.js";
|
|
9
|
+
export const parseDefinition = (def, ctx)=>{
|
|
10
|
+
const domain = domainOf(def);
|
|
11
|
+
if (domain === "string") {
|
|
12
|
+
return parseString(def, ctx);
|
|
13
|
+
}
|
|
14
|
+
if (domain !== "object") {
|
|
15
|
+
return throwParseError(writeBadDefinitionTypeMessage(domain));
|
|
16
|
+
}
|
|
17
|
+
const objectKind = objectKindOf(def);
|
|
18
|
+
switch(objectKind){
|
|
19
|
+
case "Object":
|
|
20
|
+
return parseRecord(def, ctx);
|
|
21
|
+
case "Array":
|
|
22
|
+
return parseTuple(def, ctx);
|
|
23
|
+
case "RegExp":
|
|
24
|
+
return {
|
|
25
|
+
string: {
|
|
26
|
+
regex: def.source
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
case "Function":
|
|
30
|
+
if (isType(def)) {
|
|
31
|
+
return ctx.type.scope.addAnonymousTypeReference(def, ctx);
|
|
32
|
+
}
|
|
33
|
+
if (isThunk(def)) {
|
|
34
|
+
const returned = def();
|
|
35
|
+
if (isType(returned)) {
|
|
36
|
+
return ctx.type.scope.addAnonymousTypeReference(returned, ctx);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return throwParseError(writeBadDefinitionTypeMessage("Function"));
|
|
40
|
+
default:
|
|
41
|
+
return throwParseError(writeBadDefinitionTypeMessage(objectKind ?? stringify(def)));
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
export const as = Symbol("as");
|
|
45
|
+
const isThunk = (def)=>typeof def === "function" && def.length === 0;
|
|
46
|
+
export const writeBadDefinitionTypeMessage = (actual)=>`Type definitions must be strings or objects (was ${actual})`;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { parseDefinition } from "./definition.js";
|
|
2
|
+
import { Scanner } from "./string/shift/scanner.js";
|
|
3
|
+
export const parseRecord = (def, ctx)=>{
|
|
4
|
+
const props = {};
|
|
5
|
+
for(const definitionKey in def){
|
|
6
|
+
let keyName = definitionKey;
|
|
7
|
+
let isOptional = false;
|
|
8
|
+
if (definitionKey[definitionKey.length - 1] === "?") {
|
|
9
|
+
if (definitionKey[definitionKey.length - 2] === Scanner.escapeToken) {
|
|
10
|
+
keyName = `${definitionKey.slice(0, -2)}?`;
|
|
11
|
+
} else {
|
|
12
|
+
keyName = definitionKey.slice(0, -1);
|
|
13
|
+
isOptional = true;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
ctx.path.push(keyName);
|
|
17
|
+
const propNode = parseDefinition(def[definitionKey], ctx);
|
|
18
|
+
ctx.path.pop();
|
|
19
|
+
props[keyName] = isOptional ? [
|
|
20
|
+
"?",
|
|
21
|
+
propNode
|
|
22
|
+
] : propNode;
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
object: {
|
|
26
|
+
props
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
};
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
function _defineProperty(obj, key, value) {
|
|
2
|
+
if (key in obj) {
|
|
3
|
+
Object.defineProperty(obj, key, {
|
|
4
|
+
value: value,
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true
|
|
8
|
+
});
|
|
9
|
+
} else {
|
|
10
|
+
obj[key] = value;
|
|
11
|
+
}
|
|
12
|
+
return obj;
|
|
13
|
+
}
|
|
14
|
+
import { isLiteralNode, rootIntersection, rootUnion, toArrayNode } from "../../../nodes/node.js";
|
|
15
|
+
import { minComparators } from "../../../nodes/rules/range.js";
|
|
16
|
+
import { throwInternalError, throwParseError } from "../../../utils/errors.js";
|
|
17
|
+
import { isKeyOf } from "../../../utils/generics.js";
|
|
18
|
+
import { stringify } from "../../../utils/serialize.js";
|
|
19
|
+
import { Scanner } from "../shift/scanner.js";
|
|
20
|
+
import { unclosedGroupMessage, writeMultipleLeftBoundsMessage, writeOpenRangeMessage, writeUnmatchedGroupCloseMessage, writeUnpairableComparatorMessage } from "./shared.js";
|
|
21
|
+
export class DynamicState {
|
|
22
|
+
error(message) {
|
|
23
|
+
return throwParseError(message);
|
|
24
|
+
}
|
|
25
|
+
hasRoot() {
|
|
26
|
+
return this.root !== undefined;
|
|
27
|
+
}
|
|
28
|
+
resolveRoot() {
|
|
29
|
+
this.assertHasRoot();
|
|
30
|
+
return this.ctx.type.scope.resolveTypeNode(this.root);
|
|
31
|
+
}
|
|
32
|
+
rootToString() {
|
|
33
|
+
this.assertHasRoot();
|
|
34
|
+
return stringify(this.root);
|
|
35
|
+
}
|
|
36
|
+
ejectRootIfLimit() {
|
|
37
|
+
this.assertHasRoot();
|
|
38
|
+
const resolution = typeof this.root === "string" ? this.ctx.type.scope.resolveNode(this.root) : this.root;
|
|
39
|
+
if (isLiteralNode(resolution, "number")) {
|
|
40
|
+
const limit = resolution.number.value;
|
|
41
|
+
this.root = undefined;
|
|
42
|
+
return limit;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
ejectRangeIfOpen() {
|
|
46
|
+
if (this.branches.range) {
|
|
47
|
+
const range = this.branches.range;
|
|
48
|
+
delete this.branches.range;
|
|
49
|
+
return range;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
assertHasRoot() {
|
|
53
|
+
if (this.root === undefined) {
|
|
54
|
+
return throwInternalError("Unexpected interaction with unset root");
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
assertUnsetRoot() {
|
|
58
|
+
if (this.root !== undefined) {
|
|
59
|
+
return throwInternalError("Unexpected attempt to overwrite root");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
setRoot(node) {
|
|
63
|
+
this.assertUnsetRoot();
|
|
64
|
+
this.root = node;
|
|
65
|
+
}
|
|
66
|
+
rootToArray() {
|
|
67
|
+
this.root = toArrayNode(this.ejectRoot());
|
|
68
|
+
}
|
|
69
|
+
intersect(node) {
|
|
70
|
+
this.root = rootIntersection(this.ejectRoot(), node, this.ctx.type);
|
|
71
|
+
}
|
|
72
|
+
ejectRoot() {
|
|
73
|
+
this.assertHasRoot();
|
|
74
|
+
const root = this.root;
|
|
75
|
+
this.root = undefined;
|
|
76
|
+
return root;
|
|
77
|
+
}
|
|
78
|
+
ejectFinalizedRoot() {
|
|
79
|
+
this.assertHasRoot();
|
|
80
|
+
const root = this.root;
|
|
81
|
+
this.root = ejectedProxy;
|
|
82
|
+
return root;
|
|
83
|
+
}
|
|
84
|
+
finalize() {
|
|
85
|
+
if (this.groups.length) {
|
|
86
|
+
return this.error(unclosedGroupMessage);
|
|
87
|
+
}
|
|
88
|
+
this.finalizeBranches();
|
|
89
|
+
this.scanner.finalized = true;
|
|
90
|
+
}
|
|
91
|
+
reduceLeftBound(limit, comparator) {
|
|
92
|
+
const invertedComparator = Scanner.invertedComparators[comparator];
|
|
93
|
+
if (!isKeyOf(invertedComparator, minComparators)) {
|
|
94
|
+
return this.error(writeUnpairableComparatorMessage(comparator));
|
|
95
|
+
}
|
|
96
|
+
if (this.branches.range) {
|
|
97
|
+
return this.error(writeMultipleLeftBoundsMessage(`${this.branches.range.limit}`, this.branches.range.comparator, `${limit}`, invertedComparator));
|
|
98
|
+
}
|
|
99
|
+
this.branches.range = {
|
|
100
|
+
limit,
|
|
101
|
+
comparator: invertedComparator
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
finalizeBranches() {
|
|
105
|
+
this.assertRangeUnset();
|
|
106
|
+
if (this.branches.union) {
|
|
107
|
+
this.pushRootToBranch("|");
|
|
108
|
+
this.setRoot(this.branches.union);
|
|
109
|
+
} else if (this.branches.intersection) {
|
|
110
|
+
this.setRoot(rootIntersection(this.branches.intersection, this.ejectRoot(), this.ctx.type));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
finalizeGroup() {
|
|
114
|
+
this.finalizeBranches();
|
|
115
|
+
const topBranchState = this.groups.pop();
|
|
116
|
+
if (!topBranchState) {
|
|
117
|
+
return this.error(writeUnmatchedGroupCloseMessage(this.scanner.unscanned));
|
|
118
|
+
}
|
|
119
|
+
this.branches = topBranchState;
|
|
120
|
+
}
|
|
121
|
+
pushRootToBranch(token) {
|
|
122
|
+
this.assertRangeUnset();
|
|
123
|
+
this.branches.intersection = this.branches.intersection ? rootIntersection(this.branches.intersection, this.ejectRoot(), this.ctx.type) : this.ejectRoot();
|
|
124
|
+
if (token === "|") {
|
|
125
|
+
this.branches.union = this.branches.union ? rootUnion(this.branches.union, this.branches.intersection, this.ctx.type) : this.branches.intersection;
|
|
126
|
+
delete this.branches.intersection;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
assertRangeUnset() {
|
|
130
|
+
if (this.branches.range) {
|
|
131
|
+
return this.error(writeOpenRangeMessage(`${this.branches.range.limit}`, this.branches.range.comparator));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
reduceGroupOpen() {
|
|
135
|
+
this.groups.push(this.branches);
|
|
136
|
+
this.branches = {};
|
|
137
|
+
}
|
|
138
|
+
previousOperator() {
|
|
139
|
+
return this.branches.range?.comparator ?? this.branches.intersection ? "&" : this.branches.union ? "|" : undefined;
|
|
140
|
+
}
|
|
141
|
+
shiftedByOne() {
|
|
142
|
+
this.scanner.shift();
|
|
143
|
+
return this;
|
|
144
|
+
}
|
|
145
|
+
constructor(def, ctx){
|
|
146
|
+
_defineProperty(this, "ctx", void 0);
|
|
147
|
+
_defineProperty(this, "scanner", void 0);
|
|
148
|
+
_defineProperty(this, "root", void 0);
|
|
149
|
+
_defineProperty(this, "branches", void 0);
|
|
150
|
+
_defineProperty(this, "groups", void 0);
|
|
151
|
+
this.ctx = ctx;
|
|
152
|
+
this.branches = {};
|
|
153
|
+
this.groups = [];
|
|
154
|
+
this.scanner = new Scanner(def);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const ejectedProxy = new Proxy({}, {
|
|
158
|
+
get: ()=>throwInternalError(`Unexpected attempt to access ejected attributes`)
|
|
159
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Scanner } from "../shift/scanner.js";
|
|
2
|
+
export const writeUnmatchedGroupCloseMessage = (unscanned)=>`Unmatched )${unscanned === "" ? "" : ` before ${unscanned}`}`;
|
|
3
|
+
export const unclosedGroupMessage = "Missing )";
|
|
4
|
+
export const writeOpenRangeMessage = (min, comparator)=>`Left bounds are only valid when paired with right bounds (try ...${comparator}${min})`;
|
|
5
|
+
export const writeUnpairableComparatorMessage = (comparator)=>`Left-bounded expressions must specify their limits using < or <= (was ${comparator})`;
|
|
6
|
+
export const writeMultipleLeftBoundsMessage = (openLimit, openComparator, limit, comparator)=>`An expression may have at most one left bound (parsed ${openLimit}${Scanner.invertedComparators[openComparator]}, ${limit}${Scanner.invertedComparators[comparator]})`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { getRegex } from "../../../../nodes/rules/regex.js";
|
|
2
|
+
export const parseEnclosed = (s, enclosing)=>{
|
|
3
|
+
const token = s.scanner.shiftUntil(untilLookaheadIsClosing[enclosing]);
|
|
4
|
+
if (s.scanner.lookahead === "") {
|
|
5
|
+
return s.error(writeUnterminatedEnclosedMessage(token, enclosing));
|
|
6
|
+
}
|
|
7
|
+
// Shift the scanner one additional time for the second enclosing token
|
|
8
|
+
if (s.scanner.shift() === "/") {
|
|
9
|
+
// Cache the regex instance to throw right way if its invalid
|
|
10
|
+
getRegex(token);
|
|
11
|
+
s.setRoot({
|
|
12
|
+
string: {
|
|
13
|
+
regex: token
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
} else {
|
|
17
|
+
s.setRoot({
|
|
18
|
+
string: {
|
|
19
|
+
value: token
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
export const enclosingChar = {
|
|
25
|
+
"'": 1,
|
|
26
|
+
'"': 1,
|
|
27
|
+
"/": 1
|
|
28
|
+
};
|
|
29
|
+
const untilLookaheadIsClosing = {
|
|
30
|
+
"'": (scanner)=>scanner.lookahead === `'`,
|
|
31
|
+
'"': (scanner)=>scanner.lookahead === `"`,
|
|
32
|
+
"/": (scanner)=>scanner.lookahead === `/`
|
|
33
|
+
};
|
|
34
|
+
const enclosingCharDescriptions = {
|
|
35
|
+
'"': "double-quote",
|
|
36
|
+
"'": "single-quote",
|
|
37
|
+
"/": "forward slash"
|
|
38
|
+
};
|
|
39
|
+
export const writeUnterminatedEnclosedMessage = (fragment, enclosing)=>`${enclosing}${fragment} requires a closing ${enclosingCharDescriptions[enclosing]}`;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { enclosingChar, parseEnclosed } from "./enclosed.js";
|
|
2
|
+
import { parseUnenclosed, writeMissingOperandMessage } from "./unenclosed.js";
|
|
3
|
+
export const parseOperand = (s)=>s.scanner.lookahead === "" ? s.error(writeMissingOperandMessage(s)) : s.scanner.lookahead === "(" ? s.shiftedByOne().reduceGroupOpen() : s.scanner.lookaheadIsIn(enclosingChar) ? parseEnclosed(s, s.scanner.shift()) : s.scanner.lookahead === " " ? parseOperand(s.shiftedByOne()) : parseUnenclosed(s);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { tryParseWellFormedBigint, tryParseWellFormedNumber } from "../../../../utils/numericLiterals.js";
|
|
2
|
+
export const parseUnenclosed = (s)=>{
|
|
3
|
+
const token = s.scanner.shiftUntilNextTerminator();
|
|
4
|
+
s.setRoot(unenclosedToNode(s, token));
|
|
5
|
+
};
|
|
6
|
+
const unenclosedToNode = (s, token)=>{
|
|
7
|
+
if (s.ctx.type.scope.addParsedReferenceIfResolvable(token, s.ctx)) {
|
|
8
|
+
return token;
|
|
9
|
+
}
|
|
10
|
+
return maybeParseUnenclosedLiteral(token) ?? s.error(token === "" ? writeMissingOperandMessage(s) : writeUnresolvableMessage(token));
|
|
11
|
+
};
|
|
12
|
+
const maybeParseUnenclosedLiteral = (token)=>{
|
|
13
|
+
const maybeNumber = tryParseWellFormedNumber(token);
|
|
14
|
+
if (maybeNumber !== undefined) {
|
|
15
|
+
return {
|
|
16
|
+
number: {
|
|
17
|
+
value: maybeNumber
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
const maybeBigint = tryParseWellFormedBigint(token);
|
|
22
|
+
if (maybeBigint !== undefined) {
|
|
23
|
+
return {
|
|
24
|
+
bigint: {
|
|
25
|
+
value: maybeBigint
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
export const writeUnresolvableMessage = (token)=>`'${token}' is unresolvable`;
|
|
31
|
+
export const writeMissingOperandMessage = (s)=>{
|
|
32
|
+
const operator = s.previousOperator();
|
|
33
|
+
return operator ? writeMissingRightOperandMessage(operator, s.scanner.unscanned) : writeExpressionExpectedMessage(s.scanner.unscanned);
|
|
34
|
+
};
|
|
35
|
+
export const writeMissingRightOperandMessage = (token, unscanned)=>`Token '${token}' requires a right operand${unscanned ? ` before '${unscanned}'` : ""}`;
|
|
36
|
+
export const writeExpressionExpectedMessage = (unscanned)=>`Expected an expression${unscanned ? ` before '${unscanned}'` : ""}`;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { stringifyRange } from "../../../../nodes/compose.js";
|
|
2
|
+
import { compareStrictness, maxComparators, minComparators } from "../../../../nodes/rules/range.js";
|
|
3
|
+
import { throwInternalError } from "../../../../utils/errors.js";
|
|
4
|
+
import { isKeyOf, listFrom, objectKeysOf } from "../../../../utils/generics.js";
|
|
5
|
+
import { tryParseWellFormedNumber } from "../../../../utils/numericLiterals.js";
|
|
6
|
+
import { writeUnboundableMessage } from "../../../ast/bound.js";
|
|
7
|
+
import { writeUnpairableComparatorMessage } from "../../reduce/shared.js";
|
|
8
|
+
import { Scanner } from "../scanner.js";
|
|
9
|
+
export const parseBound = (s, start)=>{
|
|
10
|
+
const comparator = shiftComparator(s, start);
|
|
11
|
+
const maybeMin = s.ejectRootIfLimit();
|
|
12
|
+
return maybeMin === undefined ? parseRightBound(s, comparator) : s.reduceLeftBound(maybeMin, comparator);
|
|
13
|
+
};
|
|
14
|
+
const shiftComparator = (s, start)=>s.scanner.lookaheadIs("=") ? `${start}${s.scanner.shift()}` : isKeyOf(start, Scanner.oneCharComparators) ? start : s.error(singleEqualsMessage);
|
|
15
|
+
export const singleEqualsMessage = `= is not a valid comparator. Use == to check for equality`;
|
|
16
|
+
export const parseRightBound = (s, comparator)=>{
|
|
17
|
+
const limitToken = s.scanner.shiftUntilNextTerminator();
|
|
18
|
+
const limit = tryParseWellFormedNumber(limitToken, writeInvalidLimitMessage(comparator, limitToken + s.scanner.unscanned));
|
|
19
|
+
const openRange = s.ejectRangeIfOpen();
|
|
20
|
+
const rightBound = {
|
|
21
|
+
comparator,
|
|
22
|
+
limit
|
|
23
|
+
};
|
|
24
|
+
const range = openRange ? !hasComparatorIn(rightBound, maxComparators) ? s.error(writeUnpairableComparatorMessage(comparator)) : compareStrictness("min", openRange, rightBound) === "l" ? s.error(writeEmptyRangeMessage({
|
|
25
|
+
min: openRange,
|
|
26
|
+
max: rightBound
|
|
27
|
+
})) : {
|
|
28
|
+
min: openRange,
|
|
29
|
+
max: rightBound
|
|
30
|
+
} : hasComparator(rightBound, "==") ? rightBound : hasComparatorIn(rightBound, minComparators) ? {
|
|
31
|
+
min: rightBound
|
|
32
|
+
} : hasComparatorIn(rightBound, maxComparators) ? {
|
|
33
|
+
max: rightBound
|
|
34
|
+
} : throwInternalError(`Unexpected comparator '${rightBound.comparator}'`);
|
|
35
|
+
s.intersect(distributeRange(range, s));
|
|
36
|
+
};
|
|
37
|
+
const distributeRange = (range, s)=>{
|
|
38
|
+
const resolution = s.resolveRoot();
|
|
39
|
+
const domains = objectKeysOf(resolution);
|
|
40
|
+
const distributedRange = {};
|
|
41
|
+
const rangePredicate = {
|
|
42
|
+
range
|
|
43
|
+
};
|
|
44
|
+
const isBoundable = domains.every((domain)=>{
|
|
45
|
+
switch(domain){
|
|
46
|
+
case "string":
|
|
47
|
+
distributedRange.string = rangePredicate;
|
|
48
|
+
return true;
|
|
49
|
+
case "number":
|
|
50
|
+
distributedRange.number = rangePredicate;
|
|
51
|
+
return true;
|
|
52
|
+
case "object":
|
|
53
|
+
distributedRange.object = rangePredicate;
|
|
54
|
+
if (resolution.object === true) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
return listFrom(resolution.object).every((branch)=>"class" in branch && branch.class === Array);
|
|
58
|
+
default:
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
if (!isBoundable) {
|
|
63
|
+
s.error(writeUnboundableMessage(s.rootToString()));
|
|
64
|
+
}
|
|
65
|
+
return distributedRange;
|
|
66
|
+
};
|
|
67
|
+
const hasComparator = (bound, comparator)=>bound.comparator === comparator;
|
|
68
|
+
const hasComparatorIn = (bound, comparators)=>bound.comparator in comparators;
|
|
69
|
+
export const writeInvalidLimitMessage = (comparator, limit)=>`Comparator ${comparator} must be followed by a number literal (was '${limit}')`;
|
|
70
|
+
export const writeEmptyRangeMessage = (range)=>`${stringifyRange(range)} is empty`;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { objectKeysOf } from "../../../../utils/generics.js";
|
|
2
|
+
import { tryParseWellFormedInteger } from "../../../../utils/numericLiterals.js";
|
|
3
|
+
import { writeIndivisibleMessage } from "../../../ast/divisor.js";
|
|
4
|
+
export const parseDivisor = (s)=>{
|
|
5
|
+
const divisorToken = s.scanner.shiftUntilNextTerminator();
|
|
6
|
+
const divisor = tryParseWellFormedInteger(divisorToken, writeInvalidDivisorMessage(divisorToken));
|
|
7
|
+
if (divisor === 0) {
|
|
8
|
+
s.error(writeInvalidDivisorMessage(0));
|
|
9
|
+
}
|
|
10
|
+
const rootDomains = objectKeysOf(s.resolveRoot());
|
|
11
|
+
if (rootDomains.length === 1 && rootDomains[0] === "number") {
|
|
12
|
+
s.intersect({
|
|
13
|
+
number: {
|
|
14
|
+
divisor
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
} else {
|
|
18
|
+
s.error(writeIndivisibleMessage(s.rootToString()));
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
export const writeInvalidDivisorMessage = (divisor)=>`% operator must be followed by a non-zero integer literal (was ${divisor})`;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { throwInternalError } from "../../../../utils/errors.js";
|
|
2
|
+
import { isKeyOf } from "../../../../utils/generics.js";
|
|
3
|
+
import { Scanner } from "../scanner.js";
|
|
4
|
+
import { parseBound } from "./bounds.js";
|
|
5
|
+
import { parseDivisor } from "./divisor.js";
|
|
6
|
+
// @snipStart:parseOperator
|
|
7
|
+
export const parseOperator = (s)=>{
|
|
8
|
+
const lookahead = s.scanner.shift();
|
|
9
|
+
return lookahead === "" ? s.finalize() : lookahead === "[" ? s.scanner.shift() === "]" ? s.rootToArray() : s.error(incompleteArrayTokenMessage) : isKeyOf(lookahead, Scanner.branchTokens) ? s.pushRootToBranch(lookahead) : lookahead === ")" ? s.finalizeGroup() : isKeyOf(lookahead, Scanner.comparatorStartChars) ? parseBound(s, lookahead) : lookahead === "%" ? parseDivisor(s) : lookahead === " " ? parseOperator(s) : throwInternalError(writeUnexpectedCharacterMessage(lookahead));
|
|
10
|
+
};
|
|
11
|
+
// @snipEnd
|
|
12
|
+
export const writeUnexpectedCharacterMessage = (char)=>`Unexpected character '${char}'`;
|
|
13
|
+
export const incompleteArrayTokenMessage = `Missing expected ']'`;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
function _defineProperty(obj, key, value) {
|
|
2
|
+
if (key in obj) {
|
|
3
|
+
Object.defineProperty(obj, key, {
|
|
4
|
+
value: value,
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true
|
|
8
|
+
});
|
|
9
|
+
} else {
|
|
10
|
+
obj[key] = value;
|
|
11
|
+
}
|
|
12
|
+
return obj;
|
|
13
|
+
}
|
|
14
|
+
export class Scanner {
|
|
15
|
+
/** Get lookahead and advance scanner by one */ shift() {
|
|
16
|
+
return this.chars[this.i++] ?? "";
|
|
17
|
+
}
|
|
18
|
+
get lookahead() {
|
|
19
|
+
return this.chars[this.i] ?? "";
|
|
20
|
+
}
|
|
21
|
+
shiftUntil(condition) {
|
|
22
|
+
let shifted = "";
|
|
23
|
+
while(this.lookahead){
|
|
24
|
+
if (condition(this, shifted)) {
|
|
25
|
+
if (shifted[shifted.length - 1] === Scanner.escapeToken) {
|
|
26
|
+
shifted = shifted.slice(0, -1);
|
|
27
|
+
} else {
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
shifted += this.shift();
|
|
32
|
+
}
|
|
33
|
+
return shifted;
|
|
34
|
+
}
|
|
35
|
+
shiftUntilNextTerminator() {
|
|
36
|
+
this.shiftUntil(Scanner.lookaheadIsNotWhitespace);
|
|
37
|
+
return this.shiftUntil(Scanner.lookaheadIsTerminator);
|
|
38
|
+
}
|
|
39
|
+
get unscanned() {
|
|
40
|
+
return this.chars.slice(this.i, this.chars.length).join("");
|
|
41
|
+
}
|
|
42
|
+
lookaheadIs(char) {
|
|
43
|
+
return this.lookahead === char;
|
|
44
|
+
}
|
|
45
|
+
lookaheadIsIn(tokens) {
|
|
46
|
+
return this.lookahead in tokens;
|
|
47
|
+
}
|
|
48
|
+
constructor(def){
|
|
49
|
+
_defineProperty(this, "chars", void 0);
|
|
50
|
+
_defineProperty(this, "i", void 0);
|
|
51
|
+
_defineProperty(this, "finalized", false);
|
|
52
|
+
this.chars = [
|
|
53
|
+
...def
|
|
54
|
+
];
|
|
55
|
+
this.i = 0;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
(function(Scanner) {
|
|
59
|
+
var lookaheadIsTerminator = Scanner.lookaheadIsTerminator = (scanner)=>scanner.lookahead in terminatingChars;
|
|
60
|
+
var lookaheadIsNotWhitespace = Scanner.lookaheadIsNotWhitespace = (scanner)=>scanner.lookahead !== whiteSpaceToken;
|
|
61
|
+
var comparatorStartChars = Scanner.comparatorStartChars = {
|
|
62
|
+
"<": true,
|
|
63
|
+
">": true,
|
|
64
|
+
"=": true
|
|
65
|
+
};
|
|
66
|
+
var terminatingChars = Scanner.terminatingChars = {
|
|
67
|
+
...comparatorStartChars,
|
|
68
|
+
"|": true,
|
|
69
|
+
"&": true,
|
|
70
|
+
")": true,
|
|
71
|
+
"[": true,
|
|
72
|
+
"%": true,
|
|
73
|
+
" ": true
|
|
74
|
+
};
|
|
75
|
+
var comparators = Scanner.comparators = {
|
|
76
|
+
"<": true,
|
|
77
|
+
">": true,
|
|
78
|
+
"<=": true,
|
|
79
|
+
">=": true,
|
|
80
|
+
"==": true
|
|
81
|
+
};
|
|
82
|
+
var oneCharComparators = Scanner.oneCharComparators = {
|
|
83
|
+
"<": true,
|
|
84
|
+
">": true
|
|
85
|
+
};
|
|
86
|
+
var comparatorDescriptions = Scanner.comparatorDescriptions = {
|
|
87
|
+
"<": "less than",
|
|
88
|
+
">": "more than",
|
|
89
|
+
"<=": "at most",
|
|
90
|
+
">=": "at least",
|
|
91
|
+
"==": "exactly"
|
|
92
|
+
};
|
|
93
|
+
var invertedComparators = Scanner.invertedComparators = {
|
|
94
|
+
"<": ">",
|
|
95
|
+
">": "<",
|
|
96
|
+
"<=": ">=",
|
|
97
|
+
">=": "<=",
|
|
98
|
+
"==": "=="
|
|
99
|
+
};
|
|
100
|
+
var branchTokens = Scanner.branchTokens = {
|
|
101
|
+
"|": true,
|
|
102
|
+
"&": true
|
|
103
|
+
};
|
|
104
|
+
var escapeToken = Scanner.escapeToken = "\\";
|
|
105
|
+
var whiteSpaceToken = Scanner.whiteSpaceToken = " ";
|
|
106
|
+
})(Scanner || (Scanner = {}));
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { toArrayNode } from "../../nodes/node.js";
|
|
2
|
+
import { DynamicState } from "./reduce/dynamic.js";
|
|
3
|
+
import { parseOperand } from "./shift/operand/operand.js";
|
|
4
|
+
import { parseOperator } from "./shift/operator/operator.js";
|
|
5
|
+
export const parseString = (def, ctx)=>ctx.type.scope.parseCache.get(def) ?? ctx.type.scope.parseCache.set(def, maybeNaiveParse(def, ctx) ?? fullStringParse(def, ctx));
|
|
6
|
+
export const maybeNaiveParse = (def, ctx)=>{
|
|
7
|
+
if (ctx.type.scope.addParsedReferenceIfResolvable(def, ctx)) {
|
|
8
|
+
return def;
|
|
9
|
+
}
|
|
10
|
+
if (def.endsWith("[]")) {
|
|
11
|
+
const elementDef = def.slice(0, -2);
|
|
12
|
+
if (ctx.type.scope.addParsedReferenceIfResolvable(def, ctx)) {
|
|
13
|
+
return toArrayNode(elementDef);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
export const fullStringParse = (def, ctx)=>{
|
|
18
|
+
const s = new DynamicState(def, ctx);
|
|
19
|
+
parseOperand(s);
|
|
20
|
+
return loop(s);
|
|
21
|
+
};
|
|
22
|
+
const loop = (s)=>{
|
|
23
|
+
while(!s.scanner.finalized){
|
|
24
|
+
next(s);
|
|
25
|
+
}
|
|
26
|
+
return s.ejectFinalizedRoot();
|
|
27
|
+
};
|
|
28
|
+
const next = (s)=>s.hasRoot() ? parseOperator(s) : parseOperand(s);
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { jsObjects, jsObjectsScope } from "./jsObjects.js";
|
|
2
|
+
import { rootScope, scope } from "./scope.js";
|
|
3
|
+
import { tsKeywords, tsKeywordsScope } from "./tsKeywords.js";
|
|
4
|
+
import { validation, validationScope } from "./validation/validation.js";
|
|
5
|
+
export const arkScope = scope({}, {
|
|
6
|
+
name: "standard",
|
|
7
|
+
includes: [
|
|
8
|
+
tsKeywords,
|
|
9
|
+
jsObjects,
|
|
10
|
+
validation
|
|
11
|
+
],
|
|
12
|
+
standard: false
|
|
13
|
+
});
|
|
14
|
+
export const ark = arkScope.compile();
|
|
15
|
+
export const scopes = {
|
|
16
|
+
root: rootScope,
|
|
17
|
+
tsKeywords: tsKeywordsScope,
|
|
18
|
+
jsObjects: jsObjectsScope,
|
|
19
|
+
validation: validationScope,
|
|
20
|
+
ark: arkScope
|
|
21
|
+
};
|
|
22
|
+
export const spaces = {
|
|
23
|
+
tsKeywords,
|
|
24
|
+
jsObjects,
|
|
25
|
+
validation,
|
|
26
|
+
ark
|
|
27
|
+
};
|
|
28
|
+
export const type = arkScope.type;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
function _defineProperty(obj, key, value) {
|
|
2
|
+
if (key in obj) {
|
|
3
|
+
Object.defineProperty(obj, key, {
|
|
4
|
+
value: value,
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true
|
|
8
|
+
});
|
|
9
|
+
} else {
|
|
10
|
+
obj[key] = value;
|
|
11
|
+
}
|
|
12
|
+
return obj;
|
|
13
|
+
}
|
|
14
|
+
import { deepFreeze } from "../utils/freeze.js";
|
|
15
|
+
export class Cache {
|
|
16
|
+
get root() {
|
|
17
|
+
return this.cache;
|
|
18
|
+
}
|
|
19
|
+
has(name) {
|
|
20
|
+
return name in this.cache;
|
|
21
|
+
}
|
|
22
|
+
get(name) {
|
|
23
|
+
return this.cache[name];
|
|
24
|
+
}
|
|
25
|
+
set(name, item) {
|
|
26
|
+
this.cache[name] = item;
|
|
27
|
+
return item;
|
|
28
|
+
}
|
|
29
|
+
constructor(){
|
|
30
|
+
_defineProperty(this, "cache", {});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export class FreezingCache extends Cache {
|
|
34
|
+
set(name, item) {
|
|
35
|
+
this.cache[name] = deepFreeze(item);
|
|
36
|
+
return item;
|
|
37
|
+
}
|
|
38
|
+
}
|