1 //#![feature(trace_macros)]
2 #![allow(dead_code)]
3
4 #[macro_use]
5 extern crate nom;
6
7 use nom::{IResult,Needed,space,be_u16,le_u64};
8
9 #[allow(dead_code)]
10 struct Range {
11 start: char,
12 end: char
13 }
14
take_char(input: &[u8]) -> IResult<&[u8], char>15 pub fn take_char(input: &[u8]) -> IResult<&[u8], char> {
16 if input.len() > 0 {
17 IResult::Done(&input[1..], input[0] as char)
18 } else {
19 IResult::Incomplete(Needed::Size(1))
20 }
21 }
22
23 //trace_macros!(true);
24
25 #[allow(dead_code)]
26 named!(range<&[u8], Range>,
27 alt!(
28 do_parse!(
29 start: take_char >>
30 tag!("-") >>
31 end: take_char >>
32 (Range {
33 start: start,
34 end: end,
35 })
36 ) |
37 map!(
38 take_char,
39 |c| {
40 Range {
41 start: c,
42 end: c,
43 }
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 range(&b"abcd"[..]);
63 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,space,digit};
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!(input,
79 opt!(complete!(space)) >>
80 res: map!(complete!(digit),
81 |x| {
82 println!("x: {:?}", x);
83 let result = str::from_utf8(x).unwrap();
84 println!("Result: {}", result);
85 println!("int is empty?: {}", x.is_empty());
86 match result.parse(){
87 Ok(i) => i,
88 Err(_) => panic!("UH OH! NOT A DIGIT!")
89 }
90 }) >>
91 (res)
92 )
93 }
94
95 #[test]
issue_142()96 fn issue_142(){
97 let subject = parse_ints(&b"12 34 5689"[..]);
98 let expected = IResult::Done(&b""[..], vec![12, 34, 5689]);
99 assert_eq!(subject, expected);
100
101 let subject = parse_ints(&b"12 34 5689 "[..]);
102 let expected = IResult::Done(&b" "[..], vec![12, 34, 5689]);
103 assert_eq!(subject, expected)
104 }
105 }
106
107 #[test]
usize_length_bytes_issue()108 fn usize_length_bytes_issue(){
109 length_bytes!(b"012346", be_u16);
110 }
111
112 /*
113 DOES NOT COMPILE
114 #[test]
115 fn issue_152() {
116 named!(take4, take!(4));
117 named!(xyz, tag!("XYZ"));
118 named!(abc, tag!("abc"));
119
120
121 named!(sw,
122 switch!(take4,
123 b"abcd" => xyz |
124 b"efgh" => abc
125 )
126 );
127 }
128 */
129
130 #[test]
take_till_issue()131 fn take_till_issue() {
132 named!(nothing,
133 take_till!(call!(|_| true))
134 );
135
136 assert_eq!(nothing(b""), IResult::Done(&b""[..], &b""[..]));
137 assert_eq!(nothing(b"abc"), IResult::Done(&b"abc"[..], &b""[..]));
138 }
139
140 named!(issue_498< Vec<&[u8]> >, separated_nonempty_list!( opt!(space), tag!("abcd") ));
141
142 named!(issue_308(&str) -> bool,
143 do_parse! (
144 tag_s! ("foo") >>
145 b: alt_complete! (
146 map! (tag_s! ("1"), |_: &str|->bool {true}) |
147 value! (false)
148 ) >>
149 (b) ));
150
151
issue_302(input: &[u8]) -> IResult<&[u8], Option<Vec<u64>> >152 fn issue_302(input: &[u8]) -> IResult<&[u8], Option<Vec<u64>> > {
153 do_parse!(input,
154 entries: cond!(true, count!(le_u64, 3)) >>
155 ( entries )
156 )
157 }
158