swc_ecma_parser/parser/
macros.rs

1macro_rules! trace_cur {
2    ($p:expr, $name:ident) => {{
3        if cfg!(feature = "debug") {
4            tracing::debug!("{}: {:?}", stringify!($name), $p.input.cur());
5        }
6    }};
7}
8
9macro_rules! syntax_error {
10    ($p:expr, $err:expr) => {
11        syntax_error!($p, $p.input().cur_span(), $err)
12    };
13    ($p:expr, $span:expr, $err:expr) => {{
14        let err = $crate::error::Error::new($span, $err);
15        {
16            let cur = $p.input().cur();
17            if cur == Token::Error {
18                let error = $p.input_mut().expect_error_token_and_bump();
19                $p.emit_error(error);
20            }
21        }
22        if cfg!(feature = "debug") {
23            tracing::error!(
24                "Syntax error called from {}:{}:{}\nCurrent token = {:?}",
25                file!(),
26                line!(),
27                column!(),
28                $p.input().cur()
29            );
30        }
31        return Err(err.into());
32    }};
33}
34
35macro_rules! expect {
36    ($p:expr, $t:expr) => {{
37        if !$p.input_mut().eat($t) {
38            let span = $p.input().cur_span();
39            let cur = $p.input_mut().dump_cur();
40            syntax_error!(
41                $p,
42                span,
43                $crate::error::SyntaxError::Expected(format!("{:?}", $t), cur)
44            )
45        }
46    }};
47}
48
49macro_rules! unexpected {
50    ($p:expr, $expected:literal) => {{
51        let got = $p.input_mut().dump_cur();
52        syntax_error!(
53            $p,
54            $p.input().cur_span(),
55            $crate::error::SyntaxError::Unexpected {
56                got,
57                expected: $expected
58            }
59        )
60    }};
61}
62
63macro_rules! debug_tracing {
64    ($p:expr, $name:tt) => {{
65        #[cfg(feature = "debug")]
66        {
67            let _ = tracing::span!(
68                tracing::Level::ERROR,
69                $name,
70                cur = tracing::field::debug(&$p.input.cur())
71            )
72            .entered();
73        }
74    }};
75}
76
77macro_rules! peek {
78    ($p:expr) => {{
79        debug_assert!(
80            $p.input().cur() != Token::Eof,
81            "parser should not call peek() without knowing current token.
82Current token is {:?}",
83            $p.input().cur(),
84        );
85        $p.input_mut().peek()
86    }};
87}
88
89macro_rules! return_if_arrow {
90    ($p:expr, $expr:expr) => {{
91        // FIXME:
92        //
93        //
94
95        // let is_cur = match $p.state.potential_arrow_start {
96        //     Some(start) => $expr.span.lo() == start,
97        //     None => false
98        // };
99        // if is_cur {
100        if let Expr::Arrow { .. } = *$expr {
101            return Ok($expr);
102        }
103        // }
104    }};
105}