Add support for block comments to lexer
parent
5838b7eec9
commit
0b437250be
48
src/lex.rs
48
src/lex.rs
|
@ -163,7 +163,9 @@ fn scan_token(partial_source: &str, start_line: usize) -> Result<Consumed, ScanE
|
|||
|
||||
Some(c @ '/') => {
|
||||
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<Consumed, ScanE
|
|||
})
|
||||
}
|
||||
|
||||
fn lex_comment_contents<I: Iterator<Item = char>>(line_iter: &mut I) -> Consumed {
|
||||
fn lex_line_comment_contents<I: Iterator<Item = char>>(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<I: Iterator<Item = char>>(line_iter: &mut I) -> Consumed
|
|||
}
|
||||
}
|
||||
|
||||
fn lex_block_comment_contents<I: Iterator<Item = char>>(
|
||||
line_iter: &mut Peekable<I>,
|
||||
start_line: usize,
|
||||
) -> Result<Consumed, ScanError> {
|
||||
// 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<I: Iterator<Item = char>>(
|
||||
line_iter: &mut I,
|
||||
start_line: usize,
|
||||
|
|
Loading…
Reference in New Issue