Created
June 12, 2015 08:05
-
-
Save MarchLiu/6401a95252c5d809776e to your computer and use it in GitHub Desktop.
this is a part of the state and either parsec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub trait BinComb<L, R, T> { | |
type S; | |
fn left(&self)->&mut Parsec<L, Self::S>; | |
fn right(&self)->&mut Parsec<R, Self::S>; | |
} | |
pub struct Either<R, S>{ | |
x: Parsec<R, S>, | |
y: Parsec<R, S>, | |
} | |
impl<R, T, S> BinComb<R, R, T> for Either<R, S> { | |
type S=State<T>; | |
fn left(&self)->&mut Parsec<R, S>{ | |
self.x | |
} | |
fn right(&self)->&mut Parsec<R, S>{ | |
self.y | |
} | |
} | |
pub fn either<T, R, S>(x: Parsec<R, S>, y:Parsec<R, S>)->Either<R, S> where S:State<T>{ | |
Either{ | |
x: x, | |
y: y, | |
} | |
} | |
//... dependences | |
pub struct VecState<T> { | |
index : usize, | |
buffer: Vec<Arc<T>>, | |
} | |
impl<A> FromIterator<A> for VecState<A> { | |
fn from_iter<T>(iterator: T) -> Self where T:IntoIterator<Item=A> { | |
VecState{ | |
index:0, | |
buffer:Vec::from_iter(iterator.into_iter().map(|x:A|Arc::new(x))), | |
} | |
} | |
} | |
pub trait State<T> { | |
fn pos(&self)-> usize; | |
fn seek_to(&mut self, usize)->bool; | |
fn next(&mut self)->Option<Arc<T>>; | |
fn next_by(&mut self, &Fn(Arc<T>)->bool)->Result<T>; | |
} | |
impl<T> State<T> for VecState<T> { | |
fn pos(&self) -> usize { | |
self.index | |
} | |
fn seek_to(&mut self, to:usize) -> bool { | |
if 0 as usize <= to && to < self.buffer.len() { | |
self.index = to; | |
true | |
} else { | |
false | |
} | |
} | |
fn next(&mut self)->Option<Arc<T>>{ | |
if 0 as usize <= self.index && self.index < self.buffer.len() { | |
let item = self.buffer[self.index].clone(); | |
self.index += 1; | |
Some(item.clone()) | |
} else { | |
None | |
} | |
} | |
fn next_by(&mut self, pred:&Fn(Arc<T>)->bool)->Result<T>{ | |
if 0 as usize <= self.index && self.index < self.buffer.len() { | |
let item = self.buffer[self.index].clone(); | |
if pred(item.clone()) { | |
self.index += 1; | |
Ok(item) | |
} else { | |
Err(SimpleError::new(self.index, String::from_str("predicate failed"))) | |
} | |
} else { | |
Err(SimpleError::new(self.index, String::from_str("eof"))) | |
} | |
} | |
} | |
pub struct SimpleError { | |
_pos: usize, | |
_message: String, | |
} | |
impl SimpleError { | |
pub fn new(pos:usize, message:String)->SimpleError{ | |
SimpleError{ | |
_pos: pos, | |
_message: message, | |
} | |
} | |
} | |
pub trait Error { | |
fn pos(&self)->usize; | |
fn message(&self)->&str; | |
} | |
impl Error for SimpleError { | |
fn pos(&self)->usize { | |
self._pos | |
} | |
fn message(&self)->&str { | |
self._message.as_str() | |
} | |
} | |
impl Debug for SimpleError { | |
fn fmt(&self, formatter:&mut Formatter)->result::Result<(), fmt::Error> { | |
let message = format!("<index:{}, mesage:{}>", self.pos(), self.message()); | |
message.fmt(formatter) | |
} | |
} | |
pub type Result<T> = result::Result<Arc<T>, SimpleError>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment