Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relate the module hierarchy to directory paths in the parser #4163

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
@@ -203,6 +203,7 @@ fn Parser(sess: parse_sess, cfg: ast::crate_cfg,
strict_keywords: token::strict_keyword_table(),
reserved_keywords: token::reserved_keyword_table(),
obsolete_set: std::map::HashMap(),
mod_path_stack: ~[],
}
}

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

drop {} /* do not copy the parser; its state is tied to outside state */
}
@@ -3041,10 +3044,12 @@ impl Parser {
let (m, attrs) = self.eval_src_mod(id, outer_attrs, id_span);
(id, m, Some(move attrs))
} else {
self.push_mod_path(id, outer_attrs);
self.expect(token::LBRACE);
let inner_attrs = self.parse_inner_attrs_and_next();
let m = self.parse_mod_items(token::RBRACE, inner_attrs.next);
self.expect(token::RBRACE);
self.pop_mod_path();
(id, item_mod(m), Some(inner_attrs.inner))
};

@@ -3081,20 +3086,40 @@ impl Parser {
}
}

fn push_mod_path(id: ident, attrs: ~[ast::attribute]) {
let default_path = self.sess.interner.get(id);
let file_path = match ::attr::first_attr_value_str_by_name(
attrs, ~"path2") {

Some(ref d) => (*d),
None => copy *default_path
};
self.mod_path_stack.push(file_path)
}

fn pop_mod_path() {
self.mod_path_stack.pop();
}

fn eval_src_mod(id: ast::ident,
outer_attrs: ~[ast::attribute],
id_sp: span) -> (ast::item_, ~[ast::attribute]) {

let prefix = Path(self.sess.cm.span_to_filename(copy self.span));
let prefix = prefix.dir_path();
let mod_path = Path(".").push_many(self.mod_path_stack);
let default_path = self.sess.interner.get(id) + ~".rs";
let file_path = match ::attr::first_attr_value_str_by_name(
outer_attrs, ~"path") {
outer_attrs, ~"path2") {

Some(ref d) => (*d),
None => default_path
Some(ref d) => mod_path.push(*d),
None => match ::attr::first_attr_value_str_by_name(
outer_attrs, ~"path") {
Some(ref d) => Path(*d),
None => mod_path.push(default_path)
}
};

let file_path = Path(file_path);
self.eval_src_mod_from_path(prefix, file_path,
outer_attrs, id_sp)
}
21 changes: 21 additions & 0 deletions src/test/run-pass/mod_dir_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// xfail-pretty
// xfail-fast

mod mod_dir_simple {
#[path2 = "test.rs"]
pub mod syrup;
}

fn main() {
assert mod_dir_simple::syrup::foo() == 10;
}
22 changes: 22 additions & 0 deletions src/test/run-pass/mod_dir_path2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// xfail-pretty
// xfail-fast

#[path2 = "mod_dir_simple"]
mod pancakes {
#[path2 = "test.rs"]
pub mod syrup;
}

fn main() {
assert pancakes::syrup::foo() == 10;
}
21 changes: 21 additions & 0 deletions src/test/run-pass/mod_dir_path3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// xfail-pretty
// xfail-fast

#[path2 = "mod_dir_simple"]
mod pancakes {
pub mod test;
}

fn main() {
assert pancakes::test::foo() == 10;
}
27 changes: 27 additions & 0 deletions src/test/run-pass/mod_dir_path_multi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// xfail-pretty
// xfail-fast

#[path2 = "mod_dir_simple"]
mod biscuits {
pub mod test;
}

#[path2 = "mod_dir_simple"]
mod gravy {
pub mod test;
}

fn main() {
assert biscuits::test::foo() == 10;
assert gravy::test::foo() == 10;
}
20 changes: 20 additions & 0 deletions src/test/run-pass/mod_dir_simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// xfail-pretty
// xfail-fast

mod mod_dir_simple {
pub mod test;
}

fn main() {
assert mod_dir_simple::test::foo() == 10;
}
11 changes: 11 additions & 0 deletions src/test/run-pass/mod_dir_simple/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

pub fn foo() -> int { 10 }