Add single character identifiers

This commit is contained in:
Eryn Wells 2016-12-24 09:07:38 -07:00
parent fd3f6f9f29
commit f7c28c6732
3 changed files with 14 additions and 2 deletions

View file

@ -9,15 +9,16 @@ pub trait Lexable {
fn is_right_paren(&self) -> bool;
fn is_identifier_initial(&self) -> bool;
fn is_identifier_subsequent(&self) -> bool;
fn is_identifier_single(&self) -> bool;
}
impl Lexable for char {
fn is_left_paren(&self) -> bool {
self == &'('
*self == '('
}
fn is_right_paren(&self) -> bool {
self == &')'
*self == ')'
}
fn is_identifier_initial(&self) -> bool {
@ -27,5 +28,9 @@ impl Lexable for char {
fn is_identifier_subsequent(&self) -> bool {
charset::identifier_subsequents().contains(&self)
}
fn is_identifier_single(&self) -> bool {
charset::identifier_singles().contains(&self)
}
}

View file

@ -41,3 +41,7 @@ pub fn identifier_subsequents() -> CharSet {
subsequents.extend(extras.iter());
subsequents
}
pub fn identifier_singles() -> CharSet {
CharSet::from_iter("+-".chars())
}

View file

@ -76,6 +76,9 @@ impl Lexer {
else if c.is_right_paren() {
*token = Some(Token::new(Kind::RightParen, c.to_string()));
}
else if c.is_identifier_single() {
*token = Some(Token::new(Kind::Identifier, c.to_string()));
}
else if c.is_identifier_initial() {
self.state = State::Identifier;
self.advance();