Skip to content

Commit b9af3dd

Browse files
committed
---
yaml --- r: 4324 b: refs/heads/master c: 5f4b7e1 h: refs/heads/master v: v3
1 parent 9decd5e commit b9af3dd

File tree

5 files changed

+67
-18
lines changed

5 files changed

+67
-18
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: 2d5b651f4981b85d0e3864d8df6bd0953578e1f4
2+
refs/heads/master: 5f4b7e1ba7121955ce4805cd55a51bc856d1f5b2

trunk/src/comp/driver/rustc.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,24 @@ fn parse_cfgspecs(cfgspecs: &vec[str]) -> ast::crate_cfg {
8686
ret words;
8787
}
8888

89+
fn input_is_stdin(filename: str) -> bool { filename == "-" }
90+
8991
fn parse_input(sess: session::session, cfg: &ast::crate_cfg, input: str) ->
9092
@ast::crate {
91-
ret if str::ends_with(input, ".rc") {
92-
parser::parse_crate_from_crate_file(input, cfg,
93-
sess.get_parse_sess())
94-
} else if (str::ends_with(input, ".rs")) {
95-
parser::parse_crate_from_source_file(input, cfg,
96-
sess.get_parse_sess())
97-
} else { sess.fatal("unknown input file type: " + input) };
93+
if !input_is_stdin(input) {
94+
parser::parse_crate_from_file(input, cfg, sess.get_parse_sess())
95+
} else {
96+
parse_input_src(sess, cfg, input).crate
97+
}
98+
}
99+
100+
fn parse_input_src(sess: session::session, cfg: &ast::crate_cfg,
101+
infile: str) -> {crate: @ast::crate, src: str} {
102+
let srcbytes = ioivec::stdin().read_whole_stream();
103+
let src = str::unsafe_from_bytes_ivec(srcbytes);
104+
let crate = parser::parse_crate_from_source_str(infile, src, cfg,
105+
sess.get_codemap());
106+
ret {crate: crate, src: src};
98107
}
99108

100109
fn time[T](do_it: bool, what: str, thunk: fn() -> T ) -> T {
@@ -195,7 +204,14 @@ fn pretty_print_input(sess: session::session, cfg: ast::crate_cfg, input: str,
195204
}
196205
}
197206

198-
let crate = parse_input(sess, cfg, input);
207+
// Because the pretty printer needs to make a pass over the source
208+
// to collect comments and literals, and we need to support reading
209+
// from stdin, we're going to just suck the source into a string
210+
// so both the parser and pretty-printer can use it.
211+
let crate_src = parse_input_src(sess, cfg, input);
212+
let crate = crate_src.crate;
213+
let src = crate_src.src;
214+
199215
if expand { crate = syntax::ext::expand::expand_crate(sess, crate); }
200216
let ann;
201217
alt ppm {
@@ -213,7 +229,7 @@ fn pretty_print_input(sess: session::session, cfg: ast::crate_cfg, input: str,
213229
ppm_normal. { ann = pprust::no_ann(); }
214230
}
215231
pprust::print_crate(sess.get_codemap(), crate, input,
216-
ioivec::file_reader(input), ioivec::stdout(), ann);
232+
ioivec::string_reader(src), ioivec::stdout(), ann);
217233
}
218234

219235
fn version(argv0: str) {
@@ -484,7 +500,13 @@ fn main(args: vec[str]) {
484500

485501
alt output_file {
486502
none. {
487-
let parts: vec[str] = str::split(ifile, '.' as u8);
503+
// "-" as input file will cause the parser to read from stdin so we
504+
// have to make up a name
505+
let parts: vec[str] = if !input_is_stdin(ifile) {
506+
str::split(ifile, '.' as u8)
507+
} else {
508+
["default", "rs"]
509+
};
488510
vec::pop[str](parts);
489511
saved_out_filename = parts.(0);
490512
alt sopts.output_type {

trunk/src/comp/syntax/codemap.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,19 @@ fn emit_diagnostic(sp: &option::t[span], msg: &str, kind: &str, color: u8,
9393
termivec::reset(ioivec::stdout().get_buf_writer());
9494
}
9595
ioivec::stdout().write_str(#fmt(" %s\n", msg));
96+
97+
maybe_highlight_lines(sp, cm, maybe_lines);
98+
}
99+
100+
fn maybe_highlight_lines(sp: &option::t[span], cm: &codemap,
101+
maybe_lines: option::t[@file_lines]) {
102+
96103
alt maybe_lines {
97104
some(lines) {
105+
// If we're not looking at a real file then we can't re-open it to
106+
// pull out the lines
107+
if lines.name == "-" { ret; }
108+
98109
// FIXME: reading in the entire file is the worst possible way to
99110
// get access to the necessary lines.
100111
let rdr = ioivec::file_reader(lines.name);

trunk/src/comp/syntax/parse/eval.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import syntax::parse::parser::parser;
99
import syntax::parse::parser::new_parser_from_file;
1010
import syntax::parse::parser::parse_inner_attrs_and_next;
1111
import syntax::parse::parser::parse_mod_items;
12+
import syntax::parse::parser::SOURCE_FILE;
1213

1314
export eval_crate_directives_to_mod;
1415
export mode_parse;
@@ -55,7 +56,7 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: str,
5556
if cx.mode == mode_depend { cx.deps += ~[full_path]; ret; }
5657
let p0 =
5758
new_parser_from_file(cx.sess, cx.cfg, full_path, cx.chpos,
58-
cx.byte_pos);
59+
cx.byte_pos, SOURCE_FILE);
5960
let inner_attrs = parse_inner_attrs_and_next(p0);
6061
let mod_attrs = attrs + inner_attrs.inner;
6162
let first_item_outer_attrs = inner_attrs.next;

trunk/src/comp/syntax/parse/parser.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ type parser =
5858
fn get_sess() -> parse_sess ;
5959
};
6060

61-
fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, path: str,
62-
chpos: uint, byte_pos: uint) -> parser {
63-
let ftype = SOURCE_FILE;
64-
if str::ends_with(path, ".rc") { ftype = CRATE_FILE; }
61+
fn new_parser_from_file(sess: parse_sess, cfg:
62+
ast::crate_cfg, path: str,
63+
chpos: uint, byte_pos: uint,
64+
ftype: file_type) -> parser {
6565
let srdr = ioivec::file_reader(path);
6666
let src = str::unsafe_from_bytes_ivec(srdr.read_whole_stream());
6767
let filemap = codemap::new_filemap(path, chpos, byte_pos);
@@ -2313,7 +2313,7 @@ fn parse_native_view(p: &parser) -> (@ast::view_item)[] {
23132313

23142314
fn parse_crate_from_source_file(input: &str, cfg: &ast::crate_cfg,
23152315
sess: &parse_sess) -> @ast::crate {
2316-
let p = new_parser_from_file(sess, cfg, input, 0u, 0u);
2316+
let p = new_parser_from_file(sess, cfg, input, 0u, 0u, SOURCE_FILE);
23172317
ret parse_crate_mod(p, cfg, sess);
23182318
}
23192319

@@ -2430,7 +2430,7 @@ fn parse_crate_directives(p: &parser, term: token::token,
24302430

24312431
fn parse_crate_from_crate_file(input: &str, cfg: &ast::crate_cfg,
24322432
sess: &parse_sess) -> @ast::crate {
2433-
let p = new_parser_from_file(sess, cfg, input, 0u, 0u);
2433+
let p = new_parser_from_file(sess, cfg, input, 0u, 0u, CRATE_FILE);
24342434
let lo = p.get_lo_pos();
24352435
let prefix = std::fs::dirname(p.get_filemap().name);
24362436
let leading_attrs = parse_inner_attrs_and_next(p);
@@ -2455,6 +2455,21 @@ fn parse_crate_from_crate_file(input: &str, cfg: &ast::crate_cfg,
24552455
attrs: crate_attrs,
24562456
config: p.get_cfg()});
24572457
}
2458+
2459+
fn parse_crate_from_file(input: &str, cfg: &ast::crate_cfg,
2460+
sess: &parse_sess) -> @ast::crate {
2461+
if str::ends_with(input, ".rc") {
2462+
parse_crate_from_crate_file(input, cfg, sess)
2463+
} else if str::ends_with(input, ".rs") {
2464+
parse_crate_from_source_file(input, cfg, sess)
2465+
} else {
2466+
codemap::emit_error(none,
2467+
"unknown input file type: " + input,
2468+
sess.cm);
2469+
fail
2470+
}
2471+
}
2472+
24582473
//
24592474
// Local Variables:
24602475
// mode: rust

0 commit comments

Comments
 (0)