blob: 8586ff333be6e83d5df17cb2b708f5356d0ee2eb [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 Tolnayc7f646a2016-10-16 10:54:39 -0700161pub fn many0<'a, T>(mut input: &'a str,
162 f: fn(&'a str) -> IResult<&'a str, T>)
163 -> IResult<&'a str, Vec<T>> {
David Tolnaybc84d5a2016-10-08 13:20:57 -0700164 let mut res = Vec::new();
165
166 loop {
167 if input.is_empty() {
168 return IResult::Done(input, res);
169 }
170
171 match f(input) {
172 IResult::Error => {
173 return IResult::Done(input, res);
174 }
175 IResult::Done(i, o) => {
176 // loop trip must always consume (otherwise infinite loops)
177 if i.len() == input.len() {
178 return IResult::Error;
179 }
180
181 res.push(o);
182 input = i;
183 }
184 }
185 }
186}
187
David Tolnayb5a7b142016-09-13 22:46:39 -0700188macro_rules! peek {
189 ($i:expr, $submac:ident!( $($args:tt)* )) => {
190 match $submac!($i, $($args)*) {
191 $crate::nom::IResult::Done(_, o) => $crate::nom::IResult::Done($i, o),
192 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
193 }
194 };
195}
196
197macro_rules! take_while1 {
198 ($input:expr, $submac:ident!( $($args:tt)* )) => {{
199 let mut offset = $input.len();
200 for (o, c) in $input.char_indices() {
201 if !$submac!(c, $($args)*) {
202 offset = o;
203 break;
204 }
205 }
206 if offset == 0 {
207 $crate::nom::IResult::Error
208 } else if offset < $input.len() {
209 $crate::nom::IResult::Done(&$input[offset..], &$input[..offset])
210 } else {
211 $crate::nom::IResult::Done("", $input)
212 }
213 }};
214
215 ($input:expr, $f:expr) => {
216 take_while1!($input, call!($f));
217 };
218}
219
David Tolnay62604d92016-09-30 14:47:48 -0700220pub fn str_chars(s: &str) -> Vec<char> {
221 // Can't do `s.chars().collect()` because it triggers a compiler bug in 1.12.0
222 // https://github.com/dtolnay/syn/issues/20
223 let mut result = Vec::new();
224 for ch in s.chars() {
225 result.push(ch);
226 }
227 result
228}
229
David Tolnayb5a7b142016-09-13 22:46:39 -0700230macro_rules! take_until {
231 ($input:expr, $substr:expr) => {{
232 if $substr.len() > $input.len() {
233 $crate::nom::IResult::Error
234 } else {
David Tolnay62604d92016-09-30 14:47:48 -0700235 let substr_vec: Vec<char> = $crate::nom::str_chars($substr);
David Tolnayb5a7b142016-09-13 22:46:39 -0700236 let mut window: Vec<char> = vec![];
237 let mut offset = $input.len();
238 let mut parsed = false;
239 for (o, c) in $input.char_indices() {
240 window.push(c);
241 if window.len() > substr_vec.len() {
242 window.remove(0);
243 }
244 if window == substr_vec {
245 parsed = true;
246 window.pop();
247 let window_len: usize = window.iter()
248 .map(|x| x.len_utf8())
249 .fold(0, |x, y| x + y);
250 offset = o - window_len;
251 break;
252 }
253 }
254 if parsed {
255 $crate::nom::IResult::Done(&$input[offset..], &$input[..offset])
256 } else {
257 $crate::nom::IResult::Error
258 }
259 }
260 }};
261}
262
263macro_rules! tag {
264 ($i:expr, $tag: expr) => {
David Tolnay0b154ea2016-10-01 15:42:50 -0700265 if $i.starts_with($tag) {
David Tolnayb5a7b142016-09-13 22:46:39 -0700266 $crate::nom::IResult::Done(&$i[$tag.len()..], &$i[0..$tag.len()])
267 } else {
268 $crate::nom::IResult::Error
269 }
270 };
271}
272
273macro_rules! switch {
274 ($i:expr, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => {
275 match $submac!($i, $($args)*) {
276 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
277 $crate::nom::IResult::Done(i, o) => match o {
278 $(
279 $p => $subrule!(i, $($args2)*),
280 )*
281 _ => $crate::nom::IResult::Error,
282 }
283 }
284 };
285}
286
287macro_rules! value {
288 ($i:expr, $res:expr) => {
289 $crate::nom::IResult::Done($i, $res)
290 };
291}
292
293macro_rules! delimited {
294 ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => {
295 match tuple_parser!($i, (), $submac!($($args)*), $($rest)*) {
296 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
297 $crate::nom::IResult::Done(i1, (_, o, _)) => $crate::nom::IResult::Done(i1, o)
298 }
299 };
300
301 ($i:expr, $f:expr, $($rest:tt)+) => {
302 delimited!($i, call!($f), $($rest)*);
303 };
304}
305
David Tolnayb5a7b142016-09-13 22:46:39 -0700306macro_rules! separated_nonempty_list {
307 ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => {{
308 let mut res = ::std::vec::Vec::new();
309 let mut input = $i;
310
311 // get the first element
312 match $submac!(input, $($args2)*) {
313 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700314 $crate::nom::IResult::Done(i, o) => {
David Tolnayb5a7b142016-09-13 22:46:39 -0700315 if i.len() == input.len() {
316 $crate::nom::IResult::Error
317 } else {
318 res.push(o);
319 input = i;
320
321 while let $crate::nom::IResult::Done(i2, _) = $sep!(input, $($args)*) {
322 if i2.len() == input.len() {
323 break;
324 }
325
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_nonempty_list!($i, $submac!($($args)*), call!($g));
344 };
345
346 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
347 separated_nonempty_list!($i, call!($f), $submac!($($args)*));
348 };
349
350 ($i:expr, $f:expr, $g:expr) => {
351 separated_nonempty_list!($i, call!($f), call!($g));
352 };
353}
354
355macro_rules! tuple {
356 ($i:expr, $($rest:tt)*) => {
357 tuple_parser!($i, (), $($rest)*)
358 };
359}
360
361/// Internal parser, do not use directly
362macro_rules! tuple_parser {
363 ($i:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => {
364 tuple_parser!($i, ($($parsed),*), call!($e), $($rest)*);
365 };
366
367 ($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
368 match $submac!($i, $($args)*) {
369 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700370 $crate::nom::IResult::Done(i, o) =>
David Tolnayb5a7b142016-09-13 22:46:39 -0700371 tuple_parser!(i, (o), $($rest)*),
372 }
373 };
374
375 ($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
376 match $submac!($i, $($args)*) {
377 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700378 $crate::nom::IResult::Done(i, o) =>
David Tolnayb5a7b142016-09-13 22:46:39 -0700379 tuple_parser!(i, ($($parsed)* , o), $($rest)*),
380 }
381 };
382
383 ($i:expr, ($($parsed:tt),*), $e:ident) => {
384 tuple_parser!($i, ($($parsed),*), call!($e));
385 };
386
387 ($i:expr, (), $submac:ident!( $($args:tt)* )) => {
388 $submac!($i, $($args)*)
389 };
390
391 ($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => {
392 match $submac!($i, $($args)*) {
393 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
394 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, ($($parsed),*, o))
395 }
396 };
397
398 ($i:expr, ($($parsed:expr),*)) => {
399 $crate::nom::IResult::Done($i, ($($parsed),*))
400 };
401}
402
403macro_rules! alt {
404 ($i:expr, $e:ident | $($rest:tt)*) => {
405 alt!($i, call!($e) | $($rest)*);
406 };
407
408 ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => {
409 match $subrule!($i, $($args)*) {
410 res @ $crate::nom::IResult::Done(_, _) => res,
411 _ => alt!($i, $($rest)*)
412 }
413 };
414
415 ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => {
416 match $subrule!($i, $($args)*) {
417 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, $gen(o)),
418 $crate::nom::IResult::Error => alt!($i, $($rest)*)
419 }
420 };
421
422 ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => {
423 alt!($i, call!($e) => { $gen } | $($rest)*);
424 };
425
426 ($i:expr, $e:ident => { $gen:expr }) => {
427 alt!($i, call!($e) => { $gen });
428 };
429
430 ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => {
431 match $subrule!($i, $($args)*) {
432 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, $gen(o)),
433 $crate::nom::IResult::Error => alt!($i)
434 }
435 };
436
437 ($i:expr, $e:ident) => {
438 alt!($i, call!($e));
439 };
440
441 ($i:expr, $subrule:ident!( $($args:tt)*)) => {
442 match $subrule!( $i, $($args)* ) {
David Tolnayde955482016-10-08 13:10:27 -0700443 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, o),
David Tolnayb5a7b142016-09-13 22:46:39 -0700444 $crate::nom::IResult::Error => alt!($i),
445 }
446 };
447
448 ($i:expr) => {
449 $crate::nom::IResult::Error
450 };
451}
452
453macro_rules! do_parse {
454 ($i:expr, ( $($rest:expr),* )) => {
455 $crate::nom::IResult::Done($i, ( $($rest),* ))
456 };
457
458 ($i:expr, $e:ident >> $($rest:tt)*) => {
459 do_parse!($i, call!($e) >> $($rest)*);
460 };
461
462 ($i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
463 match $submac!($i, $($args)*) {
464 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
465 $crate::nom::IResult::Done(i, _) =>
466 do_parse!(i, $($rest)*),
467 }
468 };
469
470 ($i:expr, $field:ident : $e:ident >> $($rest:tt)*) => {
471 do_parse!($i, $field: call!($e) >> $($rest)*);
472 };
473
474 ($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
475 match $submac!($i, $($args)*) {
476 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700477 $crate::nom::IResult::Done(i, o) => {
David Tolnayb5a7b142016-09-13 22:46:39 -0700478 let $field = o;
479 do_parse!(i, $($rest)*)
480 },
481 }
482 };
483
David Tolnayfa0edf22016-09-23 22:58:24 -0700484 ($i:expr, mut $field:ident : $e:ident >> $($rest:tt)*) => {
485 do_parse!($i, $field: call!($e) >> $($rest)*);
486 };
487
488 ($i:expr, mut $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
489 match $submac!($i, $($args)*) {
490 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700491 $crate::nom::IResult::Done(i, o) => {
David Tolnayfa0edf22016-09-23 22:58:24 -0700492 let mut $field = o;
493 do_parse!(i, $($rest)*)
494 },
495 }
496 };
David Tolnayb5a7b142016-09-13 22:46:39 -0700497}