|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { window, workspace, Uri, Position, Range, ViewColumn } from 'vscode'; |
| 4 | + |
| 5 | +// ── Detection ───────────────────────────────────────────────────────────────── |
| 6 | + |
| 7 | +/** Returns true if the file header looks like a Bison or Flex generated C file. */ |
| 8 | +export function isGeneratedFile(text: string): boolean { |
| 9 | + const header = text.slice(0, 600); |
| 10 | + return ( |
| 11 | + header.includes('/* A Bison parser, made by GNU Bison') || |
| 12 | + header.includes('/* Generated by GNU Bison') || |
| 13 | + header.includes('/* Generated by flex') || |
| 14 | + header.includes('/* A lexical scanner generated by flex') |
| 15 | + ); |
| 16 | +} |
| 17 | + |
| 18 | +// ── Direction 1: generated → source ────────────────────────────────────────── |
| 19 | + |
| 20 | +interface LineDirective { |
| 21 | + sourceLine: number; // 1-based line number in the source file |
| 22 | + sourceFile: string; |
| 23 | +} |
| 24 | + |
| 25 | +/** Scan backwards from cursorLine to find the nearest #line N "file" directive. */ |
| 26 | +function findNearestLineDirective(lines: string[], cursorLine: number): LineDirective | null { |
| 27 | + for (let i = cursorLine; i >= 0; i--) { |
| 28 | + const m = lines[i].match(/^#line\s+(\d+)\s+"([^"]+)"/); |
| 29 | + if (m) { |
| 30 | + return { sourceLine: parseInt(m[1], 10), sourceFile: m[2] }; |
| 31 | + } |
| 32 | + } |
| 33 | + return null; |
| 34 | +} |
| 35 | + |
| 36 | +/** Navigate from the generated C file at cursorLine to the original grammar source. */ |
| 37 | +export async function showInSource(): Promise<void> { |
| 38 | + const editor = window.activeTextEditor; |
| 39 | + if (!editor) return; |
| 40 | + |
| 41 | + const text = editor.document.getText(); |
| 42 | + |
| 43 | + if (!isGeneratedFile(text)) { |
| 44 | + window.showWarningMessage('This command is only available inside Bison or Flex generated files.'); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + const lines = text.split('\n'); |
| 49 | + const cursorLine = editor.selection.active.line; |
| 50 | + const directive = findNearestLineDirective(lines, cursorLine); |
| 51 | + |
| 52 | + if (!directive) { |
| 53 | + window.showWarningMessage('No #line directive found above the cursor.'); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + // Resolve path: may be absolute or relative to the generated file's directory |
| 58 | + let sourcePath = directive.sourceFile.replace(/\\\\/g, '/').replace(/\\/g, '/'); |
| 59 | + if (!path.isAbsolute(sourcePath)) { |
| 60 | + sourcePath = path.resolve(path.dirname(editor.document.uri.fsPath), sourcePath); |
| 61 | + } |
| 62 | + |
| 63 | + if (!fs.existsSync(sourcePath)) { |
| 64 | + const baseName = path.basename(sourcePath); |
| 65 | + const found = await findInWorkspace(baseName); |
| 66 | + if (!found) { |
| 67 | + window.showErrorMessage(`Source file not found: ${sourcePath}`); |
| 68 | + return; |
| 69 | + } |
| 70 | + sourcePath = found; |
| 71 | + } |
| 72 | + |
| 73 | + const targetLine = Math.max(0, directive.sourceLine - 1); // convert to 0-based |
| 74 | + const doc = await workspace.openTextDocument(Uri.file(sourcePath)); |
| 75 | + const pos = new Position(targetLine, 0); |
| 76 | + await window.showTextDocument(doc, { selection: new Range(pos, pos), viewColumn: ViewColumn.Active }); |
| 77 | +} |
| 78 | + |
| 79 | +// ── Direction 2: source → generated ────────────────────────────────────────── |
| 80 | + |
| 81 | +const BISON_CANDIDATES = (base: string, dir: string): string[] => [ |
| 82 | + path.join(dir, base + '.tab.c'), |
| 83 | + path.join(dir, base + '.tab.cpp'), |
| 84 | + path.join(dir, base + '.tab.cc'), |
| 85 | +]; |
| 86 | + |
| 87 | +const FLEX_CANDIDATES = (base: string, dir: string): string[] => [ |
| 88 | + path.join(dir, 'lex.yy.c'), |
| 89 | + path.join(dir, 'lex.yy.cc'), |
| 90 | + path.join(dir, base + '.yy.c'), |
| 91 | + path.join(dir, base + '.yy.cpp'), |
| 92 | +]; |
| 93 | + |
| 94 | +/** Scan CMakeLists.txt up the directory tree and return a build directory hint. */ |
| 95 | +function findCmakeBuildDir(sourceFilePath: string): string | undefined { |
| 96 | + const fileName = path.basename(sourceFilePath); |
| 97 | + let dir = path.dirname(sourceFilePath); |
| 98 | + |
| 99 | + for (let depth = 0; depth < 6; depth++) { |
| 100 | + const cmakePath = path.join(dir, 'CMakeLists.txt'); |
| 101 | + if (fs.existsSync(cmakePath)) { |
| 102 | + try { |
| 103 | + const content = fs.readFileSync(cmakePath, 'utf-8').replace(/#[^\n]*/g, ''); |
| 104 | + // Only proceed if this CMakeLists references our file |
| 105 | + if (!content.includes(fileName)) break; |
| 106 | + const re = /(?:BISON_TARGET|FLEX_TARGET)\s*\(\s*\w+\s+\S+\s+(\S+)/gi; |
| 107 | + let m: RegExpExecArray | null; |
| 108 | + while ((m = re.exec(content)) !== null) { |
| 109 | + const outputArg = m[1].replace(/^["']|["']$/g, ''); |
| 110 | + // Strip CMake variables (e.g. ${CMAKE_CURRENT_BINARY_DIR}) |
| 111 | + const stripped = outputArg.replace(/\$\{[^}]+\}\/?/g, '').trim(); |
| 112 | + if (stripped) { |
| 113 | + return path.dirname(path.join(dir, stripped)); |
| 114 | + } |
| 115 | + // If the output arg is purely a CMake variable (e.g. ${CMAKE_CURRENT_BINARY_DIR}/foo.tab.c), |
| 116 | + // try the standard build subdirectory convention |
| 117 | + return path.join(dir, 'build'); |
| 118 | + } |
| 119 | + } catch { |
| 120 | + // ignore unreadable files |
| 121 | + } |
| 122 | + } |
| 123 | + const parent = path.dirname(dir); |
| 124 | + if (parent === dir) break; |
| 125 | + dir = parent; |
| 126 | + } |
| 127 | + return undefined; |
| 128 | +} |
| 129 | + |
| 130 | +/** Scan Makefile for generated output paths. */ |
| 131 | +function findMakefileBuildDir(sourceFilePath: string): string | undefined { |
| 132 | + const dir = path.dirname(sourceFilePath); |
| 133 | + const base = path.basename(sourceFilePath, path.extname(sourceFilePath)); |
| 134 | + |
| 135 | + for (const name of ['Makefile', 'makefile', 'GNUmakefile']) { |
| 136 | + const mkPath = path.join(dir, name); |
| 137 | + if (!fs.existsSync(mkPath)) continue; |
| 138 | + try { |
| 139 | + const content = fs.readFileSync(mkPath, 'utf-8'); |
| 140 | + // Look for lines like: build/parser.tab.c or obj/lex.yy.c |
| 141 | + const re = new RegExp(`([^\\s:]+[\\/\\\\])?${base}\\.tab\\.[ch]|lex\\.yy\\.c`, 'g'); |
| 142 | + const m = re.exec(content); |
| 143 | + if (m && m[1]) { |
| 144 | + return path.resolve(dir, m[1].replace(/[\\/]$/, '')); |
| 145 | + } |
| 146 | + } catch { |
| 147 | + // ignore |
| 148 | + } |
| 149 | + } |
| 150 | + return undefined; |
| 151 | +} |
| 152 | + |
| 153 | +/** Locate the generated file corresponding to a grammar source file. */ |
| 154 | +async function findGeneratedFile(sourceFilePath: string): Promise<string | null> { |
| 155 | + const config = workspace.getConfiguration('bisonFlex'); |
| 156 | + const settingBuildDir = config.get<string>('buildDirectory', '').trim() || undefined; |
| 157 | + const sourceDir = path.dirname(sourceFilePath); |
| 158 | + const base = path.basename(sourceFilePath, path.extname(sourceFilePath)); |
| 159 | + const ext = path.extname(sourceFilePath).toLowerCase(); |
| 160 | + const isBison = ['.y', '.yy', '.ypp', '.bison'].includes(ext); |
| 161 | + const candidates = isBison ? BISON_CANDIDATES : FLEX_CANDIDATES; |
| 162 | + |
| 163 | + const dirsToTry: string[] = []; |
| 164 | + if (settingBuildDir) dirsToTry.push(settingBuildDir); |
| 165 | + const cmakeDir = findCmakeBuildDir(sourceFilePath); |
| 166 | + if (cmakeDir) dirsToTry.push(cmakeDir); |
| 167 | + const makeDir = findMakefileBuildDir(sourceFilePath); |
| 168 | + if (makeDir) dirsToTry.push(makeDir); |
| 169 | + dirsToTry.push(sourceDir); |
| 170 | + |
| 171 | + for (const dir of dirsToTry) { |
| 172 | + for (const c of candidates(base, dir)) { |
| 173 | + if (fs.existsSync(c)) return c; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + // Workspace-wide search as last resort — let user pick if ambiguous |
| 178 | + const pattern = isBison ? `**/${base}.tab.{c,cpp,cc}` : `**/lex.yy.{c,cc}`; |
| 179 | + const found = await workspace.findFiles(pattern, '**/node_modules/**', 10); |
| 180 | + if (found.length === 0) return null; |
| 181 | + if (found.length === 1) return found[0].fsPath; |
| 182 | + |
| 183 | + const pick = await window.showQuickPick( |
| 184 | + found.map(u => ({ label: workspace.asRelativePath(u), fsPath: u.fsPath })), |
| 185 | + { placeHolder: 'Multiple generated files found — select one' } |
| 186 | + ); |
| 187 | + return pick ? pick.fsPath : null; |
| 188 | +} |
| 189 | + |
| 190 | +/** |
| 191 | + * In the generated file, find the output line that corresponds to `sourceLine` (1-based) |
| 192 | + * in `sourceFilePath`. Returns the 0-based line index in the generated file. |
| 193 | + */ |
| 194 | +function findLineInGenerated(generatedLines: string[], sourceFilePath: string, sourceLine: number): number | null { |
| 195 | + const sourceBase = path.basename(sourceFilePath).toLowerCase(); |
| 196 | + let bestGenLine = -1; |
| 197 | + let bestSrcLine = -1; |
| 198 | + |
| 199 | + for (let i = 0; i < generatedLines.length; i++) { |
| 200 | + const m = generatedLines[i].match(/^#line\s+(\d+)\s+"([^"]+)"/); |
| 201 | + if (!m) continue; |
| 202 | + const dirSrcLine = parseInt(m[1], 10); |
| 203 | + const dirFile = path.basename(m[2]).toLowerCase(); |
| 204 | + if (dirFile !== sourceBase) continue; |
| 205 | + if (dirSrcLine <= sourceLine) { |
| 206 | + bestGenLine = i; |
| 207 | + bestSrcLine = dirSrcLine; |
| 208 | + } else { |
| 209 | + break; |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + if (bestGenLine === -1) return null; |
| 214 | + return bestGenLine + (sourceLine - bestSrcLine); |
| 215 | +} |
| 216 | + |
| 217 | +/** Navigate from a .y/.l source line to the corresponding line in the generated C file. */ |
| 218 | +export async function showInGenerated(): Promise<void> { |
| 219 | + const editor = window.activeTextEditor; |
| 220 | + if (!editor) return; |
| 221 | + |
| 222 | + const sourceFilePath = editor.document.uri.fsPath; |
| 223 | + const sourceLine = editor.selection.active.line + 1; // 1-based |
| 224 | + |
| 225 | + const generatedPath = await findGeneratedFile(sourceFilePath); |
| 226 | + if (!generatedPath) { |
| 227 | + window.showErrorMessage( |
| 228 | + 'Generated file not found. Compile the grammar first, or set bisonFlex.buildDirectory in settings.' |
| 229 | + ); |
| 230 | + return; |
| 231 | + } |
| 232 | + |
| 233 | + let generatedText: string; |
| 234 | + try { |
| 235 | + generatedText = fs.readFileSync(generatedPath, 'utf-8'); |
| 236 | + } catch { |
| 237 | + window.showErrorMessage(`Cannot read generated file: ${generatedPath}`); |
| 238 | + return; |
| 239 | + } |
| 240 | + |
| 241 | + const generatedLines = generatedText.split('\n'); |
| 242 | + const targetLine = findLineInGenerated(generatedLines, sourceFilePath, sourceLine); |
| 243 | + const pos = new Position(targetLine !== null ? targetLine : 0, 0); |
| 244 | + const doc = await workspace.openTextDocument(Uri.file(generatedPath)); |
| 245 | + await window.showTextDocument(doc, { selection: new Range(pos, pos), viewColumn: ViewColumn.Beside }); |
| 246 | + |
| 247 | + if (targetLine === null) { |
| 248 | + window.showWarningMessage( |
| 249 | + `Opened ${path.basename(generatedPath)}, but could not locate the exact line for source line ${sourceLine}.` |
| 250 | + ); |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | +// ── Helpers ─────────────────────────────────────────────────────────────────── |
| 255 | + |
| 256 | +async function findInWorkspace(baseName: string): Promise<string | null> { |
| 257 | + const found = await workspace.findFiles(`**/${baseName}`, '**/node_modules/**', 5); |
| 258 | + return found.length > 0 ? found[0].fsPath : null; |
| 259 | +} |
0 commit comments