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"))] |
David Tolnay | ecd024d | 2018-07-21 09:07:56 -0700 | [diff] [blame] | 10 | #![recursion_limit = "1024"] |
Nika Layzell | a2a1a4a | 2017-11-19 11:33:17 -0500 | [diff] [blame] | 11 | #![feature(rustc_private)] |
| 12 | |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 13 | #[macro_use] |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 14 | extern crate syn; |
David Tolnay | 42eaae1 | 2017-12-26 23:05:18 -0500 | [diff] [blame] | 15 | use syn::token::Group; |
David Tolnay | eb7d79b | 2018-03-31 22:52:17 +0200 | [diff] [blame] | 16 | use syn::{BinOp, Expr, ExprBinary, ExprGroup, ExprLit, Lit}; |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 17 | |
| 18 | extern crate proc_macro2; |
| 19 | use proc_macro2::*; |
| 20 | |
David Tolnay | dd12556 | 2017-12-31 02:16:22 -0500 | [diff] [blame] | 21 | #[macro_use] |
| 22 | mod macros; |
| 23 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 24 | mod common; |
| 25 | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 26 | fn expr<T: Into<Expr>>(t: T) -> Expr { |
| 27 | t.into() |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | fn lit<T: Into<Literal>>(t: T) -> Expr { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 31 | Expr::Lit(ExprLit { |
| 32 | attrs: Vec::new(), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 33 | lit: Lit::new(t.into()), |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 34 | }) |
| 35 | } |
| 36 | |
| 37 | #[test] |
| 38 | fn test_grouping() { |
| 39 | let raw: TokenStream = vec![ |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 40 | TokenTree::Literal(Literal::i32_suffixed(1)), |
Alex Crichton | 9dc5d0e | 2018-05-17 11:09:21 -0700 | [diff] [blame] | 41 | TokenTree::Punct(Punct::new('+', Spacing::Alone)), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 42 | TokenTree::Group(proc_macro2::Group::new( |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 43 | Delimiter::None, |
| 44 | vec![ |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 45 | TokenTree::Literal(Literal::i32_suffixed(2)), |
Alex Crichton | 9dc5d0e | 2018-05-17 11:09:21 -0700 | [diff] [blame] | 46 | TokenTree::Punct(Punct::new('+', Spacing::Alone)), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 47 | TokenTree::Literal(Literal::i32_suffixed(3)), |
David Tolnay | fb84fc0 | 2018-10-02 21:01:30 -0700 | [diff] [blame^] | 48 | ] |
| 49 | .into_iter() |
David Tolnay | a25d1dc | 2018-07-31 21:46:19 -0700 | [diff] [blame] | 50 | .collect(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 51 | )), |
Alex Crichton | 9dc5d0e | 2018-05-17 11:09:21 -0700 | [diff] [blame] | 52 | TokenTree::Punct(Punct::new('*', Spacing::Alone)), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 53 | TokenTree::Literal(Literal::i32_suffixed(4)), |
David Tolnay | fb84fc0 | 2018-10-02 21:01:30 -0700 | [diff] [blame^] | 54 | ] |
| 55 | .into_iter() |
David Tolnay | a25d1dc | 2018-07-31 21:46:19 -0700 | [diff] [blame] | 56 | .collect(); |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 57 | |
| 58 | assert_eq!(raw.to_string(), "1i32 + 2i32 + 3i32 * 4i32"); |
| 59 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 60 | assert_eq!( |
David Tolnay | 59ffad9 | 2018-08-30 21:01:04 -0700 | [diff] [blame] | 61 | syn::parse2::<syn::Expr>(raw).unwrap(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 62 | expr(ExprBinary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 63 | attrs: Vec::new(), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 64 | left: Box::new(lit(Literal::i32_suffixed(1))), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 65 | op: BinOp::Add(<Token![+]>::default()), |
| 66 | right: Box::new(expr(ExprBinary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 67 | attrs: Vec::new(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 68 | left: Box::new(expr(ExprGroup { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 69 | attrs: Vec::new(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 70 | group_token: Group::default(), |
| 71 | expr: Box::new(expr(ExprBinary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 72 | attrs: Vec::new(), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 73 | left: Box::new(lit(Literal::i32_suffixed(2))), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 74 | op: BinOp::Add(<Token![+]>::default()), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 75 | right: Box::new(lit(Literal::i32_suffixed(3))), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 76 | })), |
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 | op: BinOp::Mul(<Token![*]>::default()), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 79 | right: Box::new(lit(Literal::i32_suffixed(4))), |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 80 | })), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 81 | }) |
| 82 | ); |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 83 | } |