Skip to content

Commit aed40fb

Browse files
marijnhunknown
authored and
unknown
committed
Have the pretty-printer take a writer stream as argument
It now uses a string writer to also fill in for middle.ty.ast_ty_to_str
1 parent dddd7d8 commit aed40fb

File tree

6 files changed

+61
-119
lines changed

6 files changed

+61
-119
lines changed

src/comp/driver/rustc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impure fn pretty_print_input(session.session sess,
6666
str input) {
6767
auto p = front.parser.new_parser(sess, env, 0, input);
6868
auto crate = front.parser.parse_crate_from_source_file(p);
69-
pretty.pprust.print_ast(crate.node.module);
69+
pretty.pprust.print_ast(crate.node.module, std.io.stdout_writer());
7070
}
7171

7272
fn warn_wrong_compiler() {

src/comp/middle/ty.rs

+1-80
Original file line numberDiff line numberDiff line change
@@ -81,96 +81,17 @@ tag unify_result {
8181

8282
// Stringification
8383

84-
fn ast_ty_to_str(&@ast.ty ty) -> str {
85-
86-
fn ast_fn_input_to_str(&rec(ast.mode mode, @ast.ty ty) input) -> str {
87-
auto s;
88-
if (mode_is_alias(input.mode)) {
89-
s = "&";
90-
} else {
91-
s = "";
92-
}
93-
94-
ret s + ast_ty_to_str(input.ty);
95-
}
96-
97-
fn ast_ty_field_to_str(&ast.ty_field f) -> str {
98-
ret ast_ty_to_str(f.ty) + " " + f.ident;
99-
}
100-
101-
auto s;
102-
alt (ty.node) {
103-
case (ast.ty_nil) { s = "()"; }
104-
case (ast.ty_bool) { s = "bool"; }
105-
case (ast.ty_int) { s = "int"; }
106-
case (ast.ty_uint) { s = "uint"; }
107-
case (ast.ty_machine(?tm)) { s = common.ty_mach_to_str(tm); }
108-
case (ast.ty_char) { s = "char"; }
109-
case (ast.ty_str) { s = "str"; }
110-
case (ast.ty_box(?t)) { s = "@" + ast_ty_to_str(t); }
111-
case (ast.ty_vec(?t)) { s = "vec[" + ast_ty_to_str(t) + "]"; }
112-
case (ast.ty_type) { s = "type"; }
113-
114-
case (ast.ty_tup(?elts)) {
115-
auto f = ast_ty_to_str;
116-
s = "tup(";
117-
s += _str.connect(_vec.map[@ast.ty,str](f, elts), ",");
118-
s += ")";
119-
}
120-
121-
case (ast.ty_rec(?fields)) {
122-
auto f = ast_ty_field_to_str;
123-
s = "rec(";
124-
s += _str.connect(_vec.map[ast.ty_field,str](f, fields), ",");
125-
s += ")";
126-
}
127-
128-
case (ast.ty_fn(?proto, ?inputs, ?output)) {
129-
auto f = ast_fn_input_to_str;
130-
if (proto == ast.proto_fn) {
131-
s = "fn(";
132-
} else {
133-
s = "iter(";
134-
}
135-
auto is = _vec.map[rec(ast.mode mode, @ast.ty ty),str](f, inputs);
136-
s += _str.connect(is, ", ");
137-
s += ")";
138-
139-
if (output.node != ast.ty_nil) {
140-
s += " -> " + ast_ty_to_str(output);
141-
}
142-
}
143-
144-
case (ast.ty_path(?path, _)) {
145-
s = path_to_str(path);
146-
}
147-
148-
case (ast.ty_mutable(?t)) {
149-
s = "mutable " + ast_ty_to_str(t);
150-
}
151-
152-
153-
case (_) {
154-
fail; // FIXME: typestate bug
155-
}
156-
}
157-
158-
ret s;
159-
}
160-
16184
fn path_to_str(&ast.path pth) -> str {
16285
auto result = _str.connect(pth.node.idents, ".");
16386
if (_vec.len[@ast.ty](pth.node.types) > 0u) {
164-
auto f = ast_ty_to_str;
87+
auto f = pretty.pprust.ty_to_str;
16588
result += "[";
16689
result += _str.connect(_vec.map[@ast.ty,str](f, pth.node.types), ",");
16790
result += "]";
16891
}
16992
ret result;
17093
}
17194

172-
// FIXME use the pretty-printer for this once it has a concept of an
173-
// abstract stream
17495
fn ty_to_str(&@t typ) -> str {
17596

17697
fn fn_input_to_str(&rec(ast.mode mode, @t ty) input) -> str {

src/comp/pretty/pp.rs

+28-15
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@ type context = rec(contexttype tp, uint indent);
1717

1818
type ps = @rec(mutable vec[context] context,
1919
uint width,
20+
io.writer out,
2021
mutable vec[token] buffered,
2122
mutable uint scandepth,
2223
mutable uint bufferedcol,
2324
mutable uint col,
2425
mutable bool start_of_line);
2526

26-
fn mkstate(uint width) -> ps {
27+
fn mkstate(io.writer out, uint width) -> ps {
2728
let vec[context] stack = vec(rec(tp=cx_v, indent=0u));
2829
let vec[token] buff = vec();
2930
ret @rec(mutable context=stack,
3031
width=width,
32+
out=out,
3133
mutable buffered=buff,
3234
mutable scandepth=0u,
3335
mutable bufferedcol=0u,
@@ -46,10 +48,22 @@ impure fn pop_context(ps p) {
4648
}
4749

4850
impure fn add_token(ps p, token tok) {
49-
if (p.scandepth == 0u) {do_token(p, tok);}
51+
if (p.width == 0u) {direct_token(p, tok);}
52+
else if (p.scandepth == 0u) {do_token(p, tok);}
5053
else {buffer_token(p, tok);}
5154
}
5255

56+
impure fn direct_token(ps p, token tok) {
57+
alt (tok) {
58+
case (brk(?sz)) {
59+
while (sz > 0u) {p.out.write_str(" "); sz -= 1u;}
60+
}
61+
case (word(?w)) {p.out.write_str(w);}
62+
case (cword(?w)) {p.out.write_str(w);}
63+
case (_) {}
64+
}
65+
}
66+
5367
impure fn buffer_token(ps p, token tok) {
5468
p.buffered += vec(tok);
5569
p.bufferedcol += token_size(tok);
@@ -101,14 +115,13 @@ impure fn finish_block_scan(ps p, contexttype tp) {
101115

102116
impure fn finish_break_scan(ps p) {
103117
if (p.bufferedcol > p.width) {
104-
write_str("\n");
105-
p.col = 0u;
118+
line_break(p);
106119
}
107120
else {
108121
auto width;
109122
alt (p.buffered.(0)) {case(brk(?w)) {width = w;}}
110123
auto i = 0u;
111-
while (i < width) {write_str(" "); i+=1u;}
124+
while (i < width) {p.out.write_str(" "); i+=1u;}
112125
p.col += width;
113126
}
114127
p.scandepth = 0u;
@@ -142,20 +155,18 @@ impure fn do_token(ps p, token tok) {
142155
start_scan(p, tok);
143156
}
144157
case (cx_v) {
145-
write_str("\n");
146-
p.col = 0u;
147-
p.start_of_line = true;
158+
line_break(p);
148159
}
149160
}
150161
}
151162
case (word(?w)) {
152163
before_print(p, false);
153-
write_str(w);
164+
p.out.write_str(w);
154165
p.col += _str.byte_len(w); // TODO char_len
155166
}
156167
case (cword(?w)) {
157168
before_print(p, true);
158-
write_str(w);
169+
p.out.write_str(w);
159170
p.col += _str.byte_len(w); // TODO char_len
160171
}
161172
case (open(?tp, ?indent)) {
@@ -170,21 +181,23 @@ impure fn do_token(ps p, token tok) {
170181
}
171182
}
172183

184+
impure fn line_break(ps p) {
185+
p.out.write_str("\n");
186+
p.col = 0u;
187+
p.start_of_line = true;
188+
}
189+
173190
impure fn before_print(ps p, bool closing) {
174191
if (p.start_of_line) {
175192
p.start_of_line = false;
176193
auto ind;
177194
if (closing) {ind = base_indent(p);}
178195
else {ind = cur_context(p).indent;}
179196
p.col = ind;
180-
while (ind > 0u) {write_str(" "); ind -= 1u;}
197+
while (ind > 0u) {p.out.write_str(" "); ind -= 1u;}
181198
}
182199
}
183200

184-
fn write_str(str s) {
185-
io.writefd(1, _str.bytes(s));
186-
}
187-
188201
fn token_size(token tok) -> uint {
189202
alt (tok) {
190203
case (brk(?sz)) {ret sz;}

src/comp/pretty/pprust.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@ import foo = std.io;
1111
const uint indent_unit = 2u;
1212
const int as_prec = 5;
1313

14-
impure fn print_ast(ast._mod _mod) {
15-
auto s = pp.mkstate(80u);
14+
impure fn print_ast(ast._mod _mod, std.io.writer out) {
15+
auto s = pp.mkstate(out, 80u);
1616
for (@ast.view_item vitem in _mod.view_items) {print_view_item(s, vitem);}
1717
line(s);
1818
for (@ast.item item in _mod.items) {print_item(s, item);}
1919
}
2020

21+
fn ty_to_str(&@ast.ty ty) -> str {
22+
auto writer = std.io.string_writer();
23+
print_type(pp.mkstate(writer.get_writer(), 0u), ty);
24+
ret writer.get_str();
25+
}
26+
2127
impure fn hbox(ps s) {
2228
pp.hbox(s, indent_unit);
2329
}

src/comp/rustc.rc

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ auth middle.trans = unsafe;
4444
auth middle.trans.copy_args_to_allocas = impure;
4545
auth middle.trans.trans_block = impure;
4646
auth lib.llvm = unsafe;
47+
auth pretty.pprust = impure;
4748

4849
mod lib {
4950
alt (target_os) {

src/lib/io.rs

+22-21
Original file line numberDiff line numberDiff line change
@@ -91,26 +91,21 @@ tag fileflag {
9191
truncate;
9292
}
9393

94-
// FIXME move into fd_buf_writer
95-
fn writefd(int fd, vec[u8] v) {
96-
auto len = _vec.len[u8](v);
97-
auto count = 0u;
98-
auto vbuf;
99-
while (count < len) {
100-
vbuf = _vec.buf_off[u8](v, count);
101-
auto nout = os.libc.write(fd, vbuf, len);
102-
if (nout < 0) {
103-
log "error dumping buffer";
104-
log sys.rustrt.last_os_error();
105-
fail;
106-
}
107-
count += nout as uint;
108-
}
109-
}
110-
11194
state obj fd_buf_writer(int fd, bool must_close) {
11295
fn write(vec[u8] v) {
113-
writefd(fd, v);
96+
auto len = _vec.len[u8](v);
97+
auto count = 0u;
98+
auto vbuf;
99+
while (count < len) {
100+
vbuf = _vec.buf_off[u8](v, count);
101+
auto nout = os.libc.write(fd, vbuf, len);
102+
if (nout < 0) {
103+
log "error dumping buffer";
104+
log sys.rustrt.last_os_error();
105+
fail;
106+
}
107+
count += nout as uint;
108+
}
114109
}
115110

116111
drop {
@@ -152,9 +147,15 @@ type writer =
152147
};
153148

154149
state obj new_writer(buf_writer out) {
155-
impure fn write_str(str s) { out.write(_str.bytes(s)); }
156-
impure fn write_int(int n) { out.write(_str.bytes(_int.to_str(n, 10u))); }
157-
impure fn write_uint(uint n) { out.write(_str.bytes(_uint.to_str(n, 10u))); }
150+
impure fn write_str(str s) {
151+
out.write(_str.bytes(s));
152+
}
153+
impure fn write_int(int n) {
154+
out.write(_str.bytes(_int.to_str(n, 10u)));
155+
}
156+
impure fn write_uint(uint n) {
157+
out.write(_str.bytes(_uint.to_str(n, 10u)));
158+
}
158159
}
159160

160161
fn file_writer(str path, vec[fileflag] flags) -> writer {

0 commit comments

Comments
 (0)