diff options
| author | Paul Stansifer <paul.stansifer@gmail.com> | 2012-05-18 10:03:27 -0700 |
|---|---|---|
| committer | Paul Stansifer <paul.stansifer@gmail.com> | 2012-05-18 10:05:25 -0700 |
| commit | 0eef34bacb3cfe0c6bee9946ec1d85444fb7424a (patch) | |
| tree | 07a7445b3c58f91699b15e800fb8fee356b5faff /src/librustsyntax/ext/source_util.rs | |
| parent | ac2faad26e2e252dd5596d9577388095ee7bdeee (diff) | |
| download | rust-0eef34bacb3cfe0c6bee9946ec1d85444fb7424a.tar.gz rust-0eef34bacb3cfe0c6bee9946ec1d85444fb7424a.zip | |
add #include_bin[]
Diffstat (limited to 'src/librustsyntax/ext/source_util.rs')
| -rw-r--r-- | src/librustsyntax/ext/source_util.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/librustsyntax/ext/source_util.rs b/src/librustsyntax/ext/source_util.rs index 3cb8db18b82..99b928cfb9c 100644 --- a/src/librustsyntax/ext/source_util.rs +++ b/src/librustsyntax/ext/source_util.rs @@ -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) { |
