swc_css_parser/parser/
macros.rs

1macro_rules! span {
2    ($parser:expr, $start:expr) => {{
3        let last_pos = $parser.input.last_pos();
4        swc_common::Span::new($start, last_pos)
5    }};
6}
7
8macro_rules! tok_pat {
9    (Ident) => {
10        swc_css_ast::Token::Ident { .. }
11    };
12
13    (Percentage) => {
14        swc_css_ast::Token::Percentage { .. }
15    };
16
17    (Dimension) => {
18        swc_css_ast::Token::Dimension { .. }
19    };
20
21    (AtKeyword) => {
22        swc_css_ast::Token::AtKeyword { .. }
23    };
24
25    (Function) => {
26        swc_css_ast::Token::Function { .. }
27    };
28
29    (Str) => {
30        swc_css_ast::Token::Str { .. }
31    };
32
33    (Number) => {
34        swc_css_ast::Token::Number { .. }
35    };
36
37    (Url) => {
38        swc_css_ast::Token::Url { .. }
39    };
40
41    ($t:tt) => {
42        tok!($t)
43    };
44}
45
46macro_rules! cur {
47    ($parser:expr) => {
48        match $parser.input.cur() {
49            Some(v) => v,
50            None => {
51                let last_pos = $parser.input.last_pos();
52                let span = swc_common::Span::new(last_pos, last_pos);
53
54                for error in $parser.input.take_errors() {
55                    let (span, kind) = *error.into_inner();
56
57                    $parser.errors.push(Error::new(span, kind));
58                }
59
60                return Err(crate::error::Error::new(span, crate::error::ErrorKind::Eof));
61            }
62        }
63    };
64}
65
66macro_rules! bump {
67    ($parser:expr) => {
68        $parser.input.bump().unwrap().token
69    };
70}
71
72macro_rules! is_case_insensitive_ident {
73    ($parser:expr, $tt:tt) => {{
74        match $parser.input.cur() {
75            Some(swc_css_ast::Token::Ident { value, .. })
76                if (&**value).eq_ignore_ascii_case($tt) =>
77            {
78                true
79            }
80            _ => false,
81        }
82    }};
83}
84
85macro_rules! is_one_of_case_insensitive_ident {
86    ($parser:expr, $($tt:tt),+) => {
87        match $parser.input.cur() {
88            Some(swc_css_ast::Token::Ident { value, .. }) => {
89                if $((&**value).eq_ignore_ascii_case($tt))||* {
90                    true
91                } else {
92                    false
93                }
94            }
95            _ => false,
96        }
97    };
98}
99
100macro_rules! is {
101    ($parser:expr, EOF) => {{
102        let is_eof = $parser.input.cur().is_none();
103
104        if is_eof {
105            for error in $parser.input.take_errors() {
106                let (span, kind) = *error.into_inner();
107
108                $parser.errors.push(Error::new(span, kind));
109            }
110        }
111
112        is_eof
113    }};
114
115    ($parser:expr, $tt:tt) => {{
116        match $parser.input.cur() {
117            Some(tok_pat!($tt)) => true,
118            _ => false,
119        }
120    }};
121}
122
123macro_rules! is_one_of {
124    ($parser:expr, $($tt:tt),+) => {
125        $(
126            is!($parser, $tt)
127        )||*
128    };
129}
130
131macro_rules! peeked_is {
132    ($parser:expr, $tt:tt) => {{
133        match $parser.input.peek() {
134            Some(tok_pat!($tt)) => true,
135            _ => false,
136        }
137    }};
138}
139
140macro_rules! peeked_is_one_of {
141    ($parser:expr, $($tt:tt),+) => {
142        $(
143            peeked_is!($parser, $tt)
144        )||*
145    };
146}
147
148macro_rules! eat {
149    ($parser:expr, $tt:tt) => {
150        if is!($parser, $tt) {
151            bump!($parser);
152            true
153        } else {
154            false
155        }
156    };
157}
158
159macro_rules! expect {
160    ($parser:expr, $tt:tt) => {
161        if !eat!($parser, $tt) {
162            let span = $parser.input.cur_span();
163            return Err(crate::error::Error::new(
164                span,
165                crate::error::ErrorKind::ExpectedButGot(stringify!($tt)),
166            ));
167        }
168    };
169}