parseman 0.21.0 → 0.23.0
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/combinators/grammar.d.ts.map +1 -1
- package/dist/combinators/parser.d.ts +12 -0
- package/dist/combinators/parser.d.ts.map +1 -1
- package/dist/compiler/codegen.d.ts +6 -2
- package/dist/compiler/codegen.d.ts.map +1 -1
- package/dist/compiler/linker.d.ts +7 -2
- package/dist/compiler/linker.d.ts.map +1 -1
- package/dist/functional/run.d.ts.map +1 -1
- package/dist/index.cjs +289 -38
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +289 -38
- package/dist/index.js.map +3 -3
- package/dist/plugin/index.cjs +161 -34
- package/dist/plugin/index.cjs.map +3 -3
- package/dist/plugin/index.d.ts.map +1 -1
- package/dist/plugin/index.js +161 -34
- package/dist/plugin/index.js.map +3 -3
- package/dist/types.d.ts +10 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grammar.d.ts","sourceRoot":"","sources":["../../src/combinators/grammar.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAa,MAAM,aAAa,CAAA;AAKnF,MAAM,MAAM,YAAY,GAAG;IACzB,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB;;;;;;OAMG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB,CAAA;AAED,MAAM,MAAM,aAAa,GAAG,YAAY,GAAG;IACzC;;;;;;OAMG;IACH,MAAM,CAAC,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG,IAAI,CAAA;IACnC,gFAAgF;IAChF,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB;;;;;;OAMG;IACH,kBAAkB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;CACvC,CAAA;AAED,MAA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;CACvC,CAAA;AAED,MAAM,WAAW,cAAc,CAAC,CAAC,CAAE,SAAQ,UAAU,CAAC,CAAC,CAAC;IACtD,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,WAAW,CAAC,CAAC,CAAC,CAAA;IACpC,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,YAAY,GAAG,WAAW,CAAC,CAAC,CAAC,CAAA;CACrE;AAED,wBAAgB,MAAM,CAAC,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,CAgDrF;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,CAElE;AAOD,wBAAgB,KAAK,CAAC,CAAC,EACrB,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,EACzB,KAAK,EAAE,MAAM,EACb,IAAI,GAAE,YAAiB,GACtB,WAAW,CAAC,CAAC,CAAC,CAiChB"}
|
|
@@ -21,5 +21,17 @@ import type { Combinator } from '../types.ts';
|
|
|
21
21
|
* rules<{ value: Combinator<JSONValue>; array: Combinator<JSONValue[]> }>(g => ({ ... }))
|
|
22
22
|
* Without it, `g.*` accesses are typed as `any` but the return is still inferred.
|
|
23
23
|
*/
|
|
24
|
+
/**
|
|
25
|
+
* Grammar-level options for `rules()`. Parity with `parser({...})`, but declared
|
|
26
|
+
* ONCE for the whole grammar instead of wrapped around a scope. `trivia` becomes
|
|
27
|
+
* the ambient trivia for every rule (installed at the parse entry, inherited
|
|
28
|
+
* everywhere, incremental parse included); `parser({trivia})` / `noTrivia` still
|
|
29
|
+
* override it locally for a sub-region.
|
|
30
|
+
*/
|
|
31
|
+
export type RulesOptions = {
|
|
32
|
+
/** Ambient trivia for the whole grammar. See `parser({ trivia })` for the shape. */
|
|
33
|
+
trivia?: Combinator<unknown>;
|
|
34
|
+
};
|
|
24
35
|
export declare function rules<T extends Record<string, Combinator<unknown>>>(factory: (self: any) => T): T;
|
|
36
|
+
export declare function rules<T extends Record<string, Combinator<unknown>>>(options: RulesOptions, factory: (self: any) => T): T;
|
|
25
37
|
//# sourceMappingURL=parser.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../src/combinators/parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAS7C;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,KAAK,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../src/combinators/parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAS7C;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH;;;;;;GAMG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB,oFAAoF;IACpF,MAAM,CAAC,EAAE,UAAU,CAAC,OAAO,CAAC,CAAA;CAC7B,CAAA;AAKD,wBAAgB,KAAK,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,CAAC,GAAG,CAAC,CAAA;AAClG,wBAAgB,KAAK,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC,EAAE,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,CAAC,GAAG,CAAC,CAAA"}
|
|
@@ -65,7 +65,9 @@ export declare function ruleDependencies(ruleMap: ReadonlyArray<readonly [string
|
|
|
65
65
|
* @see https://www.greadme.com/blog/security/what-is-content-security-policy-complete-guide
|
|
66
66
|
*/
|
|
67
67
|
export declare function compile<T>(combinator: Combinator<T>, mapFnSources?: string[]): CompiledParser<T>;
|
|
68
|
-
export declare function compileRuleMap(ruleMap: ReadonlyArray<readonly [string, Combinator<unknown>]
|
|
68
|
+
export declare function compileRuleMap(ruleMap: ReadonlyArray<readonly [string, Combinator<unknown>]>, opts?: {
|
|
69
|
+
trivia?: Combinator<unknown>;
|
|
70
|
+
}): {
|
|
69
71
|
keys: string[];
|
|
70
72
|
replacement: string;
|
|
71
73
|
} | null;
|
|
@@ -110,5 +112,7 @@ export type LinkablePieces = {
|
|
|
110
112
|
mfFns: ReadonlyArray<(...args: unknown[]) => unknown>;
|
|
111
113
|
buildFns: ReadonlyArray<(...args: unknown[]) => unknown>;
|
|
112
114
|
};
|
|
113
|
-
export declare function compileLinkable(ruleMapArg: ReadonlyArray<readonly [string, Combinator<unknown>]>, ns: string
|
|
115
|
+
export declare function compileLinkable(ruleMapArg: ReadonlyArray<readonly [string, Combinator<unknown>]>, ns: string, opts?: {
|
|
116
|
+
trivia?: Combinator<unknown>;
|
|
117
|
+
}): LinkablePieces | null;
|
|
114
118
|
//# sourceMappingURL=codegen.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codegen.d.ts","sourceRoot":"","sources":["../../src/compiler/codegen.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAE,UAAU,EAAa,QAAQ,EAAE,WAAW,EAAE,YAAY,EAAE,UAAU,EAA4B,MAAM,aAAa,CAAA;AA0BnI;;;;;;;;GAQG;AACH,eAAO,MAAM,eAAe,uTAC0R,CAAA;AActT,8EAA8E;AAC9E,wBAAgB,oBAAoB,IAAI,IAAI,CAE3C;AAED,+EAA+E;AAC/E,wBAAgB,kBAAkB,IAAI,MAAM,EAAE,CAI7C;AA6bD,wBAAgB,YAAY,CAAC,OAAO,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AAU,CAAC,OAAO,CAAC,CAAA;CAAE,GACtC,cAAc,GAAG,IAAI,CAqLvB"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { LinkablePieces } from './codegen.ts';
|
|
2
2
|
import type { BuildHost, Combinator, CstCollapsePredicate } from '../types.ts';
|
|
3
|
-
export declare function linkable(rulesMap: Record<string, Combinator<unknown>>, ns?: string): LinkablePieces;
|
|
3
|
+
export declare function linkable(rulesMap: Record<string, Combinator<unknown>>, ns?: string, trivia?: Combinator<unknown>): LinkablePieces;
|
|
4
4
|
export type CstBuildHostOptions = {
|
|
5
5
|
/**
|
|
6
6
|
* Collapse transparent one-child CST wrapper nodes at build time.
|
|
@@ -43,6 +43,11 @@ export type FusedRule = (input: string, pos: number, ctx: Record<string, unknown
|
|
|
43
43
|
* compiled artifact; returns an artifact for `compose()`.
|
|
44
44
|
*/
|
|
45
45
|
export declare function pick(grammar: LinkablePieces | Record<string, Combinator<unknown>>, names: string[]): LinkablePieces;
|
|
46
|
+
/** Restrict a set of linkable pieces to `names` + their transitive dep closure,
|
|
47
|
+
* keeping each surviving rule in the piece that WINS it (later piece wins, matching
|
|
48
|
+
* compose override order). Shared by `pick()` (runtime) and the macro's build-time
|
|
49
|
+
* `pick(...)` handling, so à-la-carte selection is identical on both paths. */
|
|
50
|
+
export declare function pickPieces(pieces: LinkablePieces[], names: string[]): LinkablePieces[];
|
|
46
51
|
/** Fuse at RUNTIME (via `new Function`) — used by `compose()` when not compiled
|
|
47
52
|
* by the macro (like `compile()`). The macro path uses `emitFusedSource` instead. */
|
|
48
53
|
export declare function fuseRules(pieces: LinkablePieces[]): Record<string, FusedRule>;
|
|
@@ -62,6 +67,6 @@ export type IRPiece = {
|
|
|
62
67
|
};
|
|
63
68
|
/** Materialize a carried item to full `LinkablePieces`: an IR piece is evaluated
|
|
64
69
|
* back to a rule map and re-lowered; a full piece passes through. */
|
|
65
|
-
export declare function materializePiece(p: LinkablePieces | IRPiece): LinkablePieces;
|
|
70
|
+
export declare function materializePiece(p: LinkablePieces | IRPiece, trivia?: Combinator<unknown>): LinkablePieces;
|
|
66
71
|
export declare function compose(items: Array<LinkablePieces | Record<string, unknown>>): Record<string, FusedRule>;
|
|
67
72
|
//# sourceMappingURL=linker.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"linker.d.ts","sourceRoot":"","sources":["../../src/compiler/linker.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAClD,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,oBAAoB,EAAY,MAAM,aAAa,CAAA;AAaxF,wBAAgB,QAAQ,CACtB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC,EAC7C,EAAE,CAAC,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"linker.d.ts","sourceRoot":"","sources":["../../src/compiler/linker.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAClD,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,oBAAoB,EAAY,MAAM,aAAa,CAAA;AAaxF,wBAAgB,QAAQ,CACtB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC,EAC7C,EAAE,CAAC,EAAE,MAAM,EACX,MAAM,CAAC,EAAE,UAAU,CAAC,OAAO,CAAC,GAC3B,cAAc,CAIhB;AAED,MAAM,MAAM,mBAAmB,GAAG;IAChC;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,OAAO,GAAG,SAAS,MAAM,EAAE,GAAG,oBAAoB,CAAA;CAC9D,CAAA;AAyBD;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAAC,OAAO,CAAC,EAAE,mBAAmB,GAAG,SAAS,CAAA;AACtE,wBAAgB,YAAY,CAC1B,IAAI,EAAE,MAAM,EACZ,QAAQ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H,SAAS,EAAE,MAAM,EAAE,CAAA;IACnB;;;;4BAIwB;IACxB,cAAc,EAAE,MAAM,GAAG,IAAI,CAAA;CAC9B,CAAA;AAKD,wBAAgB,GAAG,CAAC,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE,UAAe,GAAG,SAAS,CA8CvF"}
|
package/dist/index.cjs
CHANGED
|
@@ -63,7 +63,6 @@ __export(index_exports, {
|
|
|
63
63
|
parse: () => parse,
|
|
64
64
|
parseDoc: () => parseDoc,
|
|
65
65
|
parser: () => parser,
|
|
66
|
-
pick: () => pick,
|
|
67
66
|
recover: () => recover,
|
|
68
67
|
ref: () => ref,
|
|
69
68
|
regex: () => regex,
|
|
@@ -1779,7 +1778,9 @@ function tagRule(r, key) {
|
|
|
1779
1778
|
r._ruleName = key;
|
|
1780
1779
|
if (r._def.tag === "node" && r._def.type === void 0) r._def.type = key;
|
|
1781
1780
|
}
|
|
1782
|
-
function rules(
|
|
1781
|
+
function rules(a, b) {
|
|
1782
|
+
const factory = typeof a === "function" ? a : b;
|
|
1783
|
+
const options = typeof a === "function" ? b : a;
|
|
1783
1784
|
const cache = {};
|
|
1784
1785
|
const proxy = new Proxy(cache, {
|
|
1785
1786
|
get(target, key) {
|
|
@@ -1807,6 +1808,12 @@ function rules(factory) {
|
|
|
1807
1808
|
cache[key] = parser2;
|
|
1808
1809
|
}
|
|
1809
1810
|
}
|
|
1811
|
+
if (options?.trivia !== void 0) {
|
|
1812
|
+
for (const key of Object.keys(definitions)) {
|
|
1813
|
+
const rule = cache[key];
|
|
1814
|
+
if (rule && !rule._meta.isTrivia) rule._meta.grammarTrivia = options.trivia;
|
|
1815
|
+
}
|
|
1816
|
+
}
|
|
1810
1817
|
for (const key of Object.keys(definitions)) {
|
|
1811
1818
|
markUnusedValues(cache[key]);
|
|
1812
1819
|
}
|
|
@@ -2149,8 +2156,10 @@ function parse(combinator, input, opts = {}) {
|
|
|
2149
2156
|
const trackLines = opts.trackLines ?? false;
|
|
2150
2157
|
const _errors = opts.recover ? [] : void 0;
|
|
2151
2158
|
const _probe = opts.recover ? { offset: input.length, best: null } : void 0;
|
|
2159
|
+
const grammarTrivia = combinator._meta.grammarTrivia;
|
|
2152
2160
|
const ctx = {
|
|
2153
2161
|
trackLines,
|
|
2162
|
+
...grammarTrivia !== void 0 ? { trivia: grammarTrivia, ...grammarTrivia._meta.triviaKindLabels ? { triviaKindLabels: grammarTrivia._meta.triviaKindLabels } : {} } : {},
|
|
2154
2163
|
..._errors !== void 0 ? { _errors } : {},
|
|
2155
2164
|
..._probe !== void 0 ? { _probe } : {}
|
|
2156
2165
|
};
|
|
@@ -5416,6 +5425,7 @@ function ruleDependencies(ruleMap) {
|
|
|
5416
5425
|
}
|
|
5417
5426
|
function compile(combinator, mapFnSources) {
|
|
5418
5427
|
markUnusedValues(combinator);
|
|
5428
|
+
const grammarTrivia = combinator._meta.grammarTrivia;
|
|
5419
5429
|
const ctx = {
|
|
5420
5430
|
vars: 0,
|
|
5421
5431
|
indent: 1,
|
|
@@ -5434,7 +5444,8 @@ function compile(combinator, mapFnSources) {
|
|
|
5434
5444
|
triviaFnNames: /* @__PURE__ */ new Map(),
|
|
5435
5445
|
namedFnDecls: [],
|
|
5436
5446
|
capturing: hasNodeDef(combinator),
|
|
5437
|
-
lazyUsage: analyzeLazyUsage(combinator)
|
|
5447
|
+
lazyUsage: analyzeLazyUsage(combinator),
|
|
5448
|
+
...grammarTrivia ? { activeTrivia: grammarTrivia, triviaKindLabels: grammarTrivia._meta.triviaKindLabels } : {}
|
|
5438
5449
|
};
|
|
5439
5450
|
const r = emit(combinator, ctx, "_pos");
|
|
5440
5451
|
const namedPrelude = ctx.namedFnDecls.length > 0 ? [...namedFnPrelude(), ""] : [];
|
|
@@ -5510,9 +5521,10 @@ function publicRuleWrapperSource(rule, fnSource) {
|
|
|
5510
5521
|
if (labels === void 0) return fnSource;
|
|
5511
5522
|
return `Object.assign(${fnSource}, { _meta: { triviaKindLabels: ${JSON.stringify(labels)} } })`;
|
|
5512
5523
|
}
|
|
5513
|
-
function compileLinkable(ruleMapArg, ns) {
|
|
5524
|
+
function compileLinkable(ruleMapArg, ns, opts) {
|
|
5514
5525
|
if (!ns) throw new Error("compileLinkable: ns must be a non-empty namespace");
|
|
5515
5526
|
for (const [, rule] of ruleMapArg) markUnusedValues(rule);
|
|
5527
|
+
const grammarTrivia = opts?.trivia ?? ruleMapArg.map(([, r]) => r._meta.grammarTrivia).find(Boolean);
|
|
5516
5528
|
const ruleMap = ruleMapArg.filter(([, val]) => {
|
|
5517
5529
|
const d = val._def;
|
|
5518
5530
|
if (d.tag !== "lazy") return true;
|
|
@@ -5543,7 +5555,8 @@ function compileLinkable(ruleMapArg, ns) {
|
|
|
5543
5555
|
capturing: ruleMap.some(([, rule]) => hasNodeDef(rule)),
|
|
5544
5556
|
lazyUsage: analyzeLazyUsageMulti(ruleMap.map(([, rule]) => rule)),
|
|
5545
5557
|
ns,
|
|
5546
|
-
deferFirstSetRefs: true
|
|
5558
|
+
deferFirstSetRefs: true,
|
|
5559
|
+
...grammarTrivia ? { activeTrivia: grammarTrivia, triviaKindLabels: grammarTrivia._meta.triviaKindLabels } : {}
|
|
5547
5560
|
};
|
|
5548
5561
|
const seen = /* @__PURE__ */ new Map();
|
|
5549
5562
|
const ruleNames = /* @__PURE__ */ new Map();
|
|
@@ -5606,7 +5619,10 @@ function compileLinkable(ruleMapArg, ns) {
|
|
|
5606
5619
|
ctx.indent = 1;
|
|
5607
5620
|
ctx.failLabel = "_pfail";
|
|
5608
5621
|
ctx.recordFail = true;
|
|
5622
|
+
const savedTrivia = ctx.activeTrivia;
|
|
5623
|
+
if (rule._meta.isTrivia) ctx.activeTrivia = void 0;
|
|
5609
5624
|
const body = emit(resolved, ctx, "_pos");
|
|
5625
|
+
ctx.activeTrivia = savedTrivia;
|
|
5610
5626
|
ctx.indent = savedIndent;
|
|
5611
5627
|
ctx.failLabel = savedFail;
|
|
5612
5628
|
ctx.recordFail = savedRec;
|
|
@@ -5805,6 +5821,64 @@ function escapeClassChar(ch) {
|
|
|
5805
5821
|
}
|
|
5806
5822
|
|
|
5807
5823
|
// src/compiler/ir-serialize.ts
|
|
5824
|
+
function childrenOf2(def) {
|
|
5825
|
+
switch (def.tag) {
|
|
5826
|
+
case "sequence":
|
|
5827
|
+
case "choice":
|
|
5828
|
+
return def.parsers;
|
|
5829
|
+
case "many":
|
|
5830
|
+
case "oneOrMore":
|
|
5831
|
+
case "optional":
|
|
5832
|
+
case "transform":
|
|
5833
|
+
case "trivia":
|
|
5834
|
+
case "token":
|
|
5835
|
+
case "label":
|
|
5836
|
+
case "field":
|
|
5837
|
+
case "not":
|
|
5838
|
+
case "node":
|
|
5839
|
+
case "expect":
|
|
5840
|
+
return [def.parser];
|
|
5841
|
+
case "grammar":
|
|
5842
|
+
return def.triviaParser ? [def.parser, def.triviaParser] : [def.parser];
|
|
5843
|
+
case "sepBy":
|
|
5844
|
+
return [def.parser, def.separator];
|
|
5845
|
+
case "skip":
|
|
5846
|
+
return [def.main, def.skipped];
|
|
5847
|
+
case "scanTo":
|
|
5848
|
+
return [def.sentinel, ...def.skip];
|
|
5849
|
+
case "lazy":
|
|
5850
|
+
case "literal":
|
|
5851
|
+
case "regex":
|
|
5852
|
+
case "keywords":
|
|
5853
|
+
case "guard":
|
|
5854
|
+
case "withCtx":
|
|
5855
|
+
case "recover":
|
|
5856
|
+
case "unknown":
|
|
5857
|
+
return [];
|
|
5858
|
+
}
|
|
5859
|
+
}
|
|
5860
|
+
var ruleNameOf = (c) => c._ruleName;
|
|
5861
|
+
function lazyTarget(c) {
|
|
5862
|
+
if (c._def.tag !== "lazy") return null;
|
|
5863
|
+
try {
|
|
5864
|
+
return c._def.thunk();
|
|
5865
|
+
} catch {
|
|
5866
|
+
return null;
|
|
5867
|
+
}
|
|
5868
|
+
}
|
|
5869
|
+
var Unserializable = class extends Error {
|
|
5870
|
+
};
|
|
5871
|
+
function serializeRuleMap(ruleMap) {
|
|
5872
|
+
try {
|
|
5873
|
+
return new Serializer(ruleMap).run();
|
|
5874
|
+
} catch (e) {
|
|
5875
|
+
if (e instanceof Unserializable) {
|
|
5876
|
+
if (process.env.PARSEMAN_IR_DEBUG) console.error(`[ir] fallback: ${e.message}`);
|
|
5877
|
+
return null;
|
|
5878
|
+
}
|
|
5879
|
+
throw e;
|
|
5880
|
+
}
|
|
5881
|
+
}
|
|
5808
5882
|
function evalRuleMapIR(ir) {
|
|
5809
5883
|
const _tf = (child, src) => {
|
|
5810
5884
|
let fn2;
|
|
@@ -5879,11 +5953,172 @@ function evalRuleMapIR(ir) {
|
|
|
5879
5953
|
);
|
|
5880
5954
|
return Object.entries(map);
|
|
5881
5955
|
}
|
|
5956
|
+
var Serializer = class {
|
|
5957
|
+
counts = /* @__PURE__ */ new Map();
|
|
5958
|
+
selfRef = /* @__PURE__ */ new Set();
|
|
5959
|
+
// combinators reached by an unnamed lazy pointing back into their own subtree
|
|
5960
|
+
constName = /* @__PURE__ */ new Map();
|
|
5961
|
+
emitted = /* @__PURE__ */ new Set();
|
|
5962
|
+
decls = [];
|
|
5963
|
+
ruleValues = /* @__PURE__ */ new Set();
|
|
5964
|
+
ruleMap;
|
|
5965
|
+
constructor(ruleMap) {
|
|
5966
|
+
this.ruleMap = ruleMap;
|
|
5967
|
+
for (const [, c] of ruleMap) this.ruleValues.add(this.body(c));
|
|
5968
|
+
}
|
|
5969
|
+
/** A `rules()` map wraps each rule value in a named `lazy` proxy; the real body is
|
|
5970
|
+
* its target (compileLinkable unwraps the same way). A ref INSIDE a body stays a
|
|
5971
|
+
* `g[name]` ref — only the top-level entry is unwrapped. */
|
|
5972
|
+
body(c) {
|
|
5973
|
+
return c._def.tag === "lazy" && ruleNameOf(c) !== void 0 ? lazyTarget(c) ?? c : c;
|
|
5974
|
+
}
|
|
5975
|
+
run() {
|
|
5976
|
+
for (const [, c] of this.ruleMap) this.analyze(this.body(c), /* @__PURE__ */ new Set());
|
|
5977
|
+
let n = 0;
|
|
5978
|
+
for (const [c, count] of this.counts) {
|
|
5979
|
+
if (this.ruleValues.has(c)) continue;
|
|
5980
|
+
if (count >= 2 || this.selfRef.has(c)) this.constName.set(c, `_s${n++}`);
|
|
5981
|
+
}
|
|
5982
|
+
const body = this.ruleMap.map(([name, c]) => ` ${JSON.stringify(name)}: ${this.wrap(this.body(c))}`).join(",\n");
|
|
5983
|
+
if (this.decls.length === 0) return `rules((g) => ({
|
|
5984
|
+
${body}
|
|
5985
|
+
}))`;
|
|
5986
|
+
return `rules((g) => {
|
|
5987
|
+
${this.decls.map((d) => " " + d).join("\n")}
|
|
5988
|
+
return ({
|
|
5989
|
+
${body}
|
|
5990
|
+
})
|
|
5991
|
+
})`;
|
|
5992
|
+
}
|
|
5993
|
+
/** Count identity references and flag self-referential subtrees. `active` is the
|
|
5994
|
+
* set of combinators on the current DFS path (to detect a lazy pointing back). */
|
|
5995
|
+
analyze(c, active) {
|
|
5996
|
+
const def = c._def;
|
|
5997
|
+
if (def.tag === "lazy") {
|
|
5998
|
+
if (ruleNameOf(c) !== void 0) return;
|
|
5999
|
+
const target = lazyTarget(c);
|
|
6000
|
+
if (!target) throw new Unserializable("unresolved unnamed ref");
|
|
6001
|
+
if (active.has(target)) {
|
|
6002
|
+
this.selfRef.add(target);
|
|
6003
|
+
return;
|
|
6004
|
+
}
|
|
6005
|
+
this.analyze(target, active);
|
|
6006
|
+
return;
|
|
6007
|
+
}
|
|
6008
|
+
if (def.tag === "guard" || def.tag === "withCtx" || def.tag === "recover" || def.tag === "unknown") {
|
|
6009
|
+
throw new Unserializable(`unsupported tag ${def.tag}`);
|
|
6010
|
+
}
|
|
6011
|
+
this.counts.set(c, (this.counts.get(c) ?? 0) + 1);
|
|
6012
|
+
if ((this.counts.get(c) ?? 0) > 1) return;
|
|
6013
|
+
active.add(c);
|
|
6014
|
+
for (const child of childrenOf2(def)) this.analyze(child, active);
|
|
6015
|
+
active.delete(c);
|
|
6016
|
+
}
|
|
6017
|
+
/** Reference `c`: a const var if it has one (emitting its decl first), else inline.
|
|
6018
|
+
* `selfOf` is the const currently being wrapped in a ref()/define closure — an
|
|
6019
|
+
* unnamed lazy back into it emits the local ref var instead of recursing. */
|
|
6020
|
+
ref(c, selfOf) {
|
|
6021
|
+
const cn = this.constName.get(c);
|
|
6022
|
+
if (cn) {
|
|
6023
|
+
this.emitDecl(c);
|
|
6024
|
+
return cn;
|
|
6025
|
+
}
|
|
6026
|
+
return this.expr(c, selfOf);
|
|
6027
|
+
}
|
|
6028
|
+
emitDecl(c) {
|
|
6029
|
+
const name = this.constName.get(c);
|
|
6030
|
+
if (this.emitted.has(name)) return;
|
|
6031
|
+
this.emitted.add(name);
|
|
6032
|
+
this.decls.push(`const ${name} = ${this.wrap(c)}`);
|
|
6033
|
+
}
|
|
6034
|
+
selfN = 0;
|
|
6035
|
+
/** Emit `c`'s own constructor call, wrapping it in a `ref()`+`.define()` closure
|
|
6036
|
+
* if it is self-referential (an unnamed lazy in its subtree points back to it) —
|
|
6037
|
+
* the shape `balanced()` and other recursive library combinators build. */
|
|
6038
|
+
wrap(c) {
|
|
6039
|
+
if (!this.selfRef.has(c)) return this.expr(c, null);
|
|
6040
|
+
const rv = `_rr${this.selfN++}`;
|
|
6041
|
+
const bodyExpr = this.expr(c, { comb: c, var: rv });
|
|
6042
|
+
return `(() => { const ${rv} = ref(); const _b = ${bodyExpr}; ${rv}.define(_b); return _b })()`;
|
|
6043
|
+
}
|
|
6044
|
+
/** Emit the constructor-call source for `c` (never a const shortcut for `c` itself). */
|
|
6045
|
+
expr(c, selfOf) {
|
|
6046
|
+
const def = c._def;
|
|
6047
|
+
const kid = (x) => this.ref(x, selfOf);
|
|
6048
|
+
switch (def.tag) {
|
|
6049
|
+
case "lazy": {
|
|
6050
|
+
if (ruleNameOf(c) !== void 0) return `g[${JSON.stringify(ruleNameOf(c))}]`;
|
|
6051
|
+
const target = lazyTarget(c);
|
|
6052
|
+
if (selfOf && target === selfOf.comb) return selfOf.var;
|
|
6053
|
+
return this.ref(target, selfOf);
|
|
6054
|
+
}
|
|
6055
|
+
case "literal":
|
|
6056
|
+
return `literal(${JSON.stringify(def.value)}${def.caseInsensitive ? ", { caseInsensitive: true }" : ""})`;
|
|
6057
|
+
case "regex":
|
|
6058
|
+
return `regex(${JSON.stringify(def.source)}, ${JSON.stringify(def.flags)})`;
|
|
6059
|
+
case "keywords":
|
|
6060
|
+
return `keywords(${JSON.stringify(def.words)}, { caseInsensitive: ${def.caseInsensitive}${def.boundary !== void 0 ? `, boundary: ${JSON.stringify(def.boundary)}` : ""} })`;
|
|
6061
|
+
case "sequence":
|
|
6062
|
+
return `sequence(${def.parsers.map(kid).join(", ")})`;
|
|
6063
|
+
case "choice": {
|
|
6064
|
+
if (def.gates.some((g) => g !== null)) throw new Unserializable("choice with a gate() has no source");
|
|
6065
|
+
return `choice(${def.parsers.map(kid).join(", ")})`;
|
|
6066
|
+
}
|
|
6067
|
+
case "many":
|
|
6068
|
+
return `many(${kid(def.parser)})`;
|
|
6069
|
+
case "oneOrMore":
|
|
6070
|
+
return `oneOrMore(${kid(def.parser)})`;
|
|
6071
|
+
case "optional":
|
|
6072
|
+
return `optional(${kid(def.parser)})`;
|
|
6073
|
+
case "sepBy":
|
|
6074
|
+
return `sepBy(${kid(def.parser)}, ${kid(def.separator)})`;
|
|
6075
|
+
case "not":
|
|
6076
|
+
return `not(${kid(def.parser)})`;
|
|
6077
|
+
case "trivia":
|
|
6078
|
+
return `trivia(${kid(def.parser)})`;
|
|
6079
|
+
case "token":
|
|
6080
|
+
return `token(${kid(def.parser)})`;
|
|
6081
|
+
case "label":
|
|
6082
|
+
return `label(${JSON.stringify(def.label)}, ${kid(def.parser)})`;
|
|
6083
|
+
case "field":
|
|
6084
|
+
return `field(${JSON.stringify(def.name)}, ${kid(def.parser)})`;
|
|
6085
|
+
case "expect":
|
|
6086
|
+
return `expect(${kid(def.parser)}${def.label !== void 0 ? `, ${JSON.stringify(def.label)}` : ""})`;
|
|
6087
|
+
case "skip":
|
|
6088
|
+
return `skip(${kid(def.main)}, ${kid(def.skipped)})`;
|
|
6089
|
+
case "scanTo":
|
|
6090
|
+
return `scanTo(${kid(def.sentinel)}, { skip: [${def.skip.map(kid).join(", ")}], orEOF: ${def.orEOF} })`;
|
|
6091
|
+
case "transform": {
|
|
6092
|
+
if (def.fnSrc === void 0) throw new Unserializable("transform without fnSrc");
|
|
6093
|
+
return `_tf(${kid(def.parser)}, ${JSON.stringify(def.fnSrc)})`;
|
|
6094
|
+
}
|
|
6095
|
+
case "node": {
|
|
6096
|
+
if (def.build !== void 0 && def.buildSrc === void 0) throw new Unserializable("node build without buildSrc");
|
|
6097
|
+
const opts = def.unwrap || def.collapse ? `, { ${def.unwrap ? "unwrap: true" : "collapse: true"} }` : "";
|
|
6098
|
+
if (def.type === void 0) {
|
|
6099
|
+
if (def.buildSrc !== void 0) throw new Unserializable("inferred node build without inferred type");
|
|
6100
|
+
return opts ? `node(${kid(def.parser)}, undefined${opts})` : `node(${kid(def.parser)})`;
|
|
6101
|
+
}
|
|
6102
|
+
if (def.buildSrc !== void 0) return `_nd(${JSON.stringify(def.type)}, ${kid(def.parser)}, ${JSON.stringify(def.buildSrc)}${opts})`;
|
|
6103
|
+
return opts ? `node(${JSON.stringify(def.type)}, ${kid(def.parser)}, undefined${opts})` : `node(${JSON.stringify(def.type)}, ${kid(def.parser)})`;
|
|
6104
|
+
}
|
|
6105
|
+
case "grammar": {
|
|
6106
|
+
const trivia2 = def.clearTrivia ? "null" : def.triviaParser ? kid(def.triviaParser) : "undefined";
|
|
6107
|
+
return `parser({ trivia: ${trivia2}${def.trackLines ? ", trackLines: true" : ""} }, ${kid(def.parser)})`;
|
|
6108
|
+
}
|
|
6109
|
+
case "guard":
|
|
6110
|
+
case "withCtx":
|
|
6111
|
+
case "recover":
|
|
6112
|
+
case "unknown":
|
|
6113
|
+
throw new Unserializable(`unsupported tag ${def.tag}`);
|
|
6114
|
+
}
|
|
6115
|
+
}
|
|
6116
|
+
};
|
|
5882
6117
|
|
|
5883
6118
|
// src/compiler/linker.ts
|
|
5884
6119
|
var _nsCounter = 0;
|
|
5885
|
-
function linkable(rulesMap, ns) {
|
|
5886
|
-
const pieces = compileLinkable(
|
|
6120
|
+
function linkable(rulesMap, ns, trivia2) {
|
|
6121
|
+
const pieces = compileLinkable(Object.entries(rulesMap), ns ?? `_lk${_nsCounter++}_`, trivia2 ? { trivia: trivia2 } : void 0);
|
|
5887
6122
|
if (!pieces) throw new Error("linkable(): this grammar cannot be compiled to a linkable artifact (contains a runtime-only parser fallback)");
|
|
5888
6123
|
return pieces;
|
|
5889
6124
|
}
|
|
@@ -5907,28 +6142,6 @@ function cstBuildHost(typeOrOptions, children, _fields, span, rawChildren, trivi
|
|
|
5907
6142
|
if (collapse) host._parsemanCstCollapse = collapse;
|
|
5908
6143
|
return host;
|
|
5909
6144
|
}
|
|
5910
|
-
function pick(grammar, names) {
|
|
5911
|
-
const p = grammar.ruleFns instanceof Map ? grammar : linkable(grammar);
|
|
5912
|
-
const keep = /* @__PURE__ */ new Set();
|
|
5913
|
-
const has = new Set(p.keys);
|
|
5914
|
-
for (const n of names) {
|
|
5915
|
-
if (!has.has(n)) throw new Error(`pick: rule "${n}" is not in this grammar (available: ${p.keys.join(", ")})`);
|
|
5916
|
-
}
|
|
5917
|
-
const visit = (n) => {
|
|
5918
|
-
if (keep.has(n) || !has.has(n)) return;
|
|
5919
|
-
keep.add(n);
|
|
5920
|
-
for (const d of p.deps.get(n) ?? []) visit(d);
|
|
5921
|
-
};
|
|
5922
|
-
for (const n of names) visit(n);
|
|
5923
|
-
const filt = (m) => new Map([...m].filter(([k]) => keep.has(k)));
|
|
5924
|
-
return {
|
|
5925
|
-
...p,
|
|
5926
|
-
keys: [...keep],
|
|
5927
|
-
ruleFns: filt(p.ruleFns),
|
|
5928
|
-
wrappers: filt(p.wrappers),
|
|
5929
|
-
deps: filt(p.deps)
|
|
5930
|
-
};
|
|
5931
|
-
}
|
|
5932
6145
|
function fusedBody(pieces) {
|
|
5933
6146
|
const winner = /* @__PURE__ */ new Map();
|
|
5934
6147
|
for (const p of pieces) for (const k of p.keys) winner.set(k, p);
|
|
@@ -5948,7 +6161,7 @@ function fusedBody(pieces) {
|
|
|
5948
6161
|
...needsEmptyTl ? ["const _EMPTY_TL = Object.freeze([])"] : [],
|
|
5949
6162
|
...needsHostReads ? [HOST_READS_DECL] : [],
|
|
5950
6163
|
// Each contributing artifact's namespaced private prelude (regexes, _pf, …).
|
|
5951
|
-
...
|
|
6164
|
+
...new Set(contributingPieces.flatMap((p) => p.prelude)),
|
|
5952
6165
|
// The winning `_r_<Name>` function for each rule (one per name → no redeclare).
|
|
5953
6166
|
...[...winner].map(([k, p]) => p.ruleFns.get(k))
|
|
5954
6167
|
];
|
|
@@ -5982,12 +6195,13 @@ function fuseRules(pieces) {
|
|
|
5982
6195
|
return new Function("_env", body)(env);
|
|
5983
6196
|
}
|
|
5984
6197
|
var COMPOSED_PIECES = /* @__PURE__ */ Symbol.for("parseman.composedPieces");
|
|
6198
|
+
var COMPOSED_TRIVIA = /* @__PURE__ */ Symbol.for("parseman.composedTrivia");
|
|
5985
6199
|
function isIRPiece(p) {
|
|
5986
6200
|
return !!p && typeof p === "object" && typeof p.ir === "string" && typeof p.ns === "string" && !("ruleFns" in p);
|
|
5987
6201
|
}
|
|
5988
|
-
function materializePiece(p) {
|
|
6202
|
+
function materializePiece(p, trivia2) {
|
|
5989
6203
|
if (!isIRPiece(p)) return p;
|
|
5990
|
-
const pieces = compileLinkable(evalRuleMapIR(p.ir), p.ns);
|
|
6204
|
+
const pieces = compileLinkable(evalRuleMapIR(p.ir), p.ns, trivia2 ? { trivia: trivia2 } : void 0);
|
|
5991
6205
|
if (!pieces) throw new Error(`compose: carried IR for ns "${p.ns}" could not be re-lowered`);
|
|
5992
6206
|
return pieces;
|
|
5993
6207
|
}
|
|
@@ -5999,17 +6213,52 @@ function nextComposeNs(used) {
|
|
|
5999
6213
|
used.add(ns);
|
|
6000
6214
|
return ns;
|
|
6001
6215
|
}
|
|
6002
|
-
function
|
|
6216
|
+
function itemCarried(item, used, trivia2) {
|
|
6003
6217
|
const carried = item[COMPOSED_PIECES];
|
|
6004
|
-
|
|
6005
|
-
|
|
6006
|
-
|
|
6218
|
+
if (Array.isArray(carried)) {
|
|
6219
|
+
const items = carried;
|
|
6220
|
+
for (const p of items) used.add(p.ns);
|
|
6221
|
+
return items;
|
|
6222
|
+
}
|
|
6223
|
+
if (item.ruleFns instanceof Map) {
|
|
6224
|
+
const p = item;
|
|
6225
|
+
used.add(p.ns);
|
|
6226
|
+
return [p];
|
|
6227
|
+
}
|
|
6228
|
+
const map = item;
|
|
6229
|
+
const ns = nextComposeNs(used);
|
|
6230
|
+
const entries = Object.entries(map).filter(([, val]) => {
|
|
6231
|
+
const d = val._def;
|
|
6232
|
+
if (d.tag !== "lazy") return true;
|
|
6233
|
+
try {
|
|
6234
|
+
d.thunk();
|
|
6235
|
+
return true;
|
|
6236
|
+
} catch {
|
|
6237
|
+
return false;
|
|
6238
|
+
}
|
|
6239
|
+
});
|
|
6240
|
+
const ir = serializeRuleMap(entries);
|
|
6241
|
+
return ir ? [{ ns, ir }] : [linkable(map, ns, trivia2)];
|
|
6242
|
+
}
|
|
6243
|
+
function composingTriviaOf(items) {
|
|
6244
|
+
for (let i = items.length - 1; i >= 0; i--) {
|
|
6245
|
+
const item = items[i];
|
|
6246
|
+
if (!item || item.ruleFns instanceof Map || item[COMPOSED_PIECES]) continue;
|
|
6247
|
+
for (const v2 of Object.values(item)) {
|
|
6248
|
+
const t = v2?._meta?.grammarTrivia;
|
|
6249
|
+
if (t) return t;
|
|
6250
|
+
}
|
|
6251
|
+
}
|
|
6252
|
+
return void 0;
|
|
6007
6253
|
}
|
|
6008
6254
|
function compose(items) {
|
|
6009
6255
|
const used = /* @__PURE__ */ new Set();
|
|
6010
|
-
const
|
|
6256
|
+
const trivia2 = composingTriviaOf(items);
|
|
6257
|
+
const carried = items.flatMap((item) => itemCarried(item, used, trivia2));
|
|
6258
|
+
const pieces = carried.map((p) => materializePiece(p, trivia2));
|
|
6011
6259
|
const map = fuseRules(pieces);
|
|
6012
|
-
Object.defineProperty(map, COMPOSED_PIECES, { value:
|
|
6260
|
+
Object.defineProperty(map, COMPOSED_PIECES, { value: carried, enumerable: false });
|
|
6261
|
+
if (trivia2) Object.defineProperty(map, COMPOSED_TRIVIA, { value: trivia2, enumerable: false });
|
|
6013
6262
|
return map;
|
|
6014
6263
|
}
|
|
6015
6264
|
|
|
@@ -6292,12 +6541,14 @@ function run(entry, input, options = {}) {
|
|
|
6292
6541
|
}
|
|
6293
6542
|
const triviaLog = [];
|
|
6294
6543
|
const errors = [];
|
|
6544
|
+
const grammarTrivia = typeof entry !== "function" ? entry._meta.grammarTrivia : void 0;
|
|
6295
6545
|
const ctx = {
|
|
6296
6546
|
trackLines: false,
|
|
6297
6547
|
_triviaLog: triviaLog,
|
|
6298
6548
|
_errors: errors,
|
|
6299
6549
|
build: options.build,
|
|
6300
6550
|
state: options.state,
|
|
6551
|
+
...grammarTrivia !== void 0 ? { trivia: grammarTrivia, ...grammarTrivia._meta.triviaKindLabels ? { triviaKindLabels: grammarTrivia._meta.triviaKindLabels } : {} } : {},
|
|
6301
6552
|
...options.triviaCaptureMask !== void 0 ? { _triviaCaptureMask: options.triviaCaptureMask } : {}
|
|
6302
6553
|
};
|
|
6303
6554
|
const r = invoke(entry, input, 0, ctx);
|