Class ParserEngine


  • public class ParserEngine
    extends java.lang.Object
    Performs the heavy-lifting of parsing out individual lines from a script.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int currentScriptLineNumber
      The number of the current (or last if not currently parsing) line being parsed (1-based).
      private static java.util.regex.Pattern paramInfoRegex
      This regular expression pattern breaks apart an action parameter string into three parts: param label, param data type, and param data.
      private static java.lang.String paramListDelimiter
      The delimiter used to split up a String containing all of the parameters which should be parsed and passed to an action.
      private java.lang.String scriptFile
      The path to the script file to be parsed ('-' if the script is already in memory and has no related file).
      private static java.util.regex.Pattern scriptLineRegex
      This regular expression pattern breaks apart a line in a script into two groups: the script action and the string containing all the action's parameters.
      private java.io.BufferedReader scriptReader
      The reader used to read in each line of the script.
    • Constructor Summary

      Constructors 
      Constructor Description
      ParserEngine​(java.lang.String script, boolean scriptIsFileToParse)
      Creates a new ParserEngine object.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void finalize()
      Clean up memory used by the parser engine and make sure the reader we used is closed.
      int getCurrentLineNumber()
      Returns the line number of the last line parsed by the parser engine.
      boolean hasMoreLinesToParse()
      Returns whether there are more lines in the script to be parsed.
      private static boolean isCommentLine​(java.lang.String scriptLine)
      Checks to see if a line is a comment.
      private java.io.BufferedReader openScriptFile​(java.lang.String scriptFileToParse)
      Open a script file and prepare a reader for it.
      private java.io.BufferedReader openScriptString​(java.lang.String scriptDataToParse)
      Prepare a reader for a given string of script data.
      private ParsedActionLine parseLine​(java.lang.String scriptLine)
      Parses a line from a script.
      ParsedActionLine parseNextLine()
      Parse and return the next line in the script.
      private Parameter parseParameterInfo​(java.lang.String paramInfo)
      Parse out the individual parameter information from a string.
      private ParameterTable parseParameters​(java.lang.String paramString)
      Splits a list of parameters into individual parameter information strings, parses the individual parameter strings, and puts the parameters into a look up table.
      void resetParser()
      Resets the parser to the beginning of the script.
      • Methods inherited from class java.lang.Object

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

      • scriptLineRegex

        private static final java.util.regex.Pattern scriptLineRegex
        This regular expression pattern breaks apart a line in a script into two groups: the script action and the string containing all the action's parameters.
      • paramInfoRegex

        private static final java.util.regex.Pattern paramInfoRegex
        This regular expression pattern breaks apart an action parameter string into three parts: param label, param data type, and param data.
      • paramListDelimiter

        private static final java.lang.String paramListDelimiter
        The delimiter used to split up a String containing all of the parameters which should be parsed and passed to an action.
        See Also:
        Constant Field Values
      • currentScriptLineNumber

        private int currentScriptLineNumber
        The number of the current (or last if not currently parsing) line being parsed (1-based).
      • scriptFile

        private java.lang.String scriptFile
        The path to the script file to be parsed ('-' if the script is already in memory and has no related file).
      • scriptReader

        private java.io.BufferedReader scriptReader
        The reader used to read in each line of the script.
    • Constructor Detail

      • ParserEngine

        public ParserEngine​(java.lang.String script,
                            boolean scriptIsFileToParse)
                     throws ParserException
        Creates a new ParserEngine object.
        Parameters:
        script - Either the path to the script on disk, or the text of the script.
        scriptIsFileToParse - If true, then script is the path to a file on disk. Otherwise, it is the text of a script and no file should be read in from disk.
        Throws:
        ParserException - If a problem is encountered while parsing the script.
    • Method Detail

      • finalize

        public void finalize()
        Clean up memory used by the parser engine and make sure the reader we used is closed.
        Overrides:
        finalize in class java.lang.Object
      • getCurrentLineNumber

        public int getCurrentLineNumber()
        Returns the line number of the last line parsed by the parser engine.
        Returns:
        The line number of the last line parsed by the parser engine.
      • hasMoreLinesToParse

        public boolean hasMoreLinesToParse()
                                    throws ParserException
        Returns whether there are more lines in the script to be parsed.
        Returns:
        True if the parser engine has not reached the end of the script.
        Throws:
        ParserException - If a problem is encountered while parsing the script.
      • parseNextLine

        public ParsedActionLine parseNextLine()
                                       throws ParserException
        Parse and return the next line in the script.
        Returns:
        The next line in the script, parsed into action and parameter table.
        Throws:
        ParserException - If a problem is encountered while parsing the script.
      • resetParser

        public void resetParser()
                         throws ParserException
        Resets the parser to the beginning of the script.
        Throws:
        ParserException - If a problem is encountered while resetting the script parser.
      • isCommentLine

        private static boolean isCommentLine​(java.lang.String scriptLine)
        Checks to see if a line is a comment.
        Parameters:
        scriptLine - A line from a script.
        Returns:
        True if the line is a comment, false otherwise.
      • openScriptFile

        private java.io.BufferedReader openScriptFile​(java.lang.String scriptFileToParse)
                                               throws ParserException
        Open a script file and prepare a reader for it.
        Parameters:
        scriptFileToParse - Path on disk to a script file to open.
        Returns:
        A reader for the script file.
        Throws:
        ParserException - If a problem is encountered while parsing the script.
      • openScriptString

        private java.io.BufferedReader openScriptString​(java.lang.String scriptDataToParse)
                                                 throws ParserException
        Prepare a reader for a given string of script data.
        Parameters:
        scriptDataToParse - The contents of a script.
        Returns:
        A reader for the script contents.
        Throws:
        ParserException - If a problem is encountered while parsing the script.
      • parseLine

        private ParsedActionLine parseLine​(java.lang.String scriptLine)
                                    throws ParserException
        Parses a line from a script.
        Parameters:
        scriptLine - A line of text from a script.
        Returns:
        The parsed-out information from the script line.
        Throws:
        ParserException - If a problem is encountered while parsing the script.
      • parseParameterInfo

        private Parameter parseParameterInfo​(java.lang.String paramInfo)
                                      throws ParserException
        Parse out the individual parameter information from a string.
        Parameters:
        paramInfo - Parameter information string.
        Returns:
        The new parameter.
        Throws:
        ParserException - If a problem is encountered while parsing the script.
      • parseParameters

        private ParameterTable parseParameters​(java.lang.String paramString)
                                        throws ParserException
        Splits a list of parameters into individual parameter information strings, parses the individual parameter strings, and puts the parameters into a look up table.
        Parameters:
        paramString - A list of parameters.
        Returns:
        A table containing all the parameters extracted from the string.
        Throws:
        ParserException - If a problem is encountered while parsing the script.