diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/expr.rs | 57 | ||||
-rw-r--r-- | src/lib.rs | 11 | ||||
-rw-r--r-- | src/literal.rs | 16 |
3 files changed, 40 insertions, 44 deletions
diff --git a/src/expr.rs b/src/expr.rs index b1fbfb2..5dce3c7 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -31,7 +31,7 @@ use crate::token::{Kind as TokenKind, Token}; use crate::ToCexprResult; use nom::branch::alt; use nom::combinator::{complete, map, map_opt}; -use nom::multi::{fold_many0, many0, separated_list}; +use nom::multi::{fold_many0, many0, separated_list0}; use nom::sequence::{delimited, pair, preceded}; use nom::*; @@ -100,7 +100,7 @@ macro_rules! exact_token ( ($k:ident, $c:expr) => ({ move |input: &[Token]| { if input.is_empty() { - let res: CResult<'_, &[u8]> = Err(crate::nom::Err::Incomplete(Needed::Size($c.len()))); + let res: CResult<'_, &[u8]> = Err(crate::nom::Err::Incomplete(Needed::new($c.len()))); res } else { if input[0].kind==TokenKind::$k && &input[0].raw[..]==$c { @@ -113,29 +113,16 @@ macro_rules! exact_token ( }); ); -macro_rules! typed_token ( - ($k:ident) => ({ - move |input: &[Token]| { - if input.is_empty() { - let res: CResult<'_, &[u8]> = Err(nom::Err::Incomplete(Needed::Size(1))); - res - } else { - if input[0].kind==TokenKind::$k { - Ok((&input[1..], &input[0].raw[..])) - } else { - Err(crate::nom::Err::Error((input, crate::ErrorKind::TypedToken(TokenKind::$k)).into())) - } - } - } - }); -); - -#[allow(dead_code)] -fn any_token(input: &[Token]) -> CResult<'_, &Token> { +fn identifier_token(input: &[Token]) -> CResult<'_, &[u8]> { if input.is_empty() { - Err(crate::nom::Err::Incomplete(Needed::Size(1))) + let res: CResult<'_, &[u8]> = Err(nom::Err::Incomplete(Needed::new(1))); + res } else { - Ok((&input[1..], &input[0])) + if input[0].kind == TokenKind::Identifier { + Ok((&input[1..], &input[0].raw[..])) + } else { + Err(crate::nom::Err::Error((input, crate::ErrorKind::TypedToken(TokenKind::Identifier)).into())) + } } } @@ -151,7 +138,7 @@ fn one_of_punctuation(c: &'static [&'static str]) -> impl Fn(&[Token]) -> CResul .map(|opt| opt.len()) .min() .expect("at least one option"); - Err(crate::nom::Err::Incomplete(Needed::Size(min))) + Err(crate::nom::Err::Incomplete(Needed::new(min))) } else if input[0].kind == TokenKind::Punctuation && c.iter().any(|opt| opt.as_bytes() == &input[0].raw[..]) { @@ -295,9 +282,9 @@ fn unary_op(input: (&[u8], EvalResult)) -> Option<EvalResult> { fn numeric<I: Clone, E: nom::error::ParseError<I>, F>( f: F, -) -> impl Fn(I) -> nom::IResult<I, EvalResult, E> +) -> impl FnMut(I) -> nom::IResult<I, EvalResult, E> where - F: Fn(I) -> nom::IResult<I, EvalResult, E>, + F: FnMut(I) -> nom::IResult<I, EvalResult, E>, { nom::combinator::map_opt(f, EvalResult::as_numeric) } @@ -419,7 +406,7 @@ impl<'a> PRef<'a> { impl<'a> PRef<'a> { fn identifier(self, input: &'_ [Token]) -> CResult<'_, EvalResult> { match input.split_first() { - None => Err(Err::Incomplete(Needed::Size(1))), + None => Err(Err::Incomplete(Needed::new(1))), Some(( &Token { kind: TokenKind::Identifier, @@ -443,7 +430,7 @@ impl<'a> PRef<'a> { fn literal(self, input: &'_ [Token]) -> CResult<'_, EvalResult> { match input.split_first() { - None => Err(Err::Incomplete(Needed::Size(1))), + None => Err(Err::Incomplete(Needed::new(1))), Some(( &Token { kind: TokenKind::Literal, @@ -496,7 +483,7 @@ impl<'a> PRef<'a> { } fn macro_definition(self, input: &'_ [Token]) -> CResult<'_, (&'_ [u8], EvalResult)> { - pair(typed_token!(Identifier), |i| self.expr(i))(input) + pair(identifier_token, |i| self.expr(i))(input) } } @@ -519,7 +506,7 @@ impl<'ident> IdentifierParser<'ident> { IdentifierParser { identifiers } } - /// Parse and evalute an expression of a list of tokens. + /// Parse and evaluate an expression of a list of tokens. /// /// Returns an error if the input is not a valid expression or if the token /// stream contains comments, keywords or unknown identifiers. @@ -527,7 +514,7 @@ impl<'ident> IdentifierParser<'ident> { self.as_ref().expr(input) } - /// Parse and evaluate a macro definition from of a list of tokens. + /// Parse and evaluate a macro definition from a list of tokens. /// /// Returns the identifier for the macro and its replacement evaluated as an /// expression. The input should not include `#define`. @@ -552,7 +539,7 @@ impl<'ident> IdentifierParser<'ident> { } } -/// Parse and evalute an expression of a list of tokens. +/// Parse and evaluate an expression of a list of tokens. /// /// Returns an error if the input is not a valid expression or if the token /// stream contains comments, keywords or identifiers. @@ -560,7 +547,7 @@ pub fn expr(input: &[Token]) -> CResult<'_, EvalResult> { IdentifierParser::new(&HashMap::new()).expr(input) } -/// Parse and evaluate a macro definition from of a list of tokens. +/// Parse and evaluate a macro definition from a list of tokens. /// /// Returns the identifier for the macro and its replacement evaluated as an /// expression. The input should not include `#define`. @@ -613,10 +600,10 @@ pub fn macro_definition(input: &[Token]) -> CResult<'_, (&'_ [u8], EvalResult)> /// ``` pub fn fn_macro_declaration(input: &[Token]) -> CResult<'_, (&[u8], Vec<&[u8]>)> { pair( - typed_token!(Identifier), + identifier_token, delimited( p("("), - separated_list(p(","), typed_token!(Identifier)), + separated_list0(p(","), identifier_token), p(")"), ), )(input) @@ -19,7 +19,7 @@ pub mod nom { //! nom's result types, re-exported. - pub use nom::{error::ErrorKind, Err, IResult, Needed}; + pub use nom::{error::ErrorKind, error::Error, Err, IResult, Needed}; } pub mod expr; pub mod literal; @@ -86,6 +86,15 @@ impl<I> From<(I, ErrorKind)> for Error<I> { } } +impl<I> From<::nom::error::Error<I>> for Error<I> { + fn from(e: ::nom::error::Error<I>) -> Self { + Self { + input: e.input, + error: e.code.into(), + } + } +} + impl<I> ::nom::error::ParseError<I> for Error<I> { fn from_error_kind(input: I, kind: nom::ErrorKind) -> Self { Self { diff --git a/src/literal.rs b/src/literal.rs index 39f07be..b74699f 100644 --- a/src/literal.rs +++ b/src/literal.rs @@ -70,7 +70,7 @@ impl From<u8> for CChar { } // A non-allocating version of this would be nice... -impl Into<Vec<u8>> for CChar { +impl std::convert::Into<Vec<u8>> for CChar { fn into(self) -> Vec<u8> { match self { CChar::Char(c) => { @@ -88,12 +88,12 @@ impl Into<Vec<u8>> for CChar { } /// ensures the child parser consumes the whole input -pub fn full<I: Clone, O, E: From<nom::error::ErrorKind>, F>( +pub fn full<I: Clone, O, F>( f: F, -) -> impl Fn(I) -> nom::IResult<I, O, (I, E)> +) -> impl Fn(I) -> nom::IResult<I, O> where I: nom::InputLength, - F: Fn(I) -> nom::IResult<I, O, (I, E)>, + F: Fn(I) -> nom::IResult<I, O>, { move |input| { let res = f(input); @@ -102,7 +102,7 @@ where if i.input_len() == 0 { Ok((i, o)) } else { - Err(nom::Err::Error((i, nom::error::ErrorKind::Complete.into()))) + Err(nom::Err::Error(nom::error::Error::new(i, nom::error::ErrorKind::Complete))) } } r => r, @@ -119,8 +119,8 @@ macro_rules! byte { fn parser(i: &[u8]) -> crate::nom::IResult<&[u8], u8> { match i.split_first() { $(Some((&c @ $p,rest)))|* => Ok((rest,c)), - Some(_) => Err(nom::Err::Error((i, nom::error::ErrorKind::OneOf))), - None => Err(nom::Err::Incomplete(Needed::Size(1))), + Some(_) => Err(nom::Err::Error(nom::error::Error::new(i, nom::error::ErrorKind::OneOf))), + None => Err(nom::Err::Incomplete(Needed::new(1))), } } @@ -272,7 +272,7 @@ fn c_int(i: &[u8]) -> nom::IResult<&[u8], i64> { c_int_radix(v, 8) }), map_opt(many1(complete(decimal)), |v| c_int_radix(v, 10)), - |input| Err(crate::nom::Err::Error((input, crate::nom::ErrorKind::Fix))), + |input| Err(crate::nom::Err::Error(nom::error::Error::new(input, crate::nom::ErrorKind::Fix))), )), opt(take_ul), ), |