[lexer] Add Quote token

This commit is contained in:
Eryn Wells 2018-09-08 11:16:53 -07:00
parent 30a876a3f9
commit 061868d2c2
4 changed files with 17 additions and 2 deletions

View file

@ -10,6 +10,7 @@ pub trait Lexable {
fn is_identifier_initial(&self) -> bool;
fn is_identifier_subsequent(&self) -> bool;
fn is_left_paren(&self) -> bool;
fn is_quote(&self) -> bool;
fn is_radix(&self) -> bool;
fn is_right_paren(&self) -> bool;
}
@ -43,6 +44,10 @@ impl Lexable for char {
self.is_whitespace() || self.is_left_paren() || self.is_right_paren()
}
fn is_quote(&self) -> bool {
*self == '\''
}
fn is_radix(&self) -> bool {
let radishes = &['b', 'd', 'o', 'x'];
radishes.contains(self)

View file

@ -36,6 +36,8 @@ impl State for Begin {
StateResult::advance(Box::new(Hash::new()))
} else if let Some(st) = Digit::with_char(&Builder::new(), c) {
StateResult::advance(Box::new(st))
} else if c.is_quote() {
StateResult::Emit(Token::Quote, Resume::AtNext)
} else {
StateResult::fail(Error::invalid_char(c))
}

View file

@ -14,10 +14,11 @@ pub struct Lex {
pub enum Token {
Bool(bool),
Dot,
Num(i64),
Id,
LeftParen,
Num(i64),
Quote,
RightParen,
Id
}
impl Lex {

View file

@ -95,3 +95,10 @@ fn dot() {
assert_eq!(lex.next(), Some(Ok(Lex::new(Token::Dot, ".", 0, 0))));
assert_eq!(lex.next(), None);
}
#[test]
fn quote() {
let mut lex = Lexer::new("'".chars());
assert_eq!(lex.next(), Some(Ok(Lex::new(Token::Quote, "'", 0, 0))));
assert_eq!(lex.next(), None);
}