Skip to content

Commit c6d0117

Browse files
committed
rustc: add new token-tree based quasiquoter.
1 parent 12b212f commit c6d0117

File tree

5 files changed

+368
-19
lines changed

5 files changed

+368
-19
lines changed

src/libsyntax/ext/base.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ fn syntax_expander_table() -> HashMap<~str, syntax_extension> {
9696
ext::log_syntax::expand_syntax_ext));
9797
syntax_expanders.insert(~"ast",
9898
builtin(ext::qquote::expand_ast));
99+
syntax_expanders.insert(~"quote",
100+
builtin_expr_tt(ext::quote::expand_quote));
99101
syntax_expanders.insert(~"line",
100102
builtin(ext::source_util::expand_line));
101103
syntax_expanders.insert(~"col",

src/libsyntax/ext/build.rs

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ fn mk_unary(cx: ext_ctxt, sp: span, op: ast::unop, e: @ast::expr)
3434
cx.next_id(); // see ast_util::op_expr_callee_id
3535
mk_expr(cx, sp, ast::expr_unary(op, e))
3636
}
37+
fn mk_raw_path(sp: span, idents: ~[ast::ident]) -> @ast::path {
38+
let p : @ast::path = @{span: sp, global: false, idents: idents,
39+
rp: None, types: ~[]};
40+
return p;
41+
}
3742
fn mk_path(cx: ext_ctxt, sp: span, idents: ~[ast::ident]) ->
3843
@ast::expr {
39-
let path = @{span: sp, global: false, idents: idents,
40-
rp: None, types: ~[]};
41-
let pathexpr = ast::expr_path(path);
42-
mk_expr(cx, sp, pathexpr)
44+
mk_expr(cx, sp, ast::expr_path(mk_raw_path(sp, idents)))
4345
}
4446
fn mk_access_(cx: ext_ctxt, sp: span, p: @ast::expr, m: ast::ident)
4547
-> @ast::expr {
@@ -53,7 +55,6 @@ fn mk_access(cx: ext_ctxt, sp: span, p: ~[ast::ident], m: ast::ident)
5355
fn mk_addr_of(cx: ext_ctxt, sp: span, e: @ast::expr) -> @ast::expr {
5456
return mk_expr(cx, sp, ast::expr_addr_of(ast::m_imm, e));
5557
}
56-
5758
fn mk_call_(cx: ext_ctxt, sp: span, fn_expr: @ast::expr,
5859
args: ~[@ast::expr]) -> @ast::expr {
5960
mk_expr(cx, sp, ast::expr_call(fn_expr, args, false))
@@ -90,19 +91,54 @@ fn mk_base_str(cx: ext_ctxt, sp: span, s: ~str) -> @ast::expr {
9091
fn mk_uniq_str(cx: ext_ctxt, sp: span, s: ~str) -> @ast::expr {
9192
mk_vstore_e(cx, sp, mk_base_str(cx, sp, s), ast::expr_vstore_uniq)
9293
}
93-
94+
fn mk_field(sp: span, f: &{ident: ast::ident, ex: @ast::expr})
95+
-> ast::field {
96+
{node: {mutbl: ast::m_imm, ident: f.ident, expr: f.ex}, span: sp}
97+
}
98+
fn mk_fields(sp: span, fields: ~[{ident: ast::ident, ex: @ast::expr}]) ->
99+
~[ast::field] {
100+
move fields.map(|f| mk_field(sp, f))
101+
}
94102
fn mk_rec_e(cx: ext_ctxt, sp: span,
95103
fields: ~[{ident: ast::ident, ex: @ast::expr}]) ->
96104
@ast::expr {
97-
let mut astfields: ~[ast::field] = ~[];
98-
for fields.each |field| {
99-
let ident = field.ident;
100-
let val = field.ex;
101-
let astfield =
102-
{node: {mutbl: ast::m_imm, ident: ident, expr: val}, span: sp};
103-
astfields.push(astfield);
104-
}
105-
let recexpr = ast::expr_rec(astfields, option::None::<@ast::expr>);
106-
mk_expr(cx, sp, recexpr)
105+
mk_expr(cx, sp, ast::expr_rec(mk_fields(sp, fields),
106+
option::None::<@ast::expr>))
107+
}
108+
fn mk_struct_e(cx: ext_ctxt, sp: span,
109+
ctor_path: ~[ast::ident],
110+
fields: ~[{ident: ast::ident, ex: @ast::expr}]) ->
111+
@ast::expr {
112+
mk_expr(cx, sp,
113+
ast::expr_struct(mk_raw_path(sp, ctor_path),
114+
mk_fields(sp, fields),
115+
option::None::<@ast::expr>))
116+
}
117+
fn mk_glob_use(cx: ext_ctxt, sp: span,
118+
path: ~[ast::ident]) -> @ast::view_item {
119+
let glob = @{node: ast::view_path_glob(mk_raw_path(sp, path),
120+
cx.next_id()),
121+
span: sp};
122+
@{node: ast::view_item_import(~[glob]),
123+
attrs: ~[],
124+
vis: ast::private,
125+
span: sp}
126+
}
127+
fn mk_block(cx: ext_ctxt, sp: span,
128+
view_items: ~[@ast::view_item],
129+
stmts: ~[@ast::stmt],
130+
expr: Option<@ast::expr>) -> @ast::expr {
131+
let blk = {node: {view_items: view_items,
132+
stmts: stmts,
133+
expr: expr,
134+
id: cx.next_id(),
135+
rules: ast::default_blk },
136+
span: sp };
137+
mk_expr(cx, sp, ast::expr_block(blk))
138+
}
139+
fn mk_copy(cx: ext_ctxt, sp: span, e: @ast::expr) -> @ast::expr {
140+
mk_expr(cx, sp, ast::expr_copy(e))
141+
}
142+
fn mk_managed(cx: ext_ctxt, sp: span, e: @ast::expr) -> @ast::expr {
143+
mk_expr(cx, sp, ast::expr_unary(ast::box(ast::m_imm), e))
107144
}
108-

0 commit comments

Comments
 (0)