Skip to content

Commit

Permalink
Refactoring: Only use MacroExpander for expanding outside of
Browse files Browse the repository at this point in the history
`syntax::ext::expand`
  • Loading branch information
Kimundi committed Jul 21, 2014
1 parent 94d92e6 commit cef4378
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/libregex_macros/lib.rs
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down
27 changes: 12 additions & 15 deletions src/libsyntax/ext/base.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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> {
Expand All @@ -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])
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 1 addition & 11 deletions src/libsyntax/ext/expand.rs
Expand Up @@ -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.
Expand Down Expand Up @@ -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());
Expand Down
27 changes: 27 additions & 0 deletions src/test/run-pass/macro-deep_expansion.rs
@@ -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")
}

5 comments on commit cef4378

@bors
Copy link
Contributor

@bors bors commented on cef4378 Jul 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from cmr
at Kimundi@cef4378

@bors
Copy link
Contributor

@bors bors commented on cef4378 Jul 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging Kimundi/rust/moved_syntax_env = cef4378 into auto

@bors
Copy link
Contributor

@bors bors commented on cef4378 Jul 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kimundi/rust/moved_syntax_env = cef4378 merged ok, testing candidate = 6e3e0a8

@bors
Copy link
Contributor

@bors bors commented on cef4378 Jul 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 6e3e0a8

Please sign in to comment.