[lexer] Basic handling of input offsets
This commit is contained in:
parent
efe0c27d93
commit
5f3770914f
1 changed files with 23 additions and 2 deletions
|
@ -29,11 +29,17 @@ enum IterationResult {
|
||||||
|
|
||||||
pub struct Lexer<T> where T: Iterator<Item=char> {
|
pub struct Lexer<T> where T: Iterator<Item=char> {
|
||||||
input: Peekable<T>,
|
input: Peekable<T>,
|
||||||
|
line: usize,
|
||||||
|
offset: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Lexer<T> where T: Iterator<Item=char> {
|
impl<T> Lexer<T> where T: Iterator<Item=char> {
|
||||||
pub fn new(input: T) -> Lexer<T> {
|
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 {
|
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> {
|
impl<T> Iterator for Lexer<T> where T: Iterator<Item=char> {
|
||||||
type Item = Result;
|
type Item = Result;
|
||||||
|
|
||||||
|
@ -56,7 +74,10 @@ impl<T> Iterator for Lexer<T> where T: Iterator<Item=char> {
|
||||||
match peek {
|
match peek {
|
||||||
Some(c) if c.is_left_paren() => self.emit(Token::LeftParen, Resume::AtNext),
|
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_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() => {
|
Some(c) if c.is_identifier_initial() => {
|
||||||
buffer.push(c);
|
buffer.push(c);
|
||||||
IterationResult::Continue
|
IterationResult::Continue
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue