blob: 7f362d15d48ff07c2a99ca9e5d80e76f7b9970b1 [file] [log] [blame]
David Tolnayecd024d2018-07-21 09:07:56 -07001#![recursion_limit = "1024"]
Nika Layzella2a1a4a2017-11-19 11:33:17 -05002
David Tolnayc5ab8c62017-12-26 16:43:39 -05003#[macro_use]
Michael Layzellbe1e24d2017-06-04 21:15:23 -04004extern crate syn;
David Tolnay42eaae12017-12-26 23:05:18 -05005use syn::token::Group;
David Tolnayeb7d79b2018-03-31 22:52:17 +02006use syn::{BinOp, Expr, ExprBinary, ExprGroup, ExprLit, Lit};
Michael Layzellbe1e24d2017-06-04 21:15:23 -04007
8extern crate proc_macro2;
9use proc_macro2::*;
10
David Tolnayc3f98562018-11-02 08:55:05 -070011mod features;
David Tolnayc7a5d3d2017-06-04 12:11:05 -070012
David Tolnay8c91b882017-12-28 23:04:32 -050013fn expr<T: Into<Expr>>(t: T) -> Expr {
14 t.into()
Michael Layzellbe1e24d2017-06-04 21:15:23 -040015}
16
17fn lit<T: Into<Literal>>(t: T) -> Expr {
David Tolnay8c91b882017-12-28 23:04:32 -050018 Expr::Lit(ExprLit {
19 attrs: Vec::new(),
Alex Crichton9a4dca22018-03-28 06:32:19 -070020 lit: Lit::new(t.into()),
Michael Layzellbe1e24d2017-06-04 21:15:23 -040021 })
22}
23
24#[test]
25fn test_grouping() {
26 let raw: TokenStream = vec![
Alex Crichton9a4dca22018-03-28 06:32:19 -070027 TokenTree::Literal(Literal::i32_suffixed(1)),
Alex Crichton9dc5d0e2018-05-17 11:09:21 -070028 TokenTree::Punct(Punct::new('+', Spacing::Alone)),
Alex Crichton9a4dca22018-03-28 06:32:19 -070029 TokenTree::Group(proc_macro2::Group::new(
David Tolnay51382052017-12-27 13:46:21 -050030 Delimiter::None,
31 vec![
Alex Crichton9a4dca22018-03-28 06:32:19 -070032 TokenTree::Literal(Literal::i32_suffixed(2)),
Alex Crichton9dc5d0e2018-05-17 11:09:21 -070033 TokenTree::Punct(Punct::new('+', Spacing::Alone)),
Alex Crichton9a4dca22018-03-28 06:32:19 -070034 TokenTree::Literal(Literal::i32_suffixed(3)),
David Tolnayfb84fc02018-10-02 21:01:30 -070035 ]
36 .into_iter()
David Tolnaya25d1dc2018-07-31 21:46:19 -070037 .collect(),
David Tolnay51382052017-12-27 13:46:21 -050038 )),
Alex Crichton9dc5d0e2018-05-17 11:09:21 -070039 TokenTree::Punct(Punct::new('*', Spacing::Alone)),
Alex Crichton9a4dca22018-03-28 06:32:19 -070040 TokenTree::Literal(Literal::i32_suffixed(4)),
David Tolnayfb84fc02018-10-02 21:01:30 -070041 ]
42 .into_iter()
David Tolnaya25d1dc2018-07-31 21:46:19 -070043 .collect();
Michael Layzellbe1e24d2017-06-04 21:15:23 -040044
45 assert_eq!(raw.to_string(), "1i32 + 2i32 + 3i32 * 4i32");
46
David Tolnay51382052017-12-27 13:46:21 -050047 assert_eq!(
David Tolnay59ffad92018-08-30 21:01:04 -070048 syn::parse2::<syn::Expr>(raw).unwrap(),
David Tolnay51382052017-12-27 13:46:21 -050049 expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050050 attrs: Vec::new(),
Alex Crichton9a4dca22018-03-28 06:32:19 -070051 left: Box::new(lit(Literal::i32_suffixed(1))),
David Tolnay51382052017-12-27 13:46:21 -050052 op: BinOp::Add(<Token![+]>::default()),
53 right: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050054 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -050055 left: Box::new(expr(ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -050056 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -050057 group_token: Group::default(),
58 expr: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050059 attrs: Vec::new(),
Alex Crichton9a4dca22018-03-28 06:32:19 -070060 left: Box::new(lit(Literal::i32_suffixed(2))),
David Tolnay51382052017-12-27 13:46:21 -050061 op: BinOp::Add(<Token![+]>::default()),
Alex Crichton9a4dca22018-03-28 06:32:19 -070062 right: Box::new(lit(Literal::i32_suffixed(3))),
David Tolnay51382052017-12-27 13:46:21 -050063 })),
Michael Layzellbe1e24d2017-06-04 21:15:23 -040064 })),
David Tolnay51382052017-12-27 13:46:21 -050065 op: BinOp::Mul(<Token![*]>::default()),
Alex Crichton9a4dca22018-03-28 06:32:19 -070066 right: Box::new(lit(Literal::i32_suffixed(4))),
Michael Layzellbe1e24d2017-06-04 21:15:23 -040067 })),
David Tolnay51382052017-12-27 13:46:21 -050068 })
69 );
Michael Layzellbe1e24d2017-06-04 21:15:23 -040070}