Skip to content

Commit 6e38e33

Browse files
committed
Relate the module hierarchy to directory paths in the parser
Introduces a temporary 'path2' attribute that will replace 'path' after a snapshot
1 parent 364f9af commit 6e38e33

File tree

9 files changed

+186
-4
lines changed

9 files changed

+186
-4
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ fn Parser(sess: parse_sess, cfg: ast::crate_cfg,
203203
strict_keywords: token::strict_keyword_table(),
204204
reserved_keywords: token::reserved_keyword_table(),
205205
obsolete_set: std::map::HashMap(),
206+
mod_path_stack: ~[],
206207
}
207208
}
208209

@@ -226,6 +227,8 @@ struct Parser {
226227
/// The set of seen errors about obsolete syntax. Used to suppress
227228
/// extra detail when the same error is seen twice
228229
obsolete_set: HashMap<ObsoleteSyntax, ()>,
230+
/// Used to determine the path to externally loaded source files
231+
mut mod_path_stack: ~[~str],
229232

230233
drop {} /* do not copy the parser; its state is tied to outside state */
231234
}
@@ -3041,10 +3044,12 @@ impl Parser {
30413044
let (m, attrs) = self.eval_src_mod(id, outer_attrs, id_span);
30423045
(id, m, Some(move attrs))
30433046
} else {
3047+
self.push_mod_path(id, outer_attrs);
30443048
self.expect(token::LBRACE);
30453049
let inner_attrs = self.parse_inner_attrs_and_next();
30463050
let m = self.parse_mod_items(token::RBRACE, inner_attrs.next);
30473051
self.expect(token::RBRACE);
3052+
self.pop_mod_path();
30483053
(id, item_mod(m), Some(inner_attrs.inner))
30493054
};
30503055
@@ -3081,20 +3086,40 @@ impl Parser {
30813086
}
30823087
}
30833088
3089+
fn push_mod_path(id: ident, attrs: ~[ast::attribute]) {
3090+
let default_path = self.sess.interner.get(id);
3091+
let file_path = match ::attr::first_attr_value_str_by_name(
3092+
attrs, ~"path2") {
3093+
3094+
Some(ref d) => (*d),
3095+
None => copy *default_path
3096+
};
3097+
self.mod_path_stack.push(file_path)
3098+
}
3099+
3100+
fn pop_mod_path() {
3101+
self.mod_path_stack.pop();
3102+
}
3103+
30843104
fn eval_src_mod(id: ast::ident,
30853105
outer_attrs: ~[ast::attribute],
30863106
id_sp: span) -> (ast::item_, ~[ast::attribute]) {
3107+
30873108
let prefix = Path(self.sess.cm.span_to_filename(copy self.span));
30883109
let prefix = prefix.dir_path();
3110+
let mod_path = Path(".").push_many(self.mod_path_stack);
30893111
let default_path = self.sess.interner.get(id) + ~".rs";
30903112
let file_path = match ::attr::first_attr_value_str_by_name(
3091-
outer_attrs, ~"path") {
3113+
outer_attrs, ~"path2") {
30923114
3093-
Some(ref d) => (*d),
3094-
None => default_path
3115+
Some(ref d) => mod_path.push(*d),
3116+
None => match ::attr::first_attr_value_str_by_name(
3117+
outer_attrs, ~"path") {
3118+
Some(ref d) => Path(*d),
3119+
None => mod_path.push(default_path)
3120+
}
30953121
};
30963122
3097-
let file_path = Path(file_path);
30983123
self.eval_src_mod_from_path(prefix, file_path,
30993124
outer_attrs, id_sp)
31003125
}

src/test/run-pass/mod_dir_path.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-pretty
12+
// xfail-fast
13+
14+
mod mod_dir_simple {
15+
#[path2 = "test.rs"]
16+
pub mod syrup;
17+
}
18+
19+
fn main() {
20+
assert mod_dir_simple::syrup::foo() == 10;
21+
}

src/test/run-pass/mod_dir_path2.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-pretty
12+
// xfail-fast
13+
14+
#[path2 = "mod_dir_simple"]
15+
mod pancakes {
16+
#[path2 = "test.rs"]
17+
pub mod syrup;
18+
}
19+
20+
fn main() {
21+
assert pancakes::syrup::foo() == 10;
22+
}

src/test/run-pass/mod_dir_path3.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-pretty
12+
// xfail-fast
13+
14+
#[path2 = "mod_dir_simple"]
15+
mod pancakes {
16+
pub mod test;
17+
}
18+
19+
fn main() {
20+
assert pancakes::test::foo() == 10;
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-pretty
12+
// xfail-fast
13+
14+
#[path2 = "mod_dir_simple"]
15+
mod biscuits {
16+
pub mod test;
17+
}
18+
19+
#[path2 = "mod_dir_simple"]
20+
mod gravy {
21+
pub mod test;
22+
}
23+
24+
fn main() {
25+
assert biscuits::test::foo() == 10;
26+
assert gravy::test::foo() == 10;
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-pretty
12+
// xfail-fast
13+
14+
// Testing that the parser for each file tracks its modules
15+
// and paths independently. The load_another_mod module should
16+
// not try to reuse the 'mod_dir_simple' path.
17+
18+
mod mod_dir_simple {
19+
pub mod load_another_mod;
20+
}
21+
22+
fn main() {
23+
assert mod_dir_simple::load_another_mod::test::foo() == 10;
24+
}

src/test/run-pass/mod_dir_simple.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-pretty
12+
// xfail-fast
13+
14+
mod mod_dir_simple {
15+
pub mod test;
16+
}
17+
18+
fn main() {
19+
assert mod_dir_simple::test::foo() == 10;
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
mod test;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn foo() -> int { 10 }

0 commit comments

Comments
 (0)