Skip to content

Commit cef4378

Browse files
committed
Refactoring: Only use MacroExpander for expanding outside of
`syntax::ext::expand`
1 parent 94d92e6 commit cef4378

File tree

4 files changed

+42
-27
lines changed

4 files changed

+42
-27
lines changed

src/libregex_macros/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use syntax::ext::build::AstBuilder;
3434
use syntax::ext::base::{ExtCtxt, MacResult, MacExpr, DummyResult};
3535
use syntax::parse::token;
3636
use syntax::print::pprust;
37+
use syntax::fold::Folder;
3738

3839
use rustc::plugin::Registry;
3940

@@ -615,7 +616,7 @@ fn exec<'t>(which: ::regex::native::MatchKind, input: &'t str,
615616
/// Otherwise, logs an error with cx.span_err and returns None.
616617
fn parse(cx: &mut ExtCtxt, tts: &[ast::TokenTree]) -> Option<String> {
617618
let mut parser = cx.new_parser_from_tts(tts);
618-
let entry = cx.expand_expr(parser.parse_expr());
619+
let entry = cx.expander().fold_expr(parser.parse_expr());
619620
let regex = match entry.node {
620621
ast::ExprLit(lit) => {
621622
match lit.node {

src/libsyntax/ext/base.rs

+12-15
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use parse::token;
2020
use parse::token::{InternedString, intern, str_to_ident};
2121
use util::small_vector::SmallVector;
2222
use ext::mtwt;
23+
use fold::Folder;
2324

2425
use std::collections::HashMap;
2526
use std::gc::{Gc, GC};
@@ -434,7 +435,7 @@ pub struct ExtCtxt<'a> {
434435
pub trace_mac: bool,
435436
pub exported_macros: Vec<Gc<ast::Item>>,
436437

437-
pub syntax_env: SyntaxEnv,
438+
pub syntax_env: SyntaxEnv,
438439
}
439440

440441
impl<'a> ExtCtxt<'a> {
@@ -452,18 +453,14 @@ impl<'a> ExtCtxt<'a> {
452453
}
453454
}
454455

455-
pub fn expand_expr(&mut self, mut e: Gc<ast::Expr>) -> Gc<ast::Expr> {
456-
loop {
457-
match e.node {
458-
ast::ExprMac(..) => {
459-
let mut expander = expand::MacroExpander {
460-
cx: self,
461-
};
462-
e = expand::expand_expr(e, &mut expander);
463-
}
464-
_ => return e
465-
}
466-
}
456+
#[deprecated = "Replaced with `expander().fold_expr()`"]
457+
pub fn expand_expr(&mut self, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
458+
self.expander().fold_expr(e)
459+
}
460+
461+
/// Returns a `Folder` for deeply expanding all macros in a AST node.
462+
pub fn expander<'b>(&'b mut self) -> expand::MacroExpander<'b, 'a> {
463+
expand::MacroExpander { cx: self }
467464
}
468465

469466
pub fn new_parser_from_tts(&self, tts: &[ast::TokenTree])
@@ -573,7 +570,7 @@ impl<'a> ExtCtxt<'a> {
573570
pub fn expr_to_string(cx: &mut ExtCtxt, expr: Gc<ast::Expr>, err_msg: &str)
574571
-> Option<(InternedString, ast::StrStyle)> {
575572
// we want to be able to handle e.g. concat("foo", "bar")
576-
let expr = cx.expand_expr(expr);
573+
let expr = cx.expander().fold_expr(expr);
577574
match expr.node {
578575
ast::ExprLit(l) => match l.node {
579576
ast::LitStr(ref s, style) => return Some(((*s).clone(), style)),
@@ -630,7 +627,7 @@ pub fn get_exprs_from_tts(cx: &mut ExtCtxt,
630627
let mut p = cx.new_parser_from_tts(tts);
631628
let mut es = Vec::new();
632629
while p.token != token::EOF {
633-
es.push(cx.expand_expr(p.parse_expr()));
630+
es.push(cx.expander().fold_expr(p.parse_expr()));
634631
if p.eat(&token::COMMA) {
635632
continue;
636633
}

src/libsyntax/ext/expand.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use util::small_vector::SmallVector;
3232
use std::gc::{Gc, GC};
3333

3434

35-
pub fn expand_expr(e: Gc<ast::Expr>, fld: &mut MacroExpander) -> Gc<ast::Expr> {
35+
fn expand_expr(e: Gc<ast::Expr>, fld: &mut MacroExpander) -> Gc<ast::Expr> {
3636
match e.node {
3737
// expr_mac should really be expr_ext or something; it's the
3838
// entry-point for all syntax extensions.
@@ -1347,16 +1347,6 @@ mod test {
13471347
name_finder.ident_accumulator
13481348
}
13491349

1350-
//fn expand_and_resolve(crate_str: @str) -> ast::crate {
1351-
//let expanded_ast = expand_crate_str(crate_str);
1352-
// println!("expanded: {:?}\n",expanded_ast);
1353-
//mtwt_resolve_crate(expanded_ast)
1354-
//}
1355-
//fn expand_and_resolve_and_pretty_print (crate_str: @str) -> String {
1356-
//let resolved_ast = expand_and_resolve(crate_str);
1357-
//pprust::to_string(&resolved_ast,fake_print_crate,get_ident_interner())
1358-
//}
1359-
13601350
#[test] fn macro_tokens_should_match(){
13611351
expand_crate_str(
13621352
"macro_rules! m((a)=>(13)) fn main(){m!(a);}".to_string());
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(macro_rules)]
12+
13+
macro_rules! foo2 {
14+
() => {
15+
"foo"
16+
}
17+
}
18+
19+
macro_rules! foo {
20+
() => {
21+
foo2!()
22+
}
23+
}
24+
25+
fn main() {
26+
assert_eq!(concat!(foo!(), "bar"), "foobar")
27+
}

0 commit comments

Comments
 (0)