Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update expressions to error for non-ints #438

Merged
merged 8 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion dsc_lib/src/functions/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
tgauth marked this conversation as resolved.
Show resolved Hide resolved
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();
Expand All @@ -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(_, _)));
}
}
16 changes: 13 additions & 3 deletions dsc_lib/src/parser/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, DscError> {
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})
}

Expand Down
59 changes: 59 additions & 0 deletions tree-sitter-dscexpression/corpus/invalid_expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,62 @@ Incomplete expression
(function
(functionName)))
(MISSING "]"))

=====
Invalid float argument
=====
[myFunction(a.1)]
SteveL-MSFT marked this conversation as resolved.
Show resolved Hide resolved
---

(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)))))
13 changes: 13 additions & 0 deletions tree-sitter-dscexpression/corpus/valid_expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,16 @@ Nested dot-notation
(memberName)))))
(memberAccess
(memberName))))

=====
Quotes float input
=====
[myFunction('1234.5678')]
---

(statement
(expression
(function
(functionName)
(arguments
(string)))))
Loading