Package org.antlr.v4
Class Tool
- java.lang.Object
-
- org.antlr.v4.Tool
-
public class Tool extends Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTool.Optionstatic classTool.OptionArgType
-
Field Summary
Fields Modifier and Type Field Description static List<String>ALL_GRAMMAR_EXTENSIONSString[]argsErrorManagererrMgrbooleanexact_output_dirbooleanforce_atnbooleangen_dependenciesbooleangen_listenerbooleangen_visitorbooleangenerate_ATN_dotStringgenPackagestatic StringGRAMMAR_EXTENSIONStringgrammarEncodingprotected List<String>grammarFilesMap<String,String>grammarOptionsprotected booleanhaveOutputDirFileinputDirectorybooleanlaunch_ST_inspectorstatic StringLEGACY_GRAMMAR_EXTENSIONStringlibDirectorybooleanlogLogManagerlogMgrbooleanlongMessagesStringmsgFormatstatic Tool.Option[]optionDefsStringoutputDirectoryprotected booleanreturn_dont_exitbooleanST_inspector_wait_for_closestatic StringVERSIONbooleanwarnings_are_errors
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddListener(ANTLRToolListener tl)booleancheckForRuleIssues(Grammar g)Important enough to avoid multiple definitions that we do very early, right after AST construction.GrammarcreateGrammar(GrammarRootAST ast)Given the raw AST of a grammar, create a grammar object associated with the AST.voiderror(ANTLRMessage msg)voidexit(int e)static GrammarASTfindOptionValueAST(GrammarRootAST root, String option)Manually get option node from tree; return null if no defined.voidgenerateATNs(Grammar g)static StringgenerateInterpreterData(Grammar g)FilegetImportedGrammarFile(Grammar g, String fileName)List<ANTLRToolListener>getListeners()intgetNumErrors()FilegetOutputDirectory(String fileNameWithPath)Return the location where ANTLR will generate output files for a given file.WritergetOutputFileWriter(Grammar g, String fileName)This method is used by all code generators to create new output files.protected voidhandleArgs()protected voidhandleOptionSetArg(String arg)voidhelp()voidinfo(String msg)GrammarloadGrammar(String fileName)Convenience method to load and process an ANTLR grammar.GrammarloadImportedGrammar(Grammar g, GrammarAST nameNode)Try current dir then dir of g then lib dirvoidlog(String msg)voidlog(String component, String msg)static voidmain(String[] args)Filenew_getOutputDirectory(String fileNameWithPath)voidpanic()GrammarRootASTparse(String fileName, org.antlr.runtime.CharStream in)GrammarRootASTparseGrammar(String fileName)GrammarRootASTparseGrammarFromString(String grammar)voidprocess(Grammar g, boolean gencode)To process a grammar, we load all of its imported grammars into subordinate grammar objects.voidprocessGrammarsOnCommandLine()voidprocessNonCombinedGrammar(Grammar g, boolean gencode)voidremoveListener(ANTLRToolListener tl)voidremoveListeners()List<GrammarRootAST>sortGrammarByTokenVocab(List<String> fileNames)voidversion()voidwarning(ANTLRMessage msg)protected voidwriteDOTFile(Grammar g, String name, String dot)protected voidwriteDOTFile(Grammar g, Rule r, String dot)
-
-
-
Field Detail
-
VERSION
public static final String VERSION
-
GRAMMAR_EXTENSION
public static final String GRAMMAR_EXTENSION
- See Also:
- Constant Field Values
-
LEGACY_GRAMMAR_EXTENSION
public static final String LEGACY_GRAMMAR_EXTENSION
- See Also:
- Constant Field Values
-
inputDirectory
public File inputDirectory
-
outputDirectory
public String outputDirectory
-
libDirectory
public String libDirectory
-
generate_ATN_dot
public boolean generate_ATN_dot
-
grammarEncoding
public String grammarEncoding
-
msgFormat
public String msgFormat
-
launch_ST_inspector
public boolean launch_ST_inspector
-
ST_inspector_wait_for_close
public boolean ST_inspector_wait_for_close
-
force_atn
public boolean force_atn
-
log
public boolean log
-
gen_listener
public boolean gen_listener
-
gen_visitor
public boolean gen_visitor
-
gen_dependencies
public boolean gen_dependencies
-
genPackage
public String genPackage
-
warnings_are_errors
public boolean warnings_are_errors
-
longMessages
public boolean longMessages
-
exact_output_dir
public boolean exact_output_dir
-
optionDefs
public static final Tool.Option[] optionDefs
-
haveOutputDir
protected boolean haveOutputDir
-
return_dont_exit
protected boolean return_dont_exit
-
args
public final String[] args
-
errMgr
public ErrorManager errMgr
-
logMgr
public LogManager logMgr
-
-
Constructor Detail
-
Tool
public Tool()
-
Tool
public Tool(String[] args)
-
-
Method Detail
-
main
public static void main(String[] args)
-
handleArgs
protected void handleArgs()
-
handleOptionSetArg
protected void handleOptionSetArg(String arg)
-
processGrammarsOnCommandLine
public void processGrammarsOnCommandLine()
-
process
public void process(Grammar g, boolean gencode)
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
-
processNonCombinedGrammar
public void processNonCombinedGrammar(Grammar g, boolean gencode)
-
checkForRuleIssues
public boolean checkForRuleIssues(Grammar g)
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.
-
sortGrammarByTokenVocab
public List<GrammarRootAST> sortGrammarByTokenVocab(List<String> fileNames)
-
findOptionValueAST
public static GrammarAST findOptionValueAST(GrammarRootAST root, String option)
Manually get option node from tree; return null if no defined.
-
createGrammar
public Grammar createGrammar(GrammarRootAST ast)
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.
-
parseGrammar
public GrammarRootAST parseGrammar(String fileName)
-
loadGrammar
public Grammar loadGrammar(String fileName)
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.
-
loadImportedGrammar
public Grammar loadImportedGrammar(Grammar g, GrammarAST nameNode) throws IOException
Try current dir then dir of g then lib dir- Parameters:
g-nameNode- The node associated with the imported grammar name.- Throws:
IOException
-
parseGrammarFromString
public GrammarRootAST parseGrammarFromString(String grammar)
-
parse
public GrammarRootAST parse(String fileName, org.antlr.runtime.CharStream in)
-
generateATNs
public void generateATNs(Grammar g)
-
getOutputFileWriter
public Writer getOutputFileWriter(Grammar g, String fileName) throws IOException
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. If outputDirectory==null then write a String.- Throws:
IOException
-
getOutputDirectory
public File getOutputDirectory(String fileNameWithPath)
Return 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.- Parameters:
fileNameWithPath- path to input source
-
new_getOutputDirectory
public File new_getOutputDirectory(String fileNameWithPath)
- Since:
- 4.7.1 in response to -Xexact-output-dir
-
writeDOTFile
protected void writeDOTFile(Grammar g, Rule r, String dot) throws IOException
- Throws:
IOException
-
writeDOTFile
protected void writeDOTFile(Grammar g, String name, String dot) throws IOException
- Throws:
IOException
-
help
public void help()
-
log
public void log(String msg)
-
getNumErrors
public int getNumErrors()
-
addListener
public void addListener(ANTLRToolListener tl)
-
removeListener
public void removeListener(ANTLRToolListener tl)
-
removeListeners
public void removeListeners()
-
getListeners
public List<ANTLRToolListener> getListeners()
-
info
public void info(String msg)
-
error
public void error(ANTLRMessage msg)
-
warning
public void warning(ANTLRMessage msg)
-
version
public void version()
-
exit
public void exit(int e)
-
panic
public void panic()
-
-