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 class
Tool.Option
static class
Tool.OptionArgType
-
Field Summary
Fields Modifier and Type Field Description static List<String>
ALL_GRAMMAR_EXTENSIONS
String[]
args
ErrorManager
errMgr
boolean
exact_output_dir
boolean
force_atn
boolean
gen_dependencies
boolean
gen_listener
boolean
gen_visitor
boolean
generate_ATN_dot
String
genPackage
static String
GRAMMAR_EXTENSION
String
grammarEncoding
protected List<String>
grammarFiles
Map<String,String>
grammarOptions
protected boolean
haveOutputDir
File
inputDirectory
boolean
launch_ST_inspector
static String
LEGACY_GRAMMAR_EXTENSION
String
libDirectory
boolean
log
LogManager
logMgr
boolean
longMessages
String
msgFormat
static Tool.Option[]
optionDefs
String
outputDirectory
protected boolean
return_dont_exit
boolean
ST_inspector_wait_for_close
static 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, String option)
Manually get option node from tree; return null if no defined.void
generateATNs(Grammar g)
static String
generateInterpreterData(Grammar g)
File
getImportedGrammarFile(Grammar g, String fileName)
List<ANTLRToolListener>
getListeners()
int
getNumErrors()
File
getOutputDirectory(String fileNameWithPath)
Return the location where ANTLR will generate output files for a given file.Writer
getOutputFileWriter(Grammar g, String fileName)
This method is used by all code generators to create new output files.protected void
handleArgs()
protected void
handleOptionSetArg(String arg)
void
help()
void
info(String msg)
Grammar
loadGrammar(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(String msg)
void
log(String component, String msg)
static void
main(String[] args)
File
new_getOutputDirectory(String fileNameWithPath)
void
panic()
GrammarRootAST
parse(String fileName, org.antlr.runtime.CharStream in)
GrammarRootAST
parseGrammar(String fileName)
GrammarRootAST
parseGrammarFromString(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()
List<GrammarRootAST>
sortGrammarByTokenVocab(List<String> fileNames)
void
version()
void
warning(ANTLRMessage msg)
protected void
writeDOTFile(Grammar g, String name, String dot)
protected void
writeDOTFile(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()
-
-