Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | /* * This file is released under the MIT license. * Copyright (c) 2016, 2022, Mike Lischke * * See LICENSE file for more info. */ import * as fs from "fs"; import * as path from "path"; import { Vocabulary } from "antlr4ts"; export interface IContextEntry { context: SourceContext; refCount: number; dependencies: string[]; grammar: string; // The grammar file name. } // Import modules that depend on these enums after their definition, to allow for static initializations. import { SourceContext } from "./SourceContext"; import { GrammarDebugger } from "./GrammarDebugger"; import { ISelfDiagnostics, ISymbolInfo, CodeActionType, IDiagnosticEntry, IReferenceNode, IGenerationOptions, ISentenceGenerationOptions, IFormattingOptions, IContextDetails, } from "./types"; import { IATNGraphData } from "../webview-scripts/types"; export class AntlrFacade { // Mapping file names to SourceContext instances. private sourceContexts: Map<string, IContextEntry> = new Map<string, IContextEntry>(); public constructor(private importDir: string, private extensionDir: string) { } /** * Info for unit tests. * * @returns An object with interesting details (currently only the number of existing contexts). */ public getSelfDiagnostics(): ISelfDiagnostics { return { contextCount: this.sourceContexts.keys.length, }; } public getContext(fileName: string, source?: string | undefined): SourceContext { const contextEntry = this.sourceContexts.get(fileName); Iif (!contextEntry) { return this.loadGrammar(fileName, source); } return contextEntry.context; } /** * Call this to refresh the internal input stream as a preparation to a reparse call * or for code completion. * Does nothing if no grammar has been loaded for that file name. * * @param fileName The grammar file name. * @param source The grammar code. */ public setText(fileName: string, source: string): void { const contextEntry = this.sourceContexts.get(fileName); Iif (contextEntry) { contextEntry.context.setText(source); } } /** * Triggers a parse run for the given file name. This grammar must have been loaded before. * * @param fileName The grammar file name. */ public reparse(fileName: string): void { const contextEntry = this.sourceContexts.get(fileName); Iif (contextEntry) { this.parseGrammar(contextEntry); } } public loadGrammar(fileName: string, source?: string): SourceContext { let contextEntry = this.sourceContexts.get(fileName); Iif (!contextEntry) { Iif (!source) { try { fs.statSync(fileName); source = fs.readFileSync(fileName, "utf8"); } catch (e) { source = ""; } } const context = new SourceContext(fileName, this.extensionDir); contextEntry = { context, refCount: 0, dependencies: [], grammar: fileName }; this.sourceContexts.set(fileName, contextEntry); // Do an initial parse run and load all dependencies of this context // and pass their references to this context. context.setText(source); this.parseGrammar(contextEntry); } contextEntry.refCount++; return contextEntry.context; } public releaseGrammar(fileName: string): void { this.internalReleaseGrammar(fileName); } public symbolInfoAtPosition(fileName: string, column: number, row: number, limitToChildren = true): ISymbolInfo | undefined { const context = this.getContext(fileName); return context.symbolAtPosition(column, row, limitToChildren); } public infoForSymbol(fileName: string, symbol: string): ISymbolInfo | undefined { const context = this.getContext(fileName); return context.getSymbolInfo(symbol); } public enclosingSymbolAtPosition(fileName: string, column: number, row: number, ruleScope = false): ISymbolInfo | undefined { const context = this.getContext(fileName); return context.enclosingSymbolAtPosition(column, row, ruleScope); } /** * Returns a list of top level symbols from a file (and optionally its dependencies). * * @param fileName The grammar file name. * @param fullList If true, includes symbols from all dependencies as well. * @returns A list of symbol info entries. */ public listTopLevelSymbols(fileName: string, fullList: boolean): ISymbolInfo[] { const context = this.getContext(fileName); return context.listTopLevelSymbols(!fullList); } /** * Returns the vocabulary for the given file (if it contains lexer rules). * * @param fileName The grammar file name. * @returns The vocabulary if found. */ public getLexerVocabulary(fileName: string): Vocabulary | undefined { const context = this.getContext(fileName); return context.getVocabulary(); } /** * Returns a list of rule names for the given file (if it contains parser rules). * * @param fileName The grammar file name. * @returns The list of rule names. */ public getRuleList(fileName: string): string[] | undefined { const context = this.getContext(fileName); return context.getRuleList(); } /** * Returns a list of channel names for the given file (if it contains lexer rules). * * @param fileName The grammar file name. * @returns The list of channel names. */ public getChannels(fileName: string): string[] | undefined { const context = this.getContext(fileName); return context.getChannels(); } /** * Returns a list of lexer modes for the given file (if it contains lexer rules). * * @param fileName The grammar file name. * @returns The list of mode names. */ public getModes(fileName: string): string[] | undefined { const context = this.getContext(fileName); return context.getModes(); } /** * Returns a list of actions found in the given file. * * @param fileName The grammar file name. * @param type The of actions to return. * @returns The list of actions. */ public listActions(fileName: string, type: CodeActionType): ISymbolInfo[] { const context = this.getContext(fileName); return context.listActions(type); } public getActionCounts(fileName: string): Map<CodeActionType, number> { const context = this.getContext(fileName); return context.getActionCounts(); } public async getCodeCompletionCandidates(fileName: string, column: number, row: number): Promise<ISymbolInfo[]> { const context = this.getContext(fileName); return context.getCodeCompletionCandidates(column, row); } public getDiagnostics(fileName: string): IDiagnosticEntry[] { const context = this.getContext(fileName); return context.getDiagnostics(); } public ruleFromPosition(fileName: string, column: number, row: number): [string | undefined, number | undefined] { const context = this.getContext(fileName); return context.ruleFromPosition(column, row); } /** * Count how many times a symbol has been referenced. The given file must contain the definition of this symbol. * * @param fileName The grammar file name. * @param symbol The symbol for which to determine the reference count. * @returns The reference count. */ public countReferences(fileName: string, symbol: string): number { const context = this.getContext(fileName); return context.getReferenceCount(symbol); } /** * Determines source file and position of all occurrences of the given symbol. The search includes * also all referencing and referenced contexts. * * @param fileName The grammar file name. * @param symbolName The name of the symbol to check. * @returns A list of symbol info entries, each describing one occurrence. */ public getSymbolOccurrences(fileName: string, symbolName: string): ISymbolInfo[] { const context = this.getContext(fileName); const result = context.symbolTable.getSymbolOccurrences(symbolName, false); // Sort result by kind. This way rule definitions appear before rule references and are re-parsed first. return result.sort((lhs: ISymbolInfo, rhs: ISymbolInfo) => { return lhs.kind - rhs.kind; }); } public getDependencies(fileName: string): string[] { const entry = this.sourceContexts.get(fileName); Iif (!entry) { return []; } const dependencies: Set<SourceContext> = new Set(); this.pushDependencyFiles(entry, dependencies); const result: string[] = []; for (const dep of dependencies) { result.push(dep.fileName); } return result; } public getReferenceGraph(fileName: string): Map<string, IReferenceNode> { const context = this.getContext(fileName); return context.getReferenceGraph(); } public getRRDScript(fileName: string, rule: string): string { const context = this.getContext(fileName); return context.getRRDScript(rule) || ""; } public generate(fileName: string, options: IGenerationOptions): Promise<string[]> { const context = this.getContext(fileName); const dependencies: Set<SourceContext> = new Set(); this.pushDependencyFiles(this.sourceContexts.get(fileName)!, dependencies); return context.generate(dependencies, options); } public getATNGraph(fileName: string, rule: string): IATNGraphData | undefined { const context = this.getContext(fileName); return context.getATNGraph(rule); } public generateSentence(fileName: string, rule: string, options: ISentenceGenerationOptions, callback: (sentence: string, index: number) => void): void { const context = this.getContext(fileName); const dependencies = new Set<SourceContext>(); this.pushDependencyFiles(this.sourceContexts.get(fileName)!, dependencies); const basePath = path.dirname(fileName); for (const dependency of dependencies) { Iif (dependency.hasErrors) { callback("[Fix grammar errors first]", 0); return; } Iif (!dependency.isInterpreterDataLoaded) { const errors = dependency.setupInterpreters(path.join(basePath, ".antlr")); Iif (errors.length > 0) { callback(errors, 0); } } } context.generateSentence(dependencies, rule, options, callback); } public lexTestInput(fileName: string, input: string, actionFile?: string): [string[], string] { const context = this.getContext(fileName); return context.lexTestInput(input, actionFile); } public parseTestInput(fileName: string, input: string, startRule: string, actionFile?: string): string[] { const context = this.getContext(fileName); return context.parseTestInput(input, startRule, actionFile); } public formatGrammar(fileName: string, options: IFormattingOptions, start: number, stop: number): [string, number, number] { const context = this.getContext(fileName); return context.formatGrammar(options, start, stop); } public hasErrors(fileName: string): boolean { const context = this.getContext(fileName); return context.hasErrors; } public createDebugger(fileName: string, actionFile: string, dataDir: string): GrammarDebugger | undefined { const context = this.getContext(fileName); Iif (!context) { return; } const contexts: Set<SourceContext> = new Set(); contexts.add(context); this.pushDependencyFiles(this.sourceContexts.get(fileName)!, contexts); for (const dependency of contexts) { Iif (dependency.hasErrors) { return; } Iif (!dependency.isInterpreterDataLoaded) { const errors = dependency.setupInterpreters(dataDir); Iif (errors.length > 0) { return; } } } return new GrammarDebugger([...contexts], actionFile); } public getContextDetails(fileName: string): IContextDetails { const context = this.getContext(fileName); return context.info; } private loadDependency(contextEntry: IContextEntry, depName: string): SourceContext | undefined { // The given import dir is used to locate the dependency (either relative to the base path or via an // absolute path). // If we cannot find the grammar file that way we try the base folder. const basePath = path.dirname(contextEntry.grammar); const fullPath = path.isAbsolute(this.importDir) ? this.importDir : path.join(basePath, this.importDir); try { const depPath = path.join(fullPath, depName + ".g4"); fs.accessSync(depPath, fs.constants.R_OK); // Target path can be read. Now check the target file. contextEntry.dependencies.push(depPath); return this.loadGrammar(depPath); } catch (e) { // ignore } // File not found. Try other extension. try { const depPath = path.join(fullPath, depName + ".g"); fs.accessSync(depPath, fs.constants.R_OK); // Target path can be read. Now check the target file. contextEntry.dependencies.push(depPath); return this.loadGrammar(depPath); } catch (e) { // ignore } // Couldn't find it in the import folder. Use the base then. try { const depPath = path.join(basePath, depName + ".g4"); fs.statSync(depPath); contextEntry.dependencies.push(depPath); return this.loadGrammar(depPath); } catch (e) { // ignore } try { const depPath = path.join(basePath, depName + ".g"); fs.statSync(depPath); contextEntry.dependencies.push(depPath); return this.loadGrammar(depPath); } catch (e) { // ignore } // Ignore the dependency if we cannot find the source file for it. return undefined; } private parseGrammar(contextEntry: IContextEntry) { const oldDependencies = contextEntry.dependencies; contextEntry.dependencies = []; const newDependencies = contextEntry.context.parse(); for (const dep of newDependencies) { const depContext = this.loadDependency(contextEntry, dep); Iif (depContext) { contextEntry.context.addAsReferenceTo(depContext); } } // Release all old dependencies. This will only unload grammars which have // not been ref-counted by the above dependency loading (or which are not used by other // grammars). for (const dep of oldDependencies) { this.releaseGrammar(dep); } } private internalReleaseGrammar(fileName: string, referencing?: IContextEntry): void { const contextEntry = this.sourceContexts.get(fileName); Iif (contextEntry) { Iif (referencing) { // If a referencing context is given remove this one from the reference's dependencies list, // which in turn will remove the referencing context from the dependency's referencing list. referencing.context.removeDependency(contextEntry.context); } contextEntry.refCount--; Iif (contextEntry.refCount === 0) { this.sourceContexts.delete(fileName); // Release also all dependencies. for (const dep of contextEntry.dependencies) { this.internalReleaseGrammar(dep, contextEntry); } } } } private pushDependencyFiles(entry: IContextEntry, contexts: Set<SourceContext>) { // Using a set for the context list here, to automatically exclude duplicates. for (const dep of entry.dependencies) { const depEntry = this.sourceContexts.get(dep); Iif (depEntry) { this.pushDependencyFiles(depEntry, contexts); contexts.add(depEntry.context); } } } } |