From b8157a11216eebbd64bad4739b68c055ac180f24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Hozda?= Date: Sat, 28 Oct 2017 12:39:29 +0200 Subject: [PATCH] implement comments --- src/parser.rs | 31 ++++++++++++++++++++++++++++++- src/tests.rs | 9 +++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) 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()); +}