diff --git a/src/lex.rs b/src/lex.rs index 5523961..6c8574e 100644 --- a/src/lex.rs +++ b/src/lex.rs @@ -163,7 +163,9 @@ fn scan_token(partial_source: &str, start_line: usize) -> Result { if next_matches(&mut source_chars, '/') { - return Ok(lex_comment_contents(&mut source_chars)); + return Ok(lex_line_comment_contents(&mut source_chars)); + } else if next_matches(&mut source_chars, '*') { + return lex_block_comment_contents(&mut source_chars, start_line); } Ok((TokenKind::Slash, c.to_string())) @@ -237,7 +239,7 @@ fn scan_token(partial_source: &str, start_line: usize) -> Result>(line_iter: &mut I) -> Consumed { +fn lex_line_comment_contents>(line_iter: &mut I) -> Consumed { let comment_length = length_until_end_of_line(line_iter); Consumed { span: Span { @@ -248,6 +250,48 @@ fn lex_comment_contents>(line_iter: &mut I) -> Consumed } } +fn lex_block_comment_contents>( + line_iter: &mut Peekable, + start_line: usize, +) -> Result { + // We have already read 2 characters + let mut end_found = false; + let mut chars = 2; + let mut lines = 0; + + let mut last = None; + for c in line_iter { + chars += 1; + let last_c = last; + last = Some(c); + if c == '\n' { + lines += 1; + continue; + } + + if last_c == Some('*') && c == '/' { + end_found = true; + break; + } + } + + if end_found { + Ok(Consumed { + token: None, + span: Span { lines, chars }, + }) + } else { + Err(ScanError { + span: Span { lines, chars }, + error: ScriptError { + line: start_line, + location: String::new(), + message: "Unterminated block comment".to_string(), + }, + }) + } +} + fn lex_remaining_string_literal>( line_iter: &mut I, start_line: usize,