UNPKG

antlr-ng

Version:

Next generation ANTLR Tool

117 lines (116 loc) 5.41 kB
import { CharStream } from "antlr4ng"; import { LogManager } from "./support/LogManager.js"; import { type IToolParameters } from "./tool-parameters.js"; import { ErrorManager } from "./tool/ErrorManager.js"; import type { Grammar } from "./tool/Grammar.js"; import { Rule } from "./tool/Rule.js"; import { GrammarAST } from "./tool/ast/GrammarAST.js"; import { GrammarRootAST } from "./tool/ast/GrammarRootAST.js"; import type { IGrammar, ITool } from "./types.js"; export declare class Tool implements ITool { logMgr: LogManager; readonly errorManager: ErrorManager; readonly toolParameters: IToolParameters; protected grammarFiles: string[]; private readonly importedGrammars; constructor(args?: string[]); static main(args: string[]): void; static generateInterpreterData(g: Grammar): string; /** * Manually get option node from tree. * * @param root The root of the grammar tree. * @param option The name of the option to find. * * @returns The option node or null if not found. */ private static findOptionValueAST; processGrammarsOnCommandLine(): void; /** * To process a grammar, we load all of its imported grammars into subordinate grammar objects. Then we merge the * imported rules into the root grammar. If a root grammar is a combined grammar, we have to extract the implicit * lexer. Once all this is done, we process the lexer first, if present, and then the parser grammar * * @param g The grammar to process. * @param genCode Whether to generate code or not. */ process(g: Grammar, genCode: boolean): void; processNonCombinedGrammar(g: Grammar, genCode: boolean): void; /** * Important enough to avoid multiple definitions that we do very early, right after AST construction. Also check * for undefined rules in parser/lexer to avoid exceptions later. Return true if we find multiple definitions of * the same rule or a reference to an undefined rule or parser rule ref in lexer rule. * * @param g The grammar to check. * * @returns true if there are issues with the rules. */ checkForRuleIssues(g: Grammar): boolean; sortGrammarByTokenVocab(fileNames: string[]): GrammarRootAST[]; /** * Given the raw AST of a grammar, create a grammar object associated with the AST. Once we have the grammar object, * ensure that all nodes in tree referred to this grammar. Later, we will use it for error handling and generally * knowing from where a rule comes from. * * @param grammarAST The raw AST of the grammar. * * @returns The grammar object. */ createGrammar(grammarAST: GrammarRootAST): IGrammar; parseGrammar(fileName: string): GrammarRootAST | undefined; /** * Convenience method to load and process an ANTLR grammar. Useful when creating interpreters. If you need to * access to the lexer grammar created while processing a combined grammar, use getImplicitLexer() on returned * grammar. * * @param fileName The name of the grammar file to load. * * @returns The grammar object. */ loadGrammar(fileName: string): Grammar; /** * Try current dir then dir of g then lib dir. * * @param g The grammar to import. * @param nameNode The node associated with the imported grammar name. * * @returns The imported grammar or null if not found. */ loadImportedGrammar(g: Grammar, nameNode: GrammarAST): Grammar | null; parseGrammarFromString(grammar: string): GrammarRootAST | undefined; parse(input: CharStream): GrammarRootAST | undefined; exportATNDotFiles(g: Grammar): void; /** * This method is used by all code generators to create new output files. If the outputDir set by -o is not present * it will be created. The final filename is sensitive to the output directory and the directory where the grammar * file was found. If -o is /tmp and the original grammar file was foo/t.g4 then output files go in /tmp/foo. * * The output dir -o spec takes precedence if it's absolute. E.g., if the grammar file dir is absolute the output * dir is given precedence. "-o /tmp /usr/lib/t.g4" results in "/tmp/T.java" as output (assuming t.g4 holds T.java). * * If no -o is specified, then just write to the directory where the grammar file was found. * * @param g The grammar for which we are generating a file. * @param fileName The name of the file to generate. * * @returns The full path to the output file. */ getOutputFile(g: Grammar, fileName: string): string; getImportedGrammarFile(g: Grammar, fileName: string): string | undefined; /** * @returns the location where ANTLR will generate output files for a given file. This is a base directory and * output files will be relative to here in some cases such as when -o option is used and input files are * given relative to the input directory. * * @param fileNameWithPath path to input source. */ getOutputDirectory(fileNameWithPath: string): string; logInfo(info: { component?: string; msg: string; }): void; getNumErrors(): number; exit(e: number): void; panic(): void; protected writeDOTFile(g: Grammar, rulOrName: Rule | string, dot: string): void; }