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;
|
|
|
|
use parse;
|
|
|
|
|
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;
|
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
|
|
|
#[cfg(not(stage0))]
|
|
|
|
use ast::{TokenTree, Generics, Expr};
|
|
|
|
|
|
|
|
// NOTE remove this after snapshot
|
|
|
|
// (stage0 quasiquoter needs this)
|
|
|
|
#[cfg(stage0)]
|
|
|
|
pub use ast::{Generics, TokenTree, TTTok};
|
|
|
|
#[cfg(stage0)]
|
|
|
|
pub use parse::token::{IDENT, SEMI, LBRACE, RBRACE, LIFETIME, COLON, AND, BINOP, EQ,
|
|
|
|
LBRACKET, RBRACKET, LPAREN, RPAREN, POUND, NOT, MOD_SEP, DOT, COMMA};
|
|
|
|
|
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
|
|
|
|
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-02-28 13:09:09 -08:00
|
|
|
impl ToTokens for Vec<TokenTree> {
|
|
|
|
fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
|
2013-07-02 12:47:32 -07:00
|
|
|
(*self).clone()
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-09-02 02:50:59 +02:00
|
|
|
impl ToSource for ast::Ident {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-05-29 12:19:05 +09:00
|
|
|
token::get_ident(*self).get().to_string()
|
2012-12-06 16:18:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
impl ToSource for @ast::Item {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-02-14 07:07:09 +02:00
|
|
|
pprust::item_to_str(*self)
|
2012-12-06 16:18:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
impl<'a> ToSource for &'a [@ast::Item] {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-05-07 16:33:43 -07:00
|
|
|
self.iter()
|
|
|
|
.map(|i| i.to_source())
|
2014-05-22 16:57:53 -07:00
|
|
|
.collect::<Vec<String>>()
|
2014-05-07 16:33:43 -07:00
|
|
|
.connect("\n\n")
|
2014-05-25 03:17:19 -07:00
|
|
|
.to_string()
|
2012-12-06 16:18:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-06 16:57:11 +12:00
|
|
|
impl ToSource for ast::Ty {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-02-14 07:07:09 +02:00
|
|
|
pprust::ty_to_str(self)
|
2012-12-06 16:18:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-09 23:16:18 -08:00
|
|
|
impl<'a> ToSource for &'a [ast::Ty] {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-05-07 16:33:43 -07:00
|
|
|
self.iter()
|
|
|
|
.map(|i| i.to_source())
|
2014-05-22 16:57:53 -07:00
|
|
|
.collect::<Vec<String>>()
|
2014-05-07 16:33:43 -07:00
|
|
|
.connect(", ")
|
2014-05-25 03:17:19 -07:00
|
|
|
.to_string()
|
2012-12-06 16:18:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 21:50:03 -08:00
|
|
|
impl ToSource for Generics {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-02-14 07:07:09 +02:00
|
|
|
pprust::generics_to_str(self)
|
2012-12-06 16:18:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-02 03:45:37 +02:00
|
|
|
impl ToSource for @ast::Expr {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-02-14 07:07:09 +02:00
|
|
|
pprust::expr_to_str(*self)
|
2012-12-06 16:18:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 07:38:55 +02:00
|
|
|
impl ToSource for ast::Block {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-02-14 07:07:09 +02:00
|
|
|
pprust::block_to_str(self)
|
2013-03-27 06:49:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-07 13:46:41 +00:00
|
|
|
impl ToSource for ast::Arg {
|
|
|
|
fn to_source(&self) -> String {
|
|
|
|
pprust::arg_to_str(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-01-10 17:20:07 -08:00
|
|
|
pprust::lit_to_str(&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));
|
|
|
|
pprust::lit_to_str(&lit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
pprust::lit_to_str(&lit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-27 06:49:39 -07:00
|
|
|
impl ToSource for int {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-01-09 15:05:33 +02:00
|
|
|
let lit = dummy_spanned(ast::LitInt(*self as i64, ast::TyI));
|
2014-01-10 17:20:07 -08:00
|
|
|
pprust::lit_to_str(&lit)
|
2013-03-27 06:49:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToSource for i8 {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-01-09 15:05:33 +02:00
|
|
|
let lit = dummy_spanned(ast::LitInt(*self as i64, ast::TyI8));
|
2014-01-10 17:20:07 -08:00
|
|
|
pprust::lit_to_str(&lit)
|
2013-03-27 06:49:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToSource for i16 {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-01-09 15:05:33 +02:00
|
|
|
let lit = dummy_spanned(ast::LitInt(*self as i64, ast::TyI16));
|
2014-01-10 17:20:07 -08:00
|
|
|
pprust::lit_to_str(&lit)
|
2013-03-27 06:49:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl ToSource for i32 {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-01-09 15:05:33 +02:00
|
|
|
let lit = dummy_spanned(ast::LitInt(*self as i64, ast::TyI32));
|
2014-01-10 17:20:07 -08:00
|
|
|
pprust::lit_to_str(&lit)
|
2013-03-27 06:49:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToSource for i64 {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-01-09 15:05:33 +02:00
|
|
|
let lit = dummy_spanned(ast::LitInt(*self as i64, ast::TyI64));
|
2014-01-10 17:20:07 -08:00
|
|
|
pprust::lit_to_str(&lit)
|
2013-03-27 06:49:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToSource for uint {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-01-09 15:05:33 +02:00
|
|
|
let lit = dummy_spanned(ast::LitUint(*self as u64, ast::TyU));
|
2014-01-10 17:20:07 -08:00
|
|
|
pprust::lit_to_str(&lit)
|
2013-03-27 06:49:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToSource for u8 {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-01-09 15:05:33 +02:00
|
|
|
let lit = dummy_spanned(ast::LitUint(*self as u64, ast::TyU8));
|
2014-01-10 17:20:07 -08:00
|
|
|
pprust::lit_to_str(&lit)
|
2013-03-27 06:49:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToSource for u16 {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-01-09 15:05:33 +02:00
|
|
|
let lit = dummy_spanned(ast::LitUint(*self as u64, ast::TyU16));
|
2014-01-10 17:20:07 -08:00
|
|
|
pprust::lit_to_str(&lit)
|
2013-03-27 06:49:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToSource for u32 {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-01-09 15:05:33 +02:00
|
|
|
let lit = dummy_spanned(ast::LitUint(*self as u64, ast::TyU32));
|
2014-01-10 17:20:07 -08:00
|
|
|
pprust::lit_to_str(&lit)
|
2013-03-27 06:49:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToSource for u64 {
|
2014-05-22 16:57:53 -07:00
|
|
|
fn to_source(&self) -> String {
|
2014-01-09 15:05:33 +02:00
|
|
|
let lit = dummy_spanned(ast::LitUint(*self as u64, ast::TyU64));
|
2014-01-10 17:20:07 -08:00
|
|
|
pprust::lit_to_str(&lit)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! impl_to_tokens_self(
|
|
|
|
($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-01-09 15:05:33 +02:00
|
|
|
impl_to_tokens!(@ast::Item)
|
|
|
|
impl_to_tokens_self!(&'a [@ast::Item])
|
2013-08-15 02:06:33 -04:00
|
|
|
impl_to_tokens!(ast::Ty)
|
2013-12-09 23:16:18 -08:00
|
|
|
impl_to_tokens_self!(&'a [ast::Ty])
|
2013-08-15 02:06:33 -04:00
|
|
|
impl_to_tokens!(Generics)
|
2013-09-02 03:45:37 +02:00
|
|
|
impl_to_tokens!(@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)
|
2013-12-09 23:16:18 -08:00
|
|
|
impl_to_tokens_self!(&'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-22 16:57:53 -07:00
|
|
|
fn parse_item(&self, s: String) -> @ast::Item;
|
|
|
|
fn parse_expr(&self, s: String) -> @ast::Expr;
|
|
|
|
fn parse_stmt(&self, s: String) -> @ast::Stmt;
|
|
|
|
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-22 16:57:53 -07:00
|
|
|
fn parse_item(&self, s: String) -> @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-22 16:57:53 -07:00
|
|
|
fn parse_stmt(&self, s: String) -> @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-22 16:57:53 -07:00
|
|
|
fn parse_expr(&self, s: String) -> @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-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_item",
|
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-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-01-09 15:05:33 +02:00
|
|
|
let e_refutable = cx.expr_lit(sp, ast::LitBool(true));
|
2013-07-08 15:55:14 -07:00
|
|
|
let expanded = expand_parse_call(cx, sp, "parse_pat",
|
2014-02-28 13:09:09 -08:00
|
|
|
vec!(e_refutable), 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
|
|
|
}
|
|
|
|
|
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.
|
2013-12-27 17:17:36 -07:00
|
|
|
fn mk_ident(cx: &ExtCtxt, sp: Span, ident: ast::Ident) -> @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-06-04 01:42:11 +09:00
|
|
|
fn mk_ast_path(cx: &ExtCtxt, sp: Span, name: &str) -> @ast::Expr {
|
|
|
|
let idents = vec!(id_ext("syntax"), id_ext("ast"), id_ext(name));
|
|
|
|
cx.expr_path(cx.path_global(sp, idents))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mk_token_path(cx: &ExtCtxt, sp: Span, name: &str) -> @ast::Expr {
|
|
|
|
let idents = vec!(id_ext("syntax"), id_ext("parse"), id_ext("token"), id_ext(name));
|
|
|
|
cx.expr_path(cx.path_global(sp, idents))
|
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
fn mk_binop(cx: &ExtCtxt, sp: Span, bop: token::BinOp) -> @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
|
|
|
}
|
|
|
|
|
2013-12-27 17:17:36 -07:00
|
|
|
fn mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> @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
|
|
|
}
|
|
|
|
|
2013-09-03 19:24:12 -04:00
|
|
|
LIT_CHAR(i) => {
|
2014-01-09 15:05:33 +02:00
|
|
|
let e_char = cx.expr_lit(sp, ast::LitChar(i));
|
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
|
|
|
}
|
|
|
|
|
2012-11-08 17:00:55 -08:00
|
|
|
LIT_INT(i, ity) => {
|
|
|
|
let s_ity = match ity {
|
2014-06-04 01:42:11 +09:00
|
|
|
ast::TyI => "TyI",
|
|
|
|
ast::TyI8 => "TyI8",
|
|
|
|
ast::TyI16 => "TyI16",
|
|
|
|
ast::TyI32 => "TyI32",
|
|
|
|
ast::TyI64 => "TyI64"
|
2012-11-08 17:00:55 -08:00
|
|
|
};
|
2014-06-04 01:42:11 +09:00
|
|
|
let e_ity = mk_ast_path(cx, sp, s_ity);
|
2014-01-09 15:05:33 +02:00
|
|
|
let e_i64 = cx.expr_lit(sp, ast::LitInt(i, ast::TyI64));
|
2014-06-04 01:42:11 +09:00
|
|
|
return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_INT"), vec!(e_i64, e_ity));
|
2012-11-08 17:00:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
LIT_UINT(u, uty) => {
|
|
|
|
let s_uty = match uty {
|
2014-06-04 01:42:11 +09:00
|
|
|
ast::TyU => "TyU",
|
|
|
|
ast::TyU8 => "TyU8",
|
|
|
|
ast::TyU16 => "TyU16",
|
|
|
|
ast::TyU32 => "TyU32",
|
|
|
|
ast::TyU64 => "TyU64"
|
2012-11-08 17:00:55 -08:00
|
|
|
};
|
2014-06-04 01:42:11 +09:00
|
|
|
let e_uty = mk_ast_path(cx, sp, s_uty);
|
2014-01-09 15:05:33 +02:00
|
|
|
let e_u64 = cx.expr_lit(sp, ast::LitUint(u, ast::TyU64));
|
2014-06-04 01:42:11 +09:00
|
|
|
return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_UINT"), vec!(e_u64, e_uty));
|
2012-11-08 17:00:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
LIT_INT_UNSUFFIXED(i) => {
|
2014-01-09 15:05:33 +02:00
|
|
|
let e_i64 = cx.expr_lit(sp, ast::LitInt(i, ast::TyI64));
|
2014-06-04 01:42:11 +09:00
|
|
|
return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_INT_UNSUFFIXED"), vec!(e_i64));
|
2012-11-08 17:00:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
LIT_FLOAT(fident, fty) => {
|
|
|
|
let s_fty = match fty {
|
2014-06-04 01:42:11 +09:00
|
|
|
ast::TyF32 => "TyF32",
|
|
|
|
ast::TyF64 => "TyF64",
|
|
|
|
ast::TyF128 => "TyF128"
|
2012-11-08 17:00:55 -08:00
|
|
|
};
|
2014-06-04 01:42:11 +09:00
|
|
|
let e_fty = mk_ast_path(cx, sp, s_fty);
|
2012-11-08 17:00:55 -08:00
|
|
|
let e_fident = mk_ident(cx, sp, fident);
|
2014-06-04 01:42:11 +09:00
|
|
|
return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_FLOAT"), vec!(e_fident, e_fty));
|
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"),
|
|
|
|
vec!(mk_ident(cx, sp, 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"),
|
|
|
|
vec!(mk_ident(cx, sp, 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"),
|
|
|
|
vec!(mk_ident(cx, sp, 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-02-28 13:09:09 -08:00
|
|
|
fn mk_tt(cx: &ExtCtxt, sp: Span, tt: &ast::TokenTree) -> Vec<@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-02-28 13:09:09 -08:00
|
|
|
-> Vec<@ast::Stmt> {
|
|
|
|
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])
|
2013-12-30 14:04:00 -08:00
|
|
|
-> (@ast::Expr, @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
|
|
|
|
2013-12-30 14:04:00 -08:00
|
|
|
let mut p = parse::new_parser_from_tts(cx.parse_sess(),
|
|
|
|
cx.cfg(),
|
2014-02-28 12:54:01 -08:00
|
|
|
tts.iter()
|
|
|
|
.map(|x| (*x).clone())
|
|
|
|
.collect());
|
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,
|
2013-09-02 03:45:37 +02:00
|
|
|
cx_expr: @ast::Expr,
|
|
|
|
expr: @ast::Expr) -> @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-02-28 13:09:09 -08:00
|
|
|
arg_exprs: Vec<@ast::Expr> ,
|
2014-01-09 15:05:33 +02:00
|
|
|
tts: &[ast::TokenTree]) -> @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
|
|
|
}
|