Skip to content

Commit d6633d4

Browse files
committed
---
yaml --- r: 1426 b: refs/heads/master c: 127139a h: refs/heads/master v: v3
1 parent 8663713 commit d6633d4

File tree

3 files changed

+97
-34
lines changed

3 files changed

+97
-34
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 4ebd75a54757b06718fc87a72c20389a9e7f29d0
2+
refs/heads/master: 127139aecd5c3a1227ee58c77fc083506125f7cd

trunk/src/comp/front/ast.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ type crate_ = rec(_mod module);
4646

4747
tag crate_directive_ {
4848
cdir_expr(@expr);
49+
cdir_const(@item);
4950
cdir_src_mod(ident, option.t[filename]);
50-
cdir_dir_mod(ident, option.t[filename], vec[crate_directive]);
51+
cdir_dir_mod(ident, option.t[filename], vec[@crate_directive]);
5152
cdir_view_item(@view_item);
5253
cdir_meta(@meta_item);
5354
cdir_syntax(path);

trunk/src/comp/front/parser.rs

Lines changed: 94 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import std.map.hashmap;
88

99
import driver.session;
1010
import util.common;
11+
import util.common.filename;
1112
import util.common.append;
1213
import util.common.span;
1314
import util.common.new_str_hash;
@@ -2110,9 +2111,81 @@ impure fn parse_crate_from_source_file(parser p) -> @ast.crate {
21102111
//
21112112
// Each directive imperatively extends its environment with 0 or more items.
21122113

2113-
impure fn parse_crate_directive(str prefix, parser p,
2114-
&mutable vec[@ast.item] items,
2115-
hashmap[ast.ident,ast.mod_index_entry] index)
2114+
impure fn eval_crate_directives(parser p,
2115+
vec[@ast.crate_directive] cdirs,
2116+
str prefix) -> ast._mod {
2117+
let vec[@ast.item] items = vec();
2118+
auto index = new_str_hash[ast.mod_index_entry]();
2119+
auto view_items = parse_view(p, index);
2120+
2121+
for (@ast.crate_directive sub_cdir in cdirs) {
2122+
eval_crate_directive(p, sub_cdir, prefix,
2123+
view_items, items, index);
2124+
}
2125+
2126+
ret rec(view_items=view_items, items=items, index=index);
2127+
}
2128+
2129+
impure fn eval_crate_directive(parser p,
2130+
@ast.crate_directive cdir,
2131+
str prefix,
2132+
&mutable vec[@ast.view_item] view_items,
2133+
&mutable vec[@ast.item] items,
2134+
hashmap[ast.ident,ast.mod_index_entry] index) {
2135+
alt (cdir.node) {
2136+
case (ast.cdir_expr(?e)) {}
2137+
case (ast.cdir_const(?i)) {}
2138+
2139+
case (ast.cdir_src_mod(?id, ?file_opt)) {
2140+
2141+
auto file_path = id + ".rs";
2142+
alt (file_opt) {
2143+
case (some[filename](?f)) {
2144+
file_path = f;
2145+
}
2146+
case (none[filename]) {}
2147+
}
2148+
2149+
auto full_path = prefix + std.os.path_sep() + file_path;
2150+
2151+
auto p0 = new_parser(p.get_session(), 0, full_path);
2152+
auto m0 = parse_mod_items(p0, token.EOF);
2153+
auto im = ast.item_mod(id, m0, p.next_def_id());
2154+
auto i = @spanned(cdir.span, cdir.span, im);
2155+
ast.index_item(index, i);
2156+
append[@ast.item](items, i);
2157+
}
2158+
2159+
case (ast.cdir_dir_mod(?id, ?dir_opt, ?cdirs)) {
2160+
2161+
auto path = id;
2162+
alt (dir_opt) {
2163+
case (some[filename](?d)) {
2164+
path = d;
2165+
}
2166+
case (none[filename]) {}
2167+
}
2168+
2169+
auto full_path = prefix + std.os.path_sep() + path;
2170+
auto m0 = eval_crate_directives(p, cdirs, path);
2171+
auto im = ast.item_mod(id, m0, p.next_def_id());
2172+
auto i = @spanned(cdir.span, cdir.span, im);
2173+
ast.index_item(index, i);
2174+
append[@ast.item](items, i);
2175+
}
2176+
2177+
case (ast.cdir_view_item(?vi)) {
2178+
append[@ast.view_item](view_items, vi);
2179+
ast.index_view_item(index, vi);
2180+
}
2181+
2182+
case (ast.cdir_meta(?mi)) {}
2183+
case (ast.cdir_syntax(?pth)) {}
2184+
case (ast.cdir_auth(?pth, ?eff)) {}
2185+
}
2186+
}
2187+
2188+
impure fn parse_crate_directive(parser p) -> ast.crate_directive
21162189
{
21172190
auto lo = p.get_span();
21182191
auto hi = lo;
@@ -2124,28 +2197,27 @@ impure fn parse_crate_directive(str prefix, parser p,
21242197
auto n = parse_path(p, GREEDY);
21252198
expect(p, token.EQ);
21262199
auto e = parse_effect(p);
2200+
hi = p.get_span();
21272201
expect(p, token.SEMI);
2202+
ret spanned(lo, hi, ast.cdir_auth(n, e));
21282203
}
21292204
case (token.CONST) {
21302205
auto c = parse_item_const(p);
2131-
ast.index_item(index, c);
2132-
append[@ast.item](items, c);
2206+
ret spanned(c.span, c.span, ast.cdir_const(c));
21332207
}
21342208
case (token.MOD) {
21352209
p.bump();
21362210
auto id = parse_ident(p);
2137-
auto file_path = id;
2211+
auto file_opt = none[filename];
21382212
alt (p.peek()) {
21392213
case (token.EQ) {
21402214
p.bump();
21412215
// FIXME: turn this into parse+eval expr
2142-
file_path = parse_str_lit(p);
2216+
file_opt = some[filename](parse_str_lit(p));
21432217
}
21442218
case (_) {}
21452219
}
21462220

2147-
// dir-qualify file path.
2148-
auto full_path = prefix + std.os.path_sep() + file_path;
21492221

21502222
alt (p.peek()) {
21512223

@@ -2154,29 +2226,18 @@ impure fn parse_crate_directive(str prefix, parser p,
21542226
case (token.SEMI) {
21552227
hi = p.get_span();
21562228
p.bump();
2157-
if (!_str.ends_with(full_path, ".rs")) {
2158-
full_path += ".rs";
2159-
}
2160-
auto p0 = new_parser(p.get_session(), 0, full_path);
2161-
auto m0 = parse_mod_items(p0, token.EOF);
2162-
auto im = ast.item_mod(id, m0, p.next_def_id());
2163-
auto i = @spanned(lo, hi, im);
2164-
ast.index_item(index, i);
2165-
append[@ast.item](items, i);
2229+
ret spanned(lo, hi, ast.cdir_src_mod(id, file_opt));
21662230
}
21672231

21682232
// mod x = "foo_dir" { ...directives... }
21692233

21702234
case (token.LBRACE) {
21712235
p.bump();
2172-
auto m0 = parse_crate_directives(full_path, p,
2173-
token.RBRACE);
2236+
auto cdirs = parse_crate_directives(p, token.RBRACE);
21742237
hi = p.get_span();
21752238
expect(p, token.RBRACE);
2176-
auto im = ast.item_mod(id, m0, p.next_def_id());
2177-
auto i = @spanned(lo, hi, im);
2178-
ast.index_item(index, i);
2179-
append[@ast.item](items, i);
2239+
ret spanned(lo, hi,
2240+
ast.cdir_dir_mod(id, file_opt, cdirs));
21802241
}
21812242

21822243
case (?t) {
@@ -2185,27 +2246,28 @@ impure fn parse_crate_directive(str prefix, parser p,
21852246
}
21862247
}
21872248
}
2249+
fail;
21882250
}
21892251

2190-
impure fn parse_crate_directives(str prefix, parser p,
2191-
token.token term) -> ast._mod {
2192-
auto index = new_str_hash[ast.mod_index_entry]();
2193-
auto view_items = parse_view(p, index);
2252+
impure fn parse_crate_directives(parser p, token.token term)
2253+
-> vec[@ast.crate_directive] {
21942254

2195-
let vec[@ast.item] items = vec();
2255+
let vec[@ast.crate_directive] cdirs = vec();
21962256

21972257
while (p.peek() != term) {
2198-
parse_crate_directive(prefix, p, items, index);
2258+
auto cdir = @parse_crate_directive(p);
2259+
append[@ast.crate_directive](cdirs, cdir);
21992260
}
22002261

2201-
ret rec(view_items=view_items, items=items, index=index);
2262+
ret cdirs;
22022263
}
22032264

22042265
impure fn parse_crate_from_crate_file(parser p) -> @ast.crate {
22052266
auto lo = p.get_span();
22062267
auto hi = lo;
22072268
auto prefix = std.path.dirname(lo.filename);
2208-
auto m = parse_crate_directives(prefix, p, token.EOF);
2269+
auto cdirs = parse_crate_directives(p, token.EOF);
2270+
auto m = eval_crate_directives(p, cdirs, prefix);
22092271
hi = p.get_span();
22102272
expect(p, token.EOF);
22112273
ret @spanned(lo, hi, rec(module=m));

0 commit comments

Comments
 (0)