Skip to main content

surql_parser/upstream/syn/parser/
mac.rs

1/// A macro for returning an error when a unexpected token was found.
2///
3/// This macro handles a variety of situations, including errors related to
4/// invalid tokens and unexpected `EOF` or whitespace.
5///
6/// This macro takes a reference to the parser, the token which was unexpected
7/// and a expression which explains what should be expected instead.
8///
9/// This macro attaches the span from the token as an error span to the error.
10macro_rules! unexpected {
11    (
12        $parser:expr_2021, $found:expr_2021, $expected:expr_2021 $(, @$span:expr_2021)?
13        $(, $($t:tt)*)?
14    ) => {
15        { let __found : $crate::upstream::syn::token::Token = $found; match __found.kind
16        { $crate::upstream::syn::token::TokenKind::Invalid => { return Err($parser .lexer
17        .error.take().unwrap()); } $crate::upstream::syn::token::TokenKind::Eof => { let
18        error =
19        $crate::upstream::syn::error::syntax_error!("Unexpected end of file, expected {}",$expected,
20        @ __found.span $($($t)*)?); return Err(error) } x => {
21        $crate::upstream::syn::error::bail!("Unexpected token `{}`, expected {}",
22        x,$expected, @ __found.span $($($t)*)?) } } }
23    };
24}
25/// A macro for asserting that the next token should be of the given type,
26/// returns the token if this is the case otherwise it returns an error.
27macro_rules! expected {
28    ($parser:expr_2021, $($kind:tt)*) => {
29        { let token : crate ::upstream::syn::token::Token = $parser .next(); if let
30        $($kind)* = token.kind { token } else {
31        $crate::upstream::syn::parser::unexpected!($parser, token, $($kind)*) } }
32    };
33}
34/// A macro for indicating that the parser encountered an token which it didn't
35/// expect.
36macro_rules! expected_whitespace {
37    ($parser:expr_2021, $($kind:tt)*) => {
38        { if let Some(token) = $parser .next_whitespace() { if let $($kind)* = token.kind
39        { token } else { $crate::upstream::syn::parser::unexpected!($parser, token,
40        $($kind)*) } } else {
41        $crate::upstream::syn::error::bail!("Unexpected whitespace",@$parser .last_span()
42        => "No whitespace allowed after this token") } }
43    };
44}
45macro_rules! enter_object_recursion {
46    ($name:ident = $this:expr_2021 => { $($t:tt)* }) => {
47        { if $this .settings.object_recursion_limit == 0 { return
48        Err($crate::upstream::syn::parser::SyntaxError::new("Exceeded query recursion depth limit")
49        .with_span($this .last_span(), $crate::upstream::syn::error::MessageKind::Error))
50        } struct Dropper <'a, 'b > (&'a mut $crate::upstream::syn::parser::Parser <'b >);
51        impl Drop for Dropper <'_, '_ > { fn drop(& mut self) { self.0.settings
52        .object_recursion_limit += 1; } } impl <'a > ::std::ops::Deref for Dropper <'_,'a
53        > { type Target = $crate::upstream::syn::parser::Parser <'a >; fn deref(& self)
54        -> & Self::Target { self.0 } } impl <'a > ::std::ops::DerefMut for Dropper <'_,'a
55        > { fn deref_mut(& mut self) -> & mut Self::Target { self.0 } } $this .settings
56        .object_recursion_limit -= 1; let mut $name = Dropper($this); { $($t)* } }
57    };
58}
59macro_rules! enter_query_recursion {
60    ($name:ident = $this:expr_2021 => { $($t:tt)* }) => {
61        { if $this .settings.query_recursion_limit == 0 { return
62        Err($crate::upstream::syn::parser::SyntaxError::new("Exceeded query recursion depth limit")
63        .with_span($this .last_span(), $crate::upstream::syn::error::MessageKind::Error))
64        } struct Dropper <'a, 'b > (&'a mut $crate::upstream::syn::parser::Parser <'b >);
65        impl Drop for Dropper <'_, '_ > { fn drop(& mut self) { self.0.settings
66        .query_recursion_limit += 1; } } impl <'a > ::std::ops::Deref for Dropper <'_,'a
67        > { type Target = $crate::upstream::syn::parser::Parser <'a >; fn deref(& self)
68        -> & Self::Target { self.0 } } impl <'a > ::std::ops::DerefMut for Dropper <'_,'a
69        > { fn deref_mut(& mut self) -> & mut Self::Target { self.0 } } $this .settings
70        .query_recursion_limit -= 1; let mut $name = Dropper($this); { $($t)* } }
71    };
72}
73pub(crate) use {
74	enter_object_recursion, enter_query_recursion, expected, expected_whitespace, unexpected,
75};