2012-12-03 16:48:01 -08:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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.
|
|
|
|
|
2012-12-23 17:41:37 -05:00
|
|
|
use ast;
|
2013-12-08 02:55:28 -05:00
|
|
|
use codemap::Span;
|
2013-05-17 21:27:17 +10:00
|
|
|
use ext::base::ExtCtxt;
|
2012-12-23 17:41:37 -05:00
|
|
|
use ext::base;
|
2013-05-18 00:19:28 +10:00
|
|
|
use ext::build::AstBuilder;
|
2012-12-13 13:05:22 -08:00
|
|
|
use parse::token::*;
|
2012-12-23 17:41:37 -05:00
|
|
|
use parse::token;
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
use std::gc::Gc;
|
2014-05-05 18:56:44 -07:00
|
|
|
|
2012-11-08 17:00:55 -08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Quasiquoting works via token trees.
|
|
|
|
*
|
2012-12-06 16:18:41 -08:00
|
|
|
* This is registered as a set of expression syntax extension called quote!
|
|
|
|
* that lifts its argument token-tree to an AST representing the
|
2014-01-09 15:05:33 +02:00
|
|
|
* construction of the same token tree, with ast::TTNonterminal nodes
|
2012-12-06 16:18:41 -08:00
|
|
|
* interpreted as antiquotes (splices).
|
2012-11-08 17:00:55 -08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-11-16 14:50:35 -08:00
|
|
|
pub mod rt {
|
2012-12-23 17:41:37 -05:00
|
|
|
use ast;
|
2014-06-20 18:36:21 -07:00
|
|
|
use codemap::Spanned;
|
2013-05-17 21:27:17 +10:00
|
|
|
use ext::base::ExtCtxt;
|
2014-01-10 14:02:36 -08:00
|
|
|
use parse::token;
|
2012-12-23 17:41:37 -05:00
|
|
|
use parse;
|
|
|
|
use print::pprust;
|
|
|
|
|
2014-05-29 12:19:05 +09:00
|
|
|
use ast::{TokenTree, Generics, Expr};
|
|
|
|
|
2012-11-20 16:07:57 -08:00
|
|
|
pub use parse::new_parser_from_tts;
|
2013-08-31 18:13:04 +02:00
|
|
|
pub use codemap::{BytePos, Span, dummy_spanned};
|
2012-12-06 16:18:41 -08:00
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
use std::gc::Gc;
|
|
|
|
|
2013-02-25 14:11:21 -05:00
|
|
|
pub trait ToTokens {
|
2014-02-28 13:09:09 -08:00
|
|
|
fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> ;
|
2012-12-06 16:18:41 -08:00
|
|
|
}
|
|
|
|
|
2014-06-20 18:36:21 -07:00
|
|
|
impl ToTokens for TokenTree {
|
|
|
|
fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
|
|
|
|
vec!(self.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 22:24:42 -04:00
|
|
|
impl<T: ToTokens> ToTokens for Vec<T> {
|
|
|
|
fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
|
|
|
|
let a = self.iter().flat_map(|t| t.to_tokens(cx).move_iter());
|
|
|
|
FromIterator::from_iter(a)
|
2012-12-06 16:18:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 18:36:21 -07:00
|
|
|
impl<T: ToTokens> ToTokens for Spanned<T> {
|
|
|
|
fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
|
|
|
|
// FIXME: use the span?
|
|
|
|
self.node.to_tokens(cx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 22:16:55 -04:00
|
|
|
impl<T: ToTokens> ToTokens for Option<T> {
|
|
|
|
fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
|
|
|
|
match self {
|
|
|
|
&Some(ref t) => t.to_tokens(cx),
|
|
|
|
&None => Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-06 16:18:41 -08:00
|
|
|
/* Should be (when bugs in default methods are fixed):
|
|
|
|
|
|
|
|
trait ToSource : ToTokens {
|
|
|
|
// Takes a thing and generates a string containing rust code for it.
|
2014-05-22 16:57:53 -07:00
|
|
|
pub fn to_source() -> String;
|
2012-12-06 16:18:41 -08:00
|
|
|
|
|
|
|
// If you can make source, you can definitely make tokens.
|
2014-01-09 15:05:33 +02:00
|
|
|
pub fn to_tokens(cx: &ExtCtxt) -> ~[TokenTree] {
|
2013-05-17 11:11:24 -07:00
|
|
|
cx.parse_tts(self.to_source())
|
2012-12-06 16:18:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-06-20 18:14:47 -07:00
|
|
|
// FIXME: Move this trait to pprust and get rid of *_to_str?
|
2013-02-25 14:11:21 -05:00
|
|
|
pub trait ToSource {
|
2012-12-06 16:18:41 -08:00
|
|
|
// Takes a thing and generates a string containing rust code for it.
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String;
|
2012-12-06 16:18:41 -08:00
|
|
|
}
|
|
|
|
|
2014-06-20 18:14:47 -07:00
|
|
|
macro_rules! impl_to_source(
|
|
|
|
(Gc<$t:ty>, $pp:ident) => (
|
|
|
|
impl ToSource for Gc<$t> {
|
|
|
|
fn to_source(&self) -> String {
|
|
|
|
pprust::$pp(&**self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
($t:ty, $pp:ident) => (
|
|
|
|
impl ToSource for $t {
|
|
|
|
fn to_source(&self) -> String {
|
|
|
|
pprust::$pp(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
)
|
2012-12-06 16:18:41 -08:00
|
|
|
|
2014-06-20 18:14:47 -07:00
|
|
|
fn slice_to_source<'a, T: ToSource>(sep: &'static str, xs: &'a [T]) -> String {
|
|
|
|
xs.iter()
|
|
|
|
.map(|i| i.to_source())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.connect(sep)
|
|
|
|
.to_string()
|
2012-12-06 16:18:41 -08:00
|
|
|
}
|
|
|
|
|
2014-06-20 18:14:47 -07:00
|
|
|
macro_rules! impl_to_source_slice(
|
|
|
|
($t:ty, $sep:expr) => (
|
|
|
|
impl<'a> ToSource for &'a [$t] {
|
|
|
|
fn to_source(&self) -> String {
|
|
|
|
slice_to_source($sep, *self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2012-12-06 16:18:41 -08:00
|
|
|
|
2014-06-20 18:14:47 -07:00
|
|
|
impl ToSource for ast::Ident {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-06-20 18:14:47 -07:00
|
|
|
token::get_ident(*self).get().to_string()
|
2013-03-27 06:49:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 03:39:03 -07:00
|
|
|
impl_to_source!(ast::Ty, ty_to_string)
|
|
|
|
impl_to_source!(ast::Block, block_to_string)
|
|
|
|
impl_to_source!(ast::Arg, arg_to_string)
|
|
|
|
impl_to_source!(Generics, generics_to_string)
|
|
|
|
impl_to_source!(Gc<ast::Item>, item_to_string)
|
2014-07-17 08:54:43 -04:00
|
|
|
impl_to_source!(Gc<ast::Method>, method_to_string)
|
2014-06-21 03:39:03 -07:00
|
|
|
impl_to_source!(Gc<ast::Expr>, expr_to_string)
|
|
|
|
impl_to_source!(Gc<ast::Pat>, pat_to_string)
|
2014-06-20 18:14:47 -07:00
|
|
|
impl_to_source_slice!(ast::Ty, ", ")
|
|
|
|
impl_to_source_slice!(Gc<ast::Item>, "\n\n")
|
2014-06-07 13:46:41 +00:00
|
|
|
|
2014-07-16 22:17:16 -04:00
|
|
|
impl ToSource for ast::Attribute_ {
|
|
|
|
fn to_source(&self) -> String {
|
|
|
|
pprust::attribute_to_string(&dummy_spanned(*self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-09 23:16:18 -08:00
|
|
|
impl<'a> ToSource for &'a str {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-01-10 14:02:36 -08:00
|
|
|
let lit = dummy_spanned(ast::LitStr(
|
|
|
|
token::intern_and_get_ident(*self), ast::CookedStr));
|
2014-06-21 03:39:03 -07:00
|
|
|
pprust::lit_to_string(&lit)
|
2013-03-27 06:49:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-01 23:39:00 +10:00
|
|
|
impl ToSource for () {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-05-25 03:17:19 -07:00
|
|
|
"()".to_string()
|
2014-05-01 23:39:00 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToSource for bool {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-05-01 23:39:00 +10:00
|
|
|
let lit = dummy_spanned(ast::LitBool(*self));
|
2014-06-21 03:39:03 -07:00
|
|
|
pprust::lit_to_string(&lit)
|
2014-05-01 23:39:00 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToSource for char {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-05-01 23:39:00 +10:00
|
|
|
let lit = dummy_spanned(ast::LitChar(*self));
|
2014-06-21 03:39:03 -07:00
|
|
|
pprust::lit_to_string(&lit)
|
2014-05-01 23:39:00 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 18:14:47 -07:00
|
|
|
macro_rules! impl_to_source_int(
|
|
|
|
(signed, $t:ty, $tag:ident) => (
|
|
|
|
impl ToSource for $t {
|
|
|
|
fn to_source(&self) -> String {
|
|
|
|
let lit = dummy_spanned(ast::LitInt(*self as i64, ast::$tag));
|
2014-06-21 03:39:03 -07:00
|
|
|
pprust::lit_to_string(&lit)
|
2014-06-20 18:14:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(unsigned, $t:ty, $tag:ident) => (
|
|
|
|
impl ToSource for $t {
|
|
|
|
fn to_source(&self) -> String {
|
|
|
|
let lit = dummy_spanned(ast::LitUint(*self as u64, ast::$tag));
|
2014-06-21 03:39:03 -07:00
|
|
|
pprust::lit_to_string(&lit)
|
2014-06-20 18:14:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
)
|
2013-03-27 06:49:39 -07:00
|
|
|
|
2014-06-20 18:14:47 -07:00
|
|
|
impl_to_source_int!(signed, int, TyI)
|
|
|
|
impl_to_source_int!(signed, i8, TyI8)
|
|
|
|
impl_to_source_int!(signed, i16, TyI16)
|
|
|
|
impl_to_source_int!(signed, i32, TyI32)
|
|
|
|
impl_to_source_int!(signed, i64, TyI64)
|
2013-03-27 06:49:39 -07:00
|
|
|
|
2014-06-20 18:14:47 -07:00
|
|
|
impl_to_source_int!(unsigned, uint, TyU)
|
|
|
|
impl_to_source_int!(unsigned, u8, TyU8)
|
|
|
|
impl_to_source_int!(unsigned, u16, TyU16)
|
|
|
|
impl_to_source_int!(unsigned, u32, TyU32)
|
|
|
|
impl_to_source_int!(unsigned, u64, TyU64)
|
2013-03-27 06:49:39 -07:00
|
|
|
|
2012-12-06 16:18:41 -08:00
|
|
|
// Alas ... we write these out instead. All redundant.
|
|
|
|
|
2013-08-15 02:06:33 -04:00
|
|
|
macro_rules! impl_to_tokens(
|
|
|
|
($t:ty) => (
|
|
|
|
impl ToTokens for $t {
|
2014-02-28 13:09:09 -08:00
|
|
|
fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
|
2014-01-15 16:26:20 -08:00
|
|
|
cx.parse_tts(self.to_source())
|
2013-08-15 02:06:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-06-20 18:14:47 -07:00
|
|
|
macro_rules! impl_to_tokens_lifetime(
|
2013-08-15 02:06:33 -04:00
|
|
|
($t:ty) => (
|
2013-12-09 23:16:18 -08:00
|
|
|
impl<'a> ToTokens for $t {
|
2014-02-28 13:09:09 -08:00
|
|
|
fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
|
2014-01-15 16:26:20 -08:00
|
|
|
cx.parse_tts(self.to_source())
|
2013-08-15 02:06:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-09-02 02:50:59 +02:00
|
|
|
impl_to_tokens!(ast::Ident)
|
2014-05-16 00:16:13 -07:00
|
|
|
impl_to_tokens!(Gc<ast::Item>)
|
2014-06-20 18:36:21 -07:00
|
|
|
impl_to_tokens!(Gc<ast::Pat>)
|
2014-07-17 08:54:43 -04:00
|
|
|
impl_to_tokens!(Gc<ast::Method>)
|
2014-06-20 18:14:47 -07:00
|
|
|
impl_to_tokens_lifetime!(&'a [Gc<ast::Item>])
|
2013-08-15 02:06:33 -04:00
|
|
|
impl_to_tokens!(ast::Ty)
|
2014-06-20 18:14:47 -07:00
|
|
|
impl_to_tokens_lifetime!(&'a [ast::Ty])
|
2013-08-15 02:06:33 -04:00
|
|
|
impl_to_tokens!(Generics)
|
2014-05-16 00:16:13 -07:00
|
|
|
impl_to_tokens!(Gc<ast::Expr>)
|
2013-08-15 02:06:33 -04:00
|
|
|
impl_to_tokens!(ast::Block)
|
2014-06-07 13:46:41 +00:00
|
|
|
impl_to_tokens!(ast::Arg)
|
2014-07-16 22:17:16 -04:00
|
|
|
impl_to_tokens!(ast::Attribute_)
|
2014-06-20 18:14:47 -07:00
|
|
|
impl_to_tokens_lifetime!(&'a str)
|
2014-05-01 23:39:00 +10:00
|
|
|
impl_to_tokens!(())
|
|
|
|
impl_to_tokens!(char)
|
|
|
|
impl_to_tokens!(bool)
|
2013-08-15 02:06:33 -04:00
|
|
|
impl_to_tokens!(int)
|
|
|
|
impl_to_tokens!(i8)
|
|
|
|
impl_to_tokens!(i16)
|
|
|
|
impl_to_tokens!(i32)
|
|
|
|
impl_to_tokens!(i64)
|
|
|
|
impl_to_tokens!(uint)
|
|
|
|
impl_to_tokens!(u8)
|
|
|
|
impl_to_tokens!(u16)
|
|
|
|
impl_to_tokens!(u32)
|
|
|
|
impl_to_tokens!(u64)
|
2013-03-27 06:49:39 -07:00
|
|
|
|
2013-02-25 14:11:21 -05:00
|
|
|
pub trait ExtParseUtils {
|
2014-05-16 00:16:13 -07:00
|
|
|
fn parse_item(&self, s: String) -> Gc<ast::Item>;
|
|
|
|
fn parse_expr(&self, s: String) -> Gc<ast::Expr>;
|
|
|
|
fn parse_stmt(&self, s: String) -> Gc<ast::Stmt>;
|
2014-05-22 16:57:53 -07:00
|
|
|
fn parse_tts(&self, s: String) -> Vec<ast::TokenTree> ;
|
2012-12-06 16:18:41 -08:00
|
|
|
}
|
|
|
|
|
2013-12-25 11:10:33 -07:00
|
|
|
impl<'a> ExtParseUtils for ExtCtxt<'a> {
|
2012-12-06 16:18:41 -08:00
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
fn parse_item(&self, s: String) -> Gc<ast::Item> {
|
2012-12-06 16:18:41 -08:00
|
|
|
let res = parse::parse_item_from_source_str(
|
2014-05-25 03:17:19 -07:00
|
|
|
"<quote expansion>".to_string(),
|
2013-06-13 03:02:55 +10:00
|
|
|
s,
|
2012-12-06 16:18:41 -08:00
|
|
|
self.cfg(),
|
|
|
|
self.parse_sess());
|
|
|
|
match res {
|
|
|
|
Some(ast) => ast,
|
|
|
|
None => {
|
2014-02-06 10:38:08 +01:00
|
|
|
error!("parse error");
|
2013-10-21 13:08:31 -07:00
|
|
|
fail!()
|
2012-12-06 16:18:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
fn parse_stmt(&self, s: String) -> Gc<ast::Stmt> {
|
2014-05-25 03:17:19 -07:00
|
|
|
parse::parse_stmt_from_source_str("<quote expansion>".to_string(),
|
2014-01-15 16:42:51 -08:00
|
|
|
s,
|
|
|
|
self.cfg(),
|
2014-02-28 13:09:09 -08:00
|
|
|
Vec::new(),
|
2014-01-15 16:42:51 -08:00
|
|
|
self.parse_sess())
|
2012-12-06 16:18:41 -08:00
|
|
|
}
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
fn parse_expr(&self, s: String) -> Gc<ast::Expr> {
|
2014-05-25 03:17:19 -07:00
|
|
|
parse::parse_expr_from_source_str("<quote expansion>".to_string(),
|
2014-01-15 16:42:51 -08:00
|
|
|
s,
|
|
|
|
self.cfg(),
|
|
|
|
self.parse_sess())
|
2012-12-06 16:18:41 -08:00
|
|
|
}
|
|
|
|
|
2014-05-22 16:57:53 -07:00
|
|
|
fn parse_tts(&self, s: String) -> Vec<ast::TokenTree> {
|
2014-05-25 03:17:19 -07:00
|
|
|
parse::parse_tts_from_source_str("<quote expansion>".to_string(),
|
2014-01-15 16:42:51 -08:00
|
|
|
s,
|
|
|
|
self.cfg(),
|
|
|
|
self.parse_sess())
|
2012-12-06 16:18:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-16 14:50:35 -08:00
|
|
|
}
|
2012-11-08 17:00:55 -08:00
|
|
|
|
2013-12-28 22:06:22 -07:00
|
|
|
pub fn expand_quote_tokens(cx: &mut ExtCtxt,
|
2013-08-31 18:13:04 +02:00
|
|
|
sp: Span,
|
2014-05-05 18:56:44 -07:00
|
|
|
tts: &[ast::TokenTree])
|
|
|
|
-> Box<base::MacResult> {
|
2013-08-15 02:06:33 -04:00
|
|
|
let (cx_expr, expr) = expand_tts(cx, sp, tts);
|
2013-07-08 15:55:14 -07:00
|
|
|
let expanded = expand_wrapper(cx, sp, cx_expr, expr);
|
2014-04-15 22:00:14 +10:00
|
|
|
base::MacExpr::new(expanded)
|
2012-11-16 14:50:35 -08:00
|
|
|
}
|
2012-11-08 17:00:55 -08:00
|
|
|
|
2013-12-28 22:06:22 -07:00
|
|
|
pub fn expand_quote_expr(cx: &mut ExtCtxt,
|
2013-08-31 18:13:04 +02:00
|
|
|
sp: Span,
|
2014-05-05 18:56:44 -07:00
|
|
|
tts: &[ast::TokenTree]) -> Box<base::MacResult> {
|
2014-02-28 13:09:09 -08:00
|
|
|
let expanded = expand_parse_call(cx, sp, "parse_expr", Vec::new(), tts);
|
2014-04-15 22:00:14 +10:00
|
|
|
base::MacExpr::new(expanded)
|
2012-11-16 14:50:35 -08:00
|
|
|
}
|
|
|
|
|
2013-12-28 22:06:22 -07:00
|
|
|
pub fn expand_quote_item(cx: &mut ExtCtxt,
|
2013-08-31 18:13:04 +02:00
|
|
|
sp: Span,
|
2014-05-05 18:56:44 -07:00
|
|
|
tts: &[ast::TokenTree])
|
|
|
|
-> Box<base::MacResult> {
|
2014-06-13 09:40:10 +10:00
|
|
|
let expanded = expand_parse_call(cx, sp, "parse_item_with_outer_attributes",
|
|
|
|
vec!(), tts);
|
2014-04-15 22:00:14 +10:00
|
|
|
base::MacExpr::new(expanded)
|
2012-11-16 14:50:35 -08:00
|
|
|
}
|
|
|
|
|
2013-12-28 22:06:22 -07:00
|
|
|
pub fn expand_quote_pat(cx: &mut ExtCtxt,
|
2013-08-31 18:13:04 +02:00
|
|
|
sp: Span,
|
2014-05-05 18:56:44 -07:00
|
|
|
tts: &[ast::TokenTree])
|
|
|
|
-> Box<base::MacResult> {
|
2014-06-13 09:36:26 +10:00
|
|
|
let expanded = expand_parse_call(cx, sp, "parse_pat", vec!(), tts);
|
2014-04-15 22:00:14 +10:00
|
|
|
base::MacExpr::new(expanded)
|
2012-11-16 14:50:35 -08:00
|
|
|
}
|
|
|
|
|
2013-12-28 22:06:22 -07:00
|
|
|
pub fn expand_quote_ty(cx: &mut ExtCtxt,
|
2013-08-31 18:13:04 +02:00
|
|
|
sp: Span,
|
2014-05-05 18:56:44 -07:00
|
|
|
tts: &[ast::TokenTree])
|
|
|
|
-> Box<base::MacResult> {
|
2014-01-09 15:05:33 +02:00
|
|
|
let e_param_colons = cx.expr_lit(sp, ast::LitBool(false));
|
2013-07-08 15:55:14 -07:00
|
|
|
let expanded = expand_parse_call(cx, sp, "parse_ty",
|
2014-02-28 13:09:09 -08:00
|
|
|
vec!(e_param_colons), tts);
|
2014-04-15 22:00:14 +10:00
|
|
|
base::MacExpr::new(expanded)
|
2012-11-16 14:50:35 -08:00
|
|
|
}
|
|
|
|
|
2014-07-17 01:02:27 -04:00
|
|
|
pub fn expand_quote_method(cx: &mut ExtCtxt,
|
|
|
|
sp: Span,
|
|
|
|
tts: &[ast::TokenTree])
|
|
|
|
-> Box<base::MacResult> {
|
|
|
|
let e_param_colons = cx.expr_none(sp);
|
|
|
|
let expanded = expand_parse_call(cx, sp, "parse_method",
|
|
|
|
vec!(e_param_colons), tts);
|
|
|
|
base::MacExpr::new(expanded)
|
|
|
|
}
|
|
|
|
|
2013-12-28 22:06:22 -07:00
|
|
|
pub fn expand_quote_stmt(cx: &mut ExtCtxt,
|
2013-08-31 18:13:04 +02:00
|
|
|
sp: Span,
|
2014-05-05 18:56:44 -07:00
|
|
|
tts: &[ast::TokenTree])
|
|
|
|
-> Box<base::MacResult> {
|
2014-02-28 12:54:01 -08:00
|
|
|
let e_attrs = cx.expr_vec_ng(sp);
|
2013-07-08 15:55:14 -07:00
|
|
|
let expanded = expand_parse_call(cx, sp, "parse_stmt",
|
2014-02-28 13:09:09 -08:00
|
|
|
vec!(e_attrs), tts);
|
2014-04-15 22:00:14 +10:00
|
|
|
base::MacExpr::new(expanded)
|
2012-11-08 17:00:55 -08:00
|
|
|
}
|
|
|
|
|
2014-05-22 16:57:53 -07:00
|
|
|
fn ids_ext(strs: Vec<String> ) -> Vec<ast::Ident> {
|
2014-05-07 16:33:43 -07:00
|
|
|
strs.iter().map(|str| str_to_ident((*str).as_slice())).collect()
|
2012-11-08 17:00:55 -08:00
|
|
|
}
|
|
|
|
|
2013-09-02 02:50:59 +02:00
|
|
|
fn id_ext(str: &str) -> ast::Ident {
|
2013-06-04 12:34:25 -07:00
|
|
|
str_to_ident(str)
|
2012-11-08 17:00:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lift an ident to the expr that evaluates to that ident.
|
2014-05-16 00:16:13 -07:00
|
|
|
fn mk_ident(cx: &ExtCtxt, sp: Span, ident: ast::Ident) -> Gc<ast::Expr> {
|
2014-02-14 07:07:09 +02:00
|
|
|
let e_str = cx.expr_str(sp, token::get_ident(ident));
|
2013-05-19 15:53:42 +10:00
|
|
|
cx.expr_method_call(sp,
|
2013-05-17 11:11:24 -07:00
|
|
|
cx.expr_ident(sp, id_ext("ext_cx")),
|
|
|
|
id_ext("ident_of"),
|
2014-02-28 13:09:09 -08:00
|
|
|
vec!(e_str))
|
2012-11-08 17:00:55 -08:00
|
|
|
}
|
|
|
|
|
2014-07-06 01:17:59 -07:00
|
|
|
// Lift a name to the expr that evaluates to that name
|
|
|
|
fn mk_name(cx: &ExtCtxt, sp: Span, ident: ast::Ident) -> Gc<ast::Expr> {
|
|
|
|
let e_str = cx.expr_str(sp, token::get_ident(ident));
|
|
|
|
cx.expr_method_call(sp,
|
|
|
|
cx.expr_ident(sp, id_ext("ext_cx")),
|
|
|
|
id_ext("name_of"),
|
|
|
|
vec!(e_str))
|
|
|
|
}
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
fn mk_ast_path(cx: &ExtCtxt, sp: Span, name: &str) -> Gc<ast::Expr> {
|
2014-06-04 01:42:11 +09:00
|
|
|
let idents = vec!(id_ext("syntax"), id_ext("ast"), id_ext(name));
|
|
|
|
cx.expr_path(cx.path_global(sp, idents))
|
|
|
|
}
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
fn mk_token_path(cx: &ExtCtxt, sp: Span, name: &str) -> Gc<ast::Expr> {
|
2014-06-04 01:42:11 +09:00
|
|
|
let idents = vec!(id_ext("syntax"), id_ext("parse"), id_ext("token"), id_ext(name));
|
|
|
|
cx.expr_path(cx.path_global(sp, idents))
|
|
|
|
}
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
fn mk_binop(cx: &ExtCtxt, sp: Span, bop: token::BinOp) -> Gc<ast::Expr> {
|
2012-11-08 17:00:55 -08:00
|
|
|
let name = match bop {
|
|
|
|
PLUS => "PLUS",
|
|
|
|
MINUS => "MINUS",
|
|
|
|
STAR => "STAR",
|
|
|
|
SLASH => "SLASH",
|
|
|
|
PERCENT => "PERCENT",
|
|
|
|
CARET => "CARET",
|
|
|
|
AND => "AND",
|
|
|
|
OR => "OR",
|
|
|
|
SHL => "SHL",
|
|
|
|
SHR => "SHR"
|
|
|
|
};
|
2014-06-04 01:42:11 +09:00
|
|
|
mk_token_path(cx, sp, name)
|
2012-11-08 17:00:55 -08:00
|
|
|
}
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
fn mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> Gc<ast::Expr> {
|
2012-11-08 17:00:55 -08:00
|
|
|
|
2013-05-12 00:25:31 -04:00
|
|
|
match *tok {
|
2012-11-08 17:00:55 -08:00
|
|
|
BINOP(binop) => {
|
2014-06-04 01:42:11 +09:00
|
|
|
return cx.expr_call(sp, mk_token_path(cx, sp, "BINOP"), vec!(mk_binop(cx, sp, binop)));
|
2012-11-08 17:00:55 -08:00
|
|
|
}
|
|
|
|
BINOPEQ(binop) => {
|
2014-06-04 01:42:11 +09:00
|
|
|
return cx.expr_call(sp, mk_token_path(cx, sp, "BINOPEQ"),
|
|
|
|
vec!(mk_binop(cx, sp, binop)));
|
2012-11-08 17:00:55 -08:00
|
|
|
}
|
|
|
|
|
2014-06-06 16:04:04 +01:00
|
|
|
LIT_BYTE(i) => {
|
2014-07-06 01:17:59 -07:00
|
|
|
let e_byte = mk_name(cx, sp, i.ident());
|
2014-06-06 16:04:04 +01:00
|
|
|
|
|
|
|
return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_BYTE"), vec!(e_byte));
|
|
|
|
}
|
|
|
|
|
2013-09-03 19:24:12 -04:00
|
|
|
LIT_CHAR(i) => {
|
2014-07-06 01:17:59 -07:00
|
|
|
let e_char = mk_name(cx, sp, i.ident());
|
2013-09-03 19:24:12 -04:00
|
|
|
|
2014-06-04 01:42:11 +09:00
|
|
|
return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_CHAR"), vec!(e_char));
|
2013-09-03 19:24:12 -04:00
|
|
|
}
|
|
|
|
|
2014-06-18 10:44:20 -07:00
|
|
|
LIT_INTEGER(i) => {
|
2014-07-06 01:17:59 -07:00
|
|
|
let e_int = mk_name(cx, sp, i.ident());
|
2014-06-18 10:44:20 -07:00
|
|
|
return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_INTEGER"), vec!(e_int));
|
2012-11-08 17:00:55 -08:00
|
|
|
}
|
|
|
|
|
2014-06-18 10:44:20 -07:00
|
|
|
LIT_FLOAT(fident) => {
|
2014-07-06 01:17:59 -07:00
|
|
|
let e_fident = mk_name(cx, sp, fident.ident());
|
2014-06-18 10:44:20 -07:00
|
|
|
return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_FLOAT"), vec!(e_fident));
|
2012-11-08 17:00:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
LIT_STR(ident) => {
|
2014-06-04 01:42:11 +09:00
|
|
|
return cx.expr_call(sp,
|
|
|
|
mk_token_path(cx, sp, "LIT_STR"),
|
2014-07-06 01:17:59 -07:00
|
|
|
vec!(mk_name(cx, sp, ident.ident())));
|
2012-11-08 17:00:55 -08:00
|
|
|
}
|
|
|
|
|
2013-10-02 03:32:29 +02:00
|
|
|
LIT_STR_RAW(ident, n) => {
|
2014-06-04 01:42:11 +09:00
|
|
|
return cx.expr_call(sp,
|
|
|
|
mk_token_path(cx, sp, "LIT_STR_RAW"),
|
2014-07-06 01:17:59 -07:00
|
|
|
vec!(mk_name(cx, sp, ident.ident()), cx.expr_uint(sp, n)));
|
2013-10-02 03:32:29 +02:00
|
|
|
}
|
|
|
|
|
2012-11-08 17:00:55 -08:00
|
|
|
IDENT(ident, b) => {
|
2014-06-04 01:42:11 +09:00
|
|
|
return cx.expr_call(sp,
|
|
|
|
mk_token_path(cx, sp, "IDENT"),
|
|
|
|
vec!(mk_ident(cx, sp, ident), cx.expr_bool(sp, b)));
|
2012-11-08 17:00:55 -08:00
|
|
|
}
|
|
|
|
|
2013-03-14 11:22:51 -07:00
|
|
|
LIFETIME(ident) => {
|
2014-06-04 01:42:11 +09:00
|
|
|
return cx.expr_call(sp,
|
|
|
|
mk_token_path(cx, sp, "LIFETIME"),
|
|
|
|
vec!(mk_ident(cx, sp, ident)));
|
2013-03-14 11:22:51 -07:00
|
|
|
}
|
|
|
|
|
2012-11-08 17:00:55 -08:00
|
|
|
DOC_COMMENT(ident) => {
|
2014-06-04 01:42:11 +09:00
|
|
|
return cx.expr_call(sp,
|
|
|
|
mk_token_path(cx, sp, "DOC_COMMENT"),
|
2014-07-06 01:17:59 -07:00
|
|
|
vec!(mk_name(cx, sp, ident.ident())));
|
2012-11-08 17:00:55 -08:00
|
|
|
}
|
|
|
|
|
2013-10-21 13:08:31 -07:00
|
|
|
INTERPOLATED(_) => fail!("quote! with interpolated token"),
|
2012-11-08 17:00:55 -08:00
|
|
|
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
|
2013-05-12 00:25:31 -04:00
|
|
|
let name = match *tok {
|
2012-11-08 17:00:55 -08:00
|
|
|
EQ => "EQ",
|
|
|
|
LT => "LT",
|
|
|
|
LE => "LE",
|
|
|
|
EQEQ => "EQEQ",
|
|
|
|
NE => "NE",
|
|
|
|
GE => "GE",
|
|
|
|
GT => "GT",
|
|
|
|
ANDAND => "ANDAND",
|
|
|
|
OROR => "OROR",
|
|
|
|
NOT => "NOT",
|
|
|
|
TILDE => "TILDE",
|
|
|
|
AT => "AT",
|
|
|
|
DOT => "DOT",
|
|
|
|
DOTDOT => "DOTDOT",
|
|
|
|
COMMA => "COMMA",
|
|
|
|
SEMI => "SEMI",
|
|
|
|
COLON => "COLON",
|
|
|
|
MOD_SEP => "MOD_SEP",
|
|
|
|
RARROW => "RARROW",
|
|
|
|
LARROW => "LARROW",
|
|
|
|
FAT_ARROW => "FAT_ARROW",
|
|
|
|
LPAREN => "LPAREN",
|
|
|
|
RPAREN => "RPAREN",
|
|
|
|
LBRACKET => "LBRACKET",
|
|
|
|
RBRACKET => "RBRACKET",
|
|
|
|
LBRACE => "LBRACE",
|
|
|
|
RBRACE => "RBRACE",
|
|
|
|
POUND => "POUND",
|
|
|
|
DOLLAR => "DOLLAR",
|
|
|
|
UNDERSCORE => "UNDERSCORE",
|
|
|
|
EOF => "EOF",
|
2013-10-21 13:08:31 -07:00
|
|
|
_ => fail!()
|
2012-11-08 17:00:55 -08:00
|
|
|
};
|
2014-06-04 01:42:11 +09:00
|
|
|
mk_token_path(cx, sp, name)
|
2012-11-08 17:00:55 -08:00
|
|
|
}
|
|
|
|
|
2014-05-16 00:16:13 -07:00
|
|
|
|
|
|
|
fn mk_tt(cx: &ExtCtxt, sp: Span, tt: &ast::TokenTree) -> Vec<Gc<ast::Stmt>> {
|
2012-11-08 17:00:55 -08:00
|
|
|
match *tt {
|
2014-01-09 15:05:33 +02:00
|
|
|
ast::TTTok(sp, ref tok) => {
|
2014-01-18 23:00:50 -08:00
|
|
|
let e_sp = cx.expr_ident(sp, id_ext("_sp"));
|
2014-06-04 01:42:11 +09:00
|
|
|
let e_tok = cx.expr_call(sp,
|
|
|
|
mk_ast_path(cx, sp, "TTTok"),
|
|
|
|
vec!(e_sp, mk_token(cx, sp, tok)));
|
2012-12-06 11:01:58 -08:00
|
|
|
let e_push =
|
2013-05-19 15:53:42 +10:00
|
|
|
cx.expr_method_call(sp,
|
2013-05-17 11:11:24 -07:00
|
|
|
cx.expr_ident(sp, id_ext("tt")),
|
|
|
|
id_ext("push"),
|
2014-02-28 13:09:09 -08:00
|
|
|
vec!(e_tok));
|
|
|
|
vec!(cx.stmt_expr(e_push))
|
2012-11-08 17:00:55 -08:00
|
|
|
}
|
|
|
|
|
2014-02-28 12:54:01 -08:00
|
|
|
ast::TTDelim(ref tts) => mk_tts(cx, sp, tts.as_slice()),
|
2014-01-09 15:05:33 +02:00
|
|
|
ast::TTSeq(..) => fail!("TTSeq in quote!"),
|
2012-11-08 17:00:55 -08:00
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
ast::TTNonterminal(sp, ident) => {
|
2012-12-06 16:18:41 -08:00
|
|
|
|
|
|
|
// tt.push_all_move($ident.to_tokens(ext_cx))
|
|
|
|
|
|
|
|
let e_to_toks =
|
2013-05-19 15:53:42 +10:00
|
|
|
cx.expr_method_call(sp,
|
|
|
|
cx.expr_ident(sp, ident),
|
2013-05-17 11:11:24 -07:00
|
|
|
id_ext("to_tokens"),
|
2014-02-28 13:09:09 -08:00
|
|
|
vec!(cx.expr_ident(sp, id_ext("ext_cx"))));
|
2012-12-06 16:18:41 -08:00
|
|
|
|
2012-12-06 11:01:58 -08:00
|
|
|
let e_push =
|
2013-05-19 15:53:42 +10:00
|
|
|
cx.expr_method_call(sp,
|
2013-05-17 11:11:24 -07:00
|
|
|
cx.expr_ident(sp, id_ext("tt")),
|
|
|
|
id_ext("push_all_move"),
|
2014-02-28 13:09:09 -08:00
|
|
|
vec!(e_to_toks));
|
2012-12-06 16:18:41 -08:00
|
|
|
|
2014-02-28 13:09:09 -08:00
|
|
|
vec!(cx.stmt_expr(e_push))
|
2012-12-06 11:01:58 -08:00
|
|
|
}
|
2012-11-08 17:00:55 -08:00
|
|
|
}
|
2012-11-16 14:50:35 -08:00
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
fn mk_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
2014-05-16 00:16:13 -07:00
|
|
|
-> Vec<Gc<ast::Stmt>> {
|
2014-02-28 13:09:09 -08:00
|
|
|
let mut ss = Vec::new();
|
2013-08-03 12:45:23 -04:00
|
|
|
for tt in tts.iter() {
|
2012-12-06 11:01:58 -08:00
|
|
|
ss.push_all_move(mk_tt(cx, sp, tt));
|
|
|
|
}
|
|
|
|
ss
|
2012-11-20 16:07:57 -08:00
|
|
|
}
|
2012-11-16 14:50:35 -08:00
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
fn expand_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
2014-05-16 00:16:13 -07:00
|
|
|
-> (Gc<ast::Expr>, Gc<ast::Expr>) {
|
2012-11-16 14:50:35 -08:00
|
|
|
// NB: It appears that the main parser loses its mind if we consider
|
2014-01-09 15:05:33 +02:00
|
|
|
// $foo as a TTNonterminal during the main parse, so we have to re-parse
|
2012-11-16 14:50:35 -08:00
|
|
|
// under quote_depth > 0. This is silly and should go away; the _guess_ is
|
|
|
|
// it has to do with transition away from supporting old-style macros, so
|
|
|
|
// try removing it when enough of them are gone.
|
2012-12-06 11:01:58 -08:00
|
|
|
|
2014-07-03 11:42:24 +02:00
|
|
|
let mut p = cx.new_parser_from_tts(tts);
|
2013-12-30 14:40:31 -08:00
|
|
|
p.quote_depth += 1u;
|
2012-11-16 14:50:35 -08:00
|
|
|
|
2013-08-15 02:06:33 -04:00
|
|
|
let cx_expr = p.parse_expr();
|
|
|
|
if !p.eat(&token::COMMA) {
|
2014-02-06 10:38:08 +01:00
|
|
|
p.fatal("expected token `,`");
|
2013-08-15 02:06:33 -04:00
|
|
|
}
|
2012-12-06 11:01:58 -08:00
|
|
|
|
2013-08-15 02:06:33 -04:00
|
|
|
let tts = p.parse_all_token_trees();
|
|
|
|
p.abort_if_errors();
|
2012-12-06 11:01:58 -08:00
|
|
|
|
|
|
|
// We also bind a single value, sp, to ext_cx.call_site()
|
|
|
|
//
|
|
|
|
// This causes every span in a token-tree quote to be attributed to the
|
|
|
|
// call site of the extension using the quote. We can't really do much
|
|
|
|
// better since the source of the quote may well be in a library that
|
|
|
|
// was not even parsed by this compilation run, that the user has no
|
|
|
|
// source code for (eg. in libsyntax, which they're just _using_).
|
|
|
|
//
|
|
|
|
// The old quasiquoter had an elaborate mechanism for denoting input
|
|
|
|
// file locations from which quotes originated; unfortunately this
|
|
|
|
// relied on feeding the source string of the quote back into the
|
|
|
|
// compiler (which we don't really want to do) and, in any case, only
|
|
|
|
// pushed the problem a very small step further back: an error
|
|
|
|
// resulting from a parse of the resulting quote is still attributed to
|
2013-05-09 02:34:47 +09:00
|
|
|
// the site the string literal occurred, which was in a source file
|
2012-12-06 11:01:58 -08:00
|
|
|
// _other_ than the one the user has control over. For example, an
|
|
|
|
// error in a quote from the protocol compiler, invoked in user code
|
2013-08-15 02:06:33 -04:00
|
|
|
// using macro_rules! for example, will be attributed to the macro_rules.rs
|
|
|
|
// file in libsyntax, which the user might not even have source to (unless
|
|
|
|
// they happen to have a compiler on hand). Over all, the phase distinction
|
2012-12-06 11:01:58 -08:00
|
|
|
// just makes quotes "hard to attribute". Possibly this could be fixed
|
|
|
|
// by recreating some of the original qq machinery in the tt regime
|
|
|
|
// (pushing fake FileMaps onto the parser to account for original sites
|
|
|
|
// of quotes, for example) but at this point it seems not likely to be
|
|
|
|
// worth the hassle.
|
|
|
|
|
2013-05-19 15:53:42 +10:00
|
|
|
let e_sp = cx.expr_method_call(sp,
|
2013-05-17 11:11:24 -07:00
|
|
|
cx.expr_ident(sp, id_ext("ext_cx")),
|
|
|
|
id_ext("call_site"),
|
2014-02-28 13:09:09 -08:00
|
|
|
Vec::new());
|
2012-12-06 11:01:58 -08:00
|
|
|
|
2013-05-19 15:53:42 +10:00
|
|
|
let stmt_let_sp = cx.stmt_let(sp, false,
|
2014-01-18 23:00:50 -08:00
|
|
|
id_ext("_sp"),
|
2013-05-19 15:53:42 +10:00
|
|
|
e_sp);
|
2012-12-06 16:18:41 -08:00
|
|
|
|
2014-02-28 12:54:01 -08:00
|
|
|
let stmt_let_tt = cx.stmt_let(sp, true, id_ext("tt"), cx.expr_vec_ng(sp));
|
2012-12-06 11:01:58 -08:00
|
|
|
|
2014-02-28 12:54:01 -08:00
|
|
|
let mut vector = vec!(stmt_let_sp, stmt_let_tt);
|
|
|
|
vector.push_all_move(mk_tts(cx, sp, tts.as_slice()));
|
2013-08-15 02:06:33 -04:00
|
|
|
let block = cx.expr_block(
|
|
|
|
cx.block_all(sp,
|
2014-02-28 13:09:09 -08:00
|
|
|
Vec::new(),
|
2014-02-28 12:54:01 -08:00
|
|
|
vector,
|
2013-08-15 02:06:33 -04:00
|
|
|
Some(cx.expr_ident(sp, id_ext("tt")))));
|
|
|
|
|
|
|
|
(cx_expr, block)
|
|
|
|
}
|
|
|
|
|
2013-12-27 17:17:36 -07:00
|
|
|
fn expand_wrapper(cx: &ExtCtxt,
|
2013-08-31 18:13:04 +02:00
|
|
|
sp: Span,
|
2014-05-16 00:16:13 -07:00
|
|
|
cx_expr: Gc<ast::Expr>,
|
|
|
|
expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
|
2014-05-29 12:19:05 +09:00
|
|
|
let uses = [
|
|
|
|
&["syntax", "ext", "quote", "rt"],
|
|
|
|
].iter().map(|path| {
|
|
|
|
let path = path.iter().map(|s| s.to_string()).collect();
|
|
|
|
cx.view_use_glob(sp, ast::Inherited, ids_ext(path))
|
|
|
|
}).collect();
|
2013-08-15 02:06:33 -04:00
|
|
|
|
|
|
|
let stmt_let_ext_cx = cx.stmt_let(sp, false, id_ext("ext_cx"), cx_expr);
|
|
|
|
|
2014-02-28 13:09:09 -08:00
|
|
|
cx.expr_block(cx.block_all(sp, uses, vec!(stmt_let_ext_cx), Some(expr)))
|
2012-11-16 14:50:35 -08:00
|
|
|
}
|
|
|
|
|
2013-12-27 17:17:36 -07:00
|
|
|
fn expand_parse_call(cx: &ExtCtxt,
|
2013-08-31 18:13:04 +02:00
|
|
|
sp: Span,
|
2013-05-02 10:16:07 +02:00
|
|
|
parse_method: &str,
|
2014-05-16 00:16:13 -07:00
|
|
|
arg_exprs: Vec<Gc<ast::Expr>>,
|
|
|
|
tts: &[ast::TokenTree]) -> Gc<ast::Expr> {
|
2013-08-15 02:06:33 -04:00
|
|
|
let (cx_expr, tts_expr) = expand_tts(cx, sp, tts);
|
2012-11-16 14:50:35 -08:00
|
|
|
|
2013-05-19 15:53:42 +10:00
|
|
|
let cfg_call = || cx.expr_method_call(
|
2013-05-17 11:11:24 -07:00
|
|
|
sp, cx.expr_ident(sp, id_ext("ext_cx")),
|
2014-02-28 13:09:09 -08:00
|
|
|
id_ext("cfg"), Vec::new());
|
2012-11-16 14:50:35 -08:00
|
|
|
|
2013-05-19 15:53:42 +10:00
|
|
|
let parse_sess_call = || cx.expr_method_call(
|
2013-05-17 11:11:24 -07:00
|
|
|
sp, cx.expr_ident(sp, id_ext("ext_cx")),
|
2014-02-28 13:09:09 -08:00
|
|
|
id_ext("parse_sess"), Vec::new());
|
2012-11-16 14:50:35 -08:00
|
|
|
|
|
|
|
let new_parser_call =
|
2013-08-15 02:06:33 -04:00
|
|
|
cx.expr_call(sp,
|
|
|
|
cx.expr_ident(sp, id_ext("new_parser_from_tts")),
|
2014-02-28 13:09:09 -08:00
|
|
|
vec!(parse_sess_call(), cfg_call(), tts_expr));
|
2013-08-15 02:06:33 -04:00
|
|
|
|
|
|
|
let expr = cx.expr_method_call(sp, new_parser_call, id_ext(parse_method),
|
|
|
|
arg_exprs);
|
|
|
|
|
|
|
|
expand_wrapper(cx, sp, cx_expr, expr)
|
2012-11-16 14:50:35 -08:00
|
|
|
}
|