@thi.ng/parse 2.4.8 → 2.4.10
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/CHANGELOG.md +1 -1
- package/README.md +1 -1
- package/api.js +0 -1
- package/combinators/alt.js +15 -21
- package/combinators/boundary.js +10 -4
- package/combinators/check.js +7 -4
- package/combinators/dynamic.js +8 -18
- package/combinators/expect.js +4 -1
- package/combinators/lookahead.js +17 -77
- package/combinators/maybe.js +4 -1
- package/combinators/not.js +10 -17
- package/combinators/repeat.js +26 -18
- package/combinators/seq.js +14 -10
- package/combinators/wrap.js +4 -1
- package/combinators/xform.js +16 -14
- package/context.js +134 -137
- package/error.js +6 -3
- package/grammar.js +241 -185
- package/package.json +11 -9
- package/presets/alpha.js +14 -17
- package/presets/bits.js +6 -2
- package/presets/digits.js +6 -8
- package/presets/escape.js +14 -11
- package/presets/hex.js +8 -13
- package/presets/numbers.js +12 -23
- package/presets/string.js +6 -1
- package/presets/whitespace.js +18 -32
- package/prims/always.js +6 -2
- package/prims/anchor.js +24 -21
- package/prims/fail.js +4 -1
- package/prims/lit.js +8 -19
- package/prims/none-of.js +10 -15
- package/prims/one-of.js +10 -14
- package/prims/pass.js +6 -12
- package/prims/range.js +18 -37
- package/prims/satisfy.js +17 -19
- package/prims/skip.js +13 -30
- package/prims/string.js +48 -43
- package/readers/array-reader.js +22 -20
- package/readers/string-reader.js +27 -26
- package/utils.js +4 -9
- package/xform/collect.js +9 -16
- package/xform/comp.js +19 -22
- package/xform/count.js +9 -16
- package/xform/discard.js +6 -10
- package/xform/hoist.js +15 -30
- package/xform/join.js +17 -24
- package/xform/json.js +8 -15
- package/xform/nest.js +21 -32
- package/xform/number.js +16 -36
- package/xform/print.js +21 -47
- package/xform/replace.js +9 -17
- package/xform/trim.js +8 -17
- package/xform/with-id.js +6 -8
package/error.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { defError } from "@thi.ng/errors/deferror";
|
|
2
2
|
const ParseError = defError(() => `ParseError`);
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
const parseError = (ctx, msg) => {
|
|
4
|
+
const info = ctx.reader.format(ctx.scope.state);
|
|
5
|
+
throw new ParseError(msg + (info ? ` @ ${info}` : ""));
|
|
6
|
+
};
|
|
7
|
+
export {
|
|
8
|
+
parseError
|
|
6
9
|
};
|
package/grammar.js
CHANGED
|
@@ -19,7 +19,13 @@ import { FLOAT, INT, UINT } from "./presets/numbers.js";
|
|
|
19
19
|
import { STRING } from "./presets/string.js";
|
|
20
20
|
import { DNL, NL, WS, WS0, WS1 } from "./presets/whitespace.js";
|
|
21
21
|
import { always, alwaysD } from "./prims/always.js";
|
|
22
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
inputEnd,
|
|
24
|
+
inputStart,
|
|
25
|
+
lineEnd,
|
|
26
|
+
lineStart,
|
|
27
|
+
wordBoundary
|
|
28
|
+
} from "./prims/anchor.js";
|
|
23
29
|
import { lit, litD } from "./prims/lit.js";
|
|
24
30
|
import { noneOf } from "./prims/none-of.js";
|
|
25
31
|
import { oneOf } from "./prims/one-of.js";
|
|
@@ -39,32 +45,50 @@ import { xfTrim } from "./xform/trim.js";
|
|
|
39
45
|
import { withID } from "./xform/with-id.js";
|
|
40
46
|
const apos = litD("'");
|
|
41
47
|
const dash = litD("-");
|
|
42
|
-
const REPEAT = maybe(
|
|
48
|
+
const REPEAT = maybe(
|
|
49
|
+
alt([
|
|
43
50
|
oneOf("?*+", "repeat"),
|
|
44
|
-
collect(
|
|
45
|
-
|
|
46
|
-
|
|
51
|
+
collect(
|
|
52
|
+
seq(
|
|
53
|
+
[litD("{"), UINT, maybe(lit(",")), maybe(UINT), litD("}")],
|
|
54
|
+
"repeatN"
|
|
55
|
+
)
|
|
56
|
+
)
|
|
57
|
+
])
|
|
58
|
+
);
|
|
59
|
+
const DISCARD = maybe(lit("!"), void 0, "discard");
|
|
47
60
|
const CHAR_OR_ESC = alt([UNICODE, ESC, always()]);
|
|
48
61
|
const CHAR_RANGE = seq([CHAR_OR_ESC, dash, CHAR_OR_ESC], "charRange");
|
|
49
|
-
const CHAR_SEL = seq(
|
|
62
|
+
const CHAR_SEL = seq(
|
|
63
|
+
[
|
|
50
64
|
litD("["),
|
|
51
65
|
maybe(lit("^", "invert")),
|
|
52
66
|
oneOrMore(alt([CHAR_RANGE, UNICODE, noneOf("]", "char")]), "choice"),
|
|
53
|
-
litD("]")
|
|
54
|
-
],
|
|
67
|
+
litD("]")
|
|
68
|
+
],
|
|
69
|
+
"charSel"
|
|
70
|
+
);
|
|
55
71
|
const ANY = lit(".", "any");
|
|
56
72
|
const LIT = hoistResult(seq([apos, CHAR_OR_ESC, apos], "char"));
|
|
57
73
|
const SYM = join(oneOrMore(alt([ALPHA_NUM, oneOf(".-_$")]), "sym"));
|
|
58
74
|
const RULE_REF = seq([litD("<"), SYM, litD(">")], "ref");
|
|
59
75
|
const TERM_BODY = alt([RULE_REF, ANY, LIT, STRING, CHAR_SEL]);
|
|
60
|
-
const LOOK_AHEAD = maybe(
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
76
|
+
const LOOK_AHEAD = maybe(
|
|
77
|
+
seq(
|
|
78
|
+
[
|
|
79
|
+
stringD("(?"),
|
|
80
|
+
oneOf("-+"),
|
|
81
|
+
seq([TERM_BODY, REPEAT, DISCARD], "lhterm"),
|
|
82
|
+
litD(")")
|
|
83
|
+
],
|
|
84
|
+
"lhspec"
|
|
85
|
+
),
|
|
86
|
+
void 0,
|
|
87
|
+
"lhnone"
|
|
88
|
+
);
|
|
66
89
|
const TERM = seq([TERM_BODY, REPEAT, DISCARD, LOOK_AHEAD], "term");
|
|
67
|
-
const ALT = seq(
|
|
90
|
+
const ALT = seq(
|
|
91
|
+
[
|
|
68
92
|
litD("("),
|
|
69
93
|
WS0,
|
|
70
94
|
TERM,
|
|
@@ -73,10 +97,15 @@ const ALT = seq([
|
|
|
73
97
|
litD(")"),
|
|
74
98
|
REPEAT,
|
|
75
99
|
DISCARD,
|
|
76
|
-
LOOK_AHEAD
|
|
77
|
-
],
|
|
78
|
-
|
|
79
|
-
|
|
100
|
+
LOOK_AHEAD
|
|
101
|
+
],
|
|
102
|
+
"alt"
|
|
103
|
+
);
|
|
104
|
+
const RULE_XF = hoist(
|
|
105
|
+
seq([stringD("=>"), WS1, alt([SYM, RULE_REF, STRING]), WS1], "xform")
|
|
106
|
+
);
|
|
107
|
+
const RULE = seq(
|
|
108
|
+
[
|
|
80
109
|
WS0,
|
|
81
110
|
SYM,
|
|
82
111
|
WS0,
|
|
@@ -84,204 +113,231 @@ const RULE = seq([
|
|
|
84
113
|
oneOrMore(alt([TERM, ALT, WS1]), "body"),
|
|
85
114
|
maybe(RULE_XF),
|
|
86
115
|
litD(";"),
|
|
87
|
-
WS0
|
|
88
|
-
],
|
|
116
|
+
WS0
|
|
117
|
+
],
|
|
118
|
+
"rule"
|
|
119
|
+
);
|
|
89
120
|
const COMMENT = seqD([WS0, litD("#"), lookahead(always(), DNL)]);
|
|
90
|
-
|
|
121
|
+
const GRAMMAR = zeroOrMore(alt([RULE, COMMENT]), "rules");
|
|
91
122
|
const first = ($) => $.children[0];
|
|
92
123
|
const nth = ($, n) => $.children[n];
|
|
93
|
-
const compile = defmulti(
|
|
94
|
-
|
|
95
|
-
|
|
124
|
+
const compile = defmulti(
|
|
125
|
+
(scope) => scope.id,
|
|
126
|
+
{
|
|
127
|
+
unicode: "char"
|
|
128
|
+
},
|
|
129
|
+
{
|
|
96
130
|
[DEFAULT]: ($) => unsupported(`unknown op: ${$.id}`),
|
|
97
131
|
root: ($, lang, opts, flags) => {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
132
|
+
const rules = first($).children;
|
|
133
|
+
rules.reduce(
|
|
134
|
+
(acc, r) => (acc[first(r).result] = dynamic(), acc),
|
|
135
|
+
lang.rules
|
|
136
|
+
);
|
|
137
|
+
for (let r of rules) {
|
|
138
|
+
const id = first(r).result;
|
|
139
|
+
lang.rules[id].set(
|
|
140
|
+
compile(r, lang, opts, flags)
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
return lang;
|
|
105
144
|
},
|
|
106
145
|
rule: ($, lang, opts, flags) => {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (xf
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
else if (xf.id === "string") {
|
|
133
|
-
parser = xform(parser, xfReplace(xf.result));
|
|
134
|
-
}
|
|
135
|
-
return parser;
|
|
146
|
+
const [id, body, xf] = $.children;
|
|
147
|
+
opts.debug && console.log(`rule: ${id.result}`, xf);
|
|
148
|
+
const acc = [];
|
|
149
|
+
for (let b of body.children) {
|
|
150
|
+
const c = compile(b, lang, opts, flags);
|
|
151
|
+
c && acc.push(c);
|
|
152
|
+
}
|
|
153
|
+
let parser = acc.length > 1 ? seq(acc, id.result) : withID(id.result, acc[0]);
|
|
154
|
+
if (xf.id === "sym") {
|
|
155
|
+
const $xf = lang.env[xf.result];
|
|
156
|
+
if (!$xf)
|
|
157
|
+
illegalArgs(`missing xform: ${xf.result}`);
|
|
158
|
+
parser = xform(parser, $xf);
|
|
159
|
+
} else if (xf.id === "ref") {
|
|
160
|
+
const $id = first(xf).result;
|
|
161
|
+
if ($id === id)
|
|
162
|
+
illegalArgs(`self-referential: ${$id}`);
|
|
163
|
+
const $xf = lang.rules[$id];
|
|
164
|
+
if (!$xf)
|
|
165
|
+
illegalArgs(`missing xform rule: ${$id}`);
|
|
166
|
+
parser = nest(parser, $xf);
|
|
167
|
+
} else if (xf.id === "string") {
|
|
168
|
+
parser = xform(parser, xfReplace(xf.result));
|
|
169
|
+
}
|
|
170
|
+
return parser;
|
|
136
171
|
},
|
|
137
172
|
ref: ($, lang, opts, flags) => {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
? flags.discard
|
|
143
|
-
? discard(ref)
|
|
144
|
-
: ref
|
|
145
|
-
: illegalArgs(`invalid rule ref: ${id}`);
|
|
173
|
+
const id = first($).result;
|
|
174
|
+
opts.debug && console.log(`ref: ${id}`, flags);
|
|
175
|
+
const ref = lang.rules[id];
|
|
176
|
+
return ref ? flags.discard ? discard(ref) : ref : illegalArgs(`invalid rule ref: ${id}`);
|
|
146
177
|
},
|
|
147
178
|
term: ($, lang, opts, flags) => {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
179
|
+
const [term, repeat2, discard2, lookahead2] = $.children;
|
|
180
|
+
opts.debug && console.log(`term: ${term.id}`, flags);
|
|
181
|
+
return compileRDL(
|
|
182
|
+
(discard3) => compile(term, lang, opts, { ...flags, discard: discard3 }),
|
|
183
|
+
repeat2,
|
|
184
|
+
discard2,
|
|
185
|
+
lookahead2,
|
|
186
|
+
lang,
|
|
187
|
+
opts
|
|
188
|
+
);
|
|
151
189
|
},
|
|
152
190
|
lhterm: ($, lang, opts, flags) => {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
191
|
+
const [term, repeat2, discard2] = $.children;
|
|
192
|
+
opts.debug && console.log(`lhterm: ${term.id}`);
|
|
193
|
+
return compileRD(
|
|
194
|
+
(discard3) => compile(term, lang, opts, { ...flags, discard: discard3 }),
|
|
195
|
+
repeat2,
|
|
196
|
+
discard2,
|
|
197
|
+
opts
|
|
198
|
+
);
|
|
156
199
|
},
|
|
157
200
|
alt: ($, lang, opts, flags) => {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
201
|
+
opts.debug && console.log(`alt: ${$.id}`, flags);
|
|
202
|
+
const [term0, { children: terms }, repeat2, disc, lookahead2] = $.children;
|
|
203
|
+
const acc = [compile(term0, lang, opts, flags)];
|
|
204
|
+
if (terms) {
|
|
205
|
+
for (let c of terms) {
|
|
206
|
+
acc.push(compile(first(c), lang, opts, flags));
|
|
165
207
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
208
|
+
}
|
|
209
|
+
return compileRDL(
|
|
210
|
+
(optimize) => optimize || flags.discard ? acc.length > 1 ? altD(acc) : discard(acc[0]) : acc.length > 1 ? alt(acc) : acc[0],
|
|
211
|
+
repeat2,
|
|
212
|
+
disc,
|
|
213
|
+
lookahead2,
|
|
214
|
+
lang,
|
|
215
|
+
opts
|
|
216
|
+
);
|
|
173
217
|
},
|
|
174
218
|
any: (_, __, opts, flags) => {
|
|
175
|
-
|
|
176
|
-
|
|
219
|
+
opts.debug && console.log(`any`, flags);
|
|
220
|
+
return flags.discard ? alwaysD() : always("any");
|
|
177
221
|
},
|
|
178
222
|
char: ($, _, opts, flags) => {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
223
|
+
const x = $.result;
|
|
224
|
+
opts.debug && console.log(`lit: '${x}'`, flags);
|
|
225
|
+
return (flags.discard ? litD : lit)(x);
|
|
182
226
|
},
|
|
183
227
|
string: ($, _, opts, flags) => {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
228
|
+
const x = $.result;
|
|
229
|
+
opts.debug && console.log(`string: "${x}"`, flags);
|
|
230
|
+
return (flags.discard ? stringD : string)(x);
|
|
187
231
|
},
|
|
188
232
|
charRange: ($, _, opts, flags) => {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
return (flags.discard ? rangeD : range)(a.result, b.result);
|
|
233
|
+
const [a, b] = $.children;
|
|
234
|
+
opts.debug && console.log(`range: ${a.result} - ${b.result}`, flags);
|
|
235
|
+
return (flags.discard ? rangeD : range)(a.result, b.result);
|
|
193
236
|
},
|
|
194
237
|
charSel: ($, lang, opts, flags) => {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
},
|
|
204
|
-
});
|
|
205
|
-
const compileRepeat = (parser, rspec, opts) => {
|
|
206
|
-
opts.debug && console.log(`repeat: ${rspec.id}`);
|
|
207
|
-
if (rspec.id === "repeat") {
|
|
208
|
-
switch (rspec.result) {
|
|
209
|
-
case "?":
|
|
210
|
-
return maybe(parser);
|
|
211
|
-
case "*":
|
|
212
|
-
return zeroOrMore(parser);
|
|
213
|
-
case "+":
|
|
214
|
-
return oneOrMore(parser);
|
|
215
|
-
default:
|
|
216
|
-
return parser;
|
|
217
|
-
}
|
|
238
|
+
opts.debug && console.log("charSel", flags);
|
|
239
|
+
const choices = nth($, 1).children.map(
|
|
240
|
+
(c) => compile(c, lang, opts, flags)
|
|
241
|
+
);
|
|
242
|
+
const invert = first($).result;
|
|
243
|
+
const parser = choices.length > 1 ? alt(choices) : choices[0];
|
|
244
|
+
opts.debug && console.log(`invert: ${invert}`);
|
|
245
|
+
return invert ? not(parser, flags.discard ? alwaysD() : always()) : parser;
|
|
218
246
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
247
|
+
}
|
|
248
|
+
);
|
|
249
|
+
const compileRepeat = (parser, rspec, opts) => {
|
|
250
|
+
opts.debug && console.log(`repeat: ${rspec.id}`);
|
|
251
|
+
if (rspec.id === "repeat") {
|
|
252
|
+
switch (rspec.result) {
|
|
253
|
+
case "?":
|
|
254
|
+
return maybe(parser);
|
|
255
|
+
case "*":
|
|
256
|
+
return zeroOrMore(parser);
|
|
257
|
+
case "+":
|
|
258
|
+
return oneOrMore(parser);
|
|
259
|
+
default:
|
|
260
|
+
return parser;
|
|
222
261
|
}
|
|
223
|
-
|
|
262
|
+
} else if (rspec.id === "repeatN") {
|
|
263
|
+
const [n, sep, m] = rspec.result;
|
|
264
|
+
return repeat(parser, n, sep ? m || Infinity : m || n);
|
|
265
|
+
}
|
|
266
|
+
return parser;
|
|
224
267
|
};
|
|
225
268
|
const compileDiscard = (parser, dspec, opts) => {
|
|
226
|
-
|
|
227
|
-
|
|
269
|
+
opts.debug && console.log(`discard:`, dspec.result);
|
|
270
|
+
return dspec.result === "!" ? discard(parser) : parser;
|
|
228
271
|
};
|
|
229
272
|
const compileLookahead = (parser, spec, lang, opts) => {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
273
|
+
opts.debug && console.log(`lookahead:`, spec.id);
|
|
274
|
+
return spec.id === "lhspec" ? lookahead(
|
|
275
|
+
parser,
|
|
276
|
+
compile(nth(spec, 1), lang, opts, {}),
|
|
277
|
+
first(spec).result === "+"
|
|
278
|
+
) : parser;
|
|
234
279
|
};
|
|
235
|
-
const compileRD = (parser, rspec, dspec, opts) => dspec.result != null && rspec.result == null
|
|
236
|
-
|
|
237
|
-
|
|
280
|
+
const compileRD = (parser, rspec, dspec, opts) => dspec.result != null && rspec.result == null ? parser(true) : compileDiscard(
|
|
281
|
+
compileRepeat(parser(false), rspec, opts),
|
|
282
|
+
dspec,
|
|
283
|
+
opts
|
|
284
|
+
);
|
|
238
285
|
const compileRDL = (parser, rspec, dspec, lhspec, lang, opts) => compileLookahead(compileRD(parser, rspec, dspec, opts), lhspec, lang, opts);
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
286
|
+
const defGrammar = (rules, env, opts) => {
|
|
287
|
+
opts = { debug: false, optimize: true, ...opts };
|
|
288
|
+
env = {
|
|
289
|
+
binary: xfInt(2),
|
|
290
|
+
collect: xfCollect,
|
|
291
|
+
count: xfCount,
|
|
292
|
+
discard: xfDiscard,
|
|
293
|
+
float: xfFloat,
|
|
294
|
+
hex: xfInt(16),
|
|
295
|
+
hoist: xfHoist,
|
|
296
|
+
hoistR: xfHoistResult,
|
|
297
|
+
int: xfInt(10),
|
|
298
|
+
join: xfJoin,
|
|
299
|
+
json: xfJson,
|
|
300
|
+
print: xfPrint(),
|
|
301
|
+
trim: xfTrim,
|
|
302
|
+
...env
|
|
303
|
+
};
|
|
304
|
+
const ctx = defContext(rules);
|
|
305
|
+
const result = (opts.debug ? print(GRAMMAR) : GRAMMAR)(ctx);
|
|
306
|
+
if (result) {
|
|
307
|
+
return compile(
|
|
308
|
+
ctx.root,
|
|
309
|
+
{
|
|
310
|
+
env,
|
|
311
|
+
grammar: ctx,
|
|
312
|
+
rules: {
|
|
313
|
+
ALPHA_NUM,
|
|
314
|
+
ALPHA,
|
|
315
|
+
BIT,
|
|
316
|
+
DIGIT,
|
|
317
|
+
DNL,
|
|
318
|
+
END: inputEnd,
|
|
319
|
+
ESC,
|
|
320
|
+
FLOAT,
|
|
321
|
+
HEX_DIGIT,
|
|
322
|
+
INT,
|
|
323
|
+
LEND: lineEnd,
|
|
324
|
+
LSTART: lineStart,
|
|
325
|
+
NL,
|
|
326
|
+
START: inputStart,
|
|
327
|
+
STRING,
|
|
328
|
+
UNICODE,
|
|
329
|
+
WB: wordBoundary,
|
|
330
|
+
WS,
|
|
331
|
+
WS0,
|
|
332
|
+
WS1
|
|
333
|
+
}
|
|
334
|
+
},
|
|
335
|
+
opts,
|
|
336
|
+
{}
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
export {
|
|
341
|
+
GRAMMAR,
|
|
342
|
+
defGrammar
|
|
287
343
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/parse",
|
|
3
|
-
"version": "2.4.
|
|
3
|
+
"version": "2.4.10",
|
|
4
4
|
"description": "Purely functional parser combinators & AST generation for generic inputs",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -24,7 +24,9 @@
|
|
|
24
24
|
"author": "Karsten Schmidt (https://thi.ng)",
|
|
25
25
|
"license": "Apache-2.0",
|
|
26
26
|
"scripts": {
|
|
27
|
-
"build": "yarn
|
|
27
|
+
"build": "yarn build:esbuild && yarn build:decl",
|
|
28
|
+
"build:decl": "tsc --declaration --emitDeclarationOnly",
|
|
29
|
+
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
|
|
28
30
|
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc combinators presets prims readers xform",
|
|
29
31
|
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
|
|
30
32
|
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
|
|
@@ -33,15 +35,15 @@
|
|
|
33
35
|
"test": "bun test"
|
|
34
36
|
},
|
|
35
37
|
"dependencies": {
|
|
36
|
-
"@thi.ng/api": "^8.9.
|
|
37
|
-
"@thi.ng/checks": "^3.4.
|
|
38
|
-
"@thi.ng/defmulti": "^3.0.
|
|
39
|
-
"@thi.ng/errors": "^2.4.
|
|
40
|
-
"@thi.ng/strings": "^3.7.
|
|
38
|
+
"@thi.ng/api": "^8.9.12",
|
|
39
|
+
"@thi.ng/checks": "^3.4.12",
|
|
40
|
+
"@thi.ng/defmulti": "^3.0.10",
|
|
41
|
+
"@thi.ng/errors": "^2.4.6",
|
|
42
|
+
"@thi.ng/strings": "^3.7.3"
|
|
41
43
|
},
|
|
42
44
|
"devDependencies": {
|
|
43
45
|
"@microsoft/api-extractor": "^7.38.3",
|
|
44
|
-
"
|
|
46
|
+
"esbuild": "^0.19.8",
|
|
45
47
|
"rimraf": "^5.0.5",
|
|
46
48
|
"tools": "^0.0.1",
|
|
47
49
|
"typedoc": "^0.25.4",
|
|
@@ -240,5 +242,5 @@
|
|
|
240
242
|
],
|
|
241
243
|
"year": 2020
|
|
242
244
|
},
|
|
243
|
-
"gitHead": "
|
|
245
|
+
"gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
|
|
244
246
|
}
|
package/presets/alpha.js
CHANGED
|
@@ -1,19 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
ALPHA as _ALPHA,
|
|
3
|
+
ALPHA_NUM as _ALPHA_NUM
|
|
4
|
+
} from "@thi.ng/strings/groups";
|
|
2
5
|
import { oneOf } from "../prims/one-of.js";
|
|
3
6
|
import { range } from "../prims/range.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*/
|
|
15
|
-
export const ALPHA = oneOf(_ALPHA);
|
|
16
|
-
/**
|
|
17
|
-
* Matches single in {@link ALPHA} or {@link DIGIT}.
|
|
18
|
-
*/
|
|
19
|
-
export const ALPHA_NUM = oneOf(_ALPHA_NUM);
|
|
7
|
+
const LOWER_CASE = range("a", "z");
|
|
8
|
+
const UPPER_CASE = range("A", "Z");
|
|
9
|
+
const ALPHA = oneOf(_ALPHA);
|
|
10
|
+
const ALPHA_NUM = oneOf(_ALPHA_NUM);
|
|
11
|
+
export {
|
|
12
|
+
ALPHA,
|
|
13
|
+
ALPHA_NUM,
|
|
14
|
+
LOWER_CASE,
|
|
15
|
+
UPPER_CASE
|
|
16
|
+
};
|
package/presets/bits.js
CHANGED
|
@@ -2,5 +2,9 @@ import { repeat } from "../combinators/repeat.js";
|
|
|
2
2
|
import { xform } from "../combinators/xform.js";
|
|
3
3
|
import { oneOf } from "../prims/one-of.js";
|
|
4
4
|
import { xfInt } from "../xform/number.js";
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
const BIT = oneOf("01");
|
|
6
|
+
const BINARY_UINT = xform(repeat(BIT, 1, 32, "uint"), xfInt(2));
|
|
7
|
+
export {
|
|
8
|
+
BINARY_UINT,
|
|
9
|
+
BIT
|
|
10
|
+
};
|
package/presets/digits.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { oneOrMore } from "../combinators/repeat.js";
|
|
2
2
|
import { range } from "../prims/range.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
*/
|
|
10
|
-
export const DIGITS = oneOrMore(DIGIT);
|
|
3
|
+
const DIGIT = range("0", "9", "digit");
|
|
4
|
+
const DIGITS = oneOrMore(DIGIT);
|
|
5
|
+
export {
|
|
6
|
+
DIGIT,
|
|
7
|
+
DIGITS
|
|
8
|
+
};
|
package/presets/escape.js
CHANGED
|
@@ -7,15 +7,18 @@ import { litD } from "../prims/lit.js";
|
|
|
7
7
|
import { stringD } from "../prims/string.js";
|
|
8
8
|
import { xfInt } from "../xform/number.js";
|
|
9
9
|
import { HEX_DIGIT } from "./hex.js";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
const ESC = xform(seq([litD("\\"), always()], "esc"), ($) => {
|
|
11
|
+
const id = $.children[0].result;
|
|
12
|
+
const resolved = ESCAPES[id];
|
|
13
|
+
$.result = resolved !== void 0 ? resolved : `\\${id}`;
|
|
14
|
+
$.children = null;
|
|
15
|
+
return $;
|
|
16
16
|
});
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
export
|
|
17
|
+
const UNICODE = xform(
|
|
18
|
+
seq([stringD("\\u"), repeat(HEX_DIGIT, 4, 4)], "unicode"),
|
|
19
|
+
($, ctx) => ($.result = String.fromCharCode(xfInt(16)($, ctx).result), $)
|
|
20
|
+
);
|
|
21
|
+
export {
|
|
22
|
+
ESC,
|
|
23
|
+
UNICODE
|
|
24
|
+
};
|