Pass over whitespace and count lines

This commit is contained in:
Eryn Wells 2016-12-24 09:17:08 -07:00
parent f7c28c6732
commit 8140e3c5e6
2 changed files with 13 additions and 0 deletions

View file

@ -10,6 +10,7 @@ pub trait Lexable {
fn is_identifier_initial(&self) -> bool; fn is_identifier_initial(&self) -> bool;
fn is_identifier_subsequent(&self) -> bool; fn is_identifier_subsequent(&self) -> bool;
fn is_identifier_single(&self) -> bool; fn is_identifier_single(&self) -> bool;
fn is_newline(&self) -> bool;
} }
impl Lexable for char { impl Lexable for char {
@ -32,5 +33,9 @@ impl Lexable for char {
fn is_identifier_single(&self) -> bool { fn is_identifier_single(&self) -> bool {
charset::identifier_singles().contains(&self) charset::identifier_singles().contains(&self)
} }
fn is_newline(&self) -> bool {
*self == '\n'
}
} }

View file

@ -22,6 +22,7 @@ pub struct Lexer {
input: String, input: String,
begin: usize, begin: usize,
forward: usize, forward: usize,
line: u32,
state: State, state: State,
} }
@ -31,6 +32,7 @@ impl Lexer {
input: input, input: input,
begin: 0, begin: 0,
forward: 0, forward: 0,
line: 1,
state: State::Initial, state: State::Initial,
} }
} }
@ -83,6 +85,12 @@ impl Lexer {
self.state = State::Identifier; self.state = State::Identifier;
self.advance(); self.advance();
} }
else if c.is_whitespace() {
if c.is_newline() {
self.line += 1;
}
self.advance();
}
} }
/// Handle self.state == State::Identifier /// Handle self.state == State::Identifier