-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbisonParser.ts
More file actions
636 lines (586 loc) · 24.3 KB
/
Copy pathbisonParser.ts
File metadata and controls
636 lines (586 loc) · 24.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
import { Range } from 'vscode-languageserver';
import {
BisonDocument,
TokenDeclaration,
NonTerminalDeclaration,
DefineDeclaration,
PrecedenceDeclaration,
RuleDefinition,
RuleAlternative,
DollarRef,
} from './types';
/**
* All directives recognized by GNU Bison (and common deprecated aliases).
* Anything starting with % that isn't in this set → unknown directive diagnostic.
*/
const KNOWN_BISON_DIRECTIVES = new Set([
// Modern Bison 3.x directives
'token', 'type', 'nterm', 'define', 'code',
'left', 'right', 'nonassoc', 'precedence',
'start', 'union', 'expect', 'expect-rr', 'require',
'language', 'skeleton', 'glr-parser', 'locations',
'defines', 'debug', 'param', 'parse-param', 'lex-param',
'printer', 'destructor', 'empty', 'prec',
'initial-action', 'verbose', 'no-lines', 'token-table',
'output', 'file-prefix', 'header', 'name-prefix',
'pure-parser', 'error-verbose',
// Yacc legacy (underscore variants + aliases) — tolerated without error,
// diagnostics.ts will emit Information-level migration suggestions for these.
'pure_parser', 'name_prefix', 'token_table', 'no_lines',
'lex_param', 'parse_param',
'binary', // Yacc alias for %nonassoc
'expect_rr', // underscore variant
'file_prefix', // underscore variant
]);
export function parseBisonDocument(text: string): BisonDocument {
// Strip /* ... */ block comments (including multi-line) before any line-by-line
// processing. Newlines inside comments are preserved so that all line numbers
// remain accurate for diagnostics. Non-newline characters are replaced with
// spaces so column positions of surrounding tokens are unaffected.
const processedText = text.replace(/\/\*[\s\S]*?\*\//g, m =>
m.replace(/[^\n]/g, ' '));
const lines = processedText.split(/\r?\n/);
const doc: BisonDocument = {
tokens: new Map(),
nonTerminals: new Map(),
defines: new Map(),
precedence: [],
codeBlocks: [],
rules: new Map(),
separators: [],
ruleReferences: new Map(),
unknownDirectives: [],
duplicateRules: [],
};
// Phase 1: Find %% separators (skip those inside code blocks)
let braceDepth = 0;
let inPrologueBlock = false; // %{ ... %}
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const trimmed = line.trim();
if (trimmed === '%{') {
inPrologueBlock = true;
continue;
}
if (trimmed === '%}') {
inPrologueBlock = false;
continue;
}
if (inPrologueBlock) continue;
// Track brace depth for %code blocks etc.
for (const ch of line) {
if (ch === '{') braceDepth++;
if (ch === '}') braceDepth = Math.max(0, braceDepth - 1);
}
if (trimmed === '%%' && braceDepth === 0) {
doc.separators.push(i);
}
}
const declarationsEnd = doc.separators.length > 0 ? doc.separators[0] : lines.length;
const rulesStart = doc.separators.length > 0 ? doc.separators[0] + 1 : lines.length;
const rulesEnd = doc.separators.length > 1 ? doc.separators[1] : lines.length;
// Phase 2: Parse declarations section
let lastTokenDirectiveLine = -1;
let lastTokenType: string | undefined;
for (let i = 0; i < declarationsEnd; i++) {
const line = lines[i];
const trimmed = line.trim();
// Skip comments
if (trimmed.startsWith('//') || trimmed.startsWith('/*')) continue;
// Skip %{ %} blocks
if (trimmed === '%{' || trimmed === '%}') continue;
// %token <type> NAME "alias" NAME2 "alias2" ...
const tokenMatch = trimmed.match(/^%token(?:\s+<([^>]+)>)?\s+(.+)/);
if (tokenMatch) {
lastTokenType = tokenMatch[1];
lastTokenDirectiveLine = i;
const tokenColOffset = line.indexOf(trimmed) + (tokenMatch[0].length - tokenMatch[2].length);
parseTokenNames(tokenMatch[2], lastTokenType, i, doc, tokenColOffset);
continue;
}
// %type <type> name1 name2 ...
const typeMatch = trimmed.match(/^%type\s+<([^>]+)>\s+(.+)/);
if (typeMatch) {
const type = typeMatch[1];
const names = typeMatch[2].match(/[a-zA-Z_][a-zA-Z0-9_.]*/g);
if (names) {
for (const name of names) {
const col = line.indexOf(name);
const decl: NonTerminalDeclaration = {
name,
type,
location: Range.create(i, col, i, col + name.length),
};
doc.nonTerminals.set(name, decl);
}
}
continue;
}
// %nterm <type> name1 name2 ...
const ntermMatch = trimmed.match(/^%nterm\s+<([^>]+)>\s+(.+)/);
if (ntermMatch) {
const type = ntermMatch[1];
const names = ntermMatch[2].match(/[a-zA-Z_][a-zA-Z0-9_.]*/g);
if (names) {
for (const name of names) {
const col = line.indexOf(name);
doc.nonTerminals.set(name, {
name,
type,
location: Range.create(i, col, i, col + name.length),
});
}
}
continue;
}
// %define variable value
const defineMatch = trimmed.match(/^%define\s+([a-zA-Z_.][a-zA-Z_.0-9-]*)\s*(.*)/);
if (defineMatch) {
const variable = defineMatch[1];
const value = defineMatch[2]?.trim() || '';
const col = line.indexOf(variable);
doc.defines.set(variable, {
variable,
value,
location: Range.create(i, col, i, col + variable.length),
});
continue;
}
// %left / %right / %nonassoc / %precedence
const precMatch = trimmed.match(/^%(left|right|nonassoc|precedence)\s+(.*)/);
if (precMatch) {
const kind = precMatch[1] as PrecedenceDeclaration['kind'];
const rawSymbols = precMatch[2].match(/[A-Za-z_][A-Za-z0-9_]*|"[^"]*"/g) || [];
const symbols: string[] = [];
const symbolRanges: Range[] = [];
for (const raw of rawSymbols) {
const sym = raw.replace(/"/g, '');
symbols.push(sym);
const col = line.indexOf(raw, line.indexOf(precMatch[2]));
const startCol = raw.startsWith('"') ? col + 1 : col; // skip quote for aliases
symbolRanges.push(Range.create(i, startCol, i, startCol + sym.length));
}
doc.precedence.push({
kind,
symbols,
symbolRanges,
location: Range.create(i, 0, i, line.length),
});
continue;
}
// %start symbol
const startMatch = trimmed.match(/^%start\s+([a-zA-Z_][a-zA-Z0-9_]*)/);
if (startMatch) {
doc.startSymbol = startMatch[1];
const symCol = line.indexOf(startMatch[1]);
doc.startSymbolLocation = Range.create(i, symCol >= 0 ? symCol : 0, i, (symCol >= 0 ? symCol : 0) + startMatch[1].length);
continue;
}
// %expect / %expect-rr
// %require, %language, %skeleton, etc. — parsed for awareness but no special handling
// Continuation lines for %token (indented names after a %token line)
if (lastTokenDirectiveLine >= 0 && i === lastTokenDirectiveLine + 1 && /^\s+[a-zA-Z_]/.test(line)) {
parseTokenNames(trimmed, lastTokenType, i, doc, line.indexOf(trimmed));
lastTokenDirectiveLine = i; // allow chaining
continue;
}
if (/^\s+[a-zA-Z_]/.test(line) && i > 0 && i <= lastTokenDirectiveLine + 1) {
parseTokenNames(trimmed, lastTokenType, i, doc, line.indexOf(trimmed));
lastTokenDirectiveLine = i;
continue;
}
lastTokenDirectiveLine = -1;
// Unknown directive: any %word that didn't match a known pattern above
if (trimmed.startsWith('%') && !trimmed.startsWith('%%')) {
const directiveMatch = trimmed.match(/^%([a-zA-Z][a-zA-Z0-9_-]*)/);
if (directiveMatch && !KNOWN_BISON_DIRECTIVES.has(directiveMatch[1])) {
doc.unknownDirectives.push({
name: '%' + directiveMatch[1],
location: Range.create(i, 0, i, directiveMatch[0].length),
});
}
}
}
// Phase 3: Parse rules section
let currentRule: string | undefined;
braceDepth = 0;
for (let i = rulesStart; i < rulesEnd; i++) {
const line = lines[i];
const trimmed = line.trim();
// Skip empty lines and comments
if (!trimmed || trimmed.startsWith('//') || trimmed.startsWith('/*')) continue;
// %token directive inside the rules section (Bison allows declaring tokens after %%).
// Must be handled BEFORE rule-body processing to avoid contaminating rule symbols.
if (trimmed.startsWith('%token') && braceDepth === 0) {
const tm = trimmed.match(/^%token(?:\s+<([^>]+)>)?\s+(.+)/);
if (tm) parseTokenNames(tm[2], tm[1], i, doc, line.indexOf(trimmed) + (tm[0].length - tm[2].length));
continue;
}
// Inside a multi-line action block: scan for $n refs and track brace depth.
if (braceDepth > 0) {
if (currentRule) {
const mlRule = doc.rules.get(currentRule);
if (mlRule && mlRule.alternatives.length > 0) {
const lastAlt = mlRule.alternatives[mlRule.alternatives.length - 1];
const dollarRegex = /\$(\d+)/g;
let dm: RegExpExecArray | null;
while ((dm = dollarRegex.exec(line)) !== null) {
const n = parseInt(dm[1], 10);
(lastAlt.dollarRefs ??= []).push({
n,
range: Range.create(i, dm.index, i, dm.index + dm[0].length),
});
}
}
}
for (const ch of line) {
if (ch === '{') braceDepth++;
if (ch === '}') braceDepth = Math.max(0, braceDepth - 1);
}
continue;
}
// Rule definition: name:
const ruleDefMatch = trimmed.match(/^([a-zA-Z_][a-zA-Z0-9_.]*)\s*:/);
if (ruleDefMatch) {
currentRule = ruleDefMatch[1];
const col = line.indexOf(currentRule);
if (!doc.rules.has(currentRule)) {
doc.rules.set(currentRule, {
name: currentRule,
location: Range.create(i, col, i, col + currentRule.length),
alternatives: [],
});
} else {
// Duplicate rule definition
doc.duplicateRules.push({
name: currentRule,
location: Range.create(i, col, i, col + currentRule.length),
});
}
// Parse the rest of the line after ':' as the first alternative
const rest = trimmed.substring(ruleDefMatch[0].length);
const altRange = Range.create(i, 0, i, line.length);
const precTokenMatch = rest.match(/%prec\s+(\S+)/);
const alt: RuleAlternative = {
range: altRange,
firstSymbol: getFirstSymbol(rest),
symbols: extractSymbols(rest),
dollarRefs: extractDollarRefs(rest, i, line),
hasExplicitEmpty: /%empty/.test(rest),
hasPrec: precTokenMatch !== null,
precToken: precTokenMatch ? precTokenMatch[1] : undefined,
};
doc.rules.get(currentRule)!.alternatives.push(alt);
extractRuleReferences(rest, i, line, doc);
} else if (trimmed.startsWith('|') && currentRule) {
// New alternative: track first symbol
const altBody = trimmed.slice(1); // strip leading '|'
const altRange = Range.create(i, 0, i, line.length);
const precTokenMatch = altBody.match(/%prec\s+(\S+)/);
const alt: RuleAlternative = {
range: altRange,
firstSymbol: getFirstSymbol(altBody),
symbols: extractSymbols(altBody),
dollarRefs: extractDollarRefs(altBody, i, line),
hasExplicitEmpty: /%empty/.test(altBody),
hasPrec: precTokenMatch !== null,
precToken: precTokenMatch ? precTokenMatch[1] : undefined,
};
doc.rules.get(currentRule)?.alternatives.push(alt);
extractRuleReferences(trimmed, i, line, doc);
} else if (currentRule) {
// Continuation of current alternative (no '|', no rule def)
extractRuleReferences(trimmed, i, line, doc);
// Accumulate symbols and $n refs into the last alternative.
// This fills the phantom alternative created by bare "rule :" header lines
// and handles multi-line productions (e.g. `rule:\n A B C\n { action }`).
const curRule = doc.rules.get(currentRule);
if (curRule && curRule.alternatives.length > 0) {
const lastAlt = curRule.alternatives[curRule.alternatives.length - 1];
const newSymbols = extractSymbols(trimmed);
const newRefs = extractDollarRefs(trimmed, i, line);
if (!lastAlt.firstSymbol && newSymbols.length > 0) lastAlt.firstSymbol = newSymbols[0];
lastAlt.symbols.push(...newSymbols);
lastAlt.dollarRefs = [...(lastAlt.dollarRefs ?? []), ...newRefs];
if (/%empty/.test(trimmed)) lastAlt.hasExplicitEmpty = true;
const contPrecMatch = trimmed.match(/%prec\s+(\S+)/);
if (contPrecMatch) { lastAlt.hasPrec = true; lastAlt.precToken = contPrecMatch[1]; }
}
}
// Track braces — and detect when a multi-line action block opens.
// When braceDepth goes from 0 to >0, a mid-rule action block has started.
// Bison counts each action block as a grammar symbol ($N position), so we
// add a '__midaction__' sentinel to the current alternative's symbol list.
// Inline balanced blocks (e.g. `{ $$ = $1; }` on the same line) are already
// counted by extractSymbols; only the unbalanced-open case needs handling here.
const prevDepth = braceDepth;
for (const ch of line) {
if (ch === '{') braceDepth++;
if (ch === '}') braceDepth = Math.max(0, braceDepth - 1);
}
if (prevDepth === 0 && braceDepth > 0 && currentRule) {
const curRule = doc.rules.get(currentRule);
if (curRule && curRule.alternatives.length > 0) {
curRule.alternatives[curRule.alternatives.length - 1].symbols.push('__midaction__');
}
}
}
return doc;
}
/**
* Encode a Bison string literal (the quoted content) into a unique, safe
* identifier-like placeholder. The hex encoding ensures that "+" and "{"
* produce DIFFERENT placeholders — critical for second-token disambiguation
* in the shift/reduce heuristic.
*
* e.g. "+" → __s2b__
* "(" → __s28__
* "{" → __s7b__
* "function" → __s66756e6374696f6e__
*
* All placeholders start with "__s" (lowercase) so they are valid identifiers
* but FAIL the all-caps token check -- never mistaken for grammar terminals.
*/
function strLiteralPlaceholder(content: string): string {
const hex = Array.from(content)
.map(c => c.charCodeAt(0).toString(16).padStart(2, '0'))
.join('');
return `__s${hex}__`;
}
/**
* Replace every `"..."` and `'...'` in `text` with their unique strLiteralPlaceholder.
* Single-quoted character literals like `'('` are included in the content with the
* surrounding apostrophes to keep them distinct from double-quoted aliases like `"("`.
*/
function replaceStringLiterals(text: string): string {
return text
.replace(/"((?:[^"\\]|\\.)*)"/g, (_, content) => ` ${strLiteralPlaceholder(content)} `)
.replace(/'((?:[^'\\]|\\.)*)'/g, (_, content) => ` ${strLiteralPlaceholder(`'${content}'`)} `);
}
/**
* Remove all brace-balanced { ... } blocks from `text`, replacing each with `replacement`.
* Handles arbitrarily nested braces, unlike /\{[^}]*\}/ which stops at the first `}`.
* Unmatched `{` without a closing `}` (e.g. a multi-line action opener on its own line)
* are left out of the result — the Phase 3 brace tracker handles them separately.
*/
function removeBalancedBraces(text: string, replacement: string = ' '): string {
let result = '';
let depth = 0;
let pendingOpen = false; // true while inside a block that hasn't been closed yet
for (let i = 0; i < text.length; i++) {
if (text[i] === '{') {
if (depth === 0) pendingOpen = true;
depth++;
} else if (text[i] === '}') {
depth = Math.max(0, depth - 1);
if (depth === 0 && pendingOpen) {
result += replacement; // only emit placeholder when the block is fully closed
pendingOpen = false;
}
} else if (depth === 0) {
result += text[i];
}
}
return result;
}
/**
* Extract all grammar symbols (identifiers) from a production RHS in order.
*
* String literals ("+" , "{", "function", …) ARE counted as symbols because
* Bison treats them exactly like tokens in the $N position numbering.
* They are replaced with unique hex-encoded placeholders so that the
* second-symbol disambiguation in the shift/reduce heuristic can tell
* `"("` apart from `"{"` (both have different placeholders).
*/
function extractSymbols(text: string): string[] {
const cleaned = removeBalancedBraces(replaceStringLiterals(text), ' __midaction__ ') // inline actions count as a symbol ($N position)
.replace(/%prec\s+\S+/g, ' ') // remove %prec TOKEN
.replace(/%empty/g, ' ') // remove %empty
.replace(/\/\/.*$/g, ' ') // remove line comments
.trim();
const symbols: string[] = [];
const regex = /\b([a-zA-Z_][a-zA-Z0-9_.]*)\b/g;
let m: RegExpExecArray | null;
while ((m = regex.exec(cleaned)) !== null) {
symbols.push(m[1]);
}
return symbols;
}
/**
* Extract the first terminal or non-terminal symbol from a production RHS.
* Returns undefined for empty productions (%empty) or pure action blocks.
*
* String literals are replaced with unique hex-encoded placeholders so that
* an alternative starting with "function" has a firstSymbol starting with
* `__s` (not all-caps) and is therefore not confused with a real terminal.
*/
function getFirstSymbol(text: string): string | undefined {
const cleaned = removeBalancedBraces(replaceStringLiterals(text)) // remove inline actions
.replace(/%prec\s+\S+/g, ' ') // remove %prec TOKEN
.replace(/%empty/g, ' ') // remove %empty
.replace(/\/\/.*$/g, ' ') // remove line comments
.trim();
const m = cleaned.match(/^([a-zA-Z_][a-zA-Z0-9_.]*)/);
return m ? m[1] : undefined;
}
function parseTokenNames(text: string, type: string | undefined, lineNum: number, doc: BisonDocument, colOffset: number = 0): void {
// Bison token declaration syntax: NAME [NUMBER] ["alias"] (repeating)
// The optional NUMBER comes BEFORE the optional "alias".
// We use a character scanner to correctly skip string literals and numeric values
// so that words inside "end of file" are not mistaken for token names.
let pos = 0;
while (pos < text.length) {
// Skip whitespace
while (pos < text.length && (text[pos] === ' ' || text[pos] === '\t')) pos++;
if (pos >= text.length) break;
const ch = text[pos];
// Skip string literals (these are aliases for the previous token, not new token names)
if (ch === '"') {
pos++;
while (pos < text.length && text[pos] !== '"') {
if (text[pos] === '\\') pos++; // skip escaped character
pos++;
}
pos++; // skip closing quote
continue;
}
// Skip numeric token values
if (ch >= '0' && ch <= '9') {
while (pos < text.length && text[pos] >= '0' && text[pos] <= '9') pos++;
continue;
}
// Match identifier (token name)
if (/[a-zA-Z_]/.test(ch)) {
const nameStart = pos;
while (pos < text.length && /[a-zA-Z0-9_]/.test(text[pos])) pos++;
const name = text.substring(nameStart, pos);
const col = colOffset + nameStart;
// Peek ahead: optional NUMBER then optional "alias"
let peekPos = pos;
let alias: string | undefined;
let value: number | undefined;
// Skip whitespace
while (peekPos < text.length && (text[peekPos] === ' ' || text[peekPos] === '\t')) peekPos++;
// Optional numeric token code (e.g. %token TOKEN_EOF 0 "end of file")
if (peekPos < text.length && text[peekPos] >= '0' && text[peekPos] <= '9') {
const numStart = peekPos;
while (peekPos < text.length && text[peekPos] >= '0' && text[peekPos] <= '9') peekPos++;
value = parseInt(text.substring(numStart, peekPos), 10);
pos = peekPos;
while (peekPos < text.length && (text[peekPos] === ' ' || text[peekPos] === '\t')) peekPos++;
}
// Optional string alias (e.g. %token PLUS "+" or %token TOKEN_EOF 0 "end of file")
if (peekPos < text.length && text[peekPos] === '"') {
peekPos++; // skip opening quote
const aliasStart = peekPos;
while (peekPos < text.length && text[peekPos] !== '"') {
if (text[peekPos] === '\\') peekPos++; // skip escaped character
peekPos++;
}
alias = text.substring(aliasStart, peekPos);
peekPos++; // skip closing quote
pos = peekPos;
}
doc.tokens.set(name, {
name,
type,
alias,
location: Range.create(lineNum, col, lineNum, col + name.length),
value,
});
continue;
}
// Skip any other character (e.g. punctuation, stray closing >)
pos++;
}
}
/**
* Scan the inline action block(s) on a single line for $n references.
* Uses a brace-depth scanner so that $n references appearing after a nested
* sub-block (e.g. `{ if (x) { foo(); } $$ = $1; }`) are not missed.
* Only handles single-line { ... } blocks; multi-line actions are tracked by
* the caller (Phase 3 loop in parseBisonDocument).
* $$ and $<type>n are deliberately skipped.
*/
function extractDollarRefs(text: string, lineNum: number, fullLine: string): DollarRef[] {
const refs: DollarRef[] = [];
let i = 0;
while (i < text.length) {
if (text[i] !== '{') { i++; continue; }
// Found the opening brace of an action block — scan to the matching '}'
let depth = 1;
let j = i + 1;
while (j < text.length && depth > 0) {
if (text[j] === '{') depth++;
else if (text[j] === '}') depth--;
j++;
}
// text[i+1 .. j-2] is the full content of this balanced action block
const actionContent = text.substring(i + 1, j - 1);
const dollarRegex = /\$(\d+)/g;
let m: RegExpExecArray | null;
while ((m = dollarRegex.exec(actionContent)) !== null) {
const n = parseInt(m[1], 10);
const fullMatch = '$' + m[1];
// Find the column in the original line (search after the opening brace)
const braceInLine = fullLine.indexOf('{');
const col = fullLine.indexOf(fullMatch, braceInLine >= 0 ? braceInLine : 0);
refs.push({
n,
range: Range.create(lineNum, col >= 0 ? col : 0, lineNum, (col >= 0 ? col : 0) + fullMatch.length),
});
}
i = j; // advance past the entire balanced block
}
return refs;
}
function extractRuleReferences(text: string, lineNum: number, fullLine: string, doc: BisonDocument): void {
// Track string literals used as token aliases in rule bodies (e.g. "+" instead of PLUS,
// "{" instead of LBRACE). We use a char-by-char scanner so that:
// • `"{"` at brace-depth 0 → alias `{` (rule body)
// • `"{"` inside `{ std::string s = "{"; }` → ignored (brace-depth > 0, action block)
{
let braceDepth = 0;
let inString = false;
let strStart = -1;
for (let ci = 0; ci < text.length; ci++) {
const ch = text[ci];
if (inString) {
if (ch === '\\') { ci++; continue; } // escape: skip next char
if (ch === '"') {
const alias = text.substring(strStart, ci); // content between quotes
if (alias) {
const rawStr = '"' + alias + '"';
const col = fullLine.indexOf(rawStr);
if (!doc.ruleReferences.has(alias)) doc.ruleReferences.set(alias, []);
doc.ruleReferences.get(alias)!.push(
Range.create(lineNum, col >= 0 ? col : 0, lineNum, (col >= 0 ? col : 0) + rawStr.length),
);
}
inString = false;
}
} else {
if (ch === '{') { braceDepth++; }
else if (ch === '}') { braceDepth = Math.max(0, braceDepth - 1); }
else if (ch === '"' && braceDepth === 0) { inString = true; strStart = ci + 1; }
}
}
}
// Find identifiers in rule bodies (potential token/nonterminal references)
// Skip: strings, actions (braces), %prec keyword (but keep its token), %empty, comments
const cleaned = removeBalancedBraces(text.replace(/"(?:[^"\\]|\\.)*"/g, '')) // remove strings, then inline actions
.replace(/%prec/g, '') // remove %prec keyword (keep the token name)
.replace(/%empty/g, '') // remove %empty
.replace(/\/\/.*$/g, ''); // remove line comments
const identifiers = cleaned.matchAll(/\b([a-zA-Z_][a-zA-Z0-9_.]*)\b/g);
for (const m of identifiers) {
const name = m[0];
if (!name) continue;
const col = fullLine.indexOf(name, m.index);
const range = Range.create(lineNum, col >= 0 ? col : 0, lineNum, (col >= 0 ? col : 0) + name.length);
if (!doc.ruleReferences.has(name)) {
doc.ruleReferences.set(name, []);
}
doc.ruleReferences.get(name)!.push(range);
}
}