From 720b1be0a43eead26223e6d26bf94940a462a1c7 Mon Sep 17 00:00:00 2001 From: kek kek kek Date: Tue, 18 Jul 2023 07:10:05 -0700 Subject: [PATCH] chore: fix unterminated block comment (#1958) fix unterminated block comment --- crates/noirc_frontend/src/lexer/errors.rs | 4 ++++ crates/noirc_frontend/src/lexer/lexer.rs | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/noirc_frontend/src/lexer/errors.rs b/crates/noirc_frontend/src/lexer/errors.rs index 99220b94085..0b6440dec44 100644 --- a/crates/noirc_frontend/src/lexer/errors.rs +++ b/crates/noirc_frontend/src/lexer/errors.rs @@ -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 { @@ -30,6 +32,7 @@ impl LexerErrorKind { LexerErrorKind::MalformedFuncAttribute { span, .. } => *span, LexerErrorKind::TooManyBits { span, .. } => *span, LexerErrorKind::LogicalAnd { span } => *span, + LexerErrorKind::UnterminatedBlockComment { span } => *span, } } @@ -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), } } } diff --git a/crates/noirc_frontend/src/lexer/lexer.rs b/crates/noirc_frontend/src/lexer/lexer.rs index bfb6ca48789..0e572f7247c 100644 --- a/crates/noirc_frontend/src/lexer/lexer.rs +++ b/crates/noirc_frontend/src/lexer/lexer.rs @@ -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() { @@ -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; } @@ -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 @@ -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