diff --git a/dsc_lib/src/functions/int.rs b/dsc_lib/src/functions/int.rs index 03281c35f..f1e497b45 100644 --- a/dsc_lib/src/functions/int.rs +++ b/dsc_lib/src/functions/int.rs @@ -67,6 +67,27 @@ mod tests { assert_eq!(result, 123); } + #[test] + fn float() { + let mut parser = Statement::new().unwrap(); + let err = parser.parse_and_execute("[int(1.0)]", &Context::new()).unwrap_err(); + assert!(matches!(err, DscError::Parser(_))); + } + + #[test] + fn incomplete_float_missing_digit() { + let mut parser = Statement::new().unwrap(); + let err = parser.parse_and_execute("[int(.2)]", &Context::new()).unwrap_err(); + assert!(matches!(err, DscError::Parser(_))); + } + + #[test] + fn incomplete_float_missing_decimal() { + let mut parser = Statement::new().unwrap(); + let err = parser.parse_and_execute("[int(2.)]", &Context::new()).unwrap_err(); + assert!(matches!(err, DscError::Parser(_))); + } + #[test] fn nested() { let mut parser = Statement::new().unwrap(); @@ -77,7 +98,7 @@ mod tests { #[test] fn error() { let mut parser = Statement::new().unwrap(); - let err = parser.parse_and_execute("[int('foo')]", &Context::new()).unwrap_err(); + let err = parser.parse_and_execute("[int('foo.1')]", &Context::new()).unwrap_err(); assert!(matches!(err, DscError::FunctionArg(_, _))); } } diff --git a/dsc_lib/src/parser/functions.rs b/dsc_lib/src/parser/functions.rs index c4b11ada5..0c7409ebf 100644 --- a/dsc_lib/src/parser/functions.rs +++ b/dsc_lib/src/parser/functions.rs @@ -36,13 +36,23 @@ impl Function { /// /// This function will return an error if the function node is not valid. pub fn new(statement_bytes: &[u8], function: &Node) -> Result { - let Some(function_name) = function.child_by_field_name("name") else { + let mut function_name = None; + let mut function_args = None; + let mut cursor = function.walk(); + for member in function.named_children(&mut cursor) { + match member.kind() { + "arguments" => function_args = Some(member), + "functionName" => function_name = Some(member), + "ERROR" => return Err(DscError::Parser("Found error node parsing function".to_string())), + _ => {} + } + } + let Some(name) = function_name else { return Err(DscError::Parser("Function name node not found".to_string())); }; - let function_args = function.child_by_field_name("args"); let args = convert_args_node(statement_bytes, &function_args)?; Ok(Function{ - name: function_name.utf8_text(statement_bytes)?.to_string(), + name: name.utf8_text(statement_bytes)?.to_string(), args}) } diff --git a/tree-sitter-dscexpression/corpus/invalid_expressions.txt b/tree-sitter-dscexpression/corpus/invalid_expressions.txt index dbc1fbb6f..2914a0662 100644 --- a/tree-sitter-dscexpression/corpus/invalid_expressions.txt +++ b/tree-sitter-dscexpression/corpus/invalid_expressions.txt @@ -110,3 +110,62 @@ Incomplete expression (function (functionName))) (MISSING "]")) + +===== +Invalid float argument +===== +[myFunction(a.1)] +--- + + (statement + (expression + (function + (functionName) + (ERROR + (functionName)) + (arguments + (number))))) + +===== +Plus-sign number argument +===== +[myFunction(+1)] +--- + + (statement + (expression + (function + (functionName) + (ERROR + (UNEXPECTED '+')) + (arguments + (number))))) + +===== +Float input +===== +[myFunction(1234.5678)] +--- + + (statement + (expression + (function + (functionName) + (ERROR + (number)) + (arguments + (number))))) + +===== +Float input starting with decimal +===== +[myFunction(.1)] +--- + + (statement + (expression + (function + (functionName) + (ERROR) + (arguments + (number))))) diff --git a/tree-sitter-dscexpression/corpus/valid_expressions.txt b/tree-sitter-dscexpression/corpus/valid_expressions.txt index f575990ba..7d8968ca2 100644 --- a/tree-sitter-dscexpression/corpus/valid_expressions.txt +++ b/tree-sitter-dscexpression/corpus/valid_expressions.txt @@ -101,3 +101,16 @@ Nested dot-notation (memberName))))) (memberAccess (memberName)))) + +===== +Quotes float input +===== +[myFunction('1234.5678')] +--- + + (statement + (expression + (function + (functionName) + (arguments + (string)))))