Skip to content

Commit 07187dd

Browse files
committed
---
yaml --- r: 15867 b: refs/heads/try c: 0eef34b h: refs/heads/master i: 15865: 4ad6780 15863: 1c4b558 v: v3
1 parent 283784e commit 07187dd

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
refs/heads/master: 61b1875c16de39c166b0f4d54bba19f9c6777d1a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5-
refs/heads/try: ac2faad26e2e252dd5596d9577388095ee7bdeee
5+
refs/heads/try: 0eef34bacb3cfe0c6bee9946ec1d85444fb7424a
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/try/src/librustsyntax/ext/base.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ fn syntax_expander_table() -> hashmap<str, syntax_extension> {
5353
builtin(ext::source_util::expand_include));
5454
syntax_expanders.insert("include_str",
5555
builtin(ext::source_util::expand_include_str));
56+
syntax_expanders.insert("include_bin",
57+
builtin(ext::source_util::expand_include_bin));
5658
syntax_expanders.insert("mod",
5759
builtin(ext::source_util::expand_mod));
5860
ret syntax_expanders;
@@ -173,6 +175,11 @@ fn make_new_lit(cx: ext_ctxt, sp: codemap::span, lit: ast::lit_) ->
173175
ret @{id: cx.next_id(), node: ast::expr_lit(sp_lit), span: sp};
174176
}
175177

178+
fn make_new_expr(cx: ext_ctxt, sp: codemap::span, expr: ast::expr_) ->
179+
@ast::expr {
180+
ret @{id: cx.next_id(), node: expr, span: sp};
181+
}
182+
176183
fn get_mac_args_no_max(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
177184
min: uint, name: str) -> [@ast::expr] {
178185
ret get_mac_args(cx, sp, arg, min, none, name);

branches/try/src/librustsyntax/ext/source_util.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export expand_stringify;
1010
export expand_mod;
1111
export expand_include;
1212
export expand_include_str;
13+
export expand_include_bin;
1314

1415
/* #line(): expands to the current line number */
1516
fn expand_line(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
@@ -73,6 +74,25 @@ fn expand_include_str(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg,
7374
}
7475
}
7576

77+
fn expand_include_bin(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg,
78+
_body: ast::mac_body) -> @ast::expr {
79+
let args = get_mac_args(cx,sp,arg,1u,option::some(1u),"include_bin");
80+
81+
let file = expr_to_str(cx, args[0], "#include_bin requires a string");
82+
83+
alt io::read_whole_file(res_rel_file(cx, sp, file)) {
84+
result::ok(src) {
85+
let u8_exprs = vec::map(src) { |char: u8|
86+
make_new_lit(cx, sp, ast::lit_uint(char as u64, ast::ty_u8))
87+
};
88+
ret make_new_expr(cx, sp, ast::expr_vec(u8_exprs, ast::m_imm));
89+
}
90+
result::err(e) {
91+
cx.parse_sess().span_diagnostic.handler().fatal(e)
92+
}
93+
}
94+
}
95+
7696
fn res_rel_file(cx: ext_ctxt, sp: codemap::span, arg: path) -> path {
7797
// NB: relative paths are resolved relative to the compilation unit
7898
if !path::path_is_absolute(arg) {

branches/try/src/test/run-pass/syntax-extension-source-utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ fn main() {
1717
assert(
1818
#include_str["syntax-extension-source-utils-files/includeme.fragment"]
1919
.starts_with("/* this is for "));
20+
assert(
21+
#include_bin["syntax-extension-source-utils-files/includeme.fragment"]
22+
[1] == (42 as u8)); // '*'
2023
// The Windows tests are wrapped in an extra module for some reason
2124
assert(m1::m2::where_am_i().ends_with("m1::m2"));
25+
2226
}

0 commit comments

Comments
 (0)