David Tolnay | 5553501 | 2018-01-05 16:39:23 -0800 | [diff] [blame] | 1 | // 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 Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 9 | #![cfg(all(feature = "extra-traits", feature = "full"))] |
Nika Layzell | a2a1a4a | 2017-11-19 11:33:17 -0500 | [diff] [blame] | 10 | #![feature(rustc_private)] |
| 11 | |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 12 | #[macro_use] |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 13 | extern crate syn; |
David Tolnay | 360efd2 | 2018-01-04 23:35:26 -0800 | [diff] [blame] | 14 | use syn::{BinOp, Expr, ExprBinary, ExprGroup, ExprLit, Lit}; |
David Tolnay | 42eaae1 | 2017-12-26 23:05:18 -0500 | [diff] [blame] | 15 | use syn::token::Group; |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 16 | |
| 17 | extern crate proc_macro2; |
| 18 | use proc_macro2::*; |
| 19 | |
David Tolnay | dd12556 | 2017-12-31 02:16:22 -0500 | [diff] [blame] | 20 | #[macro_use] |
| 21 | mod macros; |
| 22 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 23 | mod common; |
| 24 | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 25 | fn expr<T: Into<Expr>>(t: T) -> Expr { |
| 26 | t.into() |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | fn lit<T: Into<Literal>>(t: T) -> Expr { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 30 | Expr::Lit(ExprLit { |
| 31 | attrs: Vec::new(), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame^] | 32 | lit: Lit::new(t.into()), |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 33 | }) |
| 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn test_grouping() { |
| 38 | let raw: TokenStream = vec![ |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame^] | 39 | TokenTree::Literal(Literal::i32_suffixed(1)), |
| 40 | TokenTree::Op(Op::new('+', Spacing::Alone)), |
| 41 | TokenTree::Group(proc_macro2::Group::new( |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 42 | Delimiter::None, |
| 43 | vec![ |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame^] | 44 | TokenTree::Literal(Literal::i32_suffixed(2)), |
| 45 | TokenTree::Op(Op::new('+', Spacing::Alone)), |
| 46 | TokenTree::Literal(Literal::i32_suffixed(3)), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 47 | ].into_iter() |
| 48 | .collect(), |
| 49 | )), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame^] | 50 | TokenTree::Op(Op::new('*', Spacing::Alone)), |
| 51 | TokenTree::Literal(Literal::i32_suffixed(4)), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 52 | ].into_iter() |
| 53 | .collect(); |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 54 | |
| 55 | assert_eq!(raw.to_string(), "1i32 + 2i32 + 3i32 * 4i32"); |
| 56 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 57 | assert_eq!( |
| 58 | common::parse::syn::<Expr>(raw), |
| 59 | expr(ExprBinary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 60 | attrs: Vec::new(), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame^] | 61 | left: Box::new(lit(Literal::i32_suffixed(1))), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 62 | op: BinOp::Add(<Token![+]>::default()), |
| 63 | right: Box::new(expr(ExprBinary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 64 | attrs: Vec::new(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 65 | left: Box::new(expr(ExprGroup { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 66 | attrs: Vec::new(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 67 | group_token: Group::default(), |
| 68 | expr: Box::new(expr(ExprBinary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 69 | attrs: Vec::new(), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame^] | 70 | left: Box::new(lit(Literal::i32_suffixed(2))), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 71 | op: BinOp::Add(<Token![+]>::default()), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame^] | 72 | right: Box::new(lit(Literal::i32_suffixed(3))), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 73 | })), |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 74 | })), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 75 | op: BinOp::Mul(<Token![*]>::default()), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame^] | 76 | right: Box::new(lit(Literal::i32_suffixed(4))), |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 77 | })), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 78 | }) |
| 79 | ); |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | #[test] |
| 83 | fn test_invalid_grouping() { |
| 84 | let raw: TokenStream = vec![ |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame^] | 85 | TokenTree::Literal(Literal::i32_suffixed(1)), |
| 86 | TokenTree::Op(Op::new('+', Spacing::Alone)), |
| 87 | TokenTree::Group(proc_macro2::Group::new( |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 88 | Delimiter::None, |
| 89 | vec![ |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame^] | 90 | TokenTree::Literal(Literal::i32_suffixed(2)), |
| 91 | TokenTree::Op(Op::new('+', Spacing::Alone)), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 92 | ].into_iter() |
| 93 | .collect(), |
| 94 | )), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame^] | 95 | TokenTree::Literal(Literal::i32_suffixed(3)), |
| 96 | TokenTree::Op(Op::new('*', Spacing::Alone)), |
| 97 | TokenTree::Literal(Literal::i32_suffixed(4)), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 98 | ].into_iter() |
| 99 | .collect(); |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 100 | |
| 101 | assert_eq!(raw.to_string(), "1i32 + 2i32 + 3i32 * 4i32"); |
| 102 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 103 | assert_eq!( |
| 104 | common::parse::syn::<Expr>(raw), |
| 105 | expr(ExprBinary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 106 | attrs: Vec::new(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 107 | left: Box::new(expr(ExprBinary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 108 | attrs: Vec::new(), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame^] | 109 | left: Box::new(lit(Literal::i32_suffixed(1))), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 110 | op: BinOp::Add(<Token![+]>::default()), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame^] | 111 | right: Box::new(lit(Literal::i32_suffixed(2))), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 112 | })), |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 113 | op: BinOp::Add(<Token![+]>::default()), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 114 | right: Box::new(expr(ExprBinary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 115 | attrs: Vec::new(), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame^] | 116 | left: Box::new(lit(Literal::i32_suffixed(3))), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 117 | op: BinOp::Mul(<Token![*]>::default()), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame^] | 118 | right: Box::new(lit(Literal::i32_suffixed(4))), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 119 | })), |
| 120 | }) |
| 121 | ); |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 122 | } |