Skip to content

Commit 65e28a3

Browse files
committed
check for error nodes within rust parser for function
1 parent bf75873 commit 65e28a3

File tree

5 files changed

+52
-37
lines changed

5 files changed

+52
-37
lines changed

dsc_lib/src/functions/int.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ mod tests {
7070
#[test]
7171
fn float() {
7272
let mut parser = Statement::new().unwrap();
73-
let err = parser.parse_and_execute("[int(1.2)]", &Context::new()).unwrap_err();
74-
assert!(matches!(err, DscError::IntegerConversion(_)));
73+
let err = parser.parse_and_execute("[int(1.0)]", &Context::new()).unwrap_err();
74+
assert!(matches!(err, DscError::Parser(_)));
7575
}
7676

7777
#[test]
7878
fn incomplete_float() {
7979
let mut parser = Statement::new().unwrap();
8080
let err = parser.parse_and_execute("[int(.2)]", &Context::new()).unwrap_err();
81-
assert!(matches!(err, DscError::IntegerConversion(_)));
81+
assert!(matches!(err, DscError::Parser(_)));
8282
}
8383

8484
#[test]

dsc_lib/src/parser/functions.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,23 @@ impl Function {
3636
///
3737
/// This function will return an error if the function node is not valid.
3838
pub fn new(statement_bytes: &[u8], function: &Node) -> Result<Self, DscError> {
39-
let Some(function_name) = function.child_by_field_name("name") else {
39+
let mut function_name = None;
40+
let mut function_args = None;
41+
let mut cursor = function.walk();
42+
for member in function.named_children(&mut cursor) {
43+
match member.kind() {
44+
"arguments" => function_args = Some(member),
45+
"functionName" => function_name = Some(member),
46+
"ERROR" => return Err(DscError::Parser("Found error node parsing function".to_string())),
47+
_ => {}
48+
}
49+
}
50+
let Some(name) = function_name else {
4051
return Err(DscError::Parser("Function name node not found".to_string()));
4152
};
42-
let function_args = function.child_by_field_name("args");
4353
let args = convert_args_node(statement_bytes, &function_args)?;
4454
Ok(Function{
45-
name: function_name.utf8_text(statement_bytes)?.to_string(),
55+
name: name.utf8_text(statement_bytes)?.to_string(),
4656
args})
4757
}
4858

tree-sitter-dscexpression/corpus/invalid_expressions.txt

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ Invalid float argument
122122
(function
123123
(functionName)
124124
(ERROR
125-
(functionName)
126-
(memberName)))))
125+
(functionName))
126+
(arguments
127+
(number)))))
127128

128129
=====
129130
Plus-sign number argument
@@ -136,5 +137,35 @@ Plus-sign number argument
136137
(function
137138
(functionName)
138139
(ERROR
139-
(UNEXPECTED '+')
140-
(memberName)))))
140+
(UNEXPECTED '+'))
141+
(arguments
142+
(number)))))
143+
144+
=====
145+
Float input
146+
=====
147+
[myFunction(1234.5678)]
148+
---
149+
150+
(statement
151+
(expression
152+
(function
153+
(functionName)
154+
(ERROR
155+
(number))
156+
(arguments
157+
(number)))))
158+
159+
=====
160+
Float input starting with decimal
161+
=====
162+
[myFunction(.1)]
163+
---
164+
165+
(statement
166+
(expression
167+
(function
168+
(functionName)
169+
(ERROR)
170+
(arguments
171+
(number)))))

tree-sitter-dscexpression/corpus/valid_expressions.txt

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,6 @@ Nested dot-notation
102102
(memberAccess
103103
(memberName))))
104104

105-
=====
106-
Float input
107-
=====
108-
[myFunction(1234.5678)]
109-
---
110-
111-
(statement
112-
(expression
113-
(function
114-
(functionName)
115-
(arguments
116-
(number)))))
117-
118105
=====
119106
Quotes float input
120107
=====
@@ -127,16 +114,3 @@ Quotes float input
127114
(functionName)
128115
(arguments
129116
(string)))))
130-
131-
=====
132-
Float input starting with decimal
133-
=====
134-
[myFunction(.1)]
135-
---
136-
137-
(statement
138-
(expression
139-
(function
140-
(functionName)
141-
(arguments
142-
(number)))))

tree-sitter-dscexpression/grammar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = grammar({
2929
_quotedString: $ => seq('\'', $.string, '\''),
3030
// ARM strings do not allow to contain single-quote characters
3131
string: $ => /[^']*/,
32-
number: $ => /-?(\d*[.])?\d+/,
32+
number: $ => /-?\d+/,
3333
boolean: $ => choice('true', 'false'),
3434

3535
memberAccess: $ => seq('.', $.memberName, repeat(seq('.', $.memberName))),

0 commit comments

Comments
 (0)