2015-02-24 12:56:01 -06:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
2012-12-10 19:32:48 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-04-22 17:20:57 -05:00
|
|
|
// ignore-cross-compile
|
2014-02-07 13:08:32 -06:00
|
|
|
|
2015-04-23 01:08:04 -05:00
|
|
|
#![feature(quote, rustc_private)]
|
2012-02-01 14:41:01 -06:00
|
|
|
|
2014-02-14 12:10:06 -06:00
|
|
|
extern crate syntax;
|
2016-06-23 20:21:35 -05:00
|
|
|
extern crate syntax_pos;
|
2012-02-01 14:41:01 -06:00
|
|
|
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 14:06:49 -06:00
|
|
|
use syntax::print::pprust::*;
|
2015-08-26 19:11:53 -05:00
|
|
|
use syntax::parse::token::intern;
|
2016-06-21 17:08:13 -05:00
|
|
|
use syntax_pos::DUMMY_SP;
|
2015-04-23 01:08:04 -05:00
|
|
|
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 14:06:49 -06:00
|
|
|
fn main() {
|
2015-05-13 15:00:17 -05:00
|
|
|
let ps = syntax::parse::ParseSess::new();
|
2016-06-10 20:37:24 -05:00
|
|
|
let mut loader = syntax::ext::base::DummyMacroLoader;
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 14:06:49 -06:00
|
|
|
let mut cx = syntax::ext::base::ExtCtxt::new(
|
|
|
|
&ps, vec![],
|
2015-07-13 19:10:44 -05:00
|
|
|
syntax::ext::expand::ExpansionConfig::default("qquote".to_string()),
|
2016-06-10 20:37:24 -05:00
|
|
|
&mut loader);
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 14:06:49 -06:00
|
|
|
cx.bt_push(syntax::codemap::ExpnInfo {
|
|
|
|
call_site: DUMMY_SP,
|
|
|
|
callee: syntax::codemap::NameAndSpan {
|
2015-08-26 19:11:53 -05:00
|
|
|
format: syntax::codemap::MacroBang(intern("")),
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 14:06:49 -06:00
|
|
|
allow_internal_unstable: false,
|
|
|
|
span: None,
|
2012-12-12 12:44:01 -06:00
|
|
|
}
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 14:06:49 -06:00
|
|
|
});
|
|
|
|
let cx = &mut cx;
|
|
|
|
|
|
|
|
macro_rules! check {
|
|
|
|
($f: ident, $($e: expr),+; $expect: expr) => ({
|
|
|
|
$(assert_eq!($f(&$e), $expect);)+
|
|
|
|
});
|
2012-12-12 12:44:01 -06:00
|
|
|
}
|
2012-02-01 14:41:01 -06:00
|
|
|
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 14:06:49 -06:00
|
|
|
let abc = quote_expr!(cx, 23);
|
|
|
|
check!(expr_to_string, abc, *quote_expr!(cx, $abc); "23");
|
|
|
|
|
|
|
|
let ty = quote_ty!(cx, isize);
|
|
|
|
check!(ty_to_string, ty, *quote_ty!(cx, $ty); "isize");
|
2012-02-01 14:41:01 -06:00
|
|
|
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 14:06:49 -06:00
|
|
|
let item = quote_item!(cx, static x: $ty = 10;).unwrap();
|
|
|
|
check!(item_to_string, item, quote_item!(cx, $item).unwrap(); "static x: isize = 10;");
|
2012-02-01 14:41:01 -06:00
|
|
|
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 14:06:49 -06:00
|
|
|
let twenty: u16 = 20;
|
|
|
|
let stmt = quote_stmt!(cx, let x = $twenty;).unwrap();
|
2016-02-11 14:33:09 -06:00
|
|
|
check!(stmt_to_string, stmt, quote_stmt!(cx, $stmt).unwrap(); "let x = 20u16;");
|
2012-02-01 14:41:01 -06:00
|
|
|
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 14:06:49 -06:00
|
|
|
let pat = quote_pat!(cx, Some(_));
|
|
|
|
check!(pat_to_string, pat, *quote_pat!(cx, $pat); "Some(_)");
|
2012-02-01 23:06:36 -06:00
|
|
|
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 14:06:49 -06:00
|
|
|
let expr = quote_expr!(cx, (x, y));
|
|
|
|
let arm = quote_arm!(cx, (ref x, ref y) => $expr,);
|
|
|
|
check!(arm_to_string, arm, quote_arm!(cx, $arm); " (ref x, ref y) => (x, y),");
|
2012-02-01 14:41:01 -06:00
|
|
|
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 14:06:49 -06:00
|
|
|
let attr = quote_attr!(cx, #![cfg(foo = "bar")]);
|
|
|
|
check!(attribute_to_string, attr, quote_attr!(cx, $attr); r#"#![cfg(foo = "bar")]"#);
|
2015-11-11 14:19:01 -06:00
|
|
|
|
|
|
|
// quote_arg!
|
|
|
|
|
|
|
|
let arg = quote_arg!(cx, foo: i32);
|
|
|
|
check!(arg_to_string, arg, quote_arg!(cx, $arg); "foo: i32");
|
|
|
|
|
|
|
|
let function = quote_item!(cx, fn f($arg) { }).unwrap();
|
|
|
|
check!(item_to_string, function; "fn f(foo: i32) { }");
|
|
|
|
|
|
|
|
let args = vec![arg, quote_arg!(cx, bar: u32)];
|
|
|
|
let args = &args[..];
|
|
|
|
let function = quote_item!(cx, fn f($args) { }).unwrap();
|
|
|
|
check!(item_to_string, function; "fn f(foo: i32, bar: u32) { }");
|
|
|
|
|
|
|
|
// quote_block!
|
|
|
|
|
|
|
|
let block = quote_block!(cx, { $stmt let y = 40u32; });
|
|
|
|
check!(block_to_string, block, *quote_block!(cx, $block); "{ let x = 20u16; let y = 40u32; }");
|
|
|
|
|
|
|
|
let function = quote_item!(cx, fn f() $block).unwrap();
|
|
|
|
check!(item_to_string, function; "fn f() { let x = 20u16; let y = 40u32; }");
|
|
|
|
|
|
|
|
// quote_path!
|
|
|
|
|
|
|
|
let path = quote_path!(cx, ::syntax::ptr::P<MetaItem>);
|
|
|
|
check!(path_to_string, path, quote_path!(cx, $path); "::syntax::ptr::P<MetaItem>");
|
|
|
|
|
|
|
|
let ty = quote_ty!(cx, $path);
|
|
|
|
check!(ty_to_string, ty; "::syntax::ptr::P<MetaItem>");
|
|
|
|
|
|
|
|
// quote_meta_item!
|
|
|
|
|
|
|
|
let meta = quote_meta_item!(cx, cfg(foo = "bar"));
|
|
|
|
check!(meta_item_to_string, meta, *quote_meta_item!(cx, $meta); r#"cfg(foo = "bar")"#);
|
|
|
|
|
|
|
|
let attr = quote_attr!(cx, #![$meta]);
|
|
|
|
check!(attribute_to_string, attr; r#"#![cfg(foo = "bar")]"#);
|
2012-02-01 14:41:01 -06:00
|
|
|
}
|