diff --git a/src/lexer.rs b/src/lexer.rs index 7ddbede..a94885a 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -1,5 +1,6 @@ #[derive(PartialEq, Eq, Clone, Debug)] pub enum LTIRToken { + Comment(String), Symbol(String), Char(char), Num(i64), @@ -29,6 +30,7 @@ pub enum LexError { #[derive(PartialEq, Eq, Clone, Debug)] pub enum LexerState { Any, + Comment(String), TypeTerm(String), Sym(String), Num(i64), @@ -39,6 +41,7 @@ impl LexerState { fn into_token(self) -> Option { match self { LexerState::Any => None, + LexerState::Comment(s) => Some(LTIRToken::Comment(s)), LexerState::TypeTerm(s) => Some(LTIRToken::AssignType(s)), LexerState::Sym(s) => Some(LTIRToken::Symbol(s)), LexerState::Num(n) => Some(LTIRToken::Num(n)), @@ -169,6 +172,22 @@ where region.end += 1; state = LexerState::Char(None); } + '/' => { + self.chars.next(); + self.position += 1; + region.end += 1; + + match self.chars.next() { + Some('*') => { + self.position += 1; + region.end += 1; + state = LexerState::Comment(String::new()); + } + _ => { + return Some((region, Err(LexError::InvalidChar))); + } + } + } c => { if c.is_whitespace() { self.chars.next(); @@ -183,6 +202,38 @@ where } }, + LexerState::Comment(s) => { + + match self.chars.next() { + Some('*') => { + match self.chars.peek() { + Some('/') => { + self.chars.next(); + self.position += 2; + region.end += 2; + + if let Some(token) = state.clone().into_token() { + return Some((region, Ok(token))); + } + } + _ => { + s.push('*'); + self.position += 1; + region.end += 1; + } + } + } + Some(c) => { + s.push(c); + self.position += 1; + region.end += 1; + } + None => { + return Some((region, Err(LexError::InvalidChar))); + } + } + } + LexerState::Char(val) => { self.position += 2; region.end += 2; @@ -288,6 +339,7 @@ mod tests { fn test_lexer() { let mut lexer = crate::lexer::LTIRLexer::from( "let var1:ℕ=123; + /* comment */ let square =λx.* x x; let sqrt = λx:ℝ~machine::Float64~machine::Word.(f64-sqrt x); diff --git a/src/main.rs b/src/main.rs index 3b918d9..0082d2d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,7 +26,12 @@ fn compile( .compile( &parser::parse_expr( &scope.read().unwrap().typectx, - &mut lexer::LTIRLexer::from(source.chars().peekable()).peekable(), + &mut lexer::LTIRLexer::from(source.chars().peekable()) + .filter(|tok| match tok { + (_, Ok(lexer::LTIRToken::Comment(_))) => false, + _ => true + }) + .peekable(), ) .expect("syntax error"), ) @@ -35,7 +40,6 @@ fn compile( /* TODO: - * - Parse Comments * - write to address resulting from expression * - `::` -> '.' and allow ↦ only * - Parser error reporting