Emit line and offset information with tokens in Lex objects

This commit is contained in:
Eryn Wells 2016-12-27 10:53:38 -07:00
parent 5b88ec73ec
commit f2ff1acdd1
2 changed files with 11 additions and 9 deletions

View file

@ -40,7 +40,8 @@ pub struct Lexer {
input: String, input: String,
begin: usize, begin: usize,
forward: usize, forward: usize,
line: u32, line: usize,
line_offset: usize,
state: State, state: State,
number_builder: NumberBuilder, number_builder: NumberBuilder,
} }
@ -52,7 +53,7 @@ impl Lexer {
begin: 0, begin: 0,
forward: 0, forward: 0,
line: 1, line: 1,
line_char: 1, line_offset: 1,
state: State::Initial, state: State::Initial,
number_builder: NumberBuilder::new(), number_builder: NumberBuilder::new(),
} }
@ -68,15 +69,14 @@ impl Lexer {
/// Advance the forward pointer to the next character. /// Advance the forward pointer to the next character.
fn advance(&mut self) { fn advance(&mut self) {
self.forward = self.input.index_after(self.forward); self.forward = self.input.index_after(self.forward);
self.line_char += 1; self.line_offset += 1;
println!("> forward={}", self.forward); println!("> forward={}", self.forward);
} }
/// Retract the forward pointer to the previous character. /// Retract the forward pointer to the previous character.
fn retract(&mut self) { fn retract(&mut self) {
self.forward = self.input.index_before(self.forward); self.forward = self.input.index_before(self.forward);
self.line_char -= 1; self.line_offset -= 1;
assert!(self.line_char >= 1, "Invalid line character count: {}", self.line_char);
println!("< forward={}", self.forward); println!("< forward={}", self.forward);
} }
@ -90,7 +90,7 @@ impl Lexer {
/// Update lexer state when it encounters a newline. /// Update lexer state when it encounters a newline.
fn handle_newline(&mut self) { fn handle_newline(&mut self) {
self.line += 1; self.line += 1;
self.line_char = 1; self.line_offset = 1;
} }
/// Get the substring between the two input indexes. This is the value to give to a new Token instance. /// Get the substring between the two input indexes. This is the value to give to a new Token instance.
@ -375,7 +375,7 @@ impl Iterator for Lexer {
} }
self.advance_begin(); self.advance_begin();
match token { match token {
Some(t) => Some(Lex::new(t)), Some(t) => Some(Lex::new(t, self.line, self.line_offset)),
None => None, None => None,
} }
} }

View file

@ -22,10 +22,12 @@ pub enum Token {
#[derive(Debug)] #[derive(Debug)]
pub struct Lex { pub struct Lex {
pub token: Token, pub token: Token,
pub line: usize,
pub offset: usize,
} }
impl Lex { impl Lex {
pub fn new(token: Token) -> Lex { pub fn new(token: Token, line: usize, offset: usize) -> Lex {
Lex { token: token } Lex { token: token, line: line, offset: offset }
} }
} }