swc_ecma_quote_macros/
input.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
use syn::{
    parse::{Parse, ParseStream},
    punctuated::Punctuated,
    Token,
};

pub(super) struct QuoteInput {
    pub src: syn::LitStr,
    #[allow(unused)]
    pub as_token: Token![as],
    pub output_type: syn::Type,

    pub vars: Option<(Token![,], Punctuated<QuoteVar, Token![,]>)>,
}

pub(super) struct QuoteVar {
    pub name: syn::Ident,
    /// Defaults to `swc_ecma_ast::Ident`
    pub ty: Option<syn::Type>,

    #[allow(unused)]
    pub eq_token: Token![=],
    pub value: syn::Expr,
}

impl Parse for QuoteInput {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let src = input.parse()?;
        let as_token = input.parse()?;
        let output_type = input.parse()?;
        let vars = if input.is_empty() {
            None
        } else {
            let comma_token = input.parse()?;
            let vars = Punctuated::parse_terminated(input)?;
            Some((comma_token, vars))
        };

        Ok(Self {
            src,
            as_token,
            output_type,
            vars,
        })
    }
}

impl Parse for QuoteVar {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let name = input.parse()?;

        let ty = if input.peek(Token![:]) {
            let _: Token![:] = input.parse()?;
            Some(input.parse()?)
        } else {
            None
        };

        Ok(Self {
            name,
            ty,
            eq_token: input.parse()?,
            value: input.parse()?,
        })
    }
}