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