|
| 1 | +import { InlayHint, InlayHintKind, Position, Range } from 'vscode-languageserver'; |
| 2 | +import { TextDocument } from 'vscode-languageserver-textdocument'; |
| 3 | +import { DocumentModel, BisonDocument, isBisonDocument } from '../parser/types'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Provide inlay hints showing the inferred type of $1, $2, $$, etc. |
| 7 | + * Only applies to Bison files where %type / %token declare types. |
| 8 | + */ |
| 9 | +export function getInlayHints( |
| 10 | + doc: DocumentModel, |
| 11 | + textDoc: TextDocument, |
| 12 | + range: Range |
| 13 | +): InlayHint[] { |
| 14 | + // Inlay hints only make sense for Bison (typed semantic values $N) |
| 15 | + if (!isBisonDocument(doc)) return []; |
| 16 | + |
| 17 | + return getBisonInlayHints(doc, textDoc, range); |
| 18 | +} |
| 19 | + |
| 20 | +interface LineContext { |
| 21 | + ruleName: string; |
| 22 | + symbols: string[]; |
| 23 | +} |
| 24 | + |
| 25 | +function getBisonInlayHints(doc: BisonDocument, textDoc: TextDocument, range: Range): InlayHint[] { |
| 26 | + const hints: InlayHint[] = []; |
| 27 | + const text = textDoc.getText(); |
| 28 | + const lines = text.split(/\r?\n/); |
| 29 | + |
| 30 | + const rulesStart = doc.separators.length > 0 ? doc.separators[0] + 1 : lines.length; |
| 31 | + const rulesEnd = doc.separators.length > 1 ? doc.separators[1] : lines.length; |
| 32 | + |
| 33 | + // Phase 1: Build a map of line → { ruleName, ordered symbols } for the rules section. |
| 34 | + // We walk through rule definitions and track which alternative each line belongs to. |
| 35 | + let currentRuleName: string | undefined; |
| 36 | + let currentSymbols: string[] = []; |
| 37 | + let braceDepth = 0; |
| 38 | + const lineContext = new Map<number, LineContext>(); |
| 39 | + |
| 40 | + for (let i = rulesStart; i < rulesEnd; i++) { |
| 41 | + const line = lines[i]; |
| 42 | + const trimmed = line.trim(); |
| 43 | + |
| 44 | + if (!trimmed || trimmed.startsWith('//') || trimmed.startsWith('/*')) continue; |
| 45 | + |
| 46 | + if (braceDepth === 0) { |
| 47 | + // Rule definition: name : |
| 48 | + const ruleDefMatch = trimmed.match(/^([a-zA-Z_][a-zA-Z0-9_.]*)\s*:/); |
| 49 | + if (ruleDefMatch) { |
| 50 | + currentRuleName = ruleDefMatch[1]; |
| 51 | + const rest = trimmed.substring(ruleDefMatch[0].length); |
| 52 | + currentSymbols = extractOrderedSymbols(rest); |
| 53 | + } else if (trimmed.startsWith('|') && currentRuleName) { |
| 54 | + // New alternative |
| 55 | + currentSymbols = extractOrderedSymbols(trimmed.slice(1)); |
| 56 | + } else if (currentRuleName && trimmed !== ';') { |
| 57 | + // Continuation line — may add more symbols before the action block |
| 58 | + const moreSymbols = extractOrderedSymbols(trimmed); |
| 59 | + if (moreSymbols.length > 0) { |
| 60 | + currentSymbols = [...currentSymbols, ...moreSymbols]; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if (trimmed === ';') { |
| 65 | + currentRuleName = undefined; |
| 66 | + currentSymbols = []; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Record context for this line (even inside action blocks, so $N can be resolved) |
| 71 | + if (currentRuleName) { |
| 72 | + lineContext.set(i, { ruleName: currentRuleName, symbols: [...currentSymbols] }); |
| 73 | + } |
| 74 | + |
| 75 | + // Track brace depth |
| 76 | + for (const ch of line) { |
| 77 | + if (ch === '{') braceDepth++; |
| 78 | + if (ch === '}') braceDepth = Math.max(0, braceDepth - 1); |
| 79 | + } |
| 80 | + |
| 81 | + if (trimmed === ';' && braceDepth === 0) { |
| 82 | + currentRuleName = undefined; |
| 83 | + currentSymbols = []; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Phase 2: Scan the requested range for $N / $$ patterns and resolve types. |
| 88 | + const startLine = Math.max(range.start.line, rulesStart); |
| 89 | + const endLine = Math.min(range.end.line, rulesEnd - 1); |
| 90 | + |
| 91 | + for (let i = startLine; i <= endLine; i++) { |
| 92 | + const line = lines[i]; |
| 93 | + if (!line) continue; |
| 94 | + |
| 95 | + const ctx = lineContext.get(i); |
| 96 | + if (!ctx) continue; |
| 97 | + |
| 98 | + // Find all $N and $$ occurrences |
| 99 | + const dollarMatches = line.matchAll(/\$(\$|\d+)/g); |
| 100 | + for (const m of dollarMatches) { |
| 101 | + const value = m[1]; |
| 102 | + const col = m.index!; |
| 103 | + |
| 104 | + let type: string | undefined; |
| 105 | + |
| 106 | + if (value === '$') { |
| 107 | + // $$ → type of the rule's own non-terminal (LHS) |
| 108 | + type = resolveSymbolType(doc, ctx.ruleName); |
| 109 | + } else { |
| 110 | + const n = parseInt(value); |
| 111 | + if (n >= 1 && n <= ctx.symbols.length) { |
| 112 | + const symbol = ctx.symbols[n - 1]; |
| 113 | + type = resolveSymbolType(doc, symbol); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if (type) { |
| 118 | + hints.push({ |
| 119 | + position: Position.create(i, col + m[0].length), |
| 120 | + label: `/* <${type}> */`, |
| 121 | + kind: InlayHintKind.Type, |
| 122 | + paddingLeft: true, |
| 123 | + }); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return hints; |
| 129 | +} |
| 130 | + |
| 131 | +/** |
| 132 | + * Extract the ordered list of grammar symbols from a production body, |
| 133 | + * stopping at the first action block `{` or semicolon `;`. |
| 134 | + */ |
| 135 | +function extractOrderedSymbols(text: string): string[] { |
| 136 | + let cleaned = text |
| 137 | + .replace(/"(?:[^"\\]|\\.)*"/g, ' ') // remove double-quoted strings |
| 138 | + .replace(/'(?:[^'\\]|\\.)*'/g, ' ') // remove single-quoted char literals |
| 139 | + .replace(/%prec\s+\S+/g, ' ') // remove %prec TOKEN |
| 140 | + .replace(/%empty/g, ' ') // remove %empty |
| 141 | + .replace(/\/\/.*$/g, ' '); // remove line comments |
| 142 | + |
| 143 | + // Stop at first action block |
| 144 | + const braceIdx = cleaned.indexOf('{'); |
| 145 | + if (braceIdx >= 0) cleaned = cleaned.substring(0, braceIdx); |
| 146 | + |
| 147 | + // Stop at semicolon |
| 148 | + const semiIdx = cleaned.indexOf(';'); |
| 149 | + if (semiIdx >= 0) cleaned = cleaned.substring(0, semiIdx); |
| 150 | + |
| 151 | + const symbols: string[] = []; |
| 152 | + const matches = cleaned.matchAll(/\b([a-zA-Z_][a-zA-Z0-9_.]*)\b/g); |
| 153 | + for (const m of matches) { |
| 154 | + symbols.push(m[1]); |
| 155 | + } |
| 156 | + return symbols; |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Resolve a symbol name to its declared type (from %token or %type/%nterm). |
| 161 | + */ |
| 162 | +function resolveSymbolType(doc: BisonDocument, symbol: string): string | undefined { |
| 163 | + const token = doc.tokens.get(symbol); |
| 164 | + if (token?.type) return token.type; |
| 165 | + |
| 166 | + const nt = doc.nonTerminals.get(symbol); |
| 167 | + if (nt?.type) return nt.type; |
| 168 | + |
| 169 | + return undefined; |
| 170 | +} |
0 commit comments