1 //#![feature(trace_macros)]
2 #![allow(dead_code)]
3 #![cfg_attr(feature = "cargo-clippy", allow(redundant_closure))]
4
5 #[macro_use]
6 extern crate nom;
7
8 use nom::{character::{is_digit, streaming::space1 as space}, Err, IResult, Needed, error::ErrorKind, number::streaming::le_u64};
9
10 #[allow(dead_code)]
11 struct Range {
12 start: char,
13 end: char,
14 }
15
take_char(input: &[u8]) -> IResult<&[u8], char>16 pub fn take_char(input: &[u8]) -> IResult<&[u8], char> {
17 if !input.is_empty() {
18 Ok((&input[1..], input[0] as char))
19 } else {
20 Err(Err::Incomplete(Needed::Size(1)))
21 }
22 }
23
24 //trace_macros!(true);
25
26 #[allow(dead_code)]
27 named!(range<&[u8], Range>,
28 alt!(
29 do_parse!(
30 start: take_char >>
31 tag!("-") >>
32 end: take_char >>
33 (Range {
34 start: start,
35 end: end,
36 })
37 ) |
38 map!(
39 take_char,
40 |c| {
41 Range {
42 start: c,
43 end: c,
44 }
45 }
46 )
47 )
48 );
49
50 #[allow(dead_code)]
51 named!(literal<&[u8], Vec<char> >,
52 map!(
53 many1!(take_char),
54 |cs| {
55 cs
56 }
57 )
58 );
59
60 #[test]
issue_58()61 fn issue_58() {
62 let _ = range(&b"abcd"[..]);
63 let _ = literal(&b"abcd"[..]);
64 }
65
66 //trace_macros!(false);
67
68 #[cfg(feature = "std")]
69 mod parse_int {
70 use nom::HexDisplay;
71 use nom::{IResult, character::streaming::{digit1 as digit, space1 as space}};
72 use std::str;
73
74 named!(parse_ints<Vec<i32>>, many0!(spaces_or_int));
75
spaces_or_int(input: &[u8]) -> IResult<&[u8], i32>76 fn spaces_or_int(input: &[u8]) -> IResult<&[u8], i32> {
77 println!("{}", input.to_hex(8));
78 do_parse!(
79 input,
80 opt!(complete!(space)) >> res: map!(complete!(digit), |x| {
81 println!("x: {:?}", x);
82 let result = str::from_utf8(x).unwrap();
83 println!("Result: {}", result);
84 println!("int is empty?: {}", x.is_empty());
85 match result.parse() {
86 Ok(i) => i,
87 Err(e) => panic!("UH OH! NOT A DIGIT! {:?}", e),
88 }
89 }) >> (res)
90 )
91 }
92
93 #[test]
issue_142()94 fn issue_142() {
95 let subject = parse_ints(&b"12 34 5689a"[..]);
96 let expected = Ok((&b"a"[..], vec![12, 34, 5689]));
97 assert_eq!(subject, expected);
98
99 let subject = parse_ints(&b"12 34 5689 "[..]);
100 let expected = Ok((&b" "[..], vec![12, 34, 5689]));
101 assert_eq!(subject, expected)
102 }
103 }
104
105 #[test]
usize_length_bytes_issue()106 fn usize_length_bytes_issue() {
107 use nom::number::streaming::be_u16;
108 let _: IResult<&[u8], &[u8], (&[u8], ErrorKind)> = length_data!(b"012346", be_u16);
109 }
110
111 /*
112 DOES NOT COMPILE
113 #[test]
114 fn issue_152() {
115 named!(take4, take!(4));
116 named!(xyz, tag!("XYZ"));
117 named!(abc, tag!("abc"));
118
119
120 named!(sw,
121 switch!(take4,
122 b"abcd" => xyz |
123 b"efgh" => abc
124 )
125 );
126 }
127 */
128
129 #[test]
take_till_issue()130 fn take_till_issue() {
131 named!(nothing, take_till!(call!(|_| true)));
132
133 assert_eq!(nothing(b""), Err(Err::Incomplete(Needed::Size(1))));
134 assert_eq!(nothing(b"abc"), Ok((&b"abc"[..], &b""[..])));
135 }
136
137 named!(
138 issue_498<Vec<&[u8]>>,
139 separated_nonempty_list!(opt!(space), tag!("abcd"))
140 );
141
142 named!(issue_308(&str) -> bool,
143 do_parse! (
144 tag! ("foo") >>
145 b: alt! (
146 complete!(map! (tag! ("1"), |_: &str|->bool {true})) |
147 value! (false)
148 ) >>
149 (b) ));
150
151 #[cfg(feature = "alloc")]
issue_302(input: &[u8]) -> IResult<&[u8], Option<Vec<u64>>>152 fn issue_302(input: &[u8]) -> IResult<&[u8], Option<Vec<u64>>> {
153 do_parse!(input, entries: cond!(true, count!(le_u64, 3)) >> (entries))
154 }
155
156 #[test]
issue_655()157 fn issue_655() {
158 use nom::character::streaming::{line_ending, not_line_ending};
159 named!(twolines(&str) -> (&str, &str),
160 do_parse!(
161 l1 : not_line_ending >>
162 line_ending >>
163 l2 : not_line_ending >>
164 line_ending >>
165 ((l1, l2))
166 )
167 );
168
169 assert_eq!(twolines("foo\nbar\n"), Ok(("", ("foo", "bar"))));
170 assert_eq!(twolines("féo\nbar\n"), Ok(("", ("féo", "bar"))));
171 assert_eq!(twolines("foé\nbar\n"), Ok(("", ("foé", "bar"))));
172 assert_eq!(twolines("foé\r\nbar\n"), Ok(("", ("foé", "bar"))));
173 }
174
175 #[test]
issue_721()176 fn issue_721() {
177 named!(f1<&str, u16>, parse_to!(u16));
178 named!(f2<&str, String>, parse_to!(String));
179 assert_eq!(f1("1234"), Ok(("", 1234)));
180 assert_eq!(f2("foo"), Ok(("", "foo".to_string())));
181 //assert_eq!(parse_to!("1234", u16), Ok(("", 1234)));
182 //assert_eq!(parse_to!("foo", String), Ok(("", "foo".to_string())));
183 }
184
185 #[cfg(feature = "alloc")]
186 named!(issue_717<&[u8], Vec<&[u8]> >,
187 separated_list!(tag!([0x0]), is_not!([0x0u8]))
188 );
189
190 struct NoPartialEq {
191 value: i32,
192 }
193
194 named!(issue_724<&str, i32>,
195 do_parse!(
196 metadata: permutation!(
197 map!(tag!("hello"), |_| NoPartialEq { value: 1 }),
198 map!(tag!("world"), |_| NoPartialEq { value: 2 })
199 ) >>
200 (metadata.0.value + metadata.1.value)
201 )
202 );
203
204 #[test]
issue_752()205 fn issue_752() {
206 assert_eq!(
207 Err::Error(("ab", nom::error::ErrorKind::ParseTo)),
208 parse_to!("ab", usize).unwrap_err()
209 )
210 }
211
atom_specials(c: u8) -> bool212 fn atom_specials(c: u8) -> bool {
213 c == b'q'
214 }
215
216 named!(
217 capability<&str>,
218 do_parse!(tag!(" ") >> _atom: map_res!(take_till1!(atom_specials), std::str::from_utf8) >> ("a"))
219 );
220
221 #[test]
issue_759()222 fn issue_759() {
223 assert_eq!(capability(b" abcqd"), Ok((&b"qd"[..], "a")));
224 }
225
226 named_args!(issue_771(count: usize)<Vec<u32>>,
227 length_count!(value!(count), call!(nom::number::streaming::be_u32))
228 );
229
230 /// This test is in a separate module to check that all required symbols are imported in
231 /// `escaped_transform!()`. Without the module, the `use`-es of the current module would
232 /// mask the error ('"Use of undeclared type or module `Needed`" in escaped_transform!').
233 mod issue_780 {
234 named!(issue_780<&str, String>,
235 escaped_transform!(call!(::nom::character::streaming::alpha1), '\\', tag!("n"))
236 );
237 }
238
239 // issue 617
240 named!(digits, take_while1!( is_digit ));
241 named!(multi_617<&[u8], () >, fold_many0!( digits, (), |_, _| {}));
242
243 // Sad :(
244 named!(multi_617_fails<&[u8], () >, fold_many0!( take_while1!( is_digit ), (), |_, _| {}));
245
246 mod issue_647 {
247 use nom::{Err, number::streaming::be_f64, error::ErrorKind};
248 pub type Input<'a> = &'a [u8];
249
250 #[derive(PartialEq, Debug, Clone)]
251 struct Data {
252 c: f64,
253 v: Vec<f64>
254 }
255
list<'a,'b>(input: Input<'a>, _cs: &'b f64) -> Result<(Input<'a>,Vec<f64>), Err<(&'a [u8], ErrorKind)>>256 fn list<'a,'b>(input: Input<'a>, _cs: &'b f64) -> Result<(Input<'a>,Vec<f64>), Err<(&'a [u8], ErrorKind)>> {
257 separated_list!(input, complete!(tag!(",")), complete!(be_f64))
258 }
259
260 named!(data<Input,Data>, map!(
261 do_parse!(
262 c: be_f64 >>
263 tag!("\n") >>
264 v: call!(list,&c) >>
265 (c,v)
266 ), |(c,v)| {
267 Data {
268 c: c,
269 v: v
270 }
271 }
272 ));
273 }
274
275 named!(issue_775, take_till1!(|_| true));
276
277 #[test]
issue_848_overflow_incomplete_bits_to_bytes()278 fn issue_848_overflow_incomplete_bits_to_bytes() {
279 named!(take, take!(0x2000000000000000));
280 named!(parser<&[u8], &[u8]>, bits!(bytes!(take)));
281 assert_eq!(parser(&b""[..]), Err(Err::Failure(error_position!(&b""[..], ErrorKind::TooLarge))));
282 }
283
284 #[test]
issue_942()285 fn issue_942() {
286 use nom::error::ParseError;
287 pub fn parser<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, usize, E> {
288 use nom::{character::complete::char, error::context, multi::many0_count};
289 many0_count(context("char_a", char('a')))(i)
290 }
291 assert_eq!(parser::<()>("aaa"), Ok(("", 3)));
292 }
293
294 #[test]
issue_many_m_n_with_zeros()295 fn issue_many_m_n_with_zeros() {
296 use nom::multi::many_m_n;
297 use nom::character::complete::char;
298 let parser = many_m_n::<_, _, (), _>(0, 0, char('a'));
299 assert_eq!(parser("aaa"), Ok(("aaa", vec!())));
300 }
301
302 #[test]
issue_1027_convert_error_panic_nonempty()303 fn issue_1027_convert_error_panic_nonempty() {
304 use nom::error::{VerboseError, convert_error};
305 use nom::sequence::pair;
306 use nom::character::complete::char;
307
308 let input = "a";
309
310 let result: IResult<_, _, VerboseError<&str>> = pair(char('a'), char('b'))(input);
311 let err = match result.unwrap_err() {
312 Err::Error(e) => e,
313 _ => unreachable!(),
314 };
315
316 let msg = convert_error(&input, err);
317 assert_eq!(msg, "0: at line 1:\na\n ^\nexpected \'b\', got end of input\n\n");
318 }
319