| 1 |
//#![feature(trace_macros)] |
| 2 |
#![allow(dead_code)] |
| 3 |
#![allow(clippy::redundant_closure)] |
| 4 |
|
| 5 |
use nom::{error::ErrorKind, Err, IResult, Needed, Parser}; |
| 6 |
|
| 7 |
#[allow(dead_code)] |
| 8 |
struct Range { |
| 9 |
start: char, |
| 10 |
end: char, |
| 11 |
} |
| 12 |
|
| 13 |
pub fn take_char(input: &[u8]) -> IResult<&[u8], char> { |
| 14 |
if !input.is_empty() { |
| 15 |
Ok((&input[1..], input[0] as char)) |
| 16 |
} else { |
| 17 |
Err(Err::Incomplete(Needed::new(1))) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
#[cfg(feature = "std")] |
| 22 |
mod parse_int { |
| 23 |
use nom::{ |
| 24 |
character::streaming::{digit1 as digit, space1 as space}, |
| 25 |
combinator::{complete, map, opt}, |
| 26 |
multi::many, |
| 27 |
IResult, |
| 28 |
}; |
| 29 |
use nom::{HexDisplay, Parser}; |
| 30 |
use std::str; |
| 31 |
|
| 32 |
fn parse_ints(input: &[u8]) -> IResult<&[u8], Vec<i32>> { |
| 33 |
many(0.., spaces_or_int).parse(input) |
| 34 |
} |
| 35 |
|
| 36 |
fn spaces_or_int(input: &[u8]) -> IResult<&[u8], i32> { |
| 37 |
println!("{}", input.to_hex(8)); |
| 38 |
let (i, _) = opt(complete(space)).parse(input)?; |
| 39 |
let (i, res) = map(complete(digit), |x| { |
| 40 |
println!("x: {:?}", x); |
| 41 |
let result = str::from_utf8(x).unwrap(); |
| 42 |
println!("Result: {}", result); |
| 43 |
println!("int is empty?: {}", x.is_empty()); |
| 44 |
match result.parse() { |
| 45 |
Ok(i) => i, |
| 46 |
Err(e) => panic!("UH OH! NOT A DIGIT! {:?}", e), |
| 47 |
} |
| 48 |
}) |
| 49 |
.parse(i)?; |
| 50 |
|
| 51 |
Ok((i, res)) |
| 52 |
} |
| 53 |
|
| 54 |
#[test] |
| 55 |
fn issue_142() { |
| 56 |
let subject = parse_ints(&b"12 34 5689a"[..]); |
| 57 |
let expected = Ok((&b"a"[..], vec![12, 34, 5689])); |
| 58 |
assert_eq!(subject, expected); |
| 59 |
|
| 60 |
let subject = parse_ints(&b"12 34 5689 "[..]); |
| 61 |
let expected = Ok((&b" "[..], vec![12, 34, 5689])); |
| 62 |
assert_eq!(subject, expected) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
#[test] |
| 67 |
fn usize_length_bytes_issue() { |
| 68 |
use nom::multi::length_data; |
| 69 |
use nom::number::streaming::be_u16; |
| 70 |
let _: IResult<&[u8], &[u8], (&[u8], ErrorKind)> = length_data(be_u16).parse(b"012346"); |
| 71 |
} |
| 72 |
|
| 73 |
#[test] |
| 74 |
fn take_till_issue() { |
| 75 |
use nom::bytes::streaming::take_till; |
| 76 |
|
| 77 |
fn nothing(i: &[u8]) -> IResult<&[u8], &[u8]> { |
| 78 |
take_till(|_| true)(i) |
| 79 |
} |
| 80 |
|
| 81 |
assert_eq!(nothing(b""), Err(Err::Incomplete(Needed::new(1)))); |
| 82 |
assert_eq!(nothing(b"abc"), Ok((&b"abc"[..], &b""[..]))); |
| 83 |
} |
| 84 |
|
| 85 |
#[test] |
| 86 |
fn issue_655() { |
| 87 |
use nom::character::streaming::{line_ending, not_line_ending}; |
| 88 |
fn twolines(i: &str) -> IResult<&str, (&str, &str)> { |
| 89 |
let (i, l1) = not_line_ending(i)?; |
| 90 |
let (i, _) = line_ending(i)?; |
| 91 |
let (i, l2) = not_line_ending(i)?; |
| 92 |
let (i, _) = line_ending(i)?; |
| 93 |
|
| 94 |
Ok((i, (l1, l2))) |
| 95 |
} |
| 96 |
|
| 97 |
assert_eq!(twolines("foo\nbar\n"), Ok(("", ("foo", "bar")))); |
| 98 |
assert_eq!(twolines("féo\nbar\n"), Ok(("", ("féo", "bar")))); |
| 99 |
assert_eq!(twolines("foé\nbar\n"), Ok(("", ("foé", "bar")))); |
| 100 |
assert_eq!(twolines("foé\r\nbar\n"), Ok(("", ("foé", "bar")))); |
| 101 |
} |
| 102 |
|
| 103 |
#[cfg(feature = "alloc")] |
| 104 |
fn issue_717(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> { |
| 105 |
use nom::bytes::complete::{is_not, tag}; |
| 106 |
use nom::multi::separated_list0; |
| 107 |
|
| 108 |
separated_list0(tag(&[0x0][..]), is_not([0x0u8])).parse(i) |
| 109 |
} |
| 110 |
|
| 111 |
mod issue_647 { |
| 112 |
use nom::bytes::streaming::tag; |
| 113 |
use nom::combinator::complete; |
| 114 |
use nom::multi::separated_list0; |
| 115 |
use nom::Parser; |
| 116 |
use nom::{error::Error, number::streaming::be_f64, Err, IResult}; |
| 117 |
pub type Input<'a> = &'a [u8]; |
| 118 |
|
| 119 |
#[derive(PartialEq, Debug, Clone)] |
| 120 |
struct Data { |
| 121 |
c: f64, |
| 122 |
v: Vec<f64>, |
| 123 |
} |
| 124 |
|
| 125 |
#[allow(clippy::type_complexity)] |
| 126 |
fn list<'a>( |
| 127 |
input: Input<'a>, |
| 128 |
_cs: &'_ f64, |
| 129 |
) -> Result<(Input<'a>, Vec<f64>), Err<Error<&'a [u8]>>> { |
| 130 |
separated_list0(complete(tag(",")), complete(be_f64)).parse(input) |
| 131 |
} |
| 132 |
|
| 133 |
fn data(input: Input<'_>) -> IResult<Input<'_>, Data> { |
| 134 |
let (i, c) = be_f64(input)?; |
| 135 |
let (i, _) = tag("\n")(i)?; |
| 136 |
let (i, v) = list(i, &c)?; |
| 137 |
Ok((i, Data { c, v })) |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
#[test] |
| 142 |
fn issue_848_overflow_incomplete_bits_to_bytes() { |
| 143 |
fn take(i: &[u8]) -> IResult<&[u8], &[u8]> { |
| 144 |
use nom::bytes::streaming::take; |
| 145 |
take(0x2000000000000000_usize)(i) |
| 146 |
} |
| 147 |
fn parser(i: &[u8]) -> IResult<&[u8], &[u8]> { |
| 148 |
use nom::bits::{bits, bytes}; |
| 149 |
|
| 150 |
bits(bytes(take))(i) |
| 151 |
} |
| 152 |
assert_eq!( |
| 153 |
parser(&b""[..]), |
| 154 |
Err(Err::Failure(nom::error_position!( |
| 155 |
&b""[..], |
| 156 |
ErrorKind::TooLarge |
| 157 |
))) |
| 158 |
); |
| 159 |
} |
| 160 |
|
| 161 |
#[test] |
| 162 |
fn issue_942() { |
| 163 |
use nom::error::{ContextError, ParseError}; |
| 164 |
pub fn parser<'a, E: ParseError<&'a str> + ContextError<&'a str>>( |
| 165 |
i: &'a str, |
| 166 |
) -> IResult<&'a str, usize, E> { |
| 167 |
use nom::{character::complete::char, error::context, multi::many0_count}; |
| 168 |
many0_count(context("char_a", char('a'))).parse(i) |
| 169 |
} |
| 170 |
assert_eq!(parser::<()>("aaa"), Ok(("", 3))); |
| 171 |
} |
| 172 |
|
| 173 |
#[test] |
| 174 |
fn issue_many_m_n_with_zeros() { |
| 175 |
use nom::character::complete::char; |
| 176 |
use nom::multi::many; |
| 177 |
let mut parser = many::<_, (), Vec<char>, _, _>(0..=0, char('a')); |
| 178 |
assert_eq!(parser.parse("aaa"), Ok(("aaa", vec!()))); |
| 179 |
} |
| 180 |
|
| 181 |
#[test] |
| 182 |
fn issue_1231_bits_expect_fn_closure() { |
| 183 |
use nom::bits::{bits, complete::take}; |
| 184 |
use nom::error::Error; |
| 185 |
pub fn example(input: &[u8]) -> IResult<&[u8], (u8, u8)> { |
| 186 |
bits::<_, _, Error<_>, _, _>((take(1usize), take(1usize)))(input) |
| 187 |
} |
| 188 |
assert_eq!(example(&[0xff]), Ok((&b""[..], (1, 1)))); |
| 189 |
} |
| 190 |
|
| 191 |
#[test] |
| 192 |
fn issue_1282_findtoken_char() { |
| 193 |
use nom::character::complete::one_of; |
| 194 |
use nom::error::Error; |
| 195 |
let mut parser = one_of::<_, _, Error<_>>(&['a', 'b', 'c'][..]); |
| 196 |
assert_eq!(parser("aaa"), Ok(("aa", 'a'))); |
| 197 |
} |
| 198 |
|
| 199 |
#[test] |
| 200 |
fn issue_x_looser_fill_bounds() { |
| 201 |
use nom::{ |
| 202 |
bytes::streaming::tag, character::streaming::digit1, error_position, multi::fill, |
| 203 |
sequence::terminated, |
| 204 |
}; |
| 205 |
|
| 206 |
fn fill_pair(i: &[u8]) -> IResult<&[u8], [&[u8]; 2]> { |
| 207 |
let mut buf = [&[][..], &[][..]]; |
| 208 |
let (i, _) = fill(terminated(digit1, tag(",")), &mut buf).parse(i)?; |
| 209 |
Ok((i, buf)) |
| 210 |
} |
| 211 |
|
| 212 |
assert_eq!( |
| 213 |
fill_pair(b"123,456,"), |
| 214 |
Ok((&b""[..], [&b"123"[..], &b"456"[..]])) |
| 215 |
); |
| 216 |
assert_eq!( |
| 217 |
fill_pair(b"123,456,789"), |
| 218 |
Ok((&b"789"[..], [&b"123"[..], &b"456"[..]])) |
| 219 |
); |
| 220 |
assert_eq!( |
| 221 |
fill_pair(b"123,,"), |
| 222 |
Err(Err::Error(error_position!(&b","[..], ErrorKind::Digit))) |
| 223 |
); |
| 224 |
} |
| 225 |
|
| 226 |
#[test] |
| 227 |
fn issue_1459_clamp_capacity() { |
| 228 |
use nom::character::complete::char; |
| 229 |
|
| 230 |
// shouldn't panic |
| 231 |
use nom::multi::many_m_n; |
| 232 |
let mut parser = many_m_n::<_, (), _>(usize::MAX, usize::MAX, char('a')); |
| 233 |
assert_eq!(parser.parse("a"), Err(nom::Err::Error(()))); |
| 234 |
|
| 235 |
// shouldn't panic |
| 236 |
use nom::multi::count; |
| 237 |
let mut parser = count(char('a'), usize::MAX); |
| 238 |
assert_eq!(parser.parse("a"), Err(nom::Err::Error(()))); |
| 239 |
} |
| 240 |
|
| 241 |
#[test] |
| 242 |
fn issue_1617_count_parser_returning_zero_size() { |
| 243 |
use nom::{bytes::complete::tag, combinator::map, error::Error, multi::count}; |
| 244 |
|
| 245 |
// previously, `count()` panicked if the parser had type `O = ()` |
| 246 |
let parser = map(tag::<_, _, Error<&str>>("abc"), |_| ()); |
| 247 |
// shouldn't panic |
| 248 |
let result = count(parser, 3) |
| 249 |
.parse("abcabcabcdef") |
| 250 |
.expect("parsing should succeed"); |
| 251 |
assert_eq!(result, ("def", vec![(), (), ()])); |
| 252 |
} |
| 253 |
|
| 254 |
#[test] |
| 255 |
fn issue_1586_parser_iterator_impl() { |
| 256 |
use nom::{ |
| 257 |
character::complete::{digit1, newline}, |
| 258 |
combinator::{iterator, opt}, |
| 259 |
sequence::terminated, |
| 260 |
IResult, |
| 261 |
}; |
| 262 |
fn parse_line(i: &str) -> IResult<&str, &str> { |
| 263 |
terminated(digit1, opt(newline)).parse(i) |
| 264 |
} |
| 265 |
|
| 266 |
fn parse_input(i: &str) -> impl Iterator<Item = i32> + '_ { |
| 267 |
iterator(i, parse_line).map(|x| x.parse::<i32>().unwrap()) |
| 268 |
} |
| 269 |
|
| 270 |
assert_eq!(parse_input("123\n456").collect::<Vec<_>>(), vec![123, 456]); |
| 271 |
} |