Package org.antlr.v4

Class Tool


  • public class Tool
    extends Object
    • Field Detail

      • VERSION

        public static final String VERSION
      • ALL_GRAMMAR_EXTENSIONS

        public static final List<String> ALL_GRAMMAR_EXTENSIONS
      • 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
      • grammarFiles

        protected List<String> grammarFiles
    • 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.
      • 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.
      • 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
      • generateATNs

        public void generateATNs​(Grammar g)
      • generateInterpreterData

        public static String generateInterpreterData​(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
      • getImportedGrammarFile

        public File getImportedGrammarFile​(Grammar g,
                                           String fileName)
      • 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
      • help

        public void help()
      • log

        public void log​(String msg)
      • getNumErrors

        public int getNumErrors()
      • removeListeners

        public void removeListeners()
      • info

        public void info​(String msg)
      • version

        public void version()
      • exit

        public void exit​(int e)
      • panic

        public void panic()