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

fix: wrap || comparison in parenthesis when && is used #3070

Merged
merged 1 commit into from
Feb 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions cli/src/generate/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,17 +871,29 @@ 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, need_close_paren) = (true, 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 == ");
self.add_character(range.start);
Expand Down