diff options
| author | bors <bors@rust-lang.org> | 2014-03-24 07:11:59 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-03-24 07:11:59 -0700 |
| commit | f8f60d80bf573cd8b4b5680b44c9cabe2b862f78 (patch) | |
| tree | 6273a9a825d6622409c5766c5d70e452c2dd5c58 /src/libsyntax | |
| parent | 11d9483d5881e5ab6963e5ddab2f2e9b91f045c8 (diff) | |
| parent | cda33346d0e9e4d1ff26163583831f96d42a4bc0 (diff) | |
| download | rust-f8f60d80bf573cd8b4b5680b44c9cabe2b862f78.tar.gz rust-f8f60d80bf573cd8b4b5680b44c9cabe2b862f78.zip | |
auto merge of #12998 : huonw/rust/log_syntax, r=alexcrichton
syntax: allow `trace_macros!` and `log_syntax!` in item position.
Previously
trace_macros!(true)
fn main() {}
would complain about `trace_macros` being an expression macro in item
position. This is a pointless limitation, because the macro is purely
compile-time, with no runtime effect. (And similarly for log_syntax.)
This also changes the behaviour of `trace_macros!` very slightly, it
used to be equivalent to
macro_rules! trace_macros {
(true $($_x: tt)*) => { true };
(false $($_x: tt)*) => { false }
}
I.e. you could invoke it with arbitrary trailing arguments, which were
ignored. It is changed to accept only exactly `true` or `false` (with no
trailing arguments) and expands to `()`.
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ext/base.rs | 22 | ||||
| -rw-r--r-- | src/libsyntax/ext/log_syntax.rs | 14 | ||||
| -rw-r--r-- | src/libsyntax/ext/trace_macros.rs | 32 |
3 files changed, 33 insertions, 35 deletions
diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index ccf98f031a1..2f502c1b55f 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -119,13 +119,31 @@ impl MacResult { pub fn raw_dummy_expr(sp: codemap::Span) -> @ast::Expr { @ast::Expr { id: ast::DUMMY_NODE_ID, - node: ast::ExprTup(Vec::new()), - span: sp + node: ast::ExprLit(@codemap::respan(sp, ast::LitNil)), + span: sp, } } pub fn dummy_expr(sp: codemap::Span) -> MacResult { MRExpr(MacResult::raw_dummy_expr(sp)) } + pub fn dummy_any(sp: codemap::Span) -> MacResult { + MRAny(~DummyMacResult { sp: sp }) + } +} +struct DummyMacResult { + sp: codemap::Span +} +impl AnyMacro for DummyMacResult { + fn make_expr(&self) -> @ast::Expr { + MacResult::raw_dummy_expr(self.sp) + } + fn make_items(&self) -> SmallVector<@ast::Item> { + SmallVector::zero() + } + fn make_stmt(&self) -> @ast::Stmt { + @codemap::respan(self.sp, + ast::StmtExpr(MacResult::raw_dummy_expr(self.sp), ast::DUMMY_NODE_ID)) + } } /// An enum representing the different kinds of syntax extensions. diff --git a/src/libsyntax/ext/log_syntax.rs b/src/libsyntax/ext/log_syntax.rs index b94928238e9..1ce08b8303e 100644 --- a/src/libsyntax/ext/log_syntax.rs +++ b/src/libsyntax/ext/log_syntax.rs @@ -10,11 +10,10 @@ use ast; use codemap; -use ext::base::*; use ext::base; use print; -pub fn expand_syntax_ext(cx: &mut ExtCtxt, +pub fn expand_syntax_ext(cx: &mut base::ExtCtxt, sp: codemap::Span, tt: &[ast::TokenTree]) -> base::MacResult { @@ -23,13 +22,6 @@ pub fn expand_syntax_ext(cx: &mut ExtCtxt, println!("{}", print::pprust::tt_to_str(&ast::TTDelim( @tt.iter().map(|x| (*x).clone()).collect()))); - //trivial expression - MRExpr(@ast::Expr { - id: ast::DUMMY_NODE_ID, - node: ast::ExprLit(@codemap::Spanned { - node: ast::LitNil, - span: sp - }), - span: sp, - }) + // any so that `log_syntax` can be invoked as an expression and item. + base::MacResult::dummy_any(sp) } diff --git a/src/libsyntax/ext/trace_macros.rs b/src/libsyntax/ext/trace_macros.rs index fa49f06e516..173cf4c9ad9 100644 --- a/src/libsyntax/ext/trace_macros.rs +++ b/src/libsyntax/ext/trace_macros.rs @@ -12,33 +12,21 @@ use ast; use codemap::Span; use ext::base::ExtCtxt; use ext::base; -use parse::lexer::{new_tt_reader, Reader}; -use parse::parser::Parser; -use parse::token::keywords; +use parse::token::{keywords, is_keyword}; pub fn expand_trace_macros(cx: &mut ExtCtxt, sp: Span, tt: &[ast::TokenTree]) -> base::MacResult { - let sess = cx.parse_sess(); - let cfg = cx.cfg(); - let tt_rdr = new_tt_reader(&cx.parse_sess().span_diagnostic, - None, - tt.iter().map(|x| (*x).clone()).collect()); - let mut rust_parser = Parser(sess, cfg.clone(), tt_rdr.dup()); - - if rust_parser.is_keyword(keywords::True) { - cx.set_trace_macros(true); - } else if rust_parser.is_keyword(keywords::False) { - cx.set_trace_macros(false); - } else { - cx.span_err(sp, "trace_macros! only accepts `true` or `false`"); - return base::MacResult::dummy_expr(sp); + match tt { + [ast::TTTok(_, ref tok)] if is_keyword(keywords::True, tok) => { + cx.set_trace_macros(true); + } + [ast::TTTok(_, ref tok)] if is_keyword(keywords::False, tok) => { + cx.set_trace_macros(false); + } + _ => cx.span_err(sp, "trace_macros! accepts only `true` or `false`"), } - rust_parser.bump(); - - let mut rust_parser = Parser(sess, cfg, tt_rdr.dup()); - let result = rust_parser.parse_expr(); - base::MRExpr(result) + base::MacResult::dummy_any(sp) } |
