@nibling/editor 0.0.0-stage → 0.0.1

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.
Files changed (146) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +53 -2
  3. package/dist/commands/autoclose.d.ts +13 -0
  4. package/dist/commands/autoclose.js +78 -0
  5. package/dist/commands/commands.d.ts +104 -0
  6. package/dist/commands/commands.js +442 -0
  7. package/dist/commands/comments.d.ts +3 -0
  8. package/dist/commands/comments.js +9 -0
  9. package/dist/commands/fold.d.ts +18 -0
  10. package/dist/commands/fold.js +75 -0
  11. package/dist/commands/keymap.d.ts +40 -0
  12. package/dist/commands/keymap.js +118 -0
  13. package/dist/complete/autocomplete.d.ts +35 -0
  14. package/dist/complete/autocomplete.js +386 -0
  15. package/dist/complete/source.d.ts +72 -0
  16. package/dist/complete/source.js +52 -0
  17. package/dist/complete/words.d.ts +53 -0
  18. package/dist/complete/words.js +203 -0
  19. package/dist/element.d.ts +119 -0
  20. package/dist/element.js +1011 -0
  21. package/dist/expand/expansions.d.ts +68 -0
  22. package/dist/expand/expansions.js +143 -0
  23. package/dist/expand/projection.d.ts +73 -0
  24. package/dist/expand/projection.js +189 -0
  25. package/dist/expand/resolver.d.ts +42 -0
  26. package/dist/expand/resolver.js +585 -0
  27. package/dist/expand/slice-tokens.d.ts +25 -0
  28. package/dist/expand/slice-tokens.js +79 -0
  29. package/dist/extension.d.ts +58 -0
  30. package/dist/extension.js +1 -0
  31. package/dist/find/find.d.ts +12 -0
  32. package/dist/find/find.js +469 -0
  33. package/dist/find/search.d.ts +81 -0
  34. package/dist/find/search.js +308 -0
  35. package/dist/headless.d.ts +16 -0
  36. package/dist/headless.js +19 -0
  37. package/dist/index.d.ts +6 -0
  38. package/dist/index.js +6 -0
  39. package/dist/inline-expansion.d.ts +89 -0
  40. package/dist/inline-expansion.js +324 -0
  41. package/dist/input/edit-context.d.ts +23 -0
  42. package/dist/input/edit-context.js +102 -0
  43. package/dist/input/surface.d.ts +101 -0
  44. package/dist/input/surface.js +84 -0
  45. package/dist/input/textarea.d.ts +26 -0
  46. package/dist/input/textarea.js +125 -0
  47. package/dist/sheet.d.ts +6 -0
  48. package/dist/sheet.js +12 -0
  49. package/dist/state/core.d.ts +73 -0
  50. package/dist/state/core.js +86 -0
  51. package/dist/state/fold-ranges.d.ts +41 -0
  52. package/dist/state/fold-ranges.js +167 -0
  53. package/dist/state/folds.d.ts +45 -0
  54. package/dist/state/folds.js +240 -0
  55. package/dist/state/history.d.ts +25 -0
  56. package/dist/state/history.js +127 -0
  57. package/dist/state/indent.d.ts +33 -0
  58. package/dist/state/indent.js +121 -0
  59. package/dist/state/selection.d.ts +45 -0
  60. package/dist/state/selection.js +106 -0
  61. package/dist/state/whitespace.d.ts +16 -0
  62. package/dist/state/whitespace.js +54 -0
  63. package/dist/text/changeset.d.ts +56 -0
  64. package/dist/text/changeset.js +321 -0
  65. package/dist/text/text.d.ts +30 -0
  66. package/dist/text/text.js +321 -0
  67. package/dist/tokens/bridge.d.ts +107 -0
  68. package/dist/tokens/bridge.js +461 -0
  69. package/dist/tokens/engine.d.ts +39 -0
  70. package/dist/tokens/engine.js +360 -0
  71. package/dist/tokens/protocol.d.ts +113 -0
  72. package/dist/tokens/protocol.js +38 -0
  73. package/dist/tokens/textmate/ascii-regex.d.ts +5 -0
  74. package/dist/tokens/textmate/ascii-regex.js +156 -0
  75. package/dist/tokens/textmate/index.d.ts +7 -0
  76. package/dist/tokens/textmate/index.js +19 -0
  77. package/dist/tokens/textmate/linker.d.ts +67 -0
  78. package/dist/tokens/textmate/linker.js +238 -0
  79. package/dist/tokens/textmate/scopes.d.ts +14 -0
  80. package/dist/tokens/textmate/scopes.js +54 -0
  81. package/dist/tokens/textmate/theme.d.ts +54 -0
  82. package/dist/tokens/textmate/theme.js +138 -0
  83. package/dist/tokens/textmate/tokenizer.d.ts +29 -0
  84. package/dist/tokens/textmate/tokenizer.js +684 -0
  85. package/dist/tokens/textmate.d.ts +49 -0
  86. package/dist/tokens/textmate.js +4 -0
  87. package/dist/view/decorations.d.ts +183 -0
  88. package/dist/view/decorations.js +51 -0
  89. package/dist/view/guides.d.ts +58 -0
  90. package/dist/view/guides.js +174 -0
  91. package/dist/view/heightmap.d.ts +97 -0
  92. package/dist/view/heightmap.js +229 -0
  93. package/dist/view/line-dom.d.ts +54 -0
  94. package/dist/view/line-dom.js +168 -0
  95. package/dist/view/measure.d.ts +37 -0
  96. package/dist/view/measure.js +136 -0
  97. package/dist/view/overlays.d.ts +32 -0
  98. package/dist/view/overlays.js +135 -0
  99. package/dist/view/rail.d.ts +27 -0
  100. package/dist/view/rail.js +232 -0
  101. package/dist/view/remote.d.ts +24 -0
  102. package/dist/view/remote.js +143 -0
  103. package/dist/view/scope-focus.d.ts +27 -0
  104. package/dist/view/scope-focus.js +255 -0
  105. package/dist/view/view.d.ts +114 -0
  106. package/dist/view/view.js +1231 -0
  107. package/dist/vite.d.ts +12 -0
  108. package/dist/vite.js +64 -0
  109. package/dist/worker.d.ts +1 -0
  110. package/dist/worker.js +64 -0
  111. package/grammars/astro.mjs +2 -0
  112. package/grammars/chunks/r-90cb351dba.mjs +1 -0
  113. package/grammars/chunks/r-b92a500aab.mjs +1 -0
  114. package/grammars/chunks/r-ced5311992.mjs +1 -0
  115. package/grammars/chunks/r-ebae91d4ec.mjs +1 -0
  116. package/grammars/chunks/s-1821e66170.mjs +1 -0
  117. package/grammars/css.mjs +1 -0
  118. package/grammars/diff.mjs +1 -0
  119. package/grammars/docker.mjs +1 -0
  120. package/grammars/dotenv.mjs +1 -0
  121. package/grammars/graphql.mjs +1 -0
  122. package/grammars/html.mjs +2 -0
  123. package/grammars/ini.mjs +1 -0
  124. package/grammars/javascript.mjs +5 -0
  125. package/grammars/json.mjs +1 -0
  126. package/grammars/json5.mjs +1 -0
  127. package/grammars/jsonc.mjs +1 -0
  128. package/grammars/less.mjs +1 -0
  129. package/grammars/manifest.json +58 -0
  130. package/grammars/markdown.mjs +1 -0
  131. package/grammars/mdx.mjs +1 -0
  132. package/grammars/pug.mjs +1 -0
  133. package/grammars/scss.mjs +1 -0
  134. package/grammars/shellscript.mjs +1 -0
  135. package/grammars/sql.mjs +1 -0
  136. package/grammars/stylus.mjs +1 -0
  137. package/grammars/svelte.mjs +1 -0
  138. package/grammars/toml.mjs +1 -0
  139. package/grammars/tsx.mjs +5 -0
  140. package/grammars/typescript.mjs +2 -0
  141. package/grammars/vue.mjs +1 -0
  142. package/grammars/yaml.mjs +1 -0
  143. package/package.json +71 -3
  144. package/themes/dark-plus.json +1 -0
  145. package/themes/light-plus.json +1 -0
  146. package/themes/monokai.json +1 -0
@@ -0,0 +1,684 @@
1
+ import { asciiRegex } from "./ascii-regex.js";
2
+ import { BEGIN_END, BEGIN_WHILE, CAPTURE, INCLUDE, MATCH } from "./linker.js";
3
+ import { ScopeTable } from "./scopes.js";
4
+ const END_RULE = -1;
5
+ const CAPTURING_NAME = /\$(\d+)|\${(\d+):\/(downcase|upcase)}/g;
6
+ const MARK = 0xe000;
7
+ // A rule-stack frame. Immutable; enterPos/anchorPos are only meaningful on the line (gen) that
8
+ // created the frame, which stands in for vscode-textmate's StateStackImpl.reset().
9
+ export class Frame {
10
+ parent;
11
+ ruleId;
12
+ enterPos;
13
+ anchorPos;
14
+ gen;
15
+ capturedEOL;
16
+ endRule;
17
+ nameScopes;
18
+ contentScopes;
19
+ depth;
20
+ constructor(parent, ruleId, enterPos, anchorPos, gen, capturedEOL, endRule, nameScopes, contentScopes) {
21
+ this.parent = parent;
22
+ this.ruleId = ruleId;
23
+ this.enterPos = enterPos;
24
+ this.anchorPos = anchorPos;
25
+ this.gen = gen;
26
+ this.capturedEOL = capturedEOL;
27
+ this.endRule = endRule;
28
+ this.nameScopes = nameScopes;
29
+ this.contentScopes = contentScopes;
30
+ this.depth = parent ? parent.depth + 1 : 1;
31
+ }
32
+ equals(other) {
33
+ if (this === other)
34
+ return true;
35
+ if (!other || this.contentScopes !== other.contentScopes)
36
+ return false;
37
+ let a = this, b = other;
38
+ while (a !== b) {
39
+ if (!a || !b || a.depth !== b.depth || a.ruleId !== b.ruleId || a.endRule !== b.endRule)
40
+ return false;
41
+ a = a.parent;
42
+ b = b.parent;
43
+ }
44
+ return true;
45
+ }
46
+ }
47
+ const NON_ASCII = /[^\x00-\x7f]/;
48
+ // Whether a string contains one of a gate's literals (compile.mjs, extra.gate). Under the i flag
49
+ // the literals are lowercase, and the string is compared lowercased, with long s (\u017f) folded to
50
+ // s: it is the one character whose case folding reaches an ASCII letter but whose lowercase does
51
+ // not. The lowercase is kept for the next regex on the same string.
52
+ let lowerOf = null, lowered = '';
53
+ export function passesGate(gate, icase, str) {
54
+ let hay = str;
55
+ if (icase) {
56
+ if (str !== lowerOf) {
57
+ lowerOf = str;
58
+ lowered = str.toLowerCase();
59
+ if (NON_ASCII.test(lowered))
60
+ lowered = lowered.replace(/\u017f/g, 's');
61
+ }
62
+ hay = lowered;
63
+ }
64
+ for (const lit of gate)
65
+ if (hay.includes(lit))
66
+ return true;
67
+ return false;
68
+ }
69
+ // One compiled regex plus the per-string search cache vscode-oniguruma keeps per pattern.
70
+ class Regex {
71
+ re;
72
+ indices;
73
+ clip;
74
+ cacheable;
75
+ map = null;
76
+ gate;
77
+ icase;
78
+ gateId = -1;
79
+ gateOk = false;
80
+ strId = -1;
81
+ from = 0;
82
+ at = -1;
83
+ caps = null;
84
+ constructor(source, flags, extra) {
85
+ this.re = new RegExp(source, flags);
86
+ this.indices = flags.includes('d');
87
+ this.clip = !!extra?.clip;
88
+ this.cacheable = !this.re.sticky && !this.clip;
89
+ this.gate = extra?.gate ?? null;
90
+ this.icase = this.re.ignoreCase;
91
+ if (extra?.transfers)
92
+ throw new Error('capture transfers are not supported');
93
+ if (extra?.hidden) {
94
+ const hidden = new Set(extra.hidden);
95
+ const groups = new RegExp(`${source}|`, flags.replace(/[gy]/g, '')).exec('').length;
96
+ this.map = [0];
97
+ for (let i = 1; i < groups; i++)
98
+ if (!hidden.has(i))
99
+ this.map.push(i);
100
+ }
101
+ }
102
+ search(str, strId, pos) {
103
+ if (this.cacheable && this.strId === strId && this.from <= pos && (this.at === -1 || this.at >= pos)) {
104
+ return this.at === -1 ? null : this.caps;
105
+ }
106
+ // A string without any of the gate's literals cannot match, so the exec is skipped: a regex
107
+ // that never runs is never compiled.
108
+ const caps = this.gate && !this.#passes(str, strId) ? null : this.#exec(str, pos);
109
+ if (this.cacheable) {
110
+ this.strId = strId;
111
+ this.from = pos;
112
+ this.at = caps ? caps[0] : -1;
113
+ this.caps = caps;
114
+ }
115
+ return caps;
116
+ }
117
+ #passes(str, strId) {
118
+ if (this.gateId !== strId) {
119
+ this.gateId = strId;
120
+ this.gateOk = passesGate(this.gate, this.icase, str);
121
+ }
122
+ return this.gateOk;
123
+ }
124
+ #exec(str, pos) {
125
+ if (this.clip) {
126
+ this.re.lastIndex = 0;
127
+ const m = this.re.exec(pos ? str.slice(pos) : str);
128
+ return m ? this.#toCaps(m, pos) : null;
129
+ }
130
+ this.re.lastIndex = pos;
131
+ const m = this.re.exec(str);
132
+ return m ? this.#toCaps(m, 0) : null;
133
+ }
134
+ #toCaps(m, offset) {
135
+ if (!this.indices)
136
+ return [m.index + offset, m.index + offset + m[0].length];
137
+ const ind = m.indices, map = this.map;
138
+ const n = map ? map.length : ind.length;
139
+ const caps = new Array(n * 2);
140
+ for (let i = 0; i < n; i++) {
141
+ const r = ind[map ? map[i] : i];
142
+ if (r) {
143
+ caps[2 * i] = r[0] + offset;
144
+ caps[2 * i + 1] = r[1] + offset;
145
+ }
146
+ else {
147
+ caps[2 * i] = -1;
148
+ caps[2 * i + 1] = -1;
149
+ }
150
+ }
151
+ return caps;
152
+ }
153
+ }
154
+ // Escape captured text for any position in a v-flag pattern, including inside a class.
155
+ const escapeForRegex = (s) => s.replace(/[^A-Za-z0-9_]/gu, (c) => `\\u{${c.codePointAt(0).toString(16)}}`);
156
+ function replaceCaptures(name, line, caps) {
157
+ return name.replace(CAPTURING_NAME, (match, index, commandIndex, command) => {
158
+ const k = parseInt(index || commandIndex, 10);
159
+ if (2 * k >= caps.length)
160
+ return match;
161
+ let r = caps[2 * k] < 0 ? '' : line.substring(caps[2 * k], caps[2 * k + 1]);
162
+ while (r[0] === '.')
163
+ r = r.substring(1);
164
+ return command === 'downcase' ? r.toLowerCase() : command === 'upcase' ? r.toUpperCase() : r;
165
+ });
166
+ }
167
+ // vscode-textmate's scope selector matcher (createMatchers + nameMatcher).
168
+ function createMatchers(selector) {
169
+ const results = [];
170
+ const re = /([LR]:|[\w.:][\w.:-]*|[,|\-()])/g;
171
+ let m = re.exec(selector);
172
+ const next = () => {
173
+ if (!m)
174
+ return null;
175
+ const r = m[0];
176
+ m = re.exec(selector);
177
+ return r;
178
+ };
179
+ const isIdent = (t) => !!t && /[\w.:]+/.test(t);
180
+ let token = next();
181
+ const parseOperand = () => {
182
+ if (token === '-') {
183
+ token = next();
184
+ const neg = parseOperand();
185
+ return (s) => !!neg && !neg(s);
186
+ }
187
+ if (token === '(') {
188
+ token = next();
189
+ const inner = parseInner();
190
+ if (token === ')')
191
+ token = next();
192
+ return inner;
193
+ }
194
+ if (isIdent(token)) {
195
+ const ids = [];
196
+ do {
197
+ ids.push(token);
198
+ token = next();
199
+ } while (isIdent(token));
200
+ return (s) => nameMatcher(ids, s);
201
+ }
202
+ return null;
203
+ };
204
+ const parseConjunction = () => {
205
+ const ms = [];
206
+ let x = parseOperand();
207
+ while (x) {
208
+ ms.push(x);
209
+ x = parseOperand();
210
+ }
211
+ return (s) => ms.every((f) => f(s));
212
+ };
213
+ const parseInner = () => {
214
+ const ms = [];
215
+ let x = parseConjunction();
216
+ while (x) {
217
+ ms.push(x);
218
+ if (token === '|' || token === ',') {
219
+ do
220
+ token = next();
221
+ while (token === '|' || token === ',');
222
+ }
223
+ else
224
+ break;
225
+ x = parseConjunction();
226
+ }
227
+ return (s) => ms.some((f) => f(s));
228
+ };
229
+ while (token !== null) {
230
+ let priority = 0;
231
+ if (token.length === 2 && token[1] === ':') {
232
+ priority = token[0] === 'R' ? 1 : token[0] === 'L' ? -1 : 0;
233
+ token = next();
234
+ }
235
+ results.push({ matcher: parseConjunction(), priority });
236
+ if (token !== ',')
237
+ break;
238
+ token = next();
239
+ }
240
+ return results;
241
+ }
242
+ function nameMatcher(ids, scopes) {
243
+ if (scopes.length < ids.length)
244
+ return false;
245
+ let last = 0;
246
+ return ids.every((id) => {
247
+ for (let i = last; i < scopes.length; i++) {
248
+ const s = scopes[i];
249
+ if (s === id || (s.length > id.length && s.startsWith(id) && s[id.length] === '.')) {
250
+ last = i + 1;
251
+ return true;
252
+ }
253
+ }
254
+ return false;
255
+ });
256
+ }
257
+ export class Tokenizer {
258
+ #g;
259
+ #rules;
260
+ #pats;
261
+ #regexSpecs;
262
+ #regexCache = [];
263
+ #asciiCache = [];
264
+ #ascii = false;
265
+ #lineMask = 0;
266
+ #lineScanners = [];
267
+ #dynamicCache = new Map();
268
+ #scanners = new Map();
269
+ #scopes;
270
+ #gen = 0;
271
+ #strId = 0;
272
+ #injections = [];
273
+ #injectionMatch = new Map();
274
+ initial;
275
+ constructor(grammar, scopes = new ScopeTable()) {
276
+ this.#g = grammar;
277
+ // The linker appends to these arrays in place, so the references stay current across links.
278
+ this.#rules = grammar.rules;
279
+ this.#pats = grammar.pats;
280
+ this.#regexSpecs = grammar.regexes;
281
+ this.#scopes = scopes;
282
+ for (const [selector, ruleId] of grammar.injections) {
283
+ for (const { matcher, priority } of createMatchers(selector))
284
+ this.#injections.push({ matcher, priority, ruleId });
285
+ }
286
+ this.#injections.sort((a, b) => a.priority - b.priority);
287
+ const rootName = this.#rules[grammar.root][1];
288
+ const root = scopes.push(-1, rootName ?? 'unknown');
289
+ this.initial = new Frame(null, grammar.root, -1, -1, -1, false, null, root, root);
290
+ }
291
+ /** Call after the linked grammar relinks: pattern lists may have changed, rule ids have not. */
292
+ invalidate() {
293
+ this.#scanners.clear();
294
+ }
295
+ #regex(id) {
296
+ const spec = this.#regexSpecs[id];
297
+ const cache = this.#ascii ? this.#asciiCache : this.#regexCache;
298
+ let r = cache[id];
299
+ if (r === undefined) {
300
+ if (!spec)
301
+ r = cache[id] = null;
302
+ else if (this.#ascii) {
303
+ // The first time an ASCII line needs this regex, try the no-v form; V8 compiles it much
304
+ // faster. Cached in #asciiCache from here on, so the conversion runs at most once per id.
305
+ const src = spec[2]?.ascii ?? spec[0];
306
+ const fast = asciiRegex(src, spec[1]);
307
+ r = cache[id] = fast ? new Regex(fast.source, fast.flags, spec[2]) : new Regex(src, spec[1], spec[2]);
308
+ }
309
+ else
310
+ r = cache[id] = new Regex(spec[0], spec[1], spec[2]);
311
+ }
312
+ return r;
313
+ }
314
+ // An end/while pattern with backreferences, specialized to the captured text in endRule.
315
+ #dynamicRegex(id, endRule) {
316
+ const ascii = this.#ascii && this.#regexSpecs[id]?.[2]?.ascii;
317
+ const key = `${ascii ? 'a' : 'u'}${id}\0${endRule}`;
318
+ let r = this.#dynamicCache.get(key);
319
+ if (r === undefined) {
320
+ const spec = this.#regexSpecs[id];
321
+ if (!spec)
322
+ r = null;
323
+ else {
324
+ const values = endRule.split('\0');
325
+ let i = 0;
326
+ const src = (ascii ? ascii : spec[0]).replace(/[\ue000-\ue0ff]/g, () => escapeForRegex(values[i++] ?? ''));
327
+ r = new Regex(src, spec[1], spec[2]);
328
+ }
329
+ this.#dynamicCache.set(key, r);
330
+ }
331
+ return r;
332
+ }
333
+ // Flattened [patId, ruleId] list for a rule on top of the stack (vscode-textmate's compileAG).
334
+ #scanner(ruleId) {
335
+ let s = this.#scanners.get(ruleId);
336
+ if (s)
337
+ return s;
338
+ const pats = [], ids = [];
339
+ // The linked grammar supplies pattern lists with missing grammars pruned, and the bits of the
340
+ // grammars whose absence changed them.
341
+ const eff = this.#g.effective;
342
+ let mask = 0;
343
+ const probes = [];
344
+ const childrenOf = (id) => {
345
+ const e = eff[id];
346
+ if (!e)
347
+ return [];
348
+ mask |= e.lost;
349
+ for (const q of e.pruned)
350
+ probes.push(q);
351
+ return e.list;
352
+ };
353
+ const collect = (id) => {
354
+ const r = this.#rules[id];
355
+ if (r[0] === MATCH) {
356
+ pats.push(r[2]);
357
+ ids.push(id);
358
+ }
359
+ else if (r[0] === INCLUDE)
360
+ for (const p of childrenOf(id))
361
+ collect(p);
362
+ else if (r[0] === BEGIN_END || r[0] === BEGIN_WHILE) {
363
+ pats.push(r[3]);
364
+ ids.push(id);
365
+ }
366
+ };
367
+ const r = this.#rules[ruleId];
368
+ const children = r[0] === INCLUDE || r[0] === BEGIN_END || r[0] === BEGIN_WHILE ? childrenOf(ruleId) : [];
369
+ if (r[0] === MATCH)
370
+ collect(ruleId);
371
+ else
372
+ for (const p of children)
373
+ collect(p);
374
+ let dynamic = -1;
375
+ if (r[0] === BEGIN_END) {
376
+ const endPat = r[5];
377
+ if (r[7]) {
378
+ pats.push(endPat);
379
+ ids.push(END_RULE);
380
+ }
381
+ else {
382
+ pats.unshift(endPat);
383
+ ids.unshift(END_RULE);
384
+ }
385
+ if (this.#regexSpecs[this.#pats[endPat][0]]?.[2]?.tpl)
386
+ dynamic = r[7] ? pats.length - 1 : 0;
387
+ }
388
+ s = { pats, ids, dynamic, mask, probes, gen: -1 };
389
+ this.#scanners.set(ruleId, s);
390
+ return s;
391
+ }
392
+ #findNext(scanner, str, strId, pos, variant, endRule) {
393
+ const { pats, ids, dynamic } = scanner;
394
+ this.#lineMask |= scanner.mask;
395
+ if (scanner.probes.length && scanner.gen !== this.#gen) {
396
+ scanner.gen = this.#gen;
397
+ this.#lineScanners.push(scanner);
398
+ }
399
+ let bestCaps = null, bestRule = 0, bestAt = Infinity;
400
+ for (let i = 0; i < pats.length; i++) {
401
+ const rid = this.#pats[pats[i]][variant];
402
+ const re = i === dynamic ? (endRule === null ? null : this.#dynamicRegex(rid, endRule)) : this.#regex(rid);
403
+ if (!re)
404
+ continue;
405
+ const caps = re.search(str, strId, pos);
406
+ if (!caps)
407
+ continue;
408
+ if (caps[0] < bestAt) {
409
+ bestAt = caps[0];
410
+ bestCaps = caps;
411
+ bestRule = ids[i];
412
+ if (bestAt === pos)
413
+ break;
414
+ }
415
+ }
416
+ return bestCaps ? { caps: bestCaps, ruleId: bestRule } : null;
417
+ }
418
+ #matchInjections(str, strId, pos, frame, variant) {
419
+ let bestAt = Infinity, best = null, priority = 0;
420
+ for (let i = 0; i < this.#injections.length; i++) {
421
+ const inj = this.#injections[i];
422
+ const key = frame.contentScopes * 64 + i;
423
+ let ok = this.#injectionMatch.get(key);
424
+ if (ok === undefined) {
425
+ ok = inj.matcher(this.#scopes.names(frame.contentScopes));
426
+ this.#injectionMatch.set(key, ok);
427
+ }
428
+ if (!ok)
429
+ continue;
430
+ const r = this.#findNext(this.#scanner(inj.ruleId), str, strId, pos, variant, null);
431
+ if (!r || r.caps[0] >= bestAt)
432
+ continue;
433
+ bestAt = r.caps[0];
434
+ best = r;
435
+ priority = inj.priority;
436
+ if (bestAt === pos)
437
+ break;
438
+ }
439
+ return best ? { caps: best.caps, ruleId: best.ruleId, priorityMatch: priority === -1 } : null;
440
+ }
441
+ #match(str, strId, pos, frame, allowA, allowG) {
442
+ const variant = (allowA ? 2 : 0) + (allowG ? 1 : 0);
443
+ const r = this.#findNext(this.#scanner(frame.ruleId), str, strId, pos, variant, frame.endRule);
444
+ if (this.#injections.length === 0)
445
+ return r;
446
+ const inj = this.#matchInjections(str, strId, pos, frame, variant);
447
+ if (!inj)
448
+ return r;
449
+ if (!r)
450
+ return inj;
451
+ return inj.caps[0] < r.caps[0] || (inj.priorityMatch && inj.caps[0] === r.caps[0]) ? inj : r;
452
+ }
453
+ #enterPos(f) {
454
+ return f.gen === this.#gen ? f.enterPos : -1;
455
+ }
456
+ #anchorPos(f) {
457
+ return f.gen === this.#gen ? f.anchorPos : -1;
458
+ }
459
+ #push(parent, ruleId, enterPos, anchorPos, capturedEOL, endRule, nameScopes, contentScopes) {
460
+ return new Frame(parent, ruleId, enterPos, anchorPos, this.#gen, capturedEOL, endRule, nameScopes, contentScopes);
461
+ }
462
+ #withContentScopes(f, contentScopes) {
463
+ if (f.contentScopes === contentScopes)
464
+ return f;
465
+ return this.#push(f.parent, f.ruleId, this.#enterPos(f), this.#anchorPos(f), f.capturedEOL, f.endRule, f.nameScopes, contentScopes);
466
+ }
467
+ #withEndRule(f, endRule) {
468
+ if (f.endRule === endRule)
469
+ return f;
470
+ return this.#push(f.parent, f.ruleId, this.#enterPos(f), this.#anchorPos(f), f.capturedEOL, endRule, f.nameScopes, f.contentScopes);
471
+ }
472
+ #hasSameRuleAs(a, b) {
473
+ const bEnter = this.#enterPos(b);
474
+ for (let el = a; el && this.#enterPos(el) === bEnter; el = el.parent)
475
+ if (el.ruleId === b.ruleId)
476
+ return true;
477
+ return false;
478
+ }
479
+ #pushName(scopeId, name, line, caps) {
480
+ if (name == null)
481
+ return scopeId;
482
+ if (caps && name.includes('$'))
483
+ name = replaceCaptures(name, line, caps);
484
+ return this.#scopes.pushPath(scopeId, name);
485
+ }
486
+ tokenizeLine(line, prevState) {
487
+ this.#gen++;
488
+ // ASCII and Unicode \b agree unless a non-ASCII character is on the line.
489
+ this.#ascii = !NON_ASCII.test(line);
490
+ this.#lineMask = 0;
491
+ this.#lineScanners.length = 0;
492
+ const isFirstLine = !prevState;
493
+ const state = prevState ?? this.initial;
494
+ const str = `${line}\n`;
495
+ const out = { tokens: [], last: 0 };
496
+ const end = this.#tokenizeString(str, ++this.#strId, isFirstLine, 0, state, out, true);
497
+ const toks = out.tokens;
498
+ if (toks.length && toks[toks.length - 2] === str.length - 1)
499
+ toks.length -= 2;
500
+ if (!toks.length)
501
+ toks.push(0, end.contentScopes);
502
+ // A pruned rule only matters on a line where its begin could have matched.
503
+ for (const sc of this.#lineScanners) {
504
+ for (const [re, bits] of sc.probes)
505
+ if ((this.#lineMask & bits) !== bits && re.test(str))
506
+ this.#lineMask |= bits;
507
+ }
508
+ return { tokens: toks, state: end, misses: this.#lineMask };
509
+ }
510
+ #produce(out, scopeId, endIndex) {
511
+ if (out.last >= endIndex)
512
+ return;
513
+ const t = out.tokens;
514
+ if (t.length && t[t.length - 1] === scopeId) {
515
+ out.last = endIndex;
516
+ return;
517
+ }
518
+ t.push(out.last, scopeId);
519
+ out.last = endIndex;
520
+ }
521
+ #tokenizeString(str, strId, isFirstLine, linePos, stack, out, checkWhile) {
522
+ const lineLength = str.length;
523
+ let anchorPosition = -1;
524
+ if (checkWhile) {
525
+ const w = this.#checkWhileConditions(str, strId, isFirstLine, linePos, stack, out);
526
+ stack = w.stack;
527
+ linePos = w.linePos;
528
+ isFirstLine = w.isFirstLine;
529
+ anchorPosition = w.anchorPosition;
530
+ }
531
+ const rules = this.#rules;
532
+ for (;;) {
533
+ const r = this.#match(str, strId, linePos, stack, isFirstLine, linePos === anchorPosition);
534
+ if (!r) {
535
+ this.#produce(out, stack.contentScopes, lineLength);
536
+ return stack;
537
+ }
538
+ const caps = r.caps;
539
+ const hasAdvanced = caps[1] > linePos;
540
+ if (r.ruleId === END_RULE) {
541
+ const rule = rules[stack.ruleId];
542
+ this.#produce(out, stack.contentScopes, caps[0]);
543
+ stack = this.#withContentScopes(stack, stack.nameScopes);
544
+ this.#handleCaptures(str, isFirstLine, stack, out, rule[0] === BEGIN_END ? rule[6] : null, caps);
545
+ this.#produce(out, stack.contentScopes, caps[1]);
546
+ const popped = stack;
547
+ stack = stack.parent;
548
+ anchorPosition = this.#anchorPos(popped);
549
+ if (!hasAdvanced && this.#enterPos(popped) === linePos) {
550
+ stack = popped;
551
+ this.#produce(out, stack.contentScopes, lineLength);
552
+ return stack;
553
+ }
554
+ }
555
+ else {
556
+ const rule = rules[r.ruleId];
557
+ this.#produce(out, stack.contentScopes, caps[0]);
558
+ const beforePush = stack;
559
+ const nameScopes = this.#pushName(stack.contentScopes, rule[1], str, caps);
560
+ stack = this.#push(stack, r.ruleId, linePos, anchorPosition, caps[1] === lineLength, null, nameScopes, nameScopes);
561
+ if (rule[0] === BEGIN_END || rule[0] === BEGIN_WHILE) {
562
+ this.#handleCaptures(str, isFirstLine, stack, out, rule[4], caps);
563
+ this.#produce(out, stack.contentScopes, caps[1]);
564
+ anchorPosition = caps[1];
565
+ const contentScopes = this.#pushName(nameScopes, rule[2], str, caps);
566
+ stack = this.#withContentScopes(stack, contentScopes);
567
+ const endPat = rule[5];
568
+ const spec = this.#regexSpecs[this.#pats[endPat][0]];
569
+ if (spec?.[2]?.tpl)
570
+ stack = this.#withEndRule(stack, this.#resolveEndRule(endPat, str, caps));
571
+ if (!hasAdvanced && this.#hasSameRuleAs(beforePush, stack)) {
572
+ stack = stack.parent;
573
+ this.#produce(out, stack.contentScopes, lineLength);
574
+ return stack;
575
+ }
576
+ }
577
+ else {
578
+ this.#handleCaptures(str, isFirstLine, stack, out, rule[0] === MATCH ? rule[3] : null, caps);
579
+ this.#produce(out, stack.contentScopes, caps[1]);
580
+ stack = stack.parent;
581
+ if (!hasAdvanced) {
582
+ stack = stack.parent ?? stack;
583
+ this.#produce(out, stack.contentScopes, lineLength);
584
+ return stack;
585
+ }
586
+ }
587
+ }
588
+ if (caps[1] > linePos) {
589
+ linePos = caps[1];
590
+ isFirstLine = false;
591
+ }
592
+ }
593
+ }
594
+ // The captured values an end/while template references, in placeholder order.
595
+ #resolveEndRule(patId, str, caps) {
596
+ return this.#endRefs(patId).map((k) => (2 * k < caps.length && caps[2 * k] >= 0 ? str.substring(caps[2 * k], caps[2 * k + 1]) : '')).join('\0');
597
+ }
598
+ #endRefs(patId) {
599
+ const spec = this.#regexSpecs[this.#pats[patId][0]];
600
+ const refs = [];
601
+ for (const ch of spec[0]) {
602
+ const c = ch.charCodeAt(0);
603
+ if (c >= MARK && c <= MARK + 0xff)
604
+ refs.push(c - MARK);
605
+ }
606
+ return refs;
607
+ }
608
+ #checkWhileConditions(str, strId, isFirstLine, linePos, stack, out) {
609
+ let anchorPosition = stack.capturedEOL ? 0 : -1;
610
+ const whileFrames = [];
611
+ for (let f = stack; f; f = f.parent)
612
+ if (this.#rules[f.ruleId][0] === BEGIN_WHILE)
613
+ whileFrames.push(f);
614
+ for (let f = whileFrames.pop(); f; f = whileFrames.pop()) {
615
+ const rule = this.#rules[f.ruleId];
616
+ if (rule[0] !== BEGIN_WHILE)
617
+ continue;
618
+ const patId = rule[5];
619
+ const variant = (isFirstLine ? 2 : 0) + (linePos === anchorPosition ? 1 : 0);
620
+ const rid = this.#pats[patId][variant];
621
+ const spec = this.#regexSpecs[rid];
622
+ const re = spec?.[2]?.tpl ? this.#dynamicRegex(rid, f.endRule ?? '') : this.#regex(rid);
623
+ const caps = re ? re.search(str, strId, linePos) : null;
624
+ if (caps) {
625
+ this.#produce(out, f.contentScopes, caps[0]);
626
+ this.#handleCaptures(str, isFirstLine, f, out, rule[6], caps);
627
+ this.#produce(out, f.contentScopes, caps[1]);
628
+ anchorPosition = caps[1];
629
+ if (caps[1] > linePos) {
630
+ linePos = caps[1];
631
+ isFirstLine = false;
632
+ }
633
+ }
634
+ else {
635
+ stack = f.parent;
636
+ break;
637
+ }
638
+ }
639
+ return { stack, linePos, anchorPosition, isFirstLine };
640
+ }
641
+ #handleCaptures(str, isFirstLine, stack, out, captures, caps) {
642
+ if (!captures || captures.length === 0)
643
+ return;
644
+ const len = Math.min(captures.length, caps.length >> 1);
645
+ const local = [];
646
+ const maxEnd = caps[1];
647
+ for (let i = 0; i < len; i++) {
648
+ const capId = captures[i];
649
+ if (!capId)
650
+ continue;
651
+ const s = caps[2 * i], e = caps[2 * i + 1];
652
+ if (e - s === 0)
653
+ continue;
654
+ if (s > maxEnd)
655
+ break;
656
+ while (local.length && local[local.length - 1].end <= s) {
657
+ const top = local.pop();
658
+ this.#produce(out, top.scopes, top.end);
659
+ }
660
+ if (local.length)
661
+ this.#produce(out, local[local.length - 1].scopes, s);
662
+ else
663
+ this.#produce(out, stack.contentScopes, s);
664
+ const cap = this.#rules[capId];
665
+ if (cap[0] !== CAPTURE)
666
+ continue;
667
+ if (cap[3]) {
668
+ const nameScopes = this.#pushName(stack.contentScopes, cap[1], str, caps);
669
+ const contentScopes = this.#pushName(nameScopes, cap[2], str, caps);
670
+ const clone = this.#push(stack, cap[3], s, -1, false, null, nameScopes, contentScopes);
671
+ this.#tokenizeString(str.substring(0, e), ++this.#strId, isFirstLine && s === 0, s, clone, out, false);
672
+ continue;
673
+ }
674
+ if (cap[1] != null) {
675
+ const base = local.length ? local[local.length - 1].scopes : stack.contentScopes;
676
+ local.push({ scopes: this.#pushName(base, cap[1], str, caps), end: e });
677
+ }
678
+ }
679
+ while (local.length) {
680
+ const top = local.pop();
681
+ this.#produce(out, top.scopes, top.end);
682
+ }
683
+ }
684
+ }