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.
@@ -112,7 +112,7 @@ var REC = { scan: recoverScan, at: matchesAt, or: orSentinel, sentinel: firstSet
112
112
 
113
113
  // src/functional/run.ts
114
114
  var invoke = (r, input, pos, ctx) => typeof r === "function" ? r(input, pos, ctx) : r.parse(input, pos, ctx);
115
- function run(entry, input, options = {}) {
115
+ function runOnce(entry, input, options, phase, profileState) {
116
116
  if (typeof entry !== "function" && typeof entry?.parse !== "function") {
117
117
  throw new TypeError(
118
118
  `run(): start production is ${entry === null ? "null" : typeof entry}, not a rule \u2014 the requested grammar rule does not exist (check the rule name).`
@@ -120,11 +120,12 @@ function run(entry, input, options = {}) {
120
120
  }
121
121
  const triviaLog = [];
122
122
  const errors = [];
123
+ const profile = profileState ?? (phase === void 0 ? void 0 : { phase, nodes: 0, childSlots: 0, rawSlots: 0, triviaSlots: 0, fieldSlots: 0, hostCalls: 0 });
124
+ const skipGlobalSinks = phase === "recognizer" || phase === "capture";
123
125
  const grammarTrivia = typeof entry !== "function" ? entry._meta.grammarTrivia : void 0;
124
126
  const ctx = {
125
127
  trackLines: false,
126
- _triviaLog: triviaLog,
127
- _errors: errors,
128
+ ...skipGlobalSinks ? { _pmProfile: profile } : { _triviaLog: triviaLog, _errors: errors, ...profile === void 0 ? {} : { _pmProfile: profile } },
128
129
  build: options.build,
129
130
  state: options.state,
130
131
  ...grammarTrivia !== void 0 ? { trivia: grammarTrivia, ...grammarTrivia._meta.triviaKindLabels ? { triviaKindLabels: grammarTrivia._meta.triviaKindLabels } : {} } : {},
@@ -151,6 +152,38 @@ function run(entry, input, options = {}) {
151
152
  unconsumedFrom
152
153
  };
153
154
  }
155
+ function clonePassState(state) {
156
+ if (state === null || typeof state !== "object") return state;
157
+ return Array.isArray(state) ? [...state] : { ...state };
158
+ }
159
+ function profilePass(entry, input, options, phase) {
160
+ const state = { phase, nodes: 0, childSlots: 0, rawSlots: 0, triviaSlots: 0, fieldSlots: 0, hostCalls: 0 };
161
+ const passOptions = options.state === void 0 ? options : { ...options, state: clonePassState(options.state) };
162
+ const start = performance.now();
163
+ const result = runOnce(entry, input, passOptions, phase, state);
164
+ const { phase: _phase, ...counts } = state;
165
+ return { result, profile: { ms: performance.now() - start, ...counts } };
166
+ }
167
+ function run(entry, input, options = {}) {
168
+ if (!options.profile) return runOnce(entry, input, options);
169
+ if (typeof entry !== "function") {
170
+ throw new TypeError("run({ profile: true }) requires a compiled parser entry");
171
+ }
172
+ const recognizer = profilePass(entry, input, options, "recognizer");
173
+ const capture = profilePass(entry, input, options, "capture");
174
+ const host = profilePass(entry, input, options, "host");
175
+ 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) {
176
+ throw new Error("run({ profile: true }) changed recognition; the grammar is not profile-safe");
177
+ }
178
+ return {
179
+ ...host.result,
180
+ profile: {
181
+ recognizer: recognizer.profile,
182
+ structuralCapture: capture.profile,
183
+ hostConstruction: host.profile
184
+ }
185
+ };
186
+ }
154
187
 
155
188
  // src/combinators/completions.ts
156
189
  function completionsAt(target, input, offset, options = {}) {