parseman 0.42.0 → 0.42.1

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.
@@ -391,92 +391,6 @@ function markUnusedValues(root) {
391
391
  visit(resolveRoot(root), true);
392
392
  }
393
393
 
394
- // src/combinators/parser.ts
395
- var RULE_ORDER = "__parsemanRuleOrder";
396
- function tagRule(r, key) {
397
- ;
398
- r._ruleName = key;
399
- if (r._def.tag === "node" && r._def.type === void 0) r._def.type = key;
400
- }
401
- function isDefinableRef(v) {
402
- return !!v && typeof v === "object" && "_def" in v && v._def.tag === "lazy" && typeof v.define === "function";
403
- }
404
- function ruleNameOf(r) {
405
- return r._ruleName;
406
- }
407
- function isNamedRuleRefForAnotherRule(r, key) {
408
- const name = r._def.tag === "lazy" ? ruleNameOf(r) : void 0;
409
- return name !== void 0 && name !== key;
410
- }
411
- function rules(a, b) {
412
- const factory = typeof a === "function" ? a : b;
413
- const options = typeof a === "function" ? b : a;
414
- const cache = {};
415
- const proxy = new Proxy(cache, {
416
- get(target, key) {
417
- if (typeof key !== "string") return void 0;
418
- const record = target;
419
- if (!(key in record)) {
420
- const r = ref();
421
- tagRule(r, key);
422
- record[key] = r;
423
- }
424
- return record[key];
425
- }
426
- });
427
- const definitions = factory(proxy);
428
- for (const key of Object.keys(definitions)) {
429
- const placeholder = cache[key];
430
- const parser2 = definitions[key];
431
- if (placeholder === parser2) {
432
- throw new Error(`rules(): rule "${key}" cannot be a direct alias to itself`);
433
- }
434
- if (isDefinableRef(placeholder)) {
435
- if (!isNamedRuleRefForAnotherRule(parser2, key)) tagRule(parser2, key);
436
- placeholder.define(parser2);
437
- placeholder._meta.firstSet = parser2._meta.firstSet;
438
- placeholder._meta.canMatchNewline = parser2._meta.canMatchNewline;
439
- } else if (isNamedRuleRefForAnotherRule(parser2, key)) {
440
- const alias = ref();
441
- tagRule(alias, key);
442
- alias.define(parser2);
443
- alias._meta.firstSet = parser2._meta.firstSet;
444
- alias._meta.canMatchNewline = parser2._meta.canMatchNewline;
445
- cache[key] = alias;
446
- } else {
447
- tagRule(parser2, key);
448
- cache[key] = parser2;
449
- }
450
- }
451
- if (options?.trivia != null) {
452
- for (const key of Object.keys(definitions)) {
453
- const rule = cache[key];
454
- if (rule && !rule._meta.isTrivia) rule._meta.grammarTrivia = options.trivia;
455
- }
456
- }
457
- if (options?.scanSkip != null) {
458
- for (const key of Object.keys(definitions)) {
459
- const rule = cache[key];
460
- if (rule && !rule._meta.isTrivia) rule._meta.grammarScanSkip = options.scanSkip;
461
- }
462
- }
463
- if (options?.hostMode === "cst") {
464
- for (const key of Object.keys(definitions)) {
465
- const rule = cache[key];
466
- if (rule && !rule._meta.isTrivia) rule._meta.grammarHostMode = "cst";
467
- }
468
- }
469
- for (const key of Object.keys(definitions)) {
470
- markUnusedValues(cache[key]);
471
- }
472
- Object.defineProperty(cache, RULE_ORDER, {
473
- value: Object.keys(definitions),
474
- enumerable: false,
475
- configurable: true
476
- });
477
- return cache;
478
- }
479
-
480
394
  // src/line-index.ts
481
395
  function createLineIndex() {
482
396
  return { lineStarts: [0] };
@@ -1144,38 +1058,333 @@ function codesToFirstSet(codes) {
1144
1058
  if (codes[i] === hi + 1) {
1145
1059
  hi = codes[i];
1146
1060
  } else {
1147
- ranges.push({ lo, hi });
1148
- lo = hi = codes[i];
1061
+ ranges.push({ lo, hi });
1062
+ lo = hi = codes[i];
1063
+ }
1064
+ }
1065
+ ranges.push({ lo, hi });
1066
+ return { kind: "ranges", ranges };
1067
+ }
1068
+ function inFirstSet(code, fs) {
1069
+ if (fs.kind === "any") return true;
1070
+ if (fs.kind === "empty") return false;
1071
+ for (const r of fs.ranges) if (code >= r.lo && code <= r.hi) return true;
1072
+ return false;
1073
+ }
1074
+ function areDisjoint(sets) {
1075
+ if (sets.some((s) => s.kind === "any")) return false;
1076
+ for (let i = 0; i < sets.length; i++)
1077
+ for (let j2 = i + 1; j2 < sets.length; j2++)
1078
+ if (intersects(sets[i], sets[j2])) return false;
1079
+ return true;
1080
+ }
1081
+ function buildAsciiDispatch(parsers) {
1082
+ const table = Array(128).fill(-1);
1083
+ for (let i = 0; i < parsers.length; i++) {
1084
+ const fs = parsers[i]._meta.firstSet;
1085
+ if (fs.kind !== "ranges") continue;
1086
+ for (const { lo, hi } of fs.ranges) {
1087
+ for (let code = Math.max(0, lo); code <= Math.min(127, hi); code++) {
1088
+ table[code] = i;
1089
+ }
1090
+ }
1091
+ }
1092
+ return table;
1093
+ }
1094
+
1095
+ // src/cst/trivia-kinds.ts
1096
+ function unwrapTrivia(p) {
1097
+ let cur = p;
1098
+ while (cur._def.tag === "trivia") cur = cur._def.parser;
1099
+ return cur;
1100
+ }
1101
+ function peelLabel(p) {
1102
+ if (p._def.tag === "label") return { label: p._def.label, parser: p._def.parser };
1103
+ return null;
1104
+ }
1105
+ function analyzeLabeledTrivia(trivia2) {
1106
+ let core = unwrapTrivia(trivia2);
1107
+ let minRepeats = 1;
1108
+ if (core._def.tag === "oneOrMore") {
1109
+ core = core._def.parser;
1110
+ minRepeats = 1;
1111
+ } else if (core._def.tag === "many") {
1112
+ core = core._def.parser;
1113
+ minRepeats = 0;
1114
+ }
1115
+ const arms = [];
1116
+ if (core._def.tag === "choice") {
1117
+ for (let i = 0; i < core._def.parsers.length; i++) {
1118
+ const peeled = peelLabel(core._def.parsers[i]);
1119
+ if (!peeled) return null;
1120
+ arms.push({ label: peeled.label, kindIndex: i, parser: peeled.parser });
1121
+ }
1122
+ } else {
1123
+ const peeled = peelLabel(core);
1124
+ if (!peeled) return null;
1125
+ arms.push({ label: peeled.label, kindIndex: 0, parser: peeled.parser });
1126
+ }
1127
+ return { labels: arms.map((a) => a.label), arms, minRepeats };
1128
+ }
1129
+ function matchArmAt(input, pos, arm) {
1130
+ const r = arm.parse(input, pos, { trackLines: false });
1131
+ if (!r.ok || r.span.end <= pos) return null;
1132
+ return { end: r.span.end };
1133
+ }
1134
+ function scanLabeledTriviaChunks(input, cur, spec) {
1135
+ const chunks = [];
1136
+ let pos = cur;
1137
+ while (pos < input.length) {
1138
+ let matched = null;
1139
+ for (const arm of spec.arms) {
1140
+ const m = matchArmAt(input, pos, arm.parser);
1141
+ if (m) {
1142
+ matched = { end: m.end, kindIndex: arm.kindIndex };
1143
+ break;
1144
+ }
1145
+ }
1146
+ if (!matched) break;
1147
+ chunks.push({ start: pos, end: matched.end, kindIndex: matched.kindIndex });
1148
+ pos = matched.end;
1149
+ }
1150
+ if (chunks.length < spec.minRepeats) {
1151
+ return { end: cur, chunks: [] };
1152
+ }
1153
+ return { end: pos, chunks };
1154
+ }
1155
+ function scanFastWsCommentsChunks(input, cur, wsKind, commentKind) {
1156
+ const chunks = [];
1157
+ let pos = cur;
1158
+ while (pos < input.length) {
1159
+ const c = input.charCodeAt(pos);
1160
+ if (c === 32 || c === 9 || c === 10 || c === 13 || c === 12) {
1161
+ const start = pos;
1162
+ pos++;
1163
+ while (pos < input.length) {
1164
+ const c2 = input.charCodeAt(pos);
1165
+ if (c2 === 32 || c2 === 9 || c2 === 10 || c2 === 13 || c2 === 12) pos++;
1166
+ else break;
1167
+ }
1168
+ chunks.push({ start, end: pos, kindIndex: wsKind });
1169
+ continue;
1170
+ }
1171
+ if (c === 47 && input.charCodeAt(pos + 1) === 42) {
1172
+ let j2 = pos + 2;
1173
+ while (j2 + 1 < input.length && !(input.charCodeAt(j2) === 42 && input.charCodeAt(j2 + 1) === 47)) j2++;
1174
+ if (j2 + 1 < input.length && input.charCodeAt(j2) === 42 && input.charCodeAt(j2 + 1) === 47) {
1175
+ const start = pos;
1176
+ pos = j2 + 2;
1177
+ chunks.push({ start, end: pos, kindIndex: commentKind });
1178
+ continue;
1179
+ }
1180
+ break;
1181
+ }
1182
+ break;
1183
+ }
1184
+ return { end: pos, chunks };
1185
+ }
1186
+ function tryFastLabeledScan(input, cur, trivia2) {
1187
+ const spec = analyzeLabeledTrivia(trivia2);
1188
+ if (!spec || spec.arms.length !== 2) return null;
1189
+ const wsArm = spec.arms.find((a) => {
1190
+ const src = getCoreRegexDef(a.parser)?.source;
1191
+ return src != null && !src.includes("\\*");
1192
+ });
1193
+ const commentArm = spec.arms.find((a) => {
1194
+ const src = getCoreRegexDef(a.parser)?.source;
1195
+ return src != null && src.includes("\\*");
1196
+ });
1197
+ if (!wsArm || !commentArm) return null;
1198
+ const { chunks } = scanFastWsCommentsChunks(input, cur, wsArm.kindIndex, commentArm.kindIndex);
1199
+ if (chunks.length < spec.minRepeats) return { end: cur, chunks: [] };
1200
+ return scanFastWsCommentsChunks(input, cur, wsArm.kindIndex, commentArm.kindIndex);
1201
+ }
1202
+ function recordTriviaChunks(ctx, chunks) {
1203
+ const kinds = ctx.triviaKindLabels;
1204
+ const mask = ctx._triviaCaptureMask;
1205
+ for (const ch of chunks) {
1206
+ pushTriviaLogEntry(ctx, ch.start, ch.end, kinds ? ch.kindIndex : void 0);
1207
+ if (ctx.captureTrivia && (ctx._cstBuf !== void 0 || ctx._cstTriviaLog !== void 0)) {
1208
+ if (mask === void 0 || kinds === void 0 || (mask & 1 << ch.kindIndex) !== 0) {
1209
+ pushCstTriviaEntry(ctx, ch.start, ch.end, kinds ? ch.kindIndex : void 0);
1210
+ }
1211
+ }
1212
+ }
1213
+ }
1214
+ function triviaKindMask(labels, keep) {
1215
+ if (!labels) return void 0;
1216
+ let mask = 0;
1217
+ for (const name of keep) {
1218
+ const idx = labels.indexOf(name);
1219
+ if (idx >= 0) mask |= 1 << idx;
1220
+ }
1221
+ return mask;
1222
+ }
1223
+
1224
+ // src/combinators/grammar.ts
1225
+ function annotateResultLines(result, index) {
1226
+ const annotated = { ...result, span: annotateSpan(result.span, index) };
1227
+ const errors = annotated.errors;
1228
+ if (errors) {
1229
+ ;
1230
+ annotated.errors = errors.map((error) => ({
1231
+ ...error,
1232
+ span: annotateSpan(error.span, index)
1233
+ }));
1234
+ }
1235
+ return annotated;
1236
+ }
1237
+ function createParseLineContext(input, pos) {
1238
+ const lineIndex = createLineIndex();
1239
+ if (pos > 0) recordLineRange(lineIndex, input, 0, pos);
1240
+ return { lineIndex, lineScannedTo: pos };
1241
+ }
1242
+ function parser(opts, root) {
1243
+ const clearTrivia = opts.trivia === null;
1244
+ return {
1245
+ _tag: "grammar",
1246
+ _meta: root._meta,
1247
+ _def: {
1248
+ tag: "grammar",
1249
+ parser: root,
1250
+ triviaParser: clearTrivia ? void 0 : opts.trivia ?? void 0,
1251
+ clearTrivia,
1252
+ ...opts.captureTrivia ? { captureTrivia: true } : {},
1253
+ trackLines: opts.trackLines ?? false
1254
+ },
1255
+ parse(input, pos, _ctx) {
1256
+ const trackLines = opts.trackLines ?? _ctx?.trackLines ?? false;
1257
+ const lineContext = trackLines && _ctx?._lineIndex === void 0 && _ctx?._lineStarts === void 0 ? createParseLineContext(input, pos ?? 0) : void 0;
1258
+ const lineIndex = trackLines ? _ctx?._lineIndex ?? (_ctx?._lineStarts ? { lineStarts: _ctx._lineStarts } : lineContext.lineIndex) : void 0;
1259
+ const ctx = {
1260
+ ..._ctx,
1261
+ trackLines,
1262
+ ...lineIndex ? { _lineIndex: lineIndex } : {},
1263
+ ...lineContext ? { _lineScannedTo: lineContext.lineScannedTo } : {},
1264
+ // trivia: null clears (contiguous terms); a Combinator sets; undefined inherits.
1265
+ ...clearTrivia ? {
1266
+ trivia: void 0,
1267
+ triviaKindLabels: void 0
1268
+ } : opts.trivia != null ? {
1269
+ trivia: opts.trivia,
1270
+ ...opts.trivia._meta.triviaKindLabels ? { triviaKindLabels: opts.trivia._meta.triviaKindLabels } : {}
1271
+ } : {},
1272
+ ...opts.captureTrivia || _ctx?.captureTrivia ? { captureTrivia: true } : {},
1273
+ // Kind-filter for per-node capture. Resolve against this scope's trivia
1274
+ // labels — this parser's own trivia if it declares one, else the INHERITED
1275
+ // labels (`_ctx.triviaKindLabels`), so captureTriviaKinds still applies when
1276
+ // trivia is inherited rather than re-declared here. No labels → undefined
1277
+ // (capture all).
1278
+ ...opts.captureTriviaKinds && !clearTrivia ? { _triviaCaptureMask: triviaKindMask(opts.trivia?._meta.triviaKindLabels ?? _ctx?.triviaKindLabels, opts.captureTriviaKinds) } : {}
1279
+ };
1280
+ const result = root.parse(input, pos ?? 0, ctx);
1281
+ if (trackLines && _ctx && ctx._lineScannedTo !== void 0) {
1282
+ _ctx._lineScannedTo = Math.max(_ctx._lineScannedTo ?? 0, ctx._lineScannedTo);
1283
+ }
1284
+ return lineIndex ? annotateResultLines(result, normalizeLineIndex(lineIndex)) : result;
1285
+ }
1286
+ };
1287
+ }
1288
+
1289
+ // src/combinators/parser.ts
1290
+ var RULE_ORDER = "__parsemanRuleOrder";
1291
+ function tagRule(r, key) {
1292
+ ;
1293
+ r._ruleName = key;
1294
+ if (r._def.tag === "node" && r._def.type === void 0) r._def.type = key;
1295
+ }
1296
+ function isDefinableRef(v) {
1297
+ return !!v && typeof v === "object" && "_def" in v && v._def.tag === "lazy" && typeof v.define === "function";
1298
+ }
1299
+ function ruleNameOf(r) {
1300
+ return r._ruleName;
1301
+ }
1302
+ function isNamedRuleRefForAnotherRule(r, key) {
1303
+ const name = r._def.tag === "lazy" ? ruleNameOf(r) : void 0;
1304
+ return name !== void 0 && name !== key;
1305
+ }
1306
+ function rules(a, b) {
1307
+ const factory = typeof a === "function" ? a : b;
1308
+ const options = typeof a === "function" ? b : a;
1309
+ const cache = {};
1310
+ const proxy = new Proxy(cache, {
1311
+ get(target, key) {
1312
+ if (typeof key !== "string") return void 0;
1313
+ const record = target;
1314
+ if (!(key in record)) {
1315
+ const r = ref();
1316
+ tagRule(r, key);
1317
+ record[key] = r;
1318
+ }
1319
+ return record[key];
1320
+ }
1321
+ });
1322
+ const definitions = factory(proxy);
1323
+ for (const key of Object.keys(definitions)) {
1324
+ const placeholder = cache[key];
1325
+ const parser2 = definitions[key];
1326
+ if (placeholder === parser2) {
1327
+ throw new Error(`rules(): rule "${key}" cannot be a direct alias to itself`);
1328
+ }
1329
+ if (isDefinableRef(placeholder)) {
1330
+ if (!isNamedRuleRefForAnotherRule(parser2, key)) tagRule(parser2, key);
1331
+ placeholder.define(parser2);
1332
+ placeholder._meta.firstSet = parser2._meta.firstSet;
1333
+ placeholder._meta.canMatchNewline = parser2._meta.canMatchNewline;
1334
+ } else if (isNamedRuleRefForAnotherRule(parser2, key)) {
1335
+ const alias = ref();
1336
+ tagRule(alias, key);
1337
+ alias.define(parser2);
1338
+ alias._meta.firstSet = parser2._meta.firstSet;
1339
+ alias._meta.canMatchNewline = parser2._meta.canMatchNewline;
1340
+ cache[key] = alias;
1341
+ } else {
1342
+ tagRule(parser2, key);
1343
+ cache[key] = parser2;
1149
1344
  }
1150
1345
  }
1151
- ranges.push({ lo, hi });
1152
- return { kind: "ranges", ranges };
1153
- }
1154
- function inFirstSet(code, fs) {
1155
- if (fs.kind === "any") return true;
1156
- if (fs.kind === "empty") return false;
1157
- for (const r of fs.ranges) if (code >= r.lo && code <= r.hi) return true;
1158
- return false;
1159
- }
1160
- function areDisjoint(sets) {
1161
- if (sets.some((s) => s.kind === "any")) return false;
1162
- for (let i = 0; i < sets.length; i++)
1163
- for (let j2 = i + 1; j2 < sets.length; j2++)
1164
- if (intersects(sets[i], sets[j2])) return false;
1165
- return true;
1166
- }
1167
- function buildAsciiDispatch(parsers) {
1168
- const table = Array(128).fill(-1);
1169
- for (let i = 0; i < parsers.length; i++) {
1170
- const fs = parsers[i]._meta.firstSet;
1171
- if (fs.kind !== "ranges") continue;
1172
- for (const { lo, hi } of fs.ranges) {
1173
- for (let code = Math.max(0, lo); code <= Math.min(127, hi); code++) {
1174
- table[code] = i;
1346
+ if (options?.trivia != null) {
1347
+ for (const key of Object.keys(definitions)) {
1348
+ const rule = cache[key];
1349
+ if (rule && !rule._meta.isTrivia) rule._meta.grammarTrivia = options.trivia;
1350
+ }
1351
+ }
1352
+ if (options?.scanSkip != null) {
1353
+ for (const key of Object.keys(definitions)) {
1354
+ const rule = cache[key];
1355
+ if (rule && !rule._meta.isTrivia) rule._meta.grammarScanSkip = options.scanSkip;
1356
+ }
1357
+ }
1358
+ if (options?.hostMode === "cst") {
1359
+ for (const key of Object.keys(definitions)) {
1360
+ const rule = cache[key];
1361
+ if (rule && !rule._meta.isTrivia) rule._meta.grammarHostMode = "cst";
1362
+ }
1363
+ }
1364
+ if (options?.trackLines === true) {
1365
+ for (const key of Object.keys(definitions)) {
1366
+ const rule = cache[key];
1367
+ if (rule && !rule._meta.isTrivia) rule._meta.grammarTrackLines = true;
1368
+ }
1369
+ for (const key of Object.keys(definitions)) {
1370
+ const rule = cache[key];
1371
+ if (rule && !rule._meta.isTrivia && rule._def.tag !== "grammar") {
1372
+ const wrapped = parser({ trackLines: true }, rule);
1373
+ tagRule(wrapped, key);
1374
+ wrapped._meta.grammarTrackLines = true;
1375
+ cache[key] = wrapped;
1175
1376
  }
1176
1377
  }
1177
1378
  }
1178
- return table;
1379
+ for (const key of Object.keys(definitions)) {
1380
+ markUnusedValues(cache[key]);
1381
+ }
1382
+ Object.defineProperty(cache, RULE_ORDER, {
1383
+ value: Object.keys(definitions),
1384
+ enumerable: false,
1385
+ configurable: true
1386
+ });
1387
+ return cache;
1179
1388
  }
1180
1389
 
1181
1390
  // src/analysis/duplication.ts
@@ -1430,135 +1639,6 @@ function regex(pattern, flags = "") {
1430
1639
  };
1431
1640
  }
1432
1641
 
1433
- // src/cst/trivia-kinds.ts
1434
- function unwrapTrivia(p) {
1435
- let cur = p;
1436
- while (cur._def.tag === "trivia") cur = cur._def.parser;
1437
- return cur;
1438
- }
1439
- function peelLabel(p) {
1440
- if (p._def.tag === "label") return { label: p._def.label, parser: p._def.parser };
1441
- return null;
1442
- }
1443
- function analyzeLabeledTrivia(trivia2) {
1444
- let core = unwrapTrivia(trivia2);
1445
- let minRepeats = 1;
1446
- if (core._def.tag === "oneOrMore") {
1447
- core = core._def.parser;
1448
- minRepeats = 1;
1449
- } else if (core._def.tag === "many") {
1450
- core = core._def.parser;
1451
- minRepeats = 0;
1452
- }
1453
- const arms = [];
1454
- if (core._def.tag === "choice") {
1455
- for (let i = 0; i < core._def.parsers.length; i++) {
1456
- const peeled = peelLabel(core._def.parsers[i]);
1457
- if (!peeled) return null;
1458
- arms.push({ label: peeled.label, kindIndex: i, parser: peeled.parser });
1459
- }
1460
- } else {
1461
- const peeled = peelLabel(core);
1462
- if (!peeled) return null;
1463
- arms.push({ label: peeled.label, kindIndex: 0, parser: peeled.parser });
1464
- }
1465
- return { labels: arms.map((a) => a.label), arms, minRepeats };
1466
- }
1467
- function matchArmAt(input, pos, arm) {
1468
- const r = arm.parse(input, pos, { trackLines: false });
1469
- if (!r.ok || r.span.end <= pos) return null;
1470
- return { end: r.span.end };
1471
- }
1472
- function scanLabeledTriviaChunks(input, cur, spec) {
1473
- const chunks = [];
1474
- let pos = cur;
1475
- while (pos < input.length) {
1476
- let matched = null;
1477
- for (const arm of spec.arms) {
1478
- const m = matchArmAt(input, pos, arm.parser);
1479
- if (m) {
1480
- matched = { end: m.end, kindIndex: arm.kindIndex };
1481
- break;
1482
- }
1483
- }
1484
- if (!matched) break;
1485
- chunks.push({ start: pos, end: matched.end, kindIndex: matched.kindIndex });
1486
- pos = matched.end;
1487
- }
1488
- if (chunks.length < spec.minRepeats) {
1489
- return { end: cur, chunks: [] };
1490
- }
1491
- return { end: pos, chunks };
1492
- }
1493
- function scanFastWsCommentsChunks(input, cur, wsKind, commentKind) {
1494
- const chunks = [];
1495
- let pos = cur;
1496
- while (pos < input.length) {
1497
- const c = input.charCodeAt(pos);
1498
- if (c === 32 || c === 9 || c === 10 || c === 13 || c === 12) {
1499
- const start = pos;
1500
- pos++;
1501
- while (pos < input.length) {
1502
- const c2 = input.charCodeAt(pos);
1503
- if (c2 === 32 || c2 === 9 || c2 === 10 || c2 === 13 || c2 === 12) pos++;
1504
- else break;
1505
- }
1506
- chunks.push({ start, end: pos, kindIndex: wsKind });
1507
- continue;
1508
- }
1509
- if (c === 47 && input.charCodeAt(pos + 1) === 42) {
1510
- let j2 = pos + 2;
1511
- while (j2 + 1 < input.length && !(input.charCodeAt(j2) === 42 && input.charCodeAt(j2 + 1) === 47)) j2++;
1512
- if (j2 + 1 < input.length && input.charCodeAt(j2) === 42 && input.charCodeAt(j2 + 1) === 47) {
1513
- const start = pos;
1514
- pos = j2 + 2;
1515
- chunks.push({ start, end: pos, kindIndex: commentKind });
1516
- continue;
1517
- }
1518
- break;
1519
- }
1520
- break;
1521
- }
1522
- return { end: pos, chunks };
1523
- }
1524
- function tryFastLabeledScan(input, cur, trivia2) {
1525
- const spec = analyzeLabeledTrivia(trivia2);
1526
- if (!spec || spec.arms.length !== 2) return null;
1527
- const wsArm = spec.arms.find((a) => {
1528
- const src = getCoreRegexDef(a.parser)?.source;
1529
- return src != null && !src.includes("\\*");
1530
- });
1531
- const commentArm = spec.arms.find((a) => {
1532
- const src = getCoreRegexDef(a.parser)?.source;
1533
- return src != null && src.includes("\\*");
1534
- });
1535
- if (!wsArm || !commentArm) return null;
1536
- const { chunks } = scanFastWsCommentsChunks(input, cur, wsArm.kindIndex, commentArm.kindIndex);
1537
- if (chunks.length < spec.minRepeats) return { end: cur, chunks: [] };
1538
- return scanFastWsCommentsChunks(input, cur, wsArm.kindIndex, commentArm.kindIndex);
1539
- }
1540
- function recordTriviaChunks(ctx, chunks) {
1541
- const kinds = ctx.triviaKindLabels;
1542
- const mask = ctx._triviaCaptureMask;
1543
- for (const ch of chunks) {
1544
- pushTriviaLogEntry(ctx, ch.start, ch.end, kinds ? ch.kindIndex : void 0);
1545
- if (ctx.captureTrivia && (ctx._cstBuf !== void 0 || ctx._cstTriviaLog !== void 0)) {
1546
- if (mask === void 0 || kinds === void 0 || (mask & 1 << ch.kindIndex) !== 0) {
1547
- pushCstTriviaEntry(ctx, ch.start, ch.end, kinds ? ch.kindIndex : void 0);
1548
- }
1549
- }
1550
- }
1551
- }
1552
- function triviaKindMask(labels, keep) {
1553
- if (!labels) return void 0;
1554
- let mask = 0;
1555
- for (const name of keep) {
1556
- const idx = labels.indexOf(name);
1557
- if (idx >= 0) mask |= 1 << idx;
1558
- }
1559
- return mask;
1560
- }
1561
-
1562
1642
  // src/combinators/trivia-skip.ts
1563
1643
  var NOOP_COMMIT = () => {
1564
1644
  };
@@ -2992,71 +3072,6 @@ function node(typeOrCombinator, combinatorOrBuild, buildOrOpts, maybeOpts) {
2992
3072
  };
2993
3073
  }
2994
3074
 
2995
- // src/combinators/grammar.ts
2996
- function annotateResultLines(result, index) {
2997
- const annotated = { ...result, span: annotateSpan(result.span, index) };
2998
- const errors = annotated.errors;
2999
- if (errors) {
3000
- ;
3001
- annotated.errors = errors.map((error) => ({
3002
- ...error,
3003
- span: annotateSpan(error.span, index)
3004
- }));
3005
- }
3006
- return annotated;
3007
- }
3008
- function createParseLineContext(input, pos) {
3009
- const lineIndex = createLineIndex();
3010
- if (pos > 0) recordLineRange(lineIndex, input, 0, pos);
3011
- return { lineIndex, lineScannedTo: pos };
3012
- }
3013
- function parser(opts, root) {
3014
- const clearTrivia = opts.trivia === null;
3015
- return {
3016
- _tag: "grammar",
3017
- _meta: root._meta,
3018
- _def: {
3019
- tag: "grammar",
3020
- parser: root,
3021
- triviaParser: clearTrivia ? void 0 : opts.trivia ?? void 0,
3022
- clearTrivia,
3023
- ...opts.captureTrivia ? { captureTrivia: true } : {},
3024
- trackLines: opts.trackLines ?? false
3025
- },
3026
- parse(input, pos, _ctx) {
3027
- const trackLines = opts.trackLines ?? _ctx?.trackLines ?? false;
3028
- const lineContext = trackLines && _ctx?._lineIndex === void 0 && _ctx?._lineStarts === void 0 ? createParseLineContext(input, pos ?? 0) : void 0;
3029
- const lineIndex = trackLines ? _ctx?._lineIndex ?? (_ctx?._lineStarts ? { lineStarts: _ctx._lineStarts } : lineContext.lineIndex) : void 0;
3030
- const ctx = {
3031
- ..._ctx,
3032
- trackLines,
3033
- ...lineIndex ? { _lineIndex: lineIndex } : {},
3034
- ...lineContext ? { _lineScannedTo: lineContext.lineScannedTo } : {},
3035
- // trivia: null clears (contiguous terms); a Combinator sets; undefined inherits.
3036
- ...clearTrivia ? {
3037
- trivia: void 0,
3038
- triviaKindLabels: void 0
3039
- } : opts.trivia != null ? {
3040
- trivia: opts.trivia,
3041
- ...opts.trivia._meta.triviaKindLabels ? { triviaKindLabels: opts.trivia._meta.triviaKindLabels } : {}
3042
- } : {},
3043
- ...opts.captureTrivia || _ctx?.captureTrivia ? { captureTrivia: true } : {},
3044
- // Kind-filter for per-node capture. Resolve against this scope's trivia
3045
- // labels — this parser's own trivia if it declares one, else the INHERITED
3046
- // labels (`_ctx.triviaKindLabels`), so captureTriviaKinds still applies when
3047
- // trivia is inherited rather than re-declared here. No labels → undefined
3048
- // (capture all).
3049
- ...opts.captureTriviaKinds && !clearTrivia ? { _triviaCaptureMask: triviaKindMask(opts.trivia?._meta.triviaKindLabels ?? _ctx?.triviaKindLabels, opts.captureTriviaKinds) } : {}
3050
- };
3051
- const result = root.parse(input, pos ?? 0, ctx);
3052
- if (trackLines && _ctx && ctx._lineScannedTo !== void 0) {
3053
- _ctx._lineScannedTo = Math.max(_ctx._lineScannedTo ?? 0, ctx._lineScannedTo);
3054
- }
3055
- return lineIndex ? annotateResultLines(result, normalizeLineIndex(lineIndex)) : result;
3056
- }
3057
- };
3058
- }
3059
-
3060
3075
  // src/combinators/token.ts
3061
3076
  function token(root) {
3062
3077
  const meta = {