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 | |
| parent | ac2faad26e2e252dd5596d9577388095ee7bdeee (diff) | |
| download | rust-0eef34bacb3cfe0c6bee9946ec1d85444fb7424a.tar.gz rust-0eef34bacb3cfe0c6bee9946ec1d85444fb7424a.zip | |
add #include_bin[]
| -rw-r--r-- | src/librustsyntax/ext/base.rs | 7 | ||||
| -rw-r--r-- | src/librustsyntax/ext/source_util.rs | 20 | ||||
| -rw-r--r-- | src/test/run-pass/syntax-extension-source-utils.rs | 4 |
3 files changed, 31 insertions, 0 deletions
diff --git a/src/librustsyntax/ext/base.rs b/src/librustsyntax/ext/base.rs index aeca5b93e02..29e20212d66 100644 --- a/src/librustsyntax/ext/base.rs +++ b/src/librustsyntax/ext/base.rs @@ -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); 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) { diff --git a/src/test/run-pass/syntax-extension-source-utils.rs b/src/test/run-pass/syntax-extension-source-utils.rs index e9eb5d6a503..d784a25de73 100644 --- a/src/test/run-pass/syntax-extension-source-utils.rs +++ b/src/test/run-pass/syntax-extension-source-utils.rs @@ -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")); + } |
