diff --git a/src/syntax/expression/mod.rs b/src/syntax/expression/mod.rs deleted file mode 100644 index 8431bb9..0000000 --- a/src/syntax/expression/mod.rs +++ /dev/null @@ -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, pos: usize) -> ParsingResult { - // TODO: This must be newline/indentation aware - equality::try_parse(tokens, pos) -} - -#[cfg(test)] -mod tests {} diff --git a/src/syntax/functions/arguments_list.rs b/src/syntax/functions/arguments_list.rs index b31a21e..28d051a 100644 --- a/src/syntax/functions/arguments_list.rs +++ b/src/syntax/functions/arguments_list.rs @@ -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, pos: usize) -> ParsingResult::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); diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index 4d0e33f..45f0520 100755 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -1,6 +1,5 @@ use crate::error_handling::MistiError; -mod expression; mod functions; mod parseable; mod parsers; diff --git a/src/syntax/parsers/block.rs b/src/syntax/parsers/block.rs index 92274f2..c51fa43 100644 --- a/src/syntax/parsers/block.rs +++ b/src/syntax/parsers/block.rs @@ -96,7 +96,7 @@ mod tests { fn should_parse_empty_block() { let tokens = get_tokens(&String::from("{}")).unwrap(); let (block, next_pos) = Block::try_parse(&tokens, 0).unwrap(); - + assert_eq!(2, next_pos); assert_eq!(0, block.members.len()) } @@ -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), } } diff --git a/src/syntax/parsers/expression.rs b/src/syntax/parsers/expression.rs deleted file mode 100644 index d61aafd..0000000 --- a/src/syntax/parsers/expression.rs +++ /dev/null @@ -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, current_pos: usize) -> ParsingResult<'a, Self::Item> { - expression::try_parse(tokens, current_pos) - } -} diff --git a/src/syntax/expression/comparison.rs b/src/syntax/parsers/expression/comparison.rs similarity index 100% rename from src/syntax/expression/comparison.rs rename to src/syntax/parsers/expression/comparison.rs diff --git a/src/syntax/expression/equality.rs b/src/syntax/parsers/expression/equality.rs similarity index 100% rename from src/syntax/expression/equality.rs rename to src/syntax/parsers/expression/equality.rs diff --git a/src/syntax/expression/factor.rs b/src/syntax/parsers/expression/factor.rs similarity index 100% rename from src/syntax/expression/factor.rs rename to src/syntax/parsers/expression/factor.rs diff --git a/src/syntax/expression/function_call_expr.rs b/src/syntax/parsers/expression/function_call_expr.rs similarity index 100% rename from src/syntax/expression/function_call_expr.rs rename to src/syntax/parsers/expression/function_call_expr.rs diff --git a/src/syntax/parsers/expression/mod.rs b/src/syntax/parsers/expression/mod.rs new file mode 100644 index 0000000..dd529d5 --- /dev/null +++ b/src/syntax/parsers/expression/mod.rs @@ -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, current_pos: usize) -> ParsingResult<'a, Self::Item> { + // TODO: This must be newline/indentation aware + equality::try_parse(tokens, current_pos) + } +} + +#[cfg(test)] +mod tests {} diff --git a/src/syntax/expression/primary.rs b/src/syntax/parsers/expression/primary.rs similarity index 95% rename from src/syntax/expression/primary.rs rename to src/syntax/parsers/expression/primary.rs index 5174784..f7bef7a 100644 --- a/src/syntax/expression/primary.rs +++ b/src/syntax/parsers/expression/primary.rs @@ -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, pos: usize) -> ParsingResult { } fn parse_parenthesized_expression(tokens: &Vec, pos: usize) -> ParsingResult { - 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 { diff --git a/src/syntax/expression/term.rs b/src/syntax/parsers/expression/term.rs similarity index 100% rename from src/syntax/expression/term.rs rename to src/syntax/parsers/expression/term.rs diff --git a/src/syntax/expression/unary.rs b/src/syntax/parsers/expression/unary.rs similarity index 94% rename from src/syntax/expression/unary.rs rename to src/syntax/parsers/expression/unary.rs index b55d61c..90bf2b7 100644 --- a/src/syntax/expression/unary.rs +++ b/src/syntax/parsers/expression/unary.rs @@ -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, pos: usize) -> ParsingResult { 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,