blob: 7d2bcc6286f39eb889a4a2e2a3d411862af04dcf [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
David Tolnayecd024d2018-07-21 09:07:56 -07009#![recursion_limit = "1024"]
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 Tolnay42eaae12017-12-26 23:05:18 -050014use syn::token::Group;
David Tolnayeb7d79b2018-03-31 22:52:17 +020015use syn::{BinOp, Expr, ExprBinary, ExprGroup, ExprLit, Lit};
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;
David Tolnayc3f98562018-11-02 08:55:05 -070024mod features;
David Tolnayc7a5d3d2017-06-04 12:11:05 -070025
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 Tolnayfb84fc02018-10-02 21:01:30 -070048 ]
49 .into_iter()
David Tolnaya25d1dc2018-07-31 21:46:19 -070050 .collect(),
David Tolnay51382052017-12-27 13:46:21 -050051 )),
Alex Crichton9dc5d0e2018-05-17 11:09:21 -070052 TokenTree::Punct(Punct::new('*', Spacing::Alone)),
Alex Crichton9a4dca22018-03-28 06:32:19 -070053 TokenTree::Literal(Literal::i32_suffixed(4)),
David Tolnayfb84fc02018-10-02 21:01:30 -070054 ]
55 .into_iter()
David Tolnaya25d1dc2018-07-31 21:46:19 -070056 .collect();
Michael Layzellbe1e24d2017-06-04 21:15:23 -040057
58 assert_eq!(raw.to_string(), "1i32 + 2i32 + 3i32 * 4i32");
59
David Tolnay51382052017-12-27 13:46:21 -050060 assert_eq!(
David Tolnay59ffad92018-08-30 21:01:04 -070061 syn::parse2::<syn::Expr>(raw).unwrap(),
David Tolnay51382052017-12-27 13:46:21 -050062 expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050063 attrs: Vec::new(),
Alex Crichton9a4dca22018-03-28 06:32:19 -070064 left: Box::new(lit(Literal::i32_suffixed(1))),
David Tolnay51382052017-12-27 13:46:21 -050065 op: BinOp::Add(<Token![+]>::default()),
66 right: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050067 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -050068 left: Box::new(expr(ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -050069 attrs: Vec::new(),
David Tolnay51382052017-12-27 13:46:21 -050070 group_token: Group::default(),
71 expr: Box::new(expr(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050072 attrs: Vec::new(),
Alex Crichton9a4dca22018-03-28 06:32:19 -070073 left: Box::new(lit(Literal::i32_suffixed(2))),
David Tolnay51382052017-12-27 13:46:21 -050074 op: BinOp::Add(<Token![+]>::default()),
Alex Crichton9a4dca22018-03-28 06:32:19 -070075 right: Box::new(lit(Literal::i32_suffixed(3))),
David Tolnay51382052017-12-27 13:46:21 -050076 })),
Michael Layzellbe1e24d2017-06-04 21:15:23 -040077 })),
David Tolnay51382052017-12-27 13:46:21 -050078 op: BinOp::Mul(<Token![*]>::default()),
Alex Crichton9a4dca22018-03-28 06:32:19 -070079 right: Box::new(lit(Literal::i32_suffixed(4))),
Michael Layzellbe1e24d2017-06-04 21:15:23 -040080 })),
David Tolnay51382052017-12-27 13:46:21 -050081 })
82 );
Michael Layzellbe1e24d2017-06-04 21:15:23 -040083}