Skip to content

Commit 283784e

Browse files
committed
---
yaml --- r: 15866 b: refs/heads/try c: ac2faad h: refs/heads/master v: v3
1 parent 4ad6780 commit 283784e

File tree

6 files changed

+56
-61
lines changed

6 files changed

+56
-61
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: 07e775404f21b2a3e66e4038461918f73807f9f9
5+
refs/heads/try: ac2faad26e2e252dd5596d9577388095ee7bdeee
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ fn syntax_expander_table() -> hashmap<str, syntax_extension> {
3131
syntax_expanders.insert("auto_serialize",
3232
item_decorator(ext::auto_serialize::expand));
3333
syntax_expanders.insert("env", builtin(ext::env::expand_syntax_ext));
34-
syntax_expanders.insert("include_str",
35-
builtin(ext::include::str::expand_syntax_ext));
3634
syntax_expanders.insert("macro",
3735
macro_defining(ext::simplext::add_new_extension));
3836
syntax_expanders.insert("concat_idents",
@@ -53,6 +51,8 @@ fn syntax_expander_table() -> hashmap<str, syntax_extension> {
5351
builtin(ext::source_util::expand_stringify));
5452
syntax_expanders.insert("include",
5553
builtin(ext::source_util::expand_include));
54+
syntax_expanders.insert("include_str",
55+
builtin(ext::source_util::expand_include_str));
5656
syntax_expanders.insert("mod",
5757
builtin(ext::source_util::expand_mod));
5858
ret syntax_expanders;

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

Lines changed: 0 additions & 49 deletions
This file was deleted.

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

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import ast;
33
import codemap::span;
44
import print::pprust;
55

6+
export expand_line;
7+
export expand_col;
8+
export expand_file;
9+
export expand_stringify;
10+
export expand_mod;
11+
export expand_include;
12+
export expand_include_str;
13+
614
/* #line(): expands to the current line number */
715
fn expand_line(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
816
_body: ast::mac_body) -> @ast::expr {
@@ -35,19 +43,53 @@ fn expand_stringify(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
3543
ret make_new_lit(cx, sp, ast::lit_str(pprust::expr_to_str(args[0])));
3644
}
3745

46+
fn expand_mod(cx: ext_ctxt, sp: span, arg: ast::mac_arg, _body: ast::mac_body)
47+
-> @ast::expr {
48+
get_mac_args(cx, sp, arg, 0u, option::some(0u), "file");
49+
ret make_new_lit(cx, sp, ast::lit_str(str::connect(cx.mod_path(), "::")));
50+
}
51+
3852
fn expand_include(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
3953
_body: ast::mac_body) -> @ast::expr {
4054
let args = get_mac_args(cx, sp, arg, 1u, option::some(1u), "include");
41-
let loc = codemap::lookup_char_pos(cx.codemap(), sp.lo);
42-
let path = path::connect(path::dirname(loc.file.name),
43-
expr_to_str(cx, args[0], "#include requires a string literal"));
44-
let p = parse::new_parser_from_file(cx.parse_sess(), cx.cfg(), path,
55+
let file = expr_to_str(cx, args[0], "#include_str requires a string");
56+
let p = parse::new_parser_from_file(cx.parse_sess(), cx.cfg(),
57+
res_rel_file(cx, sp, file),
4558
parse::parser::SOURCE_FILE);
4659
ret parse::parser::parse_expr(p)
4760
}
4861

49-
fn expand_mod(cx: ext_ctxt, sp: span, arg: ast::mac_arg, _body: ast::mac_body)
50-
-> @ast::expr {
51-
get_mac_args(cx, sp, arg, 0u, option::some(0u), "file");
52-
ret make_new_lit(cx, sp, ast::lit_str(str::connect(cx.mod_path(), "::")));
62+
fn expand_include_str(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg,
63+
_body: ast::mac_body) -> @ast::expr {
64+
let args = get_mac_args(cx,sp,arg,1u,option::some(1u),"include_str");
65+
66+
let file = expr_to_str(cx, args[0], "#include_str requires a string");
67+
68+
alt io::read_whole_file_str(res_rel_file(cx, sp, file)) {
69+
result::ok(src) { ret make_new_lit(cx, sp, ast::lit_str(src)); }
70+
result::err(e) {
71+
cx.parse_sess().span_diagnostic.handler().fatal(e)
72+
}
73+
}
74+
}
75+
76+
fn res_rel_file(cx: ext_ctxt, sp: codemap::span, arg: path) -> path {
77+
// NB: relative paths are resolved relative to the compilation unit
78+
if !path::path_is_absolute(arg) {
79+
let cu = codemap::span_to_filename(sp, cx.codemap());
80+
let dir = path::dirname(cu);
81+
ret path::connect(dir, arg);
82+
} else {
83+
ret arg;
84+
}
5385
}
86+
87+
//
88+
// Local Variables:
89+
// mode: rust
90+
// fill-column: 78;
91+
// indent-tabs-mode: nil
92+
// c-basic-offset: 4
93+
// buffer-file-coding-system: utf-8-unix
94+
// End:
95+
//

branches/try/src/librustsyntax/rustsyntax.rc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ mod ext {
6363
mod simplext;
6464
mod concat_idents;
6565
mod ident_to_str;
66-
mod include;
6766
mod log_syntax;
6867
mod auto_serialize;
6968
mod source_util;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ fn main() {
1414
assert(#stringify[(2*3) + 5] == "2 * 3 + 5");
1515
assert(#include["syntax-extension-source-utils-files/includeme.fragment"]
1616
== "victory robot 6");
17+
assert(
18+
#include_str["syntax-extension-source-utils-files/includeme.fragment"]
19+
.starts_with("/* this is for "));
1720
// The Windows tests are wrapped in an extra module for some reason
1821
assert(m1::m2::where_am_i().ends_with("m1::m2"));
1922
}

0 commit comments

Comments
 (0)