From eb1c9946f3a42d0e6c150cff3cfcc8c0875703d3 Mon Sep 17 00:00:00 2001 From: Tess Gauthier Date: Tue, 21 May 2024 15:55:19 -0400 Subject: [PATCH 1/5] update expressions to error for non-ints --- dsc_lib/src/functions/int.rs | 7 +++++ .../corpus/invalid_expressions.txt | 14 ++++++++++ .../corpus/valid_expressions.txt | 26 +++++++++++++++++++ tree-sitter-dscexpression/grammar.js | 2 +- 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/dsc_lib/src/functions/int.rs b/dsc_lib/src/functions/int.rs index 03281c35..50657ff1 100644 --- a/dsc_lib/src/functions/int.rs +++ b/dsc_lib/src/functions/int.rs @@ -67,6 +67,13 @@ mod tests { assert_eq!(result, 123); } + #[test] + fn float() { + let mut parser = Statement::new().unwrap(); + let err = parser.parse_and_execute("[int(1.2)]", &Context::new()).unwrap_err(); + assert!(matches!(err, DscError::IntegerConversion(_))); + } + #[test] fn nested() { let mut parser = Statement::new().unwrap(); diff --git a/tree-sitter-dscexpression/corpus/invalid_expressions.txt b/tree-sitter-dscexpression/corpus/invalid_expressions.txt index dbc1fbb6..a595c781 100644 --- a/tree-sitter-dscexpression/corpus/invalid_expressions.txt +++ b/tree-sitter-dscexpression/corpus/invalid_expressions.txt @@ -110,3 +110,17 @@ Incomplete expression (function (functionName))) (MISSING "]")) + +===== +Invalid float argument +===== +[myFunction(a.1)] +--- + + (statement + (expression + (function + (functionName) + (ERROR + (functionName) + (memberName))))) diff --git a/tree-sitter-dscexpression/corpus/valid_expressions.txt b/tree-sitter-dscexpression/corpus/valid_expressions.txt index f575990b..258fdd6c 100644 --- a/tree-sitter-dscexpression/corpus/valid_expressions.txt +++ b/tree-sitter-dscexpression/corpus/valid_expressions.txt @@ -101,3 +101,29 @@ Nested dot-notation (memberName))))) (memberAccess (memberName)))) + +===== +Float input +===== +[myFunction(1234.5678)] +--- + + (statement + (expression + (function + (functionName) + (arguments + (number))))) + +===== +Quotes float input +===== +[myFunction('1234.5678')] +--- + + (statement + (expression + (function + (functionName) + (arguments + (string))))) diff --git a/tree-sitter-dscexpression/grammar.js b/tree-sitter-dscexpression/grammar.js index 754917b2..9a8c257f 100644 --- a/tree-sitter-dscexpression/grammar.js +++ b/tree-sitter-dscexpression/grammar.js @@ -29,7 +29,7 @@ module.exports = grammar({ _quotedString: $ => seq('\'', $.string, '\''), // ARM strings do not allow to contain single-quote characters string: $ => /[^']*/, - number: $ => /-?\d+/, + number: $ => /[+-]?([0-9]*[.])?[0-9]+/, boolean: $ => choice('true', 'false'), memberAccess: $ => seq('.', $.memberName, repeat(seq('.', $.memberName))), From 449ab935a693facdde9f27d80a2dc0e77a4eb428 Mon Sep 17 00:00:00 2001 From: Tess Gauthier Date: Thu, 23 May 2024 11:17:42 -0400 Subject: [PATCH 2/5] update num regex --- dsc_lib/src/functions/int.rs | 9 ++++++++- tree-sitter-dscexpression/grammar.js | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/dsc_lib/src/functions/int.rs b/dsc_lib/src/functions/int.rs index 50657ff1..72f8fc85 100644 --- a/dsc_lib/src/functions/int.rs +++ b/dsc_lib/src/functions/int.rs @@ -74,6 +74,13 @@ mod tests { assert!(matches!(err, DscError::IntegerConversion(_))); } + #[test] + fn incomplete_float() { + let mut parser = Statement::new().unwrap(); + let err = parser.parse_and_execute("[int(.2)]", &Context::new()).unwrap_err(); + assert!(matches!(err, DscError::IntegerConversion(_))); + } + #[test] fn nested() { let mut parser = Statement::new().unwrap(); @@ -84,7 +91,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/tree-sitter-dscexpression/grammar.js b/tree-sitter-dscexpression/grammar.js index 9a8c257f..b5095bb4 100644 --- a/tree-sitter-dscexpression/grammar.js +++ b/tree-sitter-dscexpression/grammar.js @@ -29,7 +29,7 @@ module.exports = grammar({ _quotedString: $ => seq('\'', $.string, '\''), // ARM strings do not allow to contain single-quote characters string: $ => /[^']*/, - number: $ => /[+-]?([0-9]*[.])?[0-9]+/, + number: $ => /-?(\d*[.])?\d+/, boolean: $ => choice('true', 'false'), memberAccess: $ => seq('.', $.memberName, repeat(seq('.', $.memberName))), From d4ca398ff8b48836d6c9b0ee5a53ef72bfb16ceb Mon Sep 17 00:00:00 2001 From: Tess Gauthier Date: Fri, 24 May 2024 09:34:39 -0400 Subject: [PATCH 3/5] add tests --- .../corpus/invalid_expressions.txt | 14 ++++++++++++++ .../corpus/valid_expressions.txt | 13 +++++++++++++ 2 files changed, 27 insertions(+) diff --git a/tree-sitter-dscexpression/corpus/invalid_expressions.txt b/tree-sitter-dscexpression/corpus/invalid_expressions.txt index a595c781..68e5cdc8 100644 --- a/tree-sitter-dscexpression/corpus/invalid_expressions.txt +++ b/tree-sitter-dscexpression/corpus/invalid_expressions.txt @@ -124,3 +124,17 @@ Invalid float argument (ERROR (functionName) (memberName))))) + +===== +Plus-sign number argument +===== +[myFunction(+1)] +--- + + (statement + (expression + (function + (functionName) + (ERROR + (UNEXPECTED '+') + (memberName))))) diff --git a/tree-sitter-dscexpression/corpus/valid_expressions.txt b/tree-sitter-dscexpression/corpus/valid_expressions.txt index 258fdd6c..5af79740 100644 --- a/tree-sitter-dscexpression/corpus/valid_expressions.txt +++ b/tree-sitter-dscexpression/corpus/valid_expressions.txt @@ -127,3 +127,16 @@ Quotes float input (functionName) (arguments (string))))) + +===== +Float input starting with decimal +===== +[myFunction(.1)] +--- + + (statement + (expression + (function + (functionName) + (arguments + (number))))) From 65e28a3e95c0b1e24a55552cbd2f896d46b7ac6b Mon Sep 17 00:00:00 2001 From: Tess Gauthier Date: Fri, 24 May 2024 17:16:50 -0400 Subject: [PATCH 4/5] check for error nodes within rust parser for function --- dsc_lib/src/functions/int.rs | 6 +-- dsc_lib/src/parser/functions.rs | 16 ++++++-- .../corpus/invalid_expressions.txt | 39 +++++++++++++++++-- .../corpus/valid_expressions.txt | 26 ------------- tree-sitter-dscexpression/grammar.js | 2 +- 5 files changed, 52 insertions(+), 37 deletions(-) diff --git a/dsc_lib/src/functions/int.rs b/dsc_lib/src/functions/int.rs index 72f8fc85..8ef6a13c 100644 --- a/dsc_lib/src/functions/int.rs +++ b/dsc_lib/src/functions/int.rs @@ -70,15 +70,15 @@ mod tests { #[test] fn float() { let mut parser = Statement::new().unwrap(); - let err = parser.parse_and_execute("[int(1.2)]", &Context::new()).unwrap_err(); - assert!(matches!(err, DscError::IntegerConversion(_))); + let err = parser.parse_and_execute("[int(1.0)]", &Context::new()).unwrap_err(); + assert!(matches!(err, DscError::Parser(_))); } #[test] fn incomplete_float() { let mut parser = Statement::new().unwrap(); let err = parser.parse_and_execute("[int(.2)]", &Context::new()).unwrap_err(); - assert!(matches!(err, DscError::IntegerConversion(_))); + assert!(matches!(err, DscError::Parser(_))); } #[test] diff --git a/dsc_lib/src/parser/functions.rs b/dsc_lib/src/parser/functions.rs index c4b11ada..0c7409eb 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 68e5cdc8..2914a066 100644 --- a/tree-sitter-dscexpression/corpus/invalid_expressions.txt +++ b/tree-sitter-dscexpression/corpus/invalid_expressions.txt @@ -122,8 +122,9 @@ Invalid float argument (function (functionName) (ERROR - (functionName) - (memberName))))) + (functionName)) + (arguments + (number))))) ===== Plus-sign number argument @@ -136,5 +137,35 @@ Plus-sign number argument (function (functionName) (ERROR - (UNEXPECTED '+') - (memberName))))) + (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 5af79740..7d8968ca 100644 --- a/tree-sitter-dscexpression/corpus/valid_expressions.txt +++ b/tree-sitter-dscexpression/corpus/valid_expressions.txt @@ -102,19 +102,6 @@ Nested dot-notation (memberAccess (memberName)))) -===== -Float input -===== -[myFunction(1234.5678)] ---- - - (statement - (expression - (function - (functionName) - (arguments - (number))))) - ===== Quotes float input ===== @@ -127,16 +114,3 @@ Quotes float input (functionName) (arguments (string))))) - -===== -Float input starting with decimal -===== -[myFunction(.1)] ---- - - (statement - (expression - (function - (functionName) - (arguments - (number))))) diff --git a/tree-sitter-dscexpression/grammar.js b/tree-sitter-dscexpression/grammar.js index b5095bb4..754917b2 100644 --- a/tree-sitter-dscexpression/grammar.js +++ b/tree-sitter-dscexpression/grammar.js @@ -29,7 +29,7 @@ module.exports = grammar({ _quotedString: $ => seq('\'', $.string, '\''), // ARM strings do not allow to contain single-quote characters string: $ => /[^']*/, - number: $ => /-?(\d*[.])?\d+/, + number: $ => /-?\d+/, boolean: $ => choice('true', 'false'), memberAccess: $ => seq('.', $.memberName, repeat(seq('.', $.memberName))), From 7d4107575f94a810da28da74cfe2964b9eb03b2b Mon Sep 17 00:00:00 2001 From: Tess Gauthier Date: Fri, 31 May 2024 10:15:26 -0400 Subject: [PATCH 5/5] add test for incomplete float --- dsc_lib/src/functions/int.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/dsc_lib/src/functions/int.rs b/dsc_lib/src/functions/int.rs index 8ef6a13c..f1e497b4 100644 --- a/dsc_lib/src/functions/int.rs +++ b/dsc_lib/src/functions/int.rs @@ -75,12 +75,19 @@ mod tests { } #[test] - fn incomplete_float() { + 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();