pub struct Parser<'a> {
tokens: &'a [Token],
current: usize,
line_info: &'a LineInfo<'a>,
errors: Vec<CompilerError>,
context: &'a mut CompilationContext,
}
Expand description
Parser that converts tokens into an abstract syntax tree
Fields§
§tokens: &'a [Token]
The tokens being parsed
current: usize
Current position in the token list
line_info: &'a LineInfo<'a>
Line information for error reporting
errors: Vec<CompilerError>
Errors collected during parsing
context: &'a mut CompilationContext
Compilation context for type information
Implementations§
Source§impl<'a> Parser<'a>
impl<'a> Parser<'a>
Sourcefn new(
tokens: &'a [Token],
line_info: &'a LineInfo<'_>,
context: &'a mut CompilationContext,
) -> Self
fn new( tokens: &'a [Token], line_info: &'a LineInfo<'_>, context: &'a mut CompilationContext, ) -> Self
Creates a new parser for the given tokens and line information
§Arguments
tokens
- The tokens to parseline_info
- Line information for error reportingcontext
- The compilation context
Sourcefn parse(&mut self) -> CompileResult<Vec<Statement>>
fn parse(&mut self) -> CompileResult<Vec<Statement>>
Sourcefn error(&self, error_code: ErrorCode, message: &str) -> ParseError
fn error(&self, error_code: ErrorCode, message: &str) -> ParseError
Sourcefn error_previous(&self, error_code: ErrorCode, message: &str) -> ParseError
fn error_previous(&self, error_code: ErrorCode, message: &str) -> ParseError
Sourcefn synchronize(&mut self)
fn synchronize(&mut self)
Skip until a safe synchronization point (e.g., semicolon or statement start)
Sourcefn statement(&mut self) -> Result<Statement, ParseError>
fn statement(&mut self) -> Result<Statement, ParseError>
Sourcefn return_statement(&mut self) -> Result<Statement, ParseError>
fn return_statement(&mut self) -> Result<Statement, ParseError>
Sourcefn function_declaration_statement(&mut self) -> Result<Statement, ParseError>
fn function_declaration_statement(&mut self) -> Result<Statement, ParseError>
Sourcefn parameter(&mut self) -> Result<Parameter, ParseError>
fn parameter(&mut self) -> Result<Parameter, ParseError>
Sourcefn type_definition_statement(&mut self) -> Result<Statement, ParseError>
fn type_definition_statement(&mut self) -> Result<Statement, ParseError>
Parses a type definition (struct declaration)
§Returns
The parsed type definition or an error message
Sourcefn let_statement(&mut self) -> Result<Statement, ParseError>
fn let_statement(&mut self) -> Result<Statement, ParseError>
Sourcefn expression_statement(&mut self) -> Result<Statement, ParseError>
fn expression_statement(&mut self) -> Result<Statement, ParseError>
Sourcefn expression(&mut self) -> Result<Expression, ParseError>
fn expression(&mut self) -> Result<Expression, ParseError>
Sourcefn logical_or(&mut self) -> Result<Expression, ParseError>
fn logical_or(&mut self) -> Result<Expression, ParseError>
Sourcefn logical_and(&mut self) -> Result<Expression, ParseError>
fn logical_and(&mut self) -> Result<Expression, ParseError>
Sourcefn equality(&mut self) -> Result<Expression, ParseError>
fn equality(&mut self) -> Result<Expression, ParseError>
Parses an equality expression (== and !=)
§Returns
The parsed equality expression or an error message
Sourcefn comparison(&mut self) -> Result<Expression, ParseError>
fn comparison(&mut self) -> Result<Expression, ParseError>
Parses a comparison expression (>, <, >=, <=)
§Returns
The parsed comparison expression or an error message
Sourcefn term(&mut self) -> Result<Expression, ParseError>
fn term(&mut self) -> Result<Expression, ParseError>
Sourcefn factor(&mut self) -> Result<Expression, ParseError>
fn factor(&mut self) -> Result<Expression, ParseError>
Sourcefn unary(&mut self) -> Result<Expression, ParseError>
fn unary(&mut self) -> Result<Expression, ParseError>
Sourcefn primary(&mut self) -> Result<Expression, ParseError>
fn primary(&mut self) -> Result<Expression, ParseError>
Parses a primary expression (literal, variable, or grouped expression)
§Returns
The parsed primary expression or an error message
Sourcefn parse_float(&mut self) -> Result<Expression, ParseError>
fn parse_float(&mut self) -> Result<Expression, ParseError>
Parses a float literal with optional type suffix
§Returns
The parsed float literal expression or an error message
Sourcefn finish_call(&mut self, name: String) -> Result<Expression, ParseError>
fn finish_call(&mut self, name: String) -> Result<Expression, ParseError>
Sourcefn parse_integer(&mut self) -> Result<Expression, ParseError>
fn parse_integer(&mut self) -> Result<Expression, ParseError>
Parses an integer literal with optional type suffix
§Returns
The parsed integer literal expression or an error message
Sourcefn parse_type(&mut self) -> Result<TypeId, ParseError>
fn parse_type(&mut self) -> Result<TypeId, ParseError>
Sourcefn source_location_from_token(&self, token: &Token) -> Location
fn source_location_from_token(&self, token: &Token) -> Location
Creates a SourceLocation from a token’s position
Sourcefn match_token(&mut self, token_type: &Tokentype) -> bool
fn match_token(&mut self, token_type: &Tokentype) -> bool
Sourcefn check_next(&self, token_type: &Tokentype) -> bool
fn check_next(&self, token_type: &Tokentype) -> bool
Sourcefn advance(&mut self) -> &Token
fn advance(&mut self) -> &Token
Advances to the next token and returns the previous token
§Returns
The token that was current before advancing, if the end of the token stream was not reached Otherwise, returns the last token
Sourcefn is_at_end(&self) -> bool
fn is_at_end(&self) -> bool
Checks if we’ve reached the end of the token stream
§Returns
true if all tokens have been procesed, false otherwise
Sourcefn assignment_statement(&mut self) -> Result<Statement, ParseError>
fn assignment_statement(&mut self) -> Result<Statement, ParseError>
Sourcefn conditional_expression(&mut self) -> Result<Expression, ParseError>
fn conditional_expression(&mut self) -> Result<Expression, ParseError>
Parses a conditional expression (if/else expression)
§Returns
The parsed conditional expression or an error message
Sourcefn parse_block_expression(&mut self) -> Result<BlockExpr, ParseError>
fn parse_block_expression(&mut self) -> Result<BlockExpr, ParseError>
Parses a block expression - a sequence of statements with an optional return expression
§Returns
The parsed block expression or an error message
Sourcefn if_statement(&mut self) -> Result<Statement, ParseError>
fn if_statement(&mut self) -> Result<Statement, ParseError>
Sourcefn parse_function_type_expression(&mut self) -> Result<Expression, ParseError>
fn parse_function_type_expression(&mut self) -> Result<Expression, ParseError>
Parses a function type expression: fn(type1, type2) -> return_type
§Returns
The parsed function type expression or an error message