diff --git a/src/parser.rs b/src/parser.rs index 4bf5ff32..18d3fe72 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -353,7 +353,36 @@ impl<'a> Iterator for TokenIterator<'a> { '+' => return Some(Token::Plus), '-' => return Some(Token::Minus), '*' => return Some(Token::Multiply), - '/' => return Some(Token::Divide), + '/' => { + match self.char_stream.peek() { + Some(&'/') => { + self.char_stream.next(); + while let Some(c) = self.char_stream.next() { + if c == '\n' { break; } + } + } + Some(&'*') => { + let mut level = 1; + self.char_stream.next(); + while let Some(c) = self.char_stream.next() { + match c { + '/' => if let Some('*') = self.char_stream.next() { + level+=1; + } + '*' => if let Some('/') = self.char_stream.next() { + level-=1; + } + _ => (), + } + + if level == 0 { + break; + } + } + } + _ => return Some(Token::Divide), + } + } ';' => return Some(Token::Semicolon), ':' => return Some(Token::Colon), ',' => return Some(Token::Comma), diff --git a/src/tests.rs b/src/tests.rs index 543c13f2..acec7717 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -401,3 +401,12 @@ fn test_array_with_structs() { assert!(false); } } + +#[test] +fn test_comments() { + let mut engine = Engine::new(); + + assert!(engine.eval::("let x = 5; x // I am a single line comment, yay!").is_ok()); + + assert!(engine.eval::("let /* I am a multiline comment, yay! */ x = 5; x").is_ok()); +}