arktype 2.0.0-rc.3 → 2.0.0-rc.31

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.
Files changed (176) hide show
  1. package/out/attributes.d.ts +94 -0
  2. package/out/attributes.js +3 -0
  3. package/out/config.d.ts +1 -1
  4. package/out/generic.d.ts +55 -58
  5. package/out/generic.js +6 -8
  6. package/out/index.d.ts +6 -4
  7. package/out/index.js +4 -3
  8. package/out/keywords/{constructors/Array.d.ts → Array.d.ts} +3 -3
  9. package/out/keywords/{constructors/Array.js → Array.js} +7 -7
  10. package/out/keywords/{constructors/FormData.d.ts → FormData.d.ts} +3 -3
  11. package/out/keywords/{constructors/FormData.js → FormData.js} +8 -8
  12. package/out/keywords/{constructors/TypedArray.d.ts → TypedArray.d.ts} +1 -1
  13. package/out/keywords/{constructors/TypedArray.js → TypedArray.js} +2 -2
  14. package/out/keywords/builtins.js +2 -2
  15. package/out/keywords/{constructors/constructors.d.ts → constructors.d.ts} +1 -2
  16. package/out/keywords/{constructors/constructors.js → constructors.js} +2 -2
  17. package/out/keywords/keywords.d.ts +61 -0
  18. package/out/keywords/keywords.js +29 -0
  19. package/out/keywords/number.d.ts +23 -0
  20. package/out/keywords/{number/epoch.js → number.js} +24 -2
  21. package/out/keywords/string.d.ts +327 -0
  22. package/out/keywords/string.js +398 -0
  23. package/out/keywords/ts.d.ts +42 -4
  24. package/out/keywords/ts.js +24 -7
  25. package/out/methods/array.d.ts +12 -0
  26. package/out/methods/base.d.ts +178 -0
  27. package/out/methods/date.d.ts +10 -0
  28. package/out/methods/instantiate.d.ts +17 -0
  29. package/out/methods/morph.d.ts +12 -0
  30. package/out/methods/number.d.ts +11 -0
  31. package/out/methods/object.d.ts +106 -0
  32. package/out/methods/string.d.ts +12 -0
  33. package/out/module.d.ts +2 -2
  34. package/out/parser/{semantic → ast}/bounds.d.ts +3 -3
  35. package/out/parser/ast/default.d.ts +8 -0
  36. package/out/parser/ast/generic.d.ts +17 -0
  37. package/out/parser/ast/infer.d.ts +25 -0
  38. package/out/parser/{semantic → ast}/utils.d.ts +1 -1
  39. package/out/parser/ast/validate.d.ts +30 -0
  40. package/out/parser/ast/validate.js +3 -0
  41. package/out/parser/definition.d.ts +21 -13
  42. package/out/parser/definition.js +20 -2
  43. package/out/parser/objectLiteral.d.ts +41 -67
  44. package/out/parser/objectLiteral.js +84 -71
  45. package/out/parser/property.d.ts +36 -0
  46. package/out/parser/property.js +16 -0
  47. package/out/parser/{string/reduce → reduce}/dynamic.d.ts +10 -13
  48. package/out/parser/{string/reduce → reduce}/dynamic.js +5 -6
  49. package/out/parser/{string/reduce → reduce}/shared.d.ts +1 -1
  50. package/out/parser/{string/reduce → reduce}/static.d.ts +4 -4
  51. package/out/parser/{string/shift → shift}/operand/date.d.ts +3 -1
  52. package/out/parser/{string/shift → shift}/operand/enclosed.d.ts +4 -4
  53. package/out/parser/{string/shift → shift}/operand/enclosed.js +7 -3
  54. package/out/parser/shift/operand/operand.d.ts +9 -0
  55. package/out/parser/{string/shift → shift}/operand/operand.js +2 -2
  56. package/out/parser/{string/shift → shift}/operand/unenclosed.d.ts +15 -7
  57. package/out/parser/{string/shift → shift}/operand/unenclosed.js +12 -7
  58. package/out/parser/{string/shift → shift}/operator/bounds.d.ts +7 -7
  59. package/out/parser/{string/shift → shift}/operator/bounds.js +5 -4
  60. package/out/parser/shift/operator/brand.d.ts +6 -0
  61. package/out/parser/shift/operator/brand.js +5 -0
  62. package/out/parser/{string/shift → shift}/operator/default.d.ts +4 -6
  63. package/out/parser/{string/shift → shift}/operator/default.js +4 -6
  64. package/out/parser/{string/shift → shift}/operator/divisor.d.ts +2 -2
  65. package/out/parser/shift/operator/operator.d.ts +14 -0
  66. package/out/parser/{string/shift → shift}/operator/operator.js +7 -5
  67. package/out/parser/shift/scanner.d.ts +47 -0
  68. package/out/parser/shift/scanner.js +43 -0
  69. package/out/parser/{string/string.d.ts → string.d.ts} +9 -9
  70. package/out/parser/{string/string.js → string.js} +19 -0
  71. package/out/parser/tupleExpressions.d.ts +47 -0
  72. package/out/parser/tupleExpressions.js +67 -0
  73. package/out/parser/tupleLiteral.d.ts +95 -0
  74. package/out/parser/tupleLiteral.js +122 -0
  75. package/out/scope.d.ts +41 -34
  76. package/out/scope.js +54 -62
  77. package/out/type.d.ts +46 -22
  78. package/out/type.js +15 -11
  79. package/package.json +3 -3
  80. package/out/intersect.d.ts +0 -28
  81. package/out/keys.d.ts +0 -16
  82. package/out/keywords/ark.d.ts +0 -39
  83. package/out/keywords/ark.js +0 -24
  84. package/out/keywords/ast.d.ts +0 -193
  85. package/out/keywords/ast.js +0 -2
  86. package/out/keywords/number/epoch.d.ts +0 -10
  87. package/out/keywords/number/integer.d.ts +0 -3
  88. package/out/keywords/number/integer.js +0 -5
  89. package/out/keywords/number/number.d.ts +0 -28
  90. package/out/keywords/number/number.js +0 -9
  91. package/out/keywords/string/alpha.d.ts +0 -7
  92. package/out/keywords/string/alpha.js +0 -2
  93. package/out/keywords/string/alphanumeric.d.ts +0 -7
  94. package/out/keywords/string/alphanumeric.js +0 -2
  95. package/out/keywords/string/capitalize.d.ts +0 -15
  96. package/out/keywords/string/capitalize.js +0 -12
  97. package/out/keywords/string/creditCard.d.ts +0 -8
  98. package/out/keywords/string/creditCard.js +0 -37
  99. package/out/keywords/string/date.d.ts +0 -59
  100. package/out/keywords/string/date.js +0 -98
  101. package/out/keywords/string/digits.d.ts +0 -7
  102. package/out/keywords/string/digits.js +0 -2
  103. package/out/keywords/string/email.d.ts +0 -7
  104. package/out/keywords/string/email.js +0 -4
  105. package/out/keywords/string/integer.d.ts +0 -16
  106. package/out/keywords/string/integer.js +0 -16
  107. package/out/keywords/string/ip.d.ts +0 -20
  108. package/out/keywords/string/ip.js +0 -23
  109. package/out/keywords/string/json.d.ts +0 -15
  110. package/out/keywords/string/json.js +0 -45
  111. package/out/keywords/string/lower.d.ts +0 -15
  112. package/out/keywords/string/lower.js +0 -12
  113. package/out/keywords/string/normalize.d.ts +0 -92
  114. package/out/keywords/string/normalize.js +0 -43
  115. package/out/keywords/string/numeric.d.ts +0 -15
  116. package/out/keywords/string/numeric.js +0 -13
  117. package/out/keywords/string/semver.d.ts +0 -7
  118. package/out/keywords/string/semver.js +0 -4
  119. package/out/keywords/string/string.d.ts +0 -112
  120. package/out/keywords/string/string.js +0 -41
  121. package/out/keywords/string/trim.d.ts +0 -15
  122. package/out/keywords/string/trim.js +0 -14
  123. package/out/keywords/string/upper.d.ts +0 -15
  124. package/out/keywords/string/upper.js +0 -12
  125. package/out/keywords/string/url.d.ts +0 -15
  126. package/out/keywords/string/url.js +0 -37
  127. package/out/keywords/string/utils.d.ts +0 -2
  128. package/out/keywords/string/utils.js +0 -10
  129. package/out/keywords/string/uuid.d.ts +0 -42
  130. package/out/keywords/string/uuid.js +0 -19
  131. package/out/keywords/utils.d.ts +0 -1
  132. package/out/keywords/utils.js +0 -2
  133. package/out/parser/semantic/default.d.ts +0 -9
  134. package/out/parser/semantic/default.js +0 -1
  135. package/out/parser/semantic/infer.d.ts +0 -39
  136. package/out/parser/semantic/validate.d.ts +0 -26
  137. package/out/parser/semantic/validate.js +0 -1
  138. package/out/parser/string/shift/operand/operand.d.ts +0 -9
  139. package/out/parser/string/shift/operator/operator.d.ts +0 -13
  140. package/out/parser/string/shift/scanner.d.ts +0 -69
  141. package/out/parser/string/shift/scanner.js +0 -100
  142. package/out/parser/tuple.d.ts +0 -132
  143. package/out/parser/tuple.js +0 -158
  144. package/out/subtypes/array.d.ts +0 -13
  145. package/out/subtypes/base.d.ts +0 -85
  146. package/out/subtypes/date.d.ts +0 -11
  147. package/out/subtypes/instantiate.d.ts +0 -16
  148. package/out/subtypes/morph.d.ts +0 -13
  149. package/out/subtypes/number.d.ts +0 -12
  150. package/out/subtypes/object.d.ts +0 -33
  151. package/out/subtypes/string.d.ts +0 -13
  152. package/out/subtypes/validator.d.ts +0 -11
  153. package/out/subtypes/validator.js +0 -1
  154. /package/out/{subtypes → methods}/array.js +0 -0
  155. /package/out/{subtypes → methods}/base.js +0 -0
  156. /package/out/{subtypes → methods}/date.js +0 -0
  157. /package/out/{subtypes → methods}/instantiate.js +0 -0
  158. /package/out/{subtypes → methods}/morph.js +0 -0
  159. /package/out/{subtypes → methods}/number.js +0 -0
  160. /package/out/{subtypes → methods}/object.js +0 -0
  161. /package/out/{subtypes → methods}/string.js +0 -0
  162. /package/out/parser/{semantic → ast}/bounds.js +0 -0
  163. /package/out/{intersect.js → parser/ast/default.js} +0 -0
  164. /package/out/parser/{semantic → ast}/divisor.d.ts +0 -0
  165. /package/out/parser/{semantic → ast}/divisor.js +0 -0
  166. /package/out/{keys.js → parser/ast/generic.js} +0 -0
  167. /package/out/parser/{semantic → ast}/infer.js +0 -0
  168. /package/out/parser/{semantic → ast}/keyof.d.ts +0 -0
  169. /package/out/parser/{semantic → ast}/keyof.js +0 -0
  170. /package/out/parser/{semantic → ast}/utils.js +0 -0
  171. /package/out/parser/{string/reduce → reduce}/shared.js +0 -0
  172. /package/out/parser/{string/reduce → reduce}/static.js +0 -0
  173. /package/out/parser/{string/shift → shift}/operand/date.js +0 -0
  174. /package/out/parser/{string/shift → shift}/operand/genericArgs.d.ts +0 -0
  175. /package/out/parser/{string/shift → shift}/operand/genericArgs.js +0 -0
  176. /package/out/parser/{string/shift → shift}/operator/divisor.js +0 -0
@@ -1,18 +1,20 @@
1
1
  import { writeUnresolvableMessage, type BaseRoot, type GenericAst, type GenericRoot, type arkKind, type genericParamNames, type resolvableReferenceIn, type writeNonSubmoduleDotMessage } from "@ark/schema";
2
2
  import { type BigintLiteral, type NumberLiteral, type join, type lastOf } from "@ark/util";
3
- import type { ArkAmbient } from "../../../../config.ts";
4
- import type { resolutionToAst } from "../../../../scope.ts";
5
- import type { GenericInstantiationAst, InferredAst } from "../../../semantic/infer.ts";
3
+ import type { ArkAmbient } from "../../../config.ts";
4
+ import type { resolutionToAst } from "../../../scope.ts";
5
+ import type { GenericInstantiationAst } from "../../ast/generic.ts";
6
+ import type { InferredAst } from "../../ast/infer.ts";
7
+ import { writePrefixedPrivateReferenceMessage } from "../../ast/validate.ts";
6
8
  import type { DynamicState } from "../../reduce/dynamic.ts";
7
9
  import type { StaticState, state } from "../../reduce/static.ts";
8
10
  import type { BaseCompletions } from "../../string.ts";
9
- import type { Scanner } from "../scanner.ts";
11
+ import type { ArkTypeScanner } from "../scanner.ts";
10
12
  import { parseGenericArgs, writeInvalidGenericArgCountMessage, type ParsedArgs } from "./genericArgs.ts";
11
13
  export declare const parseUnenclosed: (s: DynamicState) => void;
12
- export type parseUnenclosed<s extends StaticState, $, args> = Scanner.shiftUntilNextTerminator<s["unscanned"]> extends (Scanner.shiftResult<infer token, infer unscanned>) ? tryResolve<s, unscanned, token, $, args> extends state.from<infer s> ? s : never : never;
14
+ export type parseUnenclosed<s extends StaticState, $, args> = ArkTypeScanner.shiftUntilNextTerminator<s["unscanned"]> extends (ArkTypeScanner.shiftResult<infer token, infer unscanned>) ? tryResolve<s, unscanned, token, $, args> extends state.from<infer s> ? s : never : never;
13
15
  type parseResolution<s extends StaticState, unscanned extends string, alias extends string, resolution, $, args> = resolutionToAst<alias, resolution> extends infer ast ? ast extends GenericAst ? parseGenericInstantiation<alias, ast, state.scanTo<s, unscanned>, $, args> : state.setRoot<s, ast, unscanned> : never;
14
16
  export declare const parseGenericInstantiation: (name: string, g: GenericRoot, s: DynamicState) => BaseRoot;
15
- export type parseGenericInstantiation<name extends string, g extends GenericAst, s extends StaticState, $, args> = Scanner.skipWhitespace<s["unscanned"]> extends `<${infer unscanned}` ? parseGenericArgs<name, g, unscanned, $, args> extends infer result ? result extends ParsedArgs<infer argAsts, infer nextUnscanned> ? state.setRoot<s, GenericInstantiationAst<g, argAsts>, nextUnscanned> : result : never : state.error<writeInvalidGenericArgCountMessage<name, genericParamNames<g["paramsAst"]>, [
17
+ export type parseGenericInstantiation<name extends string, g extends GenericAst, s extends StaticState, $, args> = ArkTypeScanner.skipWhitespace<s["unscanned"]> extends `<${infer unscanned}` ? parseGenericArgs<name, g, unscanned, $, args> extends infer result ? result extends ParsedArgs<infer argAsts, infer nextUnscanned> ? state.setRoot<s, GenericInstantiationAst<g, argAsts>, nextUnscanned> : result : never : state.error<writeInvalidGenericArgCountMessage<name, genericParamNames<g["paramsAst"]>, [
16
18
  ]>>;
17
19
  type tryResolve<s extends StaticState, unscanned extends string, token extends string, $, args> = token extends keyof args ? parseResolution<s, unscanned, token, args[token], $, args> : token extends keyof $ ? parseResolution<s, unscanned, token, $[token], $, args> : `#${token}` extends keyof $ ? parseResolution<s, unscanned, token, $[`#${token}`], $, args> : token extends keyof ArkAmbient.$ ? parseResolution<s, unscanned, token, ArkAmbient.$[token], $, args> : token extends NumberLiteral<infer n> ? state.setRoot<s, InferredAst<n, token>, unscanned> : token extends (`${infer submodule extends keyof $ & string}.${infer reference}`) ? tryResolveSubmodule<token, $[submodule], reference, s, unscanned, $, args, [
18
20
  submodule
@@ -27,7 +29,13 @@ type tryResolveSubmodule<token extends string, resolution, reference extends str
27
29
  ]> : unresolvableState<s, reference, resolution, {}, submodulePath> : state.error<writeNonSubmoduleDotMessage<lastOf<submodulePath>>>;
28
30
  /** Provide valid completions for the current token, or fallback to an
29
31
  * unresolvable error if there are none */
30
- export type unresolvableState<s extends StaticState, token extends string, resolutions, args, submodulePath extends string[]> = validReferenceFromToken<token, resolutions, args, submodulePath> extends (never) ? state.error<writeUnresolvableMessage<qualifiedReference<token, submodulePath>>> : state.completion<`${s["scanned"]}${qualifiedReference<validReferenceFromToken<token, resolutions, args, submodulePath>, submodulePath>}`>;
32
+ export type unresolvableState<s extends StaticState, token extends string, resolutions, args, submodulePath extends string[]> = [
33
+ token,
34
+ s["unscanned"]
35
+ ] extends ([
36
+ "",
37
+ ArkTypeScanner.shift<"#", infer unscanned>
38
+ ]) ? ArkTypeScanner.shiftUntilNextTerminator<unscanned> extends (ArkTypeScanner.shiftResult<infer name, string>) ? state.error<writePrefixedPrivateReferenceMessage<name>> : never : validReferenceFromToken<token, resolutions, args, submodulePath> extends (never) ? state.error<writeUnresolvableMessage<qualifiedReference<token, submodulePath>>> : state.completion<`${s["scanned"]}${qualifiedReference<validReferenceFromToken<token, resolutions, args, submodulePath>, submodulePath>}`>;
31
39
  type qualifiedReference<reference extends string, submodulePath extends string[]> = join<[...submodulePath, reference], ".">;
32
40
  type validReferenceFromToken<token extends string, $, args, submodulePath extends string[]> = Extract<submodulePath["length"] extends 0 ? BaseCompletions<$, args> : resolvableReferenceIn<$>, `${token}${string}`>;
33
41
  export declare const writeMissingOperandMessage: (s: DynamicState) => string;
@@ -1,6 +1,6 @@
1
1
  import { hasArkKind, writeUnresolvableMessage } from "@ark/schema";
2
2
  import { printable, throwParseError, tryParseWellFormedBigint, tryParseWellFormedNumber } from "@ark/util";
3
- import { writePrefixedPrivateReferenceMessage } from "../../../semantic/validate.js";
3
+ import { writePrefixedPrivateReferenceMessage } from "../../ast/validate.js";
4
4
  import { parseGenericArgs, writeInvalidGenericArgCountMessage } from "./genericArgs.js";
5
5
  export const parseUnenclosed = (s) => {
6
6
  const token = s.scanner.shiftUntilNextTerminator();
@@ -19,13 +19,18 @@ export const parseGenericInstantiation = (name, g, s) => {
19
19
  };
20
20
  const unenclosedToNode = (s, token) => maybeParseReference(s, token) ??
21
21
  maybeParseUnenclosedLiteral(s, token) ??
22
- s.error(token === "" ? writeMissingOperandMessage(s)
23
- : token[0] === "#" ?
24
- writePrefixedPrivateReferenceMessage(token)
25
- : writeUnresolvableMessage(token));
22
+ s.error(token === "" ?
23
+ s.scanner.lookahead === "#" ?
24
+ writePrefixedPrivateReferenceMessage(s.shiftedByOne().scanner.shiftUntilNextTerminator())
25
+ : writeMissingOperandMessage(s)
26
+ : writeUnresolvableMessage(token));
26
27
  const maybeParseReference = (s, token) => {
27
- if (s.ctx.args?.[token])
28
- return s.ctx.args[token].internal;
28
+ if (s.ctx.args?.[token]) {
29
+ const arg = s.ctx.args[token];
30
+ if (typeof arg !== "string")
31
+ return arg;
32
+ return s.ctx.$.node("alias", { reference: arg }, { prereduced: true });
33
+ }
29
34
  const resolution = s.ctx.$.maybeResolve(token);
30
35
  if (hasArkKind(resolution, "root"))
31
36
  return resolution;
@@ -1,18 +1,18 @@
1
1
  import { type BaseRoot, type BoundKind } from "@ark/schema";
2
- import { type keySet } from "@ark/util";
3
- import type { DateLiteral } from "../../../../keywords/ast.ts";
4
- import type { InferredAst } from "../../../semantic/infer.ts";
5
- import type { astToString } from "../../../semantic/utils.ts";
2
+ import { type KeySet } from "@ark/util";
3
+ import type { DateLiteral } from "../../../attributes.ts";
4
+ import type { InferredAst } from "../../ast/infer.ts";
5
+ import type { astToString } from "../../ast/utils.ts";
6
6
  import type { DynamicState, DynamicStateWithRoot } from "../../reduce/dynamic.ts";
7
7
  import { writeUnpairableComparatorMessage, type Comparator, type InvertedComparators, type MaxComparator } from "../../reduce/shared.ts";
8
8
  import type { state, StaticState } from "../../reduce/static.ts";
9
9
  import type { parseOperand } from "../operand/operand.ts";
10
- import type { Scanner } from "../scanner.ts";
10
+ import type { ArkTypeScanner } from "../scanner.ts";
11
11
  export declare const parseBound: (s: DynamicStateWithRoot, start: ComparatorStartChar) => void;
12
- export type parseBound<s extends StaticState, start extends ComparatorStartChar, unscanned extends string, $, args> = shiftComparator<start, unscanned> extends infer shiftResultOrError ? shiftResultOrError extends (Scanner.shiftResult<infer comparator extends Comparator, infer nextUnscanned>) ? s["root"] extends (InferredAst<Date | number, `${infer limit extends number | DateLiteral}`>) ? state.reduceLeftBound<s, limit, comparator, nextUnscanned> : parseRightBound<state.scanTo<s, nextUnscanned>, comparator, $, args> : shiftResultOrError : never;
12
+ export type parseBound<s extends StaticState, start extends ComparatorStartChar, unscanned extends string, $, args> = shiftComparator<start, unscanned> extends infer shiftResultOrError ? shiftResultOrError extends (ArkTypeScanner.shiftResult<infer comparator extends Comparator, infer nextUnscanned>) ? s["root"] extends (InferredAst<Date | number, `${infer limit extends number | DateLiteral}`>) ? state.reduceLeftBound<s, limit, comparator, nextUnscanned> : parseRightBound<state.scanTo<s, nextUnscanned>, comparator, $, args> : shiftResultOrError : never;
13
13
  type OneCharComparator = ">" | "<";
14
14
  export type ComparatorStartChar = Comparator extends `${infer char}${string}` ? char : never;
15
- export declare const comparatorStartChars: keySet<ComparatorStartChar>;
15
+ export declare const comparatorStartChars: KeySet<ComparatorStartChar>;
16
16
  declare const shiftComparator: (s: DynamicState, start: ComparatorStartChar) => Comparator;
17
17
  type shiftComparator<start extends ComparatorStartChar, unscanned extends string> = unscanned extends `=${infer nextUnscanned}` ? [`${start}=`, nextUnscanned] : [start & OneCharComparator, unscanned];
18
18
  export declare const writeIncompatibleRangeMessage: (l: BoundKind, r: BoundKind) => string;
@@ -41,7 +41,7 @@ export const getBoundKinds = (comparator, limit, root, boundKind) => {
41
41
  if (typeof limit !== "number") {
42
42
  return throwParseError(writeInvalidLimitMessage(comparator, limit, boundKind));
43
43
  }
44
- return (comparator === "==" ? ["minLength", "maxLength"]
44
+ return (comparator === "==" ? ["exactLength"]
45
45
  : comparator[0] === ">" ? ["minLength"]
46
46
  : ["maxLength"]);
47
47
  }
@@ -75,9 +75,10 @@ export const parseRightBound = (s, comparator) => {
75
75
  const limit = limitNode.unit;
76
76
  // apply the newly-parsed right bound
77
77
  const exclusive = comparator.length === 1;
78
- // if the comparator is ==, both the min and max of that pair will be applied
79
- for (const kind of getBoundKinds(comparator, typeof limit === "number" ? limit : limitToken, previousRoot, "right"))
80
- s.constrainRoot(kind, { rule: limit, exclusive });
78
+ const boundKinds = getBoundKinds(comparator, typeof limit === "number" ? limit : limitToken, previousRoot, "right");
79
+ for (const kind of boundKinds) {
80
+ s.constrainRoot(kind, comparator === "==" ? { rule: limit } : { rule: limit, exclusive });
81
+ }
81
82
  if (!s.branches.leftBound)
82
83
  return;
83
84
  // if there's an open left bound, perform additional validation and apply it
@@ -0,0 +1,6 @@
1
+ import type { emptyBrandNameMessage } from "@ark/schema";
2
+ import type { DynamicStateWithRoot } from "../../reduce/dynamic.ts";
3
+ import type { StaticState, state } from "../../reduce/static.ts";
4
+ import type { ArkTypeScanner } from "../scanner.ts";
5
+ export declare const parseBrand: (s: DynamicStateWithRoot) => void;
6
+ export type parseBrand<s extends StaticState, unscanned extends string> = ArkTypeScanner.shiftUntilNextTerminator<ArkTypeScanner.skipWhitespace<unscanned>> extends (ArkTypeScanner.shiftResult<`${infer brandName}`, infer nextUnscanned>) ? brandName extends "" ? state.error<emptyBrandNameMessage> : state.setRoot<s, [s["root"], "#", brandName], nextUnscanned> : never;
@@ -0,0 +1,5 @@
1
+ export const parseBrand = (s) => {
2
+ s.scanner.shiftUntilNonWhitespace();
3
+ const brandName = s.scanner.shiftUntilNextTerminator();
4
+ s.root = s.root.brand(brandName);
5
+ };
@@ -1,12 +1,12 @@
1
1
  import type { BaseRoot } from "@ark/schema";
2
- import { type BigintLiteral, type ErrorMessage, type NumberLiteral, type trim } from "@ark/util";
3
- import type { DateLiteral } from "../../../../keywords/ast.ts";
2
+ import type { BigintLiteral, ErrorMessage, NumberLiteral, trim } from "@ark/util";
3
+ import type { DateLiteral } from "../../../attributes.ts";
4
4
  import type { DynamicStateWithRoot } from "../../reduce/dynamic.ts";
5
5
  import type { StringLiteral } from "../operand/enclosed.ts";
6
6
  type UnitLiteralKeyword = "null" | "undefined" | "true" | "false";
7
7
  export type UnitLiteral = StringLiteral | BigintLiteral | NumberLiteral | DateLiteral | UnitLiteralKeyword;
8
- export type ParsedDefault = [BaseRoot, "=", unknown];
9
- export declare const parseDefault: (s: DynamicStateWithRoot) => ParsedDefault;
8
+ export type ParsedDefaultableProperty = readonly [BaseRoot, "=", unknown];
9
+ export declare const parseDefault: (s: DynamicStateWithRoot) => ParsedDefaultableProperty;
10
10
  export type parseDefault<root, unscanned extends string> = trim<unscanned> extends infer defaultValue extends UnitLiteral ? [
11
11
  root,
12
12
  "=",
@@ -14,6 +14,4 @@ export type parseDefault<root, unscanned extends string> = trim<unscanned> exten
14
14
  ] : ErrorMessage<writeNonLiteralDefaultMessage<trim<unscanned>>>;
15
15
  export declare const writeNonLiteralDefaultMessage: <defaultDef extends string>(defaultDef: defaultDef) => writeNonLiteralDefaultMessage<defaultDef>;
16
16
  export type writeNonLiteralDefaultMessage<defaultDef extends string> = `Default value '${defaultDef}' must a literal value`;
17
- export declare const shallowDefaultMessage = "Default values must be specified on objects like { isAdmin: 'boolean = false' }";
18
- export type shallowDefaultMessage = typeof shallowDefaultMessage;
19
17
  export {};
@@ -1,7 +1,4 @@
1
- import { throwParseError } from "@ark/util";
2
1
  export const parseDefault = (s) => {
3
- if (!s.defaultable)
4
- return throwParseError(shallowDefaultMessage);
5
2
  // store the node that will be bounded
6
3
  const baseNode = s.unsetRoot();
7
4
  s.parseOperand();
@@ -10,8 +7,9 @@ export const parseDefault = (s) => {
10
7
  // token from which it was parsed
11
8
  if (!defaultNode.hasKind("unit"))
12
9
  return s.error(writeNonLiteralDefaultMessage(defaultNode.expression));
13
- // assignability is checked in parseEntries
14
- return [baseNode, "=", defaultNode.unit];
10
+ const defaultValue = defaultNode.unit instanceof Date ?
11
+ () => new Date(defaultNode.unit)
12
+ : defaultNode.unit;
13
+ return [baseNode, "=", defaultValue];
15
14
  };
16
15
  export const writeNonLiteralDefaultMessage = (defaultDef) => `Default value '${defaultDef}' must a literal value`;
17
- export const shallowDefaultMessage = `Default values must be specified on objects like { isAdmin: 'boolean = false' }`;
@@ -1,7 +1,7 @@
1
1
  import type { DynamicStateWithRoot } from "../../reduce/dynamic.ts";
2
2
  import type { StaticState, state } from "../../reduce/static.ts";
3
- import type { Scanner } from "../scanner.ts";
3
+ import type { ArkTypeScanner } from "../scanner.ts";
4
4
  export declare const parseDivisor: (s: DynamicStateWithRoot) => void;
5
- export type parseDivisor<s extends StaticState, unscanned extends string> = Scanner.shiftUntilNextTerminator<Scanner.skipWhitespace<unscanned>> extends (Scanner.shiftResult<infer scanned, infer nextUnscanned>) ? scanned extends `${infer divisor extends number}` ? divisor extends 0 ? state.error<writeInvalidDivisorMessage<0>> : state.setRoot<s, [s["root"], "%", divisor], nextUnscanned> : state.error<writeInvalidDivisorMessage<scanned>> : never;
5
+ export type parseDivisor<s extends StaticState, unscanned extends string> = ArkTypeScanner.shiftUntilNextTerminator<ArkTypeScanner.skipWhitespace<unscanned>> extends ArkTypeScanner.shiftResult<infer scanned, infer nextUnscanned> ? scanned extends `${infer divisor extends number}` ? divisor extends 0 ? state.error<writeInvalidDivisorMessage<0>> : state.setRoot<s, [s["root"], "%", divisor], nextUnscanned> : state.error<writeInvalidDivisorMessage<scanned>> : never;
6
6
  export declare const writeInvalidDivisorMessage: <divisor extends string | number>(divisor: divisor) => writeInvalidDivisorMessage<divisor>;
7
7
  export type writeInvalidDivisorMessage<divisor extends string | number> = `% operator must be followed by a non-zero integer literal (was ${divisor})`;
@@ -0,0 +1,14 @@
1
+ import { type WhitespaceChar } from "@ark/util";
2
+ import type { DynamicStateWithRoot } from "../../reduce/dynamic.ts";
3
+ import type { StaticState, state } from "../../reduce/static.ts";
4
+ import { ArkTypeScanner } from "../scanner.ts";
5
+ import { parseBound, type ComparatorStartChar } from "./bounds.ts";
6
+ import { parseBrand } from "./brand.ts";
7
+ import { parseDivisor } from "./divisor.ts";
8
+ export declare const parseOperator: (s: DynamicStateWithRoot) => void;
9
+ export type parseOperator<s extends StaticState, $, args> = s["unscanned"] extends (ArkTypeScanner.shift<infer lookahead, infer unscanned>) ? lookahead extends "[" ? unscanned extends ArkTypeScanner.shift<"]", infer nextUnscanned> ? state.setRoot<s, [s["root"], "[]"], nextUnscanned> : state.error<incompleteArrayTokenMessage> : lookahead extends "|" | "&" ? state.reduceBranch<s, lookahead, unscanned> : lookahead extends ")" ? state.finalizeGroup<s, unscanned> : ArkTypeScanner.lookaheadIsFinalizing<lookahead, unscanned> extends true ? state.finalize<state.scanTo<s, unscanned>, lookahead & ArkTypeScanner.FinalizingLookahead> : lookahead extends ComparatorStartChar ? parseBound<s, lookahead, unscanned, $, args> : lookahead extends "%" ? parseDivisor<s, unscanned> : lookahead extends "#" ? parseBrand<s, unscanned> : lookahead extends WhitespaceChar ? parseOperator<state.scanTo<s, unscanned>, $, args> : state.error<writeUnexpectedCharacterMessage<lookahead>> : state.finalize<s, "">;
10
+ export declare const writeUnexpectedCharacterMessage: <char extends string, shouldBe extends string>(char: char, shouldBe?: shouldBe) => writeUnexpectedCharacterMessage<char, shouldBe>;
11
+ export type writeUnexpectedCharacterMessage<char extends string, shouldBe extends string = ""> = `'${char}' is not allowed here${shouldBe extends "" ? "" : ` (should be ${shouldBe})`}`;
12
+ export declare const incompleteArrayTokenMessage = "Missing expected ']'";
13
+ type incompleteArrayTokenMessage = typeof incompleteArrayTokenMessage;
14
+ export {};
@@ -1,6 +1,7 @@
1
- import { isKeyOf, whiteSpaceTokens } from "@ark/util";
2
- import { Scanner } from "../scanner.js";
1
+ import { isKeyOf, whitespaceChars } from "@ark/util";
2
+ import { ArkTypeScanner } from "../scanner.js";
3
3
  import { comparatorStartChars, parseBound } from "./bounds.js";
4
+ import { parseBrand } from "./brand.js";
4
5
  import { parseDivisor } from "./divisor.js";
5
6
  export const parseOperator = (s) => {
6
7
  const lookahead = s.scanner.shift();
@@ -11,12 +12,13 @@ export const parseOperator = (s) => {
11
12
  : s.error(incompleteArrayTokenMessage)
12
13
  : lookahead === "|" || lookahead === "&" ? s.pushRootToBranch(lookahead)
13
14
  : lookahead === ")" ? s.finalizeGroup()
14
- : Scanner.lookaheadIsFinalizing(lookahead, s.scanner.unscanned) ?
15
+ : ArkTypeScanner.lookaheadIsFinalizing(lookahead, s.scanner.unscanned) ?
15
16
  s.finalize(lookahead)
16
17
  : isKeyOf(lookahead, comparatorStartChars) ? parseBound(s, lookahead)
17
18
  : lookahead === "%" ? parseDivisor(s)
18
- : lookahead in whiteSpaceTokens ? parseOperator(s)
19
- : s.error(writeUnexpectedCharacterMessage(lookahead)));
19
+ : lookahead === "#" ? parseBrand(s)
20
+ : lookahead in whitespaceChars ? parseOperator(s)
21
+ : s.error(writeUnexpectedCharacterMessage(lookahead)));
20
22
  };
21
23
  export const writeUnexpectedCharacterMessage = (char, shouldBe = "") => `'${char}' is not allowed here${shouldBe && ` (should be ${shouldBe})`}`;
22
24
  export const incompleteArrayTokenMessage = `Missing expected ']'`;
@@ -0,0 +1,47 @@
1
+ import { Scanner, type EscapeChar, type WhitespaceChar } from "@ark/util";
2
+ import type { Comparator } from "../reduce/shared.ts";
3
+ export declare class ArkTypeScanner<lookahead extends string = string> extends Scanner<lookahead> {
4
+ shiftUntilNextTerminator(): string;
5
+ static terminatingChars: {
6
+ readonly " ": 1;
7
+ readonly "\n": 1;
8
+ readonly "\t": 1;
9
+ readonly "<": 1;
10
+ readonly ">": 1;
11
+ readonly "=": 1;
12
+ readonly "|": 1;
13
+ readonly "&": 1;
14
+ readonly ")": 1;
15
+ readonly "[": 1;
16
+ readonly "%": 1;
17
+ readonly ",": 1;
18
+ readonly ":": 1;
19
+ readonly "?": 1;
20
+ readonly "#": 1;
21
+ };
22
+ static finalizingLookaheads: {
23
+ readonly ">": 1;
24
+ readonly ",": 1;
25
+ readonly "": 1;
26
+ readonly "=": 1;
27
+ readonly "?": 1;
28
+ };
29
+ static lookaheadIsFinalizing: (lookahead: string, unscanned: string) => lookahead is ">" | "," | "=" | "?";
30
+ }
31
+ export declare namespace ArkTypeScanner {
32
+ type lookaheadIsFinalizing<lookahead extends string, unscanned extends string> = lookahead extends ">" ? unscanned extends `=${infer nextUnscanned}` ? nextUnscanned extends `=${string}` ? true : false : ArkTypeScanner.skipWhitespace<unscanned> extends ("" | `${TerminatingChar}${string}`) ? true : false : lookahead extends "=" ? unscanned extends `=${string}` ? false : true : lookahead extends "," | "?" ? true : false;
33
+ type TerminatingChar = keyof typeof ArkTypeScanner.terminatingChars;
34
+ type FinalizingLookahead = keyof typeof ArkTypeScanner.finalizingLookaheads;
35
+ type InfixToken = Comparator | "|" | "&" | "%" | ":" | "=>" | "#" | "@" | "=";
36
+ type PostfixToken = "[]" | "?";
37
+ type OperatorToken = InfixToken | PostfixToken;
38
+ type shift<lookahead extends string, unscanned extends string> = `${lookahead}${unscanned}`;
39
+ type shiftUntil<unscanned extends string, terminator extends string, scanned extends string = ""> = unscanned extends shift<infer lookahead, infer nextUnscanned> ? lookahead extends terminator ? scanned extends `${infer base}${EscapeChar}` ? shiftUntil<nextUnscanned, terminator, `${base}${lookahead}`> : [scanned, unscanned] : shiftUntil<nextUnscanned, terminator, `${scanned}${lookahead}`> : [scanned, ""];
40
+ type shiftUntilNot<unscanned extends string, nonTerminator extends string, scanned extends string = ""> = unscanned extends shift<infer lookahead, infer nextUnscanned> ? lookahead extends nonTerminator ? shiftUntilNot<nextUnscanned, nonTerminator, `${scanned}${lookahead}`> : [scanned, unscanned] : [scanned, ""];
41
+ type shiftUntilNextTerminator<unscanned extends string> = shiftUntil<unscanned, TerminatingChar>;
42
+ type skipWhitespace<unscanned extends string> = shiftUntilNot<unscanned, WhitespaceChar>[1];
43
+ type shiftResult<scanned extends string, unscanned extends string> = [
44
+ scanned,
45
+ unscanned
46
+ ];
47
+ }
@@ -0,0 +1,43 @@
1
+ import { isKeyOf, Scanner, whitespaceChars } from "@ark/util";
2
+ export class ArkTypeScanner extends Scanner {
3
+ shiftUntilNextTerminator() {
4
+ this.shiftUntilNonWhitespace();
5
+ return this.shiftUntil(() => this.lookahead in ArkTypeScanner.terminatingChars);
6
+ }
7
+ static terminatingChars = {
8
+ "<": 1,
9
+ ">": 1,
10
+ "=": 1,
11
+ "|": 1,
12
+ "&": 1,
13
+ ")": 1,
14
+ "[": 1,
15
+ "%": 1,
16
+ ",": 1,
17
+ ":": 1,
18
+ "?": 1,
19
+ "#": 1,
20
+ ...whitespaceChars
21
+ };
22
+ static finalizingLookaheads = {
23
+ ">": 1,
24
+ ",": 1,
25
+ "": 1,
26
+ "=": 1,
27
+ "?": 1
28
+ };
29
+ static lookaheadIsFinalizing = (lookahead, unscanned) => lookahead === ">" ?
30
+ unscanned[0] === "=" ?
31
+ // >== would only occur in an expression like Array<number>==5
32
+ // otherwise, >= would only occur as part of a bound like number>=5
33
+ unscanned[1] === "="
34
+ // if > is the end of a generic instantiation, the next token will be
35
+ // an operator or the end of the string
36
+ : unscanned.trimStart() === "" ||
37
+ isKeyOf(unscanned.trimStart()[0], ArkTypeScanner.terminatingChars)
38
+ // "=" is a finalizer on its own (representing a default value),
39
+ // but not with a second "=" (an equality comparator)
40
+ : lookahead === "=" ? unscanned[0] !== "="
41
+ // "," and "?" are unambiguously finalizers
42
+ : lookahead === "," || lookahead === "?";
43
+ }
@@ -1,14 +1,15 @@
1
- import type { BaseRoot, resolvableReferenceIn } from "@ark/schema";
1
+ import type { BaseParseContext, resolvableReferenceIn } from "@ark/schema";
2
2
  import { type ErrorMessage } from "@ark/util";
3
- import type { ArkAmbient } from "../../config.ts";
4
- import type { resolutionToAst } from "../../scope.ts";
5
- import type { inferAstRoot } from "../semantic/infer.ts";
6
- import type { DynamicState, DynamicStateWithRoot } from "./reduce/dynamic.ts";
3
+ import type { ArkAmbient } from "../config.ts";
4
+ import type { InnerParseResult, resolutionToAst } from "../scope.ts";
5
+ import type { inferAstRoot } from "./ast/infer.ts";
6
+ import { DynamicState, type DynamicStateWithRoot } from "./reduce/dynamic.ts";
7
7
  import type { StringifiablePrefixOperator } from "./reduce/shared.ts";
8
8
  import type { state, StaticState } from "./reduce/static.ts";
9
9
  import type { parseOperand } from "./shift/operand/operand.ts";
10
- import { parseDefault, type ParsedDefault } from "./shift/operator/default.ts";
10
+ import { parseDefault } from "./shift/operator/default.ts";
11
11
  import { writeUnexpectedCharacterMessage, type parseOperator } from "./shift/operator/operator.ts";
12
+ export declare const parseString: (def: string, ctx: BaseParseContext) => InnerParseResult;
12
13
  /**
13
14
  * Try to parse the definition from right to left using the most common syntax.
14
15
  * This can be much more efficient for simple definitions.
@@ -19,12 +20,11 @@ export type parseString<def extends string, $, args> = def extends keyof $ ? res
19
20
  ] : fullStringParse<state.initialize<def>, $, args> : fullStringParse<state.initialize<def>, $, args>;
20
21
  export type inferString<def extends string, $, args> = inferAstRoot<parseString<def, $, args>, $, args>;
21
22
  export type BaseCompletions<$, args, otherSuggestions extends string = never> = resolvableReferenceIn<$> | resolvableReferenceIn<ArkAmbient.$> | (keyof args & string) | StringifiablePrefixOperator | otherSuggestions;
22
- export type StringParseResult = BaseRoot | ParsedDefault;
23
- export declare const fullStringParse: (s: DynamicState) => StringParseResult;
23
+ export declare const fullStringParse: (s: DynamicState) => InnerParseResult;
24
24
  type fullStringParse<s extends StaticState, $, args> = extractFinalizedResult<parseUntilFinalizer<s, $, args>>;
25
25
  export declare const parseUntilFinalizer: (s: DynamicState) => DynamicStateWithRoot;
26
26
  export type parseUntilFinalizer<s extends StaticState, $, args> = s["finalizer"] extends undefined ? parseUntilFinalizer<next<s, $, args>, $, args> : s;
27
27
  declare const next: (s: DynamicState) => void;
28
28
  type next<s extends StaticState, $, args> = s["root"] extends undefined ? parseOperand<s, $, args> : parseOperator<s, $, args>;
29
- export type extractFinalizedResult<s extends StaticState> = s["finalizer"] extends "" ? s["root"] : s["finalizer"] extends ErrorMessage ? s["finalizer"] : s["finalizer"] extends "=" ? parseDefault<s["root"], s["unscanned"]> : state.error<writeUnexpectedCharacterMessage<`${s["finalizer"]}`>>;
29
+ export type extractFinalizedResult<s extends StaticState> = s["finalizer"] extends "" ? s["root"] : s["finalizer"] extends ErrorMessage ? s["finalizer"] : s["finalizer"] extends "?" ? [s["root"], "?"] : s["finalizer"] extends "=" ? parseDefault<s["root"], s["unscanned"]> : state.error<writeUnexpectedCharacterMessage<`${s["finalizer"]}`>>;
30
30
  export {};
@@ -1,6 +1,23 @@
1
1
  import { throwInternalError, throwParseError } from "@ark/util";
2
+ import { DynamicState } from "./reduce/dynamic.js";
2
3
  import { parseDefault } from "./shift/operator/default.js";
3
4
  import { writeUnexpectedCharacterMessage } from "./shift/operator/operator.js";
5
+ import { ArkTypeScanner } from "./shift/scanner.js";
6
+ export const parseString = (def, ctx) => {
7
+ const aliasResolution = ctx.$.maybeResolveRoot(def);
8
+ if (aliasResolution)
9
+ return aliasResolution;
10
+ const aliasArrayResolution = def.endsWith("[]") ?
11
+ ctx.$.maybeResolveRoot(def.slice(0, -2))?.array()
12
+ : undefined;
13
+ if (aliasArrayResolution)
14
+ return aliasArrayResolution;
15
+ const s = new DynamicState(new ArkTypeScanner(def), ctx);
16
+ const node = fullStringParse(s);
17
+ if (s.finalizer === ">")
18
+ throwParseError(writeUnexpectedCharacterMessage(">"));
19
+ return node;
20
+ };
4
21
  export const fullStringParse = (s) => {
5
22
  s.parseOperand();
6
23
  let result = parseUntilFinalizer(s).root;
@@ -9,6 +26,8 @@ export const fullStringParse = (s) => {
9
26
  }
10
27
  if (s.finalizer === "=")
11
28
  result = parseDefault(s);
29
+ else if (s.finalizer === "?")
30
+ result = [result, "?"];
12
31
  s.scanner.shiftUntilNonWhitespace();
13
32
  if (s.scanner.lookahead) {
14
33
  // throw a parse error if non-whitespace characters made it here without being parsed
@@ -0,0 +1,47 @@
1
+ import { type BaseParseContext, type BaseRoot, type MetaSchema, type Morph, type Predicate, type unwrapDefault } from "@ark/schema";
2
+ import { type array, type BuiltinObjectKind, type Constructor, type Domain, type show } from "@ark/util";
3
+ import type { defaultFor, distill, inferIntersection, inferMorphOut, inferPredicate, Out, withDefault } from "../attributes.ts";
4
+ import type { type } from "../keywords/keywords.ts";
5
+ import type { inferDefinition, validateDefinition } from "./definition.ts";
6
+ import { writeMissingRightOperandMessage } from "./shift/operand/unenclosed.ts";
7
+ import type { ArkTypeScanner } from "./shift/scanner.ts";
8
+ import type { BaseCompletions } from "./string.ts";
9
+ export declare const maybeParseTupleExpression: (def: array, ctx: BaseParseContext) => BaseRoot | null;
10
+ export type maybeValidateTupleExpression<def extends array, $, args> = def extends IndexZeroExpression ? validatePrefixExpression<def, $, args> : def extends IndexOneExpression ? validateIndexOneExpression<def, $, args> : def extends (readonly ["", ...unknown[]] | readonly [unknown, "", ...unknown[]]) ? readonly [
11
+ def[0] extends "" ? BaseCompletions<$, args, IndexZeroOperator | "..."> : def[0],
12
+ def[1] extends "" ? BaseCompletions<$, args, IndexOneOperator | "..."> : def[1]
13
+ ] : null;
14
+ export type inferTupleExpression<def extends TupleExpression, $, args> = def[1] extends "[]" ? inferDefinition<def[0], $, args>[] : def[1] extends "?" ? inferDefinition<def[0], $, args> : def[1] extends "&" ? inferIntersection<inferDefinition<def[0], $, args>, inferDefinition<def[2], $, args>> : def[1] extends "|" ? inferDefinition<def[0], $, args> | inferDefinition<def[2], $, args> : def[1] extends ":" ? inferPredicate<inferDefinition<def[0], $, args>, def[2]> : def[1] extends "=>" ? parseMorph<def[0], def[2], $, args> : def[1] extends "=" ? withDefault<inferDefinition<def[0], $, args>, unwrapDefault<def[2]>> : def[1] extends "@" ? inferDefinition<def[0], $, args> : def extends readonly ["===", ...infer values] ? values[number] : def extends (readonly ["instanceof", ...infer constructors extends Constructor[]]) ? InstanceType<constructors[number]> : def[0] extends "keyof" ? inferKeyOfExpression<def[1], $, args> : never;
15
+ export type validatePrefixExpression<def extends IndexZeroExpression, $, args> = def["length"] extends 1 ? readonly [writeMissingRightOperandMessage<def[0]>] : def[0] extends "keyof" ? readonly [def[0], validateDefinition<def[1], $, args>] : def[0] extends "===" ? readonly [def[0], ...unknown[]] : def[0] extends "instanceof" ? readonly [def[0], ...Constructor[]] : never;
16
+ export type validateIndexOneExpression<def extends IndexOneExpression, $, args> = def[1] extends TuplePostfixOperator ? readonly [validateDefinition<def[0], $, args>, def[1]] : readonly [
17
+ validateDefinition<def[0], $, args>,
18
+ def["length"] extends 2 ? writeMissingRightOperandMessage<def[1]> : def[1],
19
+ def[1] extends "|" ? validateDefinition<def[2], $, args> : def[1] extends "&" ? validateDefinition<def[2], $, args> : def[1] extends ":" ? Predicate<type.infer.Out<def[0], $, args>> : def[1] extends "=>" ? Morph<type.infer.Out<def[0], $, args>> : def[1] extends "=" ? defaultFor<type.infer.In<def[0], $, args>> : def[1] extends "@" ? MetaSchema : validateDefinition<def[2], $, args>
20
+ ];
21
+ export type UnparsedTupleExpressionInput = {
22
+ instanceof: Constructor;
23
+ "===": unknown;
24
+ };
25
+ export type UnparsedTupleOperator = show<keyof UnparsedTupleExpressionInput>;
26
+ export declare const parseKeyOfTuple: PrefixParser<"keyof">;
27
+ export type inferKeyOfExpression<operandDef, $, args> = show<keyof inferDefinition<operandDef, $, args>>;
28
+ export type IndexOneParser<token extends IndexOneOperator> = (def: IndexOneExpression<token>, ctx: BaseParseContext) => BaseRoot;
29
+ export type PrefixParser<token extends IndexZeroOperator> = (def: IndexZeroExpression<token>, ctx: BaseParseContext) => BaseRoot;
30
+ export type TupleExpression = IndexZeroExpression | IndexOneExpression;
31
+ export type TupleExpressionOperator = IndexZeroOperator | IndexOneOperator;
32
+ export type IndexOneOperator = TuplePostfixOperator | TupleInfixOperator;
33
+ export type TuplePostfixOperator = "[]" | "?";
34
+ export type TupleInfixOperator = "&" | "|" | "=>" | "=" | ":" | "@";
35
+ export type IndexOneExpression<token extends IndexOneOperator = IndexOneOperator> = readonly [unknown, token, ...unknown[]];
36
+ export declare const parseMorphTuple: IndexOneParser<"=>">;
37
+ export declare const writeMalformedFunctionalExpressionMessage: (operator: ":" | "=>", value: unknown) => string;
38
+ export type parseMorph<inDef, morph, $, args> = morph extends Morph ? inferMorphOut<morph> extends infer out ? (In: distill.In<inferDefinition<inDef, $, args>>) => Out<out> : never : never;
39
+ export declare const parseNarrowTuple: IndexOneParser<":">;
40
+ export type IndexZeroOperator = "keyof" | "instanceof" | "===";
41
+ export type IndexZeroExpression<token extends IndexZeroOperator = IndexZeroOperator> = readonly [token, ...unknown[]];
42
+ export type InfixExpression = readonly [
43
+ unknown,
44
+ ArkTypeScanner.InfixToken,
45
+ ...unknown[]
46
+ ];
47
+ export declare const writeInvalidConstructorMessage: <actual extends Domain | BuiltinObjectKind>(actual: actual) => string;
@@ -0,0 +1,67 @@
1
+ import { Disjoint, intersectNodesRoot } from "@ark/schema";
2
+ import { objectKindOrDomainOf, throwParseError } from "@ark/util";
3
+ import { shallowDefaultableMessage, shallowOptionalMessage } from "./ast/validate.js";
4
+ import { writeMissingRightOperandMessage } from "./shift/operand/unenclosed.js";
5
+ export const maybeParseTupleExpression = (def, ctx) => isIndexZeroExpression(def) ? prefixParsers[def[0]](def, ctx)
6
+ : isIndexOneExpression(def) ? indexOneParsers[def[1]](def, ctx)
7
+ : null;
8
+ export const parseKeyOfTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[1], ctx).keyof();
9
+ const parseBranchTuple = (def, ctx) => {
10
+ if (def[2] === undefined)
11
+ return throwParseError(writeMissingRightOperandMessage(def[1], ""));
12
+ const l = ctx.$.parseOwnDefinitionFormat(def[0], ctx);
13
+ const r = ctx.$.parseOwnDefinitionFormat(def[2], ctx);
14
+ if (def[1] === "|")
15
+ return ctx.$.node("union", { branches: [l, r] });
16
+ const result = intersectNodesRoot(l, r, ctx.$);
17
+ if (result instanceof Disjoint)
18
+ return result.throw();
19
+ return result;
20
+ };
21
+ const parseArrayTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).array();
22
+ const isIndexOneExpression = (def) => indexOneParsers[def[1]] !== undefined;
23
+ export const parseMorphTuple = (def, ctx) => {
24
+ if (typeof def[2] !== "function") {
25
+ return throwParseError(writeMalformedFunctionalExpressionMessage("=>", def[2]));
26
+ }
27
+ return ctx.$.parseOwnDefinitionFormat(def[0], ctx).pipe(def[2]);
28
+ };
29
+ export const writeMalformedFunctionalExpressionMessage = (operator, value) => `${operator === ":" ? "Narrow" : "Morph"} expression requires a function following '${operator}' (was ${typeof value})`;
30
+ export const parseNarrowTuple = (def, ctx) => {
31
+ if (typeof def[2] !== "function") {
32
+ return throwParseError(writeMalformedFunctionalExpressionMessage(":", def[2]));
33
+ }
34
+ return ctx.$.parseOwnDefinitionFormat(def[0], ctx).constrain("predicate", def[2]);
35
+ };
36
+ const parseAttributeTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).configureShallowDescendants(def[2]);
37
+ const indexOneParsers = {
38
+ "[]": parseArrayTuple,
39
+ "|": parseBranchTuple,
40
+ "&": parseBranchTuple,
41
+ ":": parseNarrowTuple,
42
+ "=>": parseMorphTuple,
43
+ "@": parseAttributeTuple,
44
+ // since object and tuple literals parse there via `parseProperty`,
45
+ // they must be shallow if parsed directly as a tuple expression
46
+ "=": () => throwParseError(shallowDefaultableMessage),
47
+ "?": () => throwParseError(shallowOptionalMessage)
48
+ };
49
+ const prefixParsers = {
50
+ keyof: parseKeyOfTuple,
51
+ instanceof: (def, ctx) => {
52
+ if (typeof def[1] !== "function") {
53
+ return throwParseError(writeInvalidConstructorMessage(objectKindOrDomainOf(def[1])));
54
+ }
55
+ const branches = def
56
+ .slice(1)
57
+ .map(ctor => typeof ctor === "function" ?
58
+ ctx.$.node("proto", { proto: ctor })
59
+ : throwParseError(writeInvalidConstructorMessage(objectKindOrDomainOf(ctor))));
60
+ return branches.length === 1 ?
61
+ branches[0]
62
+ : ctx.$.node("union", { branches });
63
+ },
64
+ "===": (def, ctx) => ctx.$.units(def.slice(1))
65
+ };
66
+ const isIndexZeroExpression = (def) => prefixParsers[def[0]] !== undefined;
67
+ export const writeInvalidConstructorMessage = (actual) => `Expected a constructor following 'instanceof' operator (was ${actual})`;