Skip to content

Commit

Permalink
Fix repeated && and express precedence in grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Oct 31, 2024
1 parent 480a7dd commit c41637f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
10 changes: 7 additions & 3 deletions GRAMMAR.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,17 @@ import : 'import' '?'? string? eol
module : 'mod' '?'? NAME string? eol
expression : 'if' condition '{' expression '}' 'else' '{' expression '}'
expression : disjunct || expression
| disjunct
disjunct : conjunct && disjunct
| conjunct
conjunct : 'if' condition '{' expression '}' 'else' '{' expression '}'
| 'assert' '(' condition ',' expression ')'
| '/' expression
| value '/' expression
| value '+' expression
| value '&&' expression
| value '||' expression
| value
condition : expression '==' expression
Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ impl<'run, 'src> Parser<'run, 'src> {

let disjunct = if self.accepted(AmpersandAmpersand)? {
let lhs = conjunct.into();
let rhs = self.parse_conjunct()?.into();
let rhs = self.parse_disjunct()?.into();
Expression::And { lhs, rhs }
} else {
conjunct
Expand Down
6 changes: 6 additions & 0 deletions tests/and_or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ fn and_has_higher_precedence_than_or() {
evaluate("('' && 'foo') || 'bar'", "bar");
evaluate("'' && 'foo' || 'bar'", "bar");
}

#[test]
fn misc() {
evaluate("'' || '' || '' || '' || 'foo'", "foo");
evaluate("'foo' && 'foo' && 'foo' && 'foo' && 'bar'", "bar");
}

0 comments on commit c41637f

Please sign in to comment.