diff options
| author | Graydon Hoare <graydon@mozilla.com> | 2012-08-24 15:28:43 -0700 |
|---|---|---|
| committer | Graydon Hoare <graydon@mozilla.com> | 2012-08-24 15:51:16 -0700 |
| commit | c284b8b1dc348ab8b9c82350dd1b4e53fac1225c (patch) | |
| tree | 99de39b149969275f6f9ddebd7a9f555d91c5bff /src/libsyntax | |
| parent | a8f1bee4574b8427a052e2fad93a90839288584b (diff) | |
| download | rust-c284b8b1dc348ab8b9c82350dd1b4e53fac1225c.tar.gz rust-c284b8b1dc348ab8b9c82350dd1b4e53fac1225c.zip | |
Start using core::path2::Path in a lot of places.
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ext/source_util.rs | 17 | ||||
| -rw-r--r-- | src/libsyntax/parse.rs | 25 | ||||
| -rw-r--r-- | src/libsyntax/parse/eval.rs | 55 |
3 files changed, 48 insertions, 49 deletions
diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index b74a1165b79..881691b5f5c 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -59,7 +59,7 @@ fn expand_include(cx: ext_ctxt, sp: span, arg: ast::mac_arg, let args = get_mac_args(cx, sp, arg, 1u, option::some(1u), ~"include"); let file = expr_to_str(cx, args[0], ~"#include_str requires a string"); let p = parse::new_parser_from_file(cx.parse_sess(), cx.cfg(), - res_rel_file(cx, sp, file), + &res_rel_file(cx, sp, &Path(file)), parse::parser::SOURCE_FILE); return p.parse_expr(); } @@ -70,7 +70,7 @@ fn expand_include_str(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg, let file = expr_to_str(cx, args[0], ~"#include_str requires a string"); - let res = io::read_whole_file_str(res_rel_file(cx, sp, file)); + let res = io::read_whole_file_str(&res_rel_file(cx, sp, &Path(file))); match res { result::ok(_) => { /* Continue. */ } result::err(e) => { @@ -87,7 +87,7 @@ fn expand_include_bin(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg, let file = expr_to_str(cx, args[0], ~"#include_bin requires a string"); - match io::read_whole_file(res_rel_file(cx, sp, file)) { + match io::read_whole_file(&res_rel_file(cx, sp, &Path(file))) { result::ok(src) => { let u8_exprs = vec::map(src, |char: u8| { mk_u8(cx, sp, char) @@ -100,14 +100,13 @@ fn expand_include_bin(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg, } } -fn res_rel_file(cx: ext_ctxt, sp: codemap::span, +arg: Path) -> Path { +fn res_rel_file(cx: ext_ctxt, sp: codemap::span, arg: &Path) -> Path { // NB: relative paths are resolved relative to the compilation unit - if !path::path_is_absolute(arg) { - let cu = codemap::span_to_filename(sp, cx.codemap()); - let dir = path::dirname(cu); - return path::connect(dir, arg); + if !arg.is_absolute { + let cu = Path(codemap::span_to_filename(sp, cx.codemap())); + cu.dir_path().push_many(arg.components) } else { - return arg; + copy *arg } } diff --git a/src/libsyntax/parse.rs b/src/libsyntax/parse.rs index bb21ba92679..b83c687b94b 100644 --- a/src/libsyntax/parse.rs +++ b/src/libsyntax/parse.rs @@ -49,33 +49,33 @@ fn new_parse_sess_special_handler(sh: span_handler, cm: codemap::codemap) mut chpos: 0u, mut byte_pos: 0u}; } -fn parse_crate_from_file(input: ~str, cfg: ast::crate_cfg, +fn parse_crate_from_file(input: &Path, cfg: ast::crate_cfg, sess: parse_sess) -> @ast::crate { - if str::ends_with(input, ~".rc") { + if input.filetype() == some(~"rc") { parse_crate_from_crate_file(input, cfg, sess) - } else if str::ends_with(input, ~".rs") { + } else if input.filetype() == some(~"rs") { parse_crate_from_source_file(input, cfg, sess) } else { sess.span_diagnostic.handler().fatal(~"unknown input file type: " + - input) + input.to_str()) } } -fn parse_crate_from_crate_file(input: ~str, cfg: ast::crate_cfg, +fn parse_crate_from_crate_file(input: &Path, cfg: ast::crate_cfg, sess: parse_sess) -> @ast::crate { let (p, rdr) = new_parser_etc_from_file(sess, cfg, input, parser::CRATE_FILE); let lo = p.span.lo; - let prefix = path::dirname(input); + let prefix = input.dir_path(); let leading_attrs = p.parse_inner_attrs_and_next(); let { inner: crate_attrs, next: first_cdir_attr } = leading_attrs; let cdirs = p.parse_crate_directives(token::EOF, first_cdir_attr); sess.chpos = rdr.chpos; sess.byte_pos = sess.byte_pos + rdr.pos; let cx = @{sess: sess, cfg: /* FIXME (#2543) */ copy p.cfg}; - let (companionmod, _) = path::splitext(path::basename(input)); + let companionmod = option::map(input.filestem(), |s| Path(s)); let (m, attrs) = eval::eval_crate_directives_to_mod( - cx, cdirs, prefix, option::some(companionmod)); + cx, cdirs, &prefix, &companionmod); let mut hi = p.span.hi; p.expect(token::EOF); return @ast_util::respan(ast_util::mk_sp(lo, hi), @@ -85,7 +85,7 @@ fn parse_crate_from_crate_file(input: ~str, cfg: ast::crate_cfg, config: /* FIXME (#2543) */ copy p.cfg}); } -fn parse_crate_from_source_file(input: ~str, cfg: ast::crate_cfg, +fn parse_crate_from_source_file(input: &Path, cfg: ast::crate_cfg, sess: parse_sess) -> @ast::crate { let (p, rdr) = new_parser_etc_from_file(sess, cfg, input, parser::SOURCE_FILE); @@ -183,7 +183,7 @@ fn new_parser_from_source_str(sess: parse_sess, cfg: ast::crate_cfg, fn new_parser_etc_from_file(sess: parse_sess, cfg: ast::crate_cfg, - +path: ~str, ftype: parser::file_type) -> + path: &Path, ftype: parser::file_type) -> (parser, string_reader) { let res = io::read_whole_file_str(path); match res { @@ -191,14 +191,15 @@ fn new_parser_etc_from_file(sess: parse_sess, cfg: ast::crate_cfg, result::err(e) => sess.span_diagnostic.handler().fatal(e) } let src = @result::unwrap(res); - let filemap = codemap::new_filemap(path, src, sess.chpos, sess.byte_pos); + let filemap = codemap::new_filemap(path.to_str(), src, + sess.chpos, sess.byte_pos); sess.cm.files.push(filemap); let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, sess.interner); return (parser(sess, cfg, srdr as reader, ftype), srdr); } -fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, +path: ~str, +fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, path: &Path, ftype: parser::file_type) -> parser { let (p, _) = new_parser_etc_from_file(sess, cfg, path, ftype); return p; diff --git a/src/libsyntax/parse/eval.rs b/src/libsyntax/parse/eval.rs index 615d546bb96..54ec79de4c1 100644 --- a/src/libsyntax/parse/eval.rs +++ b/src/libsyntax/parse/eval.rs @@ -9,7 +9,7 @@ type ctx = fn eval_crate_directives(cx: ctx, cdirs: ~[@ast::crate_directive], - prefix: ~str, + prefix: &Path, &view_items: ~[@ast::view_item], &items: ~[@ast::item]) { for cdirs.each |sub_cdir| { @@ -18,11 +18,8 @@ fn eval_crate_directives(cx: ctx, } fn eval_crate_directives_to_mod(cx: ctx, cdirs: ~[@ast::crate_directive], - prefix: ~str, suffix: option<~str>) + prefix: &Path, suffix: &option<Path>) -> (ast::_mod, ~[ast::attribute]) { - debug!("eval crate prefix: %s", prefix); - debug!("eval crate suffix: %s", - option::get_default(suffix, ~"none")); let (cview_items, citems, cattrs) = parse_companion_mod(cx, prefix, suffix); let mut view_items: ~[@ast::view_item] = ~[]; @@ -43,17 +40,17 @@ companion mod is a .rs file with the same name as the directory. We build the path to the companion mod by combining the prefix and the optional suffix then adding the .rs extension. */ -fn parse_companion_mod(cx: ctx, prefix: ~str, suffix: option<~str>) +fn parse_companion_mod(cx: ctx, prefix: &Path, suffix: &option<Path>) -> (~[@ast::view_item], ~[@ast::item], ~[ast::attribute]) { - fn companion_file(+prefix: ~str, suffix: option<~str>) -> ~str { - return match suffix { - option::some(s) => path::connect(prefix, s), - option::none => prefix - } + ~".rs"; + fn companion_file(prefix: &Path, suffix: &option<Path>) -> Path { + return match *suffix { + option::some(s) => prefix.push_many(s.components), + option::none => copy *prefix + }.with_filetype("rs"); } - fn file_exists(path: ~str) -> bool { + fn file_exists(path: &Path) -> bool { // Crude, but there's no lib function for this and I'm not // up to writing it just now match io::file_reader(path) { @@ -62,8 +59,7 @@ fn parse_companion_mod(cx: ctx, prefix: ~str, suffix: option<~str>) } } - let modpath = companion_file(prefix, suffix); - debug!("looking for companion mod %s", modpath); + let modpath = &companion_file(prefix, suffix); if file_exists(modpath) { debug!("found companion mod"); let (p0, r0) = new_parser_etc_from_file(cx.sess, cx.cfg, @@ -85,19 +81,21 @@ fn cdir_path_opt(default: ~str, attrs: ~[ast::attribute]) -> ~str { } } -fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: ~str, +fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: &Path, &view_items: ~[@ast::view_item], &items: ~[@ast::item]) { match cdir.node { ast::cdir_src_mod(id, attrs) => { - let file_path = cdir_path_opt((cx.sess.interner.get(id) + ~".rs"), - attrs); - let full_path = - if path::path_is_absolute(file_path) { - file_path - } else { prefix + path::path_sep() + file_path }; + let file_path = Path(cdir_path_opt( + cx.sess.interner.get(id) + ~".rs", attrs)); + let full_path = if file_path.is_absolute { + copy file_path + } else { + prefix.push_many(file_path.components) + }; let (p0, r0) = - new_parser_etc_from_file(cx.sess, cx.cfg, full_path, SOURCE_FILE); + new_parser_etc_from_file(cx.sess, cx.cfg, + &full_path, SOURCE_FILE); let inner_attrs = p0.parse_inner_attrs_and_next(); let mod_attrs = vec::append(attrs, inner_attrs.inner); let first_item_outer_attrs = inner_attrs.next; @@ -112,13 +110,14 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: ~str, vec::push(items, i); } ast::cdir_dir_mod(id, cdirs, attrs) => { - let path = cdir_path_opt(*cx.sess.interner.get(id), attrs); - let full_path = - if path::path_is_absolute(path) { - path - } else { prefix + path::path_sep() + path }; + let path = Path(cdir_path_opt(*cx.sess.interner.get(id), attrs)); + let full_path = if path.is_absolute { + copy path + } else { + prefix.push_many(path.components) + }; let (m0, a0) = eval_crate_directives_to_mod( - cx, cdirs, full_path, none); + cx, cdirs, &full_path, &none); let i = @{ident: /* FIXME (#2543) */ copy id, attrs: vec::append(attrs, a0), |
