blob: 50a338106ca37c168f4ea2f190a8547dcb1bc163 [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
David Tolnayeea28d62016-10-25 20:44:08 -070064// This is actually nom's cond_with_error.
David Tolnayb5a7b142016-09-13 22:46:39 -070065macro_rules! cond {
66 ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => {
67 if $cond {
68 match $submac!($i, $($args)*) {
David Tolnayde955482016-10-08 13:10:27 -070069 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, ::std::option::Option::Some(o)),
David Tolnayeea28d62016-10-25 20:44:08 -070070 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayb5a7b142016-09-13 22:46:39 -070071 }
72 } else {
73 $crate::nom::IResult::Done($i, ::std::option::Option::None)
74 }
David Tolnaycfe55022016-10-02 22:02:27 -070075 };
76
77 ($i:expr, $cond:expr, $f:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -070078 cond!($i, $cond, call!($f))
David Tolnaycfe55022016-10-02 22:02:27 -070079 };
David Tolnayb5a7b142016-09-13 22:46:39 -070080}
81
David Tolnayaf2557e2016-10-24 11:52:21 -070082macro_rules! cond_reduce {
83 ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => {
84 if $cond {
85 $submac!($i, $($args)*)
86 } else {
87 $crate::nom::IResult::Error
88 }
89 };
90
91 ($i:expr, $cond:expr, $f:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -070092 cond_reduce!($i, $cond, call!($f))
David Tolnayaf2557e2016-10-24 11:52:21 -070093 };
94}
95
David Tolnayb5a7b142016-09-13 22:46:39 -070096macro_rules! preceded {
97 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => {
98 match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) {
99 $crate::nom::IResult::Done(remaining, (_, o)) => $crate::nom::IResult::Done(remaining, o),
100 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
101 }
102 };
103
104 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700105 preceded!($i, $submac!($($args)*), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -0700106 };
107
108 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700109 preceded!($i, call!($f), $submac!($($args)*))
David Tolnayb5a7b142016-09-13 22:46:39 -0700110 };
111
112 ($i:expr, $f:expr, $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700113 preceded!($i, call!($f), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -0700114 };
115}
116
117macro_rules! terminated {
118 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => {
119 match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) {
120 $crate::nom::IResult::Done(remaining, (o, _)) => $crate::nom::IResult::Done(remaining, o),
121 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
122 }
123 };
124
125 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700126 terminated!($i, $submac!($($args)*), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -0700127 };
128
129 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700130 terminated!($i, call!($f), $submac!($($args)*))
David Tolnayb5a7b142016-09-13 22:46:39 -0700131 };
132
133 ($i:expr, $f:expr, $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700134 terminated!($i, call!($f), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -0700135 };
136}
137
138macro_rules! many0 {
139 ($i:expr, $submac:ident!( $($args:tt)* )) => {{
140 let ret;
141 let mut res = ::std::vec::Vec::new();
142 let mut input = $i;
143
144 loop {
145 if input.is_empty() {
146 ret = $crate::nom::IResult::Done(input, res);
147 break;
148 }
149
150 match $submac!(input, $($args)*) {
151 $crate::nom::IResult::Error => {
152 ret = $crate::nom::IResult::Done(input, res);
153 break;
154 }
155 $crate::nom::IResult::Done(i, o) => {
156 // loop trip must always consume (otherwise infinite loops)
David Tolnaybc84d5a2016-10-08 13:20:57 -0700157 if i.len() == input.len() {
David Tolnayb5a7b142016-09-13 22:46:39 -0700158 ret = $crate::nom::IResult::Error;
159 break;
160 }
161
162 res.push(o);
163 input = i;
164 }
165 }
166 }
167
168 ret
169 }};
170
171 ($i:expr, $f:expr) => {
David Tolnaybc84d5a2016-10-08 13:20:57 -0700172 $crate::nom::many0($i, $f)
David Tolnayb5a7b142016-09-13 22:46:39 -0700173 };
174}
175
David Tolnayc7f646a2016-10-16 10:54:39 -0700176pub fn many0<'a, T>(mut input: &'a str,
177 f: fn(&'a str) -> IResult<&'a str, T>)
178 -> IResult<&'a str, Vec<T>> {
David Tolnaybc84d5a2016-10-08 13:20:57 -0700179 let mut res = Vec::new();
180
181 loop {
182 if input.is_empty() {
183 return IResult::Done(input, res);
184 }
185
186 match f(input) {
187 IResult::Error => {
188 return IResult::Done(input, res);
189 }
190 IResult::Done(i, o) => {
191 // loop trip must always consume (otherwise infinite loops)
192 if i.len() == input.len() {
193 return IResult::Error;
194 }
195
196 res.push(o);
197 input = i;
198 }
199 }
200 }
201}
202
David Tolnayb5a7b142016-09-13 22:46:39 -0700203macro_rules! peek {
204 ($i:expr, $submac:ident!( $($args:tt)* )) => {
205 match $submac!($i, $($args)*) {
206 $crate::nom::IResult::Done(_, o) => $crate::nom::IResult::Done($i, o),
207 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
208 }
209 };
210}
211
212macro_rules! take_while1 {
213 ($input:expr, $submac:ident!( $($args:tt)* )) => {{
214 let mut offset = $input.len();
215 for (o, c) in $input.char_indices() {
216 if !$submac!(c, $($args)*) {
217 offset = o;
218 break;
219 }
220 }
221 if offset == 0 {
222 $crate::nom::IResult::Error
223 } else if offset < $input.len() {
224 $crate::nom::IResult::Done(&$input[offset..], &$input[..offset])
225 } else {
226 $crate::nom::IResult::Done("", $input)
227 }
228 }};
229
230 ($input:expr, $f:expr) => {
231 take_while1!($input, call!($f));
232 };
233}
234
David Tolnay62604d92016-09-30 14:47:48 -0700235pub fn str_chars(s: &str) -> Vec<char> {
236 // Can't do `s.chars().collect()` because it triggers a compiler bug in 1.12.0
237 // https://github.com/dtolnay/syn/issues/20
238 let mut result = Vec::new();
239 for ch in s.chars() {
240 result.push(ch);
241 }
242 result
243}
244
David Tolnayb5a7b142016-09-13 22:46:39 -0700245macro_rules! take_until {
246 ($input:expr, $substr:expr) => {{
247 if $substr.len() > $input.len() {
248 $crate::nom::IResult::Error
249 } else {
David Tolnay62604d92016-09-30 14:47:48 -0700250 let substr_vec: Vec<char> = $crate::nom::str_chars($substr);
David Tolnayb5a7b142016-09-13 22:46:39 -0700251 let mut window: Vec<char> = vec![];
252 let mut offset = $input.len();
253 let mut parsed = false;
254 for (o, c) in $input.char_indices() {
255 window.push(c);
256 if window.len() > substr_vec.len() {
257 window.remove(0);
258 }
259 if window == substr_vec {
260 parsed = true;
261 window.pop();
262 let window_len: usize = window.iter()
263 .map(|x| x.len_utf8())
264 .fold(0, |x, y| x + y);
265 offset = o - window_len;
266 break;
267 }
268 }
269 if parsed {
270 $crate::nom::IResult::Done(&$input[offset..], &$input[..offset])
271 } else {
272 $crate::nom::IResult::Error
273 }
274 }
275 }};
276}
277
278macro_rules! tag {
279 ($i:expr, $tag: expr) => {
David Tolnay0b154ea2016-10-01 15:42:50 -0700280 if $i.starts_with($tag) {
David Tolnayb5a7b142016-09-13 22:46:39 -0700281 $crate::nom::IResult::Done(&$i[$tag.len()..], &$i[0..$tag.len()])
282 } else {
283 $crate::nom::IResult::Error
284 }
285 };
286}
287
288macro_rules! switch {
289 ($i:expr, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => {
290 match $submac!($i, $($args)*) {
291 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
292 $crate::nom::IResult::Done(i, o) => match o {
293 $(
294 $p => $subrule!(i, $($args2)*),
295 )*
296 _ => $crate::nom::IResult::Error,
297 }
298 }
299 };
300}
301
302macro_rules! value {
303 ($i:expr, $res:expr) => {
304 $crate::nom::IResult::Done($i, $res)
305 };
306}
307
308macro_rules! delimited {
309 ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => {
310 match tuple_parser!($i, (), $submac!($($args)*), $($rest)*) {
311 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
312 $crate::nom::IResult::Done(i1, (_, o, _)) => $crate::nom::IResult::Done(i1, o)
313 }
314 };
315
316 ($i:expr, $f:expr, $($rest:tt)+) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700317 delimited!($i, call!($f), $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700318 };
319}
320
David Tolnayb5a7b142016-09-13 22:46:39 -0700321macro_rules! separated_nonempty_list {
322 ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => {{
323 let mut res = ::std::vec::Vec::new();
324 let mut input = $i;
325
326 // get the first element
327 match $submac!(input, $($args2)*) {
328 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700329 $crate::nom::IResult::Done(i, o) => {
David Tolnayb5a7b142016-09-13 22:46:39 -0700330 if i.len() == input.len() {
331 $crate::nom::IResult::Error
332 } else {
333 res.push(o);
334 input = i;
335
336 while let $crate::nom::IResult::Done(i2, _) = $sep!(input, $($args)*) {
337 if i2.len() == input.len() {
338 break;
339 }
340
David Tolnayde955482016-10-08 13:10:27 -0700341 if let $crate::nom::IResult::Done(i3, o3) = $submac!(i2, $($args2)*) {
David Tolnayb5a7b142016-09-13 22:46:39 -0700342 if i3.len() == i2.len() {
343 break;
344 }
345 res.push(o3);
346 input = i3;
347 } else {
348 break;
349 }
350 }
351 $crate::nom::IResult::Done(input, res)
352 }
353 }
354 }
355 }};
356
357 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700358 separated_nonempty_list!($i, $submac!($($args)*), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -0700359 };
360
361 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700362 separated_nonempty_list!($i, call!($f), $submac!($($args)*))
David Tolnayb5a7b142016-09-13 22:46:39 -0700363 };
364
365 ($i:expr, $f:expr, $g:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700366 separated_nonempty_list!($i, call!($f), call!($g))
David Tolnayb5a7b142016-09-13 22:46:39 -0700367 };
368}
369
370macro_rules! tuple {
371 ($i:expr, $($rest:tt)*) => {
372 tuple_parser!($i, (), $($rest)*)
373 };
374}
375
376/// Internal parser, do not use directly
377macro_rules! tuple_parser {
378 ($i:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700379 tuple_parser!($i, ($($parsed),*), call!($e), $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700380 };
381
382 ($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
383 match $submac!($i, $($args)*) {
384 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700385 $crate::nom::IResult::Done(i, o) =>
David Tolnayb5a7b142016-09-13 22:46:39 -0700386 tuple_parser!(i, (o), $($rest)*),
387 }
388 };
389
390 ($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
391 match $submac!($i, $($args)*) {
392 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700393 $crate::nom::IResult::Done(i, o) =>
David Tolnayb5a7b142016-09-13 22:46:39 -0700394 tuple_parser!(i, ($($parsed)* , o), $($rest)*),
395 }
396 };
397
398 ($i:expr, ($($parsed:tt),*), $e:ident) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700399 tuple_parser!($i, ($($parsed),*), call!($e))
David Tolnayb5a7b142016-09-13 22:46:39 -0700400 };
401
402 ($i:expr, (), $submac:ident!( $($args:tt)* )) => {
403 $submac!($i, $($args)*)
404 };
405
406 ($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => {
407 match $submac!($i, $($args)*) {
408 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
409 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, ($($parsed),*, o))
410 }
411 };
412
413 ($i:expr, ($($parsed:expr),*)) => {
414 $crate::nom::IResult::Done($i, ($($parsed),*))
415 };
416}
417
418macro_rules! alt {
419 ($i:expr, $e:ident | $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700420 alt!($i, call!($e) | $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700421 };
422
423 ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => {
424 match $subrule!($i, $($args)*) {
425 res @ $crate::nom::IResult::Done(_, _) => res,
426 _ => alt!($i, $($rest)*)
427 }
428 };
429
430 ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => {
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, $($rest)*)
434 }
435 };
436
437 ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700438 alt!($i, call!($e) => { $gen } | $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700439 };
440
441 ($i:expr, $e:ident => { $gen:expr }) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700442 alt!($i, call!($e) => { $gen })
David Tolnayb5a7b142016-09-13 22:46:39 -0700443 };
444
445 ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => {
446 match $subrule!($i, $($args)*) {
447 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, $gen(o)),
David Tolnay5377b172016-10-25 01:13:12 -0700448 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayb5a7b142016-09-13 22:46:39 -0700449 }
450 };
451
452 ($i:expr, $e:ident) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700453 alt!($i, call!($e))
David Tolnayb5a7b142016-09-13 22:46:39 -0700454 };
455
456 ($i:expr, $subrule:ident!( $($args:tt)*)) => {
David Tolnay5377b172016-10-25 01:13:12 -0700457 $subrule!($i, $($args)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700458 };
459}
460
461macro_rules! do_parse {
462 ($i:expr, ( $($rest:expr),* )) => {
463 $crate::nom::IResult::Done($i, ( $($rest),* ))
464 };
465
466 ($i:expr, $e:ident >> $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700467 do_parse!($i, call!($e) >> $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700468 };
469
470 ($i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
471 match $submac!($i, $($args)*) {
472 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
473 $crate::nom::IResult::Done(i, _) =>
474 do_parse!(i, $($rest)*),
475 }
476 };
477
478 ($i:expr, $field:ident : $e:ident >> $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700479 do_parse!($i, $field: call!($e) >> $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700480 };
481
482 ($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
483 match $submac!($i, $($args)*) {
484 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700485 $crate::nom::IResult::Done(i, o) => {
David Tolnayb5a7b142016-09-13 22:46:39 -0700486 let $field = o;
487 do_parse!(i, $($rest)*)
488 },
489 }
490 };
491
David Tolnayfa0edf22016-09-23 22:58:24 -0700492 ($i:expr, mut $field:ident : $e:ident >> $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700493 do_parse!($i, $field: call!($e) >> $($rest)*)
David Tolnayfa0edf22016-09-23 22:58:24 -0700494 };
495
496 ($i:expr, mut $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
497 match $submac!($i, $($args)*) {
498 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
David Tolnayde955482016-10-08 13:10:27 -0700499 $crate::nom::IResult::Done(i, o) => {
David Tolnayfa0edf22016-09-23 22:58:24 -0700500 let mut $field = o;
501 do_parse!(i, $($rest)*)
502 },
503 }
504 };
David Tolnayb5a7b142016-09-13 22:46:39 -0700505}