2018-09-03 15:19:28 -07:00
|
|
|
/* lexer/src/states/number/prefix.rs
|
|
|
|
* Eryn Wells <eryn@erynwells.me>
|
|
|
|
*/
|
|
|
|
|
2018-09-03 15:30:48 -07:00
|
|
|
use error::Error;
|
2018-09-03 15:19:28 -07:00
|
|
|
use states::{State, StateResult};
|
2018-09-03 15:30:48 -07:00
|
|
|
use states::number::{Radix, Exact};
|
2018-09-03 15:19:28 -07:00
|
|
|
use states::number::Builder;
|
|
|
|
use token::Token;
|
|
|
|
|
|
|
|
#[derive(Debug)] pub struct Prefix(Builder);
|
|
|
|
#[derive(Debug)] pub struct Hash(Builder);
|
|
|
|
|
|
|
|
impl Prefix {
|
|
|
|
pub fn new(b: Builder) -> Prefix {
|
|
|
|
Prefix(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_char(c: char) -> Option<Prefix> {
|
|
|
|
let mut builder = Builder::new();
|
|
|
|
if let Some(ex) = Exact::from(c) {
|
|
|
|
builder.push_exact(ex);
|
|
|
|
} else if let Some(rx) = Radix::from(c) {
|
|
|
|
builder.push_radix(rx);
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(Prefix::new(builder))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl State for Prefix {
|
|
|
|
fn lex(&mut self, c: char) -> StateResult {
|
|
|
|
match c {
|
|
|
|
'#' => StateResult::advance(Box::new(Hash(self.0))),
|
2018-09-03 15:30:48 -07:00
|
|
|
_ => StateResult::fail(Error::invalid_char(c))
|
2018-09-03 15:19:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-03 15:30:48 -07:00
|
|
|
fn none(&mut self) -> Result<Option<Token>, Error> {
|
|
|
|
Err(Error::new("blah".to_string()))
|
2018-09-03 15:19:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl State for Hash {
|
|
|
|
fn lex(&mut self, c: char) -> StateResult {
|
|
|
|
if let Some(ex) = Exact::from(c) {
|
|
|
|
if !self.0.seen_exact() {
|
|
|
|
self.0.push_exact(ex);
|
|
|
|
StateResult::advance(Box::new(Prefix::new(self.0)))
|
|
|
|
} else {
|
2018-09-03 15:30:48 -07:00
|
|
|
StateResult::fail(Error::invalid_char(c))
|
2018-09-03 15:19:28 -07:00
|
|
|
}
|
|
|
|
} else if let Some(rx) = Radix::from(c) {
|
|
|
|
if !self.0.seen_radix() {
|
|
|
|
self.0.push_radix(rx);
|
|
|
|
StateResult::advance(Box::new(Prefix::new(self.0)))
|
|
|
|
} else {
|
2018-09-03 15:30:48 -07:00
|
|
|
StateResult::fail(Error::invalid_char(c))
|
2018-09-03 15:19:28 -07:00
|
|
|
}
|
|
|
|
} else {
|
2018-09-03 15:30:48 -07:00
|
|
|
StateResult::fail(Error::invalid_char(c))
|
2018-09-03 15:19:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-03 15:30:48 -07:00
|
|
|
fn none(&mut self) -> Result<Option<Token>, Error> {
|
|
|
|
Err(Error::new("blah".to_string()))
|
2018-09-03 15:19:28 -07:00
|
|
|
}
|
|
|
|
}
|