parseman 0.50.6 → 0.50.7

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.
@@ -31,7 +31,7 @@ function intersects(a, b) {
31
31
  return false;
32
32
  }
33
33
  function fromChar(code) {
34
- return { kind: "ranges", ranges: [{ lo: code, hi: code }] };
34
+ return fromRange(code, code);
35
35
  }
36
36
  function startsFirstSet(combinator, input, pos) {
37
37
  const fs = combinator._meta.firstSet;
@@ -124,11 +124,10 @@ function matchesEmptyBody(p, seen, resolve) {
124
124
  }
125
125
  }
126
126
  function isZeroWidthAssertion(p) {
127
- const tag = p._def.tag;
128
- return tag === "not" || tag === "adjacency";
127
+ return p._tag === "not" || p._tag === "adjacency";
129
128
  }
130
129
  function isPositiveLookahead(p) {
131
- return p._def.tag === "peek";
130
+ return p._tag === "peek";
132
131
  }
133
132
  function narrowBy(acc, constraint) {
134
133
  if (constraint.kind === "any") return acc;
@@ -144,34 +143,42 @@ function narrowBy(acc, constraint) {
144
143
  return ranges.length === 0 ? { kind: "empty" } : { kind: "ranges", ranges };
145
144
  }
146
145
  function applyAssertion(fs, assertion) {
147
- if (assertion === null) return fs;
148
- if (fs.kind === "empty") return assertion;
149
- return narrowBy(fs, assertion) ?? fs;
146
+ return assertion === null ? fs : fs.kind === "empty" ? assertion : narrowBy(fs, assertion);
150
147
  }
151
- function sequenceFirstSet(parsers) {
152
- let fs = empty();
153
- let assertion = null;
154
- for (const p of parsers) {
148
+ function sequenceSet(parsers, fs, empties, trivia2) {
149
+ const afterBoundary = (i) => {
150
+ const rest = at(i);
151
+ return trivia2 ? union(rest, fs(trivia2)) : rest;
152
+ };
153
+ const at = (i) => {
154
+ const p = parsers[i];
155
155
  if (isPositiveLookahead(p)) {
156
- assertion = narrowBy(assertion, p._meta.firstSet);
157
- continue;
156
+ const constraint = fs(p);
157
+ return i + 1 === parsers.length ? constraint : applyAssertion(afterBoundary(i + 1), constraint);
158
158
  }
159
- if (!isZeroWidthAssertion(p)) fs = union(fs, p._meta.firstSet);
160
- if (!matchesEmpty(p)) return applyAssertion(fs, assertion);
161
- }
162
- return applyAssertion(fs, assertion);
159
+ if (isZeroWidthAssertion(p)) {
160
+ return i + 1 === parsers.length ? empty() : afterBoundary(i + 1);
161
+ }
162
+ const first = fs(p);
163
+ if (!empties(p) || i + 1 === parsers.length) return first;
164
+ return union(first, afterBoundary(i + 1));
165
+ };
166
+ return at(0);
163
167
  }
164
- function firstSetOf(p, seen = /* @__PURE__ */ new Set(), resolve) {
168
+ function sequenceFirstSet(parsers) {
169
+ return sequenceSet(parsers, (p) => p._meta.firstSet, matchesEmpty);
170
+ }
171
+ function firstSetOf(p, seen = /* @__PURE__ */ new Set(), resolve, activeTrivia) {
165
172
  if (seen.has(p)) return any();
166
173
  seen.add(p);
167
174
  try {
168
- return firstSetBody(p, seen, resolve);
175
+ return firstSetBody(p, seen, resolve, activeTrivia);
169
176
  } finally {
170
177
  seen.delete(p);
171
178
  }
172
179
  }
173
- function firstSetBody(p, seen, resolve) {
174
- const fs = (c) => firstSetOf(c, seen, resolve);
180
+ function firstSetBody(p, seen, resolve, activeTrivia) {
181
+ const fs = (c) => firstSetOf(c, seen, resolve, activeTrivia);
175
182
  const empties = (c) => matchesEmpty(c, /* @__PURE__ */ new Set(), resolve);
176
183
  const d = p._def;
177
184
  switch (d.tag) {
@@ -197,17 +204,7 @@ function firstSetBody(p, seen, resolve) {
197
204
  return out;
198
205
  }
199
206
  case "sequence": {
200
- let out = empty();
201
- let assertion = null;
202
- for (const term of d.parsers) {
203
- if (isPositiveLookahead(term)) {
204
- assertion = narrowBy(assertion, fs(term));
205
- continue;
206
- }
207
- if (!isZeroWidthAssertion(term)) out = union(out, fs(term));
208
- if (!empties(term)) return applyAssertion(out, assertion);
209
- }
210
- return applyAssertion(out, assertion);
207
+ return sequenceSet(d.parsers, fs, empties, activeTrivia);
211
208
  }
212
209
  case "peek": {
213
210
  const inner = d.parser;
@@ -222,12 +219,16 @@ function firstSetBody(p, seen, resolve) {
222
219
  case "token":
223
220
  case "leaf":
224
221
  case "node":
225
- case "grammar":
226
- case "expect":
227
- return fs(d.parser);
228
222
  case "sepBy":
223
+ case "expect":
229
224
  return fs(d.parser);
230
- // both min 0 and min 1 start with the item
225
+ case "grammar":
226
+ return firstSetOf(
227
+ d.parser,
228
+ seen,
229
+ resolve,
230
+ d.clearTrivia ? null : d.triviaParser ?? activeTrivia
231
+ );
231
232
  default:
232
233
  return p._meta.firstSet;
233
234
  }