parseman 0.50.3 → 0.50.4

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.
@@ -35,10 +35,20 @@ function classifyFinalChoice(arms, resolve) {
35
35
  }
36
36
  return { exclusive, firstSets, nullable: nullable2 };
37
37
  }
38
- function resolveNamedRef(p, resolve) {
38
+ function winnerForRef(p, resolve) {
39
39
  if (resolve === void 0) return void 0;
40
40
  const name = p._ruleName;
41
- return name === void 0 ? void 0 : resolve(name);
41
+ const w = name === void 0 ? void 0 : resolve(name);
42
+ if (w === void 0 || w === p) return void 0;
43
+ let cur = w;
44
+ for (const seen = /* @__PURE__ */ new Set(); !seen.has(cur); ) {
45
+ if (cur === p) return void 0;
46
+ seen.add(cur);
47
+ const cd = cur._def;
48
+ if (cd.tag !== "grammar" && cd.tag !== "trivia") return w;
49
+ cur = cd.parser;
50
+ }
51
+ return w;
42
52
  }
43
53
  function union(a, b) {
44
54
  if (a.kind === "any" || b.kind === "any") return { kind: "any" };
@@ -136,13 +146,15 @@ function matchesEmptyBody(p, seen, resolve) {
136
146
  case "grammar":
137
147
  case "recover":
138
148
  return me(d.parser);
139
- case "lazy":
149
+ case "lazy": {
150
+ const w = winnerForRef(p, resolve);
151
+ if (w !== void 0) return me(w);
140
152
  try {
141
153
  return me(d.thunk());
142
154
  } catch {
143
- const t2 = resolveNamedRef(p, resolve);
144
- return t2 ? me(t2) : true;
155
+ return true;
145
156
  }
157
+ }
146
158
  default:
147
159
  return true;
148
160
  }
@@ -206,13 +218,15 @@ function firstSetBody(p, seen, resolve) {
206
218
  // terminals: no refs, cached set is exact
207
219
  case "dispatch":
208
220
  return fs(d.selector);
209
- case "lazy":
221
+ case "lazy": {
222
+ const w = winnerForRef(p, resolve);
223
+ if (w !== void 0) return fs(w);
210
224
  try {
211
225
  return fs(d.thunk());
212
226
  } catch {
213
- const t2 = resolveNamedRef(p, resolve);
214
- return t2 ? fs(t2) : any();
227
+ return any();
215
228
  }
229
+ }
216
230
  case "choice": {
217
231
  let out = empty();
218
232
  for (const arm of d.parsers) out = union(out, fs(arm));
@@ -7042,7 +7056,7 @@ var Encoder = class {
7042
7056
  d.tags === void 0 ? -1 : this.constant(d.tags)
7043
7057
  );
7044
7058
  if (matchesEmpty(d.parser)) return body;
7045
- const fs = d.parser._meta.firstSet;
7059
+ const fs = d.parser._def.tag === "lazy" ? firstSetOf(d.parser, /* @__PURE__ */ new Set(), this.refResolver()) : d.parser._meta.firstSet;
7046
7060
  if (fs.kind === "any") return body;
7047
7061
  const cls = this.charClass(fs);
7048
7062
  if (cls < 0) return body;
@@ -7156,7 +7170,7 @@ var Encoder = class {
7156
7170
  case "attempt": {
7157
7171
  const inner = this.emit(OP_ATTEMPT, this.node(d.parser).ip);
7158
7172
  if (!this.failureNeedsRollback(d.parser, void 0)) this.failureRollbackCleanSites.add(inner);
7159
- const fs = d.parser._meta.firstSet;
7173
+ const fs = d.parser._def.tag === "lazy" ? firstSetOf(d.parser, /* @__PURE__ */ new Set(), this.refResolver()) : d.parser._meta.firstSet;
7160
7174
  if (fs.kind === "any" || matchesEmpty(d.parser)) return inner;
7161
7175
  const cls = this.charClass(fs);
7162
7176
  if (cls < 0) return inner;