2012-12-03 18:48:01 -06: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.
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use ast;
|
2013-01-25 18:57:39 -06:00
|
|
|
use codemap::{BytePos, Pos, span};
|
2013-05-17 06:27:17 -05:00
|
|
|
use ext::base::ExtCtxt;
|
2012-12-23 16:41:37 -06:00
|
|
|
use ext::base;
|
2013-05-17 09:19:28 -05:00
|
|
|
use ext::build::AstBuilder;
|
2012-12-13 15:05:22 -06:00
|
|
|
use parse::token::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use parse::token;
|
|
|
|
use parse;
|
|
|
|
|
2013-05-24 21:35:29 -05:00
|
|
|
use core::vec;
|
|
|
|
|
2012-11-08 19:00:55 -06:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Quasiquoting works via token trees.
|
|
|
|
*
|
2012-12-06 18:18:41 -06:00
|
|
|
* This is registered as a set of expression syntax extension called quote!
|
|
|
|
* that lifts its argument token-tree to an AST representing the
|
|
|
|
* construction of the same token tree, with ast::tt_nonterminal nodes
|
|
|
|
* interpreted as antiquotes (splices).
|
2012-11-08 19:00:55 -06:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-11-16 16:50:35 -06:00
|
|
|
pub mod rt {
|
2013-05-17 17:28:44 -05:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use ast;
|
2013-05-17 06:27:17 -05:00
|
|
|
use ext::base::ExtCtxt;
|
2012-12-23 16:41:37 -06:00
|
|
|
use parse;
|
|
|
|
use print::pprust;
|
|
|
|
|
|
|
|
use core::str;
|
|
|
|
|
2012-11-16 16:50:35 -06:00
|
|
|
pub use ast::*;
|
|
|
|
pub use parse::token::*;
|
2012-11-20 18:07:57 -06:00
|
|
|
pub use parse::new_parser_from_tts;
|
2013-03-27 08:49:39 -05:00
|
|
|
pub use codemap::{BytePos, span, dummy_spanned};
|
2012-12-06 18:18:41 -06:00
|
|
|
|
2012-12-13 15:05:22 -06:00
|
|
|
use print::pprust::{item_to_str, ty_to_str};
|
2012-12-06 18:18:41 -06:00
|
|
|
|
2013-02-25 13:11:21 -06:00
|
|
|
pub trait ToTokens {
|
2013-05-17 06:27:17 -05:00
|
|
|
pub fn to_tokens(&self, _cx: @ExtCtxt) -> ~[token_tree];
|
2012-12-06 18:18:41 -06:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToTokens for ~[token_tree] {
|
2013-05-17 06:27:17 -05:00
|
|
|
pub fn to_tokens(&self, _cx: @ExtCtxt) -> ~[token_tree] {
|
2013-02-14 23:50:03 -06:00
|
|
|
copy *self
|
2012-12-06 18:18:41 -06: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.
|
2013-05-17 06:27:17 -05:00
|
|
|
pub fn to_source(cx: @ExtCtxt) -> ~str;
|
2012-12-06 18:18:41 -06:00
|
|
|
|
|
|
|
// If you can make source, you can definitely make tokens.
|
2013-05-17 06:27:17 -05:00
|
|
|
pub fn to_tokens(cx: @ExtCtxt) -> ~[token_tree] {
|
2012-12-06 18:18:41 -06:00
|
|
|
cx.parse_tts(self.to_source(cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-02-25 13:11:21 -06:00
|
|
|
pub trait ToSource {
|
2012-12-06 18:18:41 -06:00
|
|
|
// Takes a thing and generates a string containing rust code for it.
|
2013-05-17 06:27:17 -05:00
|
|
|
pub fn to_source(&self, cx: @ExtCtxt) -> ~str;
|
2012-12-06 18:18:41 -06:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToSource for ast::ident {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_source(&self, cx: @ExtCtxt) -> ~str {
|
2013-05-17 12:18:35 -05:00
|
|
|
copy *get_ident_interner().get(*self)
|
2012-12-06 18:18:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToSource for @ast::item {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_source(&self, cx: @ExtCtxt) -> ~str {
|
2013-05-17 12:18:35 -05:00
|
|
|
item_to_str(*self, get_ident_interner())
|
2012-12-06 18:18:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-27 09:01:45 -05:00
|
|
|
impl<'self> ToSource for &'self [@ast::item] {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_source(&self, cx: @ExtCtxt) -> ~str {
|
2013-05-19 00:07:44 -05:00
|
|
|
str::connect(self.map(|i| i.to_source(cx)), "\n\n")
|
2012-12-06 18:18:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToSource for @ast::Ty {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_source(&self, cx: @ExtCtxt) -> ~str {
|
2013-05-17 12:18:35 -05:00
|
|
|
ty_to_str(*self, get_ident_interner())
|
2012-12-06 18:18:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-27 09:01:45 -05:00
|
|
|
impl<'self> ToSource for &'self [@ast::Ty] {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_source(&self, cx: @ExtCtxt) -> ~str {
|
2013-05-19 00:07:44 -05:00
|
|
|
str::connect(self.map(|i| i.to_source(cx)), ", ")
|
2012-12-06 18:18:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 23:50:03 -06:00
|
|
|
impl ToSource for Generics {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_source(&self, cx: @ExtCtxt) -> ~str {
|
2013-05-17 12:18:35 -05:00
|
|
|
pprust::generics_to_str(self, get_ident_interner())
|
2012-12-06 18:18:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToSource for @ast::expr {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_source(&self, cx: @ExtCtxt) -> ~str {
|
2013-05-17 12:18:35 -05:00
|
|
|
pprust::expr_to_str(*self, get_ident_interner())
|
2012-12-06 18:18:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-27 08:49:39 -05:00
|
|
|
impl ToSource for ast::blk {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_source(&self, cx: @ExtCtxt) -> ~str {
|
2013-05-17 12:18:35 -05:00
|
|
|
pprust::block_to_str(self, get_ident_interner())
|
2013-03-27 08:49:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self> ToSource for &'self str {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_source(&self, _cx: @ExtCtxt) -> ~str {
|
2013-05-10 06:08:56 -05:00
|
|
|
let lit = dummy_spanned(ast::lit_str(@str::to_owned(*self)));
|
2013-03-27 08:49:39 -05:00
|
|
|
pprust::lit_to_str(@lit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToSource for int {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_source(&self, _cx: @ExtCtxt) -> ~str {
|
2013-03-27 08:49:39 -05:00
|
|
|
let lit = dummy_spanned(ast::lit_int(*self as i64, ast::ty_i));
|
|
|
|
pprust::lit_to_str(@lit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToSource for i8 {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_source(&self, _cx: @ExtCtxt) -> ~str {
|
2013-03-27 08:49:39 -05:00
|
|
|
let lit = dummy_spanned(ast::lit_int(*self as i64, ast::ty_i8));
|
|
|
|
pprust::lit_to_str(@lit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToSource for i16 {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_source(&self, _cx: @ExtCtxt) -> ~str {
|
2013-03-27 08:49:39 -05:00
|
|
|
let lit = dummy_spanned(ast::lit_int(*self as i64, ast::ty_i16));
|
|
|
|
pprust::lit_to_str(@lit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl ToSource for i32 {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_source(&self, _cx: @ExtCtxt) -> ~str {
|
2013-03-27 08:49:39 -05:00
|
|
|
let lit = dummy_spanned(ast::lit_int(*self as i64, ast::ty_i32));
|
|
|
|
pprust::lit_to_str(@lit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToSource for i64 {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_source(&self, _cx: @ExtCtxt) -> ~str {
|
2013-03-27 08:49:39 -05:00
|
|
|
let lit = dummy_spanned(ast::lit_int(*self as i64, ast::ty_i64));
|
|
|
|
pprust::lit_to_str(@lit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToSource for uint {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_source(&self, _cx: @ExtCtxt) -> ~str {
|
2013-03-27 08:49:39 -05:00
|
|
|
let lit = dummy_spanned(ast::lit_uint(*self as u64, ast::ty_u));
|
|
|
|
pprust::lit_to_str(@lit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToSource for u8 {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_source(&self, _cx: @ExtCtxt) -> ~str {
|
2013-03-27 08:49:39 -05:00
|
|
|
let lit = dummy_spanned(ast::lit_uint(*self as u64, ast::ty_u8));
|
|
|
|
pprust::lit_to_str(@lit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToSource for u16 {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_source(&self, _cx: @ExtCtxt) -> ~str {
|
2013-03-27 08:49:39 -05:00
|
|
|
let lit = dummy_spanned(ast::lit_uint(*self as u64, ast::ty_u16));
|
|
|
|
pprust::lit_to_str(@lit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToSource for u32 {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_source(&self, _cx: @ExtCtxt) -> ~str {
|
2013-03-27 08:49:39 -05:00
|
|
|
let lit = dummy_spanned(ast::lit_uint(*self as u64, ast::ty_u32));
|
|
|
|
pprust::lit_to_str(@lit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToSource for u64 {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_source(&self, _cx: @ExtCtxt) -> ~str {
|
2013-03-27 08:49:39 -05:00
|
|
|
let lit = dummy_spanned(ast::lit_uint(*self as u64, ast::ty_u64));
|
|
|
|
pprust::lit_to_str(@lit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-06 18:18:41 -06:00
|
|
|
// Alas ... we write these out instead. All redundant.
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToTokens for ast::ident {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] {
|
2012-12-06 18:18:41 -06:00
|
|
|
cx.parse_tts(self.to_source(cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToTokens for @ast::item {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] {
|
2012-12-06 18:18:41 -06:00
|
|
|
cx.parse_tts(self.to_source(cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-27 09:01:45 -05:00
|
|
|
impl<'self> ToTokens for &'self [@ast::item] {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] {
|
2012-12-06 18:18:41 -06:00
|
|
|
cx.parse_tts(self.to_source(cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToTokens for @ast::Ty {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] {
|
2012-12-06 18:18:41 -06:00
|
|
|
cx.parse_tts(self.to_source(cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-27 09:01:45 -05:00
|
|
|
impl<'self> ToTokens for &'self [@ast::Ty] {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] {
|
2012-12-06 18:18:41 -06:00
|
|
|
cx.parse_tts(self.to_source(cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 23:50:03 -06:00
|
|
|
impl ToTokens for Generics {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] {
|
2012-12-06 18:18:41 -06:00
|
|
|
cx.parse_tts(self.to_source(cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToTokens for @ast::expr {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] {
|
2012-12-06 18:18:41 -06:00
|
|
|
cx.parse_tts(self.to_source(cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-27 08:49:39 -05:00
|
|
|
impl ToTokens for ast::blk {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] {
|
2013-03-27 08:49:39 -05:00
|
|
|
cx.parse_tts(self.to_source(cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self> ToTokens for &'self str {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] {
|
2013-03-27 08:49:39 -05:00
|
|
|
cx.parse_tts(self.to_source(cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTokens for int {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] {
|
2013-03-27 08:49:39 -05:00
|
|
|
cx.parse_tts(self.to_source(cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTokens for i8 {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] {
|
2013-03-27 08:49:39 -05:00
|
|
|
cx.parse_tts(self.to_source(cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTokens for i16 {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] {
|
2013-03-27 08:49:39 -05:00
|
|
|
cx.parse_tts(self.to_source(cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTokens for i32 {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] {
|
2013-03-27 08:49:39 -05:00
|
|
|
cx.parse_tts(self.to_source(cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTokens for i64 {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] {
|
2013-03-27 08:49:39 -05:00
|
|
|
cx.parse_tts(self.to_source(cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTokens for uint {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] {
|
2013-03-27 08:49:39 -05:00
|
|
|
cx.parse_tts(self.to_source(cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTokens for u8 {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] {
|
2013-03-27 08:49:39 -05:00
|
|
|
cx.parse_tts(self.to_source(cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTokens for u16 {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] {
|
2013-03-27 08:49:39 -05:00
|
|
|
cx.parse_tts(self.to_source(cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTokens for u32 {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] {
|
2013-03-27 08:49:39 -05:00
|
|
|
cx.parse_tts(self.to_source(cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTokens for u64 {
|
2013-05-17 06:27:17 -05:00
|
|
|
fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] {
|
2013-03-27 08:49:39 -05:00
|
|
|
cx.parse_tts(self.to_source(cx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-25 13:11:21 -06:00
|
|
|
pub trait ExtParseUtils {
|
2013-03-02 15:23:38 -06:00
|
|
|
fn parse_item(&self, s: ~str) -> @ast::item;
|
|
|
|
fn parse_expr(&self, s: ~str) -> @ast::expr;
|
|
|
|
fn parse_stmt(&self, s: ~str) -> @ast::stmt;
|
|
|
|
fn parse_tts(&self, s: ~str) -> ~[ast::token_tree];
|
2012-12-06 18:18:41 -06:00
|
|
|
}
|
|
|
|
|
2013-05-17 06:27:17 -05:00
|
|
|
impl ExtParseUtils for ExtCtxt {
|
2012-12-06 18:18:41 -06:00
|
|
|
|
2013-03-02 15:23:38 -06:00
|
|
|
fn parse_item(&self, s: ~str) -> @ast::item {
|
2012-12-06 18:18:41 -06:00
|
|
|
let res = parse::parse_item_from_source_str(
|
|
|
|
~"<quote expansion>",
|
|
|
|
@(copy s),
|
|
|
|
self.cfg(),
|
|
|
|
~[],
|
|
|
|
self.parse_sess());
|
|
|
|
match res {
|
|
|
|
Some(ast) => ast,
|
|
|
|
None => {
|
|
|
|
error!("Parse error with ```\n%s\n```", s);
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!()
|
2012-12-06 18:18:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-02 15:23:38 -06:00
|
|
|
fn parse_stmt(&self, s: ~str) -> @ast::stmt {
|
2012-12-06 18:18:41 -06:00
|
|
|
parse::parse_stmt_from_source_str(
|
|
|
|
~"<quote expansion>",
|
|
|
|
@(copy s),
|
|
|
|
self.cfg(),
|
|
|
|
~[],
|
|
|
|
self.parse_sess())
|
|
|
|
}
|
|
|
|
|
2013-03-02 15:23:38 -06:00
|
|
|
fn parse_expr(&self, s: ~str) -> @ast::expr {
|
2012-12-06 18:18:41 -06:00
|
|
|
parse::parse_expr_from_source_str(
|
|
|
|
~"<quote expansion>",
|
|
|
|
@(copy s),
|
|
|
|
self.cfg(),
|
|
|
|
self.parse_sess())
|
|
|
|
}
|
|
|
|
|
2013-03-02 15:23:38 -06:00
|
|
|
fn parse_tts(&self, s: ~str) -> ~[ast::token_tree] {
|
2012-12-06 18:18:41 -06:00
|
|
|
parse::parse_tts_from_source_str(
|
|
|
|
~"<quote expansion>",
|
|
|
|
@(copy s),
|
|
|
|
self.cfg(),
|
|
|
|
self.parse_sess())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-16 16:50:35 -06:00
|
|
|
}
|
2012-11-08 19:00:55 -06:00
|
|
|
|
2013-05-17 06:27:17 -05:00
|
|
|
pub fn expand_quote_tokens(cx: @ExtCtxt,
|
2012-11-16 16:50:35 -06:00
|
|
|
sp: span,
|
2013-02-18 23:25:44 -06:00
|
|
|
tts: &[ast::token_tree]) -> base::MacResult {
|
2013-01-22 18:45:27 -06:00
|
|
|
base::MRExpr(expand_tts(cx, sp, tts))
|
2012-11-16 16:50:35 -06:00
|
|
|
}
|
2012-11-08 19:00:55 -06:00
|
|
|
|
2013-05-17 06:27:17 -05:00
|
|
|
pub fn expand_quote_expr(cx: @ExtCtxt,
|
2012-11-16 16:50:35 -06:00
|
|
|
sp: span,
|
2013-02-18 23:25:44 -06:00
|
|
|
tts: &[ast::token_tree]) -> base::MacResult {
|
2013-05-02 03:16:07 -05:00
|
|
|
base::MRExpr(expand_parse_call(cx, sp, "parse_expr", ~[], tts))
|
2012-11-16 16:50:35 -06:00
|
|
|
}
|
|
|
|
|
2013-05-17 06:27:17 -05:00
|
|
|
pub fn expand_quote_item(cx: @ExtCtxt,
|
2012-11-16 16:50:35 -06:00
|
|
|
sp: span,
|
2013-02-18 23:25:44 -06:00
|
|
|
tts: &[ast::token_tree]) -> base::MacResult {
|
2013-05-19 00:53:42 -05:00
|
|
|
let e_attrs = cx.expr_vec_uniq(sp, ~[]);
|
2013-05-02 03:16:07 -05:00
|
|
|
base::MRExpr(expand_parse_call(cx, sp, "parse_item",
|
2012-11-16 16:50:35 -06:00
|
|
|
~[e_attrs], tts))
|
|
|
|
}
|
|
|
|
|
2013-05-17 06:27:17 -05:00
|
|
|
pub fn expand_quote_pat(cx: @ExtCtxt,
|
2012-11-16 16:50:35 -06:00
|
|
|
sp: span,
|
2013-02-18 23:25:44 -06:00
|
|
|
tts: &[ast::token_tree]) -> base::MacResult {
|
2013-05-19 00:53:42 -05:00
|
|
|
let e_refutable = cx.expr_lit(sp, ast::lit_bool(true));
|
2013-05-02 03:16:07 -05:00
|
|
|
base::MRExpr(expand_parse_call(cx, sp, "parse_pat",
|
2012-11-16 16:50:35 -06:00
|
|
|
~[e_refutable], tts))
|
|
|
|
}
|
|
|
|
|
2013-05-17 06:27:17 -05:00
|
|
|
pub fn expand_quote_ty(cx: @ExtCtxt,
|
2012-12-06 13:09:46 -06:00
|
|
|
sp: span,
|
2013-02-18 23:25:44 -06:00
|
|
|
tts: &[ast::token_tree]) -> base::MacResult {
|
2013-05-19 00:53:42 -05:00
|
|
|
let e_param_colons = cx.expr_lit(sp, ast::lit_bool(false));
|
2013-05-02 03:16:07 -05:00
|
|
|
base::MRExpr(expand_parse_call(cx, sp, "parse_ty",
|
2012-11-16 16:50:35 -06:00
|
|
|
~[e_param_colons], tts))
|
|
|
|
}
|
|
|
|
|
2013-05-17 06:27:17 -05:00
|
|
|
pub fn expand_quote_stmt(cx: @ExtCtxt,
|
2012-11-16 16:50:35 -06:00
|
|
|
sp: span,
|
2013-02-18 23:25:44 -06:00
|
|
|
tts: &[ast::token_tree]) -> base::MacResult {
|
2013-05-19 00:53:42 -05:00
|
|
|
let e_attrs = cx.expr_vec_uniq(sp, ~[]);
|
2013-05-02 03:16:07 -05:00
|
|
|
base::MRExpr(expand_parse_call(cx, sp, "parse_stmt",
|
2012-11-16 16:50:35 -06:00
|
|
|
~[e_attrs], tts))
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
|
|
|
|
2013-05-17 06:27:17 -05:00
|
|
|
fn ids_ext(cx: @ExtCtxt, strs: ~[~str]) -> ~[ast::ident] {
|
2013-05-17 12:18:35 -05:00
|
|
|
strs.map(|str| get_ident_interner().intern(*str))
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
|
|
|
|
2013-05-17 06:27:17 -05:00
|
|
|
fn id_ext(cx: @ExtCtxt, str: &str) -> ast::ident {
|
2013-05-17 12:18:35 -05:00
|
|
|
get_ident_interner().intern(str)
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lift an ident to the expr that evaluates to that ident.
|
2013-05-17 06:27:17 -05:00
|
|
|
fn mk_ident(cx: @ExtCtxt, sp: span, ident: ast::ident) -> @ast::expr {
|
2013-05-19 00:53:42 -05:00
|
|
|
let e_str = cx.expr_str(sp, cx.str_of(ident));
|
|
|
|
cx.expr_method_call(sp,
|
|
|
|
cx.expr_ident(sp, id_ext(cx, "ext_cx")),
|
|
|
|
id_ext(cx, "ident_of"),
|
|
|
|
~[e_str])
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
|
|
|
|
2013-05-17 06:27:17 -05:00
|
|
|
fn mk_bytepos(cx: @ExtCtxt, sp: span, bpos: BytePos) -> @ast::expr {
|
2013-05-19 00:53:42 -05:00
|
|
|
let path = id_ext(cx, "BytePos");
|
|
|
|
let arg = cx.expr_uint(sp, bpos.to_uint());
|
|
|
|
cx.expr_call_ident(sp, path, ~[arg])
|
2012-11-16 20:54:48 -06:00
|
|
|
}
|
2012-11-08 19:00:55 -06:00
|
|
|
|
2013-05-17 06:27:17 -05:00
|
|
|
fn mk_binop(cx: @ExtCtxt, sp: span, bop: token::binop) -> @ast::expr {
|
2012-11-08 19:00:55 -06: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"
|
|
|
|
};
|
2013-05-19 00:53:42 -05:00
|
|
|
cx.expr_ident(sp, id_ext(cx, name))
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
|
|
|
|
2013-05-17 06:27:17 -05:00
|
|
|
fn mk_token(cx: @ExtCtxt, sp: span, tok: &token::Token) -> @ast::expr {
|
2012-11-08 19:00:55 -06:00
|
|
|
|
2013-05-11 23:25:31 -05:00
|
|
|
match *tok {
|
2012-11-08 19:00:55 -06:00
|
|
|
BINOP(binop) => {
|
2013-05-19 00:53:42 -05:00
|
|
|
return cx.expr_call_ident(sp,
|
|
|
|
id_ext(cx, "BINOP"),
|
|
|
|
~[mk_binop(cx, sp, binop)]);
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
|
|
|
BINOPEQ(binop) => {
|
2013-05-19 00:53:42 -05:00
|
|
|
return cx.expr_call_ident(sp,
|
|
|
|
id_ext(cx, "BINOPEQ"),
|
|
|
|
~[mk_binop(cx, sp, binop)]);
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
LIT_INT(i, ity) => {
|
|
|
|
let s_ity = match ity {
|
|
|
|
ast::ty_i => ~"ty_i",
|
|
|
|
ast::ty_char => ~"ty_char",
|
|
|
|
ast::ty_i8 => ~"ty_i8",
|
|
|
|
ast::ty_i16 => ~"ty_i16",
|
|
|
|
ast::ty_i32 => ~"ty_i32",
|
|
|
|
ast::ty_i64 => ~"ty_i64"
|
|
|
|
};
|
2013-05-19 00:53:42 -05:00
|
|
|
let e_ity = cx.expr_ident(sp, id_ext(cx, s_ity));
|
2012-11-08 19:00:55 -06:00
|
|
|
|
2013-05-19 00:53:42 -05:00
|
|
|
let e_i64 = cx.expr_lit(sp, ast::lit_int(i, ast::ty_i64));
|
2012-11-08 19:00:55 -06:00
|
|
|
|
2013-05-19 00:53:42 -05:00
|
|
|
return cx.expr_call_ident(sp,
|
|
|
|
id_ext(cx, "LIT_INT"),
|
|
|
|
~[e_i64, e_ity]);
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
LIT_UINT(u, uty) => {
|
|
|
|
let s_uty = match uty {
|
|
|
|
ast::ty_u => ~"ty_u",
|
|
|
|
ast::ty_u8 => ~"ty_u8",
|
|
|
|
ast::ty_u16 => ~"ty_u16",
|
|
|
|
ast::ty_u32 => ~"ty_u32",
|
|
|
|
ast::ty_u64 => ~"ty_u64"
|
|
|
|
};
|
2013-05-19 00:53:42 -05:00
|
|
|
let e_uty = cx.expr_ident(sp, id_ext(cx, s_uty));
|
2012-11-08 19:00:55 -06:00
|
|
|
|
2013-05-19 00:53:42 -05:00
|
|
|
let e_u64 = cx.expr_lit(sp, ast::lit_uint(u, ast::ty_u64));
|
2012-11-08 19:00:55 -06:00
|
|
|
|
2013-05-19 00:53:42 -05:00
|
|
|
return cx.expr_call_ident(sp,
|
|
|
|
id_ext(cx, "LIT_UINT"),
|
|
|
|
~[e_u64, e_uty]);
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
LIT_INT_UNSUFFIXED(i) => {
|
2013-05-19 00:53:42 -05:00
|
|
|
let e_i64 = cx.expr_lit(sp, ast::lit_int(i, ast::ty_i64));
|
2012-11-08 19:00:55 -06:00
|
|
|
|
2013-05-19 00:53:42 -05:00
|
|
|
return cx.expr_call_ident(sp,
|
|
|
|
id_ext(cx, "LIT_INT_UNSUFFIXED"),
|
|
|
|
~[e_i64]);
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
LIT_FLOAT(fident, fty) => {
|
|
|
|
let s_fty = match fty {
|
|
|
|
ast::ty_f => ~"ty_f",
|
|
|
|
ast::ty_f32 => ~"ty_f32",
|
|
|
|
ast::ty_f64 => ~"ty_f64"
|
|
|
|
};
|
2013-05-19 00:53:42 -05:00
|
|
|
let e_fty = cx.expr_ident(sp, id_ext(cx, s_fty));
|
2012-11-08 19:00:55 -06:00
|
|
|
|
|
|
|
let e_fident = mk_ident(cx, sp, fident);
|
|
|
|
|
2013-05-19 00:53:42 -05:00
|
|
|
return cx.expr_call_ident(sp,
|
|
|
|
id_ext(cx, "LIT_FLOAT"),
|
|
|
|
~[e_fident, e_fty]);
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
LIT_STR(ident) => {
|
2013-05-19 00:53:42 -05:00
|
|
|
return cx.expr_call_ident(sp,
|
|
|
|
id_ext(cx, "LIT_STR"),
|
|
|
|
~[mk_ident(cx, sp, ident)]);
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
IDENT(ident, b) => {
|
2013-05-19 00:53:42 -05:00
|
|
|
return cx.expr_call_ident(sp,
|
|
|
|
id_ext(cx, "IDENT"),
|
|
|
|
~[mk_ident(cx, sp, ident),
|
|
|
|
cx.expr_bool(sp, b)]);
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
|
|
|
|
2013-03-14 13:22:51 -05:00
|
|
|
LIFETIME(ident) => {
|
2013-05-19 00:53:42 -05:00
|
|
|
return cx.expr_call_ident(sp,
|
|
|
|
id_ext(cx, "LIFETIME"),
|
|
|
|
~[mk_ident(cx, sp, ident)]);
|
2013-03-14 13:22:51 -05:00
|
|
|
}
|
|
|
|
|
2012-11-08 19:00:55 -06:00
|
|
|
DOC_COMMENT(ident) => {
|
2013-05-19 00:53:42 -05:00
|
|
|
return cx.expr_call_ident(sp,
|
|
|
|
id_ext(cx, "DOC_COMMENT"),
|
|
|
|
~[mk_ident(cx, sp, ident)]);
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
|
|
|
|
2013-05-05 17:18:51 -05:00
|
|
|
INTERPOLATED(_) => fail!("quote! with interpolated token"),
|
2012-11-08 19:00:55 -06:00
|
|
|
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
|
2013-05-11 23:25:31 -05:00
|
|
|
let name = match *tok {
|
2012-11-08 19:00:55 -06: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",
|
|
|
|
DARROW => "DARROW",
|
|
|
|
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-02-11 21:26:38 -06:00
|
|
|
_ => fail!()
|
2012-11-08 19:00:55 -06:00
|
|
|
};
|
2013-05-19 00:53:42 -05:00
|
|
|
cx.expr_ident(sp, id_ext(cx, name))
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-17 06:27:17 -05:00
|
|
|
fn mk_tt(cx: @ExtCtxt, sp: span, tt: &ast::token_tree)
|
2012-12-06 13:01:58 -06:00
|
|
|
-> ~[@ast::stmt] {
|
|
|
|
|
2012-11-08 19:00:55 -06:00
|
|
|
match *tt {
|
2012-12-06 13:01:58 -06:00
|
|
|
|
2012-12-04 12:50:00 -06:00
|
|
|
ast::tt_tok(sp, ref tok) => {
|
2013-05-19 00:53:42 -05:00
|
|
|
let e_sp = cx.expr_ident(sp, id_ext(cx, "sp"));
|
|
|
|
let e_tok = cx.expr_call_ident(sp,
|
|
|
|
id_ext(cx, "tt_tok"),
|
|
|
|
~[e_sp, mk_token(cx, sp, tok)]);
|
2012-12-06 13:01:58 -06:00
|
|
|
let e_push =
|
2013-05-19 00:53:42 -05:00
|
|
|
cx.expr_method_call(sp,
|
|
|
|
cx.expr_ident(sp, id_ext(cx, "tt")),
|
|
|
|
id_ext(cx, "push"),
|
|
|
|
~[e_tok]);
|
|
|
|
~[cx.stmt_expr(e_push)]
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
|
|
|
|
2012-12-06 13:01:58 -06:00
|
|
|
ast::tt_delim(ref tts) => mk_tts(cx, sp, *tts),
|
2013-05-05 17:18:51 -05:00
|
|
|
ast::tt_seq(*) => fail!("tt_seq in quote!"),
|
2012-11-08 19:00:55 -06:00
|
|
|
|
2012-12-06 13:01:58 -06:00
|
|
|
ast::tt_nonterminal(sp, ident) => {
|
2012-12-06 18:18:41 -06:00
|
|
|
|
|
|
|
// tt.push_all_move($ident.to_tokens(ext_cx))
|
|
|
|
|
|
|
|
let e_to_toks =
|
2013-05-19 00:53:42 -05:00
|
|
|
cx.expr_method_call(sp,
|
|
|
|
cx.expr_ident(sp, ident),
|
|
|
|
id_ext(cx, "to_tokens"),
|
|
|
|
~[cx.expr_ident(sp, id_ext(cx, "ext_cx"))]);
|
2012-12-06 18:18:41 -06:00
|
|
|
|
2012-12-06 13:01:58 -06:00
|
|
|
let e_push =
|
2013-05-19 00:53:42 -05:00
|
|
|
cx.expr_method_call(sp,
|
|
|
|
cx.expr_ident(sp, id_ext(cx, "tt")),
|
|
|
|
id_ext(cx, "push_all_move"),
|
|
|
|
~[e_to_toks]);
|
2012-12-06 18:18:41 -06:00
|
|
|
|
2013-05-19 00:53:42 -05:00
|
|
|
~[cx.stmt_expr(e_push)]
|
2012-12-06 13:01:58 -06:00
|
|
|
}
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
2012-11-16 16:50:35 -06:00
|
|
|
}
|
|
|
|
|
2013-05-17 06:27:17 -05:00
|
|
|
fn mk_tts(cx: @ExtCtxt, sp: span, tts: &[ast::token_tree])
|
2012-12-06 13:01:58 -06:00
|
|
|
-> ~[@ast::stmt] {
|
|
|
|
let mut ss = ~[];
|
|
|
|
for tts.each |tt| {
|
|
|
|
ss.push_all_move(mk_tt(cx, sp, tt));
|
|
|
|
}
|
|
|
|
ss
|
2012-11-20 18:07:57 -06:00
|
|
|
}
|
2012-11-16 16:50:35 -06:00
|
|
|
|
2013-05-17 06:27:17 -05:00
|
|
|
fn expand_tts(cx: @ExtCtxt,
|
2012-11-20 18:07:57 -06:00
|
|
|
sp: span,
|
2013-02-18 23:25:44 -06:00
|
|
|
tts: &[ast::token_tree]) -> @ast::expr {
|
2012-12-06 13:01:58 -06:00
|
|
|
|
2012-11-16 16:50:35 -06:00
|
|
|
// NB: It appears that the main parser loses its mind if we consider
|
|
|
|
// $foo as a tt_nonterminal during the main parse, so we have to re-parse
|
|
|
|
// 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 13:01:58 -06:00
|
|
|
|
2013-02-18 23:25:44 -06:00
|
|
|
let p = parse::new_parser_from_tts(
|
|
|
|
cx.parse_sess(),
|
|
|
|
cx.cfg(),
|
2013-05-10 04:38:54 -05:00
|
|
|
vec::to_owned(tts)
|
2013-02-18 23:25:44 -06:00
|
|
|
);
|
2013-02-21 20:12:13 -06:00
|
|
|
*p.quote_depth += 1u;
|
2012-11-20 18:07:57 -06:00
|
|
|
let tts = p.parse_all_token_trees();
|
|
|
|
p.abort_if_errors();
|
2012-11-16 16:50:35 -06:00
|
|
|
|
|
|
|
// We want to emit a block expression that does a sequence of 'use's to
|
2012-12-06 13:01:58 -06:00
|
|
|
// import the runtime module, followed by a tt-building expression.
|
|
|
|
|
2013-05-19 00:53:42 -05:00
|
|
|
let uses = ~[ cx.view_use_glob(sp, ast::public,
|
|
|
|
ids_ext(cx, ~[~"syntax",
|
|
|
|
~"ext",
|
|
|
|
~"quote",
|
|
|
|
~"rt"])) ];
|
2012-12-06 13:01:58 -06: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-08 12:34:47 -05:00
|
|
|
// the site the string literal occurred, which was in a source file
|
2012-12-06 13:01:58 -06: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
|
|
|
|
// using proto! for example, will be attributed to the pipec.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
|
|
|
|
// 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 00:53:42 -05:00
|
|
|
let e_sp = cx.expr_method_call(sp,
|
|
|
|
cx.expr_ident(sp, id_ext(cx, "ext_cx")),
|
|
|
|
id_ext(cx, "call_site"),
|
|
|
|
~[]);
|
2012-12-06 13:01:58 -06:00
|
|
|
|
2013-05-19 00:53:42 -05:00
|
|
|
let stmt_let_sp = cx.stmt_let(sp, false,
|
|
|
|
id_ext(cx, "sp"),
|
|
|
|
e_sp);
|
2012-12-06 18:18:41 -06:00
|
|
|
|
2013-05-19 00:53:42 -05:00
|
|
|
let stmt_let_tt = cx.stmt_let(sp, true,
|
|
|
|
id_ext(cx, "tt"),
|
|
|
|
cx.expr_vec_uniq(sp, ~[]));
|
2012-12-06 13:01:58 -06:00
|
|
|
|
2013-05-19 00:53:42 -05:00
|
|
|
cx.expr_blk(
|
|
|
|
cx.blk_all(sp, uses,
|
|
|
|
~[stmt_let_sp,
|
|
|
|
stmt_let_tt] + mk_tts(cx, sp, tts),
|
|
|
|
Some(cx.expr_ident(sp, id_ext(cx, "tt")))))
|
2012-11-16 16:50:35 -06:00
|
|
|
}
|
|
|
|
|
2013-05-17 06:27:17 -05:00
|
|
|
fn expand_parse_call(cx: @ExtCtxt,
|
2012-11-16 16:50:35 -06:00
|
|
|
sp: span,
|
2013-05-02 03:16:07 -05:00
|
|
|
parse_method: &str,
|
2013-04-17 11:15:08 -05:00
|
|
|
arg_exprs: ~[@ast::expr],
|
2013-02-18 23:25:44 -06:00
|
|
|
tts: &[ast::token_tree]) -> @ast::expr {
|
2012-11-20 18:07:57 -06:00
|
|
|
let tts_expr = expand_tts(cx, sp, tts);
|
2012-11-16 16:50:35 -06:00
|
|
|
|
2013-05-19 00:53:42 -05:00
|
|
|
let cfg_call = || cx.expr_method_call(
|
|
|
|
sp, cx.expr_ident(sp, id_ext(cx, "ext_cx")),
|
2013-05-02 03:16:07 -05:00
|
|
|
id_ext(cx, "cfg"), ~[]);
|
2012-11-16 16:50:35 -06:00
|
|
|
|
2013-05-19 00:53:42 -05:00
|
|
|
let parse_sess_call = || cx.expr_method_call(
|
|
|
|
sp, cx.expr_ident(sp, id_ext(cx, "ext_cx")),
|
2013-05-02 03:16:07 -05:00
|
|
|
id_ext(cx, "parse_sess"), ~[]);
|
2012-11-16 16:50:35 -06:00
|
|
|
|
|
|
|
let new_parser_call =
|
2013-05-19 00:53:42 -05:00
|
|
|
cx.expr_call_global(sp,
|
|
|
|
ids_ext(cx, ~[~"syntax",
|
|
|
|
~"ext",
|
|
|
|
~"quote",
|
|
|
|
~"rt",
|
|
|
|
~"new_parser_from_tts"]),
|
|
|
|
~[parse_sess_call(),
|
|
|
|
cfg_call(),
|
|
|
|
tts_expr]);
|
|
|
|
|
|
|
|
cx.expr_method_call(sp, new_parser_call,
|
|
|
|
id_ext(cx, parse_method),
|
|
|
|
arg_exprs)
|
2012-11-16 16:50:35 -06:00
|
|
|
}
|