1 | #[macro_use ] |
2 | mod macros; |
3 | |
4 | use proc_macro2::{Delimiter, Group, Literal, Punct, Spacing, TokenStream, TokenTree}; |
5 | use std::iter::FromIterator; |
6 | use syn::Expr; |
7 | |
8 | #[test] |
9 | fn test_grouping() { |
10 | let tokens: TokenStream = TokenStream::from_iter(vec![ |
11 | TokenTree::Literal(Literal::i32_suffixed(1)), |
12 | TokenTree::Punct(Punct::new('+' , Spacing::Alone)), |
13 | TokenTree::Group(Group::new( |
14 | Delimiter::None, |
15 | TokenStream::from_iter(vec![ |
16 | TokenTree::Literal(Literal::i32_suffixed(2)), |
17 | TokenTree::Punct(Punct::new('+' , Spacing::Alone)), |
18 | TokenTree::Literal(Literal::i32_suffixed(3)), |
19 | ]), |
20 | )), |
21 | TokenTree::Punct(Punct::new('*' , Spacing::Alone)), |
22 | TokenTree::Literal(Literal::i32_suffixed(4)), |
23 | ]); |
24 | |
25 | assert_eq!(tokens.to_string(), "1i32 + 2i32 + 3i32 * 4i32" ); |
26 | |
27 | snapshot!(tokens as Expr, @r###" |
28 | Expr::Binary { |
29 | left: Expr::Lit { |
30 | lit: 1i32, |
31 | }, |
32 | op: Add, |
33 | right: Expr::Binary { |
34 | left: Expr::Group { |
35 | expr: Expr::Binary { |
36 | left: Expr::Lit { |
37 | lit: 2i32, |
38 | }, |
39 | op: Add, |
40 | right: Expr::Lit { |
41 | lit: 3i32, |
42 | }, |
43 | }, |
44 | }, |
45 | op: Mul, |
46 | right: Expr::Lit { |
47 | lit: 4i32, |
48 | }, |
49 | }, |
50 | } |
51 | "### ); |
52 | } |
53 | |