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 | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 48 | ].into_iter() |
| 49 | .collect(), |
| 50 | )), |
Alex Crichton | 9dc5d0e | 2018-05-17 11:09:21 -0700 | [diff] [blame] | 51 | TokenTree::Punct(Punct::new('*', Spacing::Alone)), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 52 | TokenTree::Literal(Literal::i32_suffixed(4)), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 53 | ].into_iter() |
| 54 | .collect(); |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 55 | |
| 56 | assert_eq!(raw.to_string(), "1i32 + 2i32 + 3i32 * 4i32"); |
| 57 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 58 | assert_eq!( |
| 59 | common::parse::syn::<Expr>(raw), |
| 60 | expr(ExprBinary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 61 | attrs: Vec::new(), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 62 | left: Box::new(lit(Literal::i32_suffixed(1))), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 63 | op: BinOp::Add(<Token![+]>::default()), |
| 64 | right: Box::new(expr(ExprBinary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 65 | attrs: Vec::new(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 66 | left: Box::new(expr(ExprGroup { |
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 | group_token: Group::default(), |
| 69 | expr: Box::new(expr(ExprBinary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 70 | attrs: Vec::new(), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 71 | left: Box::new(lit(Literal::i32_suffixed(2))), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 72 | op: BinOp::Add(<Token![+]>::default()), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 73 | right: Box::new(lit(Literal::i32_suffixed(3))), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 74 | })), |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 75 | })), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 76 | op: BinOp::Mul(<Token![*]>::default()), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 77 | right: Box::new(lit(Literal::i32_suffixed(4))), |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 78 | })), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 79 | }) |
| 80 | ); |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | #[test] |
| 84 | fn test_invalid_grouping() { |
| 85 | let raw: TokenStream = vec![ |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 86 | TokenTree::Literal(Literal::i32_suffixed(1)), |
Alex Crichton | 9dc5d0e | 2018-05-17 11:09:21 -0700 | [diff] [blame] | 87 | TokenTree::Punct(Punct::new('+', Spacing::Alone)), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 88 | TokenTree::Group(proc_macro2::Group::new( |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 89 | Delimiter::None, |
| 90 | vec![ |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 91 | TokenTree::Literal(Literal::i32_suffixed(2)), |
Alex Crichton | 9dc5d0e | 2018-05-17 11:09:21 -0700 | [diff] [blame] | 92 | TokenTree::Punct(Punct::new('+', Spacing::Alone)), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 93 | ].into_iter() |
| 94 | .collect(), |
| 95 | )), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 96 | TokenTree::Literal(Literal::i32_suffixed(3)), |
Alex Crichton | 9dc5d0e | 2018-05-17 11:09:21 -0700 | [diff] [blame] | 97 | TokenTree::Punct(Punct::new('*', Spacing::Alone)), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 98 | TokenTree::Literal(Literal::i32_suffixed(4)), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 99 | ].into_iter() |
| 100 | .collect(); |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 101 | |
| 102 | assert_eq!(raw.to_string(), "1i32 + 2i32 + 3i32 * 4i32"); |
| 103 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 104 | assert_eq!( |
| 105 | common::parse::syn::<Expr>(raw), |
| 106 | expr(ExprBinary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 107 | attrs: Vec::new(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 108 | left: Box::new(expr(ExprBinary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 109 | attrs: Vec::new(), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 110 | left: Box::new(lit(Literal::i32_suffixed(1))), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 111 | op: BinOp::Add(<Token![+]>::default()), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 112 | right: Box::new(lit(Literal::i32_suffixed(2))), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 113 | })), |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 114 | op: BinOp::Add(<Token![+]>::default()), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 115 | right: Box::new(expr(ExprBinary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 116 | attrs: Vec::new(), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 117 | left: Box::new(lit(Literal::i32_suffixed(3))), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 118 | op: BinOp::Mul(<Token![*]>::default()), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 119 | right: Box::new(lit(Literal::i32_suffixed(4))), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 120 | })), |
| 121 | }) |
| 122 | ); |
Michael Layzell | be1e24d | 2017-06-04 21:15:23 -0400 | [diff] [blame] | 123 | } |