diff --git a/src/abstract_tree/function_call.rs b/src/abstract_tree/function_call.rs index d97a58f..9071401 100644 --- a/src/abstract_tree/function_call.rs +++ b/src/abstract_tree/function_call.rs @@ -1,19 +1,27 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Error, Expression, FunctionExpression, Map, Result, Type, Value}; +use crate::{ + AbstractTree, Error, Expression, FunctionExpression, Map, Result, SyntaxPosition, Type, Value, +}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct FunctionCall { function_expression: FunctionExpression, arguments: Vec, + syntax_position: SyntaxPosition, } impl FunctionCall { - pub fn new(function_expression: FunctionExpression, arguments: Vec) -> Self { + pub fn new( + function_expression: FunctionExpression, + arguments: Vec, + syntax_position: SyntaxPosition, + ) -> Self { Self { function_expression, arguments, + syntax_position, } } } @@ -41,6 +49,7 @@ impl AbstractTree for FunctionCall { Ok(FunctionCall { function_expression, arguments, + syntax_position: node.range().into(), }) } @@ -67,10 +76,10 @@ impl AbstractTree for FunctionCall { if self.arguments.len() != parameter_types.len() { return Err(Error::ExpectedFunctionArgumentAmount { - source: "TODO".to_string(), expected: parameter_types.len(), actual: self.arguments.len(), - }); + } + .at_source_position(_source, self.syntax_position)); } Ok(()) diff --git a/src/abstract_tree/yield.rs b/src/abstract_tree/yield.rs index 02e1036..3ae6a92 100644 --- a/src/abstract_tree/yield.rs +++ b/src/abstract_tree/yield.rs @@ -39,7 +39,7 @@ impl AbstractTree for Yield { } } - let call = FunctionCall::new(function_expression, arguments); + let call = FunctionCall::new(function_expression, arguments, node.range().into()); Ok(Yield { call }) } diff --git a/src/error.rs b/src/error.rs index 84707be..de399b2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -77,7 +77,6 @@ pub enum Error { /// A function was called with the wrong amount of arguments. ExpectedFunctionArgumentAmount { - source: String, expected: usize, actual: usize, }, @@ -334,14 +333,9 @@ impl fmt::Display for Error { f, "{tool_name} expected {expected} arguments, but got {actual}.", ), - ExpectedFunctionArgumentAmount { - source, - expected, - actual, - } => write!( - f, - "{source} expected {expected} arguments, but got {actual}.", - ), + ExpectedFunctionArgumentAmount { expected, actual } => { + write!(f, "Expected {expected} arguments, but got {actual}.",) + } ExpectedFunctionArgumentMinimum { source, minumum_expected, diff --git a/tests/interpret.rs b/tests/interpret.rs index 4f863d7..0784f03 100644 --- a/tests/interpret.rs +++ b/tests/interpret.rs @@ -443,22 +443,17 @@ mod type_definition { #[test] fn argument_count_check() { - let result = interpret( - " + let source = " foo = (x ) { x } foo() - ", - ); + "; + let result = interpret(&source); assert_eq!( - Err(Error::ExpectedFunctionArgumentAmount { - source: "foo".to_string(), - expected: 1, - actual: 0 - }), - result + "Expected 1 arguments, but got 0. Occured at (4, 12) to (4, 17). Source: foo()", + result.unwrap_err().to_string() ) }