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.
@@ -1,8 +1,18 @@
1
1
  // src/combinators/first-set.ts
2
- function resolveNamedRef(p, resolve) {
2
+ function winnerForRef(p, resolve) {
3
3
  if (resolve === void 0) return void 0;
4
4
  const name = p._ruleName;
5
- return name === void 0 ? void 0 : resolve(name);
5
+ const w = name === void 0 ? void 0 : resolve(name);
6
+ if (w === void 0 || w === p) return void 0;
7
+ let cur = w;
8
+ for (const seen = /* @__PURE__ */ new Set(); !seen.has(cur); ) {
9
+ if (cur === p) return void 0;
10
+ seen.add(cur);
11
+ const cd = cur._def;
12
+ if (cd.tag !== "grammar" && cd.tag !== "trivia") return w;
13
+ cur = cd.parser;
14
+ }
15
+ return w;
6
16
  }
7
17
  function union(a, b) {
8
18
  if (a.kind === "any" || b.kind === "any") return { kind: "any" };
@@ -100,13 +110,15 @@ function matchesEmptyBody(p, seen, resolve) {
100
110
  case "grammar":
101
111
  case "recover":
102
112
  return me(d.parser);
103
- case "lazy":
113
+ case "lazy": {
114
+ const w = winnerForRef(p, resolve);
115
+ if (w !== void 0) return me(w);
104
116
  try {
105
117
  return me(d.thunk());
106
118
  } catch {
107
- const t = resolveNamedRef(p, resolve);
108
- return t ? me(t) : true;
119
+ return true;
109
120
  }
121
+ }
110
122
  default:
111
123
  return true;
112
124
  }
@@ -170,13 +182,15 @@ function firstSetBody(p, seen, resolve) {
170
182
  // terminals: no refs, cached set is exact
171
183
  case "dispatch":
172
184
  return fs(d.selector);
173
- case "lazy":
185
+ case "lazy": {
186
+ const w = winnerForRef(p, resolve);
187
+ if (w !== void 0) return fs(w);
174
188
  try {
175
189
  return fs(d.thunk());
176
190
  } catch {
177
- const t = resolveNamedRef(p, resolve);
178
- return t ? fs(t) : any();
191
+ return any();
179
192
  }
193
+ }
180
194
  case "choice": {
181
195
  let out = empty();
182
196
  for (const arm of d.parsers) out = union(out, fs(arm));