blob: 619e9fff2bdde4a03fd4f913d8106a5b457503fb [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"))]
David Tolnayecd024d2018-07-21 09:07:56 -070010#![recursion_limit = "1024"]
Nika Layzella2a1a4a2017-11-19 11:33:17 -050011#![feature(rustc_private)]
12
David Tolnayc5ab8c62017-12-26 16:43:39 -050013#[macro_use]
Michael Layzellbe1e24d2017-06-04 21:15:23 -040014extern crate syn;
David Tolnay42eaae12017-12-26 23:05:18 -050015use syn::token::Group;
David Tolnayeb7d79b2018-03-31 22:52:17 +020016use syn::{BinOp, Expr, ExprBinary, ExprGroup, ExprLit, Lit};
Michael Layzellbe1e24d2017-06-04 21:15:23 -040017
18extern crate proc_macro2;
19use proc_macro2::*;
20
David Tolnaydd125562017-12-31 02:16:22 -050021#[macro_use]
22mod macros;
23
David Tolnayc7a5d3d2017-06-04 12:11:05 -070024mod common;
25
David Tolnay8c91b882017-12-28 23:04:32 -050026fn expr<T: Into<Expr>>(t: T) -> Expr {
27 t.into()
Michael Layzellbe1e24d2017-06-04 21:15:23 -040028}
29
30fn lit<T: Into<Literal>>(t: T) -> Expr {
David Tolnay8c91b882017-12-28 23:04:32 -050031 Expr::Lit(ExprLit {
32 attrs: Vec::new(),
Alex Crichton9a4dca22018-03-28 06:32:19 -070033 lit: Lit::new(t.into()),
Michael Layzellbe1e24d2017-06-04 21:15:23 -040034 })
35}
36
37#[test]
38fn test_grouping() {
39 let raw: TokenStream = vec![
Alex Crichton9a4dca22018-03-28 06:32:19 -070040 TokenTree::Literal(Literal::i32_suffixed(1)),
Alex Crichton9dc5d0e2018-05-17 11:09:21 -070041 TokenTree::Punct(Punct::new('+', Spacing::Alone)),
Alex Crichton9a4dca22018-03-28 06:32:19 -070042 TokenTree::Group(proc_macro2::Group::new(
David Tolnay51382052017-12-27 13:46:21 -050043 Delimiter::None,
44 vec![
Alex Crichton9a4dca22018-03-28 06:32:19 -070045 TokenTree::Literal(Literal::i32_suffixed(2)),
Alex Crichton9dc5d0e2018-05-17 11:09:21 -070046 TokenTree::Punct(Punct::new('+', Spacing::Alone)),
Alex Crichton9a4dca22018-03-28 06:32:19 -070047 TokenTree::Literal(Literal::i32_suffixed(3)),
David Tolnay51382052017-12-27 13:46:21 -050048 ].into_iter()
49 .collect(),
50 )),
Alex Crichton9dc5d0e2018-05-17 11:09:21 -070051 TokenTree::Punct(Punct::new('*', Spacing::Alone)),
Alex Crichton9a4dca22018-03-28 06:32:19 -070052 TokenTree::Literal(Literal::i32_suffixed(4)),
David Tolnay51382052017-12-27 13:46:21 -050053 ].into_iter()
54 .collect();
Michael Layzellbe1e24d2017-06-04 21:15:23 -040055
56 assert_eq!(raw.to_string(), "1i32 + 2i32 + 3i32 * 4i32");
57
David Tolnay51382052017-12-27 13:46:21 -050058 assert_eq!(
59 common::parse::syn::<Expr>(raw),
60 expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050061 attrs: Vec::new(),
Alex Crichton9a4dca22018-03-28 06:32:19 -070062 left: Box::new(lit(Literal::i32_suffixed(1))),
David Tolnay51382052017-12-27 13:46:21 -050063 op: BinOp::Add(<Token![+]>::default()),
64 right: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050065 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -050066 left: Box::new(expr(ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -050067 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -050068 group_token: Group::default(),
69 expr: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050070 attrs: Vec::new(),
Alex Crichton9a4dca22018-03-28 06:32:19 -070071 left: Box::new(lit(Literal::i32_suffixed(2))),
David Tolnay51382052017-12-27 13:46:21 -050072 op: BinOp::Add(<Token![+]>::default()),
Alex Crichton9a4dca22018-03-28 06:32:19 -070073 right: Box::new(lit(Literal::i32_suffixed(3))),
David Tolnay51382052017-12-27 13:46:21 -050074 })),
Michael Layzellbe1e24d2017-06-04 21:15:23 -040075 })),
David Tolnay51382052017-12-27 13:46:21 -050076 op: BinOp::Mul(<Token![*]>::default()),
Alex Crichton9a4dca22018-03-28 06:32:19 -070077 right: Box::new(lit(Literal::i32_suffixed(4))),
Michael Layzellbe1e24d2017-06-04 21:15:23 -040078 })),
David Tolnay51382052017-12-27 13:46:21 -050079 })
80 );
Michael Layzellbe1e24d2017-06-04 21:15:23 -040081}
82
83#[test]
84fn test_invalid_grouping() {
85 let raw: TokenStream = vec![
Alex Crichton9a4dca22018-03-28 06:32:19 -070086 TokenTree::Literal(Literal::i32_suffixed(1)),
Alex Crichton9dc5d0e2018-05-17 11:09:21 -070087 TokenTree::Punct(Punct::new('+', Spacing::Alone)),
Alex Crichton9a4dca22018-03-28 06:32:19 -070088 TokenTree::Group(proc_macro2::Group::new(
David Tolnay51382052017-12-27 13:46:21 -050089 Delimiter::None,
90 vec![
Alex Crichton9a4dca22018-03-28 06:32:19 -070091 TokenTree::Literal(Literal::i32_suffixed(2)),
Alex Crichton9dc5d0e2018-05-17 11:09:21 -070092 TokenTree::Punct(Punct::new('+', Spacing::Alone)),
David Tolnay51382052017-12-27 13:46:21 -050093 ].into_iter()
94 .collect(),
95 )),
Alex Crichton9a4dca22018-03-28 06:32:19 -070096 TokenTree::Literal(Literal::i32_suffixed(3)),
Alex Crichton9dc5d0e2018-05-17 11:09:21 -070097 TokenTree::Punct(Punct::new('*', Spacing::Alone)),
Alex Crichton9a4dca22018-03-28 06:32:19 -070098 TokenTree::Literal(Literal::i32_suffixed(4)),
David Tolnay51382052017-12-27 13:46:21 -050099 ].into_iter()
100 .collect();
Michael Layzellbe1e24d2017-06-04 21:15:23 -0400101
102 assert_eq!(raw.to_string(), "1i32 + 2i32 + 3i32 * 4i32");
103
David Tolnay51382052017-12-27 13:46:21 -0500104 assert_eq!(
105 common::parse::syn::<Expr>(raw),
106 expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500107 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -0500108 left: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500109 attrs: Vec::new(),
Alex Crichton9a4dca22018-03-28 06:32:19 -0700110 left: Box::new(lit(Literal::i32_suffixed(1))),
David Tolnay51382052017-12-27 13:46:21 -0500111 op: BinOp::Add(<Token![+]>::default()),
Alex Crichton9a4dca22018-03-28 06:32:19 -0700112 right: Box::new(lit(Literal::i32_suffixed(2))),
David Tolnay51382052017-12-27 13:46:21 -0500113 })),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800114 op: BinOp::Add(<Token![+]>::default()),
David Tolnay51382052017-12-27 13:46:21 -0500115 right: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500116 attrs: Vec::new(),
Alex Crichton9a4dca22018-03-28 06:32:19 -0700117 left: Box::new(lit(Literal::i32_suffixed(3))),
David Tolnay51382052017-12-27 13:46:21 -0500118 op: BinOp::Mul(<Token![*]>::default()),
Alex Crichton9a4dca22018-03-28 06:32:19 -0700119 right: Box::new(lit(Literal::i32_suffixed(4))),
David Tolnay51382052017-12-27 13:46:21 -0500120 })),
121 })
122 );
Michael Layzellbe1e24d2017-06-04 21:15:23 -0400123}