From 9183e05891e4e9cfc1e3ce8b6f8b732bae9c2531 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sun, 30 Apr 2017 15:56:21 -0700 Subject: [PATCH] Add FromChar trait to lexer::char --- lexer/src/char.rs | 5 +++++ lexer/src/number.rs | 35 +++++++++++++++++++---------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/lexer/src/char.rs b/lexer/src/char.rs index d134a18..06fdd82 100644 --- a/lexer/src/char.rs +++ b/lexer/src/char.rs @@ -2,8 +2,13 @@ * Eryn Wells */ +use std::marker::Sized; use charset; +pub trait FromChar { + fn from_char(c: char) -> Option where Self: Sized; +} + pub trait Lexable { fn is_character_leader(&self) -> bool; fn is_dot(&self) -> bool; diff --git a/lexer/src/number.rs b/lexer/src/number.rs index 7bab968..130acde 100644 --- a/lexer/src/number.rs +++ b/lexer/src/number.rs @@ -3,6 +3,7 @@ */ use sibiltypes::number::{Number, Exact}; +use char::FromChar; #[derive(Debug)] pub enum Radix { Bin, Oct, Dec, Hex } @@ -94,16 +95,6 @@ impl NumberBuilder { } impl Radix { - pub fn from_char(c: char) -> Option { - match c { - 'b' => Some(Radix::Bin), - 'o' => Some(Radix::Oct), - 'd' => Some(Radix::Dec), - 'h' => Some(Radix::Hex), - _ => None, - } - } - pub fn value(&self) -> u32 { match *self { Radix::Bin => 2, @@ -118,8 +109,20 @@ impl Radix { } } -impl Sign { - pub fn from_char(c: char) -> Option { +impl FromChar for Radix { + fn from_char(c: char) -> Option { + 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 { match c { '+' => Some(Sign::Pos), '-' => Some(Sign::Neg), @@ -128,11 +131,11 @@ impl Sign { } } -impl Exactness { - pub fn from_char(c: char) -> Option { +impl FromChar for Exact { + fn from_char(c: char) -> Option { match c { - 'i' => Some(Exactness::Inexact), - 'e' => Some(Exactness::Exact), + 'i' => Some(Exact::No), + 'e' => Some(Exact::Yes), _ => None, } }