diff options
| author | Paul Stansifer <paul.stansifer@gmail.com> | 2011-08-03 11:46:32 -0700 |
|---|---|---|
| committer | Paul Stansifer <paul.stansifer@gmail.com> | 2011-08-03 12:42:35 -0700 |
| commit | 513276e595f8a8e988ec824a36b34cebe2aeaab4 (patch) | |
| tree | a2f54f90038d4593b149ea79e6bbcbeacc274a30 | |
| parent | 4a636b06f6fa45379603e91b8283b91616751666 (diff) | |
| download | rust-513276e595f8a8e988ec824a36b34cebe2aeaab4.tar.gz rust-513276e595f8a8e988ec824a36b34cebe2aeaab4.zip | |
Add #concat_idents[] and #ident_to_str[]
| -rw-r--r-- | src/comp/rustc.rc | 5 | ||||
| -rw-r--r-- | src/comp/syntax/ext/base.rs | 10 | ||||
| -rw-r--r-- | src/comp/syntax/ext/concat_idents.rs | 22 | ||||
| -rw-r--r-- | src/comp/syntax/ext/env.rs | 8 | ||||
| -rw-r--r-- | src/comp/syntax/ext/ident_to_str.rs | 21 | ||||
| -rw-r--r-- | src/test/run-pass/syntax-extension-minor.rs | 8 |
6 files changed, 65 insertions, 9 deletions
diff --git a/src/comp/rustc.rc b/src/comp/rustc.rc index fbf4503eef3..ce1cb27eb64 100644 --- a/src/comp/rustc.rc +++ b/src/comp/rustc.rc @@ -57,10 +57,13 @@ mod syntax { } mod ext { mod base; + mod expand; + mod fmt; mod env; mod simplext; - mod expand; + mod concat_idents; + mod ident_to_str; } mod print { mod pprust; diff --git a/src/comp/syntax/ext/base.rs b/src/comp/syntax/ext/base.rs index 06f998e71e8..fd37b6c2d3c 100644 --- a/src/comp/syntax/ext/base.rs +++ b/src/comp/syntax/ext/base.rs @@ -25,6 +25,10 @@ fn syntax_expander_table() -> hashmap[str, syntax_extension] { syntax_expanders.insert("env", normal(ext::env::expand_syntax_ext)); syntax_expanders.insert("macro", macro_defining(ext::simplext::add_new_extension)); + syntax_expanders.insert("concat_idents", + normal(ext::concat_idents::expand_syntax_ext)); + syntax_expanders.insert("ident_to_str", + normal(ext::ident_to_str::expand_syntax_ext)); ret syntax_expanders; } @@ -107,6 +111,12 @@ fn expr_to_ident(cx: &ext_ctxt, expr: @ast::expr, error: str) -> ast::ident { } } +fn make_new_lit(cx: &ext_ctxt, sp: codemap::span, lit: ast::lit_) -> + @ast::expr { + let sp_lit = @{node: lit, span: sp}; + ret @{id: cx.next_id(), node: ast::expr_lit(sp_lit), span: sp}; +} + // diff --git a/src/comp/syntax/ext/concat_idents.rs b/src/comp/syntax/ext/concat_idents.rs new file mode 100644 index 00000000000..d2e6511a993 --- /dev/null +++ b/src/comp/syntax/ext/concat_idents.rs @@ -0,0 +1,22 @@ +import std::ivec; +import std::option; +import base::*; +import syntax::ast; + +fn expand_syntax_ext(cx: &ext_ctxt, sp: codemap::span, arg: @ast::expr, + body: option::t[str]) -> @ast::expr { + let args: (@ast::expr)[] = alt arg.node { + ast::expr_vec(elts, _, _) { elts } + _ { cx.span_fatal(sp, "#concat_idents requires a vector argument .") } + }; + let res: ast::ident = ""; + for e: @ast::expr in args { + res += expr_to_ident(cx, e, "expected an ident"); + } + + ret @{id: cx.next_id(), + node: ast::expr_path( { + node: {global: false, idents: ~[res], types: ~[]}, + span: sp}), + span: sp}; +} \ No newline at end of file diff --git a/src/comp/syntax/ext/env.rs b/src/comp/syntax/ext/env.rs index c6fe7237d2a..f2f4ae6a8fa 100644 --- a/src/comp/syntax/ext/env.rs +++ b/src/comp/syntax/ext/env.rs @@ -1,12 +1,10 @@ - /* * The compiler code necessary to support the #env extension. Eventually this * should all get sucked into either the compiler syntax extension plugin * interface. */ import std::ivec; -import std::str; import std::option; import std::generic_os; import base::*; @@ -31,12 +29,6 @@ fn expand_syntax_ext(cx: &ext_ctxt, sp: codemap::span, arg: @ast::expr, } } -fn make_new_lit(cx: &ext_ctxt, sp: codemap::span, lit: ast::lit_) -> - @ast::expr { - let sp_lit = @{node: lit, span: sp}; - ret @{id: cx.next_id(), node: ast::expr_lit(sp_lit), span: sp}; -} - fn make_new_str(cx: &ext_ctxt, sp: codemap::span, s: str) -> @ast::expr { ret make_new_lit(cx, sp, ast::lit_str(s, ast::sk_rc)); } diff --git a/src/comp/syntax/ext/ident_to_str.rs b/src/comp/syntax/ext/ident_to_str.rs new file mode 100644 index 00000000000..68a4303ec72 --- /dev/null +++ b/src/comp/syntax/ext/ident_to_str.rs @@ -0,0 +1,21 @@ +import std::ivec; +import std::option; +import base::*; +import syntax::ast; + +fn expand_syntax_ext(cx: &ext_ctxt, sp: codemap::span, arg: @ast::expr, + body: option::t[str]) -> @ast::expr { + let args: (@ast::expr)[] = alt arg.node { + ast::expr_vec(elts, _, _) { elts } + _ { cx.span_fatal(sp, "#ident_to_str requires a vector argument .") } + }; + if ivec::len[@ast::expr](args) != 1u { + cx.span_fatal(sp, "malformed #ident_to_str call"); + } + + ret make_new_lit(cx, sp, + ast::lit_str(expr_to_ident(cx, args.(0u), + "expected an ident"), + ast::sk_rc)); + +} \ No newline at end of file diff --git a/src/test/run-pass/syntax-extension-minor.rs b/src/test/run-pass/syntax-extension-minor.rs new file mode 100644 index 00000000000..eabf3ebcbd0 --- /dev/null +++ b/src/test/run-pass/syntax-extension-minor.rs @@ -0,0 +1,8 @@ + +fn main() { + let asdf_fdsa = "<.<"; + assert(#concat_idents[asd,f_f,dsa] == "<.<"); + + assert(#ident_to_str[use_mention_distinction] + == "use_mention_distinction"); +} \ No newline at end of file |
