diff --git a/lexer/src/lib.rs b/lexer/src/lib.rs index ec6e03d..279e40d 100644 --- a/lexer/src/lib.rs +++ b/lexer/src/lib.rs @@ -29,11 +29,17 @@ enum IterationResult { pub struct Lexer where T: Iterator { input: Peekable, + line: usize, + offset: usize, } impl Lexer where T: Iterator { pub fn new(input: T) -> Lexer { - Lexer { input: input.peekable() } + Lexer { + input: input.peekable(), + line: 0, + offset: 0 + } } fn emit(&self, token: Token, resume: Resume) -> IterationResult { @@ -45,6 +51,18 @@ impl Lexer where T: Iterator { } } +impl Lexer where T: Iterator { + fn handle_whitespace(&mut self, c: char) { + if c == '\n' { + self.line += 1; + self.offset = 0; + } + else { + self.offset += 1; + } + } +} + impl Iterator for Lexer where T: Iterator { type Item = Result; @@ -56,7 +74,10 @@ impl Iterator for Lexer where T: Iterator { match peek { Some(c) if c.is_left_paren() => self.emit(Token::LeftParen, Resume::AtNext), Some(c) if c.is_right_paren() => self.emit(Token::RightParen, Resume::AtNext), - Some(c) if c.is_whitespace() => IterationResult::Continue, + Some(c) if c.is_whitespace() => { + self.handle_whitespace(c); + IterationResult::Continue + }, Some(c) if c.is_identifier_initial() => { buffer.push(c); IterationResult::Continue