Skip to content

Commit

Permalink
Refactor comment escaping.
Browse files Browse the repository at this point in the history
  • Loading branch information
01mf02 committed Nov 6, 2024
1 parent 896b5e5 commit 0ef6ea3
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions jaq-core/src/load/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,21 @@ impl<'a> Lexer<&'a str> {

/// Whitespace and comments.
fn space(&mut self) {
// did the previous line contain a comment ending with an odd number of backslashes?
let mut backslash = false;
self.i = self.i.trim_start();
while let Some(comment) = core::mem::take(&mut backslash)
.then_some(self.i)
.or_else(|| self.i.strip_prefix('#'))
{
let (before, after) = comment.split_once('\n').unwrap_or((comment, ""));
let before = before.strip_suffix('\r').unwrap_or(before);
self.i = after;
if before.chars().rev().take_while(|c| *c == '\\').count() % 2 == 1 {
backslash = true;
} else {
self.i = self.i.trim_start()
loop {
self.i = self.i.trim_start();
match self.i.strip_prefix('#') {
Some(comment) => self.i = comment,
None => break,
}
// ignore all lines that end with an odd number of backslashes
loop {
let (before, after) = self.i.split_once('\n').unwrap_or((self.i, ""));
let before = before.strip_suffix('\r').unwrap_or(before);
self.i = after;
// does the line end with an even number of backslashes?
if before.chars().rev().take_while(|c| *c == '\\').count() % 2 == 0 {
break;
}
}
}
}
Expand Down

0 comments on commit 0ef6ea3

Please sign in to comment.