1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
macro_rules! get_required_element {
    ($stream :expr, $expected: expr, $guidance: expr, $($pat:pat => $result:expr,)+) => {
        // TODO: probably should call get_optional_element_with_token
        match ($stream).next()? {
            Some(token) => match token.value() {
                 $(
                     $pat => {
                        Some($result)
                     },
                 )+
                 _ => {
                    $stream.error(crate::parse::Error::new($expected, Some(token), $guidance))?;
                    $stream.back(token);
                    None
                 },
            }
            None => {
                $stream.error(crate::parse::Error::new($expected, None, $guidance))?;
                None
            },
        }
    };
}

macro_rules! get_optional_element {
    ($stream :expr, $($pat:pat => $result:expr,)+) => {
        match ($stream).next()? {
            None => None,
            Some(token) => match token.value() {
                 $(
                     $pat => Some($result),
                 )+
                 _ => {
                    $stream.back(token);
                    None
                 }
            }
        }
    };
}

macro_rules! get_optional_element_with_token {
    ($stream :expr, $($pat:pat => $result:expr,)+) => {
       match ($stream).next()? {
            None => None,
            Some(token) => match token.value() {
                 $(
                     $pat => {
                        Some(($result, token))
                    },
                 )+
                 _ => {
                    $stream.back(token);
                    None
                 },
            }
        }
    };
}