Package org.antlr.v4
Class Tool
- java.lang.Object
-
- org.antlr.v4.Tool
-
public class Tool extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Tool.Option
static class
Tool.OptionArgType
-
Field Summary
Fields Modifier and Type Field Description static java.util.List<java.lang.String>
ALL_GRAMMAR_EXTENSIONS
java.lang.String[]
args
ErrorManager
errMgr
boolean
exact_output_dir
boolean
force_atn
boolean
gen_dependencies
boolean
gen_listener
boolean
gen_visitor
boolean
generate_ATN_dot
java.lang.String
genPackage
static java.lang.String
GRAMMAR_EXTENSION
java.lang.String
grammarEncoding
protected java.util.List<java.lang.String>
grammarFiles
java.util.Map<java.lang.String,java.lang.String>
grammarOptions
protected boolean
haveOutputDir
java.io.File
inputDirectory
static boolean
internalOption_PrintGrammarTree
static boolean
internalOption_ShowATNConfigsInDFA
boolean
launch_ST_inspector
static java.lang.String
LEGACY_GRAMMAR_EXTENSION
java.lang.String
libDirectory
boolean
log
LogManager
logMgr
boolean
longMessages
java.lang.String
msgFormat
static Tool.Option[]
optionDefs
java.lang.String
outputDirectory
protected boolean
return_dont_exit
boolean
ST_inspector_wait_for_close
static java.lang.String
VERSION
boolean
warnings_are_errors
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addListener(ANTLRToolListener tl)
boolean
checkForRuleIssues(Grammar g)
Important enough to avoid multiple definitions that we do very early, right after AST construction.Grammar
createGrammar(GrammarRootAST ast)
Given the raw AST of a grammar, create a grammar object associated with the AST.void
error(ANTLRMessage msg)
void
exit(int e)
static GrammarAST
findOptionValueAST(GrammarRootAST root, java.lang.String option)
Manually get option node from tree; return null if no defined.void
generateATNs(Grammar g)
java.io.File
getImportedGrammarFile(Grammar g, java.lang.String fileName)
java.util.List<ANTLRToolListener>
getListeners()
int
getNumErrors()
java.io.File
getOutputDirectory(java.lang.String fileNameWithPath)
Return the location where ANTLR will generate output files for a given file.java.io.Writer
getOutputFileWriter(Grammar g, java.lang.String fileName)
This method is used by all code generators to create new output files.protected void
handleArgs()
protected void
handleOptionSetArg(java.lang.String arg)
void
help()
void
info(java.lang.String msg)
Grammar
loadGrammar(java.lang.String fileName)
Convenience method to load and process an ANTLR grammar.Grammar
loadImportedGrammar(Grammar g, GrammarAST nameNode)
Try current dir then dir of g then lib dirvoid
log(java.lang.String msg)
void
log(java.lang.String component, java.lang.String msg)
static void
main(java.lang.String[] args)
java.io.File
new_getOutputDirectory(java.lang.String fileNameWithPath)
void
panic()
GrammarRootAST
parse(java.lang.String fileName, org.antlr.runtime.CharStream in)
GrammarRootAST
parseGrammar(java.lang.String fileName)
GrammarRootAST
parseGrammarFromString(java.lang.String grammar)
void
process(Grammar g, boolean gencode)
To process a grammar, we load all of its imported grammars into subordinate grammar objects.void
processGrammarsOnCommandLine()
void
processNonCombinedGrammar(Grammar g, boolean gencode)
void
removeListener(ANTLRToolListener tl)
void
removeListeners()
java.util.List<GrammarRootAST>
sortGrammarByTokenVocab(java.util.List<java.lang.String> fileNames)
void
version()
void
warning(ANTLRMessage msg)
protected void
writeDOTFile(Grammar g, java.lang.String name, java.lang.String dot)
protected void
writeDOTFile(Grammar g, Rule r, java.lang.String dot)
-
-
-
Field Detail
-
VERSION
public static final java.lang.String VERSION
-
GRAMMAR_EXTENSION
public static final java.lang.String GRAMMAR_EXTENSION
- See Also:
- Constant Field Values
-
LEGACY_GRAMMAR_EXTENSION
public static final java.lang.String LEGACY_GRAMMAR_EXTENSION
- See Also:
- Constant Field Values
-
ALL_GRAMMAR_EXTENSIONS
public static final java.util.List<java.lang.String> ALL_GRAMMAR_EXTENSIONS
-
inputDirectory
public java.io.File inputDirectory
-
outputDirectory
public java.lang.String outputDirectory
-
libDirectory
public java.lang.String libDirectory
-
generate_ATN_dot
public boolean generate_ATN_dot
-
grammarEncoding
public java.lang.String grammarEncoding
-
msgFormat
public java.lang.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 java.lang.String genPackage
-
grammarOptions
public java.util.Map<java.lang.String,java.lang.String> grammarOptions
-
warnings_are_errors
public boolean warnings_are_errors
-
longMessages
public boolean longMessages
-
exact_output_dir
public boolean exact_output_dir
-
optionDefs
public static Tool.Option[] optionDefs
-
haveOutputDir
protected boolean haveOutputDir
-
return_dont_exit
protected boolean return_dont_exit
-
internalOption_PrintGrammarTree
public static boolean internalOption_PrintGrammarTree
-
internalOption_ShowATNConfigsInDFA
public static boolean internalOption_ShowATNConfigsInDFA
-
args
public final java.lang.String[] args
-
grammarFiles
protected java.util.List<java.lang.String> grammarFiles
-
errMgr
public ErrorManager errMgr
-
logMgr
public LogManager logMgr
-
-
Method Detail
-
main
public static void main(java.lang.String[] args)
-
handleArgs
protected void handleArgs()
-
handleOptionSetArg
protected void handleOptionSetArg(java.lang.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 java.util.List<GrammarRootAST> sortGrammarByTokenVocab(java.util.List<java.lang.String> fileNames)
-
findOptionValueAST
public static GrammarAST findOptionValueAST(GrammarRootAST root, java.lang.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(java.lang.String fileName)
-
loadGrammar
public Grammar loadGrammar(java.lang.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 java.io.IOException
Try current dir then dir of g then lib dir- Parameters:
g
-nameNode
- The node associated with the imported grammar name.- Throws:
java.io.IOException
-
parseGrammarFromString
public GrammarRootAST parseGrammarFromString(java.lang.String grammar)
-
parse
public GrammarRootAST parse(java.lang.String fileName, org.antlr.runtime.CharStream in)
-
generateATNs
public void generateATNs(Grammar g)
-
getOutputFileWriter
public java.io.Writer getOutputFileWriter(Grammar g, java.lang.String fileName) throws java.io.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:
java.io.IOException
-
getImportedGrammarFile
public java.io.File getImportedGrammarFile(Grammar g, java.lang.String fileName)
-
getOutputDirectory
public java.io.File getOutputDirectory(java.lang.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 java.io.File new_getOutputDirectory(java.lang.String fileNameWithPath)
- Since:
- 4.7.1 in response to -Xexact-output-dir
-
writeDOTFile
protected void writeDOTFile(Grammar g, Rule r, java.lang.String dot) throws java.io.IOException
- Throws:
java.io.IOException
-
writeDOTFile
protected void writeDOTFile(Grammar g, java.lang.String name, java.lang.String dot) throws java.io.IOException
- Throws:
java.io.IOException
-
help
public void help()
-
log
public void log(java.lang.String component, java.lang.String msg)
-
log
public void log(java.lang.String msg)
-
getNumErrors
public int getNumErrors()
-
addListener
public void addListener(ANTLRToolListener tl)
-
removeListener
public void removeListener(ANTLRToolListener tl)
-
removeListeners
public void removeListeners()
-
getListeners
public java.util.List<ANTLRToolListener> getListeners()
-
info
public void info(java.lang.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()
-
-