parseman 0.26.3 → 0.27.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.
@@ -138,7 +138,7 @@ var REC = { scan: recoverScan, at: matchesAt, or: orSentinel, sentinel: firstSet
138
138
 
139
139
  // src/functional/run.ts
140
140
  var invoke = (r, input, pos, ctx) => typeof r === "function" ? r(input, pos, ctx) : r.parse(input, pos, ctx);
141
- function run(entry, input, options = {}) {
141
+ function runOnce(entry, input, options, phase, profileState) {
142
142
  if (typeof entry !== "function" && typeof entry?.parse !== "function") {
143
143
  throw new TypeError(
144
144
  `run(): start production is ${entry === null ? "null" : typeof entry}, not a rule \u2014 the requested grammar rule does not exist (check the rule name).`
@@ -146,11 +146,12 @@ function run(entry, input, options = {}) {
146
146
  }
147
147
  const triviaLog = [];
148
148
  const errors = [];
149
+ const profile = profileState ?? (phase === void 0 ? void 0 : { phase, nodes: 0, childSlots: 0, rawSlots: 0, triviaSlots: 0, fieldSlots: 0, hostCalls: 0 });
150
+ const skipGlobalSinks = phase === "recognizer" || phase === "capture";
149
151
  const grammarTrivia = typeof entry !== "function" ? entry._meta.grammarTrivia : void 0;
150
152
  const ctx = {
151
153
  trackLines: false,
152
- _triviaLog: triviaLog,
153
- _errors: errors,
154
+ ...skipGlobalSinks ? { _pmProfile: profile } : { _triviaLog: triviaLog, _errors: errors, ...profile === void 0 ? {} : { _pmProfile: profile } },
154
155
  build: options.build,
155
156
  state: options.state,
156
157
  ...grammarTrivia !== void 0 ? { trivia: grammarTrivia, ...grammarTrivia._meta.triviaKindLabels ? { triviaKindLabels: grammarTrivia._meta.triviaKindLabels } : {} } : {},
@@ -177,6 +178,38 @@ function run(entry, input, options = {}) {
177
178
  unconsumedFrom
178
179
  };
179
180
  }
181
+ function clonePassState(state) {
182
+ if (state === null || typeof state !== "object") return state;
183
+ return Array.isArray(state) ? [...state] : { ...state };
184
+ }
185
+ function profilePass(entry, input, options, phase) {
186
+ const state = { phase, nodes: 0, childSlots: 0, rawSlots: 0, triviaSlots: 0, fieldSlots: 0, hostCalls: 0 };
187
+ const passOptions = options.state === void 0 ? options : { ...options, state: clonePassState(options.state) };
188
+ const start = performance.now();
189
+ const result = runOnce(entry, input, passOptions, phase, state);
190
+ const { phase: _phase, ...counts } = state;
191
+ return { result, profile: { ms: performance.now() - start, ...counts } };
192
+ }
193
+ function run(entry, input, options = {}) {
194
+ if (!options.profile) return runOnce(entry, input, options);
195
+ if (typeof entry !== "function") {
196
+ throw new TypeError("run({ profile: true }) requires a compiled parser entry");
197
+ }
198
+ const recognizer = profilePass(entry, input, options, "recognizer");
199
+ const capture = profilePass(entry, input, options, "capture");
200
+ const host = profilePass(entry, input, options, "host");
201
+ if (recognizer.result.ok !== host.result.ok || capture.result.ok !== host.result.ok || recognizer.result.unconsumedFrom !== host.result.unconsumedFrom || capture.result.unconsumedFrom !== host.result.unconsumedFrom) {
202
+ throw new Error("run({ profile: true }) changed recognition; the grammar is not profile-safe");
203
+ }
204
+ return {
205
+ ...host.result,
206
+ profile: {
207
+ recognizer: recognizer.profile,
208
+ structuralCapture: capture.profile,
209
+ hostConstruction: host.profile
210
+ }
211
+ };
212
+ }
180
213
 
181
214
  // src/combinators/completions.ts
182
215
  function completionsAt(target, input, offset, options = {}) {