[lexer] Bit of code cleanup in Lexer
This commit is contained in:
parent
f35fe5fd08
commit
7a6c2b91d1
1 changed files with 19 additions and 14 deletions
|
@ -26,21 +26,26 @@ impl<T> Lexer<T> where T: Iterator<Item=char> {
|
||||||
Lexer {
|
Lexer {
|
||||||
input: input.peekable(),
|
input: input.peekable(),
|
||||||
line: 0,
|
line: 0,
|
||||||
offset: 0
|
offset: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<T::Item> {
|
||||||
|
let out = self.input.next();
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_whitespace(&mut self, c: char) {
|
||||||
|
if c == '\n' {
|
||||||
|
self.line += 1;
|
||||||
|
self.offset = 1;
|
||||||
|
} else {
|
||||||
|
self.offset += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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> {
|
||||||
|
@ -67,17 +72,17 @@ impl<T> Iterator for Lexer<T> where T: Iterator<Item=char> {
|
||||||
match result {
|
match result {
|
||||||
StateResult::Continue => {
|
StateResult::Continue => {
|
||||||
buffer.push(c);
|
buffer.push(c);
|
||||||
self.input.next();
|
self.next();
|
||||||
},
|
},
|
||||||
StateResult::Advance { to } => {
|
StateResult::Advance { to } => {
|
||||||
buffer.push(c);
|
buffer.push(c);
|
||||||
self.input.next();
|
self.next();
|
||||||
state = to;
|
state = to;
|
||||||
},
|
},
|
||||||
StateResult::Emit(token, resume) => {
|
StateResult::Emit(token, resume) => {
|
||||||
if resume == Resume::AtNext {
|
if resume == Resume::AtNext {
|
||||||
buffer.push(c);
|
buffer.push(c);
|
||||||
self.input.next();
|
self.next();
|
||||||
}
|
}
|
||||||
out = Some(Ok(Lex::new(token, &buffer, self.line, self.offset)));
|
out = Some(Ok(Lex::new(token, &buffer, self.line, self.offset)));
|
||||||
break;
|
break;
|
||||||
|
@ -86,7 +91,7 @@ impl<T> Iterator for Lexer<T> where T: Iterator<Item=char> {
|
||||||
panic!("{}", msg);
|
panic!("{}", msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out
|
out
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue