-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
auto merge of #9740 : alexcrichton/rust/concat, r=cmr
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.
- Loading branch information
Showing
8 changed files
with
118 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2013 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. | ||
|
||
use std::char; | ||
|
||
use ast; | ||
use codemap; | ||
use ext::base; | ||
use ext::build::AstBuilder; | ||
|
||
pub fn expand_syntax_ext(cx: @base::ExtCtxt, | ||
sp: codemap::Span, | ||
tts: &[ast::token_tree]) -> base::MacResult { | ||
let es = base::get_exprs_from_tts(cx, sp, tts); | ||
let mut accumulator = ~""; | ||
for e in es.move_iter() { | ||
let e = cx.expand_expr(e); | ||
match e.node { | ||
ast::ExprLit(lit) => { | ||
match lit.node { | ||
ast::lit_str(s, _) | | ||
ast::lit_float(s, _) | | ||
ast::lit_float_unsuffixed(s) => { | ||
accumulator.push_str(s); | ||
} | ||
ast::lit_char(c) => { | ||
accumulator.push_char(char::from_u32(c).unwrap()); | ||
} | ||
ast::lit_int(i, _) | | ||
ast::lit_int_unsuffixed(i) => { | ||
accumulator.push_str(format!("{}", i)); | ||
} | ||
ast::lit_uint(u, _) => { | ||
accumulator.push_str(format!("{}", u)); | ||
} | ||
ast::lit_nil => {} | ||
ast::lit_bool(b) => { | ||
accumulator.push_str(format!("{}", b)); | ||
} | ||
ast::lit_binary(*) => { | ||
cx.span_err(e.span, "cannot concatenate a binary literal"); | ||
} | ||
} | ||
} | ||
_ => { | ||
cx.span_err(e.span, "expected a literal"); | ||
} | ||
} | ||
} | ||
return base::MRExpr(cx.expr_str(sp, accumulator.to_managed())); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2013 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. | ||
|
||
fn main() { | ||
concat!(foo); //~ ERROR: expected a literal | ||
concat!(foo()); //~ ERROR: expected a literal | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2013 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. | ||
|
||
pub fn main() { | ||
assert_eq!(format!(concat!("foo", "bar", "{}"), "baz"), ~"foobarbaz"); | ||
assert_eq!(format!(concat!()), ~""); | ||
assert_eq!( | ||
concat!(1, 2i, 3u, 4f32, 4.0, 'a', true, ()), | ||
"12344.0atrue" | ||
); | ||
} |