@sinclair/typebox 0.33.19 → 0.33.21
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/index.d.ts +1 -0
- package/build/cjs/index.js +5 -1
- package/build/cjs/parse/index.d.ts +1 -0
- package/build/cjs/parse/index.js +18 -0
- package/build/cjs/parse/parse.d.ts +11 -0
- package/build/cjs/parse/parse.js +18 -0
- package/build/cjs/parse/parsebox/index.d.ts +2 -0
- package/build/cjs/parse/parsebox/index.js +6 -0
- package/build/cjs/parse/parsebox/runtime/guard.d.ts +17 -0
- package/build/cjs/parse/parsebox/runtime/guard.js +75 -0
- package/build/cjs/parse/parsebox/runtime/index.d.ts +5 -0
- package/build/cjs/parse/parsebox/runtime/index.js +23 -0
- package/build/cjs/parse/parsebox/runtime/module.d.ts +9 -0
- package/build/cjs/parse/parsebox/runtime/module.js +22 -0
- package/build/cjs/parse/parsebox/runtime/parse.d.ts +9 -0
- package/build/cjs/parse/parsebox/runtime/parse.js +99 -0
- package/build/cjs/parse/parsebox/runtime/token.d.ts +8 -0
- package/build/cjs/parse/parsebox/runtime/token.js +230 -0
- package/build/cjs/parse/parsebox/runtime/types.d.ts +68 -0
- package/build/cjs/parse/parsebox/runtime/types.js +45 -0
- package/build/cjs/parse/parsebox/static/index.d.ts +3 -0
- package/build/cjs/parse/parsebox/static/index.js +21 -0
- package/build/cjs/parse/parsebox/static/parse.d.ts +17 -0
- package/build/cjs/parse/parsebox/static/parse.js +3 -0
- package/build/cjs/parse/parsebox/static/token.d.ts +108 -0
- package/build/cjs/parse/parsebox/static/token.js +3 -0
- package/build/cjs/parse/parsebox/static/types.d.ts +46 -0
- package/build/cjs/parse/parsebox/static/types.js +3 -0
- package/build/cjs/parse/runtime.d.ts +55 -0
- package/build/cjs/parse/runtime.js +621 -0
- package/build/cjs/parse/static.d.ts +471 -0
- package/build/cjs/parse/static.js +3 -0
- package/build/esm/index.d.mts +1 -0
- package/build/esm/index.mjs +5 -1
- package/build/esm/parse/index.d.mts +1 -0
- package/build/esm/parse/index.mjs +1 -0
- package/build/esm/parse/parse.d.mts +11 -0
- package/build/esm/parse/parse.mjs +13 -0
- package/build/esm/parse/parsebox/index.d.mts +2 -0
- package/build/esm/parse/parsebox/index.mjs +2 -0
- package/build/esm/parse/parsebox/runtime/guard.d.mts +17 -0
- package/build/esm/parse/parsebox/runtime/guard.mjs +64 -0
- package/build/esm/parse/parsebox/runtime/index.d.mts +5 -0
- package/build/esm/parse/parsebox/runtime/index.mjs +5 -0
- package/build/esm/parse/parsebox/runtime/module.d.mts +9 -0
- package/build/esm/parse/parsebox/runtime/module.mjs +17 -0
- package/build/esm/parse/parsebox/runtime/parse.d.mts +9 -0
- package/build/esm/parse/parsebox/runtime/parse.mjs +95 -0
- package/build/esm/parse/parsebox/runtime/token.d.mts +8 -0
- package/build/esm/parse/parsebox/runtime/token.mjs +223 -0
- package/build/esm/parse/parsebox/runtime/types.d.mts +68 -0
- package/build/esm/parse/parsebox/runtime/types.mjs +32 -0
- package/build/esm/parse/parsebox/static/index.d.mts +3 -0
- package/build/esm/parse/parsebox/static/index.mjs +3 -0
- package/build/esm/parse/parsebox/static/parse.d.mts +17 -0
- package/build/esm/parse/parsebox/static/parse.mjs +1 -0
- package/build/esm/parse/parsebox/static/token.d.mts +108 -0
- package/build/esm/parse/parsebox/static/token.mjs +1 -0
- package/build/esm/parse/parsebox/static/types.d.mts +46 -0
- package/build/esm/parse/parsebox/static/types.mjs +1 -0
- package/build/esm/parse/runtime.d.mts +55 -0
- package/build/esm/parse/runtime.mjs +617 -0
- package/build/esm/parse/static.d.mts +471 -0
- package/build/esm/parse/static.mjs +1 -0
- package/package.json +11 -1
- package/parse/package.json +4 -0
- package/readme.md +5 -4
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Parse } from './parse.mjs';
|
|
2
|
+
// ------------------------------------------------------------------
|
|
3
|
+
// Module
|
|
4
|
+
// ------------------------------------------------------------------
|
|
5
|
+
// prettier-ignore
|
|
6
|
+
export class Module {
|
|
7
|
+
constructor(properties) {
|
|
8
|
+
this.properties = properties;
|
|
9
|
+
}
|
|
10
|
+
/** Parses using one of the parsers defined on this instance */
|
|
11
|
+
Parse(...args) {
|
|
12
|
+
const [key, code, context] = args.length === 3 ? [args[0], args[1], args[2]] :
|
|
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);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
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];
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import * as Guard from './guard.mjs';
|
|
2
|
+
import * as Token from './token.mjs';
|
|
3
|
+
// ------------------------------------------------------------------
|
|
4
|
+
// Tuple
|
|
5
|
+
// ------------------------------------------------------------------
|
|
6
|
+
// prettier-ignore
|
|
7
|
+
function ParseTuple(parsers, properties, code, context) {
|
|
8
|
+
const buffer = [];
|
|
9
|
+
let rest = code;
|
|
10
|
+
for (const parser of parsers) {
|
|
11
|
+
const result = ParseParser(parser, properties, rest, context);
|
|
12
|
+
if (result.length === 0)
|
|
13
|
+
return [];
|
|
14
|
+
buffer.push(result[0]);
|
|
15
|
+
rest = result[1];
|
|
16
|
+
}
|
|
17
|
+
return [buffer, rest];
|
|
18
|
+
}
|
|
19
|
+
// ------------------------------------------------------------------
|
|
20
|
+
// Union
|
|
21
|
+
// ------------------------------------------------------------------
|
|
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 [];
|
|
31
|
+
}
|
|
32
|
+
// ------------------------------------------------------------------
|
|
33
|
+
// Const
|
|
34
|
+
// ------------------------------------------------------------------
|
|
35
|
+
// prettier-ignore
|
|
36
|
+
function ParseConst(value, code, context) {
|
|
37
|
+
return Token.Const(value, code);
|
|
38
|
+
}
|
|
39
|
+
// ------------------------------------------------------------------
|
|
40
|
+
// Ref
|
|
41
|
+
// ------------------------------------------------------------------
|
|
42
|
+
// prettier-ignore
|
|
43
|
+
function ParseRef(ref, properties, code, context) {
|
|
44
|
+
const parser = properties[ref];
|
|
45
|
+
if (!Guard.IsParser(parser))
|
|
46
|
+
throw Error(`Cannot dereference parser '${ref}'`);
|
|
47
|
+
return ParseParser(parser, properties, code, context);
|
|
48
|
+
}
|
|
49
|
+
// ------------------------------------------------------------------
|
|
50
|
+
// String
|
|
51
|
+
// ------------------------------------------------------------------
|
|
52
|
+
// prettier-ignore
|
|
53
|
+
function ParseString(options, code, _context) {
|
|
54
|
+
return Token.String(options, code);
|
|
55
|
+
}
|
|
56
|
+
// ------------------------------------------------------------------
|
|
57
|
+
// Number
|
|
58
|
+
// ------------------------------------------------------------------
|
|
59
|
+
// prettier-ignore
|
|
60
|
+
function ParseNumber(code, _context) {
|
|
61
|
+
return Token.Number(code);
|
|
62
|
+
}
|
|
63
|
+
// ------------------------------------------------------------------
|
|
64
|
+
// Ident
|
|
65
|
+
// ------------------------------------------------------------------
|
|
66
|
+
// prettier-ignore
|
|
67
|
+
function ParseIdent(code, _context) {
|
|
68
|
+
return Token.Ident(code);
|
|
69
|
+
}
|
|
70
|
+
// ------------------------------------------------------------------
|
|
71
|
+
// Parser
|
|
72
|
+
// ------------------------------------------------------------------
|
|
73
|
+
// 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) :
|
|
77
|
+
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
|
+
[]);
|
|
83
|
+
return (result.length === 2
|
|
84
|
+
? [parser.mapping(result[0], context), result[1]]
|
|
85
|
+
: result);
|
|
86
|
+
}
|
|
87
|
+
/** Parses content using the given parser */
|
|
88
|
+
// prettier-ignore
|
|
89
|
+
export function Parse(...args) {
|
|
90
|
+
const withProperties = typeof args[1] === 'string' ? false : true;
|
|
91
|
+
const [parser, properties, code, context] = withProperties
|
|
92
|
+
? [args[0], args[1], args[2], args[3]]
|
|
93
|
+
: [args[0], {}, args[1], args[2]];
|
|
94
|
+
return ParseParser(parser, properties, code, context);
|
|
95
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Takes the next constant string value skipping any whitespace */
|
|
2
|
+
export declare function Const(value: string, code: string): [] | [string, string];
|
|
3
|
+
/** Scans for the next Ident token */
|
|
4
|
+
export declare function Ident(code: string): [] | [string, string];
|
|
5
|
+
/** Scans for the next number token */
|
|
6
|
+
export declare function Number(code: string): [string, string] | [];
|
|
7
|
+
/** Scans the next Literal String value */
|
|
8
|
+
export declare function String(options: string[], code: string): [string, string] | [];
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
// ------------------------------------------------------------------
|
|
2
|
+
// Chars
|
|
3
|
+
// ------------------------------------------------------------------
|
|
4
|
+
// prettier-ignore
|
|
5
|
+
var Chars;
|
|
6
|
+
(function (Chars) {
|
|
7
|
+
/** Returns true if the char code is a whitespace */
|
|
8
|
+
function IsWhitespace(value) {
|
|
9
|
+
return value === 32;
|
|
10
|
+
}
|
|
11
|
+
Chars.IsWhitespace = IsWhitespace;
|
|
12
|
+
/** Returns true if the char code is a newline */
|
|
13
|
+
function IsNewline(value) {
|
|
14
|
+
return value === 10;
|
|
15
|
+
}
|
|
16
|
+
Chars.IsNewline = IsNewline;
|
|
17
|
+
/** Returns true if the char code is a alpha */
|
|
18
|
+
function IsAlpha(value) {
|
|
19
|
+
return ((value >= 65 && value <= 90) || // A-Z
|
|
20
|
+
(value >= 97 && value <= 122) // a-z
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
Chars.IsAlpha = IsAlpha;
|
|
24
|
+
/** Returns true if the char code is zero */
|
|
25
|
+
function IsZero(value) {
|
|
26
|
+
return value === 48;
|
|
27
|
+
}
|
|
28
|
+
Chars.IsZero = IsZero;
|
|
29
|
+
/** Returns true if the char code is non-zero */
|
|
30
|
+
function IsNonZero(value) {
|
|
31
|
+
return value >= 49 && value <= 57;
|
|
32
|
+
}
|
|
33
|
+
Chars.IsNonZero = IsNonZero;
|
|
34
|
+
/** Returns true if the char code is a digit */
|
|
35
|
+
function IsDigit(value) {
|
|
36
|
+
return (IsNonZero(value) ||
|
|
37
|
+
IsZero(value));
|
|
38
|
+
}
|
|
39
|
+
Chars.IsDigit = IsDigit;
|
|
40
|
+
/** Returns true if the char code is a dot */
|
|
41
|
+
function IsDot(value) {
|
|
42
|
+
return value === 46;
|
|
43
|
+
}
|
|
44
|
+
Chars.IsDot = IsDot;
|
|
45
|
+
/** Returns true if this char code is a underscore */
|
|
46
|
+
function IsUnderscore(value) {
|
|
47
|
+
return value === 95;
|
|
48
|
+
}
|
|
49
|
+
Chars.IsUnderscore = IsUnderscore;
|
|
50
|
+
/** Returns true if this char code is a dollar sign */
|
|
51
|
+
function IsDollarSign(value) {
|
|
52
|
+
return value === 36;
|
|
53
|
+
}
|
|
54
|
+
Chars.IsDollarSign = IsDollarSign;
|
|
55
|
+
})(Chars || (Chars = {}));
|
|
56
|
+
// ------------------------------------------------------------------
|
|
57
|
+
// Trim
|
|
58
|
+
// ------------------------------------------------------------------
|
|
59
|
+
// prettier-ignore
|
|
60
|
+
var Trim;
|
|
61
|
+
(function (Trim) {
|
|
62
|
+
/** Trims Whitespace and retains Newline, Tabspaces, etc. */
|
|
63
|
+
function TrimWhitespaceOnly(code) {
|
|
64
|
+
for (let i = 0; i < code.length; i++) {
|
|
65
|
+
if (Chars.IsWhitespace(code.charCodeAt(i)))
|
|
66
|
+
continue;
|
|
67
|
+
return code.slice(i);
|
|
68
|
+
}
|
|
69
|
+
return code;
|
|
70
|
+
}
|
|
71
|
+
Trim.TrimWhitespaceOnly = TrimWhitespaceOnly;
|
|
72
|
+
/** Trims Whitespace including Newline, Tabspaces, etc. */
|
|
73
|
+
function TrimAll(code) {
|
|
74
|
+
return code.trimStart();
|
|
75
|
+
}
|
|
76
|
+
Trim.TrimAll = TrimAll;
|
|
77
|
+
})(Trim || (Trim = {}));
|
|
78
|
+
// ------------------------------------------------------------------
|
|
79
|
+
// Const
|
|
80
|
+
// ------------------------------------------------------------------
|
|
81
|
+
/** Checks the value matches the next string */
|
|
82
|
+
// prettier-ignore
|
|
83
|
+
function NextTokenCheck(value, code) {
|
|
84
|
+
if (value.length > code.length)
|
|
85
|
+
return false;
|
|
86
|
+
for (let i = 0; i < value.length; i++) {
|
|
87
|
+
if (value.charCodeAt(i) !== code.charCodeAt(i))
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
/** Gets the next constant string value or empty if no match */
|
|
93
|
+
// prettier-ignore
|
|
94
|
+
function NextConst(value, code) {
|
|
95
|
+
return NextTokenCheck(value, code)
|
|
96
|
+
? [code.slice(0, value.length), code.slice(value.length)]
|
|
97
|
+
: [];
|
|
98
|
+
}
|
|
99
|
+
/** Takes the next constant string value skipping any whitespace */
|
|
100
|
+
// prettier-ignore
|
|
101
|
+
export function Const(value, code) {
|
|
102
|
+
if (value.length === 0)
|
|
103
|
+
return ['', code];
|
|
104
|
+
const char_0 = value.charCodeAt(0);
|
|
105
|
+
return (Chars.IsNewline(char_0) ? NextConst(value, Trim.TrimWhitespaceOnly(code)) :
|
|
106
|
+
Chars.IsWhitespace(char_0) ? NextConst(value, code) :
|
|
107
|
+
NextConst(value, Trim.TrimAll(code)));
|
|
108
|
+
}
|
|
109
|
+
// ------------------------------------------------------------------
|
|
110
|
+
// Ident
|
|
111
|
+
// ------------------------------------------------------------------
|
|
112
|
+
// prettier-ignore
|
|
113
|
+
function IdentIsFirst(char) {
|
|
114
|
+
return (Chars.IsAlpha(char) ||
|
|
115
|
+
Chars.IsDollarSign(char) ||
|
|
116
|
+
Chars.IsUnderscore(char));
|
|
117
|
+
}
|
|
118
|
+
// prettier-ignore
|
|
119
|
+
function IdentIsRest(char) {
|
|
120
|
+
return (Chars.IsAlpha(char) ||
|
|
121
|
+
Chars.IsDigit(char) ||
|
|
122
|
+
Chars.IsDollarSign(char) ||
|
|
123
|
+
Chars.IsUnderscore(char));
|
|
124
|
+
}
|
|
125
|
+
// prettier-ignore
|
|
126
|
+
function NextIdent(code) {
|
|
127
|
+
if (!IdentIsFirst(code.charCodeAt(0)))
|
|
128
|
+
return [];
|
|
129
|
+
for (let i = 1; i < code.length; i++) {
|
|
130
|
+
const char = code.charCodeAt(i);
|
|
131
|
+
if (IdentIsRest(char))
|
|
132
|
+
continue;
|
|
133
|
+
const slice = code.slice(0, i);
|
|
134
|
+
const rest = code.slice(i);
|
|
135
|
+
return [slice, rest];
|
|
136
|
+
}
|
|
137
|
+
return [code, ''];
|
|
138
|
+
}
|
|
139
|
+
/** Scans for the next Ident token */
|
|
140
|
+
// prettier-ignore
|
|
141
|
+
export function Ident(code) {
|
|
142
|
+
return NextIdent(Trim.TrimAll(code));
|
|
143
|
+
}
|
|
144
|
+
// ------------------------------------------------------------------
|
|
145
|
+
// Number
|
|
146
|
+
// ------------------------------------------------------------------
|
|
147
|
+
/** Checks that the next number is not a leading zero */
|
|
148
|
+
// prettier-ignore
|
|
149
|
+
function NumberLeadingZeroCheck(code, index) {
|
|
150
|
+
const char_0 = code.charCodeAt(index + 0);
|
|
151
|
+
const char_1 = code.charCodeAt(index + 1);
|
|
152
|
+
return ((
|
|
153
|
+
// 1-9
|
|
154
|
+
Chars.IsNonZero(char_0)) || (
|
|
155
|
+
// 0
|
|
156
|
+
Chars.IsZero(char_0) &&
|
|
157
|
+
!Chars.IsDigit(char_1)) || (
|
|
158
|
+
// 0.
|
|
159
|
+
Chars.IsZero(char_0) &&
|
|
160
|
+
Chars.IsDot(char_1)) || (
|
|
161
|
+
// .0
|
|
162
|
+
Chars.IsDot(char_0) &&
|
|
163
|
+
Chars.IsDigit(char_1)));
|
|
164
|
+
}
|
|
165
|
+
/** Gets the next number token */
|
|
166
|
+
// prettier-ignore
|
|
167
|
+
function NextNumber(code) {
|
|
168
|
+
const negated = code.charAt(0) === '-';
|
|
169
|
+
const index = negated ? 1 : 0;
|
|
170
|
+
if (!NumberLeadingZeroCheck(code, index)) {
|
|
171
|
+
return [];
|
|
172
|
+
}
|
|
173
|
+
const dash = negated ? '-' : '';
|
|
174
|
+
let hasDot = false;
|
|
175
|
+
for (let i = index; i < code.length; i++) {
|
|
176
|
+
const char_i = code.charCodeAt(i);
|
|
177
|
+
if (Chars.IsDigit(char_i)) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (Chars.IsDot(char_i)) {
|
|
181
|
+
if (hasDot) {
|
|
182
|
+
const slice = code.slice(index, i);
|
|
183
|
+
const rest = code.slice(i);
|
|
184
|
+
return [`${dash}${slice}`, rest];
|
|
185
|
+
}
|
|
186
|
+
hasDot = true;
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
const slice = code.slice(index, i);
|
|
190
|
+
const rest = code.slice(i);
|
|
191
|
+
return [`${dash}${slice}`, rest];
|
|
192
|
+
}
|
|
193
|
+
return [code, ''];
|
|
194
|
+
}
|
|
195
|
+
/** Scans for the next number token */
|
|
196
|
+
// prettier-ignore
|
|
197
|
+
export function Number(code) {
|
|
198
|
+
return NextNumber(Trim.TrimAll(code));
|
|
199
|
+
}
|
|
200
|
+
// ------------------------------------------------------------------
|
|
201
|
+
// String
|
|
202
|
+
// ------------------------------------------------------------------
|
|
203
|
+
// prettier-ignore
|
|
204
|
+
function NextString(options, code) {
|
|
205
|
+
const first = code.charAt(0);
|
|
206
|
+
if (!options.includes(first))
|
|
207
|
+
return [];
|
|
208
|
+
const quote = first;
|
|
209
|
+
for (let i = 1; i < code.length; i++) {
|
|
210
|
+
const char = code.charAt(i);
|
|
211
|
+
if (char === quote) {
|
|
212
|
+
const slice = code.slice(1, i);
|
|
213
|
+
const rest = code.slice(i + 1);
|
|
214
|
+
return [slice, rest];
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return [];
|
|
218
|
+
}
|
|
219
|
+
/** Scans the next Literal String value */
|
|
220
|
+
// prettier-ignore
|
|
221
|
+
export function String(options, code) {
|
|
222
|
+
return NextString(options, Trim.TrimAll(code));
|
|
223
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
export type IModuleProperties = Record<PropertyKey, IParser>;
|
|
2
|
+
/** Infers the Output Parameter for a Parser */
|
|
3
|
+
export type StaticParser<Parser extends IParser> = Parser extends IParser<infer Output extends unknown> ? Output : unknown;
|
|
4
|
+
export type IMapping<Input extends unknown = any, Output extends unknown = unknown> = (input: Input, context: any) => Output;
|
|
5
|
+
/** Maps input to output. This is the default Mapping */
|
|
6
|
+
export declare const Identity: (value: unknown) => unknown;
|
|
7
|
+
/** Maps the output as the given parameter T */
|
|
8
|
+
export declare const As: <T>(mapping: T) => ((value: unknown) => T);
|
|
9
|
+
export interface IParser<Output extends unknown = unknown> {
|
|
10
|
+
type: string;
|
|
11
|
+
mapping: IMapping<any, Output>;
|
|
12
|
+
}
|
|
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[];
|
|
17
|
+
}
|
|
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[];
|
|
26
|
+
}
|
|
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>>;
|
|
31
|
+
export interface IConst<Output extends unknown = unknown> extends IParser<Output> {
|
|
32
|
+
type: 'Const';
|
|
33
|
+
value: string;
|
|
34
|
+
}
|
|
35
|
+
/** Creates a Const parser */
|
|
36
|
+
export declare function Const<Value extends string, Mapping extends IMapping<Value>>(value: Value, mapping: Mapping): IConst<ReturnType<Mapping>>;
|
|
37
|
+
/** Creates a Const parser */
|
|
38
|
+
export declare function Const<Value extends string>(value: Value): IConst<Value>;
|
|
39
|
+
export interface IRef<Output extends unknown = unknown> extends IParser<Output> {
|
|
40
|
+
type: 'Ref';
|
|
41
|
+
ref: string;
|
|
42
|
+
}
|
|
43
|
+
/** Creates a Ref parser */
|
|
44
|
+
export declare function Ref<Type extends unknown, Mapping extends IMapping<Type>>(ref: string, mapping: Mapping): IRef<ReturnType<Mapping>>;
|
|
45
|
+
/** Creates a Ref parser */
|
|
46
|
+
export declare function Ref<Type extends unknown>(ref: string): IRef<Type>;
|
|
47
|
+
export interface IString<Output extends unknown = unknown> extends IParser<Output> {
|
|
48
|
+
type: 'String';
|
|
49
|
+
options: string[];
|
|
50
|
+
}
|
|
51
|
+
/** Creates a String Parser. Options are an array of permissable quote characters */
|
|
52
|
+
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 */
|
|
54
|
+
export declare function String(options: string[]): IString<string>;
|
|
55
|
+
export interface IIdent<Output extends unknown = unknown> extends IParser<Output> {
|
|
56
|
+
type: 'Ident';
|
|
57
|
+
}
|
|
58
|
+
/** Creates an Ident parser */
|
|
59
|
+
export declare function Ident<Mapping extends IMapping<string>>(mapping: Mapping): IIdent<ReturnType<Mapping>>;
|
|
60
|
+
/** Creates an Ident parser */
|
|
61
|
+
export declare function Ident(): IIdent<string>;
|
|
62
|
+
export interface INumber<Output extends unknown = unknown> extends IParser<Output> {
|
|
63
|
+
type: 'Number';
|
|
64
|
+
}
|
|
65
|
+
/** Creates a Number parser */
|
|
66
|
+
export declare function Number<Mapping extends IMapping<string>>(mapping: Mapping): INumber<ReturnType<Mapping>>;
|
|
67
|
+
/** Creates a Number parser */
|
|
68
|
+
export declare function Number(): INumber<string>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** Maps input to output. This is the default Mapping */
|
|
2
|
+
export const Identity = (value) => value;
|
|
3
|
+
/** Maps the output as the given parameter T */
|
|
4
|
+
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 };
|
|
8
|
+
}
|
|
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 };
|
|
12
|
+
}
|
|
13
|
+
export function Const(...args) {
|
|
14
|
+
const [value, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], Identity];
|
|
15
|
+
return { type: 'Const', value, mapping };
|
|
16
|
+
}
|
|
17
|
+
export function Ref(...args) {
|
|
18
|
+
const [ref, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], Identity];
|
|
19
|
+
return { type: 'Ref', ref, mapping };
|
|
20
|
+
}
|
|
21
|
+
export function String(...params) {
|
|
22
|
+
const [options, mapping] = params.length === 2 ? [params[0], params[1]] : [params[0], Identity];
|
|
23
|
+
return { type: 'String', options, mapping };
|
|
24
|
+
}
|
|
25
|
+
export function Ident(...params) {
|
|
26
|
+
const mapping = params.length === 1 ? params[0] : Identity;
|
|
27
|
+
return { type: 'Ident', mapping };
|
|
28
|
+
}
|
|
29
|
+
export function Number(...params) {
|
|
30
|
+
const mapping = params.length === 1 ? params[0] : Identity;
|
|
31
|
+
return { type: 'Number', mapping };
|
|
32
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as Tokens from './token.mjs';
|
|
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> : []);
|
|
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
|
+
type IdentParser<Code extends string, _Context extends unknown> = (Tokens.Ident<Code> extends [infer Match extends string, infer Rest extends string] ? [Match, Rest] : []);
|
|
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 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
|
+
]);
|
|
11
|
+
type ParseMapping<Parser extends Types.IParser, Result extends unknown, Context extends unknown = unknown> = ((Parser['mapping'] & {
|
|
12
|
+
input: Result;
|
|
13
|
+
context: Context;
|
|
14
|
+
})['output']);
|
|
15
|
+
/** Parses code with the given parser */
|
|
16
|
+
export type Parse<Type extends Types.IParser, Code extends string, Context extends unknown = unknown> = (ParseCode<Type, Code, Context> extends [infer L extends unknown, infer R extends string] ? [ParseMapping<Type, L, Context>, R] : []);
|
|
17
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
declare namespace Chars {
|
|
2
|
+
type Empty = '';
|
|
3
|
+
type Space = ' ';
|
|
4
|
+
type Newline = '\n';
|
|
5
|
+
type Dot = '.';
|
|
6
|
+
type Hyphen = '-';
|
|
7
|
+
type Digit = [
|
|
8
|
+
'0',
|
|
9
|
+
'1',
|
|
10
|
+
'2',
|
|
11
|
+
'3',
|
|
12
|
+
'4',
|
|
13
|
+
'5',
|
|
14
|
+
'6',
|
|
15
|
+
'7',
|
|
16
|
+
'8',
|
|
17
|
+
'9'
|
|
18
|
+
];
|
|
19
|
+
type Alpha = [
|
|
20
|
+
'a',
|
|
21
|
+
'b',
|
|
22
|
+
'c',
|
|
23
|
+
'd',
|
|
24
|
+
'e',
|
|
25
|
+
'f',
|
|
26
|
+
'g',
|
|
27
|
+
'h',
|
|
28
|
+
'i',
|
|
29
|
+
'j',
|
|
30
|
+
'k',
|
|
31
|
+
'l',
|
|
32
|
+
'm',
|
|
33
|
+
'n',
|
|
34
|
+
'o',
|
|
35
|
+
'p',
|
|
36
|
+
'q',
|
|
37
|
+
'r',
|
|
38
|
+
's',
|
|
39
|
+
't',
|
|
40
|
+
'u',
|
|
41
|
+
'v',
|
|
42
|
+
'w',
|
|
43
|
+
'x',
|
|
44
|
+
'y',
|
|
45
|
+
'z',
|
|
46
|
+
'A',
|
|
47
|
+
'B',
|
|
48
|
+
'C',
|
|
49
|
+
'D',
|
|
50
|
+
'E',
|
|
51
|
+
'F',
|
|
52
|
+
'G',
|
|
53
|
+
'H',
|
|
54
|
+
'I',
|
|
55
|
+
'J',
|
|
56
|
+
'K',
|
|
57
|
+
'L',
|
|
58
|
+
'M',
|
|
59
|
+
'N',
|
|
60
|
+
'O',
|
|
61
|
+
'P',
|
|
62
|
+
'Q',
|
|
63
|
+
'R',
|
|
64
|
+
'S',
|
|
65
|
+
'T',
|
|
66
|
+
'U',
|
|
67
|
+
'V',
|
|
68
|
+
'W',
|
|
69
|
+
'X',
|
|
70
|
+
'Y',
|
|
71
|
+
'Z'
|
|
72
|
+
];
|
|
73
|
+
}
|
|
74
|
+
declare namespace Trim {
|
|
75
|
+
type W4 = `${W3}${W3}`;
|
|
76
|
+
type W3 = `${W2}${W2}`;
|
|
77
|
+
type W2 = `${W1}${W1}`;
|
|
78
|
+
type W1 = `${W0}${W0}`;
|
|
79
|
+
type W0 = ` `;
|
|
80
|
+
/** Trims whitespace only */
|
|
81
|
+
export type TrimWhitespace<Code extends string> = (Code extends `${W4}${infer Rest extends string}` ? TrimWhitespace<Rest> : Code extends `${W3}${infer Rest extends string}` ? TrimWhitespace<Rest> : Code extends `${W1}${infer Rest extends string}` ? TrimWhitespace<Rest> : Code extends `${W0}${infer Rest extends string}` ? TrimWhitespace<Rest> : Code);
|
|
82
|
+
/** Trims Whitespace and Newline */
|
|
83
|
+
export type TrimAll<Code extends string> = (Code extends `${W4}${infer Rest extends string}` ? TrimAll<Rest> : Code extends `${W3}${infer Rest extends string}` ? TrimAll<Rest> : Code extends `${W1}${infer Rest extends string}` ? TrimAll<Rest> : Code extends `${W0}${infer Rest extends string}` ? TrimAll<Rest> : Code extends `${Chars.Newline}${infer Rest extends string}` ? TrimAll<Rest> : Code);
|
|
84
|
+
export {};
|
|
85
|
+
}
|
|
86
|
+
/** Scans for the next match union */
|
|
87
|
+
type NextUnion<Variants extends string[], Code extends string> = (Variants extends [infer Variant extends string, ...infer Rest1 extends string[]] ? NextConst<Variant, Code> extends [infer Match extends string, infer Rest2 extends string] ? [Match, Rest2] : NextUnion<Rest1, Code> : []);
|
|
88
|
+
type NextConst<Value extends string, Code extends string> = (Code extends `${Value}${infer Rest extends string}` ? [Value, Rest] : []);
|
|
89
|
+
/** Scans for the next constant value */
|
|
90
|
+
export type Const<Value extends string, Code extends string> = (Value extends '' ? ['', Code] : Value extends `${infer First extends string}${string}` ? (First extends Chars.Newline ? NextConst<Value, Trim.TrimWhitespace<Code>> : First extends Chars.Space ? NextConst<Value, Code> : NextConst<Value, Trim.TrimAll<Code>>) : never);
|
|
91
|
+
type NextNumberNegate<Code extends string> = (Code extends `${Chars.Hyphen}${infer Rest extends string}` ? [Chars.Hyphen, Rest] : [Chars.Empty, Code]);
|
|
92
|
+
type NextNumberZeroCheck<Code extends string> = (Code extends `0${infer Rest}` ? NextUnion<Chars.Digit, Rest> extends [string, string] ? false : true : true);
|
|
93
|
+
type NextNumberScan<Code extends string, HasDecimal extends boolean = false, Result extends string = Chars.Empty> = (NextUnion<[...Chars.Digit, Chars.Dot], Code> extends [infer Char extends string, infer Rest extends string] ? Char extends Chars.Dot ? HasDecimal extends false ? NextNumberScan<Rest, true, `${Result}${Char}`> : [Result, `.${Rest}`] : NextNumberScan<Rest, HasDecimal, `${Result}${Char}`> : [Result, Code]);
|
|
94
|
+
export type NextNumber<Code extends string> = (NextNumberNegate<Code> extends [infer Negate extends string, infer Rest extends string] ? NextNumberZeroCheck<Rest> extends true ? NextNumberScan<Rest> extends [infer Number extends string, infer Rest2 extends string] ? Number extends Chars.Empty ? [] : [`${Negate}${Number}`, Rest2] : [] : [] : []);
|
|
95
|
+
/** Scans for the next literal number */
|
|
96
|
+
export type Number<Code extends string> = NextNumber<Trim.TrimAll<Code>>;
|
|
97
|
+
type NextStringQuote<Options extends string[], Code extends string> = NextUnion<Options, Code>;
|
|
98
|
+
type NextStringBody<Code extends string, Quote extends string, Result extends string = Chars.Empty> = (Code extends `${infer Char extends string}${infer Rest extends string}` ? Char extends Quote ? [Result, Rest] : NextStringBody<Rest, Quote, `${Result}${Char}`> : []);
|
|
99
|
+
type NextString<Options extends string[], Code extends string> = (NextStringQuote<Options, Code> extends [infer Quote extends string, infer Rest extends string] ? NextStringBody<Rest, Quote> extends [infer String extends string, infer Rest extends string] ? [String, Rest] : [] : []);
|
|
100
|
+
/** Scans for the next literal string */
|
|
101
|
+
export type String<Options extends string[], Code extends string> = NextString<Options, Trim.TrimAll<Code>>;
|
|
102
|
+
type IdentLeft = [...Chars.Alpha, '_', '$'];
|
|
103
|
+
type IdentRight = [...Chars.Digit, ...IdentLeft];
|
|
104
|
+
type NextIdentScan<Code extends string, Result extends string = Chars.Empty> = (NextUnion<IdentRight, Code> extends [infer Char extends string, infer Rest extends string] ? NextIdentScan<Rest, `${Result}${Char}`> : [Result, Code]);
|
|
105
|
+
type NextIdent<Code extends string> = (NextUnion<IdentLeft, Code> extends [infer Left extends string, infer Rest1 extends string] ? NextIdentScan<Rest1> extends [infer Right extends string, infer Rest2 extends string] ? [`${Left}${Right}`, Rest2] : [] : []);
|
|
106
|
+
/** Scans for the next Ident */
|
|
107
|
+
export type Ident<Code extends string> = NextIdent<Trim.TrimAll<Code>>;
|
|
108
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export interface IMapping {
|
|
2
|
+
context: unknown;
|
|
3
|
+
input: unknown;
|
|
4
|
+
output: unknown;
|
|
5
|
+
}
|
|
6
|
+
/** Maps input to output. This is the default Mapping */
|
|
7
|
+
export interface Identity extends IMapping {
|
|
8
|
+
output: this['input'];
|
|
9
|
+
}
|
|
10
|
+
/** Maps the output as the given parameter T */
|
|
11
|
+
export interface As<T> extends IMapping {
|
|
12
|
+
output: T;
|
|
13
|
+
}
|
|
14
|
+
/** Base type Parser implemented by all other parsers */
|
|
15
|
+
export interface IParser<Mapping extends IMapping = Identity> {
|
|
16
|
+
type: string;
|
|
17
|
+
mapping: Mapping;
|
|
18
|
+
}
|
|
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];
|
|
23
|
+
}
|
|
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];
|
|
28
|
+
}
|
|
29
|
+
/** Creates a Const Parser */
|
|
30
|
+
export interface Const<Value extends string = string, Mapping extends IMapping = Identity> extends IParser<Mapping> {
|
|
31
|
+
type: 'Const';
|
|
32
|
+
value: Value;
|
|
33
|
+
}
|
|
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. */
|
|
40
|
+
export interface Ident<Mapping extends IMapping = Identity> extends IParser<Mapping> {
|
|
41
|
+
type: 'Ident';
|
|
42
|
+
}
|
|
43
|
+
/** Creates a Number Parser. */
|
|
44
|
+
export interface Number<Mapping extends IMapping = Identity> extends IParser<Mapping> {
|
|
45
|
+
type: 'Number';
|
|
46
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|