Package org.antlr

Class Tool


  • public class Tool
    extends java.lang.Object
    The main ANTLR entry point. Read a grammar and generate a parser.
    • Constructor Summary

      Constructors 
      Constructor Description
      Tool()  
      Tool​(java.lang.String[] args)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addGrammarFile​(java.lang.String grammarFileName)  
      boolean buildRequired​(java.lang.String grammarFileName)
      Checks to see if the list of outputFiles all exist, and have last-modified timestamps which are later than the last-modified timestamp of all the grammar files involved in build the output (imports must be checked).
      void generateDFAs​(Grammar g)  
      protected void generateNFAs​(Grammar g)  
      protected void generateRecognizer​(Grammar grammar)
      Create NFA, DFA and generate code for grammar.
      int getConversionTimeout()
      Provide the current setting of the conversion timeout on DFA creation.
      static java.lang.String getCurrentTimeStamp()
      Return a time stamp string accurate to sec: yyyy-mm-dd hh:mm:ss
      java.lang.String getFileDirectory​(java.lang.String fileName)
      Return the directory containing the grammar file for this grammar.
      java.util.List<java.lang.String> getGrammarFileNames()
      Provide the List of all grammar file names that the ANTLR tool will process or has processed.
      java.io.File getImportedVocabFile​(java.lang.String vocabName)
      Return a File descriptor for vocab file.
      java.lang.String getLibraryDirectory()
      Return the Path to the directory in which ANTLR will search for ancillary files such as .tokens vocab files and imported grammar files.
      java.lang.String getLibraryFile​(java.lang.String fileName)
      Name a file from the -lib dir.
      boolean getMake()
      Indicate whether the tool will analyze the dependencies of the provided grammar file list and ensure that grammars with dependencies are built after any of the other gramamrs in the list that they are dependent on.
      java.lang.String getMessageFormat()
      Returns the current setting of the message format descriptor
      int getNumErrors()
      Returns the number of errors that the analysis/processing threw up.
      java.lang.String getOutputDirectory()
      Return the Path to the base output directory, where ANTLR will generate all the output files for the current language target as well as any ancillary files such as .tokens vocab files.
      java.io.File getOutputDirectory​(java.lang.String fileNameWithPath)
      Return the location where ANTLR will generate output files for a given file.
      java.io.Writer getOutputFile​(Grammar g, java.lang.String fileName)
      This method is used by all code generators to create new output files.
      Grammar getRootGrammar​(java.lang.String grammarFileName)
      Get a grammar mentioned on the command-line and any delegates
      boolean isDebug()
      Indicate if ANTLR has generated, or will generate a debug version of the recognizer.
      boolean isDepend()
      Indicates whether ANTLR has supplied, or will supply, a list of all the things that the input grammar depends upon and all the things that will be generated when that grammar is successfully analyzed.
      boolean isForceAllFilesToOutputDir()
      Indicates whether ANTLR will force all files to the output directory, even if the input files have relative paths from the input directory.
      boolean isGenerate_DFA_dot()
      Indicates whether ANTLR has generated or will generate a description of all the NFAs in Dot format
      boolean isGenerate_NFA_dot()
      Indicates whether ANTLR has gnerated or will generate a description of all the NFAs in Dot format
      boolean isPrintGrammar()
      Indicates whether ANTLR has printed, or will print, a version of the input grammar file(s) that is stripped of any action code embedded within.
      boolean isProfile()
      Indicates whether ANTLR has generated or will generate a version of the recognizer that gathers statistics about its execution, which it prints when it terminates.
      boolean isReport()
      Indicates whether ANTLR has generated or will generate a report of various elements of the grammar analysis, once it it has finished analyzing a grammar file.
      boolean isTrace()
      Indicate whether ANTLR has generated, or will generate a version of the recognizer that prints trace messages on entry and exit of each rule.
      boolean isVerbose()
      Indicates whether ANTLR will be verbose when analyzing grammar files, such as displaying the names of the files it is generating and similar information.
      static void main​(java.lang.String[] args)
      A list of dependency generators that are accumulated aaaas (and if) the tool is required to sort the provided grammars into build dependency order.
      void panic()
      If the tool needs to panic/exit, how do we do that?
      void process()  
      void processArgs​(java.lang.String[] args)  
      void setDebug​(boolean debug)
      Indicate whether ANTLR should generate a debug version of the recognizer.
      void setDepend​(boolean depend)
      Indicate whether ANTLR should supply a list of all the things that the input grammar depends upon and all the things that will be generated when that gramamr is successfully analyzed.
      void setForceAllFilesToOutputDir​(boolean forceAllFilesToOutputDir)
      Indicates whether ANTLR will force all files to the output directory, even if the input files have relative paths from the input directory.
      void setForceRelativeOutput​(boolean forceRelativeOutput)
      Used by build tools to force the output files to always be relative to the base output directory, even though the tool had to set the output directory to an absolute path as it cannot rely on the workign directory like command line invocation can.
      void setGenerate_DFA_dot​(boolean generate_DFA_dot)
      Indicates whether ANTLR should generate a description of all the NFAs in Dot format
      void setGenerate_NFA_dot​(boolean generate_NFA_dot)
      Indicate whether ANTLR should generate a description of all the NFAs in Dot format
      void setGrammarFileNames​(java.util.List<java.lang.String> grammarFileNames)
      Provide the List of all grammar file names that the ANTLR tool should process.
      void setInputDirectory​(java.lang.String inputDirectory)
      Set the base location of input files.
      void setLibDirectory​(java.lang.String libDirectory)
      Set the Path to the directory in which ANTLR will search for ancillary files such as .tokens vocab files and imported grammar files.
      void setMake​(boolean make)
      Indicate whether the tool should analyze the dependencies of the provided grammar file list and ensure that the grammars with dependencies are built after any of the other gramamrs in the list that they are dependent on.
      void setMaxSwitchCaseLabels​(int maxSwitchCaseLabels)
      Set the threshold of case labels beyond which ANTLR will not instruct the target template to generate switch() { case xxx: ...
      void setMessageFormat​(java.lang.String format)
      Set the message format to one of ANTLR, gnu, vs2005
      void setMinSwitchAlts​(int minSwitchAlts)
      Set the threshold of the number alts, below which ANTLR will not instruct the target template to use a switch statement.
      void setOutputDirectory​(java.lang.String outputDirectory)
      Set the location (base directory) where output files should be produced by the ANTLR tool.
      void setPrintGrammar​(boolean printGrammar)
      Indicate whether ANTLR should print a version of the input grammar file(s) that is stripped of any action code embedded within.
      void setProfile​(boolean profile)
      Indicate whether ANTLR should generate a version of the recognizer that gathers statistics about its execution, which it prints when it terminates.
      void setReport​(boolean report)
      Indicate whether ANTLR should generate a report of various elements of the grammar analysis, once it it has finished analyzing a grammar file.
      void setTrace​(boolean trace)
      Indicate whether ANTLR should generate a version of the recognizer that prints trace messages on entry and exit of each rule
      void setVerbose​(boolean verbose)
      Indicate whether ANTLR should be verbose when analyzing grammar files, such as displaying the names of the files it is generating and similar information.
      void sortGrammarFiles()  
      protected void writeDOTFile​(Grammar g, java.lang.String name, java.lang.String dot)  
      protected void writeDOTFile​(Grammar g, Rule r, java.lang.String dot)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • antlrSettings

        public final java.util.Properties antlrSettings
      • VERSION

        public final java.lang.String VERSION
      • UNINITIALIZED_DIR

        public static final java.lang.String UNINITIALIZED_DIR
        See Also:
        Constant Field Values
      • deleteTempLexer

        protected boolean deleteTempLexer
      • forcedLanguageOption

        public java.lang.String forcedLanguageOption
      • internalOption_PrintGrammarTree

        public static boolean internalOption_PrintGrammarTree
      • internalOption_PrintDFA

        public static boolean internalOption_PrintDFA
      • internalOption_ShowNFAConfigsInDFA

        public static boolean internalOption_ShowNFAConfigsInDFA
      • internalOption_watchNFAConversion

        public static boolean internalOption_watchNFAConversion
    • Constructor Detail

      • Tool

        public Tool()
      • Tool

        public Tool​(java.lang.String[] args)
    • Method Detail

      • main

        public static void main​(java.lang.String[] args)
        A list of dependency generators that are accumulated aaaas (and if) the tool is required to sort the provided grammars into build dependency order. protected Map<String, BuildDependencyGenerator> buildDependencyGenerators;
      • processArgs

        public void processArgs​(java.lang.String[] args)
      • buildRequired

        public boolean buildRequired​(java.lang.String grammarFileName)
                              throws java.io.IOException
        Checks to see if the list of outputFiles all exist, and have last-modified timestamps which are later than the last-modified timestamp of all the grammar files involved in build the output (imports must be checked). If these conditions hold, the method returns false, otherwise, it returns true.
        Parameters:
        grammarFileName - The grammar file we are checking
        Throws:
        java.io.IOException
      • process

        public void process()
      • sortGrammarFiles

        public void sortGrammarFiles()
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • getRootGrammar

        public Grammar getRootGrammar​(java.lang.String grammarFileName)
                               throws java.io.IOException
        Get a grammar mentioned on the command-line and any delegates
        Throws:
        java.io.IOException
      • generateRecognizer

        protected void generateRecognizer​(Grammar grammar)
        Create NFA, DFA and generate code for grammar. Create NFA for any delegates first. Once all NFA are created, it's ok to create DFA, which must check for left-recursion. That check is done by walking the full NFA, which therefore must be complete. After all NFA, comes DFA conversion for root grammar then code gen for root grammar. DFA and code gen for delegates comes next.
      • generateDFAs

        public void generateDFAs​(Grammar g)
      • generateNFAs

        protected void generateNFAs​(Grammar g)
      • 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
      • setMaxSwitchCaseLabels

        public void setMaxSwitchCaseLabels​(int maxSwitchCaseLabels)
        Set the threshold of case labels beyond which ANTLR will not instruct the target template to generate switch() { case xxx: ...
        Parameters:
        maxSwitchCaseLabels - Maximum number of case lables that ANTLR should allow the target code
      • setMinSwitchAlts

        public void setMinSwitchAlts​(int minSwitchAlts)
        Set the threshold of the number alts, below which ANTLR will not instruct the target template to use a switch statement.
        Parameters:
        minSwitchAlts - the minimum number of alts required to use a switch staement
      • setOutputDirectory

        public void setOutputDirectory​(java.lang.String outputDirectory)
        Set the location (base directory) where output files should be produced by the ANTLR tool.
        Parameters:
        outputDirectory -
      • setForceRelativeOutput

        public void setForceRelativeOutput​(boolean forceRelativeOutput)
        Used by build tools to force the output files to always be relative to the base output directory, even though the tool had to set the output directory to an absolute path as it cannot rely on the workign directory like command line invocation can.
        Parameters:
        forceRelativeOutput - true if output files hould always be relative to base output directory
      • setInputDirectory

        public void setInputDirectory​(java.lang.String inputDirectory)
        Set the base location of input files. Normally (when the tool is invoked from the command line), the inputDirectory is not set, but for build tools such as Maven, we need to be able to locate the input files relative to the base, as the working directory could be anywhere and changing workig directories is not a valid concept for JVMs because of threading and so on. Setting the directory just means that the getFileDirectory() method will try to open files relative to this input directory.
        Parameters:
        inputDirectory - Input source base directory
      • getOutputFile

        public java.io.Writer getOutputFile​(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.g 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 precendence. "-o /tmp /usr/lib/t.g" results in "/tmp/T.java" as output (assuming t.g 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
      • 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
      • getLibraryFile

        public java.lang.String getLibraryFile​(java.lang.String fileName)
                                        throws java.io.IOException
        Name a file from the -lib dir. Imported grammars and .tokens files If we do not locate the file in the library directory, then we try the location of the originating grammar.
        Parameters:
        fileName - input name we are looking for
        Returns:
        Path to file that we think shuold be the import file
        Throws:
        java.io.IOException
      • getFileDirectory

        public java.lang.String getFileDirectory​(java.lang.String fileName)
        Return the directory containing the grammar file for this grammar. normally this is a relative path from current directory. People will often do "java org.antlr.Tool grammars/*.g3" So the file will be "grammars/foo.g3" etc... This method returns "grammars". If we have been given a specific input directory as a base, then we must find the directory relative to this directory, unless the file name is given to us in absolute terms.
      • getImportedVocabFile

        public java.io.File getImportedVocabFile​(java.lang.String vocabName)
        Return a File descriptor for vocab file. Look in library or in -o output path. antlr -o foo T.g U.g where U needs T.tokens won't work unless we look in foo too. If we do not find the file in the lib directory then must assume that the .tokens file is going to be generated as part of this build and we have defined .tokens files so that they ALWAYS are generated in the base output directory, which means the current directory for the command line tool if there was no output directory specified.
      • panic

        public void panic()
        If the tool needs to panic/exit, how do we do that?
      • getCurrentTimeStamp

        public static java.lang.String getCurrentTimeStamp()
        Return a time stamp string accurate to sec: yyyy-mm-dd hh:mm:ss
      • getGrammarFileNames

        public java.util.List<java.lang.String> getGrammarFileNames()
        Provide the List of all grammar file names that the ANTLR tool will process or has processed.
        Returns:
        the grammarFileNames
      • isGenerate_NFA_dot

        public boolean isGenerate_NFA_dot()
        Indicates whether ANTLR has gnerated or will generate a description of all the NFAs in Dot format
        Returns:
        the generate_NFA_dot
      • isGenerate_DFA_dot

        public boolean isGenerate_DFA_dot()
        Indicates whether ANTLR has generated or will generate a description of all the NFAs in Dot format
        Returns:
        the generate_DFA_dot
      • getOutputDirectory

        public java.lang.String getOutputDirectory()
        Return the Path to the base output directory, where ANTLR will generate all the output files for the current language target as well as any ancillary files such as .tokens vocab files.
        Returns:
        the output Directory
      • getLibraryDirectory

        public java.lang.String getLibraryDirectory()
        Return the Path to the directory in which ANTLR will search for ancillary files such as .tokens vocab files and imported grammar files.
        Returns:
        the lib Directory
      • isDebug

        public boolean isDebug()
        Indicate if ANTLR has generated, or will generate a debug version of the recognizer. Debug versions of a parser communicate with a debugger such as that contained in ANTLRWorks and at start up will 'hang' waiting for a connection on an IP port (49100 by default).
        Returns:
        the debug flag
      • isTrace

        public boolean isTrace()
        Indicate whether ANTLR has generated, or will generate a version of the recognizer that prints trace messages on entry and exit of each rule.
        Returns:
        the trace flag
      • isProfile

        public boolean isProfile()
        Indicates whether ANTLR has generated or will generate a version of the recognizer that gathers statistics about its execution, which it prints when it terminates.
        Returns:
        the profile
      • isReport

        public boolean isReport()
        Indicates whether ANTLR has generated or will generate a report of various elements of the grammar analysis, once it it has finished analyzing a grammar file.
        Returns:
        the report flag
      • isPrintGrammar

        public boolean isPrintGrammar()
        Indicates whether ANTLR has printed, or will print, a version of the input grammar file(s) that is stripped of any action code embedded within.
        Returns:
        the printGrammar flag
      • isDepend

        public boolean isDepend()
        Indicates whether ANTLR has supplied, or will supply, a list of all the things that the input grammar depends upon and all the things that will be generated when that grammar is successfully analyzed.
        Returns:
        the depend flag
      • isForceAllFilesToOutputDir

        public boolean isForceAllFilesToOutputDir()
        Indicates whether ANTLR will force all files to the output directory, even if the input files have relative paths from the input directory.
        Returns:
        the forceAllFilesToOutputDir flag
      • isVerbose

        public boolean isVerbose()
        Indicates whether ANTLR will be verbose when analyzing grammar files, such as displaying the names of the files it is generating and similar information.
        Returns:
        the verbose flag
      • getConversionTimeout

        public int getConversionTimeout()
        Provide the current setting of the conversion timeout on DFA creation.
        Returns:
        DFA creation timeout value in milliseconds
      • getMessageFormat

        public java.lang.String getMessageFormat()
        Returns the current setting of the message format descriptor
        Returns:
        Current message format
      • getNumErrors

        public int getNumErrors()
        Returns the number of errors that the analysis/processing threw up.
        Returns:
        Error count
      • getMake

        public boolean getMake()
        Indicate whether the tool will analyze the dependencies of the provided grammar file list and ensure that grammars with dependencies are built after any of the other gramamrs in the list that they are dependent on. Setting this option also has the side effect that any grammars that are includes for other grammars in the list are excluded from individual analysis, which allows the caller to invoke the tool via org.antlr.tool -make *.g and not worry about the inclusion of grammars that are just includes for other grammars or what order the grammars appear on the command line. This option was coded to make life easier for tool integration (such as Maven) but may also be useful at the command line.
        Returns:
        true if the tool is currently configured to analyze and sort grammar files.
      • setMessageFormat

        public void setMessageFormat​(java.lang.String format)
        Set the message format to one of ANTLR, gnu, vs2005
        Parameters:
        format -
      • setGrammarFileNames

        public void setGrammarFileNames​(java.util.List<java.lang.String> grammarFileNames)
        Provide the List of all grammar file names that the ANTLR tool should process.
        Parameters:
        grammarFileNames - The list of grammar files to process
      • addGrammarFile

        public void addGrammarFile​(java.lang.String grammarFileName)
      • setGenerate_NFA_dot

        public void setGenerate_NFA_dot​(boolean generate_NFA_dot)
        Indicate whether ANTLR should generate a description of all the NFAs in Dot format
        Parameters:
        generate_NFA_dot - True to generate dot descriptions
      • setGenerate_DFA_dot

        public void setGenerate_DFA_dot​(boolean generate_DFA_dot)
        Indicates whether ANTLR should generate a description of all the NFAs in Dot format
        Parameters:
        generate_DFA_dot - True to generate dot descriptions
      • setLibDirectory

        public void setLibDirectory​(java.lang.String libDirectory)
        Set the Path to the directory in which ANTLR will search for ancillary files such as .tokens vocab files and imported grammar files.
        Parameters:
        libDirectory - the libDirectory to set
      • setDebug

        public void setDebug​(boolean debug)
        Indicate whether ANTLR should generate a debug version of the recognizer. Debug versions of a parser communicate with a debugger such as that contained in ANTLRWorks and at start up will 'hang' waiting for a connection on an IP port (49100 by default).
        Parameters:
        debug - true to generate a debug mode parser
      • setTrace

        public void setTrace​(boolean trace)
        Indicate whether ANTLR should generate a version of the recognizer that prints trace messages on entry and exit of each rule
        Parameters:
        trace - true to generate a tracing parser
      • setProfile

        public void setProfile​(boolean profile)
        Indicate whether ANTLR should generate a version of the recognizer that gathers statistics about its execution, which it prints when it terminates.
        Parameters:
        profile - true to generate a profiling parser
      • setReport

        public void setReport​(boolean report)
        Indicate whether ANTLR should generate a report of various elements of the grammar analysis, once it it has finished analyzing a grammar file.
        Parameters:
        report - true to generate the analysis report
      • setPrintGrammar

        public void setPrintGrammar​(boolean printGrammar)
        Indicate whether ANTLR should print a version of the input grammar file(s) that is stripped of any action code embedded within.
        Parameters:
        printGrammar - true to generate a stripped file
      • setDepend

        public void setDepend​(boolean depend)
        Indicate whether ANTLR should supply a list of all the things that the input grammar depends upon and all the things that will be generated when that gramamr is successfully analyzed.
        Parameters:
        depend - true to get depends set rather than process the grammar
      • setForceAllFilesToOutputDir

        public void setForceAllFilesToOutputDir​(boolean forceAllFilesToOutputDir)
        Indicates whether ANTLR will force all files to the output directory, even if the input files have relative paths from the input directory.
        Parameters:
        forceAllFilesToOutputDir - true to force files to output directory
      • setVerbose

        public void setVerbose​(boolean verbose)
        Indicate whether ANTLR should be verbose when analyzing grammar files, such as displaying the names of the files it is generating and similar information.
        Parameters:
        verbose - true to be verbose
      • setMake

        public void setMake​(boolean make)
        Indicate whether the tool should analyze the dependencies of the provided grammar file list and ensure that the grammars with dependencies are built after any of the other gramamrs in the list that they are dependent on. Setting this option also has the side effect that any grammars that are includes for other grammars in the list are excluded from individual analysis, which allows the caller to invoke the tool via org.antlr.tool -make *.g and not worry about the inclusion of grammars that are just includes for other grammars or what order the grammars appear on the command line. This option was coded to make life easier for tool integration (such as Maven) but may also be useful at the command line.
        Parameters:
        make -