[lexer] We can lex integers!
This commit is contained in:
parent
d272b211ae
commit
853312ce67
4 changed files with 24 additions and 14 deletions
|
@ -8,20 +8,28 @@ use token::Token;
|
|||
use states::{Resume, State, StateResult};
|
||||
use states::id::IdSub;
|
||||
use states::hash::Hash;
|
||||
use states::number::{Builder, Digit};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Begin;
|
||||
|
||||
impl State for Begin {
|
||||
fn lex(&mut self, c: char) -> StateResult {
|
||||
match c {
|
||||
c if c.is_left_paren() => StateResult::Emit(Token::LeftParen, Resume::AtNext),
|
||||
c if c.is_right_paren() => StateResult::Emit(Token::RightParen, Resume::AtNext),
|
||||
if c.is_left_paren() {
|
||||
StateResult::Emit(Token::LeftParen, Resume::AtNext)
|
||||
} else if c.is_right_paren() {
|
||||
StateResult::Emit(Token::RightParen, Resume::AtNext)
|
||||
} else if c.is_whitespace() {
|
||||
// TODO: Figure out some way to track newlines.
|
||||
c if c.is_whitespace() => StateResult::Continue,
|
||||
c if c.is_identifier_initial() => StateResult::Advance { to: Box::new(IdSub{}) },
|
||||
c if c.is_hash() => StateResult::Advance { to: Box::new(Hash::new()) },
|
||||
_ => StateResult::fail(Error::invalid_char(c)),
|
||||
StateResult::Continue
|
||||
} else if c.is_identifier_initial() {
|
||||
StateResult::advance(Box::new(IdSub{}))
|
||||
} else if c.is_hash() {
|
||||
StateResult::advance(Box::new(Hash::new()))
|
||||
} else if let Some(st) = Digit::with_char(Builder::new(), c) {
|
||||
StateResult::advance(Box::new(st))
|
||||
} else {
|
||||
StateResult::fail(Error::invalid_char(c))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ impl State for Hash {
|
|||
}
|
||||
|
||||
fn none(&mut self) -> Result<Option<Token>, Error> {
|
||||
Ok(None)
|
||||
Err(Error::unexpected_eof())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,11 +2,9 @@
|
|||
* Eryn Wells <eryn@erynwells.me>
|
||||
*/
|
||||
|
||||
use chars::Lexable;
|
||||
use error::Error;
|
||||
use states::{State, StateResult};
|
||||
use states::number::{Builder, Radix, Exact};
|
||||
use states::number::sign::Sign;
|
||||
use token::Token;
|
||||
|
||||
#[derive(Debug)] pub struct Digit(Builder);
|
||||
|
@ -39,6 +37,6 @@ impl State for Digit {
|
|||
}
|
||||
|
||||
fn none(&mut self) -> Result<Option<Token>, Error> {
|
||||
Err(Error::unexpected_eof())
|
||||
Ok(Some(Token::Num(self.0.resolve())))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ mod prefix;
|
|||
mod sign;
|
||||
|
||||
pub use self::prefix::Prefix;
|
||||
pub use self::digit::Digit;
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
pub enum Radix { Bin = 2, Oct = 8, Dec = 10, Hex = 16 }
|
||||
|
@ -83,9 +84,12 @@ impl Builder {
|
|||
}
|
||||
|
||||
fn resolve(&self) -> i64 {
|
||||
//let sign_factor: i64 = if let Some(sign) = self.sign { sign as i64 } else { 1 };
|
||||
//self.value * sign_factor
|
||||
0
|
||||
let sign_factor: i64 = if let Some(sign) = self.sign {
|
||||
sign as i64
|
||||
} else {
|
||||
1
|
||||
};
|
||||
self.value * sign_factor
|
||||
}
|
||||
|
||||
fn seen_exact(&self) -> bool { self.exact.is_some() }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue