parseman 0.42.1 → 0.43.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.
Files changed (42) hide show
  1. package/dist/combinators/node.d.ts +21 -7
  2. package/dist/combinators/node.d.ts.map +1 -1
  3. package/dist/combinators/parser.d.ts +17 -2
  4. package/dist/combinators/parser.d.ts.map +1 -1
  5. package/dist/compiler/codegen.d.ts +4 -0
  6. package/dist/compiler/codegen.d.ts.map +1 -1
  7. package/dist/compiler/ir-serialize.d.ts.map +1 -1
  8. package/dist/compiler/linker.d.ts +6 -1
  9. package/dist/compiler/linker.d.ts.map +1 -1
  10. package/dist/cst/reflection.d.ts +24 -0
  11. package/dist/cst/reflection.d.ts.map +1 -0
  12. package/dist/cst/types.d.ts +2 -0
  13. package/dist/cst/types.d.ts.map +1 -1
  14. package/dist/cst/walk.d.ts +27 -62
  15. package/dist/cst/walk.d.ts.map +1 -1
  16. package/dist/index.cjs +227 -84
  17. package/dist/index.cjs.map +4 -4
  18. package/dist/index.d.ts +4 -3
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +227 -84
  21. package/dist/index.js.map +4 -4
  22. package/dist/language-service/index.cjs +25 -33
  23. package/dist/language-service/index.cjs.map +4 -4
  24. package/dist/language-service/index.d.ts +1 -1
  25. package/dist/language-service/index.d.ts.map +1 -1
  26. package/dist/language-service/index.js +25 -33
  27. package/dist/language-service/index.js.map +4 -4
  28. package/dist/plugin/evaluator.d.ts +1 -0
  29. package/dist/plugin/evaluator.d.ts.map +1 -1
  30. package/dist/plugin/index.cjs +323 -78
  31. package/dist/plugin/index.cjs.map +4 -4
  32. package/dist/plugin/index.d.ts.map +1 -1
  33. package/dist/plugin/index.js +323 -78
  34. package/dist/plugin/index.js.map +4 -4
  35. package/dist/spec/index.cjs +107 -6
  36. package/dist/spec/index.cjs.map +4 -4
  37. package/dist/spec/index.js +107 -6
  38. package/dist/spec/index.js.map +4 -4
  39. package/dist/types.d.ts +2 -1
  40. package/dist/types.d.ts.map +1 -1
  41. package/dist/version.d.ts +1 -1
  42. package/package.json +1 -1
@@ -1286,6 +1286,104 @@ function parser(opts, root) {
1286
1286
  };
1287
1287
  }
1288
1288
 
1289
+ // src/cst/reflection.ts
1290
+ var GRAMMAR_REFLECTION = /* @__PURE__ */ Symbol.for("parseman.grammarReflection");
1291
+ function childrenOf(def) {
1292
+ switch (def.tag) {
1293
+ case "sequence":
1294
+ case "choice":
1295
+ return def.parsers;
1296
+ case "dispatch":
1297
+ return [
1298
+ def.selector,
1299
+ ...def.cases.map((c) => c.parser),
1300
+ ...def.matchers ? def.matchers.map((c) => c.parser) : [],
1301
+ ...def.otherwise ? [def.otherwise] : []
1302
+ ];
1303
+ case "many":
1304
+ case "oneOrMore":
1305
+ case "optional":
1306
+ case "attempt":
1307
+ case "transform":
1308
+ case "trivia":
1309
+ case "token":
1310
+ case "leaf":
1311
+ case "label":
1312
+ case "field":
1313
+ case "not":
1314
+ case "peek":
1315
+ case "node":
1316
+ case "withCtx":
1317
+ case "recover":
1318
+ case "expect":
1319
+ return [def.parser];
1320
+ case "grammar":
1321
+ return def.triviaParser ? [def.parser, def.triviaParser] : [def.parser];
1322
+ case "sepBy":
1323
+ return [def.parser, def.separator];
1324
+ case "skip":
1325
+ return [def.main, def.skipped];
1326
+ case "scanTo":
1327
+ return [def.sentinel, ...def.skip];
1328
+ case "literal":
1329
+ case "regex":
1330
+ case "keywords":
1331
+ case "routed":
1332
+ case "guard":
1333
+ case "lazy":
1334
+ case "unknown":
1335
+ return [];
1336
+ }
1337
+ }
1338
+ function addNode(out, def) {
1339
+ if (def.type === void 0) return;
1340
+ const existing = out.get(def.type);
1341
+ if (existing === void 0) {
1342
+ out.set(def.type, def.tags === void 0 ? [] : [...def.tags]);
1343
+ return;
1344
+ }
1345
+ if (def.tags === void 0) return;
1346
+ for (const tag of def.tags) {
1347
+ if (!existing.includes(tag)) existing.push(tag);
1348
+ }
1349
+ }
1350
+ function collectFromCombinator(parser2, out, seen, followLazy = true) {
1351
+ if (seen.has(parser2)) return;
1352
+ seen.add(parser2);
1353
+ const def = parser2._def;
1354
+ if (def.tag === "lazy") {
1355
+ if (followLazy) {
1356
+ try {
1357
+ collectFromCombinator(def.thunk(), out, seen, followLazy);
1358
+ } catch {
1359
+ }
1360
+ }
1361
+ return;
1362
+ }
1363
+ if (def.tag === "node") addNode(out, def);
1364
+ for (const child of childrenOf(def)) collectFromCombinator(child, out, seen, followLazy);
1365
+ }
1366
+ function collectGrammarReflection(ruleMap, opts) {
1367
+ const byType = /* @__PURE__ */ new Map();
1368
+ const followLazy = opts?.followLazy !== false;
1369
+ for (const [, rule] of ruleMap) {
1370
+ if (!followLazy && rule._def.tag === "lazy") {
1371
+ try {
1372
+ const resolved = rule._def.thunk();
1373
+ if (resolved._def.tag !== "lazy") collectFromCombinator(resolved, byType, /* @__PURE__ */ new Set(), false);
1374
+ } catch {
1375
+ }
1376
+ continue;
1377
+ }
1378
+ collectFromCombinator(rule, byType, /* @__PURE__ */ new Set(), followLazy);
1379
+ }
1380
+ return { nodes: [...byType].map(([type, tags]) => ({ type, tags })) };
1381
+ }
1382
+ function attachGrammarReflection(grammar, reflection) {
1383
+ Object.defineProperty(grammar, GRAMMAR_REFLECTION, { value: reflection, enumerable: false, configurable: true });
1384
+ return grammar;
1385
+ }
1386
+
1289
1387
  // src/combinators/parser.ts
1290
1388
  var RULE_ORDER = "__parsemanRuleOrder";
1291
1389
  function tagRule(r, key) {
@@ -1384,6 +1482,7 @@ function rules(a, b) {
1384
1482
  enumerable: false,
1385
1483
  configurable: true
1386
1484
  });
1485
+ attachGrammarReflection(cache, collectGrammarReflection(Object.keys(definitions).map((key) => [key, cache[key]])));
1387
1486
  return cache;
1388
1487
  }
1389
1488
 
@@ -3010,13 +3109,14 @@ function node(typeOrCombinator, combinatorOrBuild, buildOrOpts, maybeOpts) {
3010
3109
  const collapse = opts?.collapse === true;
3011
3110
  const captureTrivia = opts?.captureTrivia === true;
3012
3111
  const trailingTrivia = opts?.trailingTrivia === true;
3112
+ const tags = opts?.tags;
3013
3113
  if (unwrap && collapse) {
3014
3114
  throw new Error("node() options cannot set both unwrap and collapse");
3015
3115
  }
3016
3116
  if (project !== void 0 && (unwrap || collapse)) {
3017
3117
  throw new Error("node() options cannot combine project with unwrap or collapse");
3018
3118
  }
3019
- const def = unwrap || collapse || project !== void 0 || captureTrivia || trailingTrivia ? { ...baseDef, ...unwrap ? { unwrap: true } : {}, ...collapse ? { collapse: true } : {}, ...project !== void 0 ? { project } : {}, ...captureTrivia ? { captureTrivia: true } : {}, ...trailingTrivia ? { trailingTrivia: true } : {} } : baseDef;
3119
+ const def = unwrap || collapse || project !== void 0 || captureTrivia || trailingTrivia || tags !== void 0 && tags.length > 0 ? { ...baseDef, ...unwrap ? { unwrap: true } : {}, ...collapse ? { collapse: true } : {}, ...project !== void 0 ? { project } : {}, ...captureTrivia ? { captureTrivia: true } : {}, ...trailingTrivia ? { trailingTrivia: true } : {}, ...tags !== void 0 && tags.length > 0 ? { tags } : {} } : baseDef;
3020
3120
  const capturesTrivia = captureTrivia || trailingTrivia || (build ? buildReadsTrivia(def) : project === void 0);
3021
3121
  const clonesState = build ? buildReadsState(def) : project === void 0;
3022
3122
  const hasOwnFields = parserHasOwnFields(combinator);
@@ -3062,7 +3162,7 @@ function node(typeOrCombinator, combinatorOrBuild, buildOrOpts, maybeOpts) {
3062
3162
  const cstOutput = cstOutputHost(ctx.build);
3063
3163
  const span = ctx.trackLines ? annotateSpanFromLineContext(r.span, ctx) : r.span;
3064
3164
  const hostState = clonesState ? st : ctx.state !== void 0 ? Object.assign({}, ctx.state) : void 0;
3065
- const built = unwrap && children.length === 1 ? unwrapChild(children[0]) : collapse && children.length === 1 ? children[0] : !build && project === void 0 && ctx.build?._parsemanCstCollapse && children.length === 1 && rawChildren.length === 1 && ctx.build._parsemanCstCollapse(nodeType, children[0], children, rawChildren) ? children[0] : project !== void 0 ? cstOutput && ctx.build ? ctx.build(nodeType, children, fields, span, rawChildren, triviaLog, hostState) : projectChild(children, project, nodeType) : build ? cstOutput && ctx.build ? ctx.build(nodeType, children, fields, span, rawChildren, triviaLog, hostState) : build(children, fields, span, rawChildren, triviaLog, st) : ctx.build ? ctx.build(nodeType, children, fields, span, rawChildren, triviaLog, st) : { _tag: "node", type: nodeType, span: ctx.trackLines ? { ...span } : { start: r.span.start, end: r.span.end }, state: st ?? null, children };
3165
+ const built = unwrap && children.length === 1 ? unwrapChild(children[0]) : collapse && children.length === 1 ? children[0] : !build && project === void 0 && ctx.build?._parsemanCstCollapse && children.length === 1 && rawChildren.length === 1 && ctx.build._parsemanCstCollapse(nodeType, children[0], children, rawChildren) ? children[0] : project !== void 0 ? cstOutput && ctx.build ? ctx.build(nodeType, children, fields, span, rawChildren, triviaLog, hostState, tags) : projectChild(children, project, nodeType) : build ? cstOutput && ctx.build ? ctx.build(nodeType, children, fields, span, rawChildren, triviaLog, hostState, tags) : build(children, fields, span, rawChildren, triviaLog, st) : ctx.build ? ctx.build(nodeType, children, fields, span, rawChildren, triviaLog, st, tags) : { _tag: "node", type: nodeType, span: ctx.trackLines ? { ...span } : { start: r.span.start, end: r.span.end }, state: st ?? null, children };
3066
3166
  const rawEntry = isCstChild(built) ? built : { _tag: "leaf", value: typeof built === "string" ? built : typeof built === "object" && built !== null ? input.slice(r.span.start, r.span.end) : "", span };
3067
3167
  if (saved.buf !== void 0 || saved.ch !== void 0) {
3068
3168
  pushCstChild(ctx, built, rawEntry);
@@ -3354,15 +3454,16 @@ function normalizeCstCollapse(collapse) {
3354
3454
  }
3355
3455
  return typeof collapse === "function" ? collapse : void 0;
3356
3456
  }
3357
- function buildCstNode(type, children, _fields, span, _rawChildren, _triviaLog, state) {
3358
- return { _tag: "node", type, span: { ...span }, state: state ?? null, children: [...children] };
3457
+ function buildCstNode(type, children, _fields, span, _rawChildren, _triviaLog, state, tags) {
3458
+ return tags !== void 0 && tags.length > 0 ? { _tag: "node", type, tags, span: { ...span }, state: state ?? null, children: [...children] } : { _tag: "node", type, span: { ...span }, state: state ?? null, children: [...children] };
3359
3459
  }
3360
- function cstBuildHost(typeOrOptions, children, _fields, span, rawChildren, triviaLog, state) {
3460
+ function cstBuildHost(typeOrOptions, children, _fields, span, rawChildren, triviaLog, state, _tags) {
3361
3461
  if (typeof typeOrOptions === "string") {
3362
3462
  return buildCstNode(typeOrOptions, children ?? [], _fields, span ?? { start: 0, end: 0 }, rawChildren, triviaLog, state);
3363
3463
  }
3364
3464
  const collapse = normalizeCstCollapse(typeOrOptions?.collapse);
3365
- const host = (type, children2, fields, span2, rawChildren2, triviaLog2, state2) => buildCstNode(type, children2 ?? [], fields, span2, rawChildren2, triviaLog2, state2);
3465
+ const materializeTags = typeOrOptions?.tags === true;
3466
+ const host = (type, children2, fields, span2, rawChildren2, triviaLog2, state2, tags) => buildCstNode(type, children2 ?? [], fields, span2, rawChildren2, triviaLog2, state2, materializeTags ? tags : void 0);
3366
3467
  host._parsemanCstOutput = true;
3367
3468
  if (collapse) host._parsemanCstCollapse = collapse;
3368
3469
  return host;