blob: 6ca438d7a4d5e465b4bbc6b232d98843a1e8789b [file] [log] [blame]
Michael Layzellbe1e24d2017-06-04 21:15:23 -04001extern crate proc_macro2;
David Tolnay7d8b3312019-03-10 01:26:11 -08002extern crate syn;
Michael Layzellbe1e24d2017-06-04 21:15:23 -04003
David Tolnayc3f98562018-11-02 08:55:05 -07004mod features;
David Tolnayc7a5d3d2017-06-04 12:11:05 -07005
David Tolnay7d8b3312019-03-10 01:26:11 -08006#[macro_use]
7mod macros;
Michael Layzellbe1e24d2017-06-04 21:15:23 -04008
David Tolnay7d8b3312019-03-10 01:26:11 -08009use proc_macro2::{Delimiter, Group, Literal, Punct, Spacing, TokenStream, TokenTree};
10use syn::Expr;
11
12use std::iter::FromIterator;
Michael Layzellbe1e24d2017-06-04 21:15:23 -040013
14#[test]
15fn test_grouping() {
David Tolnay7d8b3312019-03-10 01:26:11 -080016 let tokens: TokenStream = TokenStream::from_iter(vec![
Alex Crichton9a4dca22018-03-28 06:32:19 -070017 TokenTree::Literal(Literal::i32_suffixed(1)),
Alex Crichton9dc5d0e2018-05-17 11:09:21 -070018 TokenTree::Punct(Punct::new('+', Spacing::Alone)),
David Tolnay7d8b3312019-03-10 01:26:11 -080019 TokenTree::Group(Group::new(
David Tolnay51382052017-12-27 13:46:21 -050020 Delimiter::None,
David Tolnay7d8b3312019-03-10 01:26:11 -080021 TokenStream::from_iter(vec![
Alex Crichton9a4dca22018-03-28 06:32:19 -070022 TokenTree::Literal(Literal::i32_suffixed(2)),
Alex Crichton9dc5d0e2018-05-17 11:09:21 -070023 TokenTree::Punct(Punct::new('+', Spacing::Alone)),
Alex Crichton9a4dca22018-03-28 06:32:19 -070024 TokenTree::Literal(Literal::i32_suffixed(3)),
David Tolnay7d8b3312019-03-10 01:26:11 -080025 ]),
David Tolnay51382052017-12-27 13:46:21 -050026 )),
Alex Crichton9dc5d0e2018-05-17 11:09:21 -070027 TokenTree::Punct(Punct::new('*', Spacing::Alone)),
Alex Crichton9a4dca22018-03-28 06:32:19 -070028 TokenTree::Literal(Literal::i32_suffixed(4)),
David Tolnay7d8b3312019-03-10 01:26:11 -080029 ]);
Michael Layzellbe1e24d2017-06-04 21:15:23 -040030
David Tolnay7d8b3312019-03-10 01:26:11 -080031 assert_eq!(tokens.to_string(), "1i32 + 2i32 + 3i32 * 4i32");
Michael Layzellbe1e24d2017-06-04 21:15:23 -040032
David Tolnayfcd53cf2019-05-09 13:06:59 -070033 snapshot!(tokens as Expr, @r###"
34 Expr::Binary {
35 left: Expr::Lit {
36 lit: 1,
37 },
38 op: Add,
39 right: Expr::Binary {
40 left: Expr::Group {
41 expr: Expr::Binary {
42 left: Expr::Lit {
43 lit: 2,
44 },
45 op: Add,
46 right: Expr::Lit {
47 lit: 3,
48 },
49 },
50 },
51 op: Mul,
52 right: Expr::Lit {
53 lit: 4,
54 },
55 },
56 ⋮}
57 "###);
Michael Layzellbe1e24d2017-06-04 21:15:23 -040058}