refactor: organize files for parsing expressions
This commit is contained in:
parent
0393995a49
commit
7332a2e13f
@ -1,19 +0,0 @@
|
||||
use super::{ast::Expression, ParsingResult};
|
||||
use crate::lexic::token::Token;
|
||||
|
||||
mod comparison;
|
||||
mod equality;
|
||||
mod factor;
|
||||
pub mod function_call_expr;
|
||||
mod primary;
|
||||
mod term;
|
||||
mod unary;
|
||||
|
||||
/// Expression is defined in the grammar.
|
||||
pub fn try_parse(tokens: &Vec<Token>, pos: usize) -> ParsingResult<Expression> {
|
||||
// TODO: This must be newline/indentation aware
|
||||
equality::try_parse(tokens, pos)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {}
|
@ -3,6 +3,7 @@ use crate::{
|
||||
lexic::token::{Token, TokenType},
|
||||
syntax::{
|
||||
ast::{functions::ArgumentsList, Expression},
|
||||
parseable::Parseable,
|
||||
utils::parse_token_type,
|
||||
ParsingError, ParsingResult,
|
||||
},
|
||||
@ -22,15 +23,14 @@ pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> ParsingResult<Argume
|
||||
|
||||
let mut arguments = Vec::<Expression>::new();
|
||||
loop {
|
||||
let (next_expression, next_pos) =
|
||||
match super::super::expression::try_parse(tokens, current_pos) {
|
||||
Ok((expression, next_pos)) => (expression, next_pos),
|
||||
Err(ParsingError::Err(error)) => {
|
||||
// TODO: Write a more detailed error
|
||||
return Err(ParsingError::Err(error));
|
||||
}
|
||||
_ => break,
|
||||
};
|
||||
let (next_expression, next_pos) = match Expression::try_parse(tokens, current_pos) {
|
||||
Ok((expression, next_pos)) => (expression, next_pos),
|
||||
Err(ParsingError::Err(error)) => {
|
||||
// TODO: Write a more detailed error
|
||||
return Err(ParsingError::Err(error));
|
||||
}
|
||||
_ => break,
|
||||
};
|
||||
current_pos = next_pos;
|
||||
|
||||
arguments.push(next_expression);
|
||||
|
@ -1,6 +1,5 @@
|
||||
use crate::error_handling::MistiError;
|
||||
|
||||
mod expression;
|
||||
mod functions;
|
||||
mod parseable;
|
||||
mod parsers;
|
||||
|
@ -114,7 +114,7 @@ mod tests {
|
||||
BlockMember::Stmt(Statement::FnDecl(f)) => {
|
||||
assert_eq!(f.identifier.value, "f");
|
||||
}
|
||||
_ => panic!("Expected a function declaration, got {:?}", member)
|
||||
_ => panic!("Expected a function declaration, got {:?}", member),
|
||||
}
|
||||
}
|
||||
|
||||
@ -131,7 +131,7 @@ mod tests {
|
||||
BlockMember::Stmt(Statement::FnDecl(f)) => {
|
||||
assert_eq!(f.identifier.value, "f");
|
||||
}
|
||||
_ => panic!("Expected a function declaration, got {:?}", member)
|
||||
_ => panic!("Expected a function declaration, got {:?}", member),
|
||||
}
|
||||
|
||||
let member = &block.members[1];
|
||||
@ -139,7 +139,7 @@ mod tests {
|
||||
BlockMember::Stmt(Statement::FnDecl(f)) => {
|
||||
assert_eq!(f.identifier.value, "g");
|
||||
}
|
||||
_ => panic!("Expected a function declaration, got {:?}", member)
|
||||
_ => panic!("Expected a function declaration, got {:?}", member),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,16 +0,0 @@
|
||||
use crate::{
|
||||
lexic::token::Token,
|
||||
syntax::{
|
||||
ast::Expression,
|
||||
expression,
|
||||
parseable::{Parseable, ParsingResult},
|
||||
},
|
||||
};
|
||||
|
||||
impl<'a> Parseable<'a> for Expression<'a> {
|
||||
type Item = Expression<'a>;
|
||||
|
||||
fn try_parse(tokens: &'a Vec<Token>, current_pos: usize) -> ParsingResult<'a, Self::Item> {
|
||||
expression::try_parse(tokens, current_pos)
|
||||
}
|
||||
}
|
22
src/syntax/parsers/expression/mod.rs
Normal file
22
src/syntax/parsers/expression/mod.rs
Normal file
@ -0,0 +1,22 @@
|
||||
use super::super::{ast::Expression, ParsingResult};
|
||||
use crate::{lexic::token::Token, syntax::parseable::Parseable};
|
||||
|
||||
mod comparison;
|
||||
mod equality;
|
||||
mod factor;
|
||||
pub mod function_call_expr;
|
||||
mod primary;
|
||||
mod term;
|
||||
mod unary;
|
||||
|
||||
impl<'a> Parseable<'a> for Expression<'a> {
|
||||
type Item = Expression<'a>;
|
||||
|
||||
fn try_parse(tokens: &'a Vec<Token>, current_pos: usize) -> ParsingResult<'a, Self::Item> {
|
||||
// TODO: This must be newline/indentation aware
|
||||
equality::try_parse(tokens, current_pos)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {}
|
@ -1,7 +1,8 @@
|
||||
use super::super::utils::Tokenizer;
|
||||
use crate::{
|
||||
lexic::token::{Token, TokenType},
|
||||
syntax::{ast::Expression, ParsingError, ParsingResult},
|
||||
syntax::{
|
||||
ast::Expression, parseable::Parseable, utils::Tokenizer, ParsingError, ParsingResult,
|
||||
},
|
||||
};
|
||||
|
||||
/// This grammar may not be up to date. Refer to the spec for the latest grammar.
|
||||
@ -27,7 +28,7 @@ pub fn try_parse(tokens: &Vec<Token>, pos: usize) -> ParsingResult<Expression> {
|
||||
}
|
||||
|
||||
fn parse_parenthesized_expression(tokens: &Vec<Token>, pos: usize) -> ParsingResult<Expression> {
|
||||
let expression = super::try_parse(tokens, pos + 1);
|
||||
let expression = Expression::try_parse(tokens, pos + 1);
|
||||
match expression {
|
||||
Ok((expression, next_pos)) => match tokens.get(next_pos) {
|
||||
Some(token) => match token.token_type {
|
@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
lexic::token::Token,
|
||||
syntax::{ast::Expression, ParsingError, ParsingResult},
|
||||
syntax::{ast::Expression, parseable::Parseable, ParsingError, ParsingResult},
|
||||
};
|
||||
|
||||
use super::function_call_expr;
|
||||
@ -14,7 +14,7 @@ use super::function_call_expr;
|
||||
pub fn try_parse(tokens: &Vec<Token>, pos: usize) -> ParsingResult<Expression> {
|
||||
match tokens.get(pos) {
|
||||
Some(token) if token.value == "!" || token.value == "-" => {
|
||||
match super::try_parse(tokens, pos + 1) {
|
||||
match Expression::try_parse(tokens, pos + 1) {
|
||||
Ok((expression, next_pos)) => Ok((
|
||||
Expression::UnaryOperator(&token.value, Box::new(expression)),
|
||||
next_pos,
|
Loading…
Reference in New Issue
Block a user