Implement a concat!() format extension
This extension can be used to concatenate string literals at compile time. C has
this useful ability when placing string literals lexically next to one another,
but this needs to be handled at the syntax extension level to recursively expand
macros.
The major use case for this is something like:
macro_rules! mylog( ($fmt:expr $($arg:tt)*) => {
error2!(concat!(file!(), ":", line!(), " - ", $fmt) $($arg)*);
})
Where the mylog macro will automatically prepend the filename/line number to the
beginning of every log message.
This commit is contained in:
@@ -14,6 +14,7 @@ use codemap;
|
||||
use codemap::{CodeMap, Span, ExpnInfo};
|
||||
use diagnostic::span_handler;
|
||||
use ext;
|
||||
use ext::expand;
|
||||
use parse;
|
||||
use parse::token;
|
||||
use parse::token::{ident_to_str, intern, str_to_ident};
|
||||
@@ -246,6 +247,9 @@ pub fn syntax_expander_table() -> SyntaxEnv {
|
||||
syntax_expanders.insert(intern("concat_idents"),
|
||||
builtin_normal_tt_no_ctxt(
|
||||
ext::concat_idents::expand_syntax_ext));
|
||||
syntax_expanders.insert(intern("concat"),
|
||||
builtin_normal_tt_no_ctxt(
|
||||
ext::concat::expand_syntax_ext));
|
||||
syntax_expanders.insert(intern(&"log_syntax"),
|
||||
builtin_normal_tt_no_ctxt(
|
||||
ext::log_syntax::expand_syntax_ext));
|
||||
@@ -338,6 +342,22 @@ impl ExtCtxt {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expand_expr(@self, mut e: @ast::Expr) -> @ast::Expr {
|
||||
loop {
|
||||
match e.node {
|
||||
ast::ExprMac(*) => {
|
||||
let extsbox = @mut syntax_expander_table();
|
||||
let expander = expand::MacroExpander {
|
||||
extsbox: extsbox,
|
||||
cx: self,
|
||||
};
|
||||
e = expand::expand_expr(extsbox, self, e, &expander);
|
||||
}
|
||||
_ => return e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn codemap(&self) -> @CodeMap { self.parse_sess.cm }
|
||||
pub fn parse_sess(&self) -> @mut parse::ParseSess { self.parse_sess }
|
||||
pub fn cfg(&self) -> ast::CrateConfig { self.cfg.clone() }
|
||||
|
||||
Reference in New Issue
Block a user