@sinclair/typebox 0.34.25 → 0.34.27

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 (43) hide show
  1. package/build/cjs/parser/runtime/guard.d.ts +12 -6
  2. package/build/cjs/parser/runtime/guard.js +31 -20
  3. package/build/cjs/parser/runtime/module.d.ts +2 -2
  4. package/build/cjs/parser/runtime/module.js +4 -4
  5. package/build/cjs/parser/runtime/parse.d.ts +8 -8
  6. package/build/cjs/parser/runtime/parse.js +71 -43
  7. package/build/cjs/parser/runtime/types.d.ts +56 -26
  8. package/build/cjs/parser/runtime/types.js +34 -8
  9. package/build/cjs/parser/static/parse.d.ts +6 -3
  10. package/build/cjs/parser/static/types.d.ts +41 -18
  11. package/build/cjs/syntax/runtime.d.ts +2 -2
  12. package/build/cjs/syntax/runtime.js +13 -9
  13. package/build/cjs/syntax/static.d.ts +8 -5
  14. package/build/cjs/syntax/syntax.d.ts +2 -4
  15. package/build/cjs/syntax/syntax.js +1 -8
  16. package/build/cjs/type/module/compute.d.ts +16 -14
  17. package/build/cjs/type/module/compute.js +51 -40
  18. package/build/cjs/type/static/static.d.ts +14 -10
  19. package/build/cjs/value/transform/decode.js +3 -5
  20. package/build/cjs/value/transform/encode.js +3 -5
  21. package/build/cjs/value/transform/has.js +8 -0
  22. package/build/esm/parser/runtime/guard.d.mts +12 -6
  23. package/build/esm/parser/runtime/guard.mjs +26 -18
  24. package/build/esm/parser/runtime/module.d.mts +2 -2
  25. package/build/esm/parser/runtime/module.mjs +4 -4
  26. package/build/esm/parser/runtime/parse.d.mts +8 -8
  27. package/build/esm/parser/runtime/parse.mjs +71 -43
  28. package/build/esm/parser/runtime/types.d.mts +56 -26
  29. package/build/esm/parser/runtime/types.mjs +29 -6
  30. package/build/esm/parser/static/parse.d.mts +6 -3
  31. package/build/esm/parser/static/types.d.mts +41 -18
  32. package/build/esm/syntax/runtime.d.mts +2 -2
  33. package/build/esm/syntax/runtime.mjs +13 -9
  34. package/build/esm/syntax/static.d.mts +8 -5
  35. package/build/esm/syntax/syntax.d.mts +2 -4
  36. package/build/esm/syntax/syntax.mjs +1 -8
  37. package/build/esm/type/module/compute.d.mts +16 -14
  38. package/build/esm/type/module/compute.mjs +52 -41
  39. package/build/esm/type/static/static.d.mts +14 -10
  40. package/build/esm/value/transform/decode.mjs +3 -5
  41. package/build/esm/value/transform/encode.mjs +3 -5
  42. package/build/esm/value/transform/has.mjs +8 -0
  43. package/package.json +1 -1
@@ -16,49 +16,57 @@ function IsArrayValue(value) {
16
16
  // ------------------------------------------------------------------
17
17
  // Parser Guard
18
18
  // ------------------------------------------------------------------
19
- /** Returns true if the value is a Tuple Parser */
20
- // prettier-ignore
21
- export function IsTuple(value) {
22
- return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Tuple' && HasPropertyKey(value, 'parsers') && IsArrayValue(value.parsers);
23
- }
24
- /** Returns true if the value is a Union Parser */
25
- // prettier-ignore
26
- export function IsUnion(value) {
27
- return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Union' && HasPropertyKey(value, 'parsers') && IsArrayValue(value.parsers);
19
+ /** Returns true if the value is a Array Parser */
20
+ export function IsArray(value) {
21
+ return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Array' && HasPropertyKey(value, 'parser') && IsObjectValue(value.parser);
28
22
  }
29
23
  /** Returns true if the value is a Const Parser */
30
- // prettier-ignore
31
24
  export function IsConst(value) {
32
25
  return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Const' && HasPropertyKey(value, 'value') && typeof value.value === 'string';
33
26
  }
27
+ /** Returns true if the value is a Context Parser */
28
+ export function IsContext(value) {
29
+ return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Context' && HasPropertyKey(value, 'left') && IsParser(value.left) && HasPropertyKey(value, 'right') && IsParser(value.right);
30
+ }
34
31
  /** Returns true if the value is a Ident Parser */
35
- // prettier-ignore
36
32
  export function IsIdent(value) {
37
33
  return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Ident';
38
34
  }
39
35
  /** Returns true if the value is a Number Parser */
40
- // prettier-ignore
41
36
  export function IsNumber(value) {
42
37
  return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Number';
43
38
  }
39
+ /** Returns true if the value is a Optional Parser */
40
+ export function IsOptional(value) {
41
+ return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Optional' && HasPropertyKey(value, 'parser') && IsObjectValue(value.parser);
42
+ }
44
43
  /** Returns true if the value is a Ref Parser */
45
- // prettier-ignore
46
44
  export function IsRef(value) {
47
45
  return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Ref' && HasPropertyKey(value, 'ref') && typeof value.ref === 'string';
48
46
  }
49
47
  /** Returns true if the value is a String Parser */
50
- // prettier-ignore
51
48
  export function IsString(value) {
52
49
  return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'String' && HasPropertyKey(value, 'options') && IsArrayValue(value.options);
53
50
  }
51
+ /** Returns true if the value is a Tuple Parser */
52
+ export function IsTuple(value) {
53
+ return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Tuple' && HasPropertyKey(value, 'parsers') && IsArrayValue(value.parsers);
54
+ }
55
+ /** Returns true if the value is a Union Parser */
56
+ export function IsUnion(value) {
57
+ return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Union' && HasPropertyKey(value, 'parsers') && IsArrayValue(value.parsers);
58
+ }
54
59
  /** Returns true if the value is a Parser */
55
- // prettier-ignore
56
60
  export function IsParser(value) {
57
- return (IsTuple(value) ||
58
- IsUnion(value) ||
61
+ // prettier-ignore
62
+ return (IsArray(value) ||
59
63
  IsConst(value) ||
64
+ IsContext(value) ||
60
65
  IsIdent(value) ||
61
66
  IsNumber(value) ||
67
+ IsOptional(value) ||
62
68
  IsRef(value) ||
63
- IsString(value));
69
+ IsString(value) ||
70
+ IsTuple(value) ||
71
+ IsUnion(value));
64
72
  }
@@ -3,7 +3,7 @@ export declare class Module<Properties extends Types.IModuleProperties = Types.I
3
3
  private readonly properties;
4
4
  constructor(properties: Properties);
5
5
  /** Parses using one of the parsers defined on this instance */
6
- Parse<Key extends keyof Properties>(key: Key, code: string, context: unknown): [] | [Types.StaticParser<Properties[Key]>, string];
6
+ Parse<Key extends keyof Properties>(key: Key, content: string, context: unknown): [] | [Types.StaticParser<Properties[Key]>, string];
7
7
  /** Parses using one of the parsers defined on this instance */
8
- Parse<Key extends keyof Properties>(key: Key, code: string): [] | [Types.StaticParser<Properties[Key]>, string];
8
+ Parse<Key extends keyof Properties>(key: Key, content: string): [] | [Types.StaticParser<Properties[Key]>, string];
9
9
  }
@@ -2,16 +2,16 @@ import { Parse } from './parse.mjs';
2
2
  // ------------------------------------------------------------------
3
3
  // Module
4
4
  // ------------------------------------------------------------------
5
- // prettier-ignore
6
5
  export class Module {
7
6
  constructor(properties) {
8
7
  this.properties = properties;
9
8
  }
10
9
  /** Parses using one of the parsers defined on this instance */
11
10
  Parse(...args) {
12
- const [key, code, context] = args.length === 3 ? [args[0], args[1], args[2]] :
11
+ // prettier-ignore
12
+ const [key, content, context] = (args.length === 3 ? [args[0], args[1], args[2]] :
13
13
  args.length === 2 ? [args[0], args[1], undefined] :
14
- (() => { throw Error('Invalid parse arguments'); })();
15
- return Parse(this.properties[key], this.properties, code, context);
14
+ (() => { throw Error('Invalid parse arguments'); })());
15
+ return Parse(this.properties, this.properties[key], content, context);
16
16
  }
17
17
  }
@@ -1,9 +1,9 @@
1
1
  import * as Types from './types.mjs';
2
- /** Parses content using the given parser */
3
- export declare function Parse<Parser extends Types.IParser>(parser: Parser, properties: Types.IModuleProperties, code: string, context: unknown): [] | [Types.StaticParser<Parser>, string];
4
- /** Parses content using the given parser */
5
- export declare function Parse<Parser extends Types.IParser>(parser: Parser, properties: Types.IModuleProperties, code: string): [] | [Types.StaticParser<Parser>, string];
6
- /** Parses content using the given parser */
7
- export declare function Parse<Parser extends Types.IParser>(parser: Parser, code: string, context: unknown): [] | [Types.StaticParser<Parser>, string];
8
- /** Parses content using the given parser */
9
- export declare function Parse<Parser extends Types.IParser>(parser: Parser, code: string): [] | [Types.StaticParser<Parser>, string];
2
+ /** Parses content using the given Parser */
3
+ export declare function Parse<Parser extends Types.IParser>(moduleProperties: Types.IModuleProperties, parser: Parser, code: string, context: unknown): [] | [Types.StaticParser<Parser>, string];
4
+ /** Parses content using the given Parser */
5
+ export declare function Parse<Parser extends Types.IParser>(moduleProperties: Types.IModuleProperties, parser: Parser, code: string): [] | [Types.StaticParser<Parser>, string];
6
+ /** Parses content using the given Parser */
7
+ export declare function Parse<Parser extends Types.IParser>(parser: Parser, content: string, context: unknown): [] | [Types.StaticParser<Parser>, string];
8
+ /** Parses content using the given Parser */
9
+ export declare function Parse<Parser extends Types.IParser>(parser: Parser, content: string): [] | [Types.StaticParser<Parser>, string];
@@ -1,50 +1,61 @@
1
1
  import * as Guard from './guard.mjs';
2
2
  import * as Token from './token.mjs';
3
3
  // ------------------------------------------------------------------
4
- // Tuple
4
+ // Context
5
5
  // ------------------------------------------------------------------
6
- // prettier-ignore
7
- function ParseTuple(parsers, properties, code, context) {
6
+ function ParseContext(moduleProperties, left, right, code, context) {
7
+ const result = ParseParser(moduleProperties, left, code, context);
8
+ return result.length === 2 ? ParseParser(moduleProperties, right, result[1], result[0]) : [];
9
+ }
10
+ // ------------------------------------------------------------------
11
+ // Array
12
+ // ------------------------------------------------------------------
13
+ function ParseArray(moduleProperties, parser, code, context) {
8
14
  const buffer = [];
9
15
  let rest = code;
10
- for (const parser of parsers) {
11
- const result = ParseParser(parser, properties, rest, context);
16
+ while (rest.length > 0) {
17
+ const result = ParseParser(moduleProperties, parser, rest, context);
12
18
  if (result.length === 0)
13
- return [];
19
+ return [buffer, rest];
14
20
  buffer.push(result[0]);
15
21
  rest = result[1];
16
22
  }
17
23
  return [buffer, rest];
18
24
  }
19
25
  // ------------------------------------------------------------------
20
- // Union
26
+ // Const
21
27
  // ------------------------------------------------------------------
22
- // prettier-ignore
23
- function ParseUnion(parsers, properties, code, context) {
24
- for (const parser of parsers) {
25
- const result = ParseParser(parser, properties, code, context);
26
- if (result.length === 0)
27
- continue;
28
- return result;
29
- }
30
- return [];
28
+ function ParseConst(value, code, context) {
29
+ return Token.Const(value, code);
31
30
  }
32
31
  // ------------------------------------------------------------------
33
- // Const
32
+ // Ident
33
+ // ------------------------------------------------------------------
34
+ function ParseIdent(code, _context) {
35
+ return Token.Ident(code);
36
+ }
37
+ // ------------------------------------------------------------------
38
+ // Number
34
39
  // ------------------------------------------------------------------
35
40
  // prettier-ignore
36
- function ParseConst(value, code, context) {
37
- return Token.Const(value, code);
41
+ function ParseNumber(code, _context) {
42
+ return Token.Number(code);
43
+ }
44
+ // ------------------------------------------------------------------
45
+ // Optional
46
+ // ------------------------------------------------------------------
47
+ function ParseOptional(moduleProperties, parser, code, context) {
48
+ const result = ParseParser(moduleProperties, parser, code, context);
49
+ return (result.length === 2 ? [[result[0]], result[1]] : [[], code]);
38
50
  }
39
51
  // ------------------------------------------------------------------
40
52
  // Ref
41
53
  // ------------------------------------------------------------------
42
- // prettier-ignore
43
- function ParseRef(ref, properties, code, context) {
44
- const parser = properties[ref];
54
+ function ParseRef(moduleProperties, ref, code, context) {
55
+ const parser = moduleProperties[ref];
45
56
  if (!Guard.IsParser(parser))
46
- throw Error(`Cannot dereference parser '${ref}'`);
47
- return ParseParser(parser, properties, code, context);
57
+ throw Error(`Cannot dereference Parser '${ref}'`);
58
+ return ParseParser(moduleProperties, parser, code, context);
48
59
  }
49
60
  // ------------------------------------------------------------------
50
61
  // String
@@ -54,32 +65,49 @@ function ParseString(options, code, _context) {
54
65
  return Token.String(options, code);
55
66
  }
56
67
  // ------------------------------------------------------------------
57
- // Number
68
+ // Tuple
58
69
  // ------------------------------------------------------------------
59
- // prettier-ignore
60
- function ParseNumber(code, _context) {
61
- return Token.Number(code);
70
+ function ParseTuple(moduleProperties, parsers, code, context) {
71
+ const buffer = [];
72
+ let rest = code;
73
+ for (const parser of parsers) {
74
+ const result = ParseParser(moduleProperties, parser, rest, context);
75
+ if (result.length === 0)
76
+ return [];
77
+ buffer.push(result[0]);
78
+ rest = result[1];
79
+ }
80
+ return [buffer, rest];
62
81
  }
63
82
  // ------------------------------------------------------------------
64
- // Ident
83
+ // Union
65
84
  // ------------------------------------------------------------------
66
85
  // prettier-ignore
67
- function ParseIdent(code, _context) {
68
- return Token.Ident(code);
86
+ function ParseUnion(moduleProperties, parsers, code, context) {
87
+ for (const parser of parsers) {
88
+ const result = ParseParser(moduleProperties, parser, code, context);
89
+ if (result.length === 0)
90
+ continue;
91
+ return result;
92
+ }
93
+ return [];
69
94
  }
70
95
  // ------------------------------------------------------------------
71
96
  // Parser
72
97
  // ------------------------------------------------------------------
73
98
  // prettier-ignore
74
- function ParseParser(parser, properties, code, context) {
75
- const result = (Guard.IsTuple(parser) ? ParseTuple(parser.parsers, properties, code, context) :
76
- Guard.IsUnion(parser) ? ParseUnion(parser.parsers, properties, code, context) :
99
+ function ParseParser(moduleProperties, parser, code, context) {
100
+ const result = (Guard.IsContext(parser) ? ParseContext(moduleProperties, parser.left, parser.right, code, context) :
101
+ Guard.IsArray(parser) ? ParseArray(moduleProperties, parser.parser, code, context) :
77
102
  Guard.IsConst(parser) ? ParseConst(parser.value, code, context) :
78
- Guard.IsRef(parser) ? ParseRef(parser.ref, properties, code, context) :
79
- Guard.IsString(parser) ? ParseString(parser.options, code, context) :
80
- Guard.IsIdent(parser) ? ParseIdent(code, context) :
81
- Guard.IsNumber(parser) ? ParseNumber(code, context) :
82
- []);
103
+ Guard.IsIdent(parser) ? ParseIdent(code, context) :
104
+ Guard.IsNumber(parser) ? ParseNumber(code, context) :
105
+ Guard.IsOptional(parser) ? ParseOptional(moduleProperties, parser.parser, code, context) :
106
+ Guard.IsRef(parser) ? ParseRef(moduleProperties, parser.ref, code, context) :
107
+ Guard.IsString(parser) ? ParseString(parser.options, code, context) :
108
+ Guard.IsTuple(parser) ? ParseTuple(moduleProperties, parser.parsers, code, context) :
109
+ Guard.IsUnion(parser) ? ParseUnion(moduleProperties, parser.parsers, code, context) :
110
+ []);
83
111
  return (result.length === 2
84
112
  ? [parser.mapping(result[0], context), result[1]]
85
113
  : result);
@@ -87,9 +115,9 @@ function ParseParser(parser, properties, code, context) {
87
115
  /** Parses content using the given parser */
88
116
  // prettier-ignore
89
117
  export function Parse(...args) {
90
- const withProperties = typeof args[1] === 'string' ? false : true;
91
- const [parser, properties, code, context] = withProperties
118
+ const withModuleProperties = typeof args[1] === 'string' ? false : true;
119
+ const [moduleProperties, parser, content, context] = withModuleProperties
92
120
  ? [args[0], args[1], args[2], args[3]]
93
- : [args[0], {}, args[1], args[2]];
94
- return ParseParser(parser, properties, code, context);
121
+ : [{}, args[0], args[1], args[2]];
122
+ return ParseParser(moduleProperties, parser, content, context);
95
123
  }
@@ -1,4 +1,6 @@
1
1
  export type IModuleProperties = Record<PropertyKey, IParser>;
2
+ /** Force output static type evaluation for Arrays */
3
+ export type StaticEnsure<T> = T extends infer R ? R : never;
2
4
  /** Infers the Output Parameter for a Parser */
3
5
  export type StaticParser<Parser extends IParser> = Parser extends IParser<infer Output extends unknown> ? Output : unknown;
4
6
  export type IMapping<Input extends unknown = any, Output extends unknown = unknown> = (input: Input, context: any) => Output;
@@ -10,59 +12,87 @@ export interface IParser<Output extends unknown = unknown> {
10
12
  type: string;
11
13
  mapping: IMapping<any, Output>;
12
14
  }
13
- export type TupleParameter<Parsers extends IParser[], Result extends unknown[] = []> = Parsers extends [infer L extends IParser, ...infer R extends IParser[]] ? TupleParameter<R, [...Result, StaticParser<L>]> : Result;
14
- export interface ITuple<Output extends unknown = unknown> extends IParser<Output> {
15
- type: 'Tuple';
16
- parsers: IParser[];
15
+ export type ContextParameter<_Left extends IParser, Right extends IParser> = (StaticParser<Right>);
16
+ export interface IContext<Output extends unknown = unknown> extends IParser<Output> {
17
+ type: 'Context';
18
+ left: IParser;
19
+ right: IParser;
17
20
  }
18
- /** Creates a Tuple parser */
19
- export declare function Tuple<Parsers extends IParser[], Mapping extends IMapping = IMapping<TupleParameter<Parsers>>>(parsers: [...Parsers], mapping: Mapping): ITuple<ReturnType<Mapping>>;
20
- /** Creates a Tuple parser */
21
- export declare function Tuple<Parsers extends IParser[]>(parsers: [...Parsers]): ITuple<TupleParameter<Parsers>>;
22
- export type UnionParameter<Parsers extends IParser[], Result extends unknown = never> = Parsers extends [infer L extends IParser, ...infer R extends IParser[]] ? UnionParameter<R, Result | StaticParser<L>> : Result;
23
- export interface IUnion<Output extends unknown = unknown> extends IParser<Output> {
24
- type: 'Union';
25
- parsers: IParser[];
21
+ /** `[Context]` Creates a Context Parser */
22
+ export declare function Context<Left extends IParser, Right extends IParser, Mapping extends IMapping = IMapping<ContextParameter<Left, Right>>>(left: Left, right: Right, mapping: Mapping): IContext<ReturnType<Mapping>>;
23
+ /** `[Context]` Creates a Context Parser */
24
+ export declare function Context<Left extends IParser, Right extends IParser>(left: Left, right: Right): IContext<ContextParameter<Left, Right>>;
25
+ export type ArrayParameter<Parser extends IParser> = StaticEnsure<StaticParser<Parser>[]>;
26
+ export interface IArray<Output extends unknown = unknown> extends IParser<Output> {
27
+ type: 'Array';
28
+ parser: IParser;
26
29
  }
27
- /** Creates a Union parser */
28
- export declare function Union<Parsers extends IParser[], Mapping extends IMapping = IMapping<UnionParameter<Parsers>>>(parsers: [...Parsers], mapping: Mapping): IUnion<ReturnType<Mapping>>;
29
- /** Creates a Union parser */
30
- export declare function Union<Parsers extends IParser[]>(parsers: [...Parsers]): IUnion<UnionParameter<Parsers>>;
30
+ /** `[EBNF]` Creates an Array Parser */
31
+ export declare function Array<Parser extends IParser, Mapping extends IMapping = IMapping<ArrayParameter<Parser>>>(parser: Parser, mapping: Mapping): IArray<ReturnType<Mapping>>;
32
+ /** `[EBNF]` Creates an Array Parser */
33
+ export declare function Array<Parser extends IParser>(parser: Parser): IArray<ArrayParameter<Parser>>;
31
34
  export interface IConst<Output extends unknown = unknown> extends IParser<Output> {
32
35
  type: 'Const';
33
36
  value: string;
34
37
  }
35
- /** Creates a Const parser */
38
+ /** `[TERM]` Creates a Const Parser */
36
39
  export declare function Const<Value extends string, Mapping extends IMapping<Value>>(value: Value, mapping: Mapping): IConst<ReturnType<Mapping>>;
37
- /** Creates a Const parser */
40
+ /** `[TERM]` Creates a Const Parser */
38
41
  export declare function Const<Value extends string>(value: Value): IConst<Value>;
39
42
  export interface IRef<Output extends unknown = unknown> extends IParser<Output> {
40
43
  type: 'Ref';
41
44
  ref: string;
42
45
  }
43
- /** Creates a Ref parser */
46
+ /** `[BNF]` Creates a Ref Parser. This Parser can only be used in the context of a Module */
44
47
  export declare function Ref<Type extends unknown, Mapping extends IMapping<Type>>(ref: string, mapping: Mapping): IRef<ReturnType<Mapping>>;
45
- /** Creates a Ref parser */
48
+ /** `[BNF]` Creates a Ref Parser. This Parser can only be used in the context of a Module */
46
49
  export declare function Ref<Type extends unknown>(ref: string): IRef<Type>;
47
50
  export interface IString<Output extends unknown = unknown> extends IParser<Output> {
48
51
  type: 'String';
49
52
  options: string[];
50
53
  }
51
- /** Creates a String Parser. Options are an array of permissable quote characters */
54
+ /** `[TERM]` Creates a String Parser. Options are an array of permissable quote characters */
52
55
  export declare function String<Mapping extends IMapping<string>>(options: string[], mapping: Mapping): IString<ReturnType<Mapping>>;
53
- /** Creates a String Parser. Options are an array of permissable quote characters */
56
+ /** `[TERM]` Creates a String Parser. Options are an array of permissable quote characters */
54
57
  export declare function String(options: string[]): IString<string>;
55
58
  export interface IIdent<Output extends unknown = unknown> extends IParser<Output> {
56
59
  type: 'Ident';
57
60
  }
58
- /** Creates an Ident parser */
61
+ /** `[TERM]` Creates an Ident Parser where Ident matches any valid JavaScript identifier */
59
62
  export declare function Ident<Mapping extends IMapping<string>>(mapping: Mapping): IIdent<ReturnType<Mapping>>;
60
- /** Creates an Ident parser */
63
+ /** `[TERM]` Creates an Ident Parser where Ident matches any valid JavaScript identifier */
61
64
  export declare function Ident(): IIdent<string>;
62
65
  export interface INumber<Output extends unknown = unknown> extends IParser<Output> {
63
66
  type: 'Number';
64
67
  }
65
- /** Creates a Number parser */
68
+ /** `[TERM]` Creates an Number Parser */
66
69
  export declare function Number<Mapping extends IMapping<string>>(mapping: Mapping): INumber<ReturnType<Mapping>>;
67
- /** Creates a Number parser */
70
+ /** `[TERM]` Creates an Number Parser */
68
71
  export declare function Number(): INumber<string>;
72
+ export type OptionalParameter<Parser extends IParser, Result extends unknown = [StaticParser<Parser>] | []> = (Result);
73
+ export interface IOptional<Output extends unknown = unknown> extends IParser<Output> {
74
+ type: 'Optional';
75
+ parser: IParser;
76
+ }
77
+ /** `[EBNF]` Creates an Optional Parser */
78
+ export declare function Optional<Parser extends IParser, Mapping extends IMapping = IMapping<OptionalParameter<Parser>>>(parser: Parser, mapping: Mapping): IOptional<ReturnType<Mapping>>;
79
+ /** `[EBNF]` Creates an Optional Parser */
80
+ export declare function Optional<Parser extends IParser>(parser: Parser): IOptional<OptionalParameter<Parser>>;
81
+ export type TupleParameter<Parsers extends IParser[], Result extends unknown[] = []> = StaticEnsure<Parsers extends [infer Left extends IParser, ...infer Right extends IParser[]] ? TupleParameter<Right, [...Result, StaticEnsure<StaticParser<Left>>]> : Result>;
82
+ export interface ITuple<Output extends unknown = unknown> extends IParser<Output> {
83
+ type: 'Tuple';
84
+ parsers: IParser[];
85
+ }
86
+ /** `[BNF]` Creates a Tuple Parser */
87
+ export declare function Tuple<Parsers extends IParser[], Mapping extends IMapping = IMapping<TupleParameter<Parsers>>>(parsers: [...Parsers], mapping: Mapping): ITuple<ReturnType<Mapping>>;
88
+ /** `[BNF]` Creates a Tuple Parser */
89
+ export declare function Tuple<Parsers extends IParser[]>(parsers: [...Parsers]): ITuple<TupleParameter<Parsers>>;
90
+ export type UnionParameter<Parsers extends IParser[], Result extends unknown = never> = StaticEnsure<Parsers extends [infer Left extends IParser, ...infer Right extends IParser[]] ? UnionParameter<Right, Result | StaticParser<Left>> : Result>;
91
+ export interface IUnion<Output extends unknown = unknown> extends IParser<Output> {
92
+ type: 'Union';
93
+ parsers: IParser[];
94
+ }
95
+ /** `[BNF]` Creates a Union parser */
96
+ export declare function Union<Parsers extends IParser[], Mapping extends IMapping = IMapping<UnionParameter<Parsers>>>(parsers: [...Parsers], mapping: Mapping): IUnion<ReturnType<Mapping>>;
97
+ /** `[BNF]` Creates a Union parser */
98
+ export declare function Union<Parsers extends IParser[]>(parsers: [...Parsers]): IUnion<UnionParameter<Parsers>>;
@@ -1,32 +1,55 @@
1
1
  /** Maps input to output. This is the default Mapping */
2
2
  export const Identity = (value) => value;
3
3
  /** Maps the output as the given parameter T */
4
+ // prettier-ignore
4
5
  export const As = (mapping) => (_) => mapping;
5
- export function Tuple(...args) {
6
- const [parsers, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], Identity];
7
- return { type: 'Tuple', parsers, mapping };
6
+ /** `[Context]` Creates a Context Parser */
7
+ export function Context(...args) {
8
+ const [left, right, mapping] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], args[1], Identity];
9
+ return { type: 'Context', left, right, mapping };
8
10
  }
9
- export function Union(...args) {
10
- const [parsers, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], Identity];
11
- return { type: 'Union', parsers, mapping };
11
+ /** `[EBNF]` Creates an Array Parser */
12
+ export function Array(...args) {
13
+ const [parser, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], Identity];
14
+ return { type: 'Array', parser, mapping };
12
15
  }
16
+ /** `[TERM]` Creates a Const Parser */
13
17
  export function Const(...args) {
14
18
  const [value, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], Identity];
15
19
  return { type: 'Const', value, mapping };
16
20
  }
21
+ /** `[BNF]` Creates a Ref Parser. This Parser can only be used in the context of a Module */
17
22
  export function Ref(...args) {
18
23
  const [ref, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], Identity];
19
24
  return { type: 'Ref', ref, mapping };
20
25
  }
26
+ /** `[TERM]` Creates a String Parser. Options are an array of permissable quote characters */
21
27
  export function String(...params) {
22
28
  const [options, mapping] = params.length === 2 ? [params[0], params[1]] : [params[0], Identity];
23
29
  return { type: 'String', options, mapping };
24
30
  }
31
+ /** `[TERM]` Creates an Ident Parser where Ident matches any valid JavaScript identifier */
25
32
  export function Ident(...params) {
26
33
  const mapping = params.length === 1 ? params[0] : Identity;
27
34
  return { type: 'Ident', mapping };
28
35
  }
36
+ /** `[TERM]` Creates an Number Parser */
29
37
  export function Number(...params) {
30
38
  const mapping = params.length === 1 ? params[0] : Identity;
31
39
  return { type: 'Number', mapping };
32
40
  }
41
+ /** `[EBNF]` Creates an Optional Parser */
42
+ export function Optional(...args) {
43
+ const [parser, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], Identity];
44
+ return { type: 'Optional', parser, mapping };
45
+ }
46
+ /** `[BNF]` Creates a Tuple Parser */
47
+ export function Tuple(...args) {
48
+ const [parsers, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], Identity];
49
+ return { type: 'Tuple', parsers, mapping };
50
+ }
51
+ /** `[BNF]` Creates a Union parser */
52
+ export function Union(...args) {
53
+ const [parsers, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], Identity];
54
+ return { type: 'Union', parsers, mapping };
55
+ }
@@ -1,12 +1,15 @@
1
1
  import * as Tokens from './token.mjs';
2
2
  import * as Types from './types.mjs';
3
- type TupleParser<Parsers extends Types.IParser[], Code extends string, Context extends unknown, Result extends unknown[] = []> = (Parsers extends [infer Left extends Types.IParser, ...infer Right extends Types.IParser[]] ? Parse<Left, Code, Context> extends [infer Value extends unknown, infer Rest extends string] ? TupleParser<Right, Rest, Context, [...Result, Value]> : [] : [Result, Code]);
4
- type UnionParser<Parsers extends Types.IParser[], Code extends string, Context extends unknown> = (Parsers extends [infer Left extends Types.IParser, ...infer Right extends Types.IParser[]] ? Parse<Left, Code, Context> extends [infer Value extends unknown, infer Rest extends string] ? [Value, Rest] : UnionParser<Right, Code, Context> : []);
3
+ type ContextParser<Left extends Types.IParser, Right extends Types.IParser, Code extends string, Context extends unknown> = (Parse<Left, Code, Context> extends [infer Context extends unknown, infer Rest extends string] ? Parse<Right, Rest, Context> : []);
4
+ type ArrayParser<Parser extends Types.IParser, Code extends string, Context extends unknown, Result extends unknown[] = []> = (Parse<Parser, Code, Context> extends [infer Value1 extends unknown, infer Rest extends string] ? ArrayParser<Parser, Rest, Context, [...Result, Value1]> : [Result, Code]);
5
5
  type ConstParser<Value extends string, Code extends string, _Context extends unknown> = (Tokens.Const<Value, Code> extends [infer Match extends Value, infer Rest extends string] ? [Match, Rest] : []);
6
6
  type IdentParser<Code extends string, _Context extends unknown> = (Tokens.Ident<Code> extends [infer Match extends string, infer Rest extends string] ? [Match, Rest] : []);
7
7
  type NumberParser<Code extends string, _Context extends unknown> = (Tokens.Number<Code> extends [infer Match extends string, infer Rest extends string] ? [Match, Rest] : []);
8
+ type OptionalParser<Parser extends Types.IParser, Code extends string, Context extends unknown> = (Parse<Parser, Code, Context> extends [infer Value extends unknown, infer Rest extends string] ? [[Value], Rest] : [[], Code]);
8
9
  type StringParser<Options extends string[], Code extends string, _Context extends unknown> = (Tokens.String<Options, Code> extends [infer Match extends string, infer Rest extends string] ? [Match, Rest] : []);
9
- type ParseCode<Type extends Types.IParser, Code extends string, Context extends unknown = unknown> = (Type extends Types.Union<infer S extends Types.IParser[]> ? UnionParser<S, Code, Context> : Type extends Types.Tuple<infer S extends Types.IParser[]> ? TupleParser<S, Code, Context> : Type extends Types.Const<infer S extends string> ? ConstParser<S, Code, Context> : Type extends Types.String<infer S extends string[]> ? StringParser<S, Code, Context> : Type extends Types.Ident ? IdentParser<Code, Context> : Type extends Types.Number ? NumberParser<Code, Context> : [
10
+ type TupleParser<Parsers extends Types.IParser[], Code extends string, Context extends unknown, Result extends unknown[] = []> = (Parsers extends [infer Left extends Types.IParser, ...infer Right extends Types.IParser[]] ? Parse<Left, Code, Context> extends [infer Value extends unknown, infer Rest extends string] ? TupleParser<Right, Rest, Context, [...Result, Value]> : [] : [Result, Code]);
11
+ type UnionParser<Parsers extends Types.IParser[], Code extends string, Context extends unknown> = (Parsers extends [infer Left extends Types.IParser, ...infer Right extends Types.IParser[]] ? Parse<Left, Code, Context> extends [infer Value extends unknown, infer Rest extends string] ? [Value, Rest] : UnionParser<Right, Code, Context> : []);
12
+ type ParseCode<Type extends Types.IParser, Code extends string, Context extends unknown = unknown> = (Type extends Types.Context<infer Left extends Types.IParser, infer Right extends Types.IParser> ? ContextParser<Left, Right, Code, Context> : Type extends Types.Array<infer Parser extends Types.IParser> ? ArrayParser<Parser, Code, Context> : Type extends Types.Const<infer Value extends string> ? ConstParser<Value, Code, Context> : Type extends Types.Ident ? IdentParser<Code, Context> : Type extends Types.Number ? NumberParser<Code, Context> : Type extends Types.Optional<infer Parser extends Types.IParser> ? OptionalParser<Parser, Code, Context> : Type extends Types.String<infer Options extends string[]> ? StringParser<Options, Code, Context> : Type extends Types.Tuple<infer Parsers extends Types.IParser[]> ? TupleParser<Parsers, Code, Context> : Type extends Types.Union<infer Parsers extends Types.IParser[]> ? UnionParser<Parsers, Code, Context> : [
10
13
  ]);
11
14
  type ParseMapping<Parser extends Types.IParser, Result extends unknown, Context extends unknown = unknown> = ((Parser['mapping'] & {
12
15
  input: Result;
@@ -1,13 +1,20 @@
1
+ /**
2
+ * `[ACTION]` Inference mapping base type. Used to specify semantic actions for
3
+ * Parser productions. This type is implemented as a higher-kinded type where
4
+ * productions are received on the `input` property with mapping assigned
5
+ * the `output` property. The parsing context is available on the `context`
6
+ * property.
7
+ */
1
8
  export interface IMapping {
2
9
  context: unknown;
3
10
  input: unknown;
4
11
  output: unknown;
5
12
  }
6
- /** Maps input to output. This is the default Mapping */
13
+ /** `[ACTION]` Default inference mapping. */
7
14
  export interface Identity extends IMapping {
8
15
  output: this['input'];
9
16
  }
10
- /** Maps the output as the given parameter T */
17
+ /** `[ACTION]` Maps the given argument `T` as the mapping output */
11
18
  export interface As<T> extends IMapping {
12
19
  output: T;
13
20
  }
@@ -16,31 +23,47 @@ export interface IParser<Mapping extends IMapping = Identity> {
16
23
  type: string;
17
24
  mapping: Mapping;
18
25
  }
19
- /** Creates a Tuple Parser */
20
- export interface Tuple<Parsers extends IParser[] = [], Mapping extends IMapping = Identity> extends IParser<Mapping> {
21
- type: 'Tuple';
22
- parsers: [...Parsers];
26
+ /** `[Context]` Creates a Context Parser */
27
+ export interface Context<Left extends IParser = IParser, Right extends IParser = IParser, Mapping extends IMapping = Identity> extends IParser<Mapping> {
28
+ type: 'Context';
29
+ left: Left;
30
+ right: Right;
23
31
  }
24
- /** Creates a Union Parser */
25
- export interface Union<Parsers extends IParser[] = [], Mapping extends IMapping = Identity> extends IParser<Mapping> {
26
- type: 'Union';
27
- parsers: [...Parsers];
32
+ /** `[EBNF]` Creates an Array Parser */
33
+ export interface Array<Parser extends IParser = IParser, Mapping extends IMapping = Identity> extends IParser<Mapping> {
34
+ type: 'Array';
35
+ parser: Parser;
28
36
  }
29
- /** Creates a Const Parser */
37
+ /** `[TERM]` Creates a Const Parser */
30
38
  export interface Const<Value extends string = string, Mapping extends IMapping = Identity> extends IParser<Mapping> {
31
39
  type: 'Const';
32
40
  value: Value;
33
41
  }
34
- /** Creates a String Parser. Options are an array of permissable quote characters */
35
- export interface String<Options extends string[], Mapping extends IMapping = Identity> extends IParser<Mapping> {
36
- type: 'String';
37
- quote: Options;
38
- }
39
- /** Creates an Ident Parser. */
42
+ /** `[TERM]` Creates an Ident Parser. */
40
43
  export interface Ident<Mapping extends IMapping = Identity> extends IParser<Mapping> {
41
44
  type: 'Ident';
42
45
  }
43
- /** Creates a Number Parser. */
46
+ /** `[TERM]` Creates a Number Parser. */
44
47
  export interface Number<Mapping extends IMapping = Identity> extends IParser<Mapping> {
45
48
  type: 'Number';
46
49
  }
50
+ /** `[EBNF]` Creates a Optional Parser */
51
+ export interface Optional<Parser extends IParser = IParser, Mapping extends IMapping = Identity> extends IParser<Mapping> {
52
+ type: 'Optional';
53
+ parser: Parser;
54
+ }
55
+ /** `[TERM]` Creates a String Parser. Options are an array of permissable quote characters */
56
+ export interface String<Options extends string[], Mapping extends IMapping = Identity> extends IParser<Mapping> {
57
+ type: 'String';
58
+ quote: Options;
59
+ }
60
+ /** `[BNF]` Creates a Tuple Parser */
61
+ export interface Tuple<Parsers extends IParser[] = [], Mapping extends IMapping = Identity> extends IParser<Mapping> {
62
+ type: 'Tuple';
63
+ parsers: [...Parsers];
64
+ }
65
+ /** `[BNF]` Creates a Union Parser */
66
+ export interface Union<Parsers extends IParser[] = [], Mapping extends IMapping = Identity> extends IParser<Mapping> {
67
+ type: 'Union';
68
+ parsers: [...Parsers];
69
+ }
@@ -2,7 +2,7 @@ import { Runtime } from '../parser/index.mjs';
2
2
  import * as t from '../type/index.mjs';
3
3
  export declare const Module: Runtime.Module<{
4
4
  GenericArgumentList: Runtime.IUnion<unknown[]>;
5
- GenericArguments: Runtime.ITuple<{}>;
5
+ GenericArguments: Runtime.ITuple<t.TProperties>;
6
6
  Literal: Runtime.IUnion<t.TLiteral<string> | t.TLiteral<number> | t.TLiteral<boolean>>;
7
7
  Keyword: Runtime.IUnion<t.TAny | t.TBoolean | t.TBigInt | t.TNever | t.TString | t.TNumber | t.TInteger | t.TNull | t.TSymbol | t.TUndefined | t.TUnknown | t.TVoid>;
8
8
  KeyOf: Runtime.IUnion<boolean>;
@@ -14,7 +14,7 @@ export declare const Module: Runtime.Module<{
14
14
  ExprTerm: Runtime.ITuple<t.TSchema>;
15
15
  ExprTail: Runtime.IUnion<[] | ["|", unknown, unknown]>;
16
16
  Expr: Runtime.ITuple<t.TSchema>;
17
- Type: Runtime.IRef<unknown>;
17
+ Type: Runtime.IUnion<unknown>;
18
18
  PropertyKey: Runtime.IUnion<string>;
19
19
  Readonly: Runtime.IUnion<boolean>;
20
20
  Optional: Runtime.IUnion<boolean>;