Skip to content

Commit

Permalink
chore: fix unterminated block comment (#1958)
Browse files Browse the repository at this point in the history
fix unterminated block comment
  • Loading branch information
kek kek kek authored Jul 18, 2023
1 parent f62ace9 commit 720b1be
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions crates/noirc_frontend/src/lexer/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub enum LexerErrorKind {
TooManyBits { span: Span, max: u32, got: u32 },
#[error("LogicalAnd used instead of bitwise and")]
LogicalAnd { span: Span },
#[error("Unterminated block comment")]
UnterminatedBlockComment { span: Span },
}

impl LexerErrorKind {
Expand All @@ -30,6 +32,7 @@ impl LexerErrorKind {
LexerErrorKind::MalformedFuncAttribute { span, .. } => *span,
LexerErrorKind::TooManyBits { span, .. } => *span,
LexerErrorKind::LogicalAnd { span } => *span,
LexerErrorKind::UnterminatedBlockComment { span } => *span,
}
}

Expand Down Expand Up @@ -77,6 +80,7 @@ impl LexerErrorKind {
"Try `&` instead, or use `if` only if you require short-circuiting".to_string(),
*span,
),
LexerErrorKind::UnterminatedBlockComment { span } => ("unterminated block comment".to_string(), "Unterminated block comment".to_string(), *span),
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion crates/noirc_frontend/src/lexer/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ impl<'a> Lexer<'a> {
}

fn parse_block_comment(&mut self) -> SpannedTokenResult {
let span = Span::new(self.position..self.position + 1);
let mut depth = 1usize;

while let Some(ch) = self.next_char() {
Expand All @@ -352,6 +353,8 @@ impl<'a> Lexer<'a> {
depth -= 1;

// This block comment is closed, so for a construction like "/* */ */"
// there will be a successfully parsed block comment "/* */"
// and " */" will be processed separately.
if depth == 0 {
break;
}
Expand All @@ -360,7 +363,11 @@ impl<'a> Lexer<'a> {
}
}

self.next_token()
if depth == 0 {
self.next_token()
} else {
Err(LexerErrorKind::UnterminatedBlockComment { span })
}
}

/// Skips white space. They are not significant in the source language
Expand Down Expand Up @@ -497,6 +504,16 @@ fn test_arithmetic_sugar() {
}
}

#[test]
fn unterminated_block_comment() {
let input = "/*/";

let mut lexer = Lexer::new(input);
let token = lexer.next().unwrap();

assert!(token.is_err());
}

#[test]
fn test_comment() {
let input = "// hello
Expand Down

0 comments on commit 720b1be

Please sign in to comment.