blob: d987173319a5d9c4f23cf938267c50a771751603 [file] [log] [blame]
David Tolnayb5a7b142016-09-13 22:46:39 -07001// Adapted from nom <https://github.com/Geal/nom> by removing the
2// IResult::Incomplete variant, which we don't use and which unfortunately more
3// than doubles the compilation time.
4
5#[derive(Debug, PartialEq, Eq, Clone)]
6pub enum IResult<I, O> {
7 /// indicates a correct parsing, the first field containing the rest of the
8 /// unparsed data, the second field contains the parsed data
9 Done(I, O),
10 Error,
11}
12
David Tolnayb5a7b142016-09-13 22:46:39 -070013macro_rules! named {
14 ($name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => {
15 fn $name(i: &str) -> $crate::nom::IResult<&str, $o> {
16 $submac!(i, $($args)*)
17 }
18 };
19
20 (pub $name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => {
21 pub fn $name(i: &str) -> $crate::nom::IResult<&str, $o> {
22 $submac!(i, $($args)*)
23 }
24 };
25}
26
27macro_rules! call {
David Tolnayaf2557e2016-10-24 11:52:21 -070028 ($i:expr, $fun:expr $(, $args:expr)*) => {
29 $fun($i $(, $args)*)
David Tolnayb5a7b142016-09-13 22:46:39 -070030 };
31}
32
33macro_rules! map {
34 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -070035 map_impl!($i, $submac!($($args)*), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -070036 };
37
38 ($i:expr, $f:expr, $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -070039 map_impl!($i, call!($f), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -070040 };
41}
42
43/// Internal parser, do not use directly
44macro_rules! map_impl {
45 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => {
46 match $submac!($i, $($args)*) {
47 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
48 $crate::nom::IResult::Done(i, o) => {
49 $crate::nom::IResult::Done(i, $submac2!(o, $($args2)*))
50 }
51 }
52 };
53}
54
David Tolnayb5a7b142016-09-13 22:46:39 -070055macro_rules! not {
56 ($i:expr, $submac:ident!( $($args:tt)* )) => {
57 match $submac!($i, $($args)*) {
58 $crate::nom::IResult::Done(_, _) => $crate::nom::IResult::Error,
59 $crate::nom::IResult::Error => $crate::nom::IResult::Done($i, ""),
60 }
61 };
62}
63
David Tolnayeea28d62016-10-25 20:44:08 -070064// This is actually nom's cond_with_error.
David Tolnayb5a7b142016-09-13 22:46:39 -070065macro_rules! cond {
66 ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => {
67 if $cond {
68 match $submac!($i, $($args)*) {
David Tolnayde955482016-10-08 13:10:27 -070069 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, ::std::option::Option::Some(o)),
David Tolnayeea28d62016-10-25 20:44:08 -070070 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayb5a7b142016-09-13 22:46:39 -070071 }
72 } else {
73 $crate::nom::IResult::Done($i, ::std::option::Option::None)
74 }
David Tolnaycfe55022016-10-02 22:02:27 -070075 };
76
77 ($i:expr, $cond:expr, $f:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -070078 cond!($i, $cond, call!($f))
David Tolnaycfe55022016-10-02 22:02:27 -070079 };
David Tolnayb5a7b142016-09-13 22:46:39 -070080}
81
David Tolnayaf2557e2016-10-24 11:52:21 -070082macro_rules! cond_reduce {
83 ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => {
84 if $cond {
85 $submac!($i, $($args)*)
86 } else {
87 $crate::nom::IResult::Error
88 }
89 };
90
91 ($i:expr, $cond:expr, $f:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -070092 cond_reduce!($i, $cond, call!($f))
David Tolnayaf2557e2016-10-24 11:52:21 -070093 };
94}
95
David Tolnayb5a7b142016-09-13 22:46:39 -070096macro_rules! preceded {
97 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => {
98 match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) {
99 $crate::nom::IResult::Done(remaining, (_, o)) => $crate::nom::IResult::Done(remaining, o),
100 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
101 }
102 };
103
104 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700105 preceded!($i, $submac!($($args)*), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -0700106 };
107
108 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700109 preceded!($i, call!($f), $submac!($($args)*))
David Tolnayb5a7b142016-09-13 22:46:39 -0700110 };
111
112 ($i:expr, $f:expr, $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700113 preceded!($i, call!($f), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -0700114 };
115}
116
117macro_rules! terminated {
118 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => {
119 match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) {
120 $crate::nom::IResult::Done(remaining, (o, _)) => $crate::nom::IResult::Done(remaining, o),
121 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
122 }
123 };
124
125 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700126 terminated!($i, $submac!($($args)*), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -0700127 };
128
129 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700130 terminated!($i, call!($f), $submac!($($args)*))
David Tolnayb5a7b142016-09-13 22:46:39 -0700131 };
132
133 ($i:expr, $f:expr, $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700134 terminated!($i, call!($f), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -0700135 };
136}
137
138macro_rules! many0 {
139 ($i:expr, $submac:ident!( $($args:tt)* )) => {{
140 let ret;
141 let mut res = ::std::vec::Vec::new();
142 let mut input = $i;
143
144 loop {
145 if input.is_empty() {
146 ret = $crate::nom::IResult::Done(input, res);
147 break;
148 }
149
150 match $submac!(input, $($args)*) {
151 $crate::nom::IResult::Error => {
152 ret = $crate::nom::IResult::Done(input, res);
153 break;
154 }
155 $crate::nom::IResult::Done(i, o) => {
156 // loop trip must always consume (otherwise infinite loops)
David Tolnaybc84d5a2016-10-08 13:20:57 -0700157 if i.len() == input.len() {
David Tolnayb5a7b142016-09-13 22:46:39 -0700158 ret = $crate::nom::IResult::Error;
159 break;
160 }
161
162 res.push(o);
163 input = i;
164 }
165 }
166 }
167
168 ret
169 }};
170
171 ($i:expr, $f:expr) => {
David Tolnaybc84d5a2016-10-08 13:20:57 -0700172 $crate::nom::many0($i, $f)
David Tolnayb5a7b142016-09-13 22:46:39 -0700173 };
174}
175
David Tolnayc7f646a2016-10-16 10:54:39 -0700176pub fn many0<'a, T>(mut input: &'a str,
177 f: fn(&'a str) -> IResult<&'a str, T>)
178 -> IResult<&'a str, Vec<T>> {
David Tolnaybc84d5a2016-10-08 13:20:57 -0700179 let mut res = Vec::new();
180
181 loop {
182 if input.is_empty() {
183 return IResult::Done(input, res);
184 }
185
186 match f(input) {
187 IResult::Error => {
188 return IResult::Done(input, res);
189 }
190 IResult::Done(i, o) => {
191 // loop trip must always consume (otherwise infinite loops)
192 if i.len() == input.len() {
193 return IResult::Error;
194 }
195
196 res.push(o);
197 input = i;
198 }
199 }
200 }
201}
202
David Tolnayb5a7b142016-09-13 22:46:39 -0700203macro_rules! peek {
204 ($i:expr, $submac:ident!( $($args:tt)* )) => {
205 match $submac!($i, $($args)*) {
206 $crate::nom::IResult::Done(_, o) => $crate::nom::IResult::Done($i, o),
207 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
208 }
209 };
210}
211
212macro_rules! take_while1 {
213 ($input:expr, $submac:ident!( $($args:tt)* )) => {{
214 let mut offset = $input.len();
215 for (o, c) in $input.char_indices() {
216 if !$submac!(c, $($args)*) {
217 offset = o;
218 break;
219 }
220 }
221 if offset == 0 {
222 $crate::nom::IResult::Error
223 } else if offset < $input.len() {
224 $crate::nom::IResult::Done(&$input[offset..], &$input[..offset])
225 } else {
226 $crate::nom::IResult::Done("", $input)
227 }
228 }};
229
230 ($input:expr, $f:expr) => {
231 take_while1!($input, call!($f));
232 };
233}
234
235macro_rules! take_until {
236 ($input:expr, $substr:expr) => {{
237 if $substr.len() > $input.len() {
238 $crate::nom::IResult::Error
239 } else {
David Tolnayf2c452b2016-12-21 22:45:28 -0500240 let substr_vec: Vec<char> = $substr.chars().collect();
David Tolnayb5a7b142016-09-13 22:46:39 -0700241 let mut window: Vec<char> = vec![];
242 let mut offset = $input.len();
243 let mut parsed = false;
244 for (o, c) in $input.char_indices() {
245 window.push(c);
246 if window.len() > substr_vec.len() {
247 window.remove(0);
248 }
249 if window == substr_vec {
250 parsed = true;
251 window.pop();
252 let window_len: usize = window.iter()
253 .map(|x| x.len_utf8())
254 .fold(0, |x, y| x + y);
255 offset = o - window_len;
256 break;
257 }
258 }
259 if parsed {
260 $crate::nom::IResult::Done(&$input[offset..], &$input[..offset])
261 } else {
262 $crate::nom::IResult::Error
263 }
264 }
265 }};
266}
267
268macro_rules! tag {
269 ($i:expr, $tag: expr) => {
David Tolnay0b154ea2016-10-01 15:42:50 -0700270 if $i.starts_with($tag) {
David Tolnayb5a7b142016-09-13 22:46:39 -0700271 $crate::nom::IResult::Done(&$i[$tag.len()..], &$i[0..$tag.len()])
272 } else {
273 $crate::nom::IResult::Error
274 }
275 };
276}
277
278macro_rules! switch {
279 ($i:expr, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => {
280 match $submac!($i, $($args)*) {
281 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
282 $crate::nom::IResult::Done(i, o) => match o {
283 $(
284 $p => $subrule!(i, $($args2)*),
285 )*
286 _ => $crate::nom::IResult::Error,
287 }
288 }
289 };
290}
291
292macro_rules! value {
293 ($i:expr, $res:expr) => {
294 $crate::nom::IResult::Done($i, $res)
295 };
296}
297
298macro_rules! delimited {
299 ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => {
300 match tuple_parser!($i, (), $submac!($($args)*), $($rest)*) {
301 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
302 $crate::nom::IResult::Done(i1, (_, o, _)) => $crate::nom::IResult::Done(i1, o)
303 }
304 };
305
306 ($i:expr, $f:expr, $($rest:tt)+) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700307 delimited!($i, call!($f), $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700308 };
309}
310
David Tolnayb5a7b142016-09-13 22:46:39 -0700311macro_rules! separated_nonempty_list {
312 ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => {{
313 let mut res = ::std::vec::Vec::new();
314 let mut input = $i;
315
316 // get the first element
317 match $submac!(input, $($args2)*) {
318 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700319 $crate::nom::IResult::Done(i, o) => {
David Tolnayb5a7b142016-09-13 22:46:39 -0700320 if i.len() == input.len() {
321 $crate::nom::IResult::Error
322 } else {
323 res.push(o);
324 input = i;
325
326 while let $crate::nom::IResult::Done(i2, _) = $sep!(input, $($args)*) {
327 if i2.len() == input.len() {
328 break;
329 }
330
David Tolnayde955482016-10-08 13:10:27 -0700331 if let $crate::nom::IResult::Done(i3, o3) = $submac!(i2, $($args2)*) {
David Tolnayb5a7b142016-09-13 22:46:39 -0700332 if i3.len() == i2.len() {
333 break;
334 }
335 res.push(o3);
336 input = i3;
337 } else {
338 break;
339 }
340 }
341 $crate::nom::IResult::Done(input, res)
342 }
343 }
344 }
345 }};
346
347 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700348 separated_nonempty_list!($i, $submac!($($args)*), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -0700349 };
350
351 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700352 separated_nonempty_list!($i, call!($f), $submac!($($args)*))
David Tolnayb5a7b142016-09-13 22:46:39 -0700353 };
354
355 ($i:expr, $f:expr, $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700356 separated_nonempty_list!($i, call!($f), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -0700357 };
358}
359
360macro_rules! tuple {
361 ($i:expr, $($rest:tt)*) => {
362 tuple_parser!($i, (), $($rest)*)
363 };
364}
365
366/// Internal parser, do not use directly
367macro_rules! tuple_parser {
368 ($i:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700369 tuple_parser!($i, ($($parsed),*), call!($e), $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700370 };
371
372 ($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
373 match $submac!($i, $($args)*) {
374 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700375 $crate::nom::IResult::Done(i, o) =>
David Tolnayb5a7b142016-09-13 22:46:39 -0700376 tuple_parser!(i, (o), $($rest)*),
377 }
378 };
379
380 ($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
381 match $submac!($i, $($args)*) {
382 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700383 $crate::nom::IResult::Done(i, o) =>
David Tolnayb5a7b142016-09-13 22:46:39 -0700384 tuple_parser!(i, ($($parsed)* , o), $($rest)*),
385 }
386 };
387
388 ($i:expr, ($($parsed:tt),*), $e:ident) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700389 tuple_parser!($i, ($($parsed),*), call!($e))
David Tolnayb5a7b142016-09-13 22:46:39 -0700390 };
391
392 ($i:expr, (), $submac:ident!( $($args:tt)* )) => {
393 $submac!($i, $($args)*)
394 };
395
396 ($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => {
397 match $submac!($i, $($args)*) {
398 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
399 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, ($($parsed),*, o))
400 }
401 };
402
403 ($i:expr, ($($parsed:expr),*)) => {
404 $crate::nom::IResult::Done($i, ($($parsed),*))
405 };
406}
407
408macro_rules! alt {
409 ($i:expr, $e:ident | $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700410 alt!($i, call!($e) | $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700411 };
412
413 ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => {
414 match $subrule!($i, $($args)*) {
415 res @ $crate::nom::IResult::Done(_, _) => res,
416 _ => alt!($i, $($rest)*)
417 }
418 };
419
420 ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => {
421 match $subrule!($i, $($args)*) {
422 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, $gen(o)),
423 $crate::nom::IResult::Error => alt!($i, $($rest)*)
424 }
425 };
426
427 ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700428 alt!($i, call!($e) => { $gen } | $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700429 };
430
431 ($i:expr, $e:ident => { $gen:expr }) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700432 alt!($i, call!($e) => { $gen })
David Tolnayb5a7b142016-09-13 22:46:39 -0700433 };
434
435 ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => {
436 match $subrule!($i, $($args)*) {
437 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, $gen(o)),
David Tolnay5377b172016-10-25 01:13:12 -0700438 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayb5a7b142016-09-13 22:46:39 -0700439 }
440 };
441
442 ($i:expr, $e:ident) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700443 alt!($i, call!($e))
David Tolnayb5a7b142016-09-13 22:46:39 -0700444 };
445
446 ($i:expr, $subrule:ident!( $($args:tt)*)) => {
David Tolnay5377b172016-10-25 01:13:12 -0700447 $subrule!($i, $($args)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700448 };
449}
450
451macro_rules! do_parse {
452 ($i:expr, ( $($rest:expr),* )) => {
453 $crate::nom::IResult::Done($i, ( $($rest),* ))
454 };
455
456 ($i:expr, $e:ident >> $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700457 do_parse!($i, call!($e) >> $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700458 };
459
460 ($i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
461 match $submac!($i, $($args)*) {
462 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
463 $crate::nom::IResult::Done(i, _) =>
464 do_parse!(i, $($rest)*),
465 }
466 };
467
468 ($i:expr, $field:ident : $e:ident >> $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700469 do_parse!($i, $field: call!($e) >> $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700470 };
471
472 ($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
473 match $submac!($i, $($args)*) {
474 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700475 $crate::nom::IResult::Done(i, o) => {
David Tolnayb5a7b142016-09-13 22:46:39 -0700476 let $field = o;
477 do_parse!(i, $($rest)*)
478 },
479 }
480 };
481
David Tolnayfa0edf22016-09-23 22:58:24 -0700482 ($i:expr, mut $field:ident : $e:ident >> $($rest:tt)*) => {
David Tolnay7184b132016-10-30 10:06:37 -0700483 do_parse!($i, mut $field: call!($e) >> $($rest)*)
David Tolnayfa0edf22016-09-23 22:58:24 -0700484 };
485
486 ($i:expr, mut $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
487 match $submac!($i, $($args)*) {
488 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700489 $crate::nom::IResult::Done(i, o) => {
David Tolnayfa0edf22016-09-23 22:58:24 -0700490 let mut $field = o;
491 do_parse!(i, $($rest)*)
492 },
493 }
494 };
David Tolnayb5a7b142016-09-13 22:46:39 -0700495}