Add comment parsing to the lexer
Now lexer can properly parse and skip comments, as well as any other combination of whitespace and comments.
This commit is contained in:
parent
48f47671cd
commit
569391612a
37
src/main.rs
37
src/main.rs
|
@ -48,21 +48,47 @@ impl<'a> Source<'a> {
|
|||
};
|
||||
}
|
||||
|
||||
fn skip_whitespace(&mut self) {
|
||||
fn skip_whitespace(&mut self) -> bool {
|
||||
let mut chars = self.source[self.cursor..].chars();
|
||||
let mut skipped = false;
|
||||
|
||||
while let Some(c) = chars.next() {
|
||||
if c.is_whitespace() {
|
||||
self.cursor += c.len_utf8();
|
||||
skipped = true;
|
||||
} else {
|
||||
return;
|
||||
return skipped;
|
||||
}
|
||||
};
|
||||
|
||||
return skipped;
|
||||
}
|
||||
|
||||
fn skip_comments(&mut self) -> bool {
|
||||
let mut chars = self.source[self.cursor..].chars();
|
||||
|
||||
if let Some('/') = chars.next() {
|
||||
if let Some('/') = chars.next() {
|
||||
self.cursor += 2;
|
||||
|
||||
while let Some(c) = chars.next() {
|
||||
if c == '\n' {
|
||||
self.cursor += 1;
|
||||
return true;
|
||||
};
|
||||
|
||||
self.cursor += c.len_utf8();
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
fn get_next(&mut self) -> Option<Token<'a>> {
|
||||
self.skip_whitespace();
|
||||
let mut chars = self.source[self.cursor..].chars();
|
||||
// Skip all possible comments and whitespace.
|
||||
while self.skip_comments() || self.skip_whitespace() { };
|
||||
let mut chars = self.source[self.cursor..].chars().peekable();
|
||||
|
||||
let token = match chars.next()? {
|
||||
'+' => Token::Plus,
|
||||
|
@ -215,7 +241,8 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
let inline_source = "3 + 2 - -5;";
|
||||
let inline_source = " // hello, world\n 3;";
|
||||
// let inline_source = "3 + 2 - -5;";
|
||||
// let inline_source = "3 + +5 * +7;";
|
||||
// let inline_source = "3 + 5 * 7;";
|
||||
// let inline_source = "(3 + 5) + 7;";
|
||||
|
|
Loading…
Reference in a new issue