blob: d4003126f0e6ad25e37b173c596084484c4a002c [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)*) {
68 $crate::nom::IResult::Done(i,o) => $crate::nom::IResult::Done(i, ::std::option::Option::Some(o)),
69 $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)
142 if i == input {
143 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) => {
157 many0!($i, call!($f));
158 };
159}
160
161macro_rules! peek {
162 ($i:expr, $submac:ident!( $($args:tt)* )) => {
163 match $submac!($i, $($args)*) {
164 $crate::nom::IResult::Done(_, o) => $crate::nom::IResult::Done($i, o),
165 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
166 }
167 };
168}
169
170macro_rules! take_while1 {
171 ($input:expr, $submac:ident!( $($args:tt)* )) => {{
172 let mut offset = $input.len();
173 for (o, c) in $input.char_indices() {
174 if !$submac!(c, $($args)*) {
175 offset = o;
176 break;
177 }
178 }
179 if offset == 0 {
180 $crate::nom::IResult::Error
181 } else if offset < $input.len() {
182 $crate::nom::IResult::Done(&$input[offset..], &$input[..offset])
183 } else {
184 $crate::nom::IResult::Done("", $input)
185 }
186 }};
187
188 ($input:expr, $f:expr) => {
189 take_while1!($input, call!($f));
190 };
191}
192
David Tolnay62604d92016-09-30 14:47:48 -0700193pub fn str_chars(s: &str) -> Vec<char> {
194 // Can't do `s.chars().collect()` because it triggers a compiler bug in 1.12.0
195 // https://github.com/dtolnay/syn/issues/20
196 let mut result = Vec::new();
197 for ch in s.chars() {
198 result.push(ch);
199 }
200 result
201}
202
David Tolnayb5a7b142016-09-13 22:46:39 -0700203macro_rules! take_until {
204 ($input:expr, $substr:expr) => {{
205 if $substr.len() > $input.len() {
206 $crate::nom::IResult::Error
207 } else {
David Tolnay62604d92016-09-30 14:47:48 -0700208 let substr_vec: Vec<char> = $crate::nom::str_chars($substr);
David Tolnayb5a7b142016-09-13 22:46:39 -0700209 let mut window: Vec<char> = vec![];
210 let mut offset = $input.len();
211 let mut parsed = false;
212 for (o, c) in $input.char_indices() {
213 window.push(c);
214 if window.len() > substr_vec.len() {
215 window.remove(0);
216 }
217 if window == substr_vec {
218 parsed = true;
219 window.pop();
220 let window_len: usize = window.iter()
221 .map(|x| x.len_utf8())
222 .fold(0, |x, y| x + y);
223 offset = o - window_len;
224 break;
225 }
226 }
227 if parsed {
228 $crate::nom::IResult::Done(&$input[offset..], &$input[..offset])
229 } else {
230 $crate::nom::IResult::Error
231 }
232 }
233 }};
234}
235
236macro_rules! tag {
237 ($i:expr, $tag: expr) => {
David Tolnay0b154ea2016-10-01 15:42:50 -0700238 if $i.starts_with($tag) {
David Tolnayb5a7b142016-09-13 22:46:39 -0700239 $crate::nom::IResult::Done(&$i[$tag.len()..], &$i[0..$tag.len()])
240 } else {
241 $crate::nom::IResult::Error
242 }
243 };
244}
245
246macro_rules! switch {
247 ($i:expr, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => {
248 match $submac!($i, $($args)*) {
249 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
250 $crate::nom::IResult::Done(i, o) => match o {
251 $(
252 $p => $subrule!(i, $($args2)*),
253 )*
254 _ => $crate::nom::IResult::Error,
255 }
256 }
257 };
258}
259
260macro_rules! value {
261 ($i:expr, $res:expr) => {
262 $crate::nom::IResult::Done($i, $res)
263 };
264}
265
266macro_rules! delimited {
267 ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => {
268 match tuple_parser!($i, (), $submac!($($args)*), $($rest)*) {
269 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
270 $crate::nom::IResult::Done(i1, (_, o, _)) => $crate::nom::IResult::Done(i1, o)
271 }
272 };
273
274 ($i:expr, $f:expr, $($rest:tt)+) => {
275 delimited!($i, call!($f), $($rest)*);
276 };
277}
278
279macro_rules! separated_list {
280 ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => {{
281 let mut res = ::std::vec::Vec::new();
282 let mut input = $i;
283
284 // get the first element
285 match $submac!(input, $($args2)*) {
286 $crate::nom::IResult::Error => $crate::nom::IResult::Done(input, ::std::vec::Vec::new()),
287 $crate::nom::IResult::Done(i,o) => {
288 if i.len() == input.len() {
289 $crate::nom::IResult::Error
290 } else {
291 res.push(o);
292 input = i;
293
294 // get the separator first
295 while let $crate::nom::IResult::Done(i2, _) = $sep!(input, $($args)*) {
296 if i2.len() == input.len() {
297 break;
298 }
299
300 // get the element next
301 if let $crate::nom::IResult::Done(i3,o3) = $submac!(i2, $($args2)*) {
302 if i3.len() == i2.len() {
303 break;
304 }
305 res.push(o3);
306 input = i3;
307 } else {
308 break;
309 }
310 }
311 $crate::nom::IResult::Done(input, res)
312 }
313 }
314 }
315 }};
316
317 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
318 separated_list!($i, $submac!($($args)*), call!($g));
319 };
320
321 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
322 separated_list!($i, call!($f), $submac!($($args)*));
323 };
324
325 ($i:expr, $f:expr, $g:expr) => {
326 separated_list!($i, call!($f), call!($g));
327 };
328}
329
330macro_rules! separated_nonempty_list {
331 ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => {{
332 let mut res = ::std::vec::Vec::new();
333 let mut input = $i;
334
335 // get the first element
336 match $submac!(input, $($args2)*) {
337 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
338 $crate::nom::IResult::Done(i,o) => {
339 if i.len() == input.len() {
340 $crate::nom::IResult::Error
341 } else {
342 res.push(o);
343 input = i;
344
345 while let $crate::nom::IResult::Done(i2, _) = $sep!(input, $($args)*) {
346 if i2.len() == input.len() {
347 break;
348 }
349
350 if let $crate::nom::IResult::Done(i3,o3) = $submac!(i2, $($args2)*) {
351 if i3.len() == i2.len() {
352 break;
353 }
354 res.push(o3);
355 input = i3;
356 } else {
357 break;
358 }
359 }
360 $crate::nom::IResult::Done(input, res)
361 }
362 }
363 }
364 }};
365
366 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
367 separated_nonempty_list!($i, $submac!($($args)*), call!($g));
368 };
369
370 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
371 separated_nonempty_list!($i, call!($f), $submac!($($args)*));
372 };
373
374 ($i:expr, $f:expr, $g:expr) => {
375 separated_nonempty_list!($i, call!($f), call!($g));
376 };
377}
378
379macro_rules! tuple {
380 ($i:expr, $($rest:tt)*) => {
381 tuple_parser!($i, (), $($rest)*)
382 };
383}
384
385/// Internal parser, do not use directly
386macro_rules! tuple_parser {
387 ($i:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => {
388 tuple_parser!($i, ($($parsed),*), call!($e), $($rest)*);
389 };
390
391 ($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
392 match $submac!($i, $($args)*) {
393 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
394 $crate::nom::IResult::Done(i,o) =>
395 tuple_parser!(i, (o), $($rest)*),
396 }
397 };
398
399 ($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
400 match $submac!($i, $($args)*) {
401 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
402 $crate::nom::IResult::Done(i,o) =>
403 tuple_parser!(i, ($($parsed)* , o), $($rest)*),
404 }
405 };
406
407 ($i:expr, ($($parsed:tt),*), $e:ident) => {
408 tuple_parser!($i, ($($parsed),*), call!($e));
409 };
410
411 ($i:expr, (), $submac:ident!( $($args:tt)* )) => {
412 $submac!($i, $($args)*)
413 };
414
415 ($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => {
416 match $submac!($i, $($args)*) {
417 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
418 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, ($($parsed),*, o))
419 }
420 };
421
422 ($i:expr, ($($parsed:expr),*)) => {
423 $crate::nom::IResult::Done($i, ($($parsed),*))
424 };
425}
426
427macro_rules! alt {
428 ($i:expr, $e:ident | $($rest:tt)*) => {
429 alt!($i, call!($e) | $($rest)*);
430 };
431
432 ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => {
433 match $subrule!($i, $($args)*) {
434 res @ $crate::nom::IResult::Done(_, _) => res,
435 _ => alt!($i, $($rest)*)
436 }
437 };
438
439 ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => {
440 match $subrule!($i, $($args)*) {
441 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, $gen(o)),
442 $crate::nom::IResult::Error => alt!($i, $($rest)*)
443 }
444 };
445
446 ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => {
447 alt!($i, call!($e) => { $gen } | $($rest)*);
448 };
449
450 ($i:expr, $e:ident => { $gen:expr }) => {
451 alt!($i, call!($e) => { $gen });
452 };
453
454 ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => {
455 match $subrule!($i, $($args)*) {
456 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, $gen(o)),
457 $crate::nom::IResult::Error => alt!($i)
458 }
459 };
460
461 ($i:expr, $e:ident) => {
462 alt!($i, call!($e));
463 };
464
465 ($i:expr, $subrule:ident!( $($args:tt)*)) => {
466 match $subrule!( $i, $($args)* ) {
467 $crate::nom::IResult::Done(i,o) => $crate::nom::IResult::Done(i,o),
468 $crate::nom::IResult::Error => alt!($i),
469 }
470 };
471
472 ($i:expr) => {
473 $crate::nom::IResult::Error
474 };
475}
476
477macro_rules! do_parse {
478 ($i:expr, ( $($rest:expr),* )) => {
479 $crate::nom::IResult::Done($i, ( $($rest),* ))
480 };
481
482 ($i:expr, $e:ident >> $($rest:tt)*) => {
483 do_parse!($i, call!($e) >> $($rest)*);
484 };
485
486 ($i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
487 match $submac!($i, $($args)*) {
488 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
489 $crate::nom::IResult::Done(i, _) =>
490 do_parse!(i, $($rest)*),
491 }
492 };
493
494 ($i:expr, $field:ident : $e:ident >> $($rest:tt)*) => {
495 do_parse!($i, $field: call!($e) >> $($rest)*);
496 };
497
498 ($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
499 match $submac!($i, $($args)*) {
500 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
501 $crate::nom::IResult::Done(i,o) => {
502 let $field = o;
503 do_parse!(i, $($rest)*)
504 },
505 }
506 };
507
David Tolnayfa0edf22016-09-23 22:58:24 -0700508 ($i:expr, mut $field:ident : $e:ident >> $($rest:tt)*) => {
509 do_parse!($i, $field: call!($e) >> $($rest)*);
510 };
511
512 ($i:expr, mut $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
513 match $submac!($i, $($args)*) {
514 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
515 $crate::nom::IResult::Done(i,o) => {
516 let mut $field = o;
517 do_parse!(i, $($rest)*)
518 },
519 }
520 };
521
David Tolnayb5a7b142016-09-13 22:46:39 -0700522 // ending the chain
523 ($i:expr, $e:ident >> ( $($rest:tt)* )) => {
524 do_parse!($i, call!($e) >> ( $($rest)* ));
525 };
526
527 ($i:expr, $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => {
528 match $submac!($i, $($args)*) {
529 $crate::nom::IResult::Done(i, _) => $crate::nom::IResult::Done(i, ($($rest)*)),
530 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
531 }
532 };
533
534 ($i:expr, $field:ident : $e:ident >> ( $($rest:tt)* )) => {
535 do_parse!($i, $field: call!($e) >> ( $($rest)* ) );
536 };
537
538 ($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => {
539 match $submac!($i, $($args)*) {
540 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
541 $crate::nom::IResult::Done(i,o) => {
542 let $field = o;
543 $crate::nom::IResult::Done(i, ($($rest)*))
544 },
545 }
546 };
547}