Class Lexer.Parser

java.lang.Object
net.mtrop.doom.struct.Lexer.Parser
Enclosing class:
Lexer

public abstract static class Lexer.Parser extends Object
Abstract parser class. This class aids in the creation of top-down (AKA recursive-descent) parsers.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Thrown when a Parser has a problem.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Parser(Lexer lexer)
    Constructs the parser and binds a Lexer to it.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    changeTokenType(int type)
    Changes the current token's type to a different type.
    protected String
     
    protected Lexer.Token
    Gets the token read from the last nextToken() call.
    protected boolean
    currentType(int tokenType)
    Attempts to match the type of the current token.
    protected boolean
    currentType(int... tokenTypes)
    Attempts to match the type of the current token.
    protected boolean
    currentType(int tokenType1, int tokenType2)
    Attempts to match the type of the current token.
    protected boolean
    currentType(int tokenType1, int tokenType2, int tokenType3)
    Attempts to match the type of the current token.
    Gets the Lexer that this Parser uses.
    Returns a stock error line for when an error/warning or whatever occurs during parse.
    protected boolean
    matchType(int tokenType)
    Matches the current token.
    protected void
    Reads and sets the current token to the next token.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Parser

      protected Parser(Lexer lexer)
      Constructs the parser and binds a Lexer to it.
      Parameters:
      lexer - the lexer that this reads from.
  • Method Details

    • getLexer

      public Lexer getLexer()
      Gets the Lexer that this Parser uses.
      Returns:
      the underlying Lexer.
    • currentToken

      protected Lexer.Token currentToken()
      Gets the token read from the last nextToken() call.
      Returns:
      the current token.
    • currentLexeme

      protected String currentLexeme()
      Returns:
      the current token's lexeme, or null if no current token.
      See Also:
    • matchType

      protected boolean matchType(int tokenType)
      Matches the current token. If matched, this returns true and advances to the next token. Else, this returns false.
      Parameters:
      tokenType - the type to match.
      Returns:
      true if matched, false if not.
    • currentType

      protected boolean currentType(int tokenType)
      Attempts to match the type of the current token. If matched, this returns true. This DOES NOT ADVANCE to the next token.
      Parameters:
      tokenType - the token type.
      Returns:
      true if matched, false if not.
    • currentType

      protected boolean currentType(int tokenType1, int tokenType2)
      Attempts to match the type of the current token. If matched, this returns true. This DOES NOT ADVANCE to the next token.
      Parameters:
      tokenType1 - the first token type.
      tokenType2 - the second token type.
      Returns:
      true if one was matched, false if not.
    • currentType

      protected boolean currentType(int tokenType1, int tokenType2, int tokenType3)
      Attempts to match the type of the current token. If matched, this returns true. This DOES NOT ADVANCE to the next token.
      Parameters:
      tokenType1 - the first token type.
      tokenType2 - the second token type.
      tokenType3 - the third token type.
      Returns:
      true if one was matched, false if not.
    • currentType

      protected boolean currentType(int... tokenTypes)
      Attempts to match the type of the current token. If matched, this returns true. This DOES NOT ADVANCE to the next token.
      Parameters:
      tokenTypes - the list of types.
      Returns:
      true if one was matched, false if not.
    • nextToken

      protected void nextToken()
      Reads and sets the current token to the next token. If the current token is null, it is the end of the Lexer's stream.
      Throws:
      Lexer.Parser.Exception - if the next token can't be read.
    • changeTokenType

      protected void changeTokenType(int type)
      Changes the current token's type to a different type. Only useful after nextToken() but before it is parsed by your parser.
      Parameters:
      type - the new type id.
    • getTokenInfoLine

      public String getTokenInfoLine(String message)
      Returns a stock error line for when an error/warning or whatever occurs during parse. For convenience only - not called by any of the current methods.
      Parameters:
      message - the message to append.
      Returns:
      the error message.