blob: c20fb68fd6db1fc6373a40a1fb0bea2b112e5f75 [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 {
David Tolnayaf2557e2016-10-24 11:52:21 -070028 ($i:expr, $fun:expr $(, $args:expr)*) => {
29 $fun($i $(, $args)*)
David Tolnayb5a7b142016-09-13 22:46:39 -070030 };
31}
32
33macro_rules! map {
34 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -070035 map_impl!($i, $submac!($($args)*), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -070036 };
37
38 ($i:expr, $f:expr, $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -070039 map_impl!($i, call!($f), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -070040 };
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) => {
David Tolnayb81c7a42016-10-25 10:12:12 -070077 cond!($i, $cond, call!($f))
David Tolnaycfe55022016-10-02 22:02:27 -070078 };
David Tolnayb5a7b142016-09-13 22:46:39 -070079}
80
David Tolnayaf2557e2016-10-24 11:52:21 -070081macro_rules! cond_reduce {
82 ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => {
83 if $cond {
84 $submac!($i, $($args)*)
85 } else {
86 $crate::nom::IResult::Error
87 }
88 };
89
90 ($i:expr, $cond:expr, $f:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -070091 cond_reduce!($i, $cond, call!($f))
David Tolnayaf2557e2016-10-24 11:52:21 -070092 };
93}
94
David Tolnayb5a7b142016-09-13 22:46:39 -070095macro_rules! preceded {
96 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => {
97 match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) {
98 $crate::nom::IResult::Done(remaining, (_, o)) => $crate::nom::IResult::Done(remaining, o),
99 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
100 }
101 };
102
103 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700104 preceded!($i, $submac!($($args)*), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -0700105 };
106
107 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700108 preceded!($i, call!($f), $submac!($($args)*))
David Tolnayb5a7b142016-09-13 22:46:39 -0700109 };
110
111 ($i:expr, $f:expr, $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700112 preceded!($i, call!($f), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -0700113 };
114}
115
116macro_rules! terminated {
117 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => {
118 match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) {
119 $crate::nom::IResult::Done(remaining, (o, _)) => $crate::nom::IResult::Done(remaining, o),
120 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
121 }
122 };
123
124 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700125 terminated!($i, $submac!($($args)*), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -0700126 };
127
128 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700129 terminated!($i, call!($f), $submac!($($args)*))
David Tolnayb5a7b142016-09-13 22:46:39 -0700130 };
131
132 ($i:expr, $f:expr, $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700133 terminated!($i, call!($f), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -0700134 };
135}
136
137macro_rules! many0 {
138 ($i:expr, $submac:ident!( $($args:tt)* )) => {{
139 let ret;
140 let mut res = ::std::vec::Vec::new();
141 let mut input = $i;
142
143 loop {
144 if input.is_empty() {
145 ret = $crate::nom::IResult::Done(input, res);
146 break;
147 }
148
149 match $submac!(input, $($args)*) {
150 $crate::nom::IResult::Error => {
151 ret = $crate::nom::IResult::Done(input, res);
152 break;
153 }
154 $crate::nom::IResult::Done(i, o) => {
155 // loop trip must always consume (otherwise infinite loops)
David Tolnaybc84d5a2016-10-08 13:20:57 -0700156 if i.len() == input.len() {
David Tolnayb5a7b142016-09-13 22:46:39 -0700157 ret = $crate::nom::IResult::Error;
158 break;
159 }
160
161 res.push(o);
162 input = i;
163 }
164 }
165 }
166
167 ret
168 }};
169
170 ($i:expr, $f:expr) => {
David Tolnaybc84d5a2016-10-08 13:20:57 -0700171 $crate::nom::many0($i, $f)
David Tolnayb5a7b142016-09-13 22:46:39 -0700172 };
173}
174
David Tolnayc7f646a2016-10-16 10:54:39 -0700175pub fn many0<'a, T>(mut input: &'a str,
176 f: fn(&'a str) -> IResult<&'a str, T>)
177 -> IResult<&'a str, Vec<T>> {
David Tolnaybc84d5a2016-10-08 13:20:57 -0700178 let mut res = Vec::new();
179
180 loop {
181 if input.is_empty() {
182 return IResult::Done(input, res);
183 }
184
185 match f(input) {
186 IResult::Error => {
187 return IResult::Done(input, res);
188 }
189 IResult::Done(i, o) => {
190 // loop trip must always consume (otherwise infinite loops)
191 if i.len() == input.len() {
192 return IResult::Error;
193 }
194
195 res.push(o);
196 input = i;
197 }
198 }
199 }
200}
201
David Tolnayb5a7b142016-09-13 22:46:39 -0700202macro_rules! peek {
203 ($i:expr, $submac:ident!( $($args:tt)* )) => {
204 match $submac!($i, $($args)*) {
205 $crate::nom::IResult::Done(_, o) => $crate::nom::IResult::Done($i, o),
206 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
207 }
208 };
209}
210
211macro_rules! take_while1 {
212 ($input:expr, $submac:ident!( $($args:tt)* )) => {{
213 let mut offset = $input.len();
214 for (o, c) in $input.char_indices() {
215 if !$submac!(c, $($args)*) {
216 offset = o;
217 break;
218 }
219 }
220 if offset == 0 {
221 $crate::nom::IResult::Error
222 } else if offset < $input.len() {
223 $crate::nom::IResult::Done(&$input[offset..], &$input[..offset])
224 } else {
225 $crate::nom::IResult::Done("", $input)
226 }
227 }};
228
229 ($input:expr, $f:expr) => {
230 take_while1!($input, call!($f));
231 };
232}
233
David Tolnay62604d92016-09-30 14:47:48 -0700234pub fn str_chars(s: &str) -> Vec<char> {
235 // Can't do `s.chars().collect()` because it triggers a compiler bug in 1.12.0
236 // https://github.com/dtolnay/syn/issues/20
237 let mut result = Vec::new();
238 for ch in s.chars() {
239 result.push(ch);
240 }
241 result
242}
243
David Tolnayb5a7b142016-09-13 22:46:39 -0700244macro_rules! take_until {
245 ($input:expr, $substr:expr) => {{
246 if $substr.len() > $input.len() {
247 $crate::nom::IResult::Error
248 } else {
David Tolnay62604d92016-09-30 14:47:48 -0700249 let substr_vec: Vec<char> = $crate::nom::str_chars($substr);
David Tolnayb5a7b142016-09-13 22:46:39 -0700250 let mut window: Vec<char> = vec![];
251 let mut offset = $input.len();
252 let mut parsed = false;
253 for (o, c) in $input.char_indices() {
254 window.push(c);
255 if window.len() > substr_vec.len() {
256 window.remove(0);
257 }
258 if window == substr_vec {
259 parsed = true;
260 window.pop();
261 let window_len: usize = window.iter()
262 .map(|x| x.len_utf8())
263 .fold(0, |x, y| x + y);
264 offset = o - window_len;
265 break;
266 }
267 }
268 if parsed {
269 $crate::nom::IResult::Done(&$input[offset..], &$input[..offset])
270 } else {
271 $crate::nom::IResult::Error
272 }
273 }
274 }};
275}
276
277macro_rules! tag {
278 ($i:expr, $tag: expr) => {
David Tolnay0b154ea2016-10-01 15:42:50 -0700279 if $i.starts_with($tag) {
David Tolnayb5a7b142016-09-13 22:46:39 -0700280 $crate::nom::IResult::Done(&$i[$tag.len()..], &$i[0..$tag.len()])
281 } else {
282 $crate::nom::IResult::Error
283 }
284 };
285}
286
287macro_rules! switch {
288 ($i:expr, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => {
289 match $submac!($i, $($args)*) {
290 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
291 $crate::nom::IResult::Done(i, o) => match o {
292 $(
293 $p => $subrule!(i, $($args2)*),
294 )*
295 _ => $crate::nom::IResult::Error,
296 }
297 }
298 };
299}
300
301macro_rules! value {
302 ($i:expr, $res:expr) => {
303 $crate::nom::IResult::Done($i, $res)
304 };
305}
306
307macro_rules! delimited {
308 ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => {
309 match tuple_parser!($i, (), $submac!($($args)*), $($rest)*) {
310 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
311 $crate::nom::IResult::Done(i1, (_, o, _)) => $crate::nom::IResult::Done(i1, o)
312 }
313 };
314
315 ($i:expr, $f:expr, $($rest:tt)+) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700316 delimited!($i, call!($f), $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700317 };
318}
319
David Tolnayb5a7b142016-09-13 22:46:39 -0700320macro_rules! separated_nonempty_list {
321 ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => {{
322 let mut res = ::std::vec::Vec::new();
323 let mut input = $i;
324
325 // get the first element
326 match $submac!(input, $($args2)*) {
327 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700328 $crate::nom::IResult::Done(i, o) => {
David Tolnayb5a7b142016-09-13 22:46:39 -0700329 if i.len() == input.len() {
330 $crate::nom::IResult::Error
331 } else {
332 res.push(o);
333 input = i;
334
335 while let $crate::nom::IResult::Done(i2, _) = $sep!(input, $($args)*) {
336 if i2.len() == input.len() {
337 break;
338 }
339
David Tolnayde955482016-10-08 13:10:27 -0700340 if let $crate::nom::IResult::Done(i3, o3) = $submac!(i2, $($args2)*) {
David Tolnayb5a7b142016-09-13 22:46:39 -0700341 if i3.len() == i2.len() {
342 break;
343 }
344 res.push(o3);
345 input = i3;
346 } else {
347 break;
348 }
349 }
350 $crate::nom::IResult::Done(input, res)
351 }
352 }
353 }
354 }};
355
356 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700357 separated_nonempty_list!($i, $submac!($($args)*), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -0700358 };
359
360 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700361 separated_nonempty_list!($i, call!($f), $submac!($($args)*))
David Tolnayb5a7b142016-09-13 22:46:39 -0700362 };
363
364 ($i:expr, $f:expr, $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700365 separated_nonempty_list!($i, call!($f), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -0700366 };
367}
368
369macro_rules! tuple {
370 ($i:expr, $($rest:tt)*) => {
371 tuple_parser!($i, (), $($rest)*)
372 };
373}
374
375/// Internal parser, do not use directly
376macro_rules! tuple_parser {
377 ($i:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700378 tuple_parser!($i, ($($parsed),*), call!($e), $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700379 };
380
381 ($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
382 match $submac!($i, $($args)*) {
383 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700384 $crate::nom::IResult::Done(i, o) =>
David Tolnayb5a7b142016-09-13 22:46:39 -0700385 tuple_parser!(i, (o), $($rest)*),
386 }
387 };
388
389 ($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
390 match $submac!($i, $($args)*) {
391 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700392 $crate::nom::IResult::Done(i, o) =>
David Tolnayb5a7b142016-09-13 22:46:39 -0700393 tuple_parser!(i, ($($parsed)* , o), $($rest)*),
394 }
395 };
396
397 ($i:expr, ($($parsed:tt),*), $e:ident) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700398 tuple_parser!($i, ($($parsed),*), call!($e))
David Tolnayb5a7b142016-09-13 22:46:39 -0700399 };
400
401 ($i:expr, (), $submac:ident!( $($args:tt)* )) => {
402 $submac!($i, $($args)*)
403 };
404
405 ($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => {
406 match $submac!($i, $($args)*) {
407 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
408 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, ($($parsed),*, o))
409 }
410 };
411
412 ($i:expr, ($($parsed:expr),*)) => {
413 $crate::nom::IResult::Done($i, ($($parsed),*))
414 };
415}
416
417macro_rules! alt {
418 ($i:expr, $e:ident | $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700419 alt!($i, call!($e) | $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700420 };
421
422 ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => {
423 match $subrule!($i, $($args)*) {
424 res @ $crate::nom::IResult::Done(_, _) => res,
425 _ => alt!($i, $($rest)*)
426 }
427 };
428
429 ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => {
430 match $subrule!($i, $($args)*) {
431 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, $gen(o)),
432 $crate::nom::IResult::Error => alt!($i, $($rest)*)
433 }
434 };
435
436 ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700437 alt!($i, call!($e) => { $gen } | $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700438 };
439
440 ($i:expr, $e:ident => { $gen:expr }) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700441 alt!($i, call!($e) => { $gen })
David Tolnayb5a7b142016-09-13 22:46:39 -0700442 };
443
444 ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => {
445 match $subrule!($i, $($args)*) {
446 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, $gen(o)),
David Tolnay5377b172016-10-25 01:13:12 -0700447 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayb5a7b142016-09-13 22:46:39 -0700448 }
449 };
450
451 ($i:expr, $e:ident) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700452 alt!($i, call!($e))
David Tolnayb5a7b142016-09-13 22:46:39 -0700453 };
454
455 ($i:expr, $subrule:ident!( $($args:tt)*)) => {
David Tolnay5377b172016-10-25 01:13:12 -0700456 $subrule!($i, $($args)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700457 };
458}
459
460macro_rules! do_parse {
461 ($i:expr, ( $($rest:expr),* )) => {
462 $crate::nom::IResult::Done($i, ( $($rest),* ))
463 };
464
465 ($i:expr, $e:ident >> $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700466 do_parse!($i, call!($e) >> $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700467 };
468
469 ($i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
470 match $submac!($i, $($args)*) {
471 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
472 $crate::nom::IResult::Done(i, _) =>
473 do_parse!(i, $($rest)*),
474 }
475 };
476
477 ($i:expr, $field:ident : $e:ident >> $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700478 do_parse!($i, $field: call!($e) >> $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700479 };
480
481 ($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
482 match $submac!($i, $($args)*) {
483 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700484 $crate::nom::IResult::Done(i, o) => {
David Tolnayb5a7b142016-09-13 22:46:39 -0700485 let $field = o;
486 do_parse!(i, $($rest)*)
487 },
488 }
489 };
490
David Tolnayfa0edf22016-09-23 22:58:24 -0700491 ($i:expr, mut $field:ident : $e:ident >> $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700492 do_parse!($i, $field: call!($e) >> $($rest)*)
David Tolnayfa0edf22016-09-23 22:58:24 -0700493 };
494
495 ($i:expr, mut $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
496 match $submac!($i, $($args)*) {
497 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700498 $crate::nom::IResult::Done(i, o) => {
David Tolnayfa0edf22016-09-23 22:58:24 -0700499 let mut $field = o;
500 do_parse!(i, $($rest)*)
501 },
502 }
503 };
David Tolnayb5a7b142016-09-13 22:46:39 -0700504}