nom's recognize! was fixed a long time ago

This commit is contained in:
Jethro Beekman 2018-02-17 11:58:38 -08:00
parent 23120e83c4
commit 024892e0f7
3 changed files with 7 additions and 34 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "cexpr"
version = "0.2.2"
version = "0.2.3"
authors = ["Jethro Beekman <jethro@jbeekman.nl>"]
license = "Apache-2.0/MIT"
description = "A C expression parser and evaluator"

View File

@ -114,6 +114,7 @@ macro_rules! typed_token (
});
);
#[allow(unused_macros)]
macro_rules! any_token (
($i:expr,) => ({
if $i.is_empty() {

View File

@ -83,34 +83,6 @@ impl Into<Vec<u8>> for CChar {
// ======== macros that shouldn't be necessary ========
// ====================================================
fn split_off_prefix<'a,T>(full: &'a [T], suffix: &'a [T]) -> &'a [T] {
let n=::std::mem::size_of::<T>();
let start=full.as_ptr() as usize;
let end=start+(full.len()*n);
let cur=suffix.as_ptr() as usize;
assert!(start<=cur && cur<=end);
&full[..(cur-start)/n]
}
// There is a HORRIBLE BUG in nom's recognize!
// https://github.com/Geal/nom/issues/278
#[macro_export]
macro_rules! my_recognize (
($i:expr, $submac:ident!( $($args:tt)* )) => (
{
match $submac!($i, $($args)*) {
IResult::Done(i,_) => IResult::Done(i, split_off_prefix($i,i)),
IResult::Error(e) => IResult::Error(e),
IResult::Incomplete(i) => IResult::Incomplete(i)
}
}
);
($i:expr, $f:expr) => (
my_recognize!($i, call!($f))
);
);
macro_rules! force_type (
($input:expr,IResult<$i:ty,$o:ty,$e:ty>) => (IResult::Error::<$i,$o,$e>(Err::Position(ErrorKind::Fix,$input)))
);
@ -237,11 +209,11 @@ named!(float_exp<(Option<u8>,Vec<u8>)>,preceded!(byte!(b'e'|b'E'),pair!(opt!(byt
named!(c_float<f64>,
map_opt!(alt!(
terminated!(my_recognize!(tuple!(many1!(decimal),byte!(b'.'),many0!(decimal))),opt!(float_width)) |
terminated!(my_recognize!(tuple!(many0!(decimal),byte!(b'.'),many1!(decimal))),opt!(float_width)) |
terminated!(my_recognize!(tuple!(many0!(decimal),opt!(byte!(b'.')),many1!(decimal),float_exp)),opt!(float_width)) |
terminated!(my_recognize!(tuple!(many1!(decimal),opt!(byte!(b'.')),many0!(decimal),float_exp)),opt!(float_width)) |
terminated!(my_recognize!(many1!(decimal)),float_width)
terminated!(recognize!(tuple!(many1!(decimal),byte!(b'.'),many0!(decimal))),opt!(float_width)) |
terminated!(recognize!(tuple!(many0!(decimal),byte!(b'.'),many1!(decimal))),opt!(float_width)) |
terminated!(recognize!(tuple!(many0!(decimal),opt!(byte!(b'.')),many1!(decimal),float_exp)),opt!(float_width)) |
terminated!(recognize!(tuple!(many1!(decimal),opt!(byte!(b'.')),many0!(decimal),float_exp)),opt!(float_width)) |
terminated!(recognize!(many1!(decimal)),float_width)
),|v|str::from_utf8(v).ok().and_then(|i|f64::from_str(i).ok()))
);