Skip to content

Commit 513276e

Browse files
committed
Add #concat_idents[] and #ident_to_str[]
1 parent 4a636b0 commit 513276e

File tree

6 files changed

+65
-9
lines changed

6 files changed

+65
-9
lines changed

src/comp/rustc.rc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ mod syntax {
5757
}
5858
mod ext {
5959
mod base;
60+
mod expand;
61+
6062
mod fmt;
6163
mod env;
6264
mod simplext;
63-
mod expand;
65+
mod concat_idents;
66+
mod ident_to_str;
6467
}
6568
mod print {
6669
mod pprust;

src/comp/syntax/ext/base.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ fn syntax_expander_table() -> hashmap[str, syntax_extension] {
2525
syntax_expanders.insert("env", normal(ext::env::expand_syntax_ext));
2626
syntax_expanders.insert("macro",
2727
macro_defining(ext::simplext::add_new_extension));
28+
syntax_expanders.insert("concat_idents",
29+
normal(ext::concat_idents::expand_syntax_ext));
30+
syntax_expanders.insert("ident_to_str",
31+
normal(ext::ident_to_str::expand_syntax_ext));
2832
ret syntax_expanders;
2933
}
3034

@@ -107,6 +111,12 @@ fn expr_to_ident(cx: &ext_ctxt, expr: @ast::expr, error: str) -> ast::ident {
107111
}
108112
}
109113

114+
fn make_new_lit(cx: &ext_ctxt, sp: codemap::span, lit: ast::lit_) ->
115+
@ast::expr {
116+
let sp_lit = @{node: lit, span: sp};
117+
ret @{id: cx.next_id(), node: ast::expr_lit(sp_lit), span: sp};
118+
}
119+
110120

111121

112122
//

src/comp/syntax/ext/concat_idents.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import std::ivec;
2+
import std::option;
3+
import base::*;
4+
import syntax::ast;
5+
6+
fn expand_syntax_ext(cx: &ext_ctxt, sp: codemap::span, arg: @ast::expr,
7+
body: option::t[str]) -> @ast::expr {
8+
let args: (@ast::expr)[] = alt arg.node {
9+
ast::expr_vec(elts, _, _) { elts }
10+
_ { cx.span_fatal(sp, "#concat_idents requires a vector argument .") }
11+
};
12+
let res: ast::ident = "";
13+
for e: @ast::expr in args {
14+
res += expr_to_ident(cx, e, "expected an ident");
15+
}
16+
17+
ret @{id: cx.next_id(),
18+
node: ast::expr_path( {
19+
node: {global: false, idents: ~[res], types: ~[]},
20+
span: sp}),
21+
span: sp};
22+
}

src/comp/syntax/ext/env.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11

2-
32
/*
43
* The compiler code necessary to support the #env extension. Eventually this
54
* should all get sucked into either the compiler syntax extension plugin
65
* interface.
76
*/
87
import std::ivec;
9-
import std::str;
108
import std::option;
119
import std::generic_os;
1210
import base::*;
@@ -31,12 +29,6 @@ fn expand_syntax_ext(cx: &ext_ctxt, sp: codemap::span, arg: @ast::expr,
3129
}
3230
}
3331

34-
fn make_new_lit(cx: &ext_ctxt, sp: codemap::span, lit: ast::lit_) ->
35-
@ast::expr {
36-
let sp_lit = @{node: lit, span: sp};
37-
ret @{id: cx.next_id(), node: ast::expr_lit(sp_lit), span: sp};
38-
}
39-
4032
fn make_new_str(cx: &ext_ctxt, sp: codemap::span, s: str) -> @ast::expr {
4133
ret make_new_lit(cx, sp, ast::lit_str(s, ast::sk_rc));
4234
}

src/comp/syntax/ext/ident_to_str.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import std::ivec;
2+
import std::option;
3+
import base::*;
4+
import syntax::ast;
5+
6+
fn expand_syntax_ext(cx: &ext_ctxt, sp: codemap::span, arg: @ast::expr,
7+
body: option::t[str]) -> @ast::expr {
8+
let args: (@ast::expr)[] = alt arg.node {
9+
ast::expr_vec(elts, _, _) { elts }
10+
_ { cx.span_fatal(sp, "#ident_to_str requires a vector argument .") }
11+
};
12+
if ivec::len[@ast::expr](args) != 1u {
13+
cx.span_fatal(sp, "malformed #ident_to_str call");
14+
}
15+
16+
ret make_new_lit(cx, sp,
17+
ast::lit_str(expr_to_ident(cx, args.(0u),
18+
"expected an ident"),
19+
ast::sk_rc));
20+
21+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
fn main() {
3+
let asdf_fdsa = "<.<";
4+
assert(#concat_idents[asd,f_f,dsa] == "<.<");
5+
6+
assert(#ident_to_str[use_mention_distinction]
7+
== "use_mention_distinction");
8+
}

0 commit comments

Comments
 (0)