blob: cc372ec84299c05c92c1e715f453e18b6b86dc71 [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 {
28 ($i:expr, $fun:expr) => {
29 $fun($i)
30 };
31}
32
33macro_rules! map {
34 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
35 map_impl!($i, $submac!($($args)*), call!($g));
36 };
37
38 ($i:expr, $f:expr, $g:expr) => {
39 map_impl!($i, call!($f), call!($g));
40 };
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
64macro_rules! cond {
65 ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => {
66 if $cond {
67 match $submac!($i, $($args)*) {
David Tolnayde955482016-10-08 13:10:27 -070068 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, ::std::option::Option::Some(o)),
David Tolnayb5a7b142016-09-13 22:46:39 -070069 $crate::nom::IResult::Error => $crate::nom::IResult::Done($i, ::std::option::Option::None),
70 }
71 } else {
72 $crate::nom::IResult::Done($i, ::std::option::Option::None)
73 }
David Tolnaycfe55022016-10-02 22:02:27 -070074 };
75
76 ($i:expr, $cond:expr, $f:expr) => {
77 cond!($i, $cond, call!($f));
78 };
David Tolnayb5a7b142016-09-13 22:46:39 -070079}
80
81macro_rules! preceded {
82 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => {
83 match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) {
84 $crate::nom::IResult::Done(remaining, (_, o)) => $crate::nom::IResult::Done(remaining, o),
85 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
86 }
87 };
88
89 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
90 preceded!($i, $submac!($($args)*), call!($g));
91 };
92
93 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
94 preceded!($i, call!($f), $submac!($($args)*));
95 };
96
97 ($i:expr, $f:expr, $g:expr) => {
98 preceded!($i, call!($f), call!($g));
99 };
100}
101
102macro_rules! terminated {
103 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => {
104 match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) {
105 $crate::nom::IResult::Done(remaining, (o, _)) => $crate::nom::IResult::Done(remaining, o),
106 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
107 }
108 };
109
110 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
111 terminated!($i, $submac!($($args)*), call!($g));
112 };
113
114 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
115 terminated!($i, call!($f), $submac!($($args)*));
116 };
117
118 ($i:expr, $f:expr, $g:expr) => {
119 terminated!($i, call!($f), call!($g));
120 };
121}
122
123macro_rules! many0 {
124 ($i:expr, $submac:ident!( $($args:tt)* )) => {{
125 let ret;
126 let mut res = ::std::vec::Vec::new();
127 let mut input = $i;
128
129 loop {
130 if input.is_empty() {
131 ret = $crate::nom::IResult::Done(input, res);
132 break;
133 }
134
135 match $submac!(input, $($args)*) {
136 $crate::nom::IResult::Error => {
137 ret = $crate::nom::IResult::Done(input, res);
138 break;
139 }
140 $crate::nom::IResult::Done(i, o) => {
141 // loop trip must always consume (otherwise infinite loops)
David Tolnaybc84d5a2016-10-08 13:20:57 -0700142 if i.len() == input.len() {
David Tolnayb5a7b142016-09-13 22:46:39 -0700143 ret = $crate::nom::IResult::Error;
144 break;
145 }
146
147 res.push(o);
148 input = i;
149 }
150 }
151 }
152
153 ret
154 }};
155
156 ($i:expr, $f:expr) => {
David Tolnaybc84d5a2016-10-08 13:20:57 -0700157 $crate::nom::many0($i, $f)
David Tolnayb5a7b142016-09-13 22:46:39 -0700158 };
159}
160
David Tolnaybc84d5a2016-10-08 13:20:57 -0700161pub fn many0<'a, T>(mut input: &'a str, f: fn(&'a str) -> IResult<&'a str, T>) -> IResult<&'a str, Vec<T>> {
162 let mut res = Vec::new();
163
164 loop {
165 if input.is_empty() {
166 return IResult::Done(input, res);
167 }
168
169 match f(input) {
170 IResult::Error => {
171 return IResult::Done(input, res);
172 }
173 IResult::Done(i, o) => {
174 // loop trip must always consume (otherwise infinite loops)
175 if i.len() == input.len() {
176 return IResult::Error;
177 }
178
179 res.push(o);
180 input = i;
181 }
182 }
183 }
184}
185
David Tolnayb5a7b142016-09-13 22:46:39 -0700186macro_rules! peek {
187 ($i:expr, $submac:ident!( $($args:tt)* )) => {
188 match $submac!($i, $($args)*) {
189 $crate::nom::IResult::Done(_, o) => $crate::nom::IResult::Done($i, o),
190 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
191 }
192 };
193}
194
195macro_rules! take_while1 {
196 ($input:expr, $submac:ident!( $($args:tt)* )) => {{
197 let mut offset = $input.len();
198 for (o, c) in $input.char_indices() {
199 if !$submac!(c, $($args)*) {
200 offset = o;
201 break;
202 }
203 }
204 if offset == 0 {
205 $crate::nom::IResult::Error
206 } else if offset < $input.len() {
207 $crate::nom::IResult::Done(&$input[offset..], &$input[..offset])
208 } else {
209 $crate::nom::IResult::Done("", $input)
210 }
211 }};
212
213 ($input:expr, $f:expr) => {
214 take_while1!($input, call!($f));
215 };
216}
217
David Tolnay62604d92016-09-30 14:47:48 -0700218pub fn str_chars(s: &str) -> Vec<char> {
219 // Can't do `s.chars().collect()` because it triggers a compiler bug in 1.12.0
220 // https://github.com/dtolnay/syn/issues/20
221 let mut result = Vec::new();
222 for ch in s.chars() {
223 result.push(ch);
224 }
225 result
226}
227
David Tolnayb5a7b142016-09-13 22:46:39 -0700228macro_rules! take_until {
229 ($input:expr, $substr:expr) => {{
230 if $substr.len() > $input.len() {
231 $crate::nom::IResult::Error
232 } else {
David Tolnay62604d92016-09-30 14:47:48 -0700233 let substr_vec: Vec<char> = $crate::nom::str_chars($substr);
David Tolnayb5a7b142016-09-13 22:46:39 -0700234 let mut window: Vec<char> = vec![];
235 let mut offset = $input.len();
236 let mut parsed = false;
237 for (o, c) in $input.char_indices() {
238 window.push(c);
239 if window.len() > substr_vec.len() {
240 window.remove(0);
241 }
242 if window == substr_vec {
243 parsed = true;
244 window.pop();
245 let window_len: usize = window.iter()
246 .map(|x| x.len_utf8())
247 .fold(0, |x, y| x + y);
248 offset = o - window_len;
249 break;
250 }
251 }
252 if parsed {
253 $crate::nom::IResult::Done(&$input[offset..], &$input[..offset])
254 } else {
255 $crate::nom::IResult::Error
256 }
257 }
258 }};
259}
260
261macro_rules! tag {
262 ($i:expr, $tag: expr) => {
David Tolnay0b154ea2016-10-01 15:42:50 -0700263 if $i.starts_with($tag) {
David Tolnayb5a7b142016-09-13 22:46:39 -0700264 $crate::nom::IResult::Done(&$i[$tag.len()..], &$i[0..$tag.len()])
265 } else {
266 $crate::nom::IResult::Error
267 }
268 };
269}
270
271macro_rules! switch {
272 ($i:expr, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => {
273 match $submac!($i, $($args)*) {
274 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
275 $crate::nom::IResult::Done(i, o) => match o {
276 $(
277 $p => $subrule!(i, $($args2)*),
278 )*
279 _ => $crate::nom::IResult::Error,
280 }
281 }
282 };
283}
284
285macro_rules! value {
286 ($i:expr, $res:expr) => {
287 $crate::nom::IResult::Done($i, $res)
288 };
289}
290
291macro_rules! delimited {
292 ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => {
293 match tuple_parser!($i, (), $submac!($($args)*), $($rest)*) {
294 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
295 $crate::nom::IResult::Done(i1, (_, o, _)) => $crate::nom::IResult::Done(i1, o)
296 }
297 };
298
299 ($i:expr, $f:expr, $($rest:tt)+) => {
300 delimited!($i, call!($f), $($rest)*);
301 };
302}
303
304macro_rules! separated_list {
305 ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => {{
306 let mut res = ::std::vec::Vec::new();
307 let mut input = $i;
308
David Tolnaydaaf7742016-10-03 11:11:43 -0700309// get the first element
David Tolnayb5a7b142016-09-13 22:46:39 -0700310 match $submac!(input, $($args2)*) {
311 $crate::nom::IResult::Error => $crate::nom::IResult::Done(input, ::std::vec::Vec::new()),
David Tolnayde955482016-10-08 13:10:27 -0700312 $crate::nom::IResult::Done(i, o) => {
David Tolnayb5a7b142016-09-13 22:46:39 -0700313 if i.len() == input.len() {
314 $crate::nom::IResult::Error
315 } else {
316 res.push(o);
317 input = i;
318
David Tolnaydaaf7742016-10-03 11:11:43 -0700319// get the separator first
David Tolnayb5a7b142016-09-13 22:46:39 -0700320 while let $crate::nom::IResult::Done(i2, _) = $sep!(input, $($args)*) {
321 if i2.len() == input.len() {
322 break;
323 }
324
David Tolnaydaaf7742016-10-03 11:11:43 -0700325// get the element next
David Tolnayde955482016-10-08 13:10:27 -0700326 if let $crate::nom::IResult::Done(i3, o3) = $submac!(i2, $($args2)*) {
David Tolnayb5a7b142016-09-13 22:46:39 -0700327 if i3.len() == i2.len() {
328 break;
329 }
330 res.push(o3);
331 input = i3;
332 } else {
333 break;
334 }
335 }
336 $crate::nom::IResult::Done(input, res)
337 }
338 }
339 }
340 }};
341
342 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
343 separated_list!($i, $submac!($($args)*), call!($g));
344 };
345
346 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
347 separated_list!($i, call!($f), $submac!($($args)*));
348 };
349
350 ($i:expr, $f:expr, $g:expr) => {
351 separated_list!($i, call!($f), call!($g));
352 };
353}
354
355macro_rules! separated_nonempty_list {
356 ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => {{
357 let mut res = ::std::vec::Vec::new();
358 let mut input = $i;
359
360 // get the first element
361 match $submac!(input, $($args2)*) {
362 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700363 $crate::nom::IResult::Done(i, o) => {
David Tolnayb5a7b142016-09-13 22:46:39 -0700364 if i.len() == input.len() {
365 $crate::nom::IResult::Error
366 } else {
367 res.push(o);
368 input = i;
369
370 while let $crate::nom::IResult::Done(i2, _) = $sep!(input, $($args)*) {
371 if i2.len() == input.len() {
372 break;
373 }
374
David Tolnayde955482016-10-08 13:10:27 -0700375 if let $crate::nom::IResult::Done(i3, o3) = $submac!(i2, $($args2)*) {
David Tolnayb5a7b142016-09-13 22:46:39 -0700376 if i3.len() == i2.len() {
377 break;
378 }
379 res.push(o3);
380 input = i3;
381 } else {
382 break;
383 }
384 }
385 $crate::nom::IResult::Done(input, res)
386 }
387 }
388 }
389 }};
390
391 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
392 separated_nonempty_list!($i, $submac!($($args)*), call!($g));
393 };
394
395 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
396 separated_nonempty_list!($i, call!($f), $submac!($($args)*));
397 };
398
399 ($i:expr, $f:expr, $g:expr) => {
400 separated_nonempty_list!($i, call!($f), call!($g));
401 };
402}
403
404macro_rules! tuple {
405 ($i:expr, $($rest:tt)*) => {
406 tuple_parser!($i, (), $($rest)*)
407 };
408}
409
410/// Internal parser, do not use directly
411macro_rules! tuple_parser {
412 ($i:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => {
413 tuple_parser!($i, ($($parsed),*), call!($e), $($rest)*);
414 };
415
416 ($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
417 match $submac!($i, $($args)*) {
418 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700419 $crate::nom::IResult::Done(i, o) =>
David Tolnayb5a7b142016-09-13 22:46:39 -0700420 tuple_parser!(i, (o), $($rest)*),
421 }
422 };
423
424 ($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
425 match $submac!($i, $($args)*) {
426 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700427 $crate::nom::IResult::Done(i, o) =>
David Tolnayb5a7b142016-09-13 22:46:39 -0700428 tuple_parser!(i, ($($parsed)* , o), $($rest)*),
429 }
430 };
431
432 ($i:expr, ($($parsed:tt),*), $e:ident) => {
433 tuple_parser!($i, ($($parsed),*), call!($e));
434 };
435
436 ($i:expr, (), $submac:ident!( $($args:tt)* )) => {
437 $submac!($i, $($args)*)
438 };
439
440 ($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => {
441 match $submac!($i, $($args)*) {
442 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
443 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, ($($parsed),*, o))
444 }
445 };
446
447 ($i:expr, ($($parsed:expr),*)) => {
448 $crate::nom::IResult::Done($i, ($($parsed),*))
449 };
450}
451
452macro_rules! alt {
453 ($i:expr, $e:ident | $($rest:tt)*) => {
454 alt!($i, call!($e) | $($rest)*);
455 };
456
457 ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => {
458 match $subrule!($i, $($args)*) {
459 res @ $crate::nom::IResult::Done(_, _) => res,
460 _ => alt!($i, $($rest)*)
461 }
462 };
463
464 ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => {
465 match $subrule!($i, $($args)*) {
466 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, $gen(o)),
467 $crate::nom::IResult::Error => alt!($i, $($rest)*)
468 }
469 };
470
471 ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => {
472 alt!($i, call!($e) => { $gen } | $($rest)*);
473 };
474
475 ($i:expr, $e:ident => { $gen:expr }) => {
476 alt!($i, call!($e) => { $gen });
477 };
478
479 ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => {
480 match $subrule!($i, $($args)*) {
481 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, $gen(o)),
482 $crate::nom::IResult::Error => alt!($i)
483 }
484 };
485
486 ($i:expr, $e:ident) => {
487 alt!($i, call!($e));
488 };
489
490 ($i:expr, $subrule:ident!( $($args:tt)*)) => {
491 match $subrule!( $i, $($args)* ) {
David Tolnayde955482016-10-08 13:10:27 -0700492 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, o),
David Tolnayb5a7b142016-09-13 22:46:39 -0700493 $crate::nom::IResult::Error => alt!($i),
494 }
495 };
496
497 ($i:expr) => {
498 $crate::nom::IResult::Error
499 };
500}
501
502macro_rules! do_parse {
503 ($i:expr, ( $($rest:expr),* )) => {
504 $crate::nom::IResult::Done($i, ( $($rest),* ))
505 };
506
507 ($i:expr, $e:ident >> $($rest:tt)*) => {
508 do_parse!($i, call!($e) >> $($rest)*);
509 };
510
511 ($i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
512 match $submac!($i, $($args)*) {
513 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
514 $crate::nom::IResult::Done(i, _) =>
515 do_parse!(i, $($rest)*),
516 }
517 };
518
519 ($i:expr, $field:ident : $e:ident >> $($rest:tt)*) => {
520 do_parse!($i, $field: call!($e) >> $($rest)*);
521 };
522
523 ($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
524 match $submac!($i, $($args)*) {
525 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700526 $crate::nom::IResult::Done(i, o) => {
David Tolnayb5a7b142016-09-13 22:46:39 -0700527 let $field = o;
528 do_parse!(i, $($rest)*)
529 },
530 }
531 };
532
David Tolnayfa0edf22016-09-23 22:58:24 -0700533 ($i:expr, mut $field:ident : $e:ident >> $($rest:tt)*) => {
534 do_parse!($i, $field: call!($e) >> $($rest)*);
535 };
536
537 ($i:expr, mut $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
538 match $submac!($i, $($args)*) {
539 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700540 $crate::nom::IResult::Done(i, o) => {
David Tolnayfa0edf22016-09-23 22:58:24 -0700541 let mut $field = o;
542 do_parse!(i, $($rest)*)
543 },
544 }
545 };
David Tolnayb5a7b142016-09-13 22:46:39 -0700546}