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
@@ -1252,6 +1252,104 @@ function parser(opts, root) {
1252
1252
  };
1253
1253
  }
1254
1254
 
1255
+ // src/cst/reflection.ts
1256
+ var GRAMMAR_REFLECTION = /* @__PURE__ */ Symbol.for("parseman.grammarReflection");
1257
+ function childrenOf(def) {
1258
+ switch (def.tag) {
1259
+ case "sequence":
1260
+ case "choice":
1261
+ return def.parsers;
1262
+ case "dispatch":
1263
+ return [
1264
+ def.selector,
1265
+ ...def.cases.map((c) => c.parser),
1266
+ ...def.matchers ? def.matchers.map((c) => c.parser) : [],
1267
+ ...def.otherwise ? [def.otherwise] : []
1268
+ ];
1269
+ case "many":
1270
+ case "oneOrMore":
1271
+ case "optional":
1272
+ case "attempt":
1273
+ case "transform":
1274
+ case "trivia":
1275
+ case "token":
1276
+ case "leaf":
1277
+ case "label":
1278
+ case "field":
1279
+ case "not":
1280
+ case "peek":
1281
+ case "node":
1282
+ case "withCtx":
1283
+ case "recover":
1284
+ case "expect":
1285
+ return [def.parser];
1286
+ case "grammar":
1287
+ return def.triviaParser ? [def.parser, def.triviaParser] : [def.parser];
1288
+ case "sepBy":
1289
+ return [def.parser, def.separator];
1290
+ case "skip":
1291
+ return [def.main, def.skipped];
1292
+ case "scanTo":
1293
+ return [def.sentinel, ...def.skip];
1294
+ case "literal":
1295
+ case "regex":
1296
+ case "keywords":
1297
+ case "routed":
1298
+ case "guard":
1299
+ case "lazy":
1300
+ case "unknown":
1301
+ return [];
1302
+ }
1303
+ }
1304
+ function addNode(out, def) {
1305
+ if (def.type === void 0) return;
1306
+ const existing = out.get(def.type);
1307
+ if (existing === void 0) {
1308
+ out.set(def.type, def.tags === void 0 ? [] : [...def.tags]);
1309
+ return;
1310
+ }
1311
+ if (def.tags === void 0) return;
1312
+ for (const tag of def.tags) {
1313
+ if (!existing.includes(tag)) existing.push(tag);
1314
+ }
1315
+ }
1316
+ function collectFromCombinator(parser2, out, seen, followLazy = true) {
1317
+ if (seen.has(parser2)) return;
1318
+ seen.add(parser2);
1319
+ const def = parser2._def;
1320
+ if (def.tag === "lazy") {
1321
+ if (followLazy) {
1322
+ try {
1323
+ collectFromCombinator(def.thunk(), out, seen, followLazy);
1324
+ } catch {
1325
+ }
1326
+ }
1327
+ return;
1328
+ }
1329
+ if (def.tag === "node") addNode(out, def);
1330
+ for (const child of childrenOf(def)) collectFromCombinator(child, out, seen, followLazy);
1331
+ }
1332
+ function collectGrammarReflection(ruleMap, opts) {
1333
+ const byType = /* @__PURE__ */ new Map();
1334
+ const followLazy = opts?.followLazy !== false;
1335
+ for (const [, rule] of ruleMap) {
1336
+ if (!followLazy && rule._def.tag === "lazy") {
1337
+ try {
1338
+ const resolved = rule._def.thunk();
1339
+ if (resolved._def.tag !== "lazy") collectFromCombinator(resolved, byType, /* @__PURE__ */ new Set(), false);
1340
+ } catch {
1341
+ }
1342
+ continue;
1343
+ }
1344
+ collectFromCombinator(rule, byType, /* @__PURE__ */ new Set(), followLazy);
1345
+ }
1346
+ return { nodes: [...byType].map(([type, tags]) => ({ type, tags })) };
1347
+ }
1348
+ function attachGrammarReflection(grammar, reflection) {
1349
+ Object.defineProperty(grammar, GRAMMAR_REFLECTION, { value: reflection, enumerable: false, configurable: true });
1350
+ return grammar;
1351
+ }
1352
+
1255
1353
  // src/combinators/parser.ts
1256
1354
  var RULE_ORDER = "__parsemanRuleOrder";
1257
1355
  function tagRule(r, key) {
@@ -1350,6 +1448,7 @@ function rules(a, b) {
1350
1448
  enumerable: false,
1351
1449
  configurable: true
1352
1450
  });
1451
+ attachGrammarReflection(cache, collectGrammarReflection(Object.keys(definitions).map((key) => [key, cache[key]])));
1353
1452
  return cache;
1354
1453
  }
1355
1454
 
@@ -2976,13 +3075,14 @@ function node(typeOrCombinator, combinatorOrBuild, buildOrOpts, maybeOpts) {
2976
3075
  const collapse = opts?.collapse === true;
2977
3076
  const captureTrivia = opts?.captureTrivia === true;
2978
3077
  const trailingTrivia = opts?.trailingTrivia === true;
3078
+ const tags = opts?.tags;
2979
3079
  if (unwrap && collapse) {
2980
3080
  throw new Error("node() options cannot set both unwrap and collapse");
2981
3081
  }
2982
3082
  if (project !== void 0 && (unwrap || collapse)) {
2983
3083
  throw new Error("node() options cannot combine project with unwrap or collapse");
2984
3084
  }
2985
- 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;
3085
+ 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;
2986
3086
  const capturesTrivia = captureTrivia || trailingTrivia || (build ? buildReadsTrivia(def) : project === void 0);
2987
3087
  const clonesState = build ? buildReadsState(def) : project === void 0;
2988
3088
  const hasOwnFields = parserHasOwnFields(combinator);
@@ -3028,7 +3128,7 @@ function node(typeOrCombinator, combinatorOrBuild, buildOrOpts, maybeOpts) {
3028
3128
  const cstOutput = cstOutputHost(ctx.build);
3029
3129
  const span = ctx.trackLines ? annotateSpanFromLineContext(r.span, ctx) : r.span;
3030
3130
  const hostState = clonesState ? st : ctx.state !== void 0 ? Object.assign({}, ctx.state) : void 0;
3031
- 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 };
3131
+ 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 };
3032
3132
  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 };
3033
3133
  if (saved.buf !== void 0 || saved.ch !== void 0) {
3034
3134
  pushCstChild(ctx, built, rawEntry);
@@ -3320,15 +3420,16 @@ function normalizeCstCollapse(collapse) {
3320
3420
  }
3321
3421
  return typeof collapse === "function" ? collapse : void 0;
3322
3422
  }
3323
- function buildCstNode(type, children, _fields, span, _rawChildren, _triviaLog, state) {
3324
- return { _tag: "node", type, span: { ...span }, state: state ?? null, children: [...children] };
3423
+ function buildCstNode(type, children, _fields, span, _rawChildren, _triviaLog, state, tags) {
3424
+ 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] };
3325
3425
  }
3326
- function cstBuildHost(typeOrOptions, children, _fields, span, rawChildren, triviaLog, state) {
3426
+ function cstBuildHost(typeOrOptions, children, _fields, span, rawChildren, triviaLog, state, _tags) {
3327
3427
  if (typeof typeOrOptions === "string") {
3328
3428
  return buildCstNode(typeOrOptions, children ?? [], _fields, span ?? { start: 0, end: 0 }, rawChildren, triviaLog, state);
3329
3429
  }
3330
3430
  const collapse = normalizeCstCollapse(typeOrOptions?.collapse);
3331
- const host = (type, children2, fields, span2, rawChildren2, triviaLog2, state2) => buildCstNode(type, children2 ?? [], fields, span2, rawChildren2, triviaLog2, state2);
3431
+ const materializeTags = typeOrOptions?.tags === true;
3432
+ const host = (type, children2, fields, span2, rawChildren2, triviaLog2, state2, tags) => buildCstNode(type, children2 ?? [], fields, span2, rawChildren2, triviaLog2, state2, materializeTags ? tags : void 0);
3332
3433
  host._parsemanCstOutput = true;
3333
3434
  if (collapse) host._parsemanCstCollapse = collapse;
3334
3435
  return host;