@@ -8,6 +8,7 @@ import std.map.hashmap;
8
8
9
9
import driver. session ;
10
10
import util. common ;
11
+ import util. common . filename ;
11
12
import util. common . append ;
12
13
import util. common . span ;
13
14
import util. common . new_str_hash ;
@@ -2110,9 +2111,81 @@ impure fn parse_crate_from_source_file(parser p) -> @ast.crate {
2110
2111
//
2111
2112
// Each directive imperatively extends its environment with 0 or more items.
2112
2113
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
2116
2189
{
2117
2190
auto lo = p. get_span ( ) ;
2118
2191
auto hi = lo;
@@ -2124,28 +2197,27 @@ impure fn parse_crate_directive(str prefix, parser p,
2124
2197
auto n = parse_path ( p, GREEDY ) ;
2125
2198
expect ( p, token. EQ ) ;
2126
2199
auto e = parse_effect ( p) ;
2200
+ hi = p. get_span ( ) ;
2127
2201
expect ( p, token. SEMI ) ;
2202
+ ret spanned( lo, hi, ast. cdir_auth ( n, e) ) ;
2128
2203
}
2129
2204
case ( token. CONST ) {
2130
2205
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) ) ;
2133
2207
}
2134
2208
case ( token. MOD ) {
2135
2209
p. bump ( ) ;
2136
2210
auto id = parse_ident ( p) ;
2137
- auto file_path = id ;
2211
+ auto file_opt = none [ filename ] ;
2138
2212
alt ( p. peek ( ) ) {
2139
2213
case ( token. EQ ) {
2140
2214
p. bump ( ) ;
2141
2215
// FIXME: turn this into parse+eval expr
2142
- file_path = parse_str_lit ( p) ;
2216
+ file_opt = some [ filename ] ( parse_str_lit ( p) ) ;
2143
2217
}
2144
2218
case ( _) { }
2145
2219
}
2146
2220
2147
- // dir-qualify file path.
2148
- auto full_path = prefix + std. os . path_sep ( ) + file_path;
2149
2221
2150
2222
alt ( p. peek ( ) ) {
2151
2223
@@ -2154,29 +2226,18 @@ impure fn parse_crate_directive(str prefix, parser p,
2154
2226
case ( token. SEMI ) {
2155
2227
hi = p. get_span ( ) ;
2156
2228
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) ) ;
2166
2230
}
2167
2231
2168
2232
// mod x = "foo_dir" { ...directives... }
2169
2233
2170
2234
case ( token. LBRACE ) {
2171
2235
p. bump ( ) ;
2172
- auto m0 = parse_crate_directives ( full_path, p,
2173
- token. RBRACE ) ;
2236
+ auto cdirs = parse_crate_directives ( p, token. RBRACE ) ;
2174
2237
hi = p. get_span ( ) ;
2175
2238
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) ) ;
2180
2241
}
2181
2242
2182
2243
case ( ?t) {
@@ -2185,27 +2246,28 @@ impure fn parse_crate_directive(str prefix, parser p,
2185
2246
}
2186
2247
}
2187
2248
}
2249
+ fail;
2188
2250
}
2189
2251
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 ] {
2194
2254
2195
- let vec[ @ast. item ] items = vec ( ) ;
2255
+ let vec[ @ast. crate_directive ] cdirs = vec ( ) ;
2196
2256
2197
2257
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) ;
2199
2260
}
2200
2261
2201
- ret rec ( view_items=view_items , items=items , index=index ) ;
2262
+ ret cdirs ;
2202
2263
}
2203
2264
2204
2265
impure fn parse_crate_from_crate_file ( parser p) -> @ast. crate {
2205
2266
auto lo = p. get_span ( ) ;
2206
2267
auto hi = lo;
2207
2268
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) ;
2209
2271
hi = p. get_span ( ) ;
2210
2272
expect ( p, token. EOF ) ;
2211
2273
ret @spanned ( lo, hi, rec ( module=m) ) ;
0 commit comments