From db257057e26bec4abac459299cde778474c1fa8f Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Mon, 19 May 2014 18:44:20 -0700 Subject: [PATCH] support both LL(0) and LL(1) grammars --- de.rs | 187 +++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 145 insertions(+), 42 deletions(-) diff --git a/de.rs b/de.rs index 623759c2..0c8b28cb 100644 --- a/de.rs +++ b/de.rs @@ -62,23 +62,40 @@ pub trait Deserializer: Iterator> { } #[inline] - fn expect_null(&mut self, token: Token) -> Result<(), E> { + fn expect_null(&mut self) -> Result<(), E> { + let token = try!(self.expect_token()); + self.expect_null_token(token) + } + + #[inline] + fn expect_null_token(&mut self, token: Token) -> Result<(), E> { decode_primitive!( Null => Ok(()), CollectionStart(_) => { - let token = try!(self.expect_token()); - self.expect_collection_end(token) + self.expect_collection_end() } ) } #[inline] - fn expect_bool(&mut self, token: Token) -> Result { + fn expect_bool(&mut self) -> Result { + let token = try!(self.expect_token()); + self.expect_bool_token(token) + } + + #[inline] + fn expect_bool_token(&mut self, token: Token) -> Result { decode_primitive!(Bool(value) => Ok(value)) } #[inline] - fn expect_num(&mut self, token: Token) -> Result { + fn expect_num(&mut self) -> Result { + let token = try!(self.expect_token()); + self.expect_num_token(token) + } + + #[inline] + fn expect_num_token(&mut self, token: Token) -> Result { match token { Int(x) => to_result!(num::cast(x), self.syntax_error()), I8(x) => to_result!(num::cast(x), self.syntax_error()), @@ -97,17 +114,35 @@ pub trait Deserializer: Iterator> { } #[inline] - fn expect_char(&mut self, token: Token) -> Result { + fn expect_char(&mut self) -> Result { + let token = try!(self.expect_token()); + self.expect_char_token(token) + } + + #[inline] + fn expect_char_token(&mut self, token: Token) -> Result { decode_primitive!(Char(value) => Ok(value)) } #[inline] - fn expect_str(&mut self, token: Token) -> Result<&'static str, E> { + fn expect_str(&mut self) -> Result<&'static str, E> { + let token = try!(self.expect_token()); + self.expect_str_token(token) + } + + #[inline] + fn expect_str_token(&mut self, token: Token) -> Result<&'static str, E> { decode_primitive!(Str(value) => Ok(value)) } #[inline] - fn expect_strbuf(&mut self, token: Token) -> Result { + fn expect_strbuf(&mut self) -> Result { + let token = try!(self.expect_token()); + self.expect_strbuf_token(token) + } + + #[inline] + fn expect_strbuf_token(&mut self, token: Token) -> Result { decode_primitive!( Str(value) => Ok(value.to_strbuf()), StrBuf(value) => Ok(value) @@ -117,6 +152,14 @@ pub trait Deserializer: Iterator> { #[inline] fn expect_option< T: Deserializable + >(&mut self) -> Result, E> { + let token = try!(self.expect_token()); + self.expect_option_token(token) + } + + #[inline] + fn expect_option_token< + T: Deserializable >(&mut self, token: Token) -> Result, E> { match token { Null => Ok(None), @@ -131,8 +174,17 @@ pub trait Deserializer: Iterator> { fn expect_collection< T: Deserializable, C: FromIterator + >(&mut self) -> Result { + let token = try!(self.expect_token()); + self.expect_collection_token(token) + } + + #[inline] + fn expect_collection_token< + T: Deserializable, + C: FromIterator >(&mut self, token: Token) -> Result { - let len = try!(self.expect_collection_start(token)); + let len = try!(self.expect_collection_start_token(token)); let iter = self.by_ref().batch(|d| { let d = d.iter(); @@ -160,7 +212,13 @@ pub trait Deserializer: Iterator> { } #[inline] - fn expect_collection_start(&mut self, token: Token) -> Result { + fn expect_collection_start(&mut self) -> Result { + let token = try!(self.expect_token()); + self.expect_collection_start_token(token) + } + + #[inline] + fn expect_collection_start_token(&mut self, token: Token) -> Result { match token { CollectionStart(len) => Ok(len), _ => Err(self.syntax_error()), @@ -168,7 +226,13 @@ pub trait Deserializer: Iterator> { } #[inline] - fn expect_collection_end(&mut self, token: Token) -> Result<(), E> { + fn expect_collection_end(&mut self) -> Result<(), E> { + let token = try!(self.expect_token()); + self.expect_collection_end_token(token) + } + + #[inline] + fn expect_collection_end_token(&mut self, token: Token) -> Result<(), E> { match token { CollectionEnd => Ok(()), _ => Err(self.syntax_error()), @@ -179,10 +243,7 @@ pub trait Deserializer: Iterator> { ////////////////////////////////////////////////////////////////////////////// pub trait Deserializable> { - fn deserialize(d: &mut D) -> Result { - let token = try!(d.expect_token()); - Deserializable::deserialize_token(d, token) - } + fn deserialize(d: &mut D) -> Result; fn deserialize_token(d: &mut D, token: Token) -> Result; } @@ -190,35 +251,40 @@ pub trait Deserializable> { ////////////////////////////////////////////////////////////////////////////// macro_rules! impl_deserializable { - ($ty:ty, $method:ident) => { + ($ty:ty, $method:ident, $method_token:ident) => { impl< E, D: Deserializer > Deserializable for $ty { + #[inline] + fn deserialize(d: &mut D) -> Result<$ty, E> { + d.$method() + } + #[inline] fn deserialize_token(d: &mut D, token: Token) -> Result<$ty, E> { - d.$method(token) + d.$method_token(token) } } } } -impl_deserializable!(bool, expect_bool) -impl_deserializable!(int, expect_num) -impl_deserializable!(i8, expect_num) -impl_deserializable!(i16, expect_num) -impl_deserializable!(i32, expect_num) -impl_deserializable!(i64, expect_num) -impl_deserializable!(uint, expect_num) -impl_deserializable!(u8, expect_num) -impl_deserializable!(u16, expect_num) -impl_deserializable!(u32, expect_num) -impl_deserializable!(u64, expect_num) -impl_deserializable!(f32, expect_num) -impl_deserializable!(f64, expect_num) -impl_deserializable!(char, expect_char) -impl_deserializable!(&'static str, expect_str) -impl_deserializable!(StrBuf, expect_strbuf) +impl_deserializable!(bool, expect_bool, expect_bool_token) +impl_deserializable!(int, expect_num, expect_num_token) +impl_deserializable!(i8, expect_num, expect_num_token) +impl_deserializable!(i16, expect_num, expect_num_token) +impl_deserializable!(i32, expect_num, expect_num_token) +impl_deserializable!(i64, expect_num, expect_num_token) +impl_deserializable!(uint, expect_num, expect_num_token) +impl_deserializable!(u8, expect_num, expect_num_token) +impl_deserializable!(u16, expect_num, expect_num_token) +impl_deserializable!(u32, expect_num, expect_num_token) +impl_deserializable!(u64, expect_num, expect_num_token) +impl_deserializable!(f32, expect_num, expect_num_token) +impl_deserializable!(f64, expect_num, expect_num_token) +impl_deserializable!(char, expect_char, expect_char_token) +impl_deserializable!(&'static str, expect_str, expect_str_token) +impl_deserializable!(StrBuf, expect_strbuf, expect_strbuf_token) ////////////////////////////////////////////////////////////////////////////// @@ -227,9 +293,14 @@ impl< D: Deserializer, T: Deserializable > Deserializable for Option { + #[inline] + fn deserialize(d: &mut D) -> Result, E> { + d.expect_option() + } + #[inline] fn deserialize_token(d: &mut D, token: Token) -> Result, E> { - d.expect_option(token) + d.expect_option_token(token) } } @@ -240,9 +311,14 @@ impl< D: Deserializer, T: Deserializable > Deserializable for Vec { + #[inline] + fn deserialize(d: &mut D) -> Result, E> { + d.expect_collection() + } + #[inline] fn deserialize_token(d: &mut D, token: Token) -> Result, E> { - d.expect_collection(token) + d.expect_collection_token(token) } } @@ -252,9 +328,14 @@ impl< K: Deserializable + TotalEq + Hash, V: Deserializable > Deserializable for HashMap { + #[inline] + fn deserialize(d: &mut D) -> Result, E> { + d.expect_collection() + } + #[inline] fn deserialize_token(d: &mut D, token: Token) -> Result, E> { - d.expect_collection(token) + d.expect_collection_token(token) } } @@ -264,9 +345,14 @@ impl< E, D: Deserializer > Deserializable for () { + #[inline] + fn deserialize(d: &mut D) -> Result<(), E> { + d.expect_null() + } + #[inline] fn deserialize_token(d: &mut D, token: Token) -> Result<(), E> { - d.expect_null(token) + d.expect_null_token(token) } } @@ -280,11 +366,12 @@ macro_rules! deserialize_tuple ( impl< E, D: Deserializer, - $($name:Deserializable),*> Deserializable for ($($name,)*) { + $($name:Deserializable),* + > Deserializable for ($($name,)*) { #[inline] #[allow(uppercase_variables)] - fn deserialize_token(d: &mut D, token: Token) -> Result<($($name,)*), E> { - try!(d.expect_collection_start(token)); + fn deserialize(d: &mut D) -> Result<($($name,)*), E> { + try!(d.expect_collection_start()); let result = ($( { @@ -293,8 +380,24 @@ macro_rules! deserialize_tuple ( } ,)*); - let token = try!(d.expect_token()); - try!(d.expect_collection_end(token)); + try!(d.expect_collection_end()); + + Ok(result) + } + + #[inline] + #[allow(uppercase_variables)] + fn deserialize_token(d: &mut D, token: Token) -> Result<($($name,)*), E> { + try!(d.expect_collection_start_token(token)); + + let result = ($( + { + let $name = try!(Deserializable::deserialize(d)); + $name + } + ,)*); + + try!(d.expect_collection_end()); Ok(result) }