Add FromChar trait to lexer::char

This commit is contained in:
Eryn Wells 2017-04-30 15:56:21 -07:00
parent 3ffb694ce1
commit 9183e05891
2 changed files with 24 additions and 16 deletions

View file

@ -2,8 +2,13 @@
* Eryn Wells <eryn@erynwells.me> * Eryn Wells <eryn@erynwells.me>
*/ */
use std::marker::Sized;
use charset; use charset;
pub trait FromChar {
fn from_char(c: char) -> Option<Self> where Self: Sized;
}
pub trait Lexable { pub trait Lexable {
fn is_character_leader(&self) -> bool; fn is_character_leader(&self) -> bool;
fn is_dot(&self) -> bool; fn is_dot(&self) -> bool;

View file

@ -3,6 +3,7 @@
*/ */
use sibiltypes::number::{Number, Exact}; use sibiltypes::number::{Number, Exact};
use char::FromChar;
#[derive(Debug)] #[derive(Debug)]
pub enum Radix { Bin, Oct, Dec, Hex } pub enum Radix { Bin, Oct, Dec, Hex }
@ -94,16 +95,6 @@ impl NumberBuilder {
} }
impl Radix { impl Radix {
pub fn from_char(c: char) -> Option<Radix> {
match c {
'b' => Some(Radix::Bin),
'o' => Some(Radix::Oct),
'd' => Some(Radix::Dec),
'h' => Some(Radix::Hex),
_ => None,
}
}
pub fn value(&self) -> u32 { pub fn value(&self) -> u32 {
match *self { match *self {
Radix::Bin => 2, Radix::Bin => 2,
@ -118,8 +109,20 @@ impl Radix {
} }
} }
impl Sign { impl FromChar for Radix {
pub fn from_char(c: char) -> Option<Sign> { fn from_char(c: char) -> Option<Radix> {
match c {
'b' => Some(Radix::Bin),
'o' => Some(Radix::Oct),
'd' => Some(Radix::Dec),
'h' => Some(Radix::Hex),
_ => None,
}
}
}
impl FromChar for Sign {
fn from_char(c: char) -> Option<Sign> {
match c { match c {
'+' => Some(Sign::Pos), '+' => Some(Sign::Pos),
'-' => Some(Sign::Neg), '-' => Some(Sign::Neg),
@ -128,11 +131,11 @@ impl Sign {
} }
} }
impl Exactness { impl FromChar for Exact {
pub fn from_char(c: char) -> Option<Exactness> { fn from_char(c: char) -> Option<Exact> {
match c { match c {
'i' => Some(Exactness::Inexact), 'i' => Some(Exact::No),
'e' => Some(Exactness::Exact), 'e' => Some(Exact::Yes),
_ => None, _ => None,
} }
} }