swc_ecma_lexer/common/parser/
macros.rs

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