blob: 3e9e8f8d00921dbf1ad527647ff17b213d79b129 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
Michael Layzellbe1e24d2017-06-04 21:15:23 -04009#![cfg(all(feature = "extra-traits", feature = "full"))]
Nika Layzella2a1a4a2017-11-19 11:33:17 -050010#![feature(rustc_private)]
11
David Tolnayc5ab8c62017-12-26 16:43:39 -050012#[macro_use]
Michael Layzellbe1e24d2017-06-04 21:15:23 -040013extern crate syn;
David Tolnay360efd22018-01-04 23:35:26 -080014use syn::{BinOp, Expr, ExprBinary, ExprGroup, ExprLit, Lit};
David Tolnay42eaae12017-12-26 23:05:18 -050015use syn::token::Group;
Michael Layzellbe1e24d2017-06-04 21:15:23 -040016
17extern crate proc_macro2;
18use proc_macro2::*;
19
David Tolnaydd125562017-12-31 02:16:22 -050020#[macro_use]
21mod macros;
22
David Tolnayc7a5d3d2017-06-04 12:11:05 -070023mod common;
24
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070025fn tt(k: TokenNode) -> TokenTree {
Michael Layzellbe1e24d2017-06-04 21:15:23 -040026 TokenTree {
27 span: Span::default(),
28 kind: k,
29 }
30}
31
David Tolnay8c91b882017-12-28 23:04:32 -050032fn expr<T: Into<Expr>>(t: T) -> Expr {
33 t.into()
Michael Layzellbe1e24d2017-06-04 21:15:23 -040034}
35
36fn lit<T: Into<Literal>>(t: T) -> Expr {
David Tolnay8c91b882017-12-28 23:04:32 -050037 Expr::Lit(ExprLit {
38 attrs: Vec::new(),
David Tolnay360efd22018-01-04 23:35:26 -080039 lit: Lit::new(t.into(), Span::default()),
Michael Layzellbe1e24d2017-06-04 21:15:23 -040040 })
41}
42
43#[test]
44fn test_grouping() {
45 let raw: TokenStream = vec![
Alex Crichton605643b2017-07-05 18:35:14 -070046 tt(TokenNode::Literal(Literal::i32(1))),
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070047 tt(TokenNode::Op('+', Spacing::Alone)),
David Tolnay51382052017-12-27 13:46:21 -050048 tt(TokenNode::Group(
49 Delimiter::None,
50 vec![
51 tt(TokenNode::Literal(Literal::i32(2))),
52 tt(TokenNode::Op('+', Spacing::Alone)),
53 tt(TokenNode::Literal(Literal::i32(3))),
54 ].into_iter()
55 .collect(),
56 )),
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070057 tt(TokenNode::Op('*', Spacing::Alone)),
Alex Crichton605643b2017-07-05 18:35:14 -070058 tt(TokenNode::Literal(Literal::i32(4))),
David Tolnay51382052017-12-27 13:46:21 -050059 ].into_iter()
60 .collect();
Michael Layzellbe1e24d2017-06-04 21:15:23 -040061
62 assert_eq!(raw.to_string(), "1i32 + 2i32 + 3i32 * 4i32");
63
David Tolnay51382052017-12-27 13:46:21 -050064 assert_eq!(
65 common::parse::syn::<Expr>(raw),
66 expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050067 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -050068 left: Box::new(lit(Literal::i32(1))),
69 op: BinOp::Add(<Token![+]>::default()),
70 right: 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(expr(ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -050073 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -050074 group_token: Group::default(),
75 expr: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050076 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -050077 left: Box::new(lit(Literal::i32(2))),
78 op: BinOp::Add(<Token![+]>::default()),
79 right: Box::new(lit(Literal::i32(3))),
80 })),
Michael Layzellbe1e24d2017-06-04 21:15:23 -040081 })),
David Tolnay51382052017-12-27 13:46:21 -050082 op: BinOp::Mul(<Token![*]>::default()),
83 right: Box::new(lit(Literal::i32(4))),
Michael Layzellbe1e24d2017-06-04 21:15:23 -040084 })),
David Tolnay51382052017-12-27 13:46:21 -050085 })
86 );
Michael Layzellbe1e24d2017-06-04 21:15:23 -040087}
88
89#[test]
90fn test_invalid_grouping() {
91 let raw: TokenStream = vec![
Alex Crichton605643b2017-07-05 18:35:14 -070092 tt(TokenNode::Literal(Literal::i32(1))),
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070093 tt(TokenNode::Op('+', Spacing::Alone)),
David Tolnay51382052017-12-27 13:46:21 -050094 tt(TokenNode::Group(
95 Delimiter::None,
96 vec![
97 tt(TokenNode::Literal(Literal::i32(2))),
98 tt(TokenNode::Op('+', Spacing::Alone)),
99 ].into_iter()
100 .collect(),
101 )),
Alex Crichton605643b2017-07-05 18:35:14 -0700102 tt(TokenNode::Literal(Literal::i32(3))),
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700103 tt(TokenNode::Op('*', Spacing::Alone)),
Alex Crichton605643b2017-07-05 18:35:14 -0700104 tt(TokenNode::Literal(Literal::i32(4))),
David Tolnay51382052017-12-27 13:46:21 -0500105 ].into_iter()
106 .collect();
Michael Layzellbe1e24d2017-06-04 21:15:23 -0400107
108 assert_eq!(raw.to_string(), "1i32 + 2i32 + 3i32 * 4i32");
109
David Tolnay51382052017-12-27 13:46:21 -0500110 assert_eq!(
111 common::parse::syn::<Expr>(raw),
112 expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500113 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -0500114 left: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500115 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -0500116 left: Box::new(lit(Literal::i32(1))),
117 op: BinOp::Add(<Token![+]>::default()),
118 right: Box::new(lit(Literal::i32(2))),
119 })),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800120 op: BinOp::Add(<Token![+]>::default()),
David Tolnay51382052017-12-27 13:46:21 -0500121 right: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500122 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -0500123 left: Box::new(lit(Literal::i32(3))),
124 op: BinOp::Mul(<Token![*]>::default()),
125 right: Box::new(lit(Literal::i32(4))),
126 })),
127 })
128 );
Michael Layzellbe1e24d2017-06-04 21:15:23 -0400129}