@sinclair/typebox 0.34.25 → 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.
- package/build/cjs/parser/runtime/guard.d.ts +12 -6
- package/build/cjs/parser/runtime/guard.js +31 -20
- package/build/cjs/parser/runtime/module.d.ts +2 -2
- package/build/cjs/parser/runtime/module.js +4 -4
- package/build/cjs/parser/runtime/parse.d.ts +8 -8
- package/build/cjs/parser/runtime/parse.js +71 -43
- package/build/cjs/parser/runtime/types.d.ts +56 -26
- package/build/cjs/parser/runtime/types.js +34 -8
- package/build/cjs/parser/static/parse.d.ts +6 -3
- package/build/cjs/parser/static/types.d.ts +41 -18
- package/build/cjs/syntax/runtime.d.ts +2 -2
- package/build/cjs/syntax/runtime.js +13 -9
- package/build/cjs/syntax/static.d.ts +8 -5
- package/build/cjs/syntax/syntax.d.ts +2 -4
- package/build/cjs/syntax/syntax.js +1 -8
- package/build/esm/parser/runtime/guard.d.mts +12 -6
- package/build/esm/parser/runtime/guard.mjs +26 -18
- package/build/esm/parser/runtime/module.d.mts +2 -2
- package/build/esm/parser/runtime/module.mjs +4 -4
- package/build/esm/parser/runtime/parse.d.mts +8 -8
- package/build/esm/parser/runtime/parse.mjs +71 -43
- package/build/esm/parser/runtime/types.d.mts +56 -26
- package/build/esm/parser/runtime/types.mjs +29 -6
- package/build/esm/parser/static/parse.d.mts +6 -3
- package/build/esm/parser/static/types.d.mts +41 -18
- package/build/esm/syntax/runtime.d.mts +2 -2
- package/build/esm/syntax/runtime.mjs +13 -9
- package/build/esm/syntax/static.d.mts +8 -5
- package/build/esm/syntax/syntax.d.mts +2 -4
- package/build/esm/syntax/syntax.mjs +1 -8
- package/package.json +1 -1
|
@@ -1,17 +1,23 @@
|
|
|
1
|
-
import { IIdent, INumber, IRef, IString,
|
|
2
|
-
/** Returns true if the value is a
|
|
3
|
-
export declare function
|
|
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';
|
|
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> |
|
|
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>;
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.
|
|
5
|
-
exports.IsUnion = IsUnion;
|
|
4
|
+
exports.IsArray = IsArray;
|
|
6
5
|
exports.IsConst = IsConst;
|
|
6
|
+
exports.IsContext = IsContext;
|
|
7
7
|
exports.IsIdent = IsIdent;
|
|
8
8
|
exports.IsNumber = IsNumber;
|
|
9
|
+
exports.IsOptional = IsOptional;
|
|
9
10
|
exports.IsRef = IsRef;
|
|
10
11
|
exports.IsString = IsString;
|
|
12
|
+
exports.IsTuple = IsTuple;
|
|
13
|
+
exports.IsUnion = IsUnion;
|
|
11
14
|
exports.IsParser = IsParser;
|
|
12
15
|
// ------------------------------------------------------------------
|
|
13
16
|
// Value Guard
|
|
@@ -27,49 +30,57 @@ function IsArrayValue(value) {
|
|
|
27
30
|
// ------------------------------------------------------------------
|
|
28
31
|
// Parser Guard
|
|
29
32
|
// ------------------------------------------------------------------
|
|
30
|
-
/** Returns true if the value is a
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Tuple' && HasPropertyKey(value, 'parsers') && IsArrayValue(value.parsers);
|
|
34
|
-
}
|
|
35
|
-
/** Returns true if the value is a Union Parser */
|
|
36
|
-
// prettier-ignore
|
|
37
|
-
function IsUnion(value) {
|
|
38
|
-
return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Union' && HasPropertyKey(value, 'parsers') && IsArrayValue(value.parsers);
|
|
33
|
+
/** Returns true if the value is a Array Parser */
|
|
34
|
+
function IsArray(value) {
|
|
35
|
+
return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Array' && HasPropertyKey(value, 'parser') && IsObjectValue(value.parser);
|
|
39
36
|
}
|
|
40
37
|
/** Returns true if the value is a Const Parser */
|
|
41
|
-
// prettier-ignore
|
|
42
38
|
function IsConst(value) {
|
|
43
39
|
return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Const' && HasPropertyKey(value, 'value') && typeof value.value === 'string';
|
|
44
40
|
}
|
|
41
|
+
/** Returns true if the value is a Context Parser */
|
|
42
|
+
function IsContext(value) {
|
|
43
|
+
return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Context' && HasPropertyKey(value, 'left') && IsParser(value.left) && HasPropertyKey(value, 'right') && IsParser(value.right);
|
|
44
|
+
}
|
|
45
45
|
/** Returns true if the value is a Ident Parser */
|
|
46
|
-
// prettier-ignore
|
|
47
46
|
function IsIdent(value) {
|
|
48
47
|
return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Ident';
|
|
49
48
|
}
|
|
50
49
|
/** Returns true if the value is a Number Parser */
|
|
51
|
-
// prettier-ignore
|
|
52
50
|
function IsNumber(value) {
|
|
53
51
|
return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Number';
|
|
54
52
|
}
|
|
53
|
+
/** Returns true if the value is a Optional Parser */
|
|
54
|
+
function IsOptional(value) {
|
|
55
|
+
return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Optional' && HasPropertyKey(value, 'parser') && IsObjectValue(value.parser);
|
|
56
|
+
}
|
|
55
57
|
/** Returns true if the value is a Ref Parser */
|
|
56
|
-
// prettier-ignore
|
|
57
58
|
function IsRef(value) {
|
|
58
59
|
return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Ref' && HasPropertyKey(value, 'ref') && typeof value.ref === 'string';
|
|
59
60
|
}
|
|
60
61
|
/** Returns true if the value is a String Parser */
|
|
61
|
-
// prettier-ignore
|
|
62
62
|
function IsString(value) {
|
|
63
63
|
return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'String' && HasPropertyKey(value, 'options') && IsArrayValue(value.options);
|
|
64
64
|
}
|
|
65
|
+
/** Returns true if the value is a Tuple Parser */
|
|
66
|
+
function IsTuple(value) {
|
|
67
|
+
return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Tuple' && HasPropertyKey(value, 'parsers') && IsArrayValue(value.parsers);
|
|
68
|
+
}
|
|
69
|
+
/** Returns true if the value is a Union Parser */
|
|
70
|
+
function IsUnion(value) {
|
|
71
|
+
return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Union' && HasPropertyKey(value, 'parsers') && IsArrayValue(value.parsers);
|
|
72
|
+
}
|
|
65
73
|
/** Returns true if the value is a Parser */
|
|
66
|
-
// prettier-ignore
|
|
67
74
|
function IsParser(value) {
|
|
68
|
-
|
|
69
|
-
|
|
75
|
+
// prettier-ignore
|
|
76
|
+
return (IsArray(value) ||
|
|
70
77
|
IsConst(value) ||
|
|
78
|
+
IsContext(value) ||
|
|
71
79
|
IsIdent(value) ||
|
|
72
80
|
IsNumber(value) ||
|
|
81
|
+
IsOptional(value) ||
|
|
73
82
|
IsRef(value) ||
|
|
74
|
-
IsString(value)
|
|
83
|
+
IsString(value) ||
|
|
84
|
+
IsTuple(value) ||
|
|
85
|
+
IsUnion(value));
|
|
75
86
|
}
|
|
@@ -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,
|
|
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,
|
|
8
|
+
Parse<Key extends keyof Properties>(key: Key, content: string): [] | [Types.StaticParser<Properties[Key]>, string];
|
|
9
9
|
}
|
|
@@ -6,17 +6,17 @@ const parse_1 = require("./parse");
|
|
|
6
6
|
// ------------------------------------------------------------------
|
|
7
7
|
// Module
|
|
8
8
|
// ------------------------------------------------------------------
|
|
9
|
-
// prettier-ignore
|
|
10
9
|
class Module {
|
|
11
10
|
constructor(properties) {
|
|
12
11
|
this.properties = properties;
|
|
13
12
|
}
|
|
14
13
|
/** Parses using one of the parsers defined on this instance */
|
|
15
14
|
Parse(...args) {
|
|
16
|
-
|
|
15
|
+
// prettier-ignore
|
|
16
|
+
const [key, content, context] = (args.length === 3 ? [args[0], args[1], args[2]] :
|
|
17
17
|
args.length === 2 ? [args[0], args[1], undefined] :
|
|
18
|
-
(() => { throw Error('Invalid parse arguments'); })();
|
|
19
|
-
return (0, parse_1.Parse)(this.properties
|
|
18
|
+
(() => { throw Error('Invalid parse arguments'); })());
|
|
19
|
+
return (0, parse_1.Parse)(this.properties, this.properties[key], content, context);
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
exports.Module = Module;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import * as Types from './types';
|
|
2
|
-
/** Parses content using the given
|
|
3
|
-
export declare function Parse<Parser extends Types.IParser>(
|
|
4
|
-
/** Parses content using the given
|
|
5
|
-
export declare function Parse<Parser extends Types.IParser>(
|
|
6
|
-
/** Parses content using the given
|
|
7
|
-
export declare function Parse<Parser extends Types.IParser>(parser: Parser,
|
|
8
|
-
/** Parses content using the given
|
|
9
|
-
export declare function Parse<Parser extends Types.IParser>(parser: Parser,
|
|
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];
|
|
@@ -5,50 +5,61 @@ exports.Parse = Parse;
|
|
|
5
5
|
const Guard = require("./guard");
|
|
6
6
|
const Token = require("./token");
|
|
7
7
|
// ------------------------------------------------------------------
|
|
8
|
-
//
|
|
8
|
+
// Context
|
|
9
9
|
// ------------------------------------------------------------------
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
function ParseContext(moduleProperties, left, right, code, context) {
|
|
11
|
+
const result = ParseParser(moduleProperties, left, code, context);
|
|
12
|
+
return result.length === 2 ? ParseParser(moduleProperties, right, result[1], result[0]) : [];
|
|
13
|
+
}
|
|
14
|
+
// ------------------------------------------------------------------
|
|
15
|
+
// Array
|
|
16
|
+
// ------------------------------------------------------------------
|
|
17
|
+
function ParseArray(moduleProperties, parser, code, context) {
|
|
12
18
|
const buffer = [];
|
|
13
19
|
let rest = code;
|
|
14
|
-
|
|
15
|
-
const result = ParseParser(
|
|
20
|
+
while (rest.length > 0) {
|
|
21
|
+
const result = ParseParser(moduleProperties, parser, rest, context);
|
|
16
22
|
if (result.length === 0)
|
|
17
|
-
return [];
|
|
23
|
+
return [buffer, rest];
|
|
18
24
|
buffer.push(result[0]);
|
|
19
25
|
rest = result[1];
|
|
20
26
|
}
|
|
21
27
|
return [buffer, rest];
|
|
22
28
|
}
|
|
23
29
|
// ------------------------------------------------------------------
|
|
24
|
-
//
|
|
30
|
+
// Const
|
|
25
31
|
// ------------------------------------------------------------------
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
for (const parser of parsers) {
|
|
29
|
-
const result = ParseParser(parser, properties, code, context);
|
|
30
|
-
if (result.length === 0)
|
|
31
|
-
continue;
|
|
32
|
-
return result;
|
|
33
|
-
}
|
|
34
|
-
return [];
|
|
32
|
+
function ParseConst(value, code, context) {
|
|
33
|
+
return Token.Const(value, code);
|
|
35
34
|
}
|
|
36
35
|
// ------------------------------------------------------------------
|
|
37
|
-
//
|
|
36
|
+
// Ident
|
|
37
|
+
// ------------------------------------------------------------------
|
|
38
|
+
function ParseIdent(code, _context) {
|
|
39
|
+
return Token.Ident(code);
|
|
40
|
+
}
|
|
41
|
+
// ------------------------------------------------------------------
|
|
42
|
+
// Number
|
|
38
43
|
// ------------------------------------------------------------------
|
|
39
44
|
// prettier-ignore
|
|
40
|
-
function
|
|
41
|
-
return Token.
|
|
45
|
+
function ParseNumber(code, _context) {
|
|
46
|
+
return Token.Number(code);
|
|
47
|
+
}
|
|
48
|
+
// ------------------------------------------------------------------
|
|
49
|
+
// Optional
|
|
50
|
+
// ------------------------------------------------------------------
|
|
51
|
+
function ParseOptional(moduleProperties, parser, code, context) {
|
|
52
|
+
const result = ParseParser(moduleProperties, parser, code, context);
|
|
53
|
+
return (result.length === 2 ? [[result[0]], result[1]] : [[], code]);
|
|
42
54
|
}
|
|
43
55
|
// ------------------------------------------------------------------
|
|
44
56
|
// Ref
|
|
45
57
|
// ------------------------------------------------------------------
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const parser = properties[ref];
|
|
58
|
+
function ParseRef(moduleProperties, ref, code, context) {
|
|
59
|
+
const parser = moduleProperties[ref];
|
|
49
60
|
if (!Guard.IsParser(parser))
|
|
50
|
-
throw Error(`Cannot dereference
|
|
51
|
-
return ParseParser(
|
|
61
|
+
throw Error(`Cannot dereference Parser '${ref}'`);
|
|
62
|
+
return ParseParser(moduleProperties, parser, code, context);
|
|
52
63
|
}
|
|
53
64
|
// ------------------------------------------------------------------
|
|
54
65
|
// String
|
|
@@ -58,32 +69,49 @@ function ParseString(options, code, _context) {
|
|
|
58
69
|
return Token.String(options, code);
|
|
59
70
|
}
|
|
60
71
|
// ------------------------------------------------------------------
|
|
61
|
-
//
|
|
72
|
+
// Tuple
|
|
62
73
|
// ------------------------------------------------------------------
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
74
|
+
function ParseTuple(moduleProperties, parsers, code, context) {
|
|
75
|
+
const buffer = [];
|
|
76
|
+
let rest = code;
|
|
77
|
+
for (const parser of parsers) {
|
|
78
|
+
const result = ParseParser(moduleProperties, parser, rest, context);
|
|
79
|
+
if (result.length === 0)
|
|
80
|
+
return [];
|
|
81
|
+
buffer.push(result[0]);
|
|
82
|
+
rest = result[1];
|
|
83
|
+
}
|
|
84
|
+
return [buffer, rest];
|
|
66
85
|
}
|
|
67
86
|
// ------------------------------------------------------------------
|
|
68
|
-
//
|
|
87
|
+
// Union
|
|
69
88
|
// ------------------------------------------------------------------
|
|
70
89
|
// prettier-ignore
|
|
71
|
-
function
|
|
72
|
-
|
|
90
|
+
function ParseUnion(moduleProperties, parsers, code, context) {
|
|
91
|
+
for (const parser of parsers) {
|
|
92
|
+
const result = ParseParser(moduleProperties, parser, code, context);
|
|
93
|
+
if (result.length === 0)
|
|
94
|
+
continue;
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
return [];
|
|
73
98
|
}
|
|
74
99
|
// ------------------------------------------------------------------
|
|
75
100
|
// Parser
|
|
76
101
|
// ------------------------------------------------------------------
|
|
77
102
|
// prettier-ignore
|
|
78
|
-
function ParseParser(
|
|
79
|
-
const result = (Guard.
|
|
80
|
-
Guard.
|
|
103
|
+
function ParseParser(moduleProperties, parser, code, context) {
|
|
104
|
+
const result = (Guard.IsContext(parser) ? ParseContext(moduleProperties, parser.left, parser.right, code, context) :
|
|
105
|
+
Guard.IsArray(parser) ? ParseArray(moduleProperties, parser.parser, code, context) :
|
|
81
106
|
Guard.IsConst(parser) ? ParseConst(parser.value, code, context) :
|
|
82
|
-
Guard.
|
|
83
|
-
Guard.
|
|
84
|
-
Guard.
|
|
85
|
-
Guard.
|
|
86
|
-
|
|
107
|
+
Guard.IsIdent(parser) ? ParseIdent(code, context) :
|
|
108
|
+
Guard.IsNumber(parser) ? ParseNumber(code, context) :
|
|
109
|
+
Guard.IsOptional(parser) ? ParseOptional(moduleProperties, parser.parser, code, context) :
|
|
110
|
+
Guard.IsRef(parser) ? ParseRef(moduleProperties, parser.ref, code, context) :
|
|
111
|
+
Guard.IsString(parser) ? ParseString(parser.options, code, context) :
|
|
112
|
+
Guard.IsTuple(parser) ? ParseTuple(moduleProperties, parser.parsers, code, context) :
|
|
113
|
+
Guard.IsUnion(parser) ? ParseUnion(moduleProperties, parser.parsers, code, context) :
|
|
114
|
+
[]);
|
|
87
115
|
return (result.length === 2
|
|
88
116
|
? [parser.mapping(result[0], context), result[1]]
|
|
89
117
|
: result);
|
|
@@ -91,9 +119,9 @@ function ParseParser(parser, properties, code, context) {
|
|
|
91
119
|
/** Parses content using the given parser */
|
|
92
120
|
// prettier-ignore
|
|
93
121
|
function Parse(...args) {
|
|
94
|
-
const
|
|
95
|
-
const [
|
|
122
|
+
const withModuleProperties = typeof args[1] === 'string' ? false : true;
|
|
123
|
+
const [moduleProperties, parser, content, context] = withModuleProperties
|
|
96
124
|
? [args[0], args[1], args[2], args[3]]
|
|
97
|
-
: [args[0],
|
|
98
|
-
return ParseParser(
|
|
125
|
+
: [{}, args[0], args[1], args[2]];
|
|
126
|
+
return ParseParser(moduleProperties, parser, content, context);
|
|
99
127
|
}
|
|
@@ -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
|
|
14
|
-
export interface
|
|
15
|
-
type: '
|
|
16
|
-
|
|
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
|
|
19
|
-
export declare function
|
|
20
|
-
/** Creates a
|
|
21
|
-
export declare function
|
|
22
|
-
export type
|
|
23
|
-
export interface
|
|
24
|
-
type: '
|
|
25
|
-
|
|
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
|
|
28
|
-
export declare function
|
|
29
|
-
/** Creates
|
|
30
|
-
export declare function
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
68
|
+
/** `[TERM]` Creates an Number Parser */
|
|
66
69
|
export declare function Number<Mapping extends IMapping<string>>(mapping: Mapping): INumber<ReturnType<Mapping>>;
|
|
67
|
-
/** Creates
|
|
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>>;
|
|
@@ -2,44 +2,70 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.As = exports.Identity = void 0;
|
|
5
|
-
exports.
|
|
6
|
-
exports.
|
|
5
|
+
exports.Context = Context;
|
|
6
|
+
exports.Array = Array;
|
|
7
7
|
exports.Const = Const;
|
|
8
8
|
exports.Ref = Ref;
|
|
9
9
|
exports.String = String;
|
|
10
10
|
exports.Ident = Ident;
|
|
11
11
|
exports.Number = Number;
|
|
12
|
+
exports.Optional = Optional;
|
|
13
|
+
exports.Tuple = Tuple;
|
|
14
|
+
exports.Union = Union;
|
|
12
15
|
/** Maps input to output. This is the default Mapping */
|
|
13
16
|
const Identity = (value) => value;
|
|
14
17
|
exports.Identity = Identity;
|
|
15
18
|
/** Maps the output as the given parameter T */
|
|
19
|
+
// prettier-ignore
|
|
16
20
|
const As = (mapping) => (_) => mapping;
|
|
17
21
|
exports.As = As;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
22
|
+
/** `[Context]` Creates a Context Parser */
|
|
23
|
+
function Context(...args) {
|
|
24
|
+
const [left, right, mapping] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], args[1], exports.Identity];
|
|
25
|
+
return { type: 'Context', left, right, mapping };
|
|
21
26
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
27
|
+
/** `[EBNF]` Creates an Array Parser */
|
|
28
|
+
function Array(...args) {
|
|
29
|
+
const [parser, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], exports.Identity];
|
|
30
|
+
return { type: 'Array', parser, mapping };
|
|
25
31
|
}
|
|
32
|
+
/** `[TERM]` Creates a Const Parser */
|
|
26
33
|
function Const(...args) {
|
|
27
34
|
const [value, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], exports.Identity];
|
|
28
35
|
return { type: 'Const', value, mapping };
|
|
29
36
|
}
|
|
37
|
+
/** `[BNF]` Creates a Ref Parser. This Parser can only be used in the context of a Module */
|
|
30
38
|
function Ref(...args) {
|
|
31
39
|
const [ref, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], exports.Identity];
|
|
32
40
|
return { type: 'Ref', ref, mapping };
|
|
33
41
|
}
|
|
42
|
+
/** `[TERM]` Creates a String Parser. Options are an array of permissable quote characters */
|
|
34
43
|
function String(...params) {
|
|
35
44
|
const [options, mapping] = params.length === 2 ? [params[0], params[1]] : [params[0], exports.Identity];
|
|
36
45
|
return { type: 'String', options, mapping };
|
|
37
46
|
}
|
|
47
|
+
/** `[TERM]` Creates an Ident Parser where Ident matches any valid JavaScript identifier */
|
|
38
48
|
function Ident(...params) {
|
|
39
49
|
const mapping = params.length === 1 ? params[0] : exports.Identity;
|
|
40
50
|
return { type: 'Ident', mapping };
|
|
41
51
|
}
|
|
52
|
+
/** `[TERM]` Creates an Number Parser */
|
|
42
53
|
function Number(...params) {
|
|
43
54
|
const mapping = params.length === 1 ? params[0] : exports.Identity;
|
|
44
55
|
return { type: 'Number', mapping };
|
|
45
56
|
}
|
|
57
|
+
/** `[EBNF]` Creates an Optional Parser */
|
|
58
|
+
function Optional(...args) {
|
|
59
|
+
const [parser, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], exports.Identity];
|
|
60
|
+
return { type: 'Optional', parser, mapping };
|
|
61
|
+
}
|
|
62
|
+
/** `[BNF]` Creates a Tuple Parser */
|
|
63
|
+
function Tuple(...args) {
|
|
64
|
+
const [parsers, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], exports.Identity];
|
|
65
|
+
return { type: 'Tuple', parsers, mapping };
|
|
66
|
+
}
|
|
67
|
+
/** `[BNF]` Creates a Union parser */
|
|
68
|
+
function Union(...args) {
|
|
69
|
+
const [parsers, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], exports.Identity];
|
|
70
|
+
return { type: 'Union', parsers, mapping };
|
|
71
|
+
}
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import * as Tokens from './token';
|
|
2
2
|
import * as Types from './types';
|
|
3
|
-
type
|
|
4
|
-
type
|
|
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
|
|
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;
|