blob: 7d031d2cdd4bc3c765d38a85d4edde756b75390a [file] [log] [blame]
Michael Layzellbe1e24d2017-06-04 21:15:23 -04001#![cfg(all(feature = "extra-traits", feature = "full"))]
Nika Layzella2a1a4a2017-11-19 11:33:17 -05002#![feature(rustc_private)]
3
David Tolnayc5ab8c62017-12-26 16:43:39 -05004#[macro_use]
Michael Layzellbe1e24d2017-06-04 21:15:23 -04005extern crate syn;
David Tolnay8c91b882017-12-28 23:04:32 -05006use syn::{BinOp, Expr, ExprBinary, ExprGroup, ExprLit, Lit, LitKind};
David Tolnay42eaae12017-12-26 23:05:18 -05007use syn::token::Group;
Michael Layzellbe1e24d2017-06-04 21:15:23 -04008
9extern crate proc_macro2;
10use proc_macro2::*;
11
David Tolnaydd125562017-12-31 02:16:22 -050012#[macro_use]
13mod macros;
14
David Tolnayc7a5d3d2017-06-04 12:11:05 -070015mod common;
16
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070017fn tt(k: TokenNode) -> TokenTree {
Michael Layzellbe1e24d2017-06-04 21:15:23 -040018 TokenTree {
19 span: Span::default(),
20 kind: k,
21 }
22}
23
David Tolnay8c91b882017-12-28 23:04:32 -050024fn expr<T: Into<Expr>>(t: T) -> Expr {
25 t.into()
Michael Layzellbe1e24d2017-06-04 21:15:23 -040026}
27
28fn lit<T: Into<Literal>>(t: T) -> Expr {
David Tolnay8c91b882017-12-28 23:04:32 -050029 Expr::Lit(ExprLit {
30 attrs: Vec::new(),
31 lit: Lit {
32 value: LitKind::Other(t.into()),
33 span: Span::default(),
34 },
Michael Layzellbe1e24d2017-06-04 21:15:23 -040035 })
36}
37
38#[test]
39fn test_grouping() {
40 let raw: TokenStream = vec![
Alex Crichton605643b2017-07-05 18:35:14 -070041 tt(TokenNode::Literal(Literal::i32(1))),
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070042 tt(TokenNode::Op('+', Spacing::Alone)),
David Tolnay51382052017-12-27 13:46:21 -050043 tt(TokenNode::Group(
44 Delimiter::None,
45 vec![
46 tt(TokenNode::Literal(Literal::i32(2))),
47 tt(TokenNode::Op('+', Spacing::Alone)),
48 tt(TokenNode::Literal(Literal::i32(3))),
49 ].into_iter()
50 .collect(),
51 )),
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070052 tt(TokenNode::Op('*', Spacing::Alone)),
Alex Crichton605643b2017-07-05 18:35:14 -070053 tt(TokenNode::Literal(Literal::i32(4))),
David Tolnay51382052017-12-27 13:46:21 -050054 ].into_iter()
55 .collect();
Michael Layzellbe1e24d2017-06-04 21:15:23 -040056
57 assert_eq!(raw.to_string(), "1i32 + 2i32 + 3i32 * 4i32");
58
David Tolnay51382052017-12-27 13:46:21 -050059 assert_eq!(
60 common::parse::syn::<Expr>(raw),
61 expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050062 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -050063 left: Box::new(lit(Literal::i32(1))),
64 op: BinOp::Add(<Token![+]>::default()),
65 right: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050066 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -050067 left: Box::new(expr(ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -050068 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -050069 group_token: Group::default(),
70 expr: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050071 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -050072 left: Box::new(lit(Literal::i32(2))),
73 op: BinOp::Add(<Token![+]>::default()),
74 right: Box::new(lit(Literal::i32(3))),
75 })),
Michael Layzellbe1e24d2017-06-04 21:15:23 -040076 })),
David Tolnay51382052017-12-27 13:46:21 -050077 op: BinOp::Mul(<Token![*]>::default()),
78 right: Box::new(lit(Literal::i32(4))),
Michael Layzellbe1e24d2017-06-04 21:15:23 -040079 })),
David Tolnay51382052017-12-27 13:46:21 -050080 })
81 );
Michael Layzellbe1e24d2017-06-04 21:15:23 -040082}
83
84#[test]
85fn test_invalid_grouping() {
86 let raw: TokenStream = vec![
Alex Crichton605643b2017-07-05 18:35:14 -070087 tt(TokenNode::Literal(Literal::i32(1))),
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070088 tt(TokenNode::Op('+', Spacing::Alone)),
David Tolnay51382052017-12-27 13:46:21 -050089 tt(TokenNode::Group(
90 Delimiter::None,
91 vec![
92 tt(TokenNode::Literal(Literal::i32(2))),
93 tt(TokenNode::Op('+', Spacing::Alone)),
94 ].into_iter()
95 .collect(),
96 )),
Alex Crichton605643b2017-07-05 18:35:14 -070097 tt(TokenNode::Literal(Literal::i32(3))),
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070098 tt(TokenNode::Op('*', Spacing::Alone)),
Alex Crichton605643b2017-07-05 18:35:14 -070099 tt(TokenNode::Literal(Literal::i32(4))),
David Tolnay51382052017-12-27 13:46:21 -0500100 ].into_iter()
101 .collect();
Michael Layzellbe1e24d2017-06-04 21:15:23 -0400102
103 assert_eq!(raw.to_string(), "1i32 + 2i32 + 3i32 * 4i32");
104
David Tolnay51382052017-12-27 13:46:21 -0500105 assert_eq!(
106 common::parse::syn::<Expr>(raw),
107 expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500108 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -0500109 left: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500110 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -0500111 left: Box::new(lit(Literal::i32(1))),
112 op: BinOp::Add(<Token![+]>::default()),
113 right: Box::new(lit(Literal::i32(2))),
114 })),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800115 op: BinOp::Add(<Token![+]>::default()),
David Tolnay51382052017-12-27 13:46:21 -0500116 right: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500117 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -0500118 left: Box::new(lit(Literal::i32(3))),
119 op: BinOp::Mul(<Token![*]>::default()),
120 right: Box::new(lit(Literal::i32(4))),
121 })),
122 })
123 );
Michael Layzellbe1e24d2017-06-04 21:15:23 -0400124}