Class ScriptParser

java.lang.Object
com.blackrook.rookscript.struct.Lexer.Parser
com.blackrook.rookscript.compiler.ScriptParser

public class ScriptParser extends Lexer.Parser
The parser that parses text for the script reader.
Author:
Matthew Tropiano
  • Field Details

  • Constructor Details

    • ScriptParser

      public ScriptParser(ScriptLexer lexer)
      Creates a new script parser.
      Parameters:
      lexer - the lexer to fetch tokens from.
  • Method Details

    • getErrorMessages

      public String[] getErrorMessages()
      Returns:
      the list of error messages emitted by this parser.
    • getErrors

      public ScriptParser.ErrorMessage[] getErrors()
      Returns:
      the list of error messages emitted by this parser.
      Since:
      1.11.0, this is exposed to implementors.
    • readScript

      public void readScript(Script script)
      Starts parsing a script.

      This is equivalent to:

       nextToken();
       parseScript(script);
       ErrorMessage[] errors = getErrorMessages();
       if (errors.length > 0)
           throw new ScriptParseException(errors);
       
      Parameters:
      script - the script to start adding compiled code to.
      Throws:
      ScriptParseException - if any errors were logged.
      Since:
      1.11.0, this calls parseScript(Script)
    • parseScript

      public boolean parseScript(Script script)
      Parses a script from the current token until the end of the token stream is reached or an error is encountered or an exception is thrown.

      If this function returns false, then one or more errors may have been encountered, and they should be fetched from getErrorMessages() for display or logging.

      Parameters:
      script - the script to emit commands and entries to.
      Returns:
      true if no errors were encountered, or false if a parse error occurred and parsing was halted.
      Since:
      1.11.0
    • parseScriptlet

      public Integer parseScriptlet(Script script)
      Parses a scriptlet into an existing script. A scriptlet is either a single function call statement (scoped or not), or a set of statements between curly braces. Scriptlets are demarcated in the output script with labels.

      Use caution when parsing scriptlets - tokens are parsed and emitted as-is as though they were part of a full script!

       [Scriptlet] :=
                      [IDENTIFIER] [FunctionCall] 
                      "{" [StatementList] "}"
       
      Parameters:
      script - the script to emit commands into.
      Returns:
      the index of the scriptlet starting point if no errors were encountered, or null if a parse error occurred and parsing was halted.
      Since:
      1.11.0, this has been made public.
    • parseFunctionEntry

      public boolean parseFunctionEntry(Script script, boolean checkMode)
      Parses a single function entry (as though the token "function" was already parsed). The function entry is added to the script, and the label and the rest of the function are emitted into the script.

      Use caution when parsing functions - tokens are parsed and emitted as-is as though they were part of a full script!

       [FunctionEntry] := 
           [IDENTIFIER] "(" [FunctionArgumentList] ")" "{" [StatementList] "}"
       

      If this method returns false, a parse error occurred and you must check for any emitted errors via getErrorMessages().

      Parameters:
      script - the script to add the function to and emit commands into.
      checkMode - if true, the function is parsed as though "check" was prepended to the function entry.
      Returns:
      true if no errors were encountered, or false if a parse error occurred and parsing was halted.
      Since:
      1.11.0, this has been made public.
    • parseEntryPoint

      public boolean parseEntryPoint(Script script, boolean checkMode)
      Parses a single entry point (as though the token "entry" was already parsed). The entry point is added to the script, and the label and the rest of the entry are emitted into the script.

      Use caution when parsing entry points - tokens are parsed and emitted as-is as though they were part of a full script!

       [ScriptEntry] := 
           [ScriptName] "(" [ScriptEntryArgumentList] ")" "{" [StatementList] "}"
       

      If this method returns false, a parse error occurred and you must check for any emitted errors via getErrorMessages().

      Parameters:
      script - the script to add the entry point to and emit commands into.
      checkMode - if true, the entry point is parsed as though "check" was prepended to the entry.
      Returns:
      true if no errors were encountered, or false if a parse error occurred and parsing was halted.
      Since:
      1.11.0, this has been made public.
    • addErrorMessage

      protected void addErrorMessage(String message)
      Adds an error message (along with current token info) to the error message list.
      Parameters:
      message - the error message.
      Since:
      1.11.0, this is exposed as a protected method.
    • parseValueFor

      protected <T> T parseValueFor(Class<T> clazz)
    • mark

      protected int mark(Script currentScript, String label)
      Marks a label on the current command.
      Parameters:
      currentScript - the current script.
      label - the label to set for the current command index.
      Returns:
      the command index marked.