Handle single and multi line comments

This commit is contained in:
ConnorSkees 2020-01-05 12:22:38 -05:00
parent 197129aa6d
commit e5f70e0a7a

View File

@ -52,6 +52,7 @@ impl<'a> Iterator for Lexer<'a> {
'{' => symbol!(self, OpenBrace),
'*' => symbol!(self, Mul),
'}' => symbol!(self, CloseBrace),
'/' => self.lex_forward_slash(),
'%' => {
self.buf.next();
self.pos.next_char();
@ -117,6 +118,27 @@ impl<'a> Lexer<'a> {
}
}
fn lex_forward_slash(&mut self) -> TokenKind {
self.buf.next();
self.pos.next_char();
match self.buf.peek().expect("expected something after '/'") {
'/' => {
self.buf.by_ref().skip_while(|x| x != &'\n').count();
}
'*' => {
while let Some(tok) = self.buf.next() {
if tok == '*' {
if self.buf.next() == Some('/') {
break;
}
}
}
}
_ => return TokenKind::Symbol(Symbol::Div),
}
TokenKind::Whitespace(Whitespace::Newline)
}
fn lex_num(&mut self) -> TokenKind {
let mut string = String::with_capacity(99);
while let Some(c) = self.buf.peek() {