[lexer] Add number::Sign state

This commit is contained in:
Eryn Wells 2018-09-03 15:49:13 -07:00
parent 663ae3a9f1
commit eabc8f98e8
3 changed files with 60 additions and 5 deletions

View file

@ -18,5 +18,9 @@ impl Error {
Error::new(format!("invalid character: {}", c))
}
pub fn unexpected_eof() -> Error {
Error::new("unexpected EOF".to_string())
}
pub fn msg(&self) -> &str { &self.message }
}

View file

@ -2,10 +2,11 @@
* Eryn Wells <eryn@erynwells.me>
*/
use chars::Lexable;
use error::Error;
use states::{State, StateResult};
use states::number::{Radix, Exact};
use states::number::Builder;
use states::number::{Builder, Radix, Exact};
use states::number::sign::Sign;
use token::Token;
#[derive(Debug)] pub struct Prefix(Builder);
@ -31,9 +32,12 @@ impl Prefix {
impl State for Prefix {
fn lex(&mut self, c: char) -> StateResult {
match c {
'#' => StateResult::advance(Box::new(Hash(self.0))),
_ => StateResult::fail(Error::invalid_char(c))
if c.is_hash() {
StateResult::advance(Box::new(Hash(self.0)))
} else if let Some(sn) = Sign::with_char(self.0, c) {
StateResult::advance(Box::new(sn))
} else {
StateResult::fail(Error::invalid_char(c))
}
}

View file

@ -0,0 +1,47 @@
/* lexer/src/states/number/sign.rs
* Eryn Wells <eryn@erynwells.me>
*/
use error::Error;
use states::{State, StateResult};
use states::number::Builder;
use states::number::Sign as Sgn;
use token::Token;
#[derive(Debug)] pub struct Sign(Builder);
impl Sign {
pub fn new(b: Builder) -> Sign {
Sign(b)
}
pub fn with_char(b: Builder, c: char) -> Option<Sign> {
if !b.seen_sign() {
match c {
'+' => {
let mut b = b.clone();
b.push_sign(Sgn::Pos);
Some(Sign::new(b))
},
'-' => {
let mut b = b.clone();
b.push_sign(Sgn::Neg);
Some(Sign::new(b))
},
_ => None
}
} else {
None
}
}
}
impl State for Sign {
fn lex(&mut self, c: char) -> StateResult {
StateResult::fail(Error::invalid_char(c))
}
fn none(&mut self) -> Result<Option<Token>, Error> {
Err(Error::unexpected_eof())
}
}