about summary refs log tree commit diff
path: root/src/parse/macros.rs
blob: aad6144a6aaa46be0b712e2e07d0cdb8da2ff236 (plain)
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_export]
macro_rules! check {
    ($self:ident, $($kind:pat_param)|+) => {
        if let Some(Token {kind: $( $kind )|+, ..}) = $self.tokens.peek() {
            true
        } else {
            false
        }
    };
}

#[macro_export]
macro_rules! consume {
    ($self:ident, $($kind:pat_param)|+) => {
        if let Some(token) = $self.tokens.next() {
            if let Token {kind: $( $kind )|+, ..} = token {
                Ok(token)
            } else {
                Err(ParserError::new(ParserErrorLocation::Specific(token.location), ParserErrorKind::UnexpectedToken {
                    received: token.kind,
                    expected: merge_token_names!($($kind),+),
                }))
            }
        } else {
            Err(ParserError::new(ParserErrorLocation::Eof, ParserErrorKind::UnexpectedEof {
                expected: merge_token_names!($($kind),+),
            }))
        }
    };
}

#[macro_export]
macro_rules! consume_if {
    ($self:ident, $($kind:pat_param)|+) => {
        if let Some(Token {kind: $( $kind )|+, ..}) = $self.tokens.peek() {
            Some($self.tokens.next().unwrap())
        } else {
            None
        }
    };
}

#[macro_export]
macro_rules! inner {
    ($token:expr, $kind:path ) => {
        match $token.kind {
            $kind(inner) => inner,
            _ => panic!("Tried getting inner content of incorrect kind."),
        }
    };
}

// TODO: Better names for the tokens.
#[macro_export]
macro_rules! merge_token_names {
    ($($kind:pat_param),+) => {
        vec![$( stringify!($kind) ),+].join(", ")
    };
}