]>
Commit | Line | Data |
---|---|---|
c34b1796 | 1 | // Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
223e47cc LB |
2 | // file at the top-level directory of this distribution and at |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
d9579d0f | 11 | // ignore-cross-compile |
223e47cc | 12 | |
d9579d0f | 13 | #![feature(quote, rustc_private)] |
1a4d82fc JJ |
14 | |
15 | extern crate syntax; | |
223e47cc | 16 | |
d9579d0f AL |
17 | use syntax::codemap::DUMMY_SP; |
18 | use syntax::print::pprust::*; | |
e9174d1e | 19 | use syntax::parse::token::intern; |
223e47cc | 20 | |
d9579d0f | 21 | fn main() { |
62682a34 | 22 | let ps = syntax::parse::ParseSess::new(); |
e9174d1e | 23 | let mut feature_gated_cfgs = vec![]; |
d9579d0f AL |
24 | let mut cx = syntax::ext::base::ExtCtxt::new( |
25 | &ps, vec![], | |
e9174d1e SL |
26 | syntax::ext::expand::ExpansionConfig::default("qquote".to_string()), |
27 | &mut feature_gated_cfgs); | |
d9579d0f AL |
28 | cx.bt_push(syntax::codemap::ExpnInfo { |
29 | call_site: DUMMY_SP, | |
30 | callee: syntax::codemap::NameAndSpan { | |
e9174d1e | 31 | format: syntax::codemap::MacroBang(intern("")), |
d9579d0f AL |
32 | allow_internal_unstable: false, |
33 | span: None, | |
223e47cc | 34 | } |
d9579d0f AL |
35 | }); |
36 | let cx = &mut cx; | |
223e47cc | 37 | |
d9579d0f AL |
38 | macro_rules! check { |
39 | ($f: ident, $($e: expr),+; $expect: expr) => ({ | |
40 | $(assert_eq!($f(&$e), $expect);)+ | |
41 | }); | |
42 | } | |
223e47cc | 43 | |
1a4d82fc | 44 | let abc = quote_expr!(cx, 23); |
d9579d0f | 45 | check!(expr_to_string, abc, *quote_expr!(cx, $abc); "23"); |
223e47cc | 46 | |
c34b1796 | 47 | let ty = quote_ty!(cx, isize); |
d9579d0f | 48 | check!(ty_to_string, ty, *quote_ty!(cx, $ty); "isize"); |
223e47cc | 49 | |
d9579d0f AL |
50 | let item = quote_item!(cx, static x: $ty = 10;).unwrap(); |
51 | check!(item_to_string, item, quote_item!(cx, $item).unwrap(); "static x: isize = 10;"); | |
223e47cc | 52 | |
d9579d0f AL |
53 | let twenty: u16 = 20; |
54 | let stmt = quote_stmt!(cx, let x = $twenty;).unwrap(); | |
55 | check!(stmt_to_string, stmt, *quote_stmt!(cx, $stmt).unwrap(); "let x = 20u16;"); | |
223e47cc | 56 | |
1a4d82fc | 57 | let pat = quote_pat!(cx, Some(_)); |
d9579d0f | 58 | check!(pat_to_string, pat, *quote_pat!(cx, $pat); "Some(_)"); |
223e47cc | 59 | |
d9579d0f AL |
60 | let expr = quote_expr!(cx, (x, y)); |
61 | let arm = quote_arm!(cx, (ref x, ref y) => $expr,); | |
62 | check!(arm_to_string, arm, quote_arm!(cx, $arm); " (ref x, ref y) => (x, y),"); | |
c34b1796 AL |
63 | |
64 | let attr = quote_attr!(cx, #![cfg(foo = "bar")]); | |
d9579d0f | 65 | check!(attribute_to_string, attr, quote_attr!(cx, $attr); r#"#![cfg(foo = "bar")]"#); |
92a42be0 SL |
66 | |
67 | // quote_arg! | |
68 | ||
69 | let arg = quote_arg!(cx, foo: i32); | |
70 | check!(arg_to_string, arg, quote_arg!(cx, $arg); "foo: i32"); | |
71 | ||
72 | let function = quote_item!(cx, fn f($arg) { }).unwrap(); | |
73 | check!(item_to_string, function; "fn f(foo: i32) { }"); | |
74 | ||
75 | let args = vec![arg, quote_arg!(cx, bar: u32)]; | |
76 | let args = &args[..]; | |
77 | let function = quote_item!(cx, fn f($args) { }).unwrap(); | |
78 | check!(item_to_string, function; "fn f(foo: i32, bar: u32) { }"); | |
79 | ||
80 | // quote_block! | |
81 | ||
82 | let block = quote_block!(cx, { $stmt let y = 40u32; }); | |
83 | check!(block_to_string, block, *quote_block!(cx, $block); "{ let x = 20u16; let y = 40u32; }"); | |
84 | ||
85 | let function = quote_item!(cx, fn f() $block).unwrap(); | |
86 | check!(item_to_string, function; "fn f() { let x = 20u16; let y = 40u32; }"); | |
87 | ||
88 | // quote_path! | |
89 | ||
90 | let path = quote_path!(cx, ::syntax::ptr::P<MetaItem>); | |
91 | check!(path_to_string, path, quote_path!(cx, $path); "::syntax::ptr::P<MetaItem>"); | |
92 | ||
93 | let ty = quote_ty!(cx, $path); | |
94 | check!(ty_to_string, ty; "::syntax::ptr::P<MetaItem>"); | |
95 | ||
96 | // quote_meta_item! | |
97 | ||
98 | let meta = quote_meta_item!(cx, cfg(foo = "bar")); | |
99 | check!(meta_item_to_string, meta, *quote_meta_item!(cx, $meta); r#"cfg(foo = "bar")"#); | |
100 | ||
101 | let attr = quote_attr!(cx, #![$meta]); | |
102 | check!(attribute_to_string, attr; r#"#![cfg(foo = "bar")]"#); | |
223e47cc | 103 | } |