Debian Codegraph @

third_party/rust/derive_more-impl/src/parsing.rs

raw
1 //! Common parsing utilities for derive macros.
2 //!
3 //! Fair parsing of [`syn::Type`] and [`syn::Expr`] requires [`syn`]'s `full`
4 //! feature to be enabled, which unnecessary increases compile times. As we
5 //! don't have complex AST manipulation, usually requiring only understanding
6 //! where syntax item begins and ends, simpler manual parsing is implemented.
7
8 use proc_macro2::{Delimiter, Spacing, TokenStream};
9 use quote::ToTokens;
10 use syn::{
11 buffer::Cursor,
12 parse::{Parse, ParseStream},
13 punctuated::Punctuated,
14 spanned::Spanned as _,
15 token, Error, Ident, Result,
16 };
17
18 /// [`syn::Type`] [`Parse`]ing polyfill.
19 #[derive(Clone, Debug)]
20 pub(crate) enum Type {
21 /// [`syn::Type::Tuple`] [`Parse`]ing polyfill.
22 Tuple {
23 paren: token::Paren,
24 items: Punctuated<TokenStream, token::Comma>,
25 },
26
27 /// Every other [`syn::Type`] variant.
28 Other(TokenStream),
29 }
30
31 impl Type {
32 /// Creates a [`Type::Tuple`] from the provided [`Iterator`] of [`TokenStream`]s.
33 pub(crate) fn tuple<T: ToTokens>(items: impl IntoIterator<Item = T>) -> Self {
34 Self::Tuple {
35 paren: token::Paren::default(),
36 items: items.into_iter().map(ToTokens::into_token_stream).collect(),
37 }
38 }
39 }
40
41 impl Parse for Type {
42 fn parse(input: ParseStream) -> Result<Self> {
43 input.step(|c| {
44 let outer = *c;
45
46 if let Some((mut cursor, paren_span, next_item)) =
47 outer.group(Delimiter::Parenthesis)
48 {
49 let mut items = Punctuated::new();
50 while !cursor.eof() {
51 let (stream, c) = Self::parse_other(cursor).ok_or_else(|| {
52 Error::new(cursor.span(), "failed to parse type")
53 })?;
54 items.push_value(stream);
55 cursor = c;
56 if let Some((p, c)) = punct(',')(cursor) {
57 items.push_punct(token::Comma(p.span()));
58 cursor = c;
59 }
60 }
61 // `(Type)` is equivalent to `Type`, so isn't top-level tuple.
62 if items.len() == 1 && !items.trailing_punct() {
63 let stream = outer
64 .token_tree()
65 .unwrap_or_else(|| unreachable!())
66 .0
67 .into_token_stream();
68 Ok((Type::Other(stream), next_item))
69 } else {
70 Ok((
71 Type::Tuple {
72 paren: token::Paren(paren_span),
73 items,
74 },
75 next_item,
76 ))
77 }
78 } else {
79 Self::parse_other(outer)
80 .map(|(s, c)| (Self::Other(s), c))
81 .ok_or_else(|| Error::new(outer.span(), "failed to parse type"))
82 }
83 })
84 }
85 }
86
87 impl ToTokens for Type {
88 fn to_tokens(&self, tokens: &mut TokenStream) {
89 match self {
90 Type::Tuple { paren, items } => {
91 paren.surround(tokens, |tokens| items.to_tokens(tokens))
92 }
93 Type::Other(other) => other.to_tokens(tokens),
94 }
95 }
96 }
97
98 impl Type {
99 /// Parses a single [`Type::Other`].
100 pub fn parse_other(c: Cursor<'_>) -> Option<(TokenStream, Cursor<'_>)> {
101 take_until1(
102 alt([&mut balanced_pair(punct('<'), punct('>')), &mut token_tree]),
103 punct(','),
104 )(c)
105 }
106 }
107
108 /// [`syn::Expr`] [`Parse`]ing polyfill.
109 #[derive(Debug)]
110 pub(crate) enum Expr {
111 /// [`syn::Expr::Path`] of length 1 [`Parse`]ing polyfill.
112 Ident(Ident),
113
114 /// Every other [`syn::Expr`] variant.
115 Other(TokenStream),
116 }
117
118 impl Expr {
119 /// Returns an [`Ident`] in case this [`Expr`] is represented only by it.
120 pub(crate) fn ident(&self) -> Option<&Ident> {
121 match self {
122 Self::Ident(ident) => Some(ident),
123 Self::Other(_) => None,
124 }
125 }
126 }
127
128 impl Parse for Expr {
129 fn parse(input: ParseStream) -> Result<Self> {
130 if let Ok(ident) = input.step(|c| {
131 c.ident()
132 .filter(|(_, c)| c.eof() || punct(',')(*c).is_some())
133 .ok_or_else(|| Error::new(c.span(), "expected `ident(,|eof)`"))
134 }) {
135 Ok(Self::Ident(ident))
136 } else {
137 input.step(|c| {
138 take_until1(
139 alt([
140 &mut seq([
141 &mut colon2,
142 &mut balanced_pair(punct('<'), punct('>')),
143 ]),
144 &mut seq([
145 &mut balanced_pair(punct('<'), punct('>')),
146 &mut colon2,
147 ]),
148 &mut balanced_pair(punct('|'), punct('|')),
149 &mut token_tree,
150 ]),
151 punct(','),
152 )(*c)
153 .map(|(stream, cursor)| (Self::Other(stream), cursor))
154 .ok_or_else(|| Error::new(c.span(), "failed to parse expression"))
155 })
156 }
157 }
158 }
159
160 impl ToTokens for Expr {
161 fn to_tokens(&self, tokens: &mut TokenStream) {
162 match self {
163 Self::Ident(ident) => ident.to_tokens(tokens),
164 Self::Other(other) => other.to_tokens(tokens),
165 }
166 }
167 }
168
169 /// Result of parsing.
170 type ParsingResult<'a> = Option<(TokenStream, Cursor<'a>)>;
171
172 /// Tries to parse a [`syn::token::Colon2`].
173 pub fn colon2(c: Cursor<'_>) -> ParsingResult<'_> {
174 seq([
175 &mut punct_with_spacing(':', Spacing::Joint),
176 &mut punct(':'),
177 ])(c)
178 }
179
180 /// Tries to parse a [`punct`] with [`Spacing`].
181 pub fn punct_with_spacing(
182 p: char,
183 spacing: Spacing,
184 ) -> impl FnMut(Cursor<'_>) -> ParsingResult<'_> {
185 move |c| {
186 c.punct().and_then(|(punct, c)| {
187 (punct.as_char() == p && punct.spacing() == spacing)
188 .then(|| (punct.into_token_stream(), c))
189 })
190 }
191 }
192
193 /// Tries to parse a [`Punct`].
194 ///
195 /// [`Punct`]: proc_macro2::Punct
196 pub fn punct(p: char) -> impl FnMut(Cursor<'_>) -> ParsingResult<'_> {
197 move |c| {
198 c.punct().and_then(|(punct, c)| {
199 (punct.as_char() == p).then(|| (punct.into_token_stream(), c))
200 })
201 }
202 }
203
204 /// Tries to parse any [`TokenTree`].
205 ///
206 /// [`TokenTree`]: proc_macro2::TokenTree
207 pub fn token_tree(c: Cursor<'_>) -> ParsingResult<'_> {
208 c.token_tree().map(|(tt, c)| (tt.into_token_stream(), c))
209 }
210
211 /// Parses until balanced amount of `open` and `close` or eof.
212 ///
213 /// [`Cursor`] should be pointing **right after** the first `open`ing.
214 pub fn balanced_pair(
215 mut open: impl FnMut(Cursor<'_>) -> ParsingResult<'_>,
216 mut close: impl FnMut(Cursor<'_>) -> ParsingResult<'_>,
217 ) -> impl FnMut(Cursor<'_>) -> ParsingResult<'_> {
218 move |c| {
219 let (mut out, mut c) = open(c)?;
220 let mut count = 1;
221
222 while count != 0 {
223 let (stream, cursor) = if let Some(closing) = close(c) {
224 count -= 1;
225 closing
226 } else if let Some(opening) = open(c) {
227 count += 1;
228 opening
229 } else {
230 let (tt, c) = c.token_tree()?;
231 (tt.into_token_stream(), c)
232 };
233 out.extend(stream);
234 c = cursor;
235 }
236
237 Some((out, c))
238 }
239 }
240
241 /// Tries to execute the provided sequence of `parsers`.
242 pub fn seq<const N: usize>(
243 mut parsers: [&mut dyn FnMut(Cursor<'_>) -> ParsingResult<'_>; N],
244 ) -> impl FnMut(Cursor<'_>) -> ParsingResult<'_> + '_ {
245 move |c| {
246 parsers
247 .iter_mut()
248 .fold(Some((TokenStream::new(), c)), |out, parser| {
249 let (mut out, mut c) = out?;
250 let (stream, cursor) = parser(c)?;
251 out.extend(stream);
252 c = cursor;
253 Some((out, c))
254 })
255 }
256 }
257
258 /// Tries to execute the first successful parser.
259 pub fn alt<const N: usize>(
260 mut parsers: [&mut dyn FnMut(Cursor<'_>) -> ParsingResult<'_>; N],
261 ) -> impl FnMut(Cursor<'_>) -> ParsingResult<'_> + '_ {
262 move |c| {
263 parsers
264 .iter_mut()
265 .find_map(|parser| parser(c).map(|(s, c)| (s, c)))
266 }
267 }
268
269 /// Parses with `basic` while `until` fails. Returns [`None`] in case
270 /// `until` succeeded initially or `basic` never succeeded. Doesn't consume
271 /// tokens parsed by `until`.
272 pub fn take_until1<P, U>(
273 mut parser: P,
274 mut until: U,
275 ) -> impl FnMut(Cursor<'_>) -> ParsingResult<'_>
276 where
277 P: FnMut(Cursor<'_>) -> ParsingResult<'_>,
278 U: FnMut(Cursor<'_>) -> ParsingResult<'_>,
279 {
280 move |mut cursor| {
281 let mut out = TokenStream::new();
282 let mut parsed = false;
283
284 loop {
285 if cursor.eof() || until(cursor).is_some() {
286 return parsed.then_some((out, cursor));
287 }
288
289 let (stream, c) = parser(cursor)?;
290 out.extend(stream);
291 cursor = c;
292 parsed = true;
293 }
294 }
295 }
296
297 #[cfg(test)]
298 mod spec {
299 use std::{fmt::Debug, str::FromStr};
300
301 use itertools::Itertools as _;
302 use proc_macro2::TokenStream;
303 use quote::ToTokens;
304 use syn::{
305 parse::{Parse, Parser as _},
306 punctuated::Punctuated,
307 token::Comma,
308 };
309
310 use super::{Expr, Type};
311
312 fn assert<'a, T: Debug + Parse + ToTokens>(
313 input: &'a str,
314 parsed: impl AsRef<[&'a str]>,
315 ) {
316 let parsed = parsed.as_ref();
317 let punctuated = Punctuated::<T, Comma>::parse_terminated
318 .parse2(TokenStream::from_str(input).unwrap())
319 .unwrap();
320
321 assert_eq!(
322 parsed.len(),
323 punctuated.len(),
324 "Wrong length\n\
325 Expected: {parsed:?}\n\
326 Found: {punctuated:?}",
327 );
328
329 punctuated
330 .iter()
331 .map(|ty| ty.to_token_stream().to_string())
332 .zip(parsed)
333 .enumerate()
334 .for_each(|(i, (found, expected))| {
335 assert_eq!(
336 *expected, &found,
337 "Mismatch at index {i}\n\
338 Expected: {parsed:?}\n\
339 Found: {punctuated:?}",
340 );
341 });
342 }
343
344 mod tuple {
345 use super::*;
346
347 #[test]
348 fn zst_is_tuple() {
349 let zst = "()";
350 match syn::parse_str::<Type>(zst).unwrap() {
351 Type::Tuple { items, .. } => {
352 assert!(items.is_empty(), "Expected empty tuple, found: {items:?}");
353 }
354 other => panic!("Expected `Type::Tuple {{ .. }}`, found: {other:?}"),
355 }
356 }
357
358 #[test]
359 fn group_not_tuple() {
360 let group = "(Type)";
361 match syn::parse_str::<Type>(group).unwrap() {
362 Type::Other(tokens) => {
363 assert_eq!(tokens.to_string(), group);
364 }
365 tuple => panic!("Expected `Type::Other(_)`, found: {tuple:?}"),
366 }
367 }
368
369 #[test]
370 fn single_element_tuple() {
371 let tuple = "(Type,)";
372 match syn::parse_str::<Type>(tuple).unwrap() {
373 Type::Tuple { items, .. } => {
374 assert_eq!(
375 items.len(),
376 1,
377 "Expected empty tuple, found: {items:?}",
378 );
379 assert_eq!(items.first().unwrap().to_string(), "Type");
380 }
381 other => panic!("Expected `Type::Tuple {{ .. }}`, found: {other:?}"),
382 }
383 }
384
385 #[test]
386 fn cases() {
387 let cases = [
388 "[Type ; 3]",
389 "fn (usize) -> bool",
390 "for <'a > fn (&'a usize) -> bool",
391 "(Type)",
392 "path :: to :: Type",
393 "path :: to :: Generic < Type >",
394 "< Type as Trait >:: Assoc",
395 "< Type as Trait >:: Assoc < GAT >",
396 "* const ()",
397 "* mut ()",
398 "& i32",
399 "&'static str",
400 "& [str]",
401 "dyn Trait",
402 "dyn Trait + Send",
403 "()",
404 "(Type ,)",
405 "(Type , Type)",
406 "(Type , Type ,)",
407 ];
408
409 assert::<Type>("", []);
410 for i in 1..4 {
411 for permutations in cases.into_iter().permutations(i) {
412 let mut input = permutations.join(",");
413 assert::<Type>(&input, &permutations);
414 input.push(',');
415 assert::<Type>(&input, &permutations);
416 }
417 }
418 }
419 }
420
421 mod expr {
422 use super::*;
423
424 #[test]
425 fn cases() {
426 let cases = [
427 "ident",
428 "[a , b , c , d]",
429 "counter += 1",
430 "async { fut . await }",
431 "a < b",
432 "a > b",
433 "{ let x = (a , b) ; }",
434 "invoke (a , b)",
435 "foo as f64",
436 "| a , b | a + b",
437 "obj . k",
438 "for pat in expr { break pat ; }",
439 "if expr { true } else { false }",
440 "vector [2]",
441 "1",
442 "\"foo\"",
443 "loop { break i ; }",
444 "format ! (\"{}\" , q)",
445 "match n { Some (n) => { } , None => { } }",
446 "x . foo ::< T > (a , b)",
447 "x . foo ::< T < [T < T >; if a < b { 1 } else { 2 }] >, { a < b } > (a , b)",
448 "(a + b)",
449 "i32 :: MAX",
450 "1 .. 2",
451 "& a",
452 "[0u8 ; N]",
453 "(a , b , c , d)",
454 "< Ty as Trait > :: T",
455 "< Ty < Ty < T >, { a < b } > as Trait < T > > :: T",
456 ];
457
458 assert::<Expr>("", []);
459 for i in 1..4 {
460 for permutations in cases.into_iter().permutations(i) {
461 let mut input = permutations.clone().join(",");
462 assert::<Expr>(&input, &permutations);
463 input.push(',');
464 assert::<Expr>(&input, &permutations);
465 }
466 }
467 }
468 }
469 }