From 199e1c9184e80a576388d62f8a40db2d632ae9cb Mon Sep 17 00:00:00 2001 From: Jeff Date: Sun, 17 Mar 2024 10:19:22 -0400 Subject: [PATCH] Fix tests and parser --- src/abstract_tree/block.rs | 4 ++++ src/abstract_tree/if_else.rs | 37 ++++++++++++++++-------------------- src/parser.rs | 29 ++++++++++------------------ tests/functions.rs | 2 +- 4 files changed, 31 insertions(+), 41 deletions(-) diff --git a/src/abstract_tree/block.rs b/src/abstract_tree/block.rs index 343aa5a..9bf062b 100644 --- a/src/abstract_tree/block.rs +++ b/src/abstract_tree/block.rs @@ -14,6 +14,10 @@ impl Block { pub fn new(statements: Vec>) -> Self { Self { statements } } + + pub fn last_statement(&self) -> &WithPosition { + self.statements.last().unwrap() + } } impl AbstractTree for Block { diff --git a/src/abstract_tree/if_else.rs b/src/abstract_tree/if_else.rs index eec8065..d6091dd 100644 --- a/src/abstract_tree/if_else.rs +++ b/src/abstract_tree/if_else.rs @@ -8,15 +8,15 @@ use super::{AbstractTree, Action, Block, Expression, Type, WithPosition}; #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] pub struct IfElse { if_expression: WithPosition, - if_block: WithPosition, - else_block: Option>, + if_block: Block, + else_block: Option, } impl IfElse { pub fn new( if_expression: WithPosition, - if_block: WithPosition, - else_block: Option>, + if_block: Block, + else_block: Option, ) -> Self { Self { if_expression, @@ -28,20 +28,20 @@ impl IfElse { impl AbstractTree for IfElse { fn expected_type(&self, _context: &Context) -> Result { - self.if_block.node.expected_type(_context) + self.if_block.expected_type(_context) } fn validate(&self, context: &Context) -> Result<(), ValidationError> { if let Type::Boolean = self.if_expression.node.expected_type(context)? { if let Some(else_block) = &self.else_block { - let expected = self.if_block.node.expected_type(context)?; - let actual = else_block.node.expected_type(context)?; + let expected = self.if_block.expected_type(context)?; + let actual = else_block.expected_type(context)?; expected .check(&actual) .map_err(|conflict| ValidationError::TypeCheck { conflict, - actual_position: self.if_block.position, + actual_position: self.if_block.last_statement().position, expected_position: self.if_expression.position, })?; } @@ -61,9 +61,9 @@ impl AbstractTree for IfElse { .as_boolean()?; if if_boolean { - self.if_block.node.run(_context) + self.if_block.run(_context) } else if let Some(else_statement) = self.else_block { - else_statement.node.run(_context) + else_statement.run(_context) } else { Ok(Action::None) } @@ -87,8 +87,7 @@ mod tests { Block::new(vec![Statement::Expression(Expression::Value( ValueNode::String("foo".to_string()) )) - .with_position((0, 0))]) - .with_position((0, 0)), + .with_position((0, 0))]), None ) .run(&Context::new()), @@ -104,15 +103,11 @@ mod tests { Block::new(vec![Statement::Expression(Expression::Value( ValueNode::String("foo".to_string()) )) - .with_position((0, 0))]) - .with_position((0, 0)), - Some( - Block::new(vec![Statement::Expression(Expression::Value( - ValueNode::String("bar".to_string()) - )) - .with_position((0, 0))]) - .with_position((0, 0)) - ) + .with_position((0, 0))]), + Some(Block::new(vec![Statement::Expression(Expression::Value( + ValueNode::String("bar".to_string()) + )) + .with_position((0, 0))])) ) .run(&Context::new()), Ok(Action::Return(Value::string("bar".to_string()))) diff --git a/src/parser.rs b/src/parser.rs index 445c417..6bf3890 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -121,10 +121,6 @@ pub fn parser<'src>() -> DustParser<'src> { ) .map(|statements| Block::new(statements)); - let positioned_block = block - .clone() - .map_with(|block, state| block.with_position(state.span())); - let positioned_expression = recursive(|positioned_expression| { let identifier_expression = identifier.clone().map_with(|identifier, state| { Expression::Identifier(identifier).with_position(state.span()) @@ -336,10 +332,10 @@ pub fn parser<'src>() -> DustParser<'src> { )); choice(( - function, - function_call, range, logic_math_and_index, + function, + function_call, identifier_expression, list, map, @@ -397,10 +393,10 @@ pub fn parser<'src>() -> DustParser<'src> { let if_else = just(Token::Keyword("if")) .ignore_then(positioned_expression.clone()) - .then(positioned_block.clone()) + .then(block.clone()) .then( just(Token::Keyword("else")) - .ignore_then(positioned_block.clone()) + .ignore_then(block.clone()) .or_not(), ) .map_with(|((if_expression, if_block), else_block), state| { @@ -574,8 +570,7 @@ mod tests { Block::new(vec![Statement::Expression(Expression::Value( ValueNode::String("foo".to_string()) ),) - .with_position((10, 15))]) - .with_position((8, 17)), + .with_position((10, 15))]), None ),) ); @@ -590,15 +585,11 @@ mod tests { Block::new(vec![Statement::Expression(Expression::Value( ValueNode::String("foo".to_string()) ),) - .with_position((9, 14))]) - .with_position((8, 17)), - Some( - Block::new(vec![Statement::Expression(Expression::Value( - ValueNode::String("bar".to_string()) - ),) - .with_position((24, 29))]) - .with_position((22, 31)) - ) + .with_position((9, 14))]), + Some(Block::new(vec![Statement::Expression(Expression::Value( + ValueNode::String("bar".to_string()) + ),) + .with_position((24, 29))])) ),) ) } diff --git a/tests/functions.rs b/tests/functions.rs index c51d515..ef54603 100644 --- a/tests/functions.rs +++ b/tests/functions.rs @@ -62,7 +62,7 @@ fn function_context_does_not_capture_values() { ), Err(vec![Error::Validation { error: ValidationError::VariableNotFound(Identifier::new("x")), - position: (0, 0).into() + position: (32, 66).into() }]) );