Refactoring: Only use MacroExpander for expanding outside of

`syntax::ext::expand`
This commit is contained in:
Marvin Löbel 2014-07-20 16:25:35 +02:00
parent 94d92e6830
commit cef4378269
4 changed files with 42 additions and 27 deletions

View File

@ -34,6 +34,7 @@ use syntax::ext::build::AstBuilder;
use syntax::ext::base::{ExtCtxt, MacResult, MacExpr, DummyResult};
use syntax::parse::token;
use syntax::print::pprust;
use syntax::fold::Folder;
use rustc::plugin::Registry;
@ -615,7 +616,7 @@ fn exec<'t>(which: ::regex::native::MatchKind, input: &'t str,
/// Otherwise, logs an error with cx.span_err and returns None.
fn parse(cx: &mut ExtCtxt, tts: &[ast::TokenTree]) -> Option<String> {
let mut parser = cx.new_parser_from_tts(tts);
let entry = cx.expand_expr(parser.parse_expr());
let entry = cx.expander().fold_expr(parser.parse_expr());
let regex = match entry.node {
ast::ExprLit(lit) => {
match lit.node {

View File

@ -20,6 +20,7 @@ use parse::token;
use parse::token::{InternedString, intern, str_to_ident};
use util::small_vector::SmallVector;
use ext::mtwt;
use fold::Folder;
use std::collections::HashMap;
use std::gc::{Gc, GC};
@ -434,7 +435,7 @@ pub struct ExtCtxt<'a> {
pub trace_mac: bool,
pub exported_macros: Vec<Gc<ast::Item>>,
pub syntax_env: SyntaxEnv,
pub syntax_env: SyntaxEnv,
}
impl<'a> ExtCtxt<'a> {
@ -452,18 +453,14 @@ impl<'a> ExtCtxt<'a> {
}
}
pub fn expand_expr(&mut self, mut e: Gc<ast::Expr>) -> Gc<ast::Expr> {
loop {
match e.node {
ast::ExprMac(..) => {
let mut expander = expand::MacroExpander {
cx: self,
};
e = expand::expand_expr(e, &mut expander);
}
_ => return e
}
}
#[deprecated = "Replaced with `expander().fold_expr()`"]
pub fn expand_expr(&mut self, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
self.expander().fold_expr(e)
}
/// Returns a `Folder` for deeply expanding all macros in a AST node.
pub fn expander<'b>(&'b mut self) -> expand::MacroExpander<'b, 'a> {
expand::MacroExpander { cx: self }
}
pub fn new_parser_from_tts(&self, tts: &[ast::TokenTree])
@ -573,7 +570,7 @@ impl<'a> ExtCtxt<'a> {
pub fn expr_to_string(cx: &mut ExtCtxt, expr: Gc<ast::Expr>, err_msg: &str)
-> Option<(InternedString, ast::StrStyle)> {
// we want to be able to handle e.g. concat("foo", "bar")
let expr = cx.expand_expr(expr);
let expr = cx.expander().fold_expr(expr);
match expr.node {
ast::ExprLit(l) => match l.node {
ast::LitStr(ref s, style) => return Some(((*s).clone(), style)),
@ -630,7 +627,7 @@ pub fn get_exprs_from_tts(cx: &mut ExtCtxt,
let mut p = cx.new_parser_from_tts(tts);
let mut es = Vec::new();
while p.token != token::EOF {
es.push(cx.expand_expr(p.parse_expr()));
es.push(cx.expander().fold_expr(p.parse_expr()));
if p.eat(&token::COMMA) {
continue;
}

View File

@ -32,7 +32,7 @@ use util::small_vector::SmallVector;
use std::gc::{Gc, GC};
pub fn expand_expr(e: Gc<ast::Expr>, fld: &mut MacroExpander) -> Gc<ast::Expr> {
fn expand_expr(e: Gc<ast::Expr>, fld: &mut MacroExpander) -> Gc<ast::Expr> {
match e.node {
// expr_mac should really be expr_ext or something; it's the
// entry-point for all syntax extensions.
@ -1347,16 +1347,6 @@ mod test {
name_finder.ident_accumulator
}
//fn expand_and_resolve(crate_str: @str) -> ast::crate {
//let expanded_ast = expand_crate_str(crate_str);
// println!("expanded: {:?}\n",expanded_ast);
//mtwt_resolve_crate(expanded_ast)
//}
//fn expand_and_resolve_and_pretty_print (crate_str: @str) -> String {
//let resolved_ast = expand_and_resolve(crate_str);
//pprust::to_string(&resolved_ast,fake_print_crate,get_ident_interner())
//}
#[test] fn macro_tokens_should_match(){
expand_crate_str(
"macro_rules! m((a)=>(13)) fn main(){m!(a);}".to_string());

View File

@ -0,0 +1,27 @@
// Copyright 2014 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.
#![feature(macro_rules)]
macro_rules! foo2 {
() => {
"foo"
}
}
macro_rules! foo {
() => {
foo2!()
}
}
fn main() {
assert_eq!(concat!(foo!(), "bar"), "foobar")
}