parseman 0.42.1 → 0.44.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 (68) hide show
  1. package/dist/combinators/attempt.d.ts.map +1 -1
  2. package/dist/combinators/choice.d.ts.map +1 -1
  3. package/dist/combinators/grammar.d.ts +7 -0
  4. package/dist/combinators/grammar.d.ts.map +1 -1
  5. package/dist/combinators/map.d.ts +11 -0
  6. package/dist/combinators/map.d.ts.map +1 -1
  7. package/dist/combinators/node.d.ts +21 -7
  8. package/dist/combinators/node.d.ts.map +1 -1
  9. package/dist/combinators/parser.d.ts +17 -2
  10. package/dist/combinators/parser.d.ts.map +1 -1
  11. package/dist/combinators/token.d.ts +3 -2
  12. package/dist/combinators/token.d.ts.map +1 -1
  13. package/dist/combinators/trivia-skip.d.ts +1 -0
  14. package/dist/combinators/trivia-skip.d.ts.map +1 -1
  15. package/dist/compiler/codegen.d.ts +4 -0
  16. package/dist/compiler/codegen.d.ts.map +1 -1
  17. package/dist/compiler/ir-serialize.d.ts.map +1 -1
  18. package/dist/compiler/linker.d.ts +6 -1
  19. package/dist/compiler/linker.d.ts.map +1 -1
  20. package/dist/compiler/scannable-run.d.ts +1 -1
  21. package/dist/compiler/scannable-run.d.ts.map +1 -1
  22. package/dist/compiler/trivia-fast-path.d.ts +4 -3
  23. package/dist/compiler/trivia-fast-path.d.ts.map +1 -1
  24. package/dist/cst/reflection.d.ts +24 -0
  25. package/dist/cst/reflection.d.ts.map +1 -0
  26. package/dist/cst/trivia-entries.d.ts +15 -9
  27. package/dist/cst/trivia-entries.d.ts.map +1 -1
  28. package/dist/cst/trivia-kinds.d.ts +15 -11
  29. package/dist/cst/trivia-kinds.d.ts.map +1 -1
  30. package/dist/cst/types.d.ts +2 -0
  31. package/dist/cst/types.d.ts.map +1 -1
  32. package/dist/cst/walk.d.ts +27 -62
  33. package/dist/cst/walk.d.ts.map +1 -1
  34. package/dist/functional/run.d.ts +22 -16
  35. package/dist/functional/run.d.ts.map +1 -1
  36. package/dist/index.cjs +1651 -1207
  37. package/dist/index.cjs.map +4 -4
  38. package/dist/index.d.ts +6 -5
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +1651 -1207
  41. package/dist/index.js.map +4 -4
  42. package/dist/language-service/index.cjs +186 -135
  43. package/dist/language-service/index.cjs.map +4 -4
  44. package/dist/language-service/index.d.ts +1 -1
  45. package/dist/language-service/index.d.ts.map +1 -1
  46. package/dist/language-service/index.js +186 -135
  47. package/dist/language-service/index.js.map +4 -4
  48. package/dist/plugin/evaluator.d.ts +1 -0
  49. package/dist/plugin/evaluator.d.ts.map +1 -1
  50. package/dist/plugin/index.cjs +1525 -1048
  51. package/dist/plugin/index.cjs.map +4 -4
  52. package/dist/plugin/index.d.ts.map +1 -1
  53. package/dist/plugin/index.js +1525 -1048
  54. package/dist/plugin/index.js.map +4 -4
  55. package/dist/run/index.cjs +152 -102
  56. package/dist/run/index.cjs.map +3 -3
  57. package/dist/run/index.d.ts +1 -1
  58. package/dist/run/index.d.ts.map +1 -1
  59. package/dist/run/index.js +152 -102
  60. package/dist/run/index.js.map +3 -3
  61. package/dist/spec/index.cjs +1381 -1152
  62. package/dist/spec/index.cjs.map +4 -4
  63. package/dist/spec/index.js +1381 -1152
  64. package/dist/spec/index.js.map +4 -4
  65. package/dist/types.d.ts +27 -1
  66. package/dist/types.d.ts.map +1 -1
  67. package/dist/version.d.ts +1 -1
  68. package/package.json +1 -1
@@ -102,6 +102,7 @@ function probeAt(sentinel, input, pos, ctx) {
102
102
  _cstRawChildren: ctx._cstRawChildren,
103
103
  _cstTriviaLog: ctx._cstTriviaLog,
104
104
  _triviaLog: ctx._triviaLog,
105
+ _rootTriviaLog: ctx._rootTriviaLog,
105
106
  _fields: ctx._fields,
106
107
  _cstBuf: ctx._cstBuf,
107
108
  _tolerant: ctx._tolerant,
@@ -114,6 +115,7 @@ function probeAt(sentinel, input, pos, ctx) {
114
115
  ctx._cstRawChildren = void 0;
115
116
  ctx._cstTriviaLog = void 0;
116
117
  ctx._triviaLog = void 0;
118
+ ctx._rootTriviaLog = void 0;
117
119
  ctx._fields = void 0;
118
120
  ctx._cstBuf = void 0;
119
121
  ctx._tolerant = void 0;
@@ -128,6 +130,7 @@ function probeAt(sentinel, input, pos, ctx) {
128
130
  ctx._cstRawChildren = s._cstRawChildren;
129
131
  ctx._cstTriviaLog = s._cstTriviaLog;
130
132
  ctx._triviaLog = s._triviaLog;
133
+ ctx._rootTriviaLog = s._rootTriviaLog;
131
134
  ctx._fields = s._fields;
132
135
  ctx._cstBuf = s._cstBuf;
133
136
  ctx._tolerant = s._tolerant;
@@ -185,105 +188,97 @@ var REC = { scan: recoverScan, at: matchesAt, or: orSentinel, sentinel: firstSet
185
188
  // src/cst/trivia-entries.ts
186
189
  var EMPTY_INDICES = Object.freeze([]);
187
190
  var EMPTY_GAPS = Object.freeze([]);
188
- function entryOffset(i, stride) {
189
- return i * stride;
190
- }
191
- function triviaEntries(log, labels, opts) {
192
- const baseStride = opts?.nodeLog ? 3 : 2;
193
- const stride = labels ? baseStride + 1 : baseStride;
194
- const length = Math.floor(log.length / stride);
195
- return {
191
+ var SELECTED_ROOT_STRIDE = 5;
192
+ function buildRootTriviaIndex(log, labels) {
193
+ const length = Math.floor(log.length / SELECTED_ROOT_STRIDE);
194
+ const markerOffset = (i) => i * SELECTED_ROOT_STRIDE;
195
+ const ownedRangeStart = (i) => log[markerOffset(i)];
196
+ const ownedRangeEnd = (i) => log[markerOffset(i) + 1];
197
+ const markerStart = (i) => log[markerOffset(i) + 2];
198
+ const markerEnd = (i) => log[markerOffset(i) + 3];
199
+ const markerKind = (i) => log[markerOffset(i) + 4];
200
+ const entries = {
196
201
  length,
197
202
  labels,
198
- stride,
199
- start(i) {
200
- return log[entryOffset(i, stride)];
201
- },
202
- end(i) {
203
- return log[entryOffset(i, stride) + 1];
204
- },
205
- insertIndex(i) {
206
- return opts?.nodeLog ? log[entryOffset(i, stride) + 2] : void 0;
207
- },
208
- kindIndex(i) {
209
- if (!labels) return void 0;
210
- return log[entryOffset(i, stride) + baseStride];
211
- },
203
+ stride: SELECTED_ROOT_STRIDE,
204
+ start: markerStart,
205
+ end: markerEnd,
206
+ insertIndex: () => void 0,
207
+ kindIndex: markerKind,
212
208
  kind(i) {
213
- const ki = labels ? log[entryOffset(i, stride) + baseStride] : void 0;
214
- return ki !== void 0 ? labels[ki] : void 0;
209
+ return labels[markerKind(i)];
215
210
  },
216
211
  text(i, input) {
217
- const s = log[entryOffset(i, stride)];
218
- const e = log[entryOffset(i, stride) + 1];
219
- return input.slice(s, e);
212
+ return input.slice(markerStart(i), markerEnd(i));
220
213
  }
221
214
  };
222
- }
223
- function appendEntryRange(map, key, first, last) {
224
- let indices = map.get(key);
225
- if (!indices) {
226
- indices = [];
227
- map.set(key, indices);
228
- }
229
- for (let i = first; i <= last; i++) indices.push(i);
230
- }
231
- function buildGap(entries, start, end, first, last) {
232
- const entryIndices = [];
233
- for (let i = first; i <= last; i++) entryIndices.push(i);
234
- return {
235
- start,
236
- end,
237
- entryIndices,
238
- hasKind(kind) {
239
- for (const i of entryIndices) {
240
- if (entries.kind(i) === kind) return true;
241
- }
242
- return false;
243
- },
244
- text(input) {
245
- return input.slice(start, end);
215
+ const lowerBound = (offset, by) => {
216
+ let lo = 0;
217
+ let hi = length;
218
+ while (lo < hi) {
219
+ const mid = lo + hi >>> 1;
220
+ if (by(mid) < offset) lo = mid + 1;
221
+ else hi = mid;
246
222
  }
223
+ return lo;
247
224
  };
248
- }
249
- function buildRootMaps(entries) {
250
- const before = /* @__PURE__ */ new Map();
251
- const after = /* @__PURE__ */ new Map();
252
- const beforeGaps = /* @__PURE__ */ new Map();
253
- const afterGaps = /* @__PURE__ */ new Map();
254
- const gaps = [];
255
- if (entries.length === 0) return { before, after, beforeGaps, afterGaps, gaps };
256
- const appendGap = (first2, last, start2, end2) => {
257
- const gap = buildGap(entries, start2, end2, first2, last);
258
- gaps.push(gap);
259
- afterGaps.set(start2, gap);
260
- beforeGaps.set(end2, gap);
261
- appendEntryRange(after, start2, first2, last);
262
- appendEntryRange(before, end2, first2, last);
225
+ const gapRowsAt = (offset, direction) => {
226
+ const edge = direction === "before" ? ownedRangeEnd : ownedRangeStart;
227
+ let first = lowerBound(offset, edge);
228
+ if (first === length || edge(first) !== offset) return void 0;
229
+ const start = ownedRangeStart(first);
230
+ const end = ownedRangeEnd(first);
231
+ while (first > 0 && ownedRangeStart(first - 1) === start && ownedRangeEnd(first - 1) === end) first--;
232
+ let last = first + 1;
233
+ while (last < length && ownedRangeStart(last) === start && ownedRangeEnd(last) === end) last++;
234
+ return [first, last];
235
+ };
236
+ const indices = (first, last) => {
237
+ const out = [];
238
+ for (let i = first; i < last; i++) out.push(i);
239
+ return out;
240
+ };
241
+ const makeGap = (first, last) => {
242
+ const start = ownedRangeStart(first);
243
+ const end = ownedRangeEnd(first);
244
+ let entryIndices;
245
+ return {
246
+ start,
247
+ end,
248
+ get entryIndices() {
249
+ return entryIndices ??= indices(first, last);
250
+ },
251
+ hasKind(kind) {
252
+ for (let i = first; i < last; i++) {
253
+ if (labels[markerKind(i)] === kind) return true;
254
+ }
255
+ return false;
256
+ },
257
+ text(input) {
258
+ return input.slice(start, end);
259
+ }
260
+ };
263
261
  };
264
- let first = 0;
265
- let start = entries.start(0);
266
- let end = entries.end(0);
267
- for (let i = 1; i < entries.length; i++) {
268
- const nextStart = entries.start(i);
269
- const nextEnd = entries.end(i);
270
- if (nextStart === end) {
271
- end = nextEnd;
272
- continue;
273
- }
274
- appendGap(first, i - 1, start, end);
275
- first = i;
276
- start = nextStart;
277
- end = nextEnd;
278
- }
279
- appendGap(first, entries.length - 1, start, end);
280
- return { before, after, beforeGaps, afterGaps, gaps };
281
- }
282
- function buildRootTriviaIndex(log, labels) {
283
- const entries = triviaEntries(log, labels);
284
262
  let maps;
285
263
  const getMaps = () => {
286
- maps ??= buildRootMaps(entries);
264
+ if (maps !== void 0) return maps;
265
+ const before = /* @__PURE__ */ new Map();
266
+ const after = /* @__PURE__ */ new Map();
267
+ const beforeGaps = /* @__PURE__ */ new Map();
268
+ const afterGaps = /* @__PURE__ */ new Map();
269
+ const gaps = [];
270
+ for (let first = 0; first < length; ) {
271
+ let last = first + 1;
272
+ while (last < length && ownedRangeStart(last) === ownedRangeStart(first) && ownedRangeEnd(last) === ownedRangeEnd(first)) last++;
273
+ const gap = makeGap(first, last);
274
+ gaps.push(gap);
275
+ afterGaps.set(gap.start, gap);
276
+ beforeGaps.set(gap.end, gap);
277
+ after.set(gap.start, [...gap.entryIndices]);
278
+ before.set(gap.end, [...gap.entryIndices]);
279
+ first = last;
280
+ }
281
+ maps = { before, after, beforeGaps, afterGaps, gaps };
287
282
  return maps;
288
283
  };
289
284
  return {
@@ -296,16 +291,20 @@ function buildRootTriviaIndex(log, labels) {
296
291
  return getMaps().after;
297
292
  },
298
293
  entryIndicesBefore(offset) {
299
- return getMaps().before.get(offset) ?? EMPTY_INDICES;
294
+ const rows = gapRowsAt(offset, "before");
295
+ return rows ? indices(rows[0], rows[1]) : EMPTY_INDICES;
300
296
  },
301
297
  entryIndicesAfter(offset) {
302
- return getMaps().after.get(offset) ?? EMPTY_INDICES;
298
+ const rows = gapRowsAt(offset, "after");
299
+ return rows ? indices(rows[0], rows[1]) : EMPTY_INDICES;
303
300
  },
304
301
  gapBefore(offset) {
305
- return getMaps().beforeGaps.get(offset);
302
+ const rows = gapRowsAt(offset, "before");
303
+ return rows ? makeGap(rows[0], rows[1]) : void 0;
306
304
  },
307
305
  gapAfter(offset) {
308
- return getMaps().afterGaps.get(offset);
306
+ const rows = gapRowsAt(offset, "after");
307
+ return rows ? makeGap(rows[0], rows[1]) : void 0;
309
308
  },
310
309
  gaps() {
311
310
  return getMaps().gaps;
@@ -313,13 +312,21 @@ function buildRootTriviaIndex(log, labels) {
313
312
  gapsWithKind(kind) {
314
313
  const kinds = typeof kind === "string" ? [kind] : kind;
315
314
  const matches2 = [];
316
- for (const gap of getMaps().gaps) {
317
- for (const k of kinds) {
318
- if (gap.hasKind(k)) {
319
- matches2.push(gap);
320
- break;
315
+ for (let first = 0; first < length; ) {
316
+ let last = first + 1;
317
+ while (last < length && ownedRangeStart(last) === ownedRangeStart(first) && ownedRangeEnd(last) === ownedRangeEnd(first)) last++;
318
+ let matched = false;
319
+ for (let i = first; i < last && !matched; i++) {
320
+ const label2 = labels[markerKind(i)];
321
+ for (const kind2 of kinds) {
322
+ if (label2 === kind2) {
323
+ matched = true;
324
+ break;
325
+ }
321
326
  }
322
327
  }
328
+ if (matched) matches2.push(makeGap(first, last));
329
+ first = last;
323
330
  }
324
331
  return matches2.length === 0 ? EMPTY_GAPS : matches2;
325
332
  }
@@ -349,7 +356,20 @@ function triviaKindLabelsFromRunnable(r) {
349
356
  if (!r) return void 0;
350
357
  if (typeof r === "function") return r._meta?.triviaKindLabels;
351
358
  const grammarTrivia = r._meta.grammarTrivia;
352
- return r._meta.triviaKindLabels ?? grammarTrivia?._meta.triviaKindLabels ?? (r._def.tag === "grammar" ? r._def.triviaParser?._meta.triviaKindLabels : void 0);
359
+ return grammarTrivia?._meta.triviaKindLabels ?? r._meta.triviaKindLabels ?? (r._def.tag === "grammar" ? r._def.triviaParser?._meta.triviaKindLabels : void 0);
360
+ }
361
+ function rootTriviaClassifiedFromRunnable(r) {
362
+ if (!r) return false;
363
+ if (typeof r === "function") return r._meta?.rootTriviaClassified === true;
364
+ const grammarTrivia = r._meta.grammarTrivia;
365
+ return grammarTrivia?._meta.rootTriviaClassified === true || r._meta.rootTriviaClassified === true;
366
+ }
367
+ function makeSelectedRootLabelIndex(labels) {
368
+ const index = /* @__PURE__ */ Object.create(null);
369
+ for (let i = 0; i < labels.length; i++) {
370
+ if (index[labels[i]] === void 0) index[labels[i]] = i;
371
+ }
372
+ return index;
353
373
  }
354
374
  function runOnce(entry, input, options, phase, profileState) {
355
375
  if (typeof entry !== "function" && typeof entry?.parse !== "function") {
@@ -357,13 +377,31 @@ function runOnce(entry, input, options, phase, profileState) {
357
377
  `run(): start production is ${entry === null ? "null" : typeof entry}, not a rule \u2014 the requested grammar rule does not exist (check the rule name).`
358
378
  );
359
379
  }
360
- const triviaLog = [];
380
+ const rootTriviaSelection = options.rootTrivia?.select;
381
+ const captureRootTrivia = rootTriviaSelection !== void 0 && rootTriviaSelection.length > 0;
382
+ const selectedRootLog = captureRootTrivia ? [] : void 0;
383
+ const selectedRootLabelIndex = captureRootTrivia ? makeSelectedRootLabelIndex(rootTriviaSelection) : void 0;
361
384
  const errors = [];
362
385
  const profile = profileState ?? (phase === void 0 ? void 0 : { phase, nodes: 0, childSlots: 0, rawSlots: 0, triviaSlots: 0, fieldSlots: 0, hostCalls: 0 });
363
386
  const skipGlobalSinks = phase === "recognizer" || phase === "capture";
364
387
  const grammarTrivia = typeof entry !== "function" ? entry._meta.grammarTrivia : void 0;
365
388
  const grammarScanSkip = typeof entry !== "function" ? entry._meta.grammarScanSkip : void 0;
366
389
  const triviaKindLabels = triviaKindLabelsFromRunnable(entry) ?? triviaKindLabelsFromRunnable(options.trivia);
390
+ if (captureRootTrivia && triviaKindLabels === void 0) {
391
+ throw new TypeError("run(): rootTrivia.select requires labeled grammar trivia.");
392
+ }
393
+ if (captureRootTrivia) {
394
+ for (const label2 of rootTriviaSelection) {
395
+ if (!triviaKindLabels.includes(label2)) {
396
+ throw new TypeError(`run(): rootTrivia.select contains unknown trivia label ${JSON.stringify(label2)}.`);
397
+ }
398
+ }
399
+ }
400
+ if (captureRootTrivia && !rootTriviaClassifiedFromRunnable(entry) && !rootTriviaClassifiedFromRunnable(options.trivia)) {
401
+ throw new TypeError(
402
+ "run(): rootTrivia.select requires classifiedTrivia() at the root."
403
+ );
404
+ }
367
405
  const stamped = entry;
368
406
  assertHostModeCompatible(
369
407
  typeof entry === "function" ? stamped[FUSED_HOST_MODE] ?? "ast" : entry._meta.grammarHostMode ?? "ast",
@@ -372,7 +410,15 @@ function runOnce(entry, input, options, phase, profileState) {
372
410
  );
373
411
  const ctx = {
374
412
  trackLines: false,
375
- ...skipGlobalSinks ? { _pmProfile: profile } : { _triviaLog: triviaLog, _errors: errors, ...profile === void 0 ? {} : { _pmProfile: profile } },
413
+ ...skipGlobalSinks ? { _pmProfile: profile } : {
414
+ ...captureRootTrivia ? {
415
+ _rootTriviaLog: selectedRootLog,
416
+ _rootTriviaKindIndex: selectedRootLabelIndex,
417
+ _rootTriviaStrictScopes: true
418
+ } : {},
419
+ _errors: errors,
420
+ ...profile === void 0 ? {} : { _pmProfile: profile }
421
+ },
376
422
  build: options.build,
377
423
  state: options.state,
378
424
  ...grammarTrivia !== void 0 ? { trivia: grammarTrivia, ...triviaKindLabels ? { triviaKindLabels } : {} } : triviaKindLabels ? { triviaKindLabels } : {},
@@ -397,9 +443,13 @@ function runOnce(entry, input, options, phase, profileState) {
397
443
  span: r.span ?? { start: 0, end: 0 },
398
444
  expected: r.ok ? [] : r.expected ?? [],
399
445
  errors,
400
- triviaLog,
401
- ...triviaKindLabels === void 0 ? {} : { triviaKindLabels },
402
- triviaMap: buildRootTriviaIndex(triviaLog, triviaKindLabels),
446
+ ...selectedRootLog !== void 0 && selectedRootLog.length > 0 ? {
447
+ rootTrivia: {
448
+ rows: selectedRootLog,
449
+ select: rootTriviaSelection,
450
+ index: buildRootTriviaIndex(selectedRootLog, rootTriviaSelection)
451
+ }
452
+ } : {},
403
453
  unconsumedFrom
404
454
  };
405
455
  }
@@ -458,6 +508,10 @@ var WORDS_RE = new RegExp(`^${WORD_RE_SRC}(?:\\|${WORD_RE_SRC})*$`);
458
508
 
459
509
  // src/compiler/scannable-run.ts
460
510
  var META = new Set("()[]{}*+?|^$.".split(""));
511
+ var SELECTED_ROOT_OWNED_RANGE_PLACEHOLDER = Array.from(
512
+ { length: SELECTED_ROOT_STRIDE - 3 },
513
+ () => "0"
514
+ ).join(", ");
461
515
 
462
516
  // src/compiler/trivia-fast-path.ts
463
517
  var CAP_RECORD = [
@@ -466,6 +520,11 @@ var CAP_RECORD = [
466
520
  ` if (_cap === 1 && _ctx._cstTriviaLog !== undefined && _ctx.captureTrivia) _ctx._cstTriviaLog.push(_pos, _e, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0)`,
467
521
  ` }`
468
522
  ].join("\n");
523
+ var SELECTED_ROOT_FINISH = [
524
+ ` if (_cap && _ctx._rootTriviaCapture !== false && _ctx._rootTriviaLog !== undefined && _ctx._rootTriviaLog.length !== _rootMark) {`,
525
+ ` for (let _ri = _rootMark; _ri < _ctx._rootTriviaLog.length; _ri += ${SELECTED_ROOT_STRIDE}) { _ctx._rootTriviaLog[_ri] = _pos; _ctx._rootTriviaLog[_ri + 1] = _e }`,
526
+ ` }`
527
+ ].join("\n");
469
528
 
470
529
  // src/compiler/linker.ts
471
530
  function normalizeCstCollapse(collapse) {
@@ -476,15 +535,16 @@ function normalizeCstCollapse(collapse) {
476
535
  }
477
536
  return typeof collapse === "function" ? collapse : void 0;
478
537
  }
479
- function buildCstNode(type, children, _fields, span, _rawChildren, _triviaLog, state) {
480
- return { _tag: "node", type, span: { ...span }, state: state ?? null, children: [...children] };
538
+ function buildCstNode(type, children, _fields, span, _rawChildren, _triviaLog, state, tags) {
539
+ 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] };
481
540
  }
482
- function cstBuildHost(typeOrOptions, children, _fields, span, rawChildren, triviaLog, state) {
541
+ function cstBuildHost(typeOrOptions, children, _fields, span, rawChildren, triviaLog, state, _tags) {
483
542
  if (typeof typeOrOptions === "string") {
484
543
  return buildCstNode(typeOrOptions, children ?? [], _fields, span ?? { start: 0, end: 0 }, rawChildren, triviaLog, state);
485
544
  }
486
545
  const collapse = normalizeCstCollapse(typeOrOptions?.collapse);
487
- const host = (type, children2, fields, span2, rawChildren2, triviaLog2, state2) => buildCstNode(type, children2 ?? [], fields, span2, rawChildren2, triviaLog2, state2);
546
+ const materializeTags = typeOrOptions?.tags === true;
547
+ const host = (type, children2, fields, span2, rawChildren2, triviaLog2, state2, tags) => buildCstNode(type, children2 ?? [], fields, span2, rawChildren2, triviaLog2, state2, materializeTags ? tags : void 0);
488
548
  host._parsemanCstOutput = true;
489
549
  if (collapse) host._parsemanCstCollapse = collapse;
490
550
  return host;
@@ -498,19 +558,6 @@ function fusedHostElidedOf(registry) {
498
558
  return registry[FUSED_HOST_ELIDED] === true;
499
559
  }
500
560
 
501
- // src/cst/walk.ts
502
- function walk(root, visitor, ctx = void 0) {
503
- const go = (node2, parent) => {
504
- const descend = visitor.enter ? visitor.enter(node2, parent, ctx) : void 0;
505
- const children = node2.children;
506
- if (descend !== false && Array.isArray(children)) {
507
- for (const child of children) go(child, node2);
508
- }
509
- visitor.leave?.(node2, parent, ctx);
510
- };
511
- go(root, null);
512
- }
513
-
514
561
  // src/cst/relative-spans.ts
515
562
  function relativizeCST(node2, parentBase = 0) {
516
563
  const span = { start: node2.span.start - parentBase, end: node2.span.end - parentBase };
@@ -953,6 +1000,14 @@ function parseDoc(registry, rootRule, input, opts = {}) {
953
1000
 
954
1001
  // src/language-service/index.ts
955
1002
  var errorMessage = (expected) => expected.length ? `Unexpected input; expected ${expected.join(" or ")}` : "Unexpected input";
1003
+ function visitTree(root, enter) {
1004
+ const go = (node2) => {
1005
+ enter(node2);
1006
+ const children = node2.children;
1007
+ if (Array.isArray(children)) for (const child of children) go(child);
1008
+ };
1009
+ go(root);
1010
+ }
956
1011
  function mergeDiagnostics(tree, flat, unconsumedFrom, inputLength, config) {
957
1012
  const out = [];
958
1013
  const seen = /* @__PURE__ */ new Set();
@@ -972,17 +1027,15 @@ function mergeDiagnostics(tree, flat, unconsumedFrom, inputLength, config) {
972
1027
  function diagnoseTree(root, config) {
973
1028
  const out = [];
974
1029
  const rules2 = config.diagnostics;
975
- walk(root, {
976
- enter(node2) {
977
- if (node2._tag === "parseError") {
978
- out.push({ severity: "error", message: errorMessage(node2.expected ?? []), span: node2.span });
979
- return;
980
- }
981
- const h = rules2 && node2.type !== void 0 ? rules2[node2.type] : void 0;
982
- if (h) {
983
- const d = h(node2);
984
- if (d) out.push(...Array.isArray(d) ? d : [d]);
985
- }
1030
+ visitTree(root, (node2) => {
1031
+ if (node2._tag === "parseError") {
1032
+ out.push({ severity: "error", message: errorMessage(node2.expected ?? []), span: node2.span });
1033
+ return;
1034
+ }
1035
+ const h = rules2 && node2.type !== void 0 ? rules2[node2.type] : void 0;
1036
+ if (h) {
1037
+ const d = h(node2);
1038
+ if (d) out.push(...Array.isArray(d) ? d : [d]);
986
1039
  }
987
1040
  });
988
1041
  return out;
@@ -1040,11 +1093,9 @@ function languageService(grammar, config = {}) {
1040
1093
  function ruleAtCursor(root, offset) {
1041
1094
  if (!root || typeof root !== "object") return null;
1042
1095
  let best = null;
1043
- walk(root, {
1044
- enter(node2) {
1045
- const s = node2.span;
1046
- if (s && s.start <= offset && offset <= s.end && node2.type !== void 0) best = node2.type;
1047
- }
1096
+ visitTree(root, (node2) => {
1097
+ const s = node2.span;
1098
+ if (s && s.start <= offset && offset <= s.end && node2.type !== void 0) best = node2.type;
1048
1099
  });
1049
1100
  return best;
1050
1101
  }