Skip to content

Commit

Permalink
fix: panic while parsing some invalid function calls.
Browse files Browse the repository at this point in the history
The parser was panicking with function calls like `uint16(,0)`
  • Loading branch information
plusvic committed Sep 25, 2024
1 parent e7f6363 commit a2b7394
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/src/compiler/tests/testdata/errors/137.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rule test {
condition:
uint16(,0) == 0
}
6 changes: 6 additions & 0 deletions lib/src/compiler/tests/testdata/errors/137.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error[E001]: syntax error
--> line:3:13
|
3 | uint16(,0) == 0
| ^ expecting expression, `for`, `all`, `none`, `any`, `with` or `)`, found `,`
|
4 changes: 4 additions & 0 deletions parser/src/ast/cst2ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ impl<'src> Builder<'src> {
match self.next()? {
Event::Token { kind, span } => {
if expected_kind != kind {
// If this ever happens is because we are finding an
// unexpected syntax in the CST. Either the source -> CST
// phase is not strict enough, or the CST -> AST phase is
// overly strict.
panic!("expected {:?}, got {:?}", expected_kind, kind);
}
Ok(span)
Expand Down
7 changes: 4 additions & 3 deletions parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,11 +1432,12 @@ impl<'src> ParserImpl<'src> {
.then(|p| p.primary_expr())
.cond(t!(L_BRACKET), |p| p.expr().expect(t!(R_BRACKET)))
.cond(t!(L_PAREN), |p| {
p.opt(|p| p.boolean_expr())
.zero_or_more(|p| {
p.opt(|p| {
p.boolean_expr().zero_or_more(|p| {
p.expect(t!(COMMA)).then(|p| p.boolean_expr())
})
.expect(t!(R_PAREN))
})
.expect(t!(R_PAREN))
})
.end()
}
Expand Down

0 comments on commit a2b7394

Please sign in to comment.