Package com.tonic.analysis.source.parser
Class Parser
- java.lang.Object
-
- com.tonic.analysis.source.parser.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.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CompilationUnitparseCompilationUnit()Parses a whole file: optional package declaration, imports, then type declarations.ExpressionparseExpression()Parses a single expression, assignment included.StatementparseStatement()Parses a single statement.SourceTypeparseType()Parses a type reference, including array and generic forms.
-
-
-
Constructor Detail
-
Parser
public Parser(Lexer lexer, String source, ParseErrorListener errorListener)
Creates a parser and reads the first token.- Parameters:
lexer- supplies the token streamsource- the text the tokens came from, used for error contexterrorListener- 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 streamsource- 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
-
-