Skip to content

Commit

Permalink
fix block comment lexing
Browse files Browse the repository at this point in the history
  • Loading branch information
rcgoodfellow committed Nov 9, 2022
1 parent 277e9e9 commit bbefd1a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
61 changes: 42 additions & 19 deletions p4/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,8 +784,10 @@ impl<'a> Lexer<'a> {
Some(token)
}

pub fn check_end_of_line(&mut self) {
pub fn check_end_of_line(&mut self) -> bool {
let mut end = false;
while self.cursor.is_empty() {
end = true;
self.line += 1;
self.col = 0;
if self.line < self.lines.len() {
Expand All @@ -794,6 +796,7 @@ impl<'a> Lexer<'a> {
break;
}
}
end
}

fn skip_whitespace(&mut self) -> bool {
Expand All @@ -806,7 +809,7 @@ impl<'a> Lexer<'a> {
skipped = true;
self.cursor = &self.cursor[1..];
self.col += 1;
self.check_end_of_line()
self.check_end_of_line();
}
skipped
}
Expand Down Expand Up @@ -839,30 +842,50 @@ impl<'a> Lexer<'a> {

fn skip_block_comment(&mut self) {
let mut chars = self.cursor.chars();
let mut len = 0;
match chars.next() {
Some('/') => {}
_ => return,
}
match chars.next() {
Some('*') => {}
_ => return,
}
self.cursor = &self.cursor[2..];
loop {
match chars.next() {
Some('*') => {
len += 1;
match chars.next() {
Some('/') => {
len += 1;
self.col += len;
self.cursor = &self.cursor[len..];
self.check_end_of_line();
self.skip_whitespace();
return;
loop {
match chars.next() {
Some('*') => {
self.cursor = &self.cursor[1..];
match chars.next() {
Some('/') => {
self.col += 1;
self.cursor = &self.cursor[1..];
self.check_end_of_line();
self.skip_whitespace();
return;
}
_ => {
if self.check_end_of_line() {
break;
}
self.cursor = &self.cursor[1..];
continue;
}
}
_ => {
}
None => {
self.skip_whitespace();
if self.check_end_of_line() {
break;
}
}
}
_ => {
len += 1;
continue;
_ => {
self.cursor = &self.cursor[1..];
continue;
}
}
}
chars = self.cursor.chars();
}
}

Expand Down
3 changes: 3 additions & 0 deletions test/src/p4/core.p4
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/*
* This is core.p4
*/
extern packet_in {
void extract<T>(out T headerLvalue);
void extract<T>(out T variableSizeHeader, in bit<32> varFieldSizeBits);
Expand Down

0 comments on commit bbefd1a

Please sign in to comment.