parseman 0.40.0 → 0.41.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.
@@ -136,6 +136,150 @@ function captureError(ctx, error) {
136
136
  }
137
137
  var REC = { scan: recoverScan, at: matchesAt, or: orSentinel, sentinel: firstSetSentinel, capture: captureError };
138
138
 
139
+ // src/cst/trivia-entries.ts
140
+ var EMPTY_INDICES = Object.freeze([]);
141
+ var EMPTY_GAPS = Object.freeze([]);
142
+ function entryOffset(i, stride) {
143
+ return i * stride;
144
+ }
145
+ function triviaEntries(log, labels, opts) {
146
+ const baseStride = opts?.nodeLog ? 3 : 2;
147
+ const stride = labels ? baseStride + 1 : baseStride;
148
+ const length = Math.floor(log.length / stride);
149
+ return {
150
+ length,
151
+ labels,
152
+ stride,
153
+ start(i) {
154
+ return log[entryOffset(i, stride)];
155
+ },
156
+ end(i) {
157
+ return log[entryOffset(i, stride) + 1];
158
+ },
159
+ insertIndex(i) {
160
+ return opts?.nodeLog ? log[entryOffset(i, stride) + 2] : void 0;
161
+ },
162
+ kindIndex(i) {
163
+ if (!labels) return void 0;
164
+ return log[entryOffset(i, stride) + baseStride];
165
+ },
166
+ kind(i) {
167
+ const ki = labels ? log[entryOffset(i, stride) + baseStride] : void 0;
168
+ return ki !== void 0 ? labels[ki] : void 0;
169
+ },
170
+ text(i, input) {
171
+ const s = log[entryOffset(i, stride)];
172
+ const e = log[entryOffset(i, stride) + 1];
173
+ return input.slice(s, e);
174
+ }
175
+ };
176
+ }
177
+ function appendEntryRange(map, key, first, last) {
178
+ let indices = map.get(key);
179
+ if (!indices) {
180
+ indices = [];
181
+ map.set(key, indices);
182
+ }
183
+ for (let i = first; i <= last; i++) indices.push(i);
184
+ }
185
+ function buildGap(entries, start, end, first, last) {
186
+ const entryIndices = [];
187
+ for (let i = first; i <= last; i++) entryIndices.push(i);
188
+ return {
189
+ start,
190
+ end,
191
+ entryIndices,
192
+ hasKind(kind) {
193
+ for (const i of entryIndices) {
194
+ if (entries.kind(i) === kind) return true;
195
+ }
196
+ return false;
197
+ },
198
+ text(input) {
199
+ return input.slice(start, end);
200
+ }
201
+ };
202
+ }
203
+ function buildRootMaps(entries) {
204
+ const before = /* @__PURE__ */ new Map();
205
+ const after = /* @__PURE__ */ new Map();
206
+ const beforeGaps = /* @__PURE__ */ new Map();
207
+ const afterGaps = /* @__PURE__ */ new Map();
208
+ const gaps = [];
209
+ if (entries.length === 0) return { before, after, beforeGaps, afterGaps, gaps };
210
+ const appendGap = (first2, last, start2, end2) => {
211
+ const gap = buildGap(entries, start2, end2, first2, last);
212
+ gaps.push(gap);
213
+ afterGaps.set(start2, gap);
214
+ beforeGaps.set(end2, gap);
215
+ appendEntryRange(after, start2, first2, last);
216
+ appendEntryRange(before, end2, first2, last);
217
+ };
218
+ let first = 0;
219
+ let start = entries.start(0);
220
+ let end = entries.end(0);
221
+ for (let i = 1; i < entries.length; i++) {
222
+ const nextStart = entries.start(i);
223
+ const nextEnd = entries.end(i);
224
+ if (nextStart === end) {
225
+ end = nextEnd;
226
+ continue;
227
+ }
228
+ appendGap(first, i - 1, start, end);
229
+ first = i;
230
+ start = nextStart;
231
+ end = nextEnd;
232
+ }
233
+ appendGap(first, entries.length - 1, start, end);
234
+ return { before, after, beforeGaps, afterGaps, gaps };
235
+ }
236
+ function buildRootTriviaIndex(log, labels) {
237
+ const entries = triviaEntries(log, labels);
238
+ let maps;
239
+ const getMaps = () => {
240
+ maps ??= buildRootMaps(entries);
241
+ return maps;
242
+ };
243
+ return {
244
+ entries,
245
+ labels,
246
+ get before() {
247
+ return getMaps().before;
248
+ },
249
+ get after() {
250
+ return getMaps().after;
251
+ },
252
+ entryIndicesBefore(offset) {
253
+ return getMaps().before.get(offset) ?? EMPTY_INDICES;
254
+ },
255
+ entryIndicesAfter(offset) {
256
+ return getMaps().after.get(offset) ?? EMPTY_INDICES;
257
+ },
258
+ gapBefore(offset) {
259
+ return getMaps().beforeGaps.get(offset);
260
+ },
261
+ gapAfter(offset) {
262
+ return getMaps().afterGaps.get(offset);
263
+ },
264
+ gaps() {
265
+ return getMaps().gaps;
266
+ },
267
+ gapsWithKind(kind) {
268
+ const kinds = typeof kind === "string" ? [kind] : kind;
269
+ const matches2 = [];
270
+ for (const gap of getMaps().gaps) {
271
+ for (const k of kinds) {
272
+ if (gap.hasKind(k)) {
273
+ matches2.push(gap);
274
+ break;
275
+ }
276
+ }
277
+ }
278
+ return matches2.length === 0 ? EMPTY_GAPS : matches2;
279
+ }
280
+ };
281
+ }
282
+
139
283
  // src/cst/host-mode.ts
140
284
  var FUSED_HOST_MODE = /* @__PURE__ */ Symbol.for("parseman.fusedHostMode");
141
285
  var FUSED_HOST_ELIDED = /* @__PURE__ */ Symbol.for("parseman.fusedHostElided");
@@ -155,6 +299,12 @@ function assertHostModeCompatible(mode, build, hostBranchElided = true) {
155
299
 
156
300
  // src/functional/run.ts
157
301
  var invoke = (r, input, pos, ctx) => typeof r === "function" ? r(input, pos, ctx) : r.parse(input, pos, ctx);
302
+ function triviaKindLabelsFromRunnable(r) {
303
+ if (!r) return void 0;
304
+ if (typeof r === "function") return r._meta?.triviaKindLabels;
305
+ const grammarTrivia = r._meta.grammarTrivia;
306
+ return r._meta.triviaKindLabels ?? grammarTrivia?._meta.triviaKindLabels ?? (r._def.tag === "grammar" ? r._def.triviaParser?._meta.triviaKindLabels : void 0);
307
+ }
158
308
  function runOnce(entry, input, options, phase, profileState) {
159
309
  if (typeof entry !== "function" && typeof entry?.parse !== "function") {
160
310
  throw new TypeError(
@@ -167,6 +317,7 @@ function runOnce(entry, input, options, phase, profileState) {
167
317
  const skipGlobalSinks = phase === "recognizer" || phase === "capture";
168
318
  const grammarTrivia = typeof entry !== "function" ? entry._meta.grammarTrivia : void 0;
169
319
  const grammarScanSkip = typeof entry !== "function" ? entry._meta.grammarScanSkip : void 0;
320
+ const triviaKindLabels = triviaKindLabelsFromRunnable(entry) ?? triviaKindLabelsFromRunnable(options.trivia);
170
321
  const stamped = entry;
171
322
  assertHostModeCompatible(
172
323
  typeof entry === "function" ? stamped[FUSED_HOST_MODE] ?? "ast" : entry._meta.grammarHostMode ?? "ast",
@@ -178,7 +329,7 @@ function runOnce(entry, input, options, phase, profileState) {
178
329
  ...skipGlobalSinks ? { _pmProfile: profile } : { _triviaLog: triviaLog, _errors: errors, ...profile === void 0 ? {} : { _pmProfile: profile } },
179
330
  build: options.build,
180
331
  state: options.state,
181
- ...grammarTrivia !== void 0 ? { trivia: grammarTrivia, ...grammarTrivia._meta.triviaKindLabels ? { triviaKindLabels: grammarTrivia._meta.triviaKindLabels } : {} } : {},
332
+ ...grammarTrivia !== void 0 ? { trivia: grammarTrivia, ...triviaKindLabels ? { triviaKindLabels } : {} } : triviaKindLabels ? { triviaKindLabels } : {},
182
333
  ...grammarScanSkip !== void 0 ? { scanSkip: grammarScanSkip } : {},
183
334
  ...options.triviaCaptureMask !== void 0 ? { _triviaCaptureMask: options.triviaCaptureMask } : {},
184
335
  ...options.tolerant ? { _tolerant: true, _rec: REC } : {},
@@ -201,6 +352,8 @@ function runOnce(entry, input, options, phase, profileState) {
201
352
  expected: r.ok ? [] : r.expected ?? [],
202
353
  errors,
203
354
  triviaLog,
355
+ ...triviaKindLabels === void 0 ? {} : { triviaKindLabels },
356
+ triviaMap: buildRootTriviaIndex(triviaLog, triviaKindLabels),
204
357
  unconsumedFrom
205
358
  };
206
359
  }
@@ -264,7 +417,7 @@ var META = new Set("()[]{}*+?|^$.".split(""));
264
417
  var CAP_RECORD = [
265
418
  ` if (_cap && _e > _pos) {`,
266
419
  ` if (_ctx._triviaLog !== undefined) _ctx._triviaLog.push(_pos, _e)`,
267
- ` if (_ctx._cstTriviaLog !== undefined && _ctx.captureTrivia) _ctx._cstTriviaLog.push(_pos, _e, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0)`,
420
+ ` if (_cap === 1 && _ctx._cstTriviaLog !== undefined && _ctx.captureTrivia) _ctx._cstTriviaLog.push(_pos, _e, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0)`,
268
421
  ` }`
269
422
  ].join("\n");
270
423