Class Parser


  • public class Parser
    extends Object
    Recursive-descent parser over a Lexer token stream, tracking a scope stack so declared variable types are known while parsing expressions.
    • Constructor Detail

      • Parser

        public Parser​(Lexer lexer,
                      String source,
                      ParseErrorListener errorListener)
        Creates a parser and reads the first token.
        Parameters:
        lexer - supplies the token stream
        source - the text the tokens came from, used for error context
        errorListener - where parse errors are reported; defaults to rethrowing when null
        Throws:
        ParseException - if the listener rethrows and the first token cannot be read
      • Parser

        public Parser​(Lexer lexer,
                      String source)
        Creates a parser that throws on the first parse error.
        Parameters:
        lexer - supplies the token stream
        source - the text the tokens came from, used for error context
        Throws:
        ParseException - if the first token cannot be read
    • Method Detail

      • parseCompilationUnit

        public CompilationUnit parseCompilationUnit()
        Parses a whole file: optional package declaration, imports, then type declarations.
        Returns:
        the compilation unit
        Throws:
        ParseException - if the input does not parse and the error listener rethrows
      • parseExpression

        public Expression parseExpression()
        Parses a single expression, assignment included.
        Returns:
        the expression
        Throws:
        ParseException - if the input does not parse and the error listener rethrows
      • parseStatement

        public Statement parseStatement()
        Parses a single statement.
        Returns:
        the statement
        Throws:
        ParseException - if the input does not parse and the error listener rethrows
      • parseType

        public SourceType parseType()
        Parses a type reference, including array and generic forms.
        Returns:
        the type
        Throws:
        ParseException - if the input does not parse and the error listener rethrows