parseman 0.26.1 → 0.26.3
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/dist/compiler/codegen.d.ts.map +1 -1
- package/dist/compiler/ir-serialize.d.ts.map +1 -1
- package/dist/index.cjs +78 -24
- package/dist/index.cjs.map +3 -3
- package/dist/index.js +78 -24
- package/dist/index.js.map +3 -3
- package/dist/plugin/evaluator.d.ts.map +1 -1
- package/dist/plugin/index.cjs +121 -25
- package/dist/plugin/index.cjs.map +4 -4
- package/dist/plugin/index.js +121 -25
- package/dist/plugin/index.js.map +4 -4
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"evaluator.d.ts","sourceRoot":"","sources":["../../src/plugin/evaluator.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,KAAK,EACV,UAAU,EAAE,IAAI,EAMjB,MAAM,oBAAoB,CAAA;AAC3B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"evaluator.d.ts","sourceRoot":"","sources":["../../src/plugin/evaluator.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,KAAK,EACV,UAAU,EAAE,IAAI,EAMjB,MAAM,oBAAoB,CAAA;AAC3B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AA2E7C,MAAM,MAAM,UAAU,GAAG;IACvB,KAAK,EAAE,UAAU,CAAC,OAAO,CAAC,CAAA;IAC1B,MAAM,EAAE,MAAM,EAAE,CAAA;CACjB,CAAA;AACD,MAAM,MAAM,KAAK,GAAG,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,CAAA;AAM3C,KAAK,gBAAgB,GAAG;IAAE,GAAG,EAAE,aAAa,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAA;AA4dhE,0EAA0E;AAC1E,wBAAgB,mBAAmB,CACjC,IAAI,EAA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,EAAE,KAAK,GAAG,OAAO,CAcnF"}
|
package/dist/plugin/index.cjs
CHANGED
|
@@ -5565,7 +5565,7 @@ function emitDispatch(p, ctx, pos) {
|
|
|
5565
5565
|
ctx.indent = 1;
|
|
5566
5566
|
ctx.failLabel = "_pfail";
|
|
5567
5567
|
ctx.recordFail = true;
|
|
5568
|
-
const innerR =
|
|
5568
|
+
const innerR = emitDispatch(innerParser, ctx, "_pos");
|
|
5569
5569
|
ctx.indent = savedIndent;
|
|
5570
5570
|
ctx.failLabel = savedFailLabel;
|
|
5571
5571
|
ctx.recordFail = savedRecord;
|
|
@@ -6282,6 +6282,23 @@ function escapeClassChar(ch) {
|
|
|
6282
6282
|
return ch.replace(/[\\\]^-]/g, "\\$&");
|
|
6283
6283
|
}
|
|
6284
6284
|
|
|
6285
|
+
// src/combinators/withCtx.ts
|
|
6286
|
+
function withCtx(extra, combinator) {
|
|
6287
|
+
const meta = {
|
|
6288
|
+
firstSet: combinator._meta.firstSet,
|
|
6289
|
+
canMatchNewline: combinator._meta.canMatchNewline,
|
|
6290
|
+
isTrivia: false
|
|
6291
|
+
};
|
|
6292
|
+
return {
|
|
6293
|
+
_tag: "withCtx",
|
|
6294
|
+
_meta: meta,
|
|
6295
|
+
_def: { tag: "withCtx", extra, parser: combinator },
|
|
6296
|
+
parse(input, pos, ctx) {
|
|
6297
|
+
return combinator.parse(input, pos, { ...ctx, state: extra });
|
|
6298
|
+
}
|
|
6299
|
+
};
|
|
6300
|
+
}
|
|
6301
|
+
|
|
6285
6302
|
// src/compiler/ir-serialize.ts
|
|
6286
6303
|
function childrenOf2(def) {
|
|
6287
6304
|
switch (def.tag) {
|
|
@@ -6298,6 +6315,7 @@ function childrenOf2(def) {
|
|
|
6298
6315
|
case "field":
|
|
6299
6316
|
case "not":
|
|
6300
6317
|
case "node":
|
|
6318
|
+
case "withCtx":
|
|
6301
6319
|
case "expect":
|
|
6302
6320
|
return [def.parser];
|
|
6303
6321
|
case "grammar":
|
|
@@ -6313,7 +6331,6 @@ function childrenOf2(def) {
|
|
|
6313
6331
|
case "regex":
|
|
6314
6332
|
case "keywords":
|
|
6315
6333
|
case "guard":
|
|
6316
|
-
case "withCtx":
|
|
6317
6334
|
case "recover":
|
|
6318
6335
|
case "unknown":
|
|
6319
6336
|
return [];
|
|
@@ -6360,6 +6377,45 @@ function evalRuleMapIR(ir) {
|
|
|
6360
6377
|
n._def.buildSrc = src;
|
|
6361
6378
|
return n;
|
|
6362
6379
|
};
|
|
6380
|
+
const _gch = (items) => {
|
|
6381
|
+
const gateSrcs = [];
|
|
6382
|
+
const arms = items.map((it) => {
|
|
6383
|
+
if (Array.isArray(it)) {
|
|
6384
|
+
const [src, comb] = it;
|
|
6385
|
+
gateSrcs.push(src);
|
|
6386
|
+
let gate;
|
|
6387
|
+
try {
|
|
6388
|
+
gate = (0, eval)(`(${src})`);
|
|
6389
|
+
} catch {
|
|
6390
|
+
gate = () => {
|
|
6391
|
+
throw new Error("IR gate fn not materialized");
|
|
6392
|
+
};
|
|
6393
|
+
}
|
|
6394
|
+
return { gate, combinator: comb };
|
|
6395
|
+
}
|
|
6396
|
+
gateSrcs.push(null);
|
|
6397
|
+
return it;
|
|
6398
|
+
});
|
|
6399
|
+
const c = choice(...arms);
|
|
6400
|
+
const gates = c._def.gates;
|
|
6401
|
+
if (gates && gates.length !== gateSrcs.length) {
|
|
6402
|
+
throw new Error(`_gch: gateSrcs length ${gateSrcs.length} != choice gates length ${gates.length}`);
|
|
6403
|
+
}
|
|
6404
|
+
;
|
|
6405
|
+
c._def.gateSrcs = gateSrcs;
|
|
6406
|
+
return c;
|
|
6407
|
+
};
|
|
6408
|
+
const _wc = (src, inner) => {
|
|
6409
|
+
let extra;
|
|
6410
|
+
try {
|
|
6411
|
+
extra = (0, eval)(`(${src})`);
|
|
6412
|
+
} catch {
|
|
6413
|
+
extra = void 0;
|
|
6414
|
+
}
|
|
6415
|
+
const w = withCtx(extra, inner);
|
|
6416
|
+
w._def.extraSrc = src;
|
|
6417
|
+
return w;
|
|
6418
|
+
};
|
|
6363
6419
|
const fn = new Function(
|
|
6364
6420
|
"rules",
|
|
6365
6421
|
"ref",
|
|
@@ -6385,6 +6441,8 @@ function evalRuleMapIR(ir) {
|
|
|
6385
6441
|
"expect",
|
|
6386
6442
|
"_tf",
|
|
6387
6443
|
"_nd",
|
|
6444
|
+
"_gch",
|
|
6445
|
+
"_wc",
|
|
6388
6446
|
`return (${ir})`
|
|
6389
6447
|
);
|
|
6390
6448
|
const map = fn(
|
|
@@ -6411,7 +6469,9 @@ function evalRuleMapIR(ir) {
|
|
|
6411
6469
|
field,
|
|
6412
6470
|
expect,
|
|
6413
6471
|
_tf,
|
|
6414
|
-
_nd
|
|
6472
|
+
_nd,
|
|
6473
|
+
_gch,
|
|
6474
|
+
_wc
|
|
6415
6475
|
);
|
|
6416
6476
|
return Object.entries(map);
|
|
6417
6477
|
}
|
|
@@ -6467,7 +6527,7 @@ ${body}
|
|
|
6467
6527
|
this.analyze(target, active);
|
|
6468
6528
|
return;
|
|
6469
6529
|
}
|
|
6470
|
-
if (def.tag === "guard" || def.tag === "
|
|
6530
|
+
if (def.tag === "guard" || def.tag === "recover" || def.tag === "unknown") {
|
|
6471
6531
|
throw new Unserializable(`unsupported tag ${def.tag}`);
|
|
6472
6532
|
}
|
|
6473
6533
|
this.counts.set(c, (this.counts.get(c) ?? 0) + 1);
|
|
@@ -6523,8 +6583,16 @@ ${body}
|
|
|
6523
6583
|
case "sequence":
|
|
6524
6584
|
return `sequence(${def.parsers.map(kid).join(", ")})`;
|
|
6525
6585
|
case "choice": {
|
|
6526
|
-
|
|
6527
|
-
return `choice(${def.parsers.map(kid).join(", ")})`;
|
|
6586
|
+
const gates = def.gates;
|
|
6587
|
+
if (gates.every((g) => g === null)) return `choice(${def.parsers.map(kid).join(", ")})`;
|
|
6588
|
+
const gateSrcs = def.gateSrcs;
|
|
6589
|
+
const items = def.parsers.map((p, i) => {
|
|
6590
|
+
if (gates[i] === null) return kid(p);
|
|
6591
|
+
const src = gateSrcs?.[i];
|
|
6592
|
+
if (src == null) throw new Unserializable("choice gate() has no captured source");
|
|
6593
|
+
return `[${JSON.stringify(src)}, ${kid(p)}]`;
|
|
6594
|
+
});
|
|
6595
|
+
return `_gch([${items.join(", ")}])`;
|
|
6528
6596
|
}
|
|
6529
6597
|
case "many":
|
|
6530
6598
|
return `many(${kid(def.parser)})`;
|
|
@@ -6568,8 +6636,11 @@ ${body}
|
|
|
6568
6636
|
const trivia2 = def.clearTrivia ? "null" : def.triviaParser ? kid(def.triviaParser) : "undefined";
|
|
6569
6637
|
return `parser({ trivia: ${trivia2}${def.trackLines ? ", trackLines: true" : ""} }, ${kid(def.parser)})`;
|
|
6570
6638
|
}
|
|
6639
|
+
case "withCtx": {
|
|
6640
|
+
if (def.extraSrc === void 0) throw new Unserializable("withCtx without extraSrc");
|
|
6641
|
+
return `_wc(${JSON.stringify(def.extraSrc)}, ${kid(def.parser)})`;
|
|
6642
|
+
}
|
|
6571
6643
|
case "guard":
|
|
6572
|
-
case "withCtx":
|
|
6573
6644
|
case "recover":
|
|
6574
6645
|
case "unknown":
|
|
6575
6646
|
throw new Unserializable(`unsupported tag ${def.tag}`);
|
|
@@ -6681,23 +6752,6 @@ function guard(predicate) {
|
|
|
6681
6752
|
};
|
|
6682
6753
|
}
|
|
6683
6754
|
|
|
6684
|
-
// src/combinators/withCtx.ts
|
|
6685
|
-
function withCtx(extra, combinator) {
|
|
6686
|
-
const meta = {
|
|
6687
|
-
firstSet: combinator._meta.firstSet,
|
|
6688
|
-
canMatchNewline: combinator._meta.canMatchNewline,
|
|
6689
|
-
isTrivia: false
|
|
6690
|
-
};
|
|
6691
|
-
return {
|
|
6692
|
-
_tag: "withCtx",
|
|
6693
|
-
_meta: meta,
|
|
6694
|
-
_def: { tag: "withCtx", extra, parser: combinator },
|
|
6695
|
-
parse(input, pos, ctx) {
|
|
6696
|
-
return combinator.parse(input, pos, { ...ctx, state: extra });
|
|
6697
|
-
}
|
|
6698
|
-
};
|
|
6699
|
-
}
|
|
6700
|
-
|
|
6701
6755
|
// src/cst/offset-model.ts
|
|
6702
6756
|
var { isFinite } = Number;
|
|
6703
6757
|
|
|
@@ -6705,6 +6759,48 @@ var { isFinite } = Number;
|
|
|
6705
6759
|
registerRegexAnalyzer(firstSetFromRegex);
|
|
6706
6760
|
|
|
6707
6761
|
// src/plugin/evaluator.ts
|
|
6762
|
+
function stripTsFromSource(node2, code) {
|
|
6763
|
+
const cuts = [];
|
|
6764
|
+
const walk2 = (n) => {
|
|
6765
|
+
if (!n || typeof n !== "object") return;
|
|
6766
|
+
const rec = n;
|
|
6767
|
+
if (typeof rec.type === "string" && rec.type.startsWith("TS") && typeof rec.start === "number" && typeof rec.end === "number") {
|
|
6768
|
+
const ex = rec.expression;
|
|
6769
|
+
if ((rec.type === "TSAsExpression" || rec.type === "TSSatisfiesExpression" || rec.type === "TSNonNullExpression" || rec.type === "TSInstantiationExpression") && ex && typeof ex.end === "number") {
|
|
6770
|
+
cuts.push([ex.end, rec.end]);
|
|
6771
|
+
walk2(rec.expression);
|
|
6772
|
+
return;
|
|
6773
|
+
}
|
|
6774
|
+
if (rec.type === "TSTypeAssertion" && ex && typeof ex.start === "number") {
|
|
6775
|
+
cuts.push([rec.start, ex.start]);
|
|
6776
|
+
walk2(rec.expression);
|
|
6777
|
+
return;
|
|
6778
|
+
}
|
|
6779
|
+
cuts.push([rec.start, rec.end]);
|
|
6780
|
+
return;
|
|
6781
|
+
}
|
|
6782
|
+
for (const key of Object.keys(rec)) {
|
|
6783
|
+
if (key === "type" || key === "start" || key === "end") continue;
|
|
6784
|
+
const v2 = rec[key];
|
|
6785
|
+
if (Array.isArray(v2)) {
|
|
6786
|
+
for (const item of v2) walk2(item);
|
|
6787
|
+
} else if (v2 && typeof v2 === "object") walk2(v2);
|
|
6788
|
+
}
|
|
6789
|
+
};
|
|
6790
|
+
walk2(node2);
|
|
6791
|
+
const start = node2.start;
|
|
6792
|
+
const end = node2.end;
|
|
6793
|
+
if (cuts.length === 0) return code.slice(start, end);
|
|
6794
|
+
cuts.sort((a, b) => a[0] - b[0]);
|
|
6795
|
+
let out = "";
|
|
6796
|
+
let cur = start;
|
|
6797
|
+
for (const [s, e] of cuts) {
|
|
6798
|
+
if (s < cur) continue;
|
|
6799
|
+
out += code.slice(cur, s);
|
|
6800
|
+
cur = e;
|
|
6801
|
+
}
|
|
6802
|
+
return out + code.slice(cur, end);
|
|
6803
|
+
}
|
|
6708
6804
|
function isWordFactory(v2) {
|
|
6709
6805
|
return !!v2 && typeof v2 === "object" && v2.tag === "wordFactory";
|
|
6710
6806
|
}
|
|
@@ -6980,7 +7076,7 @@ function exprToCombi(node2, scope, code, mfs) {
|
|
|
6980
7076
|
if (isGatedArmExpr(argNode)) {
|
|
6981
7077
|
const parts = gatedArmParts(argNode);
|
|
6982
7078
|
if (!parts) return null;
|
|
6983
|
-
const gateSrc =
|
|
7079
|
+
const gateSrc = stripTsFromSource(parts.gate, code);
|
|
6984
7080
|
mfs.push(gateSrc);
|
|
6985
7081
|
const combi = anyValue(parts.combinator, scope, code, mfs);
|
|
6986
7082
|
if (!isCombinator(combi)) return null;
|