2015-12-31 12:23:09 +00:00
|
|
|
//#![feature(trace_macros)]
|
2017-06-16 12:34:49 +00:00
|
|
|
#![allow(dead_code)]
|
2017-12-10 13:03:25 +00:00
|
|
|
#![cfg_attr(feature = "cargo-clippy", allow(redundant_closure))]
|
2017-06-16 12:34:49 +00:00
|
|
|
|
2021-08-04 16:21:46 +00:00
|
|
|
use nom::{error::ErrorKind, Err, IResult, Needed};
|
2015-08-04 07:07:55 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
struct Range {
|
|
|
|
start: char,
|
2018-02-25 12:12:11 +00:00
|
|
|
end: char,
|
2015-08-04 07:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn take_char(input: &[u8]) -> IResult<&[u8], char> {
|
2017-12-10 13:03:25 +00:00
|
|
|
if !input.is_empty() {
|
2017-09-07 12:28:33 +00:00
|
|
|
Ok((&input[1..], input[0] as char))
|
2015-08-04 07:07:55 +00:00
|
|
|
} else {
|
2020-10-11 13:56:30 +00:00
|
|
|
Err(Err::Incomplete(Needed::new(1)))
|
2015-08-04 07:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:00:21 +00:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
mod parse_int {
|
|
|
|
use nom::HexDisplay;
|
2020-04-10 09:01:58 +00:00
|
|
|
use nom::{
|
|
|
|
character::streaming::{digit1 as digit, space1 as space},
|
2021-08-04 17:10:13 +00:00
|
|
|
combinator::{complete, map, opt},
|
|
|
|
multi::many0,
|
2020-04-10 09:01:58 +00:00
|
|
|
IResult,
|
|
|
|
};
|
2017-05-09 11:00:21 +00:00
|
|
|
use std::str;
|
|
|
|
|
2021-08-04 17:10:13 +00:00
|
|
|
fn parse_ints(input: &[u8]) -> IResult<&[u8], Vec<i32>> {
|
|
|
|
many0(spaces_or_int)(input)
|
|
|
|
}
|
2017-05-09 11:00:21 +00:00
|
|
|
|
2017-12-10 17:39:11 +00:00
|
|
|
fn spaces_or_int(input: &[u8]) -> IResult<&[u8], i32> {
|
2017-05-09 11:00:21 +00:00
|
|
|
println!("{}", input.to_hex(8));
|
2021-08-04 17:10:13 +00:00
|
|
|
let (i, _) = opt(complete(space))(input)?;
|
|
|
|
let (i, 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(e) => panic!("UH OH! NOT A DIGIT! {:?}", e),
|
|
|
|
}
|
|
|
|
})(i)?;
|
|
|
|
|
|
|
|
Ok((i, res))
|
2017-05-09 11:00:21 +00:00
|
|
|
}
|
2015-12-30 15:24:10 +00:00
|
|
|
|
2017-05-09 11:00:21 +00:00
|
|
|
#[test]
|
2017-12-10 17:39:11 +00:00
|
|
|
fn issue_142() {
|
2018-01-11 18:29:46 +00:00
|
|
|
let subject = parse_ints(&b"12 34 5689a"[..]);
|
|
|
|
let expected = Ok((&b"a"[..], vec![12, 34, 5689]));
|
2017-12-10 17:39:11 +00:00
|
|
|
assert_eq!(subject, expected);
|
|
|
|
|
|
|
|
let subject = parse_ints(&b"12 34 5689 "[..]);
|
|
|
|
let expected = Ok((&b" "[..], vec![12, 34, 5689]));
|
|
|
|
assert_eq!(subject, expected)
|
2017-05-09 11:00:21 +00:00
|
|
|
}
|
2015-12-30 15:24:10 +00:00
|
|
|
}
|
2015-12-31 12:23:09 +00:00
|
|
|
|
2016-03-28 08:07:03 +00:00
|
|
|
#[test]
|
2017-12-10 17:39:11 +00:00
|
|
|
fn usize_length_bytes_issue() {
|
2021-08-04 17:10:13 +00:00
|
|
|
use nom::multi::length_data;
|
2019-04-15 14:53:57 +00:00
|
|
|
use nom::number::streaming::be_u16;
|
2021-08-04 17:10:13 +00:00
|
|
|
let _: IResult<&[u8], &[u8], (&[u8], ErrorKind)> = length_data(be_u16)(b"012346");
|
2016-03-28 08:07:03 +00:00
|
|
|
}
|
|
|
|
|
2016-04-11 21:59:50 +00:00
|
|
|
#[test]
|
|
|
|
fn take_till_issue() {
|
2021-08-04 17:10:13 +00:00
|
|
|
use nom::bytes::streaming::take_till;
|
|
|
|
|
|
|
|
fn nothing(i: &[u8]) -> IResult<&[u8], &[u8]> {
|
|
|
|
take_till(|_| true)(i)
|
|
|
|
}
|
2016-04-11 21:59:50 +00:00
|
|
|
|
2020-10-11 13:56:30 +00:00
|
|
|
assert_eq!(nothing(b""), Err(Err::Incomplete(Needed::new(1))));
|
2017-12-10 17:39:11 +00:00
|
|
|
assert_eq!(nothing(b"abc"), Ok((&b"abc"[..], &b""[..])));
|
2016-04-11 21:59:50 +00:00
|
|
|
}
|
2017-05-09 14:10:53 +00:00
|
|
|
|
2018-01-15 11:07:51 +00:00
|
|
|
#[test]
|
|
|
|
fn issue_655() {
|
2019-04-15 14:53:57 +00:00
|
|
|
use nom::character::streaming::{line_ending, not_line_ending};
|
2021-08-04 17:10:13 +00:00
|
|
|
fn twolines(i: &str) -> IResult<&str, (&str, &str)> {
|
|
|
|
let (i, l1) = not_line_ending(i)?;
|
|
|
|
let (i, _) = line_ending(i)?;
|
|
|
|
let (i, l2) = not_line_ending(i)?;
|
|
|
|
let (i, _) = line_ending(i)?;
|
|
|
|
|
|
|
|
Ok((i, (l1, l2)))
|
|
|
|
}
|
2018-01-15 11:07:51 +00:00
|
|
|
|
|
|
|
assert_eq!(twolines("foo\nbar\n"), Ok(("", ("foo", "bar"))));
|
|
|
|
assert_eq!(twolines("féo\nbar\n"), Ok(("", ("féo", "bar"))));
|
|
|
|
assert_eq!(twolines("foé\nbar\n"), Ok(("", ("foé", "bar"))));
|
|
|
|
assert_eq!(twolines("foé\r\nbar\n"), Ok(("", ("foé", "bar"))));
|
|
|
|
}
|
2018-01-24 15:36:09 +00:00
|
|
|
|
2018-04-03 22:26:46 +00:00
|
|
|
#[cfg(feature = "alloc")]
|
2021-08-04 17:10:13 +00:00
|
|
|
fn issue_717(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
|
|
|
|
use nom::bytes::complete::{is_not, tag};
|
|
|
|
use nom::multi::separated_list0;
|
|
|
|
|
|
|
|
separated_list0(tag([0x0]), is_not([0x0u8]))(i)
|
|
|
|
}
|
2018-03-26 09:21:59 +00:00
|
|
|
|
2018-08-18 16:04:45 +00:00
|
|
|
mod issue_647 {
|
2021-08-04 17:10:13 +00:00
|
|
|
use nom::bytes::streaming::tag;
|
|
|
|
use nom::combinator::complete;
|
|
|
|
use nom::multi::separated_list0;
|
|
|
|
use nom::{error::Error, number::streaming::be_f64, Err, IResult};
|
2018-08-18 16:04:45 +00:00
|
|
|
pub type Input<'a> = &'a [u8];
|
|
|
|
|
|
|
|
#[derive(PartialEq, Debug, Clone)]
|
|
|
|
struct Data {
|
2020-04-10 09:01:58 +00:00
|
|
|
c: f64,
|
|
|
|
v: Vec<f64>,
|
2018-08-18 16:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 09:01:58 +00:00
|
|
|
fn list<'a, 'b>(
|
|
|
|
input: Input<'a>,
|
|
|
|
_cs: &'b f64,
|
2020-10-11 13:56:30 +00:00
|
|
|
) -> Result<(Input<'a>, Vec<f64>), Err<Error<&'a [u8]>>> {
|
2021-08-04 17:10:13 +00:00
|
|
|
separated_list0(complete(tag(",")), complete(be_f64))(input)
|
2018-08-18 16:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 17:10:13 +00:00
|
|
|
fn data(input: Input<'_>) -> IResult<Input<'_>, Data> {
|
|
|
|
let (i, c) = be_f64(input)?;
|
|
|
|
let (i, _) = tag("\n")(i)?;
|
|
|
|
let (i, v) = list(i, &c)?;
|
|
|
|
Ok((i, Data { c, v }))
|
|
|
|
}
|
2018-08-18 16:04:45 +00:00
|
|
|
}
|
|
|
|
|
2018-12-26 18:22:18 +00:00
|
|
|
#[test]
|
|
|
|
fn issue_848_overflow_incomplete_bits_to_bytes() {
|
2021-08-04 17:10:13 +00:00
|
|
|
fn take(i: &[u8]) -> IResult<&[u8], &[u8]> {
|
|
|
|
use nom::bytes::streaming::take;
|
|
|
|
take(0x2000000000000000_usize)(i)
|
|
|
|
}
|
|
|
|
fn parser(i: &[u8]) -> IResult<&[u8], &[u8]> {
|
|
|
|
use nom::bits::{bits, bytes};
|
|
|
|
|
|
|
|
bits(bytes(take))(i)
|
|
|
|
}
|
2020-04-10 09:01:58 +00:00
|
|
|
assert_eq!(
|
|
|
|
parser(&b""[..]),
|
2021-08-05 16:33:49 +00:00
|
|
|
Err(Err::Failure(nom::error_position!(
|
|
|
|
&b""[..],
|
|
|
|
ErrorKind::TooLarge
|
|
|
|
)))
|
2020-04-10 09:01:58 +00:00
|
|
|
);
|
2018-12-26 18:22:18 +00:00
|
|
|
}
|
2019-05-05 08:48:28 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_942() {
|
2020-10-24 13:19:14 +00:00
|
|
|
use nom::error::{ContextError, ParseError};
|
|
|
|
pub fn parser<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
|
|
|
|
i: &'a str,
|
|
|
|
) -> IResult<&'a str, usize, E> {
|
2019-05-05 08:48:28 +00:00
|
|
|
use nom::{character::complete::char, error::context, multi::many0_count};
|
|
|
|
many0_count(context("char_a", char('a')))(i)
|
|
|
|
}
|
|
|
|
assert_eq!(parser::<()>("aaa"), Ok(("", 3)));
|
|
|
|
}
|
2019-07-16 13:48:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_many_m_n_with_zeros() {
|
2020-04-10 09:01:58 +00:00
|
|
|
use nom::character::complete::char;
|
|
|
|
use nom::multi::many_m_n;
|
2020-10-11 13:56:30 +00:00
|
|
|
let mut parser = many_m_n::<_, _, (), _>(0, 0, char('a'));
|
2021-10-18 21:36:58 +00:00
|
|
|
assert_eq!(parser("aaa"), Ok(("aaa", vec![])));
|
2019-07-16 13:48:45 +00:00
|
|
|
}
|
2019-09-13 20:46:29 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_1027_convert_error_panic_nonempty() {
|
|
|
|
use nom::character::complete::char;
|
2020-04-10 09:01:58 +00:00
|
|
|
use nom::error::{convert_error, VerboseError};
|
|
|
|
use nom::sequence::pair;
|
2019-09-13 20:46:29 +00:00
|
|
|
|
|
|
|
let input = "a";
|
|
|
|
|
|
|
|
let result: IResult<_, _, VerboseError<&str>> = pair(char('a'), char('b'))(input);
|
|
|
|
let err = match result.unwrap_err() {
|
|
|
|
Err::Error(e) => e,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
2020-10-11 13:56:30 +00:00
|
|
|
let msg = convert_error(input, err);
|
2020-04-10 09:01:58 +00:00
|
|
|
assert_eq!(
|
|
|
|
msg,
|
|
|
|
"0: at line 1:\na\n ^\nexpected \'b\', got end of input\n\n"
|
|
|
|
);
|
2020-01-07 18:11:19 +00:00
|
|
|
}
|
2020-10-25 15:52:40 +00:00
|
|
|
|
2020-11-12 07:55:59 +00:00
|
|
|
#[test]
|
|
|
|
fn issue_1231_bits_expect_fn_closure() {
|
2020-11-24 10:29:17 +00:00
|
|
|
use nom::bits::{bits, complete::take};
|
|
|
|
use nom::error::Error;
|
|
|
|
use nom::sequence::tuple;
|
|
|
|
pub fn example(input: &[u8]) -> IResult<&[u8], (u8, u8)> {
|
|
|
|
bits::<_, _, Error<_>, _, _>(tuple((take(1usize), take(1usize))))(input)
|
|
|
|
}
|
|
|
|
assert_eq!(example(&[0xff]), Ok((&b""[..], (1, 1))));
|
2020-11-12 07:55:59 +00:00
|
|
|
}
|