mirror of
https://gitee.com/openharmony/third_party_rust_nom
synced 2024-11-27 17:51:02 +00:00
Fix compilation
This commit is contained in:
parent
a413d4be1a
commit
60d8071af9
@ -78,7 +78,7 @@ macro_rules! take_bits (
|
||||
{
|
||||
use std::ops::Div;
|
||||
let (input, bit_offset) = $i;
|
||||
if $count == 0 {
|
||||
let res : $crate::IResult<(&[u8],usize), $t> = if $count == 0 {
|
||||
$crate::IResult::Done( (input, bit_offset), 0)
|
||||
} else {
|
||||
let cnt = ($count as usize + bit_offset).div(8);
|
||||
@ -112,7 +112,8 @@ macro_rules! take_bits (
|
||||
}
|
||||
$crate::IResult::Done( (&input[cnt..], end_offset) , acc)
|
||||
}
|
||||
}
|
||||
};
|
||||
res
|
||||
}
|
||||
);
|
||||
);
|
||||
|
12
src/bytes.rs
12
src/bytes.rs
@ -25,15 +25,16 @@ macro_rules! tag (
|
||||
}
|
||||
|
||||
let expected = $inp;
|
||||
let bytes = as_bytes(&expected);
|
||||
let bytes: &[u8] = as_bytes(&expected);
|
||||
|
||||
if bytes.len() > $i.len() {
|
||||
let res: $crate::IResult<&[u8],&[u8]> = if bytes.len() > $i.len() {
|
||||
$crate::IResult::Incomplete($crate::Needed::Size(bytes.len()))
|
||||
} else if &$i[0..bytes.len()] == bytes {
|
||||
$crate::IResult::Done(&$i[bytes.len()..], &$i[0..bytes.len()])
|
||||
} else {
|
||||
$crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Tag, $i))
|
||||
}
|
||||
};
|
||||
res
|
||||
}
|
||||
);
|
||||
);
|
||||
@ -254,11 +255,12 @@ macro_rules! take(
|
||||
($i:expr, $count:expr) => (
|
||||
{
|
||||
let cnt = $count as usize;
|
||||
if $i.len() < cnt {
|
||||
let res: $crate::IResult<&[u8],&[u8]> = if $i.len() < cnt {
|
||||
$crate::IResult::Incomplete($crate::Needed::Size(cnt))
|
||||
} else {
|
||||
$crate::IResult::Done(&$i[cnt..],&$i[0..cnt])
|
||||
}
|
||||
};
|
||||
res
|
||||
}
|
||||
);
|
||||
);
|
||||
|
@ -114,7 +114,7 @@ macro_rules! named (
|
||||
}
|
||||
);
|
||||
($name:ident, $submac:ident!( $($args:tt)* )) => (
|
||||
fn $name<'a>( i: &'a [u8] ) -> $crate::IResult<'a,&[u8], &[u8], u32> {
|
||||
fn $name<'a>( i: &'a [u8] ) -> $crate::IResult<'a,&[u8], &[u8]> {
|
||||
$submac!(i, $($args)*)
|
||||
}
|
||||
);
|
||||
@ -134,7 +134,7 @@ macro_rules! named (
|
||||
}
|
||||
);
|
||||
(pub $name:ident, $submac:ident!( $($args:tt)* )) => (
|
||||
pub fn $name<'a>( i: &'a [u8] ) -> $crate::IResult<'a,&[u8], &[u8], u32> {
|
||||
pub fn $name<'a>( i: &'a [u8] ) -> $crate::IResult<'a,&[u8], &[u8]> {
|
||||
$submac!(i, $($args)*)
|
||||
}
|
||||
);
|
||||
@ -728,12 +728,10 @@ macro_rules! alt_parser (
|
||||
|
||||
($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => (
|
||||
{
|
||||
match $subrule!( $i, $($args)* ) {
|
||||
$crate::IResult::Done(i,o) => $crate::IResult::Done(i,o),
|
||||
$crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x),
|
||||
$crate::IResult::Error(_) => {
|
||||
alt_parser!($i, $($rest)*)
|
||||
}
|
||||
let res = $submac!($i, $($args)*);
|
||||
match res {
|
||||
$crate::IResult::Done(_,_) => res,
|
||||
_ => alt_parser!($i, $($rest)*)
|
||||
}
|
||||
}
|
||||
);
|
||||
@ -900,18 +898,19 @@ macro_rules! opt_res (
|
||||
/// ```
|
||||
/// # #[macro_use] extern crate nom;
|
||||
/// # use nom::IResult::Done;
|
||||
/// # use nom::IResult;
|
||||
/// # fn main() {
|
||||
/// let b = true;
|
||||
/// let f = closure!(&'static[u8],
|
||||
/// cond!( b, tag!("abcd") )
|
||||
/// let f: Box<Fn(&'static [u8]) -> IResult<&[u8],Option<&[u8]>>> = Box::new(closure!(&'static[u8],
|
||||
/// cond!( b, tag!("abcd") ))
|
||||
/// );
|
||||
///
|
||||
/// let a = b"abcdef";
|
||||
/// assert_eq!(f(&a[..]), Done(&b"ef"[..], Some(&b"abcd"[..])));
|
||||
///
|
||||
/// let b2 = false;
|
||||
/// let f2 = closure!(&'static[u8],
|
||||
/// cond!( b2, tag!("abcd") )
|
||||
/// let f2:Box<Fn(&'static [u8]) -> IResult<&[u8],Option<&[u8]>>> = Box::new(closure!(&'static[u8],
|
||||
/// cond!( b2, tag!("abcd") ))
|
||||
/// );
|
||||
/// assert_eq!(f2(&a[..]), Done(&b"abcdef"[..], None));
|
||||
/// # }
|
||||
@ -1724,13 +1723,14 @@ mod tests {
|
||||
let expected = $inp;
|
||||
let bytes = as_bytes(&expected);
|
||||
|
||||
if bytes.len() > $i.len() {
|
||||
let res : $crate::IResult<&[u8],&[u8]> = if bytes.len() > $i.len() {
|
||||
$crate::IResult::Incomplete($crate::Needed::Size(bytes.len()))
|
||||
} else if &$i[0..bytes.len()] == bytes {
|
||||
$crate::IResult::Done(&$i[bytes.len()..], &$i[0..bytes.len()])
|
||||
} else {
|
||||
$crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Tag, $i))
|
||||
}
|
||||
};
|
||||
res
|
||||
}
|
||||
);
|
||||
);
|
||||
@ -1971,7 +1971,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn alt() {
|
||||
fn work(input: &[u8]) -> IResult<&[u8],&[u8]> {
|
||||
fn work(input: &[u8]) -> IResult<&[u8],&[u8], &'static str> {
|
||||
Done(&b""[..], input)
|
||||
}
|
||||
|
||||
@ -1980,13 +1980,22 @@ mod tests {
|
||||
Error(Code(ErrorKind::Custom("abcd")))
|
||||
}
|
||||
|
||||
fn work2(input: &[u8]) -> IResult<&[u8],&[u8]> {
|
||||
fn work2(input: &[u8]) -> IResult<&[u8],&[u8], &'static str> {
|
||||
Done(input, &b""[..])
|
||||
}
|
||||
|
||||
named!(alt1, alt!(dont_work | dont_work));
|
||||
named!(alt2, alt!(dont_work | work));
|
||||
named!(alt3, alt!(dont_work | dont_work | work2 | dont_work));
|
||||
fn alt1(i:&[u8]) -> IResult<&[u8],&[u8], &'static str> {
|
||||
alt!(i, dont_work | dont_work)
|
||||
}
|
||||
fn alt2(i:&[u8]) -> IResult<&[u8],&[u8], &'static str> {
|
||||
alt!(i, dont_work | work)
|
||||
}
|
||||
fn alt3(i:&[u8]) -> IResult<&[u8],&[u8], &'static str> {
|
||||
alt!(i, dont_work | dont_work | work2 | dont_work)
|
||||
}
|
||||
//named!(alt1, alt!(dont_work | dont_work));
|
||||
//named!(alt2, alt!(dont_work | work));
|
||||
//named!(alt3, alt!(dont_work | dont_work | work2 | dont_work));
|
||||
|
||||
let a = &b"abcd"[..];
|
||||
assert_eq!(alt1(a), Error(Position(ErrorKind::Alt, a)));
|
||||
@ -2058,13 +2067,14 @@ mod tests {
|
||||
#[test]
|
||||
fn cond() {
|
||||
let b = true;
|
||||
let f = closure!(&'static [u8], cond!( b, tag!("abcd") ) );
|
||||
let f: Box<Fn(&'static [u8]) -> IResult<&[u8],Option<&[u8]>, &str>> = Box::new(closure!(&'static [u8], cond!( b, tag!("abcd") ) ));
|
||||
|
||||
let a = b"abcdef";
|
||||
assert_eq!(f(&a[..]), Done(&b"ef"[..], Some(&b"abcd"[..])));
|
||||
|
||||
let b2 = false;
|
||||
let f2 = closure!(&'static [u8], cond!( b2, tag!("abcd") ) );
|
||||
let f2: Box<Fn(&'static [u8]) -> IResult<&[u8],Option<&[u8]>, &str>> = Box::new(closure!(&'static [u8], cond!( b2, tag!("abcd") ) ));
|
||||
//let f2 = closure!(&'static [u8], cond!( b2, tag!("abcd") ) );
|
||||
|
||||
assert_eq!(f2(&a[..]), Done(&b"abcdef"[..], None));
|
||||
}
|
||||
@ -2076,7 +2086,8 @@ mod tests {
|
||||
named!(silly, tag!("foo"));
|
||||
|
||||
let b = true;
|
||||
let f = closure!(&'static [u8], cond!( b, silly ) );
|
||||
//let f = closure!(&'static [u8], cond!( b, silly ) );
|
||||
let f: Box<Fn(&'static [u8]) -> IResult<&[u8],Option<&[u8]>, &str>> = Box::new(closure!(&'static [u8], cond!( b, silly ) ));
|
||||
assert_eq!(f(b"foobar"), Done(&b"bar"[..], Some(&b"foo"[..])));
|
||||
}
|
||||
|
||||
|
@ -6,18 +6,14 @@ use nom::{IResult,digit, multispace};
|
||||
use std::str;
|
||||
use std::str::FromStr;
|
||||
|
||||
//named!(parens<i64>, delimited!(
|
||||
fn parens(input:&[u8]) -> IResult<&[u8], i64, ()> {
|
||||
delimited!(input,
|
||||
named!(parens<i64>, delimited!(
|
||||
delimited!(opt!(multispace), tag!("("), opt!(multispace)),
|
||||
expr,
|
||||
delimited!(opt!(multispace), tag!(")"), opt!(multispace))
|
||||
)
|
||||
}
|
||||
);
|
||||
|
||||
//named!(factor<i64>,
|
||||
fn factor(input:&[u8]) -> IResult<&[u8], i64, ()> {
|
||||
alt!(input,
|
||||
named!(factor<i64>, alt!(
|
||||
map_res!(
|
||||
map_res!(
|
||||
delimited!(opt!(multispace), digit, opt!(multispace)),
|
||||
@ -27,11 +23,9 @@ fn factor(input:&[u8]) -> IResult<&[u8], i64, ()> {
|
||||
)
|
||||
| parens
|
||||
)
|
||||
}
|
||||
);
|
||||
|
||||
//named!(term <i64>,
|
||||
fn term(input:&[u8]) -> IResult<&[u8], i64, ()> {
|
||||
chain!(input,
|
||||
named!(term <i64>, chain!(
|
||||
mut acc: factor ~
|
||||
many0!(
|
||||
alt!(
|
||||
@ -41,11 +35,9 @@ fn term(input:&[u8]) -> IResult<&[u8], i64, ()> {
|
||||
),
|
||||
|| { return acc }
|
||||
)
|
||||
}
|
||||
);
|
||||
|
||||
//named!(expr <i64>,
|
||||
fn expr(input:&[u8]) -> IResult<&[u8], i64, ()> {
|
||||
chain!(input,
|
||||
named!(expr <i64>, chain!(
|
||||
mut acc: term ~
|
||||
many0!(
|
||||
alt!(
|
||||
@ -55,7 +47,7 @@ fn expr(input:&[u8]) -> IResult<&[u8], i64, ()> {
|
||||
),
|
||||
|| { return acc }
|
||||
)
|
||||
}
|
||||
);
|
||||
|
||||
#[test]
|
||||
fn factor_test() {
|
||||
|
@ -270,7 +270,7 @@ fn unknown_box_type(input:&[u8]) -> IResult<&[u8], MP4BoxType> {
|
||||
}
|
||||
|
||||
//named!(box_type<&[u8], MP4BoxType>,
|
||||
fn box_type(input: &[u8]) -> IResult<&[u8], MP4BoxType, ()> {
|
||||
fn box_type(input: &[u8]) -> IResult<&[u8], MP4BoxType, u32> {
|
||||
alt!(input,
|
||||
tag!("ftyp") => { |_| MP4BoxType::Ftyp } |
|
||||
tag!("moov") => { |_| MP4BoxType::Moov } |
|
||||
|
@ -1,4 +1,3 @@
|
||||
|
||||
#[macro_use]
|
||||
extern crate nom;
|
||||
|
||||
@ -18,10 +17,7 @@ struct TestConsumer {
|
||||
}
|
||||
|
||||
named!(om_parser, tag!("om"));
|
||||
//named!(nomnom_parser<&[u8],Vec<&[u8]> >, many1!(tag!("nom")));
|
||||
fn nomnom_parser(input:&[u8]) -> IResult<&[u8], Vec<&[u8]>, ()> {
|
||||
many1!(input, tag!("nom"))
|
||||
}
|
||||
named!(nomnom_parser<&[u8],Vec<&[u8]> >, many1!(tag!("nom")));
|
||||
named!(end_parser, tag!("kthxbye"));
|
||||
|
||||
impl Consumer for TestConsumer {
|
||||
|
Loading…
Reference in New Issue
Block a user