@sinclair/typebox 0.34.24 → 0.34.26

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 (36) 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 +7 -9
  15. package/build/cjs/syntax/syntax.js +3 -10
  16. package/build/cjs/type/instantiate/instantiate.d.ts +1 -1
  17. package/build/cjs/type/instantiate/instantiate.js +2 -2
  18. package/build/esm/parser/runtime/guard.d.mts +12 -6
  19. package/build/esm/parser/runtime/guard.mjs +26 -18
  20. package/build/esm/parser/runtime/module.d.mts +2 -2
  21. package/build/esm/parser/runtime/module.mjs +4 -4
  22. package/build/esm/parser/runtime/parse.d.mts +8 -8
  23. package/build/esm/parser/runtime/parse.mjs +71 -43
  24. package/build/esm/parser/runtime/types.d.mts +56 -26
  25. package/build/esm/parser/runtime/types.mjs +29 -6
  26. package/build/esm/parser/static/parse.d.mts +6 -3
  27. package/build/esm/parser/static/types.d.mts +41 -18
  28. package/build/esm/syntax/runtime.d.mts +2 -2
  29. package/build/esm/syntax/runtime.mjs +13 -9
  30. package/build/esm/syntax/static.d.mts +8 -5
  31. package/build/esm/syntax/syntax.d.mts +7 -9
  32. package/build/esm/syntax/syntax.mjs +3 -10
  33. package/build/esm/type/instantiate/instantiate.d.mts +1 -1
  34. package/build/esm/type/instantiate/instantiate.mjs +2 -2
  35. package/package.json +1 -1
  36. package/readme.md +19 -1
@@ -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';
2
2
  import * as t from '../type/index';
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>;
@@ -60,15 +60,15 @@ const GenericArgumentList = index_1.Runtime.Union([
60
60
  // GenericArguments
61
61
  // ------------------------------------------------------------------
62
62
  // prettier-ignore
63
- const GenericArgumentsContext = (args) => {
63
+ const GenericArgumentsContext = (args, context) => {
64
64
  return args.reduce((result, arg, index) => {
65
65
  return { ...result, [arg]: t.Argument(index) };
66
- }, {});
66
+ }, context);
67
67
  };
68
68
  // prettier-ignore
69
- const GenericArgumentsMapping = (results) => {
69
+ const GenericArgumentsMapping = (results, context) => {
70
70
  return results.length === 3
71
- ? GenericArgumentsContext(results[1])
71
+ ? GenericArgumentsContext(results[1], context)
72
72
  : {};
73
73
  };
74
74
  // prettier-ignore
@@ -76,7 +76,7 @@ const GenericArguments = index_1.Runtime.Tuple([
76
76
  index_1.Runtime.Const(LAngle),
77
77
  index_1.Runtime.Ref('GenericArgumentList'),
78
78
  index_1.Runtime.Const(RAngle),
79
- ], results => GenericArgumentsMapping(results));
79
+ ], (results, context) => GenericArgumentsMapping(results, context));
80
80
  // ------------------------------------------------------------------
81
81
  // GenericReference
82
82
  // ------------------------------------------------------------------
@@ -286,7 +286,11 @@ const Expr = index_1.Runtime.Tuple([
286
286
  // ------------------------------------------------------------------
287
287
  // Type
288
288
  // ------------------------------------------------------------------
289
- const Type = index_1.Runtime.Ref('Expr');
289
+ // prettier-ignore
290
+ const Type = index_1.Runtime.Union([
291
+ index_1.Runtime.Context(index_1.Runtime.Ref('GenericArguments'), index_1.Runtime.Ref('Expr')),
292
+ index_1.Runtime.Ref('Expr')
293
+ ]);
290
294
  // ------------------------------------------------------------------
291
295
  // Properties
292
296
  // ------------------------------------------------------------------
@@ -653,12 +657,12 @@ const Uint8Array = index_1.Runtime.Const('Uint8Array', index_1.Runtime.As(t.Uint
653
657
  // prettier-ignore
654
658
  exports.Module = new index_1.Runtime.Module({
655
659
  // ----------------------------------------------------------------
656
- // Generic Arguments
660
+ // Generics
657
661
  // ----------------------------------------------------------------
658
662
  GenericArgumentList,
659
663
  GenericArguments,
660
664
  // ----------------------------------------------------------------
661
- // Type Expressions
665
+ // Type
662
666
  // ----------------------------------------------------------------
663
667
  Literal,
664
668
  Keyword,
@@ -671,7 +675,7 @@ exports.Module = new index_1.Runtime.Module({
671
675
  ExprTerm,
672
676
  ExprTail,
673
677
  Expr,
674
- Type, // Alias for Expr
678
+ Type,
675
679
  PropertyKey,
676
680
  Readonly,
677
681
  Optional,
@@ -52,13 +52,13 @@ type GenericArgumentList = Static.Union<[
52
52
  Static.Tuple<[Static.Ident]>,
53
53
  Static.Tuple<[]>
54
54
  ], GenericArgumentListMapping>;
55
- type GenericArgumentsContext<Args extends string[], Result extends t.TProperties = {}> = (Args extends [...infer Left extends string[], infer Right extends string] ? GenericArgumentsContext<Left, Result & {
55
+ type GenericArgumentsContext<Args extends string[], Context extends t.TProperties, Result extends t.TProperties = {}> = (Args extends [...infer Left extends string[], infer Right extends string] ? GenericArgumentsContext<Left, Context, Result & {
56
56
  [_ in Right]: t.TArgument<Left['length']>;
57
- }> : t.Evaluate<Result>);
57
+ }> : t.Evaluate<Result & Context>);
58
58
  interface GenericArgumentsMapping extends Static.IMapping {
59
- output: this['input'] extends [LAngle, infer Args extends string[], RAngle] ? GenericArgumentsContext<Args> : never;
59
+ output: this['input'] extends [LAngle, infer Args extends string[], RAngle] ? this['context'] extends infer Context extends t.TProperties ? GenericArgumentsContext<Args, Context> : never : never;
60
60
  }
61
- export type GenericArguments = Static.Tuple<[
61
+ type GenericArguments = Static.Tuple<[
62
62
  Static.Const<LAngle>,
63
63
  GenericArgumentList,
64
64
  Static.Const<RAngle>
@@ -204,7 +204,10 @@ type Expr = Static.Tuple<[
204
204
  ExprTerm,
205
205
  ExprTail
206
206
  ], ExprBinaryMapping>;
207
- export type Type = Expr;
207
+ export type Type = Static.Union<[
208
+ Static.Context<GenericArguments, Expr>,
209
+ Expr
210
+ ]>;
208
211
  interface PropertyKeyStringMapping extends Static.IMapping {
209
212
  output: this['input'];
210
213
  }
@@ -1,16 +1,15 @@
1
1
  import * as t from '../type/index';
2
2
  import { Static } from '../parser/index';
3
- import { Type, GenericArguments } from './static';
4
- type TParseSyntax<Context extends Record<PropertyKey, t.TSchema>, Code extends string> = (Static.Parse<GenericArguments, Code, {}> extends [infer Args extends t.TProperties, infer Rest extends string] ? Static.Parse<Type, Rest, Context & Args> : Static.Parse<Type, Code, Context>);
5
- /** Parses a TypeScript annotation into a TypeBox type but does not infer schematics */
3
+ import { Type } from './static';
4
+ /** `[Experimental]` Parses a TypeScript annotation into a TypeBox type but does not infer schematics */
6
5
  export declare function NoInfer<Context extends Record<PropertyKey, t.TSchema>, Code extends string>(context: Context, code: Code, options?: t.SchemaOptions): t.TSchema;
7
- /** Parses a TypeScript annotation into a TypeBox type but does not infer schematics */
6
+ /** `[Experimental]` Parses a TypeScript annotation into a TypeBox type but does not infer schematics */
8
7
  export declare function NoInfer<Code extends string>(code: Code, options?: t.SchemaOptions): t.TSchema;
9
- /** Parses a TypeScript annotation into a TypeBox type */
10
- export type TSyntax<Context extends Record<PropertyKey, t.TSchema>, Code extends string> = (TParseSyntax<Context, Code> extends [infer Type extends t.TSchema, string] ? Type : t.TNever);
11
- /** Parses a TypeScript annotation into a TypeBox type */
8
+ /** `[Experimental]` Parses a TypeScript annotation into a TypeBox type */
9
+ export type TSyntax<Context extends Record<PropertyKey, t.TSchema>, Code extends string> = (Static.Parse<Type, Code, Context> extends [infer Type extends t.TSchema, string] ? Type : t.TNever);
10
+ /** `[Experimental]` Parses a TypeScript annotation into a TypeBox type */
12
11
  export declare function Syntax<Context extends Record<PropertyKey, t.TSchema>, Annotation extends string>(context: Context, annotation: Annotation, options?: t.SchemaOptions): TSyntax<Context, Annotation>;
13
- /** Parses a TypeScript annotation into a TypeBox type */
12
+ /** `[Experimental]` Parses a TypeScript annotation into a TypeBox type */
14
13
  export declare function Syntax<Annotation extends string>(annotation: Annotation, options?: t.SchemaOptions): TSyntax<{}, Annotation>;
15
14
  /**
16
15
  * @deprecated Use Syntax() function
@@ -28,4 +27,3 @@ export declare function ParseOnly<Context extends Record<PropertyKey, t.TSchema>
28
27
  * @deprecated Use NoInfer() function
29
28
  */
30
29
  export declare function ParseOnly<Code extends string>(code: Code, options?: t.SchemaOptions): t.TSchema | undefined;
31
- export {};
@@ -7,24 +7,17 @@ exports.Parse = Parse;
7
7
  exports.ParseOnly = ParseOnly;
8
8
  const t = require("../type/index");
9
9
  const runtime_1 = require("./runtime");
10
- // prettier-ignore
11
- function ParseSyntax(context, code) {
12
- const results = runtime_1.Module.Parse('GenericArguments', code, {}); // [ArgumentContext, Rest]
13
- return (results.length === 2
14
- ? runtime_1.Module.Parse('Type', results[1], { ...context, ...results[0] })
15
- : runtime_1.Module.Parse('Type', code, context));
16
- }
17
- /** Parses a TypeScript annotation into a TypeBox type but does not infer schematics */
10
+ /** `[Experimental]` Parses a TypeScript annotation into a TypeBox type but does not infer schematics */
18
11
  // prettier-ignore
19
12
  function NoInfer(...args) {
20
13
  const withContext = typeof args[0] === 'string' ? false : true;
21
14
  const [context, code, options] = withContext ? [args[0], args[1], args[2] || {}] : [{}, args[0], args[1] || {}];
22
- const result = ParseSyntax(context, code)[0];
15
+ const result = runtime_1.Module.Parse('Type', code, context)[0];
23
16
  return t.KindGuard.IsSchema(result)
24
17
  ? t.CloneType(result, options)
25
18
  : t.Never(options);
26
19
  }
27
- /** Parses a TypeScript annotation into a TypeBox type */
20
+ /** `[Experimental]` Parses a TypeScript annotation into a TypeBox type */
28
21
  function Syntax(...args) {
29
22
  return NoInfer.apply(null, args);
30
23
  }
@@ -24,7 +24,7 @@ type TFromArray<Args extends TSchema[], Type extends TSchema, Result extends TAr
24
24
  type TFromAsyncIterator<Args extends TSchema[], Type extends TSchema, Result extends TAsyncIterator = TAsyncIterator<TFromType<Args, Type>>> = Result;
25
25
  type TFromIterator<Args extends TSchema[], Type extends TSchema, Result extends TIterator = TIterator<TFromType<Args, Type>>> = Result;
26
26
  type TFromPromise<Args extends TSchema[], Type extends TSchema, Result extends TPromise = TPromise<TFromType<Args, Type>>> = Result;
27
- type TFromObject<Args extends TSchema[], Properties extends TProperties, Result extends TProperties = TFromProperties<Args, Properties>> = TObject<Result>;
27
+ type TFromObject<Args extends TSchema[], Properties extends TProperties, MappedProperties extends TProperties = TFromProperties<Args, Properties>, Result extends TObject = TObject<MappedProperties>> = Result;
28
28
  type TFromRecord<Args extends TSchema[], Key extends TSchema, Value extends TSchema, MappedKey extends TSchema = TFromType<Args, Key>, MappedValue extends TSchema = TFromType<Args, Value>, Result extends TSchema = TRecordOrObject<MappedKey, MappedValue>> = Result;
29
29
  type TFromArgument<Args extends TSchema[], Index extends number, Result extends TSchema = Index extends keyof Args[Index] ? Args[Index] : TUnknown> = Result;
30
30
  type TFromProperty<Args extends TSchema[], Type extends TSchema, IsReadonly extends boolean = Type extends TReadonly<Type> ? true : false, IsOptional extends boolean = Type extends TOptional<Type> ? true : false, Mapped extends TSchema = TFromType<Args, Type>, Result extends TSchema = ([
@@ -63,8 +63,8 @@ function FromPromise(args, type) {
63
63
  }
64
64
  // prettier-ignore
65
65
  function FromObject(args, type) {
66
- const properties = FromProperties(args, type.properties);
67
- return { ...type, ...(0, index_5.Object)(properties) }; // retain options
66
+ const mappedProperties = FromProperties(args, type.properties);
67
+ return { ...type, ...(0, index_5.Object)(mappedProperties) }; // retain options
68
68
  }
69
69
  // prettier-ignore
70
70
  function FromRecord(args, type) {
@@ -1,17 +1,23 @@
1
- import { IIdent, INumber, IRef, IString, IConst, ITuple, IUnion } from './types.mjs';
2
- /** Returns true if the value is a Tuple Parser */
3
- export declare function IsTuple(value: unknown): value is ITuple;
4
- /** Returns true if the value is a Union Parser */
5
- export declare function IsUnion(value: unknown): value is IUnion;
1
+ import { IArray, IConst, IContext, IIdent, INumber, IOptional, IRef, IString, ITuple, IUnion } from './types.mjs';
2
+ /** Returns true if the value is a Array Parser */
3
+ export declare function IsArray(value: unknown): value is IArray;
6
4
  /** Returns true if the value is a Const Parser */
7
5
  export declare function IsConst(value: unknown): value is IConst;
6
+ /** Returns true if the value is a Context Parser */
7
+ export declare function IsContext(value: unknown): value is IContext;
8
8
  /** Returns true if the value is a Ident Parser */
9
9
  export declare function IsIdent(value: unknown): value is IIdent;
10
10
  /** Returns true if the value is a Number Parser */
11
11
  export declare function IsNumber(value: unknown): value is INumber;
12
+ /** Returns true if the value is a Optional Parser */
13
+ export declare function IsOptional(value: unknown): value is IOptional;
12
14
  /** Returns true if the value is a Ref Parser */
13
15
  export declare function IsRef(value: unknown): value is IRef;
14
16
  /** Returns true if the value is a String Parser */
15
17
  export declare function IsString(value: unknown): value is IString;
18
+ /** Returns true if the value is a Tuple Parser */
19
+ export declare function IsTuple(value: unknown): value is ITuple;
20
+ /** Returns true if the value is a Union Parser */
21
+ export declare function IsUnion(value: unknown): value is IUnion;
16
22
  /** Returns true if the value is a Parser */
17
- export declare function IsParser(value: unknown): value is IUnion<unknown> | ITuple<unknown> | IConst<unknown> | IIdent<unknown> | INumber<unknown> | IRef<unknown> | IString<unknown>;
23
+ export declare function IsParser(value: unknown): value is IContext<unknown> | IUnion<unknown> | IArray<unknown> | IConst<unknown> | IIdent<unknown> | INumber<unknown> | IOptional<unknown> | IRef<unknown> | IString<unknown> | ITuple<unknown>;
@@ -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
  }