[lexer] Basic handling of input offsets

This commit is contained in:
Eryn Wells 2017-06-26 21:54:05 -07:00
parent efe0c27d93
commit 5f3770914f

View file

@ -29,11 +29,17 @@ enum IterationResult {
pub struct Lexer<T> where T: Iterator<Item=char> {
input: Peekable<T>,
line: usize,
offset: usize,
}
impl<T> Lexer<T> where T: Iterator<Item=char> {
pub fn new(input: T) -> Lexer<T> {
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<T> Lexer<T> where T: Iterator<Item=char> {
}
}
impl<T> Lexer<T> where T: Iterator<Item=char> {
fn handle_whitespace(&mut self, c: char) {
if c == '\n' {
self.line += 1;
self.offset = 0;
}
else {
self.offset += 1;
}
}
}
impl<T> Iterator for Lexer<T> where T: Iterator<Item=char> {
type Item = Result;
@ -56,7 +74,10 @@ impl<T> Iterator for Lexer<T> where T: Iterator<Item=char> {
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