add #include_bin[]
This commit is contained in:
parent
ac2faad26e
commit
0eef34bacb
|
@ -53,6 +53,8 @@ fn syntax_expander_table() -> hashmap<str, syntax_extension> {
|
|||
builtin(ext::source_util::expand_include));
|
||||
syntax_expanders.insert("include_str",
|
||||
builtin(ext::source_util::expand_include_str));
|
||||
syntax_expanders.insert("include_bin",
|
||||
builtin(ext::source_util::expand_include_bin));
|
||||
syntax_expanders.insert("mod",
|
||||
builtin(ext::source_util::expand_mod));
|
||||
ret syntax_expanders;
|
||||
|
@ -173,6 +175,11 @@ fn make_new_lit(cx: ext_ctxt, sp: codemap::span, lit: ast::lit_) ->
|
|||
ret @{id: cx.next_id(), node: ast::expr_lit(sp_lit), span: sp};
|
||||
}
|
||||
|
||||
fn make_new_expr(cx: ext_ctxt, sp: codemap::span, expr: ast::expr_) ->
|
||||
@ast::expr {
|
||||
ret @{id: cx.next_id(), node: expr, span: sp};
|
||||
}
|
||||
|
||||
fn get_mac_args_no_max(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
|
||||
min: uint, name: str) -> [@ast::expr] {
|
||||
ret get_mac_args(cx, sp, arg, min, none, name);
|
||||
|
|
|
@ -10,6 +10,7 @@ export expand_stringify;
|
|||
export expand_mod;
|
||||
export expand_include;
|
||||
export expand_include_str;
|
||||
export expand_include_bin;
|
||||
|
||||
/* #line(): expands to the current line number */
|
||||
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,
|
|||
}
|
||||
}
|
||||
|
||||
fn expand_include_bin(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg,
|
||||
_body: ast::mac_body) -> @ast::expr {
|
||||
let args = get_mac_args(cx,sp,arg,1u,option::some(1u),"include_bin");
|
||||
|
||||
let file = expr_to_str(cx, args[0], "#include_bin requires a string");
|
||||
|
||||
alt io::read_whole_file(res_rel_file(cx, sp, file)) {
|
||||
result::ok(src) {
|
||||
let u8_exprs = vec::map(src) { |char: u8|
|
||||
make_new_lit(cx, sp, ast::lit_uint(char as u64, ast::ty_u8))
|
||||
};
|
||||
ret make_new_expr(cx, sp, ast::expr_vec(u8_exprs, ast::m_imm));
|
||||
}
|
||||
result::err(e) {
|
||||
cx.parse_sess().span_diagnostic.handler().fatal(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn res_rel_file(cx: ext_ctxt, sp: codemap::span, arg: path) -> path {
|
||||
// NB: relative paths are resolved relative to the compilation unit
|
||||
if !path::path_is_absolute(arg) {
|
||||
|
|
|
@ -17,6 +17,10 @@ fn main() {
|
|||
assert(
|
||||
#include_str["syntax-extension-source-utils-files/includeme.fragment"]
|
||||
.starts_with("/* this is for "));
|
||||
assert(
|
||||
#include_bin["syntax-extension-source-utils-files/includeme.fragment"]
|
||||
[1] == (42 as u8)); // '*'
|
||||
// The Windows tests are wrapped in an extra module for some reason
|
||||
assert(m1::m2::where_am_i().ends_with("m1::m2"));
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue