mirror of
https://gitee.com/openharmony/third_party_rust_nom
synced 2024-11-23 07:29:54 +00:00
Replace the core feature by a std feature activated by default
compilation features are additive, so a 'core' that removes some functionality makes no sense. To use nom in no_std environment, the code should nom import nom and deactivate 'std'
This commit is contained in:
parent
f723e30483
commit
d8dde827e6
@ -22,9 +22,9 @@ include = [
|
||||
]
|
||||
|
||||
[features]
|
||||
core = []
|
||||
std = []
|
||||
nightly = []
|
||||
default = ["stream"]
|
||||
default = ["std", "stream"]
|
||||
regexp = ["regex"]
|
||||
regexp_macros = ["regexp", "lazy_static"]
|
||||
stream = []
|
||||
|
@ -57,7 +57,7 @@ extern crate nom;
|
||||
|
||||
There are a few compilation features:
|
||||
|
||||
* `core`: enables `no_std` builds
|
||||
* `std`: if disabled, nom can work in `no_std` builds
|
||||
* `regexp`: enables regular expression parsers with the `regex` crate
|
||||
* `regexp_macros`: enables regular expression parsers with the `regex` and `regex_macros` crates. Regular expressions can be defined at compile time, but it requires a nightly version of rustc
|
||||
* `verbose-errors`: accumulate error codes and input positions as you backtrack through the parser tree. This gives you precise information about which part of the parser was affected by which part of the input
|
||||
|
@ -190,7 +190,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn take_bits() {
|
||||
let input = vec![0b10101010, 0b11110000, 0b00110011];
|
||||
let input = [0b10101010, 0b11110000, 0b00110011];
|
||||
let sl = &input[..];
|
||||
|
||||
assert_eq!(take_bits!( (sl, 0), u8, 0 ), IResult::Done((sl, 0), 0));
|
||||
@ -212,7 +212,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn tag_bits() {
|
||||
let input = vec![0b10101010, 0b11110000, 0b00110011];
|
||||
let input = [0b10101010, 0b11110000, 0b00110011];
|
||||
let sl = &input[..];
|
||||
|
||||
assert_eq!(tag_bits!( (sl, 0), u8, 3, 0b101), IResult::Done((&sl[0..], 3), 5));
|
||||
@ -230,7 +230,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn chain_bits() {
|
||||
let input = vec![0b10101010, 0b11110000, 0b00110011];
|
||||
let input = [0b10101010, 0b11110000, 0b00110011];
|
||||
let sl = &input[..];
|
||||
assert_eq!(ch((&input[..],0)), IResult::Done((&sl[1..], 4), (5,15)));
|
||||
assert_eq!(ch((&input[..],4)), IResult::Done((&sl[2..], 0), (7,16)));
|
||||
@ -240,7 +240,7 @@ mod tests {
|
||||
named!(ch_bytes<(u8,u8)>, bits!(ch));
|
||||
#[test]
|
||||
fn bits_to_bytes() {
|
||||
let input = vec![0b10101010, 0b11110000, 0b00110011];
|
||||
let input = [0b10101010, 0b11110000, 0b00110011];
|
||||
assert_eq!(ch_bytes(&input[..]), IResult::Done(&input[2..], (5,15)));
|
||||
assert_eq!(ch_bytes(&input[..1]), IResult::Incomplete(Needed::Size(2)));
|
||||
assert_eq!(ch_bytes(&input[1..]), IResult::Error(error_position!(ErrorKind::TagBits, &input[1..])));
|
||||
|
@ -909,6 +909,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn take_until_test() {
|
||||
named!(x, take_until_and_consume!("efgh"));
|
||||
let r = x(&b"abcdabcdefghijkl"[..]);
|
||||
@ -1081,6 +1082,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn recognize_take_while() {
|
||||
use nom::is_alphanumeric;
|
||||
named!(x, take_while!(is_alphanumeric));
|
||||
@ -1129,7 +1131,7 @@ mod tests {
|
||||
fn tag_fixed_size_array() {
|
||||
named!(test, tag!([0x42]));
|
||||
named!(test2, tag!(&[0x42]));
|
||||
let input = vec!(0x42, 0x00);
|
||||
let input = [0x42, 0x00];
|
||||
assert_eq!(test(&input), Done(&b"\x00"[..], &b"\x42"[..]));
|
||||
assert_eq!(test2(&input), Done(&b"\x00"[..], &b"\x42"[..]));
|
||||
}
|
||||
|
@ -3,7 +3,7 @@
|
||||
use self::IResult::*;
|
||||
use self::Needed::*;
|
||||
|
||||
#[cfg(feature = "core")]
|
||||
#[cfg(not(feature = "std"))]
|
||||
use std::prelude::v1::*;
|
||||
|
||||
#[cfg(feature = "verbose-errors")]
|
||||
@ -370,6 +370,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn iresult_map_err() {
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
struct Error(u32);
|
||||
|
16
src/lib.rs
16
src/lib.rs
@ -86,14 +86,14 @@
|
||||
//! assert_eq!(expr(b"2*2/(5-1)+3"), IResult::Done(&b""[..], 4));
|
||||
//! }
|
||||
//! ```
|
||||
#![cfg_attr(feature = "core", feature(no_std))]
|
||||
#![cfg_attr(feature = "core", feature(collections))]
|
||||
#![cfg_attr(feature = "core", no_std)]
|
||||
#![cfg_attr(not(feature = "std"), feature(no_std))]
|
||||
#![cfg_attr(not(feature = "std"), feature(collections))]
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![cfg_attr(feature = "nightly", feature(test))]
|
||||
#![cfg_attr(feature = "nightly", feature(const_fn))]
|
||||
//#![warn(missing_docs)]
|
||||
|
||||
#[cfg(feature = "core")]
|
||||
#[cfg(not(feature = "std"))]
|
||||
extern crate collections;
|
||||
#[cfg(feature = "regexp")]
|
||||
extern crate regex;
|
||||
@ -102,10 +102,10 @@ extern crate regex;
|
||||
#[cfg(feature = "nightly")]
|
||||
extern crate test;
|
||||
|
||||
#[cfg(feature = "core")]
|
||||
#[cfg(not(feature = "std"))]
|
||||
mod std {
|
||||
#[macro_use]
|
||||
pub use core::{fmt, iter, option, ops, slice, str, mem};
|
||||
pub use core::{fmt, cmp, iter, option, result, ops, slice, str, mem};
|
||||
pub use collections::{boxed, vec, string};
|
||||
pub mod prelude {
|
||||
pub use core::prelude as v1;
|
||||
@ -138,7 +138,7 @@ pub use self::whitespace::*;
|
||||
#[cfg(feature = "regexp")]
|
||||
pub use self::regexp::*;
|
||||
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(feature = "stream")]
|
||||
pub use self::stream::*;
|
||||
|
||||
@ -170,7 +170,7 @@ pub mod whitespace;
|
||||
#[macro_use] mod regexp;
|
||||
|
||||
#[macro_use]
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(feature = "stream")]
|
||||
mod stream;
|
||||
|
||||
|
@ -1268,6 +1268,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn cond() {
|
||||
let f_true: Box<Fn(&'static [u8]) -> IResult<&[u8],Option<&[u8]>, &str>> = Box::new(closure!(&'static [u8], cond!( true, tag!("abcd") ) ));
|
||||
let f_false: Box<Fn(&'static [u8]) -> IResult<&[u8],Option<&[u8]>, &str>> = Box::new(closure!(&'static [u8], cond!( false, tag!("abcd") ) ));
|
||||
@ -1283,6 +1284,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn cond_wrapping() {
|
||||
// Test that cond!() will wrap a given identifier in the call!() macro.
|
||||
named!( tag_abcd, tag!("abcd") );
|
||||
|
25
src/multi.rs
25
src/multi.rs
@ -309,7 +309,9 @@ macro_rules! many1(
|
||||
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i),
|
||||
$crate::IResult::Done(i1,o1) => {
|
||||
if i1.input_len() == 0 {
|
||||
$crate::IResult::Done(i1,vec![o1])
|
||||
let mut res = ::std::vec::Vec::new();
|
||||
res.push(o1);
|
||||
$crate::IResult::Done(i1,res)
|
||||
} else {
|
||||
|
||||
let mut res = ::std::vec::Vec::with_capacity(4);
|
||||
@ -1148,6 +1150,7 @@ mod tests {
|
||||
);
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn separated_list() {
|
||||
named!(multi<&[u8],Vec<&[u8]> >, separated_list!(tag!(","), tag!("abcd")));
|
||||
named!(multi_empty<&[u8],Vec<&[u8]> >, separated_list!(tag!(","), tag!("")));
|
||||
@ -1179,6 +1182,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn separated_nonempty_list() {
|
||||
named!(multi<&[u8],Vec<&[u8]> >, separated_nonempty_list!(tag!(","), tag!("abcd")));
|
||||
named!(multi_longsep<&[u8],Vec<&[u8]> >, separated_list!(tag!(".."), tag!("abcd")));
|
||||
@ -1206,6 +1210,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn many0() {
|
||||
named!( tag_abcd, tag!("abcd") );
|
||||
named!( tag_empty, tag!("") );
|
||||
@ -1234,6 +1239,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn many1() {
|
||||
named!(multi<&[u8],Vec<&[u8]> >, many1!(tag!("abcd")));
|
||||
|
||||
@ -1251,6 +1257,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn many_till() {
|
||||
named!(multi<&[u8], (Vec<&[u8]>, &[u8]) >, many_till!( tag!( "abcd" ), tag!( "efgh" ) ) );
|
||||
|
||||
@ -1266,6 +1273,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn infinite_many() {
|
||||
fn tst(input: &[u8]) -> IResult<&[u8], &[u8]> {
|
||||
println!("input: {:?}", input);
|
||||
@ -1283,6 +1291,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn many_m_n() {
|
||||
named!(multi<&[u8],Vec<&[u8]> >, many_m_n!(2, 4, tag!("Abcd")));
|
||||
|
||||
@ -1303,6 +1312,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn count() {
|
||||
const TIMES: usize = 2;
|
||||
named!( tag_abc, tag!("abc") );
|
||||
@ -1317,6 +1327,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn count_zero() {
|
||||
const TIMES: usize = 0;
|
||||
named!( tag_abc, tag!("abc") );
|
||||
@ -1406,6 +1417,7 @@ mod tests {
|
||||
));
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn length_count() {
|
||||
named!(tag_abc, tag!(&b"abc"[..]) );
|
||||
named!( cnt<&[u8], Vec<&[u8]> >, length_count!(number, tag_abc) );
|
||||
@ -1432,24 +1444,25 @@ mod tests {
|
||||
named!(length_value_1<&[u8], u16 >, length_value!(be_u8, be_u16));
|
||||
named!(length_value_2<&[u8], (u8, u8) >, length_value!(be_u8, tuple!(be_u8, be_u8)));
|
||||
|
||||
let i1 = vec![0, 5, 6];
|
||||
let i1 = [0, 5, 6];
|
||||
assert_eq!(length_value_1(&i1), IResult::Error(error_position!(ErrorKind::Complete, &b""[..])));
|
||||
assert_eq!(length_value_2(&i1), IResult::Error(error_position!(ErrorKind::Complete, &b""[..])));
|
||||
|
||||
let i2 = vec![1, 5, 6, 3];
|
||||
let i2 = [1, 5, 6, 3];
|
||||
assert_eq!(length_value_1(&i2), IResult::Error(error_position!(ErrorKind::Complete, &i2[1..])));
|
||||
assert_eq!(length_value_2(&i2), IResult::Error(error_position!(ErrorKind::Complete, &i2[1..])));
|
||||
|
||||
let i3 = vec![2, 5, 6, 3, 4, 5, 7];
|
||||
let i3 = [2, 5, 6, 3, 4, 5, 7];
|
||||
assert_eq!(length_value_1(&i3), IResult::Done(&i3[3..], 1286));
|
||||
assert_eq!(length_value_2(&i3), IResult::Done(&i3[3..], (5, 6)));
|
||||
|
||||
let i4 = vec![3, 5, 6, 3, 4, 5];
|
||||
let i4 = [3, 5, 6, 3, 4, 5];
|
||||
assert_eq!(length_value_1(&i4), IResult::Done(&i4[4..], 1286));
|
||||
assert_eq!(length_value_2(&i4), IResult::Done(&i4[4..], (5, 6)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn fold_many0() {
|
||||
fn fold_into_vec<T>(mut acc: Vec<T>, item: T) -> Vec<T> {
|
||||
acc.push(item);
|
||||
@ -1470,6 +1483,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn fold_many1() {
|
||||
fn fold_into_vec<T>(mut acc: Vec<T>, item: T) -> Vec<T> {
|
||||
acc.push(item);
|
||||
@ -1491,6 +1505,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn fold_many_m_n() {
|
||||
fn fold_into_vec<T>(mut acc: Vec<T>, item: T) -> Vec<T> {
|
||||
acc.push(item);
|
||||
|
15
src/nom.rs
15
src/nom.rs
@ -6,7 +6,7 @@
|
||||
//! but the macros system makes no promises.
|
||||
//!
|
||||
|
||||
#[cfg(feature = "core")]
|
||||
#[cfg(not(feature = "std"))]
|
||||
use std::prelude::v1::*;
|
||||
use std::boxed::Box;
|
||||
|
||||
@ -30,7 +30,7 @@ pub fn tag_cl<'a,'b>(rec:&'a[u8]) -> Box<Fn(&'b[u8]) -> IResult<&'b[u8], &'b[u8
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
#[inline]
|
||||
pub fn print<T: Debug>(input: T) -> IResult<T, ()> {
|
||||
println!("{:?}", input);
|
||||
@ -630,7 +630,7 @@ pub fn rest_s(input: &str) -> IResult<&str, &str> {
|
||||
}
|
||||
|
||||
/// Recognizes floating point number in a byte string and returs a f32
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
pub fn float(input: &[u8]) -> IResult<&[u8],f32> {
|
||||
flat_map!(input,
|
||||
recognize!(
|
||||
@ -653,7 +653,7 @@ pub fn float(input: &[u8]) -> IResult<&[u8],f32> {
|
||||
}
|
||||
|
||||
/// Recognizes floating point number in a string and returs a f32
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
pub fn float_s(input: &str) -> IResult<&str,f32> {
|
||||
flat_map!(input,
|
||||
recognize!(
|
||||
@ -676,7 +676,7 @@ pub fn float_s(input: &str) -> IResult<&str,f32> {
|
||||
}
|
||||
|
||||
/// Recognizes floating point number in a byte string and returs a f64
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
pub fn double(input: &[u8]) -> IResult<&[u8],f64> {
|
||||
flat_map!(input,
|
||||
recognize!(
|
||||
@ -699,7 +699,7 @@ pub fn double(input: &[u8]) -> IResult<&[u8],f64> {
|
||||
}
|
||||
|
||||
/// Recognizes floating point number in a string and returs a f64
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
pub fn double_s(input: &str) -> IResult<&str,f64> {
|
||||
flat_map!(input,
|
||||
recognize!(
|
||||
@ -884,6 +884,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn buffer_with_size() {
|
||||
let i:Vec<u8> = vec![7,8];
|
||||
let o:Vec<u8> = vec![4,5,6];
|
||||
@ -1058,6 +1059,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn manual_configurable_endianness_test() {
|
||||
let x = 1;
|
||||
let int_parse: Box<Fn(&[u8]) -> IResult<&[u8], u16> > = if x == 2 {
|
||||
@ -1178,6 +1180,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn float_test() {
|
||||
assert_eq!(float(&b"+3.14"[..]), Done(&b""[..], 3.14));
|
||||
assert_eq!(float_s(&"3.14"[..]), Done(&""[..], 3.14));
|
||||
|
@ -1327,10 +1327,10 @@ mod tests {
|
||||
)
|
||||
);
|
||||
|
||||
let a: Vec<u8> = vec!(2, 3, 4, 5);
|
||||
let res_a: Vec<u8> = vec!(3, 4);
|
||||
let a = [2u8, 3, 4, 5];
|
||||
let res_a = [3u8, 4];
|
||||
assert_eq!(length_value(&a[..]), Done(&a[3..], &res_a[..]));
|
||||
let b: Vec<u8> = vec!(5, 3, 4, 5);
|
||||
let b = [5u8, 3, 4, 5];
|
||||
assert_eq!(length_value(&b[..]), Incomplete(Needed::Size(6)));
|
||||
}
|
||||
}
|
||||
|
@ -64,18 +64,18 @@ impl<I,O,E> IResult<I,O,E> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
use std::any::Any;
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
use std::{error,fmt};
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
impl<E: fmt::Debug+Any> error::Error for Err<E> {
|
||||
fn description(&self) -> &str {
|
||||
self.description()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
impl<E: fmt::Debug> fmt::Display for Err<E> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.description())
|
||||
|
@ -622,6 +622,7 @@ mod test {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn recognize_is_a_s() {
|
||||
let a = "aabbab";
|
||||
let b = "ababcd";
|
||||
|
27
src/util.rs
27
src/util.rs
@ -5,23 +5,22 @@ use internal::IResult;
|
||||
#[cfg(feature = "verbose-errors")]
|
||||
use verbose_errors::Err;
|
||||
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[cfg(feature = "core")]
|
||||
#[cfg(not(feature = "std"))]
|
||||
use std::prelude::v1::*;
|
||||
|
||||
use std::vec::Vec;
|
||||
use std::string::ToString;
|
||||
|
||||
/// useful functions to calculate the offset between slices and show a hexdump of a slice
|
||||
#[cfg(not(feature = "core"))]
|
||||
pub trait Offset {
|
||||
/// offset between the first byte of self and the first byte of the argument
|
||||
fn offset(&self, second:&Self) -> usize;
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
pub trait HexDisplay {
|
||||
/// Converts the value of `self` to a hex dump, returning the owned
|
||||
/// string.
|
||||
@ -34,7 +33,6 @@ pub trait HexDisplay {
|
||||
|
||||
static CHARS: &'static[u8] = b"0123456789abcdef";
|
||||
|
||||
#[cfg(not(feature = "core"))]
|
||||
impl Offset for [u8] {
|
||||
fn offset(&self, second:&[u8]) -> usize {
|
||||
let fst = self.as_ptr();
|
||||
@ -44,7 +42,6 @@ impl Offset for [u8] {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "core"))]
|
||||
impl Offset for str {
|
||||
fn offset(&self, second: &Self) -> usize {
|
||||
let fst = self.as_ptr();
|
||||
@ -54,7 +51,7 @@ impl Offset for str {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
impl HexDisplay for [u8] {
|
||||
#[allow(unused_variables)]
|
||||
fn to_hex(&self, chunk_size: usize) -> String {
|
||||
@ -150,7 +147,7 @@ macro_rules! dbg (
|
||||
///
|
||||
/// It also displays the input in hexdump format
|
||||
///
|
||||
/// ```
|
||||
/// ```ignore
|
||||
/// # #[macro_use] extern crate nom;
|
||||
/// # fn main() {
|
||||
/// named!(f, dbg_dmp!( tag!( "abcd" ) ) );
|
||||
@ -211,11 +208,11 @@ pub fn compare_error_paths<P,E:Clone+PartialEq>(e1:&Err<P,E>, e2:&Err<P,E>) -> b
|
||||
}
|
||||
|
||||
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(feature = "verbose-errors")]
|
||||
use std::hash::Hash;
|
||||
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(feature = "verbose-errors")]
|
||||
pub fn add_error_pattern<'a,I,O,E: Clone+Hash+Eq>(h: &mut HashMap<Vec<ErrorKind<E>>, &'a str>, res: IResult<I,O,E>, message: &'a str) -> bool {
|
||||
if let IResult::Error(e) = res {
|
||||
@ -233,7 +230,7 @@ pub fn slice_to_offsets(input: &[u8], s: &[u8]) -> (usize, usize) {
|
||||
(off1, off2)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(feature = "verbose-errors")]
|
||||
pub fn prepare_errors<O,E: Clone>(input: &[u8], res: IResult<&[u8],O,E>) -> Option<Vec<(ErrorKind<E>, usize, usize)> > {
|
||||
if let IResult::Error(e) = res {
|
||||
@ -267,7 +264,7 @@ pub fn prepare_errors<O,E: Clone>(input: &[u8], res: IResult<&[u8],O,E>) -> Opti
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(feature = "verbose-errors")]
|
||||
pub fn print_error<O,E:Clone>(input: &[u8], res: IResult<&[u8],O,E>) {
|
||||
if let Some(v) = prepare_errors(input, res) {
|
||||
@ -280,7 +277,7 @@ pub fn print_error<O,E:Clone>(input: &[u8], res: IResult<&[u8],O,E>) {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(feature = "verbose-errors")]
|
||||
pub fn generate_colors<E>(v: &[(ErrorKind<E>, usize, usize)]) -> HashMap<u32, u8> {
|
||||
let mut h: HashMap<u32, u8> = HashMap::new();
|
||||
@ -333,7 +330,7 @@ pub fn write_color(v: &mut Vec<u8>, color: u8) {
|
||||
v.push('m' as u8);
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
pub fn print_codes(colors: HashMap<u32, u8>, names: HashMap<u32, &str>) -> String {
|
||||
let mut v = Vec::new();
|
||||
for (code, &color) in &colors {
|
||||
@ -355,7 +352,7 @@ pub fn print_codes(colors: HashMap<u32, u8>, names: HashMap<u32, &str>) -> Strin
|
||||
String::from_utf8_lossy(&v[..]).into_owned()
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(feature = "verbose-errors")]
|
||||
pub fn print_offsets<E>(input: &[u8], from: usize, offsets: &[(ErrorKind<E>, usize, usize)]) -> String {
|
||||
let mut v = Vec::with_capacity(input.len() * 3);
|
||||
|
@ -83,13 +83,13 @@ impl<I,O,E> IResult<I,O,E> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
use std::any::Any;
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
use std::{error,fmt};
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
use std::fmt::Debug;
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
impl<P:Debug+Any,E:Debug+Any> error::Error for Err<P,E> {
|
||||
fn description(&self) -> &str {
|
||||
let kind = match *self {
|
||||
@ -99,7 +99,7 @@ impl<P:Debug+Any,E:Debug+Any> error::Error for Err<P,E> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "core"))]
|
||||
#[cfg(feature = "std")]
|
||||
impl<P:fmt::Debug,E:fmt::Debug> fmt::Display for Err<P,E> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
|
@ -2,8 +2,7 @@
|
||||
#[macro_use]
|
||||
extern crate nom;
|
||||
|
||||
use nom::{IResult,Needed,HexDisplay,space,digit,be_u16};
|
||||
use std::str;
|
||||
use nom::{IResult,Needed,space,digit,be_u16};
|
||||
|
||||
#[allow(dead_code)]
|
||||
struct Range {
|
||||
@ -64,36 +63,43 @@ fn issue_58() {
|
||||
|
||||
//trace_macros!(false);
|
||||
|
||||
named!(parse_ints< Vec<i32> >, many0!(spaces_or_int));
|
||||
#[cfg(feature = "std")]
|
||||
mod parse_int {
|
||||
use nom::HexDisplay;
|
||||
use nom::{IResult,Needed,space,digit,be_u16};
|
||||
use std::str;
|
||||
|
||||
fn spaces_or_int(input: &[u8]) -> IResult<&[u8], i32>{
|
||||
println!("{}", input.to_hex(8));
|
||||
do_parse!(input,
|
||||
opt!(complete!(space)) >>
|
||||
res: map!(complete!(digit),
|
||||
|x| {
|
||||
println!("x: {:?}", x);
|
||||
let result = str::from_utf8(x).unwrap();
|
||||
println!("Result: {}", result);
|
||||
println!("int is empty?: {}", x.is_empty());
|
||||
match result.parse(){
|
||||
Ok(i) => i,
|
||||
Err(_) => panic!("UH OH! NOT A DIGIT!")
|
||||
}
|
||||
}) >>
|
||||
(res)
|
||||
)
|
||||
}
|
||||
named!(parse_ints< Vec<i32> >, many0!(spaces_or_int));
|
||||
|
||||
#[test]
|
||||
fn issue_142(){
|
||||
let subject = parse_ints(&b"12 34 5689"[..]);
|
||||
let expected = IResult::Done(&b""[..], vec![12, 34, 5689]);
|
||||
assert_eq!(subject, expected);
|
||||
fn spaces_or_int(input: &[u8]) -> IResult<&[u8], i32>{
|
||||
println!("{}", input.to_hex(8));
|
||||
do_parse!(input,
|
||||
opt!(complete!(space)) >>
|
||||
res: map!(complete!(digit),
|
||||
|x| {
|
||||
println!("x: {:?}", x);
|
||||
let result = str::from_utf8(x).unwrap();
|
||||
println!("Result: {}", result);
|
||||
println!("int is empty?: {}", x.is_empty());
|
||||
match result.parse(){
|
||||
Ok(i) => i,
|
||||
Err(_) => panic!("UH OH! NOT A DIGIT!")
|
||||
}
|
||||
}) >>
|
||||
(res)
|
||||
)
|
||||
}
|
||||
|
||||
let subject = parse_ints(&b"12 34 5689 "[..]);
|
||||
let expected = IResult::Done(&b" "[..], vec![12, 34, 5689]);
|
||||
assert_eq!(subject, expected)
|
||||
#[test]
|
||||
fn issue_142(){
|
||||
let subject = parse_ints(&b"12 34 5689"[..]);
|
||||
let expected = IResult::Done(&b""[..], vec![12, 34, 5689]);
|
||||
assert_eq!(subject, expected);
|
||||
|
||||
let subject = parse_ints(&b"12 34 5689 "[..]);
|
||||
let expected = IResult::Done(&b" "[..], vec![12, 34, 5689]);
|
||||
assert_eq!(subject, expected)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -60,6 +60,7 @@ fn overflow_incomplete_many0() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn overflow_incomplete_many1() {
|
||||
named!(multi<&[u8], Vec<&[u8]> >, many1!( length_bytes!(be_u64) ) );
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user