blob: 9394232f7d8f1ea8dcd614efd5d41b3c7eac1425 [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) => {
David Tolnay0b154ea2016-10-01 15:42:50 -0700234 if $i.starts_with($tag) {
David Tolnayb5a7b142016-09-13 22:46:39 -0700235 $crate::nom::IResult::Done(&$i[$tag.len()..], &$i[0..$tag.len()])
236 } else {
237 $crate::nom::IResult::Error
238 }
239 };
240}
241
242macro_rules! switch {
243 ($i:expr, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => {
244 match $submac!($i, $($args)*) {
245 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
246 $crate::nom::IResult::Done(i, o) => match o {
247 $(
248 $p => $subrule!(i, $($args2)*),
249 )*
250 _ => $crate::nom::IResult::Error,
251 }
252 }
253 };
254}
255
256macro_rules! value {
257 ($i:expr, $res:expr) => {
258 $crate::nom::IResult::Done($i, $res)
259 };
260}
261
262macro_rules! delimited {
263 ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => {
264 match tuple_parser!($i, (), $submac!($($args)*), $($rest)*) {
265 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
266 $crate::nom::IResult::Done(i1, (_, o, _)) => $crate::nom::IResult::Done(i1, o)
267 }
268 };
269
270 ($i:expr, $f:expr, $($rest:tt)+) => {
271 delimited!($i, call!($f), $($rest)*);
272 };
273}
274
275macro_rules! separated_list {
276 ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => {{
277 let mut res = ::std::vec::Vec::new();
278 let mut input = $i;
279
280 // get the first element
281 match $submac!(input, $($args2)*) {
282 $crate::nom::IResult::Error => $crate::nom::IResult::Done(input, ::std::vec::Vec::new()),
283 $crate::nom::IResult::Done(i,o) => {
284 if i.len() == input.len() {
285 $crate::nom::IResult::Error
286 } else {
287 res.push(o);
288 input = i;
289
290 // get the separator first
291 while let $crate::nom::IResult::Done(i2, _) = $sep!(input, $($args)*) {
292 if i2.len() == input.len() {
293 break;
294 }
295
296 // get the element next
297 if let $crate::nom::IResult::Done(i3,o3) = $submac!(i2, $($args2)*) {
298 if i3.len() == i2.len() {
299 break;
300 }
301 res.push(o3);
302 input = i3;
303 } else {
304 break;
305 }
306 }
307 $crate::nom::IResult::Done(input, res)
308 }
309 }
310 }
311 }};
312
313 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
314 separated_list!($i, $submac!($($args)*), call!($g));
315 };
316
317 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
318 separated_list!($i, call!($f), $submac!($($args)*));
319 };
320
321 ($i:expr, $f:expr, $g:expr) => {
322 separated_list!($i, call!($f), call!($g));
323 };
324}
325
326macro_rules! separated_nonempty_list {
327 ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => {{
328 let mut res = ::std::vec::Vec::new();
329 let mut input = $i;
330
331 // get the first element
332 match $submac!(input, $($args2)*) {
333 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
334 $crate::nom::IResult::Done(i,o) => {
335 if i.len() == input.len() {
336 $crate::nom::IResult::Error
337 } else {
338 res.push(o);
339 input = i;
340
341 while let $crate::nom::IResult::Done(i2, _) = $sep!(input, $($args)*) {
342 if i2.len() == input.len() {
343 break;
344 }
345
346 if let $crate::nom::IResult::Done(i3,o3) = $submac!(i2, $($args2)*) {
347 if i3.len() == i2.len() {
348 break;
349 }
350 res.push(o3);
351 input = i3;
352 } else {
353 break;
354 }
355 }
356 $crate::nom::IResult::Done(input, res)
357 }
358 }
359 }
360 }};
361
362 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
363 separated_nonempty_list!($i, $submac!($($args)*), call!($g));
364 };
365
366 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
367 separated_nonempty_list!($i, call!($f), $submac!($($args)*));
368 };
369
370 ($i:expr, $f:expr, $g:expr) => {
371 separated_nonempty_list!($i, call!($f), call!($g));
372 };
373}
374
375macro_rules! tuple {
376 ($i:expr, $($rest:tt)*) => {
377 tuple_parser!($i, (), $($rest)*)
378 };
379}
380
381/// Internal parser, do not use directly
382macro_rules! tuple_parser {
383 ($i:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => {
384 tuple_parser!($i, ($($parsed),*), call!($e), $($rest)*);
385 };
386
387 ($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
388 match $submac!($i, $($args)*) {
389 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
390 $crate::nom::IResult::Done(i,o) =>
391 tuple_parser!(i, (o), $($rest)*),
392 }
393 };
394
395 ($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
396 match $submac!($i, $($args)*) {
397 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
398 $crate::nom::IResult::Done(i,o) =>
399 tuple_parser!(i, ($($parsed)* , o), $($rest)*),
400 }
401 };
402
403 ($i:expr, ($($parsed:tt),*), $e:ident) => {
404 tuple_parser!($i, ($($parsed),*), call!($e));
405 };
406
407 ($i:expr, (), $submac:ident!( $($args:tt)* )) => {
408 $submac!($i, $($args)*)
409 };
410
411 ($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => {
412 match $submac!($i, $($args)*) {
413 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
414 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, ($($parsed),*, o))
415 }
416 };
417
418 ($i:expr, ($($parsed:expr),*)) => {
419 $crate::nom::IResult::Done($i, ($($parsed),*))
420 };
421}
422
423macro_rules! alt {
424 ($i:expr, $e:ident | $($rest:tt)*) => {
425 alt!($i, call!($e) | $($rest)*);
426 };
427
428 ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => {
429 match $subrule!($i, $($args)*) {
430 res @ $crate::nom::IResult::Done(_, _) => res,
431 _ => alt!($i, $($rest)*)
432 }
433 };
434
435 ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => {
436 match $subrule!($i, $($args)*) {
437 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, $gen(o)),
438 $crate::nom::IResult::Error => alt!($i, $($rest)*)
439 }
440 };
441
442 ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => {
443 alt!($i, call!($e) => { $gen } | $($rest)*);
444 };
445
446 ($i:expr, $e:ident => { $gen:expr }) => {
447 alt!($i, call!($e) => { $gen });
448 };
449
450 ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => {
451 match $subrule!($i, $($args)*) {
452 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, $gen(o)),
453 $crate::nom::IResult::Error => alt!($i)
454 }
455 };
456
457 ($i:expr, $e:ident) => {
458 alt!($i, call!($e));
459 };
460
461 ($i:expr, $subrule:ident!( $($args:tt)*)) => {
462 match $subrule!( $i, $($args)* ) {
463 $crate::nom::IResult::Done(i,o) => $crate::nom::IResult::Done(i,o),
464 $crate::nom::IResult::Error => alt!($i),
465 }
466 };
467
468 ($i:expr) => {
469 $crate::nom::IResult::Error
470 };
471}
472
473macro_rules! do_parse {
474 ($i:expr, ( $($rest:expr),* )) => {
475 $crate::nom::IResult::Done($i, ( $($rest),* ))
476 };
477
478 ($i:expr, $e:ident >> $($rest:tt)*) => {
479 do_parse!($i, call!($e) >> $($rest)*);
480 };
481
482 ($i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
483 match $submac!($i, $($args)*) {
484 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
485 $crate::nom::IResult::Done(i, _) =>
486 do_parse!(i, $($rest)*),
487 }
488 };
489
490 ($i:expr, $field:ident : $e:ident >> $($rest:tt)*) => {
491 do_parse!($i, $field: call!($e) >> $($rest)*);
492 };
493
494 ($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
495 match $submac!($i, $($args)*) {
496 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
497 $crate::nom::IResult::Done(i,o) => {
498 let $field = o;
499 do_parse!(i, $($rest)*)
500 },
501 }
502 };
503
David Tolnayfa0edf22016-09-23 22:58:24 -0700504 ($i:expr, mut $field:ident : $e:ident >> $($rest:tt)*) => {
505 do_parse!($i, $field: call!($e) >> $($rest)*);
506 };
507
508 ($i:expr, mut $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
509 match $submac!($i, $($args)*) {
510 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
511 $crate::nom::IResult::Done(i,o) => {
512 let mut $field = o;
513 do_parse!(i, $($rest)*)
514 },
515 }
516 };
517
David Tolnayb5a7b142016-09-13 22:46:39 -0700518 // ending the chain
519 ($i:expr, $e:ident >> ( $($rest:tt)* )) => {
520 do_parse!($i, call!($e) >> ( $($rest)* ));
521 };
522
523 ($i:expr, $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => {
524 match $submac!($i, $($args)*) {
525 $crate::nom::IResult::Done(i, _) => $crate::nom::IResult::Done(i, ($($rest)*)),
526 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
527 }
528 };
529
530 ($i:expr, $field:ident : $e:ident >> ( $($rest:tt)* )) => {
531 do_parse!($i, $field: call!($e) >> ( $($rest)* ) );
532 };
533
534 ($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => {
535 match $submac!($i, $($args)*) {
536 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
537 $crate::nom::IResult::Done(i,o) => {
538 let $field = o;
539 $crate::nom::IResult::Done(i, ($($rest)*))
540 },
541 }
542 };
543}