Struct Parser

Source
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>

Source

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 parse
  • line_info - Line information for error reporting
  • context - The compilation context
Source

fn parse(&mut self) -> CompileResult<Vec<Statement>>

Parses the tokens into a list of statements

§Returns

The parsed statements or an error message

Source

fn error(&self, error_code: ErrorCode, message: &str) -> ParseError

Creates an error at the current token position

§Arguments
  • èrror_code - The error code for the error
  • message - The error message to display
§Returns

A new ParseError with the current token position and length

Source

fn error_previous(&self, error_code: ErrorCode, message: &str) -> ParseError

Creates an error at the previous token position

§Arguments
  • error_code - The error code for the error
  • message - The error message to display
§Returns

A new ParseError with the previous token position and length

Source

fn synchronize(&mut self)

Skip until a safe synchronization point (e.g., semicolon or statement start)

Source

fn statement(&mut self) -> Result<Statement, ParseError>

Parses a single statement

§Returns

The parsed statement or an error message

Source

fn return_statement(&mut self) -> Result<Statement, ParseError>

Parses a return statement

§Returns

The parsed return statement or an error message

Source

fn function_declaration_statement(&mut self) -> Result<Statement, ParseError>

Parses a function declaration

§Returns

The parsed function declaration or an error message

Source

fn parameter(&mut self) -> Result<Parameter, ParseError>

Parses a function parameter

§Returns

The parsed parameter or an error message

Source

fn type_definition_statement(&mut self) -> Result<Statement, ParseError>

Parses a type definition (struct declaration)

§Returns

The parsed type definition or an error message

Source

fn let_statement(&mut self) -> Result<Statement, ParseError>

Parses a variable declaration

§Returns

The parsed variable declaration or an error message

Source

fn expression_statement(&mut self) -> Result<Statement, ParseError>

Parses an expression statement

§Returns

The parsed expression statement or an error message

Source

fn expression(&mut self) -> Result<Expression, ParseError>

Parses an expression

§Returns

The parsed expression or an error message

Source

fn logical_or(&mut self) -> Result<Expression, ParseError>

Parses a logical OR expression

§Returns

The parsed logical OR expression or an error message

Source

fn logical_and(&mut self) -> Result<Expression, ParseError>

Parses a logical AND expression

§Returns

The parsed logical AND expression or an error message

Source

fn equality(&mut self) -> Result<Expression, ParseError>

Parses an equality expression (== and !=)

§Returns

The parsed equality expression or an error message

Source

fn comparison(&mut self) -> Result<Expression, ParseError>

Parses a comparison expression (>, <, >=, <=)

§Returns

The parsed comparison expression or an error message

Source

fn term(&mut self) -> Result<Expression, ParseError>

Parses a term (addition/subtraction)

§Returns

The parsed term or an error message

Source

fn factor(&mut self) -> Result<Expression, ParseError>

Parses a factor (multiplication/division)

§Returns

The parsed factor or an error message

Source

fn unary(&mut self) -> Result<Expression, ParseError>

Parses a unary expression

§Returns

The parsed unary expression or an error message

Source

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

Source

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

Source

fn finish_call(&mut self, name: String) -> Result<Expression, ParseError>

Finishes parsing a function call after the name and ‘(’

§Arguments
  • name - The name of the function being called
§Returns

The parsed function call expression or an error message

Source

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

Source

fn parse_type(&mut self) -> Result<TypeId, ParseError>

Parses a type name

§Returns

The type ID for the parsed type or an error

Source

fn source_location_from_token(&self, token: &Token) -> Location

Creates a SourceLocation from a token’s position

Source

fn match_token(&mut self, token_type: &Tokentype) -> bool

Consumes the current token if it matches the expected type

§Arguments
  • token_type - The token type to match
§Returns

true if the token was consumed, false otherwise

Source

fn match_any(&mut self, types: &[Tokentype]) -> bool

Consumes the current token if it matches any of the expected types

§Arguments
  • types - The token types to match
§Returns

true if a token was consumed, false otherwise

Source

fn check(&self, token_type: &Tokentype) -> bool

Checks if the current token is of the expected type

§Arguments
  • token_type - The token type to check for
§Returns

true if the current token matches, false otherwise

Source

fn check_next(&self, token_type: &Tokentype) -> bool

Checks if the next token matches the given type (lookahead of 2)

§Arguments
  • token_type - The token type to check against
§Returns

true if the next token matches, false otherwise

Source

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

Source

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

Source

fn peek(&self) -> &Token

Returns the current token without consuming it

§Returns

The current token

Source

fn previous(&self) -> &Token

Returns the most recently consumed token

§Returns

The previous token

Source

fn assignment_statement(&mut self) -> Result<Statement, ParseError>

Parses an assignment statement

§Returns

The parsed assignment statement or an error message

Source

fn conditional_expression(&mut self) -> Result<Expression, ParseError>

Parses a conditional expression (if/else expression)

§Returns

The parsed conditional expression or an error message

Source

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

Source

fn if_statement(&mut self) -> Result<Statement, ParseError>

Parses an if statement (if/else statement)

§Returns

The parsed if statement or an error message

Source

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

Auto Trait Implementations§

§

impl<'a> Freeze for Parser<'a>

§

impl<'a> RefUnwindSafe for Parser<'a>

§

impl<'a> Send for Parser<'a>

§

impl<'a> Sync for Parser<'a>

§

impl<'a> Unpin for Parser<'a>

§

impl<'a> !UnwindSafe for Parser<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.