Add single character identifiers
This commit is contained in:
parent
fd3f6f9f29
commit
f7c28c6732
3 changed files with 14 additions and 2 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -41,3 +41,7 @@ pub fn identifier_subsequents() -> CharSet {
|
|||
subsequents.extend(extras.iter());
|
||||
subsequents
|
||||
}
|
||||
|
||||
pub fn identifier_singles() -> CharSet {
|
||||
CharSet::from_iter("+-".chars())
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue