swc_css_parser/parser/
macros.rs

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
macro_rules! span {
    ($parser:expr, $start:expr) => {{
        let last_pos = $parser.input.last_pos();
        swc_common::Span::new($start, last_pos)
    }};
}

macro_rules! tok_pat {
    (Ident) => {
        swc_css_ast::Token::Ident { .. }
    };

    (Percentage) => {
        swc_css_ast::Token::Percentage { .. }
    };

    (Dimension) => {
        swc_css_ast::Token::Dimension { .. }
    };

    (AtKeyword) => {
        swc_css_ast::Token::AtKeyword { .. }
    };

    (Function) => {
        swc_css_ast::Token::Function { .. }
    };

    (Str) => {
        swc_css_ast::Token::Str { .. }
    };

    (Number) => {
        swc_css_ast::Token::Number { .. }
    };

    (Url) => {
        swc_css_ast::Token::Url { .. }
    };

    ($t:tt) => {
        tok!($t)
    };
}

macro_rules! cur {
    ($parser:expr) => {
        match $parser.input.cur() {
            Some(v) => v,
            None => {
                let last_pos = $parser.input.last_pos();
                let span = swc_common::Span::new(last_pos, last_pos);

                for error in $parser.input.take_errors() {
                    let (span, kind) = *error.into_inner();

                    $parser.errors.push(Error::new(span, kind));
                }

                return Err(crate::error::Error::new(span, crate::error::ErrorKind::Eof));
            }
        }
    };
}

macro_rules! bump {
    ($parser:expr) => {
        $parser.input.bump().unwrap().token
    };
}

macro_rules! is_case_insensitive_ident {
    ($parser:expr, $tt:tt) => {{
        match $parser.input.cur() {
            Some(swc_css_ast::Token::Ident { value, .. })
                if (&**value).eq_ignore_ascii_case($tt) =>
            {
                true
            }
            _ => false,
        }
    }};
}

macro_rules! is_one_of_case_insensitive_ident {
    ($parser:expr, $($tt:tt),+) => {
        match $parser.input.cur() {
            Some(swc_css_ast::Token::Ident { value, .. }) => {
                if $((&**value).eq_ignore_ascii_case($tt))||* {
                    true
                } else {
                    false
                }
            }
            _ => false,
        }
    };
}

macro_rules! is {
    ($parser:expr, EOF) => {{
        let is_eof = $parser.input.cur().is_none();

        if is_eof {
            for error in $parser.input.take_errors() {
                let (span, kind) = *error.into_inner();

                $parser.errors.push(Error::new(span, kind));
            }
        }

        is_eof
    }};

    ($parser:expr, $tt:tt) => {{
        match $parser.input.cur() {
            Some(tok_pat!($tt)) => true,
            _ => false,
        }
    }};
}

macro_rules! is_one_of {
    ($parser:expr, $($tt:tt),+) => {
        $(
            is!($parser, $tt)
        )||*
    };
}

macro_rules! peeked_is {
    ($parser:expr, $tt:tt) => {{
        match $parser.input.peek() {
            Some(tok_pat!($tt)) => true,
            _ => false,
        }
    }};
}

macro_rules! peeked_is_one_of {
    ($parser:expr, $($tt:tt),+) => {
        $(
            peeked_is!($parser, $tt)
        )||*
    };
}

macro_rules! eat {
    ($parser:expr, $tt:tt) => {
        if is!($parser, $tt) {
            bump!($parser);
            true
        } else {
            false
        }
    };
}

macro_rules! expect {
    ($parser:expr, $tt:tt) => {
        if !eat!($parser, $tt) {
            let span = $parser.input.cur_span();
            return Err(crate::error::Error::new(
                span,
                crate::error::ErrorKind::ExpectedButGot(stringify!($tt)),
            ));
        }
    };
}