blob: ca686f6940d656e6939d1fbb6267d72e2fe93813 [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
David Tolnay8c91b882017-12-28 23:04:32 -050025fn expr<T: Into<Expr>>(t: T) -> Expr {
26 t.into()
Michael Layzellbe1e24d2017-06-04 21:15:23 -040027}
28
29fn lit<T: Into<Literal>>(t: T) -> Expr {
David Tolnay8c91b882017-12-28 23:04:32 -050030 Expr::Lit(ExprLit {
31 attrs: Vec::new(),
Alex Crichton9a4dca22018-03-28 06:32:19 -070032 lit: Lit::new(t.into()),
Michael Layzellbe1e24d2017-06-04 21:15:23 -040033 })
34}
35
36#[test]
37fn test_grouping() {
38 let raw: TokenStream = vec![
Alex Crichton9a4dca22018-03-28 06:32:19 -070039 TokenTree::Literal(Literal::i32_suffixed(1)),
40 TokenTree::Op(Op::new('+', Spacing::Alone)),
41 TokenTree::Group(proc_macro2::Group::new(
David Tolnay51382052017-12-27 13:46:21 -050042 Delimiter::None,
43 vec![
Alex Crichton9a4dca22018-03-28 06:32:19 -070044 TokenTree::Literal(Literal::i32_suffixed(2)),
45 TokenTree::Op(Op::new('+', Spacing::Alone)),
46 TokenTree::Literal(Literal::i32_suffixed(3)),
David Tolnay51382052017-12-27 13:46:21 -050047 ].into_iter()
48 .collect(),
49 )),
Alex Crichton9a4dca22018-03-28 06:32:19 -070050 TokenTree::Op(Op::new('*', Spacing::Alone)),
51 TokenTree::Literal(Literal::i32_suffixed(4)),
David Tolnay51382052017-12-27 13:46:21 -050052 ].into_iter()
53 .collect();
Michael Layzellbe1e24d2017-06-04 21:15:23 -040054
55 assert_eq!(raw.to_string(), "1i32 + 2i32 + 3i32 * 4i32");
56
David Tolnay51382052017-12-27 13:46:21 -050057 assert_eq!(
58 common::parse::syn::<Expr>(raw),
59 expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050060 attrs: Vec::new(),
Alex Crichton9a4dca22018-03-28 06:32:19 -070061 left: Box::new(lit(Literal::i32_suffixed(1))),
David Tolnay51382052017-12-27 13:46:21 -050062 op: BinOp::Add(<Token![+]>::default()),
63 right: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050064 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -050065 left: Box::new(expr(ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -050066 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -050067 group_token: Group::default(),
68 expr: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050069 attrs: Vec::new(),
Alex Crichton9a4dca22018-03-28 06:32:19 -070070 left: Box::new(lit(Literal::i32_suffixed(2))),
David Tolnay51382052017-12-27 13:46:21 -050071 op: BinOp::Add(<Token![+]>::default()),
Alex Crichton9a4dca22018-03-28 06:32:19 -070072 right: Box::new(lit(Literal::i32_suffixed(3))),
David Tolnay51382052017-12-27 13:46:21 -050073 })),
Michael Layzellbe1e24d2017-06-04 21:15:23 -040074 })),
David Tolnay51382052017-12-27 13:46:21 -050075 op: BinOp::Mul(<Token![*]>::default()),
Alex Crichton9a4dca22018-03-28 06:32:19 -070076 right: Box::new(lit(Literal::i32_suffixed(4))),
Michael Layzellbe1e24d2017-06-04 21:15:23 -040077 })),
David Tolnay51382052017-12-27 13:46:21 -050078 })
79 );
Michael Layzellbe1e24d2017-06-04 21:15:23 -040080}
81
82#[test]
83fn test_invalid_grouping() {
84 let raw: TokenStream = vec![
Alex Crichton9a4dca22018-03-28 06:32:19 -070085 TokenTree::Literal(Literal::i32_suffixed(1)),
86 TokenTree::Op(Op::new('+', Spacing::Alone)),
87 TokenTree::Group(proc_macro2::Group::new(
David Tolnay51382052017-12-27 13:46:21 -050088 Delimiter::None,
89 vec![
Alex Crichton9a4dca22018-03-28 06:32:19 -070090 TokenTree::Literal(Literal::i32_suffixed(2)),
91 TokenTree::Op(Op::new('+', Spacing::Alone)),
David Tolnay51382052017-12-27 13:46:21 -050092 ].into_iter()
93 .collect(),
94 )),
Alex Crichton9a4dca22018-03-28 06:32:19 -070095 TokenTree::Literal(Literal::i32_suffixed(3)),
96 TokenTree::Op(Op::new('*', Spacing::Alone)),
97 TokenTree::Literal(Literal::i32_suffixed(4)),
David Tolnay51382052017-12-27 13:46:21 -050098 ].into_iter()
99 .collect();
Michael Layzellbe1e24d2017-06-04 21:15:23 -0400100
101 assert_eq!(raw.to_string(), "1i32 + 2i32 + 3i32 * 4i32");
102
David Tolnay51382052017-12-27 13:46:21 -0500103 assert_eq!(
104 common::parse::syn::<Expr>(raw),
105 expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500106 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -0500107 left: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500108 attrs: Vec::new(),
Alex Crichton9a4dca22018-03-28 06:32:19 -0700109 left: Box::new(lit(Literal::i32_suffixed(1))),
David Tolnay51382052017-12-27 13:46:21 -0500110 op: BinOp::Add(<Token![+]>::default()),
Alex Crichton9a4dca22018-03-28 06:32:19 -0700111 right: Box::new(lit(Literal::i32_suffixed(2))),
David Tolnay51382052017-12-27 13:46:21 -0500112 })),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800113 op: BinOp::Add(<Token![+]>::default()),
David Tolnay51382052017-12-27 13:46:21 -0500114 right: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500115 attrs: Vec::new(),
Alex Crichton9a4dca22018-03-28 06:32:19 -0700116 left: Box::new(lit(Literal::i32_suffixed(3))),
David Tolnay51382052017-12-27 13:46:21 -0500117 op: BinOp::Mul(<Token![*]>::default()),
Alex Crichton9a4dca22018-03-28 06:32:19 -0700118 right: Box::new(lit(Literal::i32_suffixed(4))),
David Tolnay51382052017-12-27 13:46:21 -0500119 })),
120 })
121 );
Michael Layzellbe1e24d2017-06-04 21:15:23 -0400122}