as_derive_utils/
parse_utils.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use crate::utils::SynErrorExt;

use syn::parse::{Parse, ParseBuffer, Peek};

#[doc(hidden)]
#[macro_export]
macro_rules! ret_err_on_peek {
    ($input:ident, $peeked:expr, $expected:expr, $found_msg:expr $(,)*) => {
        if $input.peek($peeked) {
            return Err($input.error(concat!("expected ", $expected, ", found ", $found_msg)));
        }
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! ret_err_on {
    ($cond:expr, $span:expr, $expected:expr, $found_msg:expr $(,)*) => {
        if $cond {
            return Err(syn::Error::new(
                $span,
                concat!("expected ", $expected, ", found ", $found_msg),
            ));
        }
    };
}

pub use crate::{ret_err_on, ret_err_on_peek};

fn parse_with_prefix_err<T>(input: &ParseBuffer<'_>, prefix: &str) -> Result<T, syn::Error>
where
    T: Parse,
{
    input
        .parse::<T>()
        .map_err(|e| e.prepend_msg(format!("{}: ", prefix)))
}

pub trait ParseBufferExt {
    fn as_pb(&self) -> &ParseBuffer<'_>;

    fn peek_parse<F, X, P>(&self, f: F) -> Result<Option<P>, syn::Error>
    where
        F: FnOnce(X) -> P + Peek,
        P: Parse,
    {
        let this = self.as_pb();
        if this.peek(f) {
            this.parse::<P>().map(Some)
        } else {
            Ok(None)
        }
    }

    /// Alternate method for parsing a type, with a better (?) error message.
    fn parse_type(&self) -> Result<syn::Type, syn::Error> {
        let input = self.as_pb();

        if input.peek(syn::Lit) {
            Err(input.error("expected type, found literal"))
        } else {
            parse_with_prefix_err(input, "while parsing type")
        }
    }

    /// Alternate method for parsing an expression, with a better (?) error message.
    fn parse_expr(&self) -> Result<syn::Expr, syn::Error> {
        parse_with_prefix_err(self.as_pb(), "while parsing expression")
    }

    /// skips a token tree.
    fn skip_tt(&self) {
        let _ = self.as_pb().parse::<proc_macro2::TokenTree>();
    }

    /// Ignores the rest of the parse input
    fn ignore_rest(&self) {
        let _ = self.as_pb().parse::<proc_macro2::TokenStream>();
    }

    /// Checks that a token is parsable, advancing the parse buffer if it is.
    ///
    /// This returns:
    /// - Ok(true): if the token was the passed in one, advancing the parser.
    /// - Ok(false): if the token was not passed in one, keeping the token unparsed.
    /// - Err: if there was an error parsing the token
    fn check_parse<F, X, P>(&self, f: F) -> Result<bool, syn::Error>
    where
        F: FnOnce(X) -> P + Peek,
        P: Parse,
    {
        let this = self.as_pb();
        if this.peek(f) {
            match this.parse::<P>() {
                Ok(_) => Ok(true),
                Err(e) => Err(e),
            }
        } else {
            Ok(false)
        }
    }

    fn parse_paren_buffer(&self) -> Result<ParseBuffer, syn::Error> {
        let content;
        let _ = syn::parenthesized!(content in self.as_pb());
        Ok(content)
    }

    fn parse_paren_as<T>(&self) -> Result<T, syn::Error>
    where
        T: Parse,
    {
        self.as_pb().parse_paren_with(|x| x.parse::<T>())
    }

    fn parse_paren_with<T, F>(&self, f: F) -> Result<T, syn::Error>
    where
        F: FnOnce(&ParseBuffer<'_>) -> Result<T, syn::Error>,
    {
        let content;
        let _ = syn::parenthesized!(content in self.as_pb());
        f(&content)
    }

    fn parse_int<N>(&self) -> Result<N, syn::Error>
    where
        N: std::str::FromStr,
        N::Err: std::fmt::Display,
    {
        self.as_pb().parse::<syn::LitInt>()?.base10_parse::<N>()
    }

    fn for_each_separated<F, G, P>(&self, _sep: G, mut func: F) -> Result<(), syn::Error>
    where
        F: FnMut(&ParseBuffer<'_>) -> Result<(), syn::Error>,
        G: Fn(proc_macro2::Span) -> P + Copy,
        P: Parse,
    {
        let this = self.as_pb();

        if this.is_empty() {
            return Ok(());
        }

        loop {
            func(this)?;

            if !this.is_empty() {
                let _ = this.parse::<P>()?;
            }
            if this.is_empty() {
                break Ok(());
            }
        }
    }
}

impl ParseBufferExt for ParseBuffer<'_> {
    #[inline(always)]
    fn as_pb(&self) -> &ParseBuffer<'_> {
        self
    }
}