feat: parse dot operator

This commit is contained in:
Araozu 2024-11-01 19:24:46 -05:00
parent b4e5caa0f0
commit cef4459648
3 changed files with 51 additions and 2 deletions

View File

@ -0,0 +1,48 @@
use crate::{
lexic::token::Token,
syntax::{
ast::Expression, parsers::expression::utils::try_binary_op, ParsingError, ParsingResult,
},
};
/// Parses a dot access
///
/// ```ebnf
/// dot_access = unary, (("."), unary)*;
/// ```
pub fn try_parse(tokens: &Vec<Token>, pos: usize) -> ParsingResult<Expression> {
let (unary, next_pos) = match super::unary::try_parse(tokens, pos) {
Ok((expr, next_pos)) => (expr, next_pos),
_ => return Err(ParsingError::Unmatched),
};
parse_many(tokens, next_pos, unary, 0)
}
fn parse_many<'a>(
tokens: &'a Vec<Token>,
pos: usize,
prev_expr: Expression<'a>,
indentation_level: u32,
) -> ParsingResult<'a, Expression<'a>> {
// (("/" | "*" | "%"), unary)*
try_binary_op(
tokens,
pos,
prev_expr,
vec!["."],
indentation_level,
|tokens, next_pos, prev_expr, token, indent_count: u32| {
// match next
match super::unary::try_parse(tokens, next_pos) {
Ok((expr, next_pos)) => {
let expr =
Expression::BinaryOperator(Box::new(prev_expr), Box::new(expr), &token);
parse_many(tokens, next_pos, expr, indentation_level + indent_count)
}
_ => return Err(ParsingError::Unmatched),
}
},
)
}

View File

@ -8,10 +8,10 @@ use crate::{
/// Parses a factor expression. /// Parses a factor expression.
/// ///
/// ```ebnf /// ```ebnf
/// factor = unary, (("/" | "*" | "%"), unary)*; /// factor = dot_access, (("/" | "*" | "%"), dot_access)*;
/// ``` /// ```
pub fn try_parse(tokens: &Vec<Token>, pos: usize) -> ParsingResult<Expression> { pub fn try_parse(tokens: &Vec<Token>, pos: usize) -> ParsingResult<Expression> {
let (unary, next_pos) = match super::unary::try_parse(tokens, pos) { let (unary, next_pos) = match super::dot_access::try_parse(tokens, pos) {
Ok((expr, next_pos)) => (expr, next_pos), Ok((expr, next_pos)) => (expr, next_pos),
_ => return Err(ParsingError::Unmatched), _ => return Err(ParsingError::Unmatched),
}; };

View File

@ -3,6 +3,7 @@ use crate::{lexic::token::Token, syntax::parseable::Parseable};
mod array; mod array;
mod comparison; mod comparison;
mod dot_access;
mod equality; mod equality;
mod factor; mod factor;
pub mod function_call_expr; pub mod function_call_expr;