Skip to content

Commit

Permalink
fix: wrap || comparison in parenthesis when && is used
Browse files Browse the repository at this point in the history
  • Loading branch information
amaanq committed Feb 23, 2024
1 parent 9301c1e commit 9fb7ab8
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion cli/src/generate/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,22 +871,40 @@ impl Generator {
line_break.push_str(" ");
}

// parenthesis needed if we add the `!eof` condition to explicitly avoid confusion with
// precedence of `&&` and `||`
let (mut need_open_paren, mut need_close_paren) = (false, false);
for (i, range) in ranges.iter().enumerate() {
if is_included {
if i > 0 {
add!(self, " ||{line_break}");
}
if range.start == '\0' {
add!(self, "!eof && ");
need_open_paren = true;
need_close_paren = true;
}
if range.end == range.start {
if need_open_paren {
add!(self, "(");
need_open_paren = false;
}
add!(self, "lookahead == ");
self.add_character(range.start);
if need_close_paren && i == ranges.len() - 1 {
add!(self, ")");
need_close_paren = false;
}
} else if range.end as u32 == range.start as u32 + 1 {
add!(self, "lookahead == ");
add!(self, "(lookahead == ");
self.add_character(range.start);
add!(self, " ||{line_break}lookahead == ");
self.add_character(range.end);
if need_close_paren && i == ranges.len() - 1 {
add!(self, ")");
need_close_paren = false;
}
add!(self, ")");
} else {
add!(self, "(");
self.add_character(range.start);
Expand Down

0 comments on commit 9fb7ab8

Please sign in to comment.