From e5f70e0a7acfd0c86c2af1cda31550eec1453982 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sun, 5 Jan 2020 12:22:38 -0500 Subject: [PATCH] Handle single and multi line comments --- src/lexer.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/lexer.rs b/src/lexer.rs index 90cc1ed..8125336 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -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() {