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.
@@ -110,6 +110,150 @@ function captureError(ctx, error) {
110
110
  }
111
111
  var REC = { scan: recoverScan, at: matchesAt, or: orSentinel, sentinel: firstSetSentinel, capture: captureError };
112
112
 
113
+ // src/cst/trivia-entries.ts
114
+ var EMPTY_INDICES = Object.freeze([]);
115
+ var EMPTY_GAPS = Object.freeze([]);
116
+ function entryOffset(i, stride) {
117
+ return i * stride;
118
+ }
119
+ function triviaEntries(log, labels, opts) {
120
+ const baseStride = opts?.nodeLog ? 3 : 2;
121
+ const stride = labels ? baseStride + 1 : baseStride;
122
+ const length = Math.floor(log.length / stride);
123
+ return {
124
+ length,
125
+ labels,
126
+ stride,
127
+ start(i) {
128
+ return log[entryOffset(i, stride)];
129
+ },
130
+ end(i) {
131
+ return log[entryOffset(i, stride) + 1];
132
+ },
133
+ insertIndex(i) {
134
+ return opts?.nodeLog ? log[entryOffset(i, stride) + 2] : void 0;
135
+ },
136
+ kindIndex(i) {
137
+ if (!labels) return void 0;
138
+ return log[entryOffset(i, stride) + baseStride];
139
+ },
140
+ kind(i) {
141
+ const ki = labels ? log[entryOffset(i, stride) + baseStride] : void 0;
142
+ return ki !== void 0 ? labels[ki] : void 0;
143
+ },
144
+ text(i, input) {
145
+ const s = log[entryOffset(i, stride)];
146
+ const e = log[entryOffset(i, stride) + 1];
147
+ return input.slice(s, e);
148
+ }
149
+ };
150
+ }
151
+ function appendEntryRange(map, key, first, last) {
152
+ let indices = map.get(key);
153
+ if (!indices) {
154
+ indices = [];
155
+ map.set(key, indices);
156
+ }
157
+ for (let i = first; i <= last; i++) indices.push(i);
158
+ }
159
+ function buildGap(entries, start, end, first, last) {
160
+ const entryIndices = [];
161
+ for (let i = first; i <= last; i++) entryIndices.push(i);
162
+ return {
163
+ start,
164
+ end,
165
+ entryIndices,
166
+ hasKind(kind) {
167
+ for (const i of entryIndices) {
168
+ if (entries.kind(i) === kind) return true;
169
+ }
170
+ return false;
171
+ },
172
+ text(input) {
173
+ return input.slice(start, end);
174
+ }
175
+ };
176
+ }
177
+ function buildRootMaps(entries) {
178
+ const before = /* @__PURE__ */ new Map();
179
+ const after = /* @__PURE__ */ new Map();
180
+ const beforeGaps = /* @__PURE__ */ new Map();
181
+ const afterGaps = /* @__PURE__ */ new Map();
182
+ const gaps = [];
183
+ if (entries.length === 0) return { before, after, beforeGaps, afterGaps, gaps };
184
+ const appendGap = (first2, last, start2, end2) => {
185
+ const gap = buildGap(entries, start2, end2, first2, last);
186
+ gaps.push(gap);
187
+ afterGaps.set(start2, gap);
188
+ beforeGaps.set(end2, gap);
189
+ appendEntryRange(after, start2, first2, last);
190
+ appendEntryRange(before, end2, first2, last);
191
+ };
192
+ let first = 0;
193
+ let start = entries.start(0);
194
+ let end = entries.end(0);
195
+ for (let i = 1; i < entries.length; i++) {
196
+ const nextStart = entries.start(i);
197
+ const nextEnd = entries.end(i);
198
+ if (nextStart === end) {
199
+ end = nextEnd;
200
+ continue;
201
+ }
202
+ appendGap(first, i - 1, start, end);
203
+ first = i;
204
+ start = nextStart;
205
+ end = nextEnd;
206
+ }
207
+ appendGap(first, entries.length - 1, start, end);
208
+ return { before, after, beforeGaps, afterGaps, gaps };
209
+ }
210
+ function buildRootTriviaIndex(log, labels) {
211
+ const entries = triviaEntries(log, labels);
212
+ let maps;
213
+ const getMaps = () => {
214
+ maps ??= buildRootMaps(entries);
215
+ return maps;
216
+ };
217
+ return {
218
+ entries,
219
+ labels,
220
+ get before() {
221
+ return getMaps().before;
222
+ },
223
+ get after() {
224
+ return getMaps().after;
225
+ },
226
+ entryIndicesBefore(offset) {
227
+ return getMaps().before.get(offset) ?? EMPTY_INDICES;
228
+ },
229
+ entryIndicesAfter(offset) {
230
+ return getMaps().after.get(offset) ?? EMPTY_INDICES;
231
+ },
232
+ gapBefore(offset) {
233
+ return getMaps().beforeGaps.get(offset);
234
+ },
235
+ gapAfter(offset) {
236
+ return getMaps().afterGaps.get(offset);
237
+ },
238
+ gaps() {
239
+ return getMaps().gaps;
240
+ },
241
+ gapsWithKind(kind) {
242
+ const kinds = typeof kind === "string" ? [kind] : kind;
243
+ const matches2 = [];
244
+ for (const gap of getMaps().gaps) {
245
+ for (const k of kinds) {
246
+ if (gap.hasKind(k)) {
247
+ matches2.push(gap);
248
+ break;
249
+ }
250
+ }
251
+ }
252
+ return matches2.length === 0 ? EMPTY_GAPS : matches2;
253
+ }
254
+ };
255
+ }
256
+
113
257
  // src/cst/host-mode.ts
114
258
  var FUSED_HOST_MODE = /* @__PURE__ */ Symbol.for("parseman.fusedHostMode");
115
259
  var FUSED_HOST_ELIDED = /* @__PURE__ */ Symbol.for("parseman.fusedHostElided");
@@ -129,6 +273,12 @@ function assertHostModeCompatible(mode, build, hostBranchElided = true) {
129
273
 
130
274
  // src/functional/run.ts
131
275
  var invoke = (r, input, pos, ctx) => typeof r === "function" ? r(input, pos, ctx) : r.parse(input, pos, ctx);
276
+ function triviaKindLabelsFromRunnable(r) {
277
+ if (!r) return void 0;
278
+ if (typeof r === "function") return r._meta?.triviaKindLabels;
279
+ const grammarTrivia = r._meta.grammarTrivia;
280
+ return r._meta.triviaKindLabels ?? grammarTrivia?._meta.triviaKindLabels ?? (r._def.tag === "grammar" ? r._def.triviaParser?._meta.triviaKindLabels : void 0);
281
+ }
132
282
  function runOnce(entry, input, options, phase, profileState) {
133
283
  if (typeof entry !== "function" && typeof entry?.parse !== "function") {
134
284
  throw new TypeError(
@@ -141,6 +291,7 @@ function runOnce(entry, input, options, phase, profileState) {
141
291
  const skipGlobalSinks = phase === "recognizer" || phase === "capture";
142
292
  const grammarTrivia = typeof entry !== "function" ? entry._meta.grammarTrivia : void 0;
143
293
  const grammarScanSkip = typeof entry !== "function" ? entry._meta.grammarScanSkip : void 0;
294
+ const triviaKindLabels = triviaKindLabelsFromRunnable(entry) ?? triviaKindLabelsFromRunnable(options.trivia);
144
295
  const stamped = entry;
145
296
  assertHostModeCompatible(
146
297
  typeof entry === "function" ? stamped[FUSED_HOST_MODE] ?? "ast" : entry._meta.grammarHostMode ?? "ast",
@@ -152,7 +303,7 @@ function runOnce(entry, input, options, phase, profileState) {
152
303
  ...skipGlobalSinks ? { _pmProfile: profile } : { _triviaLog: triviaLog, _errors: errors, ...profile === void 0 ? {} : { _pmProfile: profile } },
153
304
  build: options.build,
154
305
  state: options.state,
155
- ...grammarTrivia !== void 0 ? { trivia: grammarTrivia, ...grammarTrivia._meta.triviaKindLabels ? { triviaKindLabels: grammarTrivia._meta.triviaKindLabels } : {} } : {},
306
+ ...grammarTrivia !== void 0 ? { trivia: grammarTrivia, ...triviaKindLabels ? { triviaKindLabels } : {} } : triviaKindLabels ? { triviaKindLabels } : {},
156
307
  ...grammarScanSkip !== void 0 ? { scanSkip: grammarScanSkip } : {},
157
308
  ...options.triviaCaptureMask !== void 0 ? { _triviaCaptureMask: options.triviaCaptureMask } : {},
158
309
  ...options.tolerant ? { _tolerant: true, _rec: REC } : {},
@@ -175,6 +326,8 @@ function runOnce(entry, input, options, phase, profileState) {
175
326
  expected: r.ok ? [] : r.expected ?? [],
176
327
  errors,
177
328
  triviaLog,
329
+ ...triviaKindLabels === void 0 ? {} : { triviaKindLabels },
330
+ triviaMap: buildRootTriviaIndex(triviaLog, triviaKindLabels),
178
331
  unconsumedFrom
179
332
  };
180
333
  }
@@ -238,7 +391,7 @@ var META = new Set("()[]{}*+?|^$.".split(""));
238
391
  var CAP_RECORD = [
239
392
  ` if (_cap && _e > _pos) {`,
240
393
  ` if (_ctx._triviaLog !== undefined) _ctx._triviaLog.push(_pos, _e)`,
241
- ` if (_ctx._cstTriviaLog !== undefined && _ctx.captureTrivia) _ctx._cstTriviaLog.push(_pos, _e, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0)`,
394
+ ` if (_cap === 1 && _ctx._cstTriviaLog !== undefined && _ctx.captureTrivia) _ctx._cstTriviaLog.push(_pos, _e, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0)`,
242
395
  ` }`
243
396
  ].join("\n");
244
397