pub struct Parser {
pub tokens: Vec<Token>,
pub current: usize,
pub context_stack: Vec<ParseContext>,
}Expand description
A parser that converts a list of tokens into an Abstract Syntax Tree (AST).
This struct takes tokens generated by the lexer and produces an AST, which represents the structure of the source code in a tree format.
Fields§
§tokens: Vec<Token>A list of tokens to be parsed.
current: usizeThe current index of the token being processed.
context_stack: Vec<ParseContext>The current context stack for parsing.
Implementations§
Source§impl Parser
impl Parser
Sourcepub fn parse_expr(&mut self) -> Result<Expr, Error>
pub fn parse_expr(&mut self) -> Result<Expr, Error>
Parses any expression, starting with an assignment.
This method serves as the entry point for expression parsing.
§Returns
Ok(Expr): The parsed expression if successful.Err(anyhow::Error): An error if parsing fails.
Sourcepub fn expression_stmt(&mut self) -> Result<Stmt, Error>
pub fn expression_stmt(&mut self) -> Result<Stmt, Error>
Parses an expression statement.
This method parses an expression and checks for a semicolon to terminate the statement.
§Returns
Ok(Stmt): The expression statement if successful.Err(anyhow::Error): An error if parsing fails.
Sourcepub fn parse_binary_expression(&mut self) -> Result<Expr, Error>
pub fn parse_binary_expression(&mut self) -> Result<Expr, Error>
Parses a binary expression.
This method first parses a unary expression and then handles the binary operators in the expression.
§Returns
Ok(Expr): The parsed binary expression if successful.Err(anyhow::Error): An error if parsing fails.
Sourcepub fn parse_binary_expression_recurse(
&mut self,
left: Expr,
precedence: u8,
) -> Result<Expr, Error>
pub fn parse_binary_expression_recurse( &mut self, left: Expr, precedence: u8, ) -> Result<Expr, Error>
Parses binary expressions recursively, handling operator precedence and associativity.
This method continues to parse binary expressions until no further valid operators are found.
§Parameters
left: The left-hand side expression.precedence: The precedence of the operator being processed.
§Returns
Ok(Expr): The final parsed expression if successful.Err(anyhow::Error): An error if parsing fails.
Sourcepub fn parse_unary_operator(&mut self) -> Option<UnOperator>
pub fn parse_unary_operator(&mut self) -> Option<UnOperator>
Attempts to parse a unary operator.
This method checks the next token to see if it’s a unary operator and returns it if found.
§Returns
Some(UnOperator): The parsed unary operator if found.None: If no unary operator is found.
Sourcepub fn parse_unary_expression(&mut self) -> Result<Expr, Error>
pub fn parse_unary_expression(&mut self) -> Result<Expr, Error>
Parses a unary expression, handling unary operators.
This method checks for a unary operator and processes the operand accordingly.
§Returns
Ok(Expr): The parsed unary expression if successful.Err(anyhow::Error): An error if parsing fails.
Sourcepub fn parse_access_expression(&mut self) -> Result<Expr, Error>
pub fn parse_access_expression(&mut self) -> Result<Expr, Error>
Parses an access expression.
Sourcepub fn parse_struct_constructor(
&mut self,
identifier: Token,
) -> Result<Expr, Error>
pub fn parse_struct_constructor( &mut self, identifier: Token, ) -> Result<Expr, Error>
Parses a struct constructor expression.
This method expects an identifier followed by a left brace and a list of field assignments.
§Parameters
identifier: The token representing the struct name.
§Returns
Ok(Expr): The parsed struct constructor expression if successful.Err(anyhow::Error): An error if parsing fails.
Sourcepub fn parse_primary_expression(&mut self) -> Result<Expr, Error>
pub fn parse_primary_expression(&mut self) -> Result<Expr, Error>
Parses a primary expression, such as literals, identifiers, or parenthesized expressions.
§Returns
Ok(Expr): The parsed primary expression if successful.Err(anyhow::Error): An error if parsing fails.
Sourcepub fn parse_optional_type_annotation(
&mut self,
) -> Result<Option<TypeAnnotation>, Error>
pub fn parse_optional_type_annotation( &mut self, ) -> Result<Option<TypeAnnotation>, Error>
Parses an optional type annotation.
This method checks for a colon followed by a type annotation and parses it if present.
§Returns
Ok(Some(TypeAnnotation)): The parsed type annotation if present.Ok(None): If no type annotation is present.
Source§impl Parser
impl Parser
Sourcepub fn parse_stmt(&mut self) -> Result<Option<Stmt>, Error>
pub fn parse_stmt(&mut self) -> Result<Option<Stmt>, Error>
Parses a statement from the tokens.
This method checks the type of token to determine the kind of statement it should create. It supports function declarations, variable assignments, control flow, and more.
§Returns
Ok(Some(Stmt)): A parsed statement.Ok(None): If the token is a comment or semicolon.Err: If there is a parsing error.
Sourcepub fn parse_impl(
&mut self,
impl_keyword: Token,
ident: Token,
) -> Result<Stmt, Error>
pub fn parse_impl( &mut self, impl_keyword: Token, ident: Token, ) -> Result<Stmt, Error>
Parses an impl block for implementing a struct.
An impl block is used to implement methods for a struct.
§Returns
Ok(Stmt): An impl block.Err: If there is a parsing error.
Sourcepub fn parse_trait_impl(
&mut self,
impl_keyword: Token,
ident: Token,
) -> Result<Stmt, Error>
pub fn parse_trait_impl( &mut self, impl_keyword: Token, ident: Token, ) -> Result<Stmt, Error>
Parses an impl block for implementing a trait.
An impl block is used to implement methods for a trait.
§Returns
Ok(Stmt): An impl block.Err: If there is a parsing error.
Sourcepub fn parse_pub(&mut self, expected: TokenKind) -> Result<(Token, bool), Error>
pub fn parse_pub(&mut self, expected: TokenKind) -> Result<(Token, bool), Error>
Parses a ‘pub’ keyword (if present) followed by an identifier.
Sourcepub fn parse_trait(&mut self) -> Result<Stmt, Error>
pub fn parse_trait(&mut self) -> Result<Stmt, Error>
Parses a trait declaration.
A trait declaration defines a new interface that can be implemented by other types.
§Returns
Ok(Stmt): A trait declaration.Err: If there is a parsing error.
Sourcepub fn parse_const(&mut self) -> Result<Stmt, Error>
pub fn parse_const(&mut self) -> Result<Stmt, Error>
Parses an expression statement.
An expression statement is a statement that consists of an expression followed by a semicolon.
§Returns
Stmt: An expression statement.Err: If there is a parsing error.
Sourcepub fn parse_struct(&mut self) -> Result<Stmt, Error>
pub fn parse_struct(&mut self) -> Result<Stmt, Error>
Parses a struct declaration.
A struct declaration defines a new data structure with named fields.
§Returns
Ok(Stmt): A struct declaration.Err: If there is a parsing error.
Sourcepub fn parse_while(&mut self) -> Result<Option<Stmt>, Error>
pub fn parse_while(&mut self) -> Result<Option<Stmt>, Error>
Parses a while statement.
A while statement is used to execute a block of code repeatedly as long as a condition is true.
§Returns
Ok(Stmt): A while statement.Err: If there is a parsing error.
Sourcepub fn parse_throw(&mut self) -> Result<Stmt, Error>
pub fn parse_throw(&mut self) -> Result<Stmt, Error>
Parses a throw statement.
A throw statement is used to raise an exception.
§Returns
Ok(Stmt): A throw statement.Err: If there is a parsing error.
Sourcepub fn parse_try(&mut self) -> Result<Stmt, Error>
pub fn parse_try(&mut self) -> Result<Stmt, Error>
Parses a try statement with a catch block.
The try statement lets you catch exceptions and handle errors in a safe way.
§Returns
Ok(Stmt): A try-catch statement.Err: If there is a parsing error.
Sourcepub fn parse_return(&mut self) -> Result<Option<Stmt>, Error>
pub fn parse_return(&mut self) -> Result<Option<Stmt>, Error>
Parses a return statement.
The return statement is used to return a value from a function.
§Returns
Ok(Some(Stmt)): A return statement with or without a value.Err: If there is a parsing error.
Sourcepub fn parse_let(&mut self) -> Result<Stmt, Error>
pub fn parse_let(&mut self) -> Result<Stmt, Error>
Parses a let statement.
A let statement declares a new variable with an optional type annotation.
§Returns
Ok(Stmt): A variable declaration statement.Err: If there is a parsing error.
Sourcepub fn parse_if(&mut self) -> Result<Stmt, Error>
pub fn parse_if(&mut self) -> Result<Stmt, Error>
Parses an if statement with optional else if and else blocks.
An if statement is used for conditional logic.
§Returns
Ok(Stmt): An if statement with possible elseif and else blocks.Err: If there is a parsing error.
Sourcepub fn parse_use(&mut self) -> Result<Stmt, Error>
pub fn parse_use(&mut self) -> Result<Stmt, Error>
Parses a use statement for importing modules.
A use statement allows importing items from other modules or files.
§Returns
Ok(Stmt): A use statement.Err: If there is a parsing error.
Sourcepub fn is_nullable(&mut self) -> bool
pub fn is_nullable(&mut self) -> bool
Checks if the next token is a question mark and consumes it.
Sourcepub fn parse_type_annotation(&mut self) -> Result<TypeAnnotation, Error>
pub fn parse_type_annotation(&mut self) -> Result<TypeAnnotation, Error>
Parses a type annotation following a variable or parameter.
§Returns
Ok(TypeAnnotation): A parsed type annotation.Err: If there is a parsing error.
Sourcepub fn parse_return_type(&mut self) -> Result<Option<FunctionType>, Error>
pub fn parse_return_type(&mut self) -> Result<Option<FunctionType>, Error>
Parses the return type of function.
§Returns
Ok(Some(FunctionType)): If the return type is parsed.Ok(None): If no return type is provided.Err: If the syntax is incorrect.
Sourcepub fn validate_type_name(token: Token) -> Result<(), Error>
pub fn validate_type_name(token: Token) -> Result<(), Error>
Validates if the provided string is valid type name.
§Returns
Ok(()): If the type name is valid.Err: If the type name is invalid.
Source§impl Parser
impl Parser
Sourcepub fn current_context(&self) -> &ParseContext
pub fn current_context(&self) -> &ParseContext
Returns a reference to the current parser context.
§Panics
This function will panic if the context_stack is empty. However, this condition
should never happen because the context stack should always have at least one context.
Sourcepub fn push_context(&mut self, context: ParseContext)
pub fn push_context(&mut self, context: ParseContext)
Pushes a new context onto the context stack.
§Parameters
context: TheParseContextto be pushed onto the stack.
Sourcepub fn pop_context(&mut self)
pub fn pop_context(&mut self)
Pops the current context off the context stack.
Sourcepub fn is_context(&self, context: &ParseContext) -> bool
pub fn is_context(&self, context: &ParseContext) -> bool
Source§impl Parser
impl Parser
Sourcepub fn parse(&mut self) -> Result<Ast, Error>
pub fn parse(&mut self) -> Result<Ast, Error>
Parses the list of tokens and constructs an AST.
This method will continue parsing tokens until the end of the token stream (or an EOF token) is encountered. Each token sequence is turned into a statement and added to the AST.
§Returns
Ok(Ast)- If the parsing is successful and an AST is created.Err- If an error occurs during parsing.
Sourcepub fn consume(&mut self) -> Token
pub fn consume(&mut self) -> Token
Consumes the current token and advances to the next token in the stream.
§Returns
- The previous token that was consumed.
Sourcepub fn is_eof(&self) -> bool
pub fn is_eof(&self) -> bool
Checks if the current token is the end of file (EOF).
§Returns
trueif the current token is EOF, otherwisefalse.
Sourcepub fn possible_check(&mut self, kind: TokenKind)
pub fn possible_check(&mut self, kind: TokenKind)
Consumes the current token if it matches the specified TokenKind.
§Arguments
kind- The kind of token to check.
Sourcepub fn expect(&mut self, kind: TokenKind) -> Result<Token, Error>
pub fn expect(&mut self, kind: TokenKind) -> Result<Token, Error>
Expects the current token to be of a specific TokenKind and consumes it.
If the token matches the expected kind, it is consumed and returned. If not, an error is raised indicating the expected token.
§Arguments
kind- The expected kind of the current token.
§Returns
Ok(Token)- The token that was consumed.Err- An error if the current token is not of the expected kind.