[lexer] Identify delimiters and emit numbers

This commit is contained in:
Eryn Wells 2018-09-03 17:20:27 -07:00
parent 853312ce67
commit 569fe82c1a
2 changed files with 9 additions and 5 deletions

View file

@ -2,9 +2,10 @@
* Eryn Wells <eryn@erynwells.me> * Eryn Wells <eryn@erynwells.me>
*/ */
use chars::Lexable;
use error::Error; use error::Error;
use states::{State, StateResult}; use states::{State, StateResult, Resume};
use states::number::{Builder, Radix, Exact}; use states::number::{Builder, Radix};
use token::Token; use token::Token;
#[derive(Debug)] pub struct Digit(Builder); #[derive(Debug)] pub struct Digit(Builder);
@ -31,6 +32,8 @@ impl State for Digit {
fn lex(&mut self, c: char) -> StateResult { fn lex(&mut self, c: char) -> StateResult {
if self.0.push_digit(c).is_ok() { if self.0.push_digit(c).is_ok() {
StateResult::Continue StateResult::Continue
} else if c.is_identifier_delimiter() {
StateResult::emit(Token::Num(self.0.resolve()), Resume::Here)
} else { } else {
StateResult::fail(Error::invalid_char(c)) StateResult::fail(Error::invalid_char(c))
} }

View file

@ -75,8 +75,9 @@ fn bool_with_spaces() {
#[test] #[test]
fn integer() { fn integer() {
let expected_lex = Lex::new(Token::Num(23), "23", 0, 0); let mut lex = Lexer::new("23 42".chars());
let mut lex = Lexer::new("23".chars()); assert_eq!(lex.next(), Some(Ok(Lex::new(Token::Num(23), "23", 0, 0))));
assert_eq!(lex.next(), Some(Ok(expected_lex))); // TODO: Fix this once issue #12 is fixed.
assert_eq!(lex.next(), Some(Ok(Lex::new(Token::Num(42), " 42", 0, 0))));
assert_eq!(lex.next(), None); assert_eq!(lex.next(), None);
} }