2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-05 16:23:07 -07:00
|
|
|
import std::ivec;
|
2011-07-06 11:26:26 -07:00
|
|
|
import std::int;
|
|
|
|
import std::io;
|
2011-07-05 16:23:07 -07:00
|
|
|
import std::str;
|
|
|
|
import std::uint;
|
|
|
|
import std::vec;
|
2011-05-12 17:24:54 +02:00
|
|
|
import std::option;
|
2011-07-05 11:48:19 +02:00
|
|
|
import parse::lexer;
|
|
|
|
import syntax::codemap::codemap;
|
|
|
|
import ast;
|
2011-06-02 17:18:43 -07:00
|
|
|
import option::some;
|
|
|
|
import option::none;
|
2011-05-28 19:16:18 -07:00
|
|
|
import pp::printer;
|
|
|
|
import pp::break_offset;
|
2011-05-30 14:10:54 -07:00
|
|
|
import pp::word;
|
2011-05-30 17:34:59 -07:00
|
|
|
import pp::huge_word;
|
2011-06-01 10:54:11 -07:00
|
|
|
import pp::zero_word;
|
2011-05-28 19:16:18 -07:00
|
|
|
import pp::space;
|
2011-05-30 14:10:54 -07:00
|
|
|
import pp::zerobreak;
|
2011-05-28 19:16:18 -07:00
|
|
|
import pp::hardbreak;
|
2011-05-29 07:43:33 +00:00
|
|
|
import pp::breaks;
|
|
|
|
import pp::consistent;
|
|
|
|
import pp::inconsistent;
|
2011-05-28 19:16:18 -07:00
|
|
|
import pp::eof;
|
2011-05-31 10:58:30 -07:00
|
|
|
|
2011-07-05 11:48:19 +02:00
|
|
|
// The ps is stored here to prevent recursive type.
|
|
|
|
// FIXME use a nominal tag instead
|
|
|
|
tag ann_node {
|
|
|
|
node_block(ps, ast::block);
|
|
|
|
node_item(ps, @ast::item);
|
|
|
|
node_expr(ps, @ast::expr);
|
|
|
|
node_pat(ps, @ast::pat);
|
|
|
|
}
|
|
|
|
type pp_ann = rec(fn(&ann_node node) pre,
|
|
|
|
fn(&ann_node node) post);
|
|
|
|
|
|
|
|
fn no_ann() -> pp_ann {
|
|
|
|
fn ignore(&ann_node node) {}
|
|
|
|
ret rec(pre=ignore, post=ignore);
|
|
|
|
}
|
|
|
|
|
|
|
|
type ps =
|
|
|
|
@rec(pp::printer s,
|
|
|
|
option::t[codemap] cm,
|
|
|
|
option::t[vec[lexer::cmnt]] comments,
|
|
|
|
option::t[vec[lexer::lit]] literals,
|
|
|
|
mutable uint cur_cmnt,
|
|
|
|
mutable uint cur_lit,
|
|
|
|
mutable vec[pp::breaks] boxes,
|
|
|
|
pp_ann ann);
|
|
|
|
|
|
|
|
fn ibox(&ps s, uint u) {
|
|
|
|
vec::push(s.boxes, pp::inconsistent);
|
|
|
|
pp::ibox(s.s, u);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(&ps s) { vec::pop(s.boxes); pp::end(s.s); }
|
|
|
|
|
|
|
|
fn rust_printer(io::writer writer) -> ps {
|
|
|
|
let vec[pp::breaks] boxes = [];
|
|
|
|
ret @rec(s=pp::mk_printer(writer, default_columns),
|
|
|
|
cm=none[codemap],
|
|
|
|
comments=none[vec[lexer::cmnt]],
|
|
|
|
literals=none[vec[lexer::lit]],
|
|
|
|
mutable cur_cmnt=0u,
|
|
|
|
mutable cur_lit=0u,
|
|
|
|
mutable boxes=boxes,
|
|
|
|
ann=no_ann());
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint indent_unit = 4u;
|
|
|
|
|
|
|
|
const uint default_columns = 78u;
|
|
|
|
|
|
|
|
fn print_crate(&codemap cm, @ast::crate crate, str filename,
|
2011-07-05 10:38:35 +02:00
|
|
|
io::writer out, &pp_ann ann) {
|
2011-06-01 15:29:38 -07:00
|
|
|
let vec[pp::breaks] boxes = [];
|
2011-07-05 11:48:19 +02:00
|
|
|
auto r = lexer::gather_comments_and_literals(cm, filename);
|
2011-06-15 11:19:50 -07:00
|
|
|
auto s =
|
|
|
|
@rec(s=pp::mk_printer(out, default_columns),
|
2011-07-05 11:48:19 +02:00
|
|
|
cm=some(cm),
|
2011-06-15 11:19:50 -07:00
|
|
|
comments=some(r.cmnts),
|
|
|
|
literals=some(r.lits),
|
|
|
|
mutable cur_cmnt=0u,
|
|
|
|
mutable cur_lit=0u,
|
|
|
|
mutable boxes=boxes,
|
2011-07-05 10:38:35 +02:00
|
|
|
ann=ann);
|
2011-06-30 16:03:07 -07:00
|
|
|
print_mod(s, crate.node.module, crate.node.attrs);
|
2011-07-05 14:34:34 -07:00
|
|
|
print_remaining_comments(s);
|
2011-05-28 19:16:18 -07:00
|
|
|
eof(s.s);
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
|
|
|
|
2011-05-31 10:58:30 -07:00
|
|
|
fn ty_to_str(&ast::ty ty) -> str { be to_str(ty, print_type); }
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-01 17:47:59 -07:00
|
|
|
fn pat_to_str(&@ast::pat pat) -> str { be to_str(pat, print_pat); }
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-01 17:47:59 -07:00
|
|
|
fn expr_to_str(&@ast::expr e) -> str { be to_str(e, print_expr); }
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-01 17:47:59 -07:00
|
|
|
fn stmt_to_str(&ast::stmt s) -> str { be to_str(s, print_stmt); }
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-01 17:47:59 -07:00
|
|
|
fn item_to_str(&@ast::item i) -> str { be to_str(i, print_item); }
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-01 17:47:59 -07:00
|
|
|
fn path_to_str(&ast::path p) -> str { be to_str(p, print_path); }
|
2011-05-31 10:58:30 -07:00
|
|
|
|
|
|
|
fn fun_to_str(&ast::_fn f, str name, vec[ast::ty_param] params) -> str {
|
2011-05-12 17:24:54 +02:00
|
|
|
auto writer = io::string_writer();
|
2011-05-31 10:58:30 -07:00
|
|
|
auto s = rust_printer(writer.get_writer());
|
2011-06-01 13:18:16 -07:00
|
|
|
print_fn(s, f.decl, f.proto, name, params);
|
2011-05-28 19:16:18 -07:00
|
|
|
eof(s.s);
|
2011-03-24 16:33:20 +01:00
|
|
|
ret writer.get_str();
|
2011-03-09 11:41:50 +01:00
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn block_to_str(&ast::block blk) -> str {
|
|
|
|
auto writer = io::string_writer();
|
2011-05-31 10:58:30 -07:00
|
|
|
auto s = rust_printer(writer.get_writer());
|
2011-05-31 09:54:18 -07:00
|
|
|
// containing cbox, will be closed by print-block at }
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
cbox(s, indent_unit);
|
2011-05-31 09:54:18 -07:00
|
|
|
// head-ibox, will be closed by print-block after {
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-01 15:29:38 -07:00
|
|
|
ibox(s, 0u);
|
2011-03-31 20:29:59 -04:00
|
|
|
print_block(s, blk);
|
2011-05-28 19:16:18 -07:00
|
|
|
eof(s.s);
|
2011-03-31 20:29:59 -04:00
|
|
|
ret writer.get_str();
|
|
|
|
}
|
|
|
|
|
2011-06-27 18:32:15 -07:00
|
|
|
fn meta_item_to_str(&ast::meta_item mi) -> str {
|
|
|
|
ret to_str(@mi, print_meta_item);
|
|
|
|
}
|
|
|
|
|
2011-06-27 19:41:48 -07:00
|
|
|
fn attribute_to_str(&ast::attribute attr) -> str {
|
|
|
|
be to_str(attr, print_attribute);
|
|
|
|
}
|
|
|
|
|
2011-06-01 15:29:38 -07:00
|
|
|
fn cbox(&ps s, uint u) {
|
|
|
|
vec::push(s.boxes, pp::consistent);
|
|
|
|
pp::cbox(s.s, u);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn box(&ps s, uint u, pp::breaks b) {
|
|
|
|
vec::push(s.boxes, b);
|
|
|
|
pp::box(s.s, u, b);
|
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
fn word_nbsp(&ps s, str w) { word(s.s, w); word(s.s, " "); }
|
2011-05-28 19:16:18 -07:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
fn word_space(&ps s, str w) { word(s.s, w); space(s.s); }
|
2011-05-28 19:16:18 -07:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
fn popen(&ps s) { word(s.s, "("); }
|
2011-05-28 19:16:18 -07:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
fn pclose(&ps s) { word(s.s, ")"); }
|
2011-05-28 19:16:18 -07:00
|
|
|
|
2011-05-31 10:58:30 -07:00
|
|
|
fn head(&ps s, str w) {
|
2011-05-28 19:16:18 -07:00
|
|
|
// outer-box is consistent
|
2011-06-01 15:29:38 -07:00
|
|
|
cbox(s, indent_unit);
|
2011-05-28 19:16:18 -07:00
|
|
|
// head-box is inconsistent
|
2011-06-01 15:29:38 -07:00
|
|
|
ibox(s, str::char_len(w) + 1u);
|
2011-05-28 19:16:18 -07:00
|
|
|
// keyword that starts the head
|
2011-05-30 14:10:54 -07:00
|
|
|
word_nbsp(s, w);
|
2011-05-28 19:16:18 -07:00
|
|
|
}
|
|
|
|
|
2011-05-31 10:58:30 -07:00
|
|
|
fn bopen(&ps s) {
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, "{");
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s); // close the head-box
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-05-28 19:16:18 -07:00
|
|
|
|
2011-07-05 11:48:19 +02:00
|
|
|
fn bclose(&ps s, codemap::span span) {
|
2011-03-24 16:33:20 +01:00
|
|
|
maybe_print_comment(s, span.hi);
|
2011-05-28 19:16:18 -07:00
|
|
|
break_offset(s.s, 1u, -(indent_unit as int));
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, "}");
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s); // close the outer-box
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
2011-05-28 19:16:18 -07:00
|
|
|
|
2011-06-20 16:15:11 +00:00
|
|
|
fn hardbreak_if_not_bol(&ps s) {
|
|
|
|
if (s.s.last_token() != pp::EOF &&
|
|
|
|
s.s.last_token() != pp::hardbreak_tok()) {
|
2011-06-20 10:45:05 -04:00
|
|
|
hardbreak(s.s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-19 22:55:28 -04:00
|
|
|
fn space_if_not_hardbreak(&ps s) {
|
|
|
|
if (s.s.last_token() != pp::hardbreak_tok()) {
|
|
|
|
space(s.s);
|
|
|
|
}
|
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-01 18:53:52 -07:00
|
|
|
// Synthesizes a comment that was not textually present in the original source
|
|
|
|
// file.
|
|
|
|
fn synth_comment(&ps s, str text) {
|
|
|
|
word(s.s, "/*");
|
|
|
|
space(s.s);
|
|
|
|
word(s.s, text);
|
|
|
|
space(s.s);
|
|
|
|
word(s.s, "*/");
|
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
fn commasep[IN](&ps s, breaks b, vec[IN] elts, fn(&ps, &IN) op) {
|
2011-06-01 15:29:38 -07:00
|
|
|
box(s, 0u, b);
|
2011-03-24 16:33:20 +01:00
|
|
|
auto first = true;
|
|
|
|
for (IN elt in elts) {
|
2011-06-15 11:19:50 -07:00
|
|
|
if (first) { first = false; } else { word_space(s, ","); }
|
2011-03-24 16:33:20 +01:00
|
|
|
op(s, elt);
|
|
|
|
}
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s);
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
2011-05-28 19:16:18 -07:00
|
|
|
|
2011-07-05 16:23:07 -07:00
|
|
|
fn commasep_ivec[IN](&ps s, breaks b, &IN[] elts, fn(&ps, &IN) op) {
|
|
|
|
box(s, 0u, b);
|
|
|
|
auto first = true;
|
|
|
|
for (IN elt in elts) {
|
|
|
|
if (first) { first = false; } else { word_space(s, ","); }
|
|
|
|
op(s, elt);
|
|
|
|
}
|
|
|
|
end(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
fn commasep_cmnt[IN](&ps s, breaks b, vec[IN] elts, fn(&ps, &IN) op,
|
2011-07-05 11:48:19 +02:00
|
|
|
fn(&IN) -> codemap::span get_span) {
|
2011-06-01 15:29:38 -07:00
|
|
|
box(s, 0u, b);
|
2011-05-17 20:41:41 +02:00
|
|
|
auto len = vec::len[IN](elts);
|
2011-03-29 12:46:55 +02:00
|
|
|
auto i = 0u;
|
|
|
|
for (IN elt in elts) {
|
2011-05-30 15:56:01 -07:00
|
|
|
maybe_print_comment(s, get_span(elt).hi);
|
2011-03-29 12:46:55 +02:00
|
|
|
op(s, elt);
|
|
|
|
i += 1u;
|
|
|
|
if (i < len) {
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, ",");
|
2011-06-03 17:24:05 -07:00
|
|
|
maybe_print_trailing_comment(s, get_span(elt),
|
|
|
|
some(get_span(elts.(i)).hi));
|
2011-06-19 22:55:28 -04:00
|
|
|
space_if_not_hardbreak(s);
|
2011-03-29 12:46:55 +02:00
|
|
|
}
|
|
|
|
}
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s);
|
2011-03-29 12:46:55 +02:00
|
|
|
}
|
2011-05-28 19:16:18 -07:00
|
|
|
|
2011-05-31 10:58:30 -07:00
|
|
|
fn commasep_exprs(&ps s, breaks b, vec[@ast::expr] exprs) {
|
2011-07-05 11:48:19 +02:00
|
|
|
fn expr_span(&@ast::expr expr) -> codemap::span { ret expr.span; }
|
2011-06-14 17:09:01 +02:00
|
|
|
commasep_cmnt(s, b, exprs, print_expr, expr_span);
|
2011-03-29 12:46:55 +02:00
|
|
|
}
|
2011-03-04 07:22:43 +01:00
|
|
|
|
2011-07-05 17:02:51 -07:00
|
|
|
fn print_mod(&ps s, ast::_mod _mod, &ast::attribute[] attrs) {
|
2011-06-30 16:03:07 -07:00
|
|
|
print_inner_attributes(s, attrs);
|
2011-05-12 17:24:54 +02:00
|
|
|
for (@ast::view_item vitem in _mod.view_items) {
|
|
|
|
print_view_item(s, vitem);
|
|
|
|
}
|
2011-05-28 19:16:18 -07:00
|
|
|
for (@ast::item item in _mod.items) {
|
2011-06-30 17:25:13 -07:00
|
|
|
hardbreak_if_not_bol(s);
|
2011-05-28 19:16:18 -07:00
|
|
|
print_item(s, item);
|
|
|
|
}
|
2011-03-17 17:39:47 -07:00
|
|
|
}
|
|
|
|
|
2011-05-31 10:58:30 -07:00
|
|
|
fn print_boxed_type(&ps s, &@ast::ty ty) { print_type(s, *ty); }
|
2011-05-28 19:16:18 -07:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
fn print_type(&ps s, &ast::ty ty) {
|
2011-03-24 16:33:20 +01:00
|
|
|
maybe_print_comment(s, ty.span.lo);
|
2011-06-01 15:29:38 -07:00
|
|
|
ibox(s, 0u);
|
2011-03-24 16:33:20 +01:00
|
|
|
alt (ty.node) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (ast::ty_nil) { word(s.s, "()"); }
|
|
|
|
case (ast::ty_bool) { word(s.s, "bool"); }
|
|
|
|
case (ast::ty_bot) { word(s.s, "!"); }
|
|
|
|
case (ast::ty_int) { word(s.s, "int"); }
|
|
|
|
case (ast::ty_uint) { word(s.s, "uint"); }
|
|
|
|
case (ast::ty_float) { word(s.s, "float"); }
|
2011-07-05 11:48:19 +02:00
|
|
|
case (ast::ty_machine(?tm)) { word(s.s, ast::ty_mach_to_str(tm)); }
|
2011-06-15 11:19:50 -07:00
|
|
|
case (ast::ty_char) { word(s.s, "char"); }
|
|
|
|
case (ast::ty_str) { word(s.s, "str"); }
|
2011-06-23 18:40:17 -04:00
|
|
|
case (ast::ty_istr) { word(s.s, "istr"); }
|
2011-06-15 11:19:50 -07:00
|
|
|
case (ast::ty_box(?mt)) { word(s.s, "@"); print_mt(s, mt); }
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::ty_vec(?mt)) {
|
2011-06-15 11:19:50 -07:00
|
|
|
word(s.s, "vec[");
|
|
|
|
print_mt(s, mt);
|
|
|
|
word(s.s, "]");
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-16 14:01:02 -07:00
|
|
|
case (ast::ty_ivec(?mt)) {
|
|
|
|
print_type(s, *mt.ty);
|
|
|
|
word(s.s, "[");
|
|
|
|
print_mutability(s, mt.mut);
|
|
|
|
word(s.s, "]");
|
|
|
|
}
|
2011-06-23 18:40:17 -04:00
|
|
|
case (ast::ty_ptr(?mt)) {
|
|
|
|
word(s.s, "*");
|
|
|
|
print_mt(s, mt);
|
|
|
|
}
|
|
|
|
case (ast::ty_task) {
|
|
|
|
word(s.s, "task");
|
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::ty_port(?t)) {
|
2011-06-15 11:19:50 -07:00
|
|
|
word(s.s, "port[");
|
|
|
|
print_type(s, *t);
|
|
|
|
word(s.s, "]");
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::ty_chan(?t)) {
|
2011-06-15 11:19:50 -07:00
|
|
|
word(s.s, "chan[");
|
|
|
|
print_type(s, *t);
|
|
|
|
word(s.s, "]");
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::ty_tup(?elts)) {
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, "tup");
|
2011-03-24 16:33:20 +01:00
|
|
|
popen(s);
|
2011-06-14 17:09:01 +02:00
|
|
|
commasep(s, inconsistent, elts, print_mt);
|
2011-03-24 16:33:20 +01:00
|
|
|
pclose(s);
|
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::ty_rec(?fields)) {
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, "rec");
|
2011-03-24 16:33:20 +01:00
|
|
|
popen(s);
|
2011-05-31 10:58:30 -07:00
|
|
|
fn print_field(&ps s, &ast::ty_field f) {
|
2011-06-01 15:29:38 -07:00
|
|
|
cbox(s, indent_unit);
|
2011-06-03 15:26:03 -07:00
|
|
|
print_mt(s, f.node.mt);
|
2011-03-24 16:33:20 +01:00
|
|
|
space(s.s);
|
2011-06-03 15:26:03 -07:00
|
|
|
word(s.s, f.node.ident);
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-07-05 11:48:19 +02:00
|
|
|
fn get_span(&ast::ty_field f) -> codemap::span { ret f.span; }
|
2011-06-14 17:09:01 +02:00
|
|
|
commasep_cmnt(s, consistent, fields, print_field, get_span);
|
2011-03-24 16:33:20 +01:00
|
|
|
pclose(s);
|
|
|
|
}
|
2011-06-23 18:40:17 -04:00
|
|
|
case (ast::ty_fn(?proto, ?inputs, ?output, ?cf, ?constrs)) {
|
|
|
|
print_ty_fn(s, proto, none[str], inputs, output, cf, constrs);
|
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::ty_obj(?methods)) {
|
2011-05-28 19:16:18 -07:00
|
|
|
head(s, "obj");
|
2011-03-24 16:33:20 +01:00
|
|
|
bopen(s);
|
2011-05-12 17:24:54 +02:00
|
|
|
for (ast::ty_method m in methods) {
|
2011-06-30 17:25:13 -07:00
|
|
|
hardbreak_if_not_bol(s);
|
2011-06-01 15:29:38 -07:00
|
|
|
cbox(s, indent_unit);
|
2011-06-03 15:26:03 -07:00
|
|
|
maybe_print_comment(s, m.span.lo);
|
|
|
|
print_ty_fn(s, m.node.proto, some(m.node.ident),
|
2011-06-09 09:48:16 -07:00
|
|
|
m.node.inputs, m.node.output, m.node.cf,
|
|
|
|
m.node.constrs);
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, ";");
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-05-28 19:16:18 -07:00
|
|
|
bclose(s, ty.span);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
case (ast::ty_path(?path, _)) { print_path(s, path); }
|
2011-06-23 18:40:17 -04:00
|
|
|
case (ast::ty_type) { word(s.s, "type"); }
|
|
|
|
case (ast::ty_constr(?t, ?cs)) {
|
|
|
|
print_type(s, *t);
|
|
|
|
space(s.s);
|
|
|
|
word(s.s, ":");
|
|
|
|
space(s.s);
|
|
|
|
word(s.s, ast_constrs_str(cs));
|
|
|
|
}
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s);
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
|
|
|
|
2011-05-31 10:58:30 -07:00
|
|
|
fn print_item(&ps s, &@ast::item item) {
|
2011-06-20 16:15:11 +00:00
|
|
|
hardbreak_if_not_bol(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
maybe_print_comment(s, item.span.lo);
|
2011-06-16 13:00:19 -07:00
|
|
|
print_outer_attributes(s, item.attrs);
|
2011-07-05 10:38:35 +02:00
|
|
|
auto ann_node = node_item(s, item);
|
|
|
|
s.ann.pre(ann_node);
|
2011-03-24 16:33:20 +01:00
|
|
|
alt (item.node) {
|
2011-06-16 11:53:06 +02:00
|
|
|
case (ast::item_const(?ty, ?expr)) {
|
2011-05-28 19:16:18 -07:00
|
|
|
head(s, "const");
|
2011-05-31 10:58:30 -07:00
|
|
|
print_type(s, *ty);
|
2011-03-24 16:33:20 +01:00
|
|
|
space(s.s);
|
2011-06-16 11:53:06 +02:00
|
|
|
word_space(s, item.ident);
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s); // end the head-ibox
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-05-28 19:16:18 -07:00
|
|
|
word_space(s, "=");
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, expr);
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, ";");
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s); // end the outer cbox
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-16 11:53:06 +02:00
|
|
|
case (ast::item_fn(?_fn, ?typarams)) {
|
|
|
|
print_fn(s, _fn.decl, _fn.proto, item.ident, typarams);
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, " ");
|
2011-03-24 16:33:20 +01:00
|
|
|
print_block(s, _fn.body);
|
|
|
|
}
|
2011-06-16 11:53:06 +02:00
|
|
|
case (ast::item_mod(?_mod)) {
|
2011-05-28 19:16:18 -07:00
|
|
|
head(s, "mod");
|
2011-06-16 11:53:06 +02:00
|
|
|
word_nbsp(s, item.ident);
|
2011-03-24 16:33:20 +01:00
|
|
|
bopen(s);
|
2011-06-30 16:03:07 -07:00
|
|
|
print_mod(s, _mod, item.attrs);
|
2011-05-28 19:16:18 -07:00
|
|
|
bclose(s, item.span);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-16 11:53:06 +02:00
|
|
|
case (ast::item_native_mod(?nmod)) {
|
2011-05-28 19:16:18 -07:00
|
|
|
head(s, "native");
|
2011-03-24 16:33:20 +01:00
|
|
|
alt (nmod.abi) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (ast::native_abi_rust) { word_nbsp(s, "\"rust\""); }
|
|
|
|
case (ast::native_abi_cdecl) { word_nbsp(s, "\"cdecl\""); }
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::native_abi_rust_intrinsic) {
|
2011-05-28 19:16:18 -07:00
|
|
|
word_nbsp(s, "\"rust-intrinsic\"");
|
2011-05-03 18:03:59 -07:00
|
|
|
}
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-05-28 19:16:18 -07:00
|
|
|
word_nbsp(s, "mod");
|
2011-06-16 11:53:06 +02:00
|
|
|
word_nbsp(s, item.ident);
|
2011-03-24 16:33:20 +01:00
|
|
|
bopen(s);
|
2011-07-05 12:38:53 -07:00
|
|
|
print_inner_attributes(s, item.attrs);
|
2011-05-12 17:24:54 +02:00
|
|
|
for (@ast::native_item item in nmod.items) {
|
2011-06-30 17:25:13 -07:00
|
|
|
hardbreak_if_not_bol(s);
|
2011-07-05 12:38:53 -07:00
|
|
|
print_outer_attributes(s, item.attrs);
|
2011-06-01 15:29:38 -07:00
|
|
|
ibox(s, indent_unit);
|
2011-03-24 16:33:20 +01:00
|
|
|
maybe_print_comment(s, item.span.lo);
|
|
|
|
alt (item.node) {
|
2011-06-21 23:01:08 +02:00
|
|
|
case (ast::native_item_ty) {
|
2011-05-28 19:16:18 -07:00
|
|
|
word_nbsp(s, "type");
|
2011-06-21 23:01:08 +02:00
|
|
|
word(s.s, item.ident);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-21 23:01:08 +02:00
|
|
|
case (ast::native_item_fn(?lname, ?decl, ?typarams)) {
|
|
|
|
print_fn(s, decl, ast::proto_fn, item.ident,
|
|
|
|
typarams);
|
2011-03-24 16:33:20 +01:00
|
|
|
alt (lname) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (none) { }
|
2011-06-15 11:39:31 -07:00
|
|
|
case (some(?ss)) {
|
|
|
|
space(s.s);
|
|
|
|
word_space(s, "=");
|
|
|
|
print_string(s, ss);
|
|
|
|
}
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-15 10:08:59 -07:00
|
|
|
end(s); // end head-ibox
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-15 10:08:59 -07:00
|
|
|
end(s); // end the outer fn box
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
|
|
|
}
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, ";");
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-05-28 19:16:18 -07:00
|
|
|
bclose(s, item.span);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-16 11:53:06 +02:00
|
|
|
case (ast::item_ty(?ty, ?params)) {
|
2011-06-01 15:29:38 -07:00
|
|
|
ibox(s, indent_unit);
|
|
|
|
ibox(s, 0u);
|
2011-05-28 19:16:18 -07:00
|
|
|
word_nbsp(s, "type");
|
2011-06-16 11:53:06 +02:00
|
|
|
word(s.s, item.ident);
|
2011-03-24 16:33:20 +01:00
|
|
|
print_type_params(s, params);
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s); // end the inner ibox
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-03-24 16:33:20 +01:00
|
|
|
space(s.s);
|
2011-05-28 19:16:18 -07:00
|
|
|
word_space(s, "=");
|
2011-05-31 10:58:30 -07:00
|
|
|
print_type(s, *ty);
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, ";");
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s); // end the outer ibox
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-05-28 19:16:18 -07:00
|
|
|
break_offset(s.s, 0u, 0);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-16 11:53:06 +02:00
|
|
|
case (ast::item_tag(?variants, ?params)) {
|
2011-07-01 13:03:05 +02:00
|
|
|
auto newtype = vec::len(variants) == 1u &&
|
|
|
|
str::eq(item.ident, variants.(0).node.name) &&
|
|
|
|
vec::len(variants.(0).node.args) == 1u;
|
|
|
|
if (newtype) {
|
|
|
|
ibox(s, indent_unit);
|
|
|
|
word_space(s, "tag");
|
|
|
|
} else {
|
|
|
|
head(s, "tag");
|
|
|
|
}
|
2011-06-16 11:53:06 +02:00
|
|
|
word(s.s, item.ident);
|
2011-03-24 16:33:20 +01:00
|
|
|
print_type_params(s, params);
|
|
|
|
space(s.s);
|
2011-07-01 13:03:05 +02:00
|
|
|
if (newtype) {
|
|
|
|
word_space(s, "=");
|
|
|
|
print_type(s, *variants.(0).node.args.(0).ty);
|
|
|
|
word(s.s, ";");
|
|
|
|
end(s);
|
|
|
|
} else {
|
|
|
|
bopen(s);
|
|
|
|
for (ast::variant v in variants) {
|
|
|
|
space(s.s);
|
|
|
|
maybe_print_comment(s, v.span.lo);
|
|
|
|
word(s.s, v.node.name);
|
|
|
|
if (vec::len(v.node.args) > 0u) {
|
|
|
|
popen(s);
|
|
|
|
fn print_variant_arg(&ps s, &ast::variant_arg arg) {
|
|
|
|
print_type(s, *arg.ty);
|
|
|
|
}
|
|
|
|
commasep(s, consistent, v.node.args,
|
|
|
|
print_variant_arg);
|
|
|
|
pclose(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-07-01 13:03:05 +02:00
|
|
|
word(s.s, ";");
|
|
|
|
maybe_print_trailing_comment(s, v.span, none[uint]);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-07-01 13:03:05 +02:00
|
|
|
bclose(s, item.span);
|
2011-03-20 20:18:19 -07:00
|
|
|
}
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
2011-06-16 11:53:06 +02:00
|
|
|
case (ast::item_obj(?_obj, ?params, _)) {
|
2011-05-28 19:16:18 -07:00
|
|
|
head(s, "obj");
|
2011-06-16 11:53:06 +02:00
|
|
|
word(s.s, item.ident);
|
2011-03-24 16:33:20 +01:00
|
|
|
print_type_params(s, params);
|
|
|
|
popen(s);
|
2011-05-31 10:58:30 -07:00
|
|
|
fn print_field(&ps s, &ast::obj_field field) {
|
2011-06-01 15:29:38 -07:00
|
|
|
ibox(s, indent_unit);
|
2011-06-15 10:25:23 -07:00
|
|
|
print_mutability(s, field.mut);
|
2011-05-31 10:58:30 -07:00
|
|
|
print_type(s, *field.ty);
|
2011-03-24 16:33:20 +01:00
|
|
|
space(s.s);
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, field.ident);
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-07-05 11:48:19 +02:00
|
|
|
fn get_span(&ast::obj_field f) -> codemap::span { ret f.ty.span; }
|
2011-06-14 17:09:01 +02:00
|
|
|
commasep_cmnt(s, consistent, _obj.fields, print_field, get_span);
|
2011-03-24 16:33:20 +01:00
|
|
|
pclose(s);
|
|
|
|
space(s.s);
|
|
|
|
bopen(s);
|
2011-05-12 17:24:54 +02:00
|
|
|
for (@ast::method meth in _obj.methods) {
|
2011-05-16 18:21:22 -07:00
|
|
|
let vec[ast::ty_param] typarams = [];
|
2011-06-30 17:25:13 -07:00
|
|
|
hardbreak_if_not_bol(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
maybe_print_comment(s, meth.span.lo);
|
2011-06-01 13:18:16 -07:00
|
|
|
print_fn(s, meth.node.meth.decl, meth.node.meth.proto,
|
|
|
|
meth.node.ident, typarams);
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, " ");
|
2011-03-24 16:33:20 +01:00
|
|
|
print_block(s, meth.node.meth.body);
|
|
|
|
}
|
|
|
|
alt (_obj.dtor) {
|
2011-06-02 17:18:43 -07:00
|
|
|
case (some(?dtor)) {
|
2011-05-28 19:16:18 -07:00
|
|
|
head(s, "drop");
|
2011-04-14 16:50:48 -07:00
|
|
|
print_block(s, dtor.node.meth.body);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
case (_) { }
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-05-28 19:16:18 -07:00
|
|
|
bclose(s, item.span);
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
2011-06-24 18:10:40 +02:00
|
|
|
case (ast::item_res(?dt, ?dt_id, ?tps, ?ct_id)) {
|
2011-07-01 10:43:23 +02:00
|
|
|
head(s, "resource");
|
2011-06-24 18:10:40 +02:00
|
|
|
word(s.s, item.ident);
|
|
|
|
print_type_params(s, tps);
|
|
|
|
popen(s);
|
|
|
|
print_type(s, *dt.decl.inputs.(0).ty);
|
|
|
|
space(s.s);
|
|
|
|
word(s.s, dt.decl.inputs.(0).ident);
|
|
|
|
pclose(s);
|
|
|
|
space(s.s);
|
|
|
|
print_block(s, dt.body);
|
|
|
|
}
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
2011-07-05 10:38:35 +02:00
|
|
|
s.ann.post(ann_node);
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
|
|
|
|
2011-07-05 17:02:51 -07:00
|
|
|
fn print_outer_attributes(&ps s, &ast::attribute[] attrs) {
|
2011-06-14 18:53:12 -07:00
|
|
|
auto count = 0;
|
|
|
|
for (ast::attribute attr in attrs) {
|
|
|
|
alt (attr.node.style) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (ast::attr_outer) { print_attribute(s, attr); count += 1; }
|
|
|
|
case (_) {/* fallthrough */ }
|
2011-06-14 18:53:12 -07:00
|
|
|
}
|
|
|
|
}
|
2011-06-30 17:25:13 -07:00
|
|
|
if (count > 0) { hardbreak_if_not_bol(s); }
|
2011-06-14 18:53:12 -07:00
|
|
|
}
|
|
|
|
|
2011-07-05 17:02:51 -07:00
|
|
|
fn print_inner_attributes(&ps s, &ast::attribute[] attrs) {
|
2011-06-16 13:00:19 -07:00
|
|
|
auto count = 0;
|
|
|
|
for (ast::attribute attr in attrs) {
|
|
|
|
alt (attr.node.style) {
|
|
|
|
case (ast::attr_inner) {
|
|
|
|
print_attribute(s, attr);
|
|
|
|
word(s.s, ";");
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
case (_) { /* fallthrough */ }
|
|
|
|
}
|
|
|
|
}
|
2011-06-30 17:25:13 -07:00
|
|
|
if (count > 0) { hardbreak_if_not_bol(s); }
|
2011-06-16 13:00:19 -07:00
|
|
|
}
|
|
|
|
|
2011-06-14 18:53:12 -07:00
|
|
|
fn print_attribute(&ps s, &ast::attribute attr) {
|
2011-06-30 17:25:13 -07:00
|
|
|
hardbreak_if_not_bol(s);
|
2011-06-14 18:53:12 -07:00
|
|
|
maybe_print_comment(s, attr.span.lo);
|
|
|
|
word(s.s, "#[");
|
|
|
|
print_meta_item(s, @attr.node.value);
|
|
|
|
word(s.s, "]");
|
|
|
|
}
|
|
|
|
|
2011-05-31 10:58:30 -07:00
|
|
|
fn print_stmt(&ps s, &ast::stmt st) {
|
|
|
|
maybe_print_comment(s, st.span.lo);
|
|
|
|
alt (st.node) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (ast::stmt_decl(?decl, _)) { print_decl(s, decl); }
|
2011-06-19 22:55:28 -04:00
|
|
|
case (ast::stmt_expr(?expr, _)) {
|
|
|
|
space_if_not_hardbreak(s);
|
|
|
|
print_expr(s, expr);
|
|
|
|
}
|
2011-05-31 10:58:30 -07:00
|
|
|
}
|
2011-07-05 11:48:19 +02:00
|
|
|
if (parse::parser::stmt_ends_with_semi(st)) { word(s.s, ";"); }
|
2011-06-03 17:24:05 -07:00
|
|
|
maybe_print_trailing_comment(s, st.span, none[uint]);
|
2011-05-31 10:58:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn print_block(&ps s, ast::block blk) {
|
2011-03-24 16:33:20 +01:00
|
|
|
maybe_print_comment(s, blk.span.lo);
|
2011-07-05 10:38:35 +02:00
|
|
|
auto ann_node = node_block(s, blk);
|
|
|
|
s.ann.pre(ann_node);
|
2011-03-24 16:33:20 +01:00
|
|
|
bopen(s);
|
2011-06-15 11:19:50 -07:00
|
|
|
for (@ast::stmt st in blk.node.stmts) { print_stmt(s, *st) }
|
2011-03-24 16:33:20 +01:00
|
|
|
alt (blk.node.expr) {
|
2011-06-02 17:18:43 -07:00
|
|
|
case (some(?expr)) {
|
2011-06-19 22:55:28 -04:00
|
|
|
space_if_not_hardbreak(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, expr);
|
2011-06-15 11:19:50 -07:00
|
|
|
maybe_print_trailing_comment(s, expr.span, some(blk.span.hi));
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
case (_) { }
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
2011-05-28 19:16:18 -07:00
|
|
|
bclose(s, blk.span);
|
2011-07-05 10:38:35 +02:00
|
|
|
s.ann.post(ann_node);
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
|
|
|
|
2011-06-16 14:08:17 -07:00
|
|
|
fn print_if(&ps s, &@ast::expr test, &ast::block block,
|
2011-06-20 16:06:21 +00:00
|
|
|
&option::t[@ast::expr] elseopt, bool chk) {
|
2011-06-16 14:08:17 -07:00
|
|
|
head(s, "if");
|
2011-06-20 16:06:21 +00:00
|
|
|
if (chk) {
|
|
|
|
word_nbsp(s, "check");
|
|
|
|
}
|
2011-06-16 14:08:17 -07:00
|
|
|
popen(s);
|
|
|
|
print_expr(s, test);
|
|
|
|
pclose(s);
|
|
|
|
space(s.s);
|
|
|
|
print_block(s, block);
|
|
|
|
fn do_else(&ps s, option::t[@ast::expr] els) {
|
|
|
|
alt (els) {
|
|
|
|
case (some(?_else)) {
|
|
|
|
alt (_else.node) {
|
2011-06-21 22:16:40 +02:00
|
|
|
// "another else-if"
|
|
|
|
case (ast::expr_if(?i, ?t, ?e)) {
|
2011-06-16 14:08:17 -07:00
|
|
|
cbox(s, indent_unit - 1u);
|
|
|
|
ibox(s, 0u);
|
|
|
|
word(s.s, " else if ");
|
|
|
|
popen(s);
|
|
|
|
print_expr(s, i);
|
|
|
|
pclose(s);
|
|
|
|
space(s.s);
|
|
|
|
print_block(s, t);
|
|
|
|
do_else(s, e);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
// "final else"
|
|
|
|
case (ast::expr_block(?b)) {
|
2011-06-16 14:08:17 -07:00
|
|
|
cbox(s, indent_unit - 1u);
|
|
|
|
ibox(s, 0u);
|
|
|
|
word(s.s, " else ");
|
|
|
|
print_block(s, b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case (_) {/* fall through */ }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
do_else(s, elseopt);
|
|
|
|
}
|
|
|
|
|
2011-05-31 10:58:30 -07:00
|
|
|
fn print_expr(&ps s, &@ast::expr expr) {
|
2011-03-24 16:33:20 +01:00
|
|
|
maybe_print_comment(s, expr.span.lo);
|
2011-06-01 15:29:38 -07:00
|
|
|
ibox(s, indent_unit);
|
2011-07-05 10:38:35 +02:00
|
|
|
auto ann_node = node_expr(s, expr);
|
|
|
|
s.ann.pre(ann_node);
|
2011-03-24 16:33:20 +01:00
|
|
|
alt (expr.node) {
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_vec(?exprs, ?mut, ?kind)) {
|
2011-06-01 15:29:38 -07:00
|
|
|
ibox(s, indent_unit);
|
2011-06-09 17:11:21 -07:00
|
|
|
alt (kind) {
|
|
|
|
case (ast::sk_rc) { word(s.s, "["); }
|
|
|
|
case (ast::sk_unique) { word(s.s, "~["); }
|
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
if (mut == ast::mut) { word_nbsp(s, "mutable"); }
|
2011-05-29 07:43:33 +00:00
|
|
|
commasep_exprs(s, inconsistent, exprs);
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, "]");
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s);
|
2011-03-14 15:03:47 +01:00
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_tup(?exprs)) {
|
2011-05-31 10:58:30 -07:00
|
|
|
fn printElt(&ps s, &ast::elt elt) {
|
2011-06-01 15:29:38 -07:00
|
|
|
ibox(s, indent_unit);
|
2011-06-15 11:19:50 -07:00
|
|
|
if (elt.mut == ast::mut) { word_nbsp(s, "mutable"); }
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, elt.expr);
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-07-05 11:48:19 +02:00
|
|
|
fn get_span(&ast::elt elt) -> codemap::span { ret elt.expr.span; }
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, "tup");
|
2011-03-24 16:33:20 +01:00
|
|
|
popen(s);
|
2011-06-14 17:09:01 +02:00
|
|
|
commasep_cmnt(s, inconsistent, exprs, printElt, get_span);
|
2011-03-24 16:33:20 +01:00
|
|
|
pclose(s);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_rec(?fields, ?wth)) {
|
2011-05-31 10:58:30 -07:00
|
|
|
fn print_field(&ps s, &ast::field field) {
|
2011-06-01 15:29:38 -07:00
|
|
|
ibox(s, indent_unit);
|
2011-06-15 11:19:50 -07:00
|
|
|
if (field.node.mut == ast::mut) { word_nbsp(s, "mutable"); }
|
2011-05-30 15:56:01 -07:00
|
|
|
word(s.s, field.node.ident);
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, "=");
|
2011-05-30 15:56:01 -07:00
|
|
|
print_expr(s, field.node.expr);
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-07-05 11:48:19 +02:00
|
|
|
fn get_span(&ast::field field) -> codemap::span {
|
|
|
|
ret field.span;
|
|
|
|
}
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, "rec");
|
2011-03-24 16:33:20 +01:00
|
|
|
popen(s);
|
2011-06-14 17:09:01 +02:00
|
|
|
commasep_cmnt(s, consistent, fields, print_field, get_span);
|
2011-03-24 16:33:20 +01:00
|
|
|
alt (wth) {
|
2011-06-02 17:18:43 -07:00
|
|
|
case (some(?expr)) {
|
2011-06-15 11:19:50 -07:00
|
|
|
if (vec::len(fields) > 0u) { space(s.s); }
|
2011-06-01 15:29:38 -07:00
|
|
|
ibox(s, indent_unit);
|
2011-05-28 19:16:18 -07:00
|
|
|
word_space(s, "with");
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, expr);
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
case (_) { }
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
|
|
|
pclose(s);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_call(?func, ?args)) {
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, func);
|
|
|
|
popen(s);
|
2011-05-29 07:43:33 +00:00
|
|
|
commasep_exprs(s, inconsistent, args);
|
2011-03-24 16:33:20 +01:00
|
|
|
pclose(s);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_self_method(?ident)) {
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, "self.");
|
2011-04-05 14:18:44 -07:00
|
|
|
print_ident(s, ident);
|
2011-03-31 11:26:25 -07:00
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_bind(?func, ?args)) {
|
2011-05-31 10:58:30 -07:00
|
|
|
fn print_opt(&ps s, &option::t[@ast::expr] expr) {
|
2011-03-24 16:33:20 +01:00
|
|
|
alt (expr) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (some(?expr)) { print_expr(s, expr); }
|
|
|
|
case (_) { word(s.s, "_"); }
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
|
|
|
}
|
2011-05-28 19:16:18 -07:00
|
|
|
word_nbsp(s, "bind");
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, func);
|
|
|
|
popen(s);
|
2011-06-14 17:09:01 +02:00
|
|
|
commasep(s, inconsistent, args, print_opt);
|
2011-03-24 16:33:20 +01:00
|
|
|
pclose(s);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_spawn(_, _, ?e, ?es)) {
|
2011-06-15 11:19:50 -07:00
|
|
|
word_nbsp(s, "spawn");
|
|
|
|
print_expr(s, e);
|
|
|
|
popen(s);
|
|
|
|
commasep_exprs(s, inconsistent, es);
|
|
|
|
pclose(s);
|
Handle nested items correctly in typestate_check
Summary says it all. Actually, only nested objects and functions
are handled, but that's better than before. The fold that I was using
before to traverse a crate wasn't working correctly, because annotations
have to reflect the number of local variables of the nearest enclosing
function (in turn, because annotations are represented as bit vectors).
The fold was traversing the AST in the wrong order, first filling in
the annotations correctly, but then re-traversing them with the bit
vector length for any outer nested functions, and so on.
Remedying this required writing a lot of tedious boilerplate code
because I scrapped the idea of using a fold altogether.
I also made typestate_check handle unary, field, alt, and fail.
Also, some miscellaneous changes:
* added annotations to blocks in typeck
* fix pprust so it can handle spawn
* added more logging functions in util.common
* fixed _vec.or
* added maybe and from_maybe in option
* removed fold_block field from ast_fold, since it was never used
2011-04-18 15:33:10 -07:00
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_binary(?op, ?lhs, ?rhs)) {
|
2011-03-24 16:33:20 +01:00
|
|
|
auto prec = operator_prec(op);
|
|
|
|
print_maybe_parens(s, lhs, prec);
|
|
|
|
space(s.s);
|
2011-05-28 19:16:18 -07:00
|
|
|
word_space(s, ast::binop_to_str(op));
|
2011-03-24 16:33:20 +01:00
|
|
|
print_maybe_parens(s, rhs, prec + 1);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_unary(?op, ?expr)) {
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, ast::unop_to_str(op));
|
2011-07-05 11:48:19 +02:00
|
|
|
print_maybe_parens(s, expr, parse::parser::unop_prec);
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_lit(?lit)) { print_literal(s, lit); }
|
|
|
|
case (ast::expr_cast(?expr, ?ty)) {
|
2011-07-05 11:48:19 +02:00
|
|
|
print_maybe_parens(s, expr, parse::parser::as_prec);
|
2011-03-24 16:33:20 +01:00
|
|
|
space(s.s);
|
2011-05-28 19:16:18 -07:00
|
|
|
word_space(s, "as");
|
2011-05-31 10:58:30 -07:00
|
|
|
print_type(s, *ty);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_if(?test, ?block, ?elseopt)) {
|
2011-06-20 16:06:21 +00:00
|
|
|
print_if(s, test, block, elseopt, false);
|
2011-06-16 14:08:17 -07:00
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_if_check(?test, ?block, ?elseopt)) {
|
2011-06-20 16:06:21 +00:00
|
|
|
print_if(s, test, block, elseopt, true);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-23 23:14:40 -07:00
|
|
|
case (ast::expr_ternary(?test, ?then, ?els)) {
|
|
|
|
print_expr(s, test);
|
|
|
|
space(s.s);
|
|
|
|
word_space(s, "?");
|
|
|
|
print_expr(s, then);
|
|
|
|
space(s.s);
|
|
|
|
word_space(s, ":");
|
|
|
|
print_expr(s, els);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_while(?test, ?block)) {
|
2011-05-28 19:16:18 -07:00
|
|
|
head(s, "while");
|
|
|
|
popen(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, test);
|
|
|
|
pclose(s);
|
|
|
|
space(s.s);
|
|
|
|
print_block(s, block);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_for(?decl, ?expr, ?block)) {
|
2011-05-28 19:16:18 -07:00
|
|
|
head(s, "for");
|
|
|
|
popen(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
print_for_decl(s, decl);
|
|
|
|
space(s.s);
|
2011-05-28 19:16:18 -07:00
|
|
|
word_space(s, "in");
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, expr);
|
|
|
|
pclose(s);
|
|
|
|
space(s.s);
|
|
|
|
print_block(s, block);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_for_each(?decl, ?expr, ?block)) {
|
2011-05-28 19:16:18 -07:00
|
|
|
head(s, "for each");
|
|
|
|
popen(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
print_for_decl(s, decl);
|
|
|
|
space(s.s);
|
2011-05-28 19:16:18 -07:00
|
|
|
word_space(s, "in");
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, expr);
|
|
|
|
pclose(s);
|
|
|
|
space(s.s);
|
|
|
|
print_block(s, block);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_do_while(?block, ?expr)) {
|
2011-05-28 19:16:18 -07:00
|
|
|
head(s, "do");
|
2011-03-24 16:33:20 +01:00
|
|
|
space(s.s);
|
|
|
|
print_block(s, block);
|
|
|
|
space(s.s);
|
2011-05-28 19:16:18 -07:00
|
|
|
word_space(s, "while");
|
|
|
|
popen(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, expr);
|
|
|
|
pclose(s);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_alt(?expr, ?arms)) {
|
2011-05-28 19:16:18 -07:00
|
|
|
head(s, "alt");
|
|
|
|
popen(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, expr);
|
|
|
|
pclose(s);
|
|
|
|
space(s.s);
|
|
|
|
bopen(s);
|
2011-05-12 17:24:54 +02:00
|
|
|
for (ast::arm arm in arms) {
|
2011-05-28 19:16:18 -07:00
|
|
|
space(s.s);
|
|
|
|
head(s, "case");
|
|
|
|
popen(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
print_pat(s, arm.pat);
|
|
|
|
pclose(s);
|
|
|
|
space(s.s);
|
|
|
|
print_block(s, arm.block);
|
|
|
|
}
|
2011-05-28 19:16:18 -07:00
|
|
|
bclose(s, expr.span);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_fn(?f)) {
|
2011-06-14 15:20:04 +02:00
|
|
|
head(s, "fn");
|
|
|
|
print_fn_args_and_ret(s, f.decl);
|
|
|
|
space(s.s);
|
|
|
|
print_block(s, f.body);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_block(?block)) {
|
2011-05-31 09:54:18 -07:00
|
|
|
// containing cbox, will be closed by print-block at }
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
cbox(s, indent_unit);
|
2011-05-31 09:54:18 -07:00
|
|
|
// head-box, will be closed by print-block after {
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-01 15:29:38 -07:00
|
|
|
ibox(s, 0u);
|
2011-03-24 16:33:20 +01:00
|
|
|
print_block(s, block);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_move(?lhs, ?rhs)) {
|
2011-05-27 17:38:52 -07:00
|
|
|
print_expr(s, lhs);
|
|
|
|
space(s.s);
|
2011-05-31 16:16:36 -07:00
|
|
|
word_space(s, "<-");
|
2011-05-27 17:38:52 -07:00
|
|
|
print_expr(s, rhs);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_assign(?lhs, ?rhs)) {
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, lhs);
|
|
|
|
space(s.s);
|
2011-05-28 19:16:18 -07:00
|
|
|
word_space(s, "=");
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, rhs);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_swap(?lhs, ?rhs)) {
|
2011-06-13 17:34:54 -07:00
|
|
|
print_expr(s, lhs);
|
|
|
|
space(s.s);
|
|
|
|
word_space(s, "<->");
|
|
|
|
print_expr(s, rhs);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_assign_op(?op, ?lhs, ?rhs)) {
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, lhs);
|
|
|
|
space(s.s);
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, ast::binop_to_str(op));
|
2011-05-28 19:16:18 -07:00
|
|
|
word_space(s, "=");
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, rhs);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_send(?lhs, ?rhs)) {
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, lhs);
|
|
|
|
space(s.s);
|
2011-05-28 19:16:18 -07:00
|
|
|
word_space(s, "<|");
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, rhs);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_recv(?lhs, ?rhs)) {
|
2011-06-18 20:16:30 -07:00
|
|
|
print_expr(s, lhs);
|
2011-05-27 11:59:19 -07:00
|
|
|
space(s.s);
|
2011-05-28 19:16:18 -07:00
|
|
|
word_space(s, "|>");
|
2011-06-18 20:16:30 -07:00
|
|
|
print_expr(s, rhs);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_field(?expr, ?id)) {
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, expr);
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, ".");
|
|
|
|
word(s.s, id);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_index(?expr, ?index)) {
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, expr);
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, ".");
|
2011-05-28 19:16:18 -07:00
|
|
|
popen(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, index);
|
|
|
|
pclose(s);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_path(?path)) { print_path(s, path); }
|
2011-07-01 14:33:15 -04:00
|
|
|
case (ast::expr_fail(?maybe_fail_val)) {
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, "fail");
|
2011-07-01 14:33:15 -04:00
|
|
|
alt (maybe_fail_val) {
|
|
|
|
case (some(?expr)) { word(s.s, " "); print_expr(s, expr); }
|
2011-06-15 11:19:50 -07:00
|
|
|
case (_) { }
|
2011-06-08 03:58:52 -04:00
|
|
|
}
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_break) { word(s.s, "break"); }
|
|
|
|
case (ast::expr_cont) { word(s.s, "cont"); }
|
|
|
|
case (ast::expr_ret(?result)) {
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, "ret");
|
2011-03-24 16:33:20 +01:00
|
|
|
alt (result) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (some(?expr)) { word(s.s, " "); print_expr(s, expr); }
|
|
|
|
case (_) { }
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_put(?result)) {
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, "put");
|
2011-03-24 16:33:20 +01:00
|
|
|
alt (result) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (some(?expr)) { word(s.s, " "); print_expr(s, expr); }
|
|
|
|
case (_) { }
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_be(?result)) {
|
2011-05-28 19:16:18 -07:00
|
|
|
word_nbsp(s, "be");
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, result);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_log(?lvl, ?expr)) {
|
2011-04-19 11:21:23 +02:00
|
|
|
alt (lvl) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (1) { word_nbsp(s, "log"); }
|
|
|
|
case (0) { word_nbsp(s, "log_err"); }
|
2011-04-19 11:21:23 +02:00
|
|
|
}
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, expr);
|
|
|
|
}
|
2011-06-28 16:29:37 -07:00
|
|
|
case (ast::expr_check(?m, ?expr)) {
|
|
|
|
alt (m) {
|
|
|
|
case (ast::unchecked) {
|
|
|
|
word_nbsp(s, "claim");
|
|
|
|
}
|
|
|
|
case (ast::checked) {
|
|
|
|
word_nbsp(s, "check");
|
|
|
|
}
|
|
|
|
}
|
2011-05-28 19:16:18 -07:00
|
|
|
popen(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, expr);
|
|
|
|
pclose(s);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_assert(?expr)) {
|
2011-05-28 19:16:18 -07:00
|
|
|
word_nbsp(s, "assert");
|
|
|
|
popen(s);
|
2011-05-02 17:47:24 -07:00
|
|
|
print_expr(s, expr);
|
|
|
|
pclose(s);
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_ext(?path, ?args, ?body, _)) {
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, "#");
|
2011-03-24 16:33:20 +01:00
|
|
|
print_path(s, path);
|
2011-06-14 17:09:01 +02:00
|
|
|
if (vec::len(args) > 0u) {
|
2011-03-24 16:33:20 +01:00
|
|
|
popen(s);
|
2011-05-29 07:43:33 +00:00
|
|
|
commasep_exprs(s, inconsistent, args);
|
2011-03-24 16:33:20 +01:00
|
|
|
pclose(s);
|
|
|
|
}
|
|
|
|
// FIXME: extension 'body'
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-28 15:54:16 -07:00
|
|
|
case (ast::expr_port(?ot)) {
|
2011-06-30 17:58:19 -07:00
|
|
|
word(s.s, "port");
|
2011-06-28 15:54:16 -07:00
|
|
|
alt(ot) {
|
|
|
|
case(some(?t)) {
|
|
|
|
word(s.s, "[");
|
|
|
|
print_type(s, *t);
|
|
|
|
word(s.s, "]");
|
|
|
|
}
|
|
|
|
case(none) {}
|
|
|
|
}
|
2011-06-30 17:58:19 -07:00
|
|
|
popen(s); pclose(s);
|
2011-06-28 15:54:16 -07:00
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_chan(?expr)) {
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, "chan");
|
2011-05-28 19:16:18 -07:00
|
|
|
popen(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, expr);
|
|
|
|
pclose(s);
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
2011-07-06 11:29:22 -07:00
|
|
|
case (ast::expr_anon_obj(_, _)) {
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, "anon obj");
|
2011-06-15 12:29:34 -07:00
|
|
|
// FIXME (issue #499): nicer pretty-printing of anon objs
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-05-11 16:41:25 -07:00
|
|
|
}
|
2011-03-24 23:03:12 -04:00
|
|
|
}
|
2011-07-05 10:38:35 +02:00
|
|
|
s.ann.post(ann_node);
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s);
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
|
|
|
|
2011-05-31 10:58:30 -07:00
|
|
|
fn print_decl(&ps s, &@ast::decl decl) {
|
2011-03-24 16:33:20 +01:00
|
|
|
maybe_print_comment(s, decl.span.lo);
|
|
|
|
alt (decl.node) {
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::decl_local(?loc)) {
|
2011-06-20 10:45:05 -04:00
|
|
|
space_if_not_hardbreak(s);
|
2011-06-01 15:29:38 -07:00
|
|
|
ibox(s, indent_unit);
|
2011-06-16 15:58:25 -07:00
|
|
|
alt (loc.node.ty) {
|
2011-06-02 17:18:43 -07:00
|
|
|
case (some(?ty)) {
|
2011-05-28 19:16:18 -07:00
|
|
|
word_nbsp(s, "let");
|
2011-05-31 10:58:30 -07:00
|
|
|
print_type(s, *ty);
|
2011-03-24 16:33:20 +01:00
|
|
|
space(s.s);
|
|
|
|
}
|
|
|
|
case (_) {
|
2011-05-28 19:16:18 -07:00
|
|
|
word_nbsp(s, "auto");
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-03-24 23:03:12 -04:00
|
|
|
}
|
2011-06-16 15:58:25 -07:00
|
|
|
word(s.s, loc.node.ident);
|
|
|
|
alt (loc.node.init) {
|
2011-06-02 17:18:43 -07:00
|
|
|
case (some(?init)) {
|
2011-03-24 16:33:20 +01:00
|
|
|
space(s.s);
|
|
|
|
alt (init.op) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (ast::init_assign) { word_space(s, "="); }
|
|
|
|
case (ast::init_move) { word_space(s, "<-"); }
|
|
|
|
case (ast::init_recv) { word_space(s, "|>"); }
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
|
|
|
print_expr(s, init.expr);
|
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
case (_) { }
|
2011-03-24 23:03:12 -04:00
|
|
|
}
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s);
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
case (ast::decl_item(?item)) { print_item(s, item); }
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
fn print_ident(&ps s, &ast::ident ident) { word(s.s, ident); }
|
2011-04-05 14:18:44 -07:00
|
|
|
|
2011-06-13 17:04:15 -07:00
|
|
|
fn print_for_decl(&ps s, @ast::local loc) {
|
2011-06-14 17:09:01 +02:00
|
|
|
print_type(s, *option::get(loc.node.ty));
|
2011-06-13 17:04:15 -07:00
|
|
|
space(s.s);
|
|
|
|
word(s.s, loc.node.ident);
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
|
|
|
|
2011-05-31 10:58:30 -07:00
|
|
|
fn print_path(&ps s, &ast::path path) {
|
2011-03-24 16:33:20 +01:00
|
|
|
maybe_print_comment(s, path.span.lo);
|
|
|
|
auto first = true;
|
|
|
|
for (str id in path.node.idents) {
|
2011-06-15 11:19:50 -07:00
|
|
|
if (first) { first = false; } else { word(s.s, "::"); }
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, id);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-07-05 16:23:07 -07:00
|
|
|
if (ivec::len(path.node.types) > 0u) {
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, "[");
|
2011-07-05 16:23:07 -07:00
|
|
|
commasep_ivec(s, inconsistent, path.node.types, print_boxed_type);
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, "]");
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
|
|
|
|
2011-05-31 10:58:30 -07:00
|
|
|
fn print_pat(&ps s, &@ast::pat pat) {
|
2011-03-24 16:33:20 +01:00
|
|
|
maybe_print_comment(s, pat.span.lo);
|
2011-07-05 10:38:35 +02:00
|
|
|
auto ann_node = node_pat(s, pat);
|
|
|
|
s.ann.pre(ann_node);
|
2011-03-24 16:33:20 +01:00
|
|
|
alt (pat.node) {
|
2011-07-04 21:53:33 +02:00
|
|
|
case (ast::pat_wild) { word(s.s, "_"); }
|
|
|
|
case (ast::pat_bind(?id)) { word(s.s, "?" + id); }
|
|
|
|
case (ast::pat_lit(?lit)) { print_literal(s, lit); }
|
|
|
|
case (ast::pat_tag(?path, ?args)) {
|
2011-03-24 16:33:20 +01:00
|
|
|
print_path(s, path);
|
2011-06-14 17:09:01 +02:00
|
|
|
if (vec::len(args) > 0u) {
|
2011-05-28 19:16:18 -07:00
|
|
|
popen(s);
|
2011-06-14 17:09:01 +02:00
|
|
|
commasep(s, inconsistent, args, print_pat);
|
2011-03-24 16:33:20 +01:00
|
|
|
pclose(s);
|
|
|
|
}
|
|
|
|
}
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
2011-07-05 10:38:35 +02:00
|
|
|
s.ann.post(ann_node);
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
|
|
|
|
2011-06-01 13:18:16 -07:00
|
|
|
fn print_fn(&ps s, ast::fn_decl decl, ast::proto proto, str name,
|
2011-05-31 10:58:30 -07:00
|
|
|
vec[ast::ty_param] typarams) {
|
2011-05-04 11:28:13 -07:00
|
|
|
alt (decl.purity) {
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::impure_fn) {
|
2011-06-01 13:18:16 -07:00
|
|
|
if (proto == ast::proto_iter) {
|
|
|
|
head(s, "iter");
|
2011-06-15 11:19:50 -07:00
|
|
|
} else { head(s, "fn"); }
|
2011-05-04 11:28:13 -07:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
case (_) { head(s, "pred"); }
|
2011-05-04 11:28:13 -07:00
|
|
|
}
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, name);
|
2011-03-24 16:33:20 +01:00
|
|
|
print_type_params(s, typarams);
|
2011-06-14 15:20:04 +02:00
|
|
|
print_fn_args_and_ret(s, decl);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_fn_args_and_ret(&ps s, &ast::fn_decl decl) {
|
2011-03-24 16:33:20 +01:00
|
|
|
popen(s);
|
2011-05-31 10:58:30 -07:00
|
|
|
fn print_arg(&ps s, &ast::arg x) {
|
2011-06-01 15:29:38 -07:00
|
|
|
ibox(s, indent_unit);
|
2011-06-10 12:03:50 +02:00
|
|
|
print_alias(s, x.mode);
|
2011-05-31 10:58:30 -07:00
|
|
|
print_type(s, *x.ty);
|
2011-03-24 16:33:20 +01:00
|
|
|
space(s.s);
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, x.ident);
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-14 17:09:01 +02:00
|
|
|
commasep(s, inconsistent, decl.inputs, print_arg);
|
2011-03-24 16:33:20 +01:00
|
|
|
pclose(s);
|
|
|
|
maybe_print_comment(s, decl.output.span.lo);
|
2011-05-12 17:24:54 +02:00
|
|
|
if (decl.output.node != ast::ty_nil) {
|
2011-06-20 10:45:05 -04:00
|
|
|
space_if_not_hardbreak(s);
|
2011-05-28 19:16:18 -07:00
|
|
|
word_space(s, "->");
|
2011-05-31 10:58:30 -07:00
|
|
|
print_type(s, *decl.output);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
|
|
|
|
2011-06-10 12:03:50 +02:00
|
|
|
fn print_alias(&ps s, ast::mode m) {
|
|
|
|
alt (m) {
|
|
|
|
case (ast::alias(true)) { word_space(s, "&mutable"); }
|
|
|
|
case (ast::alias(false)) { word(s.s, "&"); }
|
2011-06-15 11:19:50 -07:00
|
|
|
case (ast::val) { }
|
2011-06-10 12:03:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-31 10:58:30 -07:00
|
|
|
fn print_type_params(&ps s, &vec[ast::ty_param] params) {
|
2011-06-14 17:09:01 +02:00
|
|
|
if (vec::len(params) > 0u) {
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, "[");
|
2011-06-15 11:19:50 -07:00
|
|
|
fn printParam(&ps s, &ast::ty_param param) { word(s.s, param); }
|
2011-06-14 17:09:01 +02:00
|
|
|
commasep(s, inconsistent, params, printParam);
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, "]");
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
|
|
|
|
2011-06-14 18:53:12 -07:00
|
|
|
fn print_meta_item(&ps s, &@ast::meta_item item) {
|
|
|
|
ibox(s, indent_unit);
|
2011-06-21 14:23:16 -07:00
|
|
|
alt (item.node) {
|
2011-06-27 23:02:02 -07:00
|
|
|
case (ast::meta_word(?name)) {
|
|
|
|
word(s.s, name);
|
|
|
|
}
|
|
|
|
case (ast::meta_name_value(?name, ?value)) {
|
|
|
|
word_space(s, name);
|
2011-06-21 14:23:16 -07:00
|
|
|
word_space(s, "=");
|
2011-07-05 17:01:23 -07:00
|
|
|
print_literal(s, @value);
|
2011-06-21 14:23:16 -07:00
|
|
|
}
|
2011-06-27 23:02:02 -07:00
|
|
|
case (ast::meta_list(?name, ?items)) {
|
|
|
|
word(s.s, name);
|
|
|
|
popen(s);
|
2011-07-05 17:57:34 -07:00
|
|
|
commasep_ivec(s, consistent, items, print_meta_item);
|
2011-06-27 23:02:02 -07:00
|
|
|
pclose(s);
|
2011-06-21 14:23:16 -07:00
|
|
|
}
|
|
|
|
}
|
2011-06-14 18:53:12 -07:00
|
|
|
end(s);
|
|
|
|
}
|
|
|
|
|
2011-05-31 10:58:30 -07:00
|
|
|
fn print_view_item(&ps s, &@ast::view_item item) {
|
2011-06-20 16:15:11 +00:00
|
|
|
hardbreak_if_not_bol(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
maybe_print_comment(s, item.span.lo);
|
|
|
|
alt (item.node) {
|
2011-06-19 22:41:21 +02:00
|
|
|
case (ast::view_item_use(?id, ?mta, _)) {
|
2011-05-28 19:16:18 -07:00
|
|
|
head(s, "use");
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, id);
|
2011-07-05 17:57:34 -07:00
|
|
|
if (ivec::len(mta) > 0u) {
|
2011-03-24 16:33:20 +01:00
|
|
|
popen(s);
|
2011-07-05 17:57:34 -07:00
|
|
|
commasep_ivec(s, consistent, mta, print_meta_item);
|
2011-03-24 16:33:20 +01:00
|
|
|
pclose(s);
|
|
|
|
}
|
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
case (ast::view_item_import(?id, ?ids, _)) {
|
2011-05-28 19:16:18 -07:00
|
|
|
head(s, "import");
|
2011-06-15 11:19:50 -07:00
|
|
|
if (!str::eq(id, ids.(vec::len(ids) - 1u))) {
|
2011-05-28 19:16:18 -07:00
|
|
|
word_space(s, id);
|
|
|
|
word_space(s, "=");
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
|
|
|
auto first = true;
|
|
|
|
for (str elt in ids) {
|
2011-06-15 11:19:50 -07:00
|
|
|
if (first) { first = false; } else { word(s.s, "::"); }
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, elt);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
case (ast::view_item_import_glob(?ids, _)) {
|
2011-06-01 11:08:44 -07:00
|
|
|
head(s, "import");
|
|
|
|
auto first = true;
|
|
|
|
for (str elt in ids) {
|
2011-06-15 11:19:50 -07:00
|
|
|
if (first) { first = false; } else { word(s.s, "::"); }
|
2011-06-01 11:08:44 -07:00
|
|
|
word(s.s, elt);
|
|
|
|
}
|
|
|
|
word(s.s, "::*");
|
|
|
|
}
|
2011-06-19 22:41:21 +02:00
|
|
|
case (ast::view_item_export(?id, _)) {
|
2011-05-28 19:16:18 -07:00
|
|
|
head(s, "export");
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, id);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
2011-05-30 14:10:54 -07:00
|
|
|
word(s.s, ";");
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s); // end inner head-block
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s); // end outer head-block
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-03-04 07:22:43 +01:00
|
|
|
// FIXME: The fact that this builds up the table anew for every call is
|
|
|
|
// not good. Eventually, table should be a const.
|
2011-05-12 17:24:54 +02:00
|
|
|
fn operator_prec(ast::binop op) -> int {
|
2011-07-05 11:48:19 +02:00
|
|
|
for (parse::parser::op_spec spec in parse::parser::prec_table()) {
|
2011-06-15 11:19:50 -07:00
|
|
|
if (spec.op == op) { ret spec.prec; }
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
|
|
|
fail;
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
|
|
|
|
2011-05-31 10:58:30 -07:00
|
|
|
fn print_maybe_parens(&ps s, &@ast::expr expr, int outer_prec) {
|
2011-03-24 16:33:20 +01:00
|
|
|
auto add_them;
|
|
|
|
alt (expr.node) {
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_binary(?op, _, _)) {
|
2011-03-24 16:33:20 +01:00
|
|
|
add_them = operator_prec(op) < outer_prec;
|
|
|
|
}
|
2011-06-21 22:16:40 +02:00
|
|
|
case (ast::expr_cast(_, _)) {
|
2011-07-05 11:48:19 +02:00
|
|
|
add_them = parse::parser::as_prec < outer_prec;
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-23 23:14:40 -07:00
|
|
|
case (ast::expr_ternary(_, _, _)) {
|
2011-07-05 11:48:19 +02:00
|
|
|
add_them = parse::parser::ternary_prec < outer_prec;
|
2011-06-23 23:14:40 -07:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
case (_) { add_them = false; }
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
if (add_them) { popen(s); }
|
2011-03-24 16:33:20 +01:00
|
|
|
print_expr(s, expr);
|
2011-06-15 11:19:50 -07:00
|
|
|
if (add_them) { pclose(s); }
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
|
|
|
|
2011-06-15 10:25:23 -07:00
|
|
|
fn print_mutability(&ps s, &ast::mutability mut) {
|
|
|
|
alt (mut) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (ast::mut) { word_nbsp(s, "mutable"); }
|
2011-05-28 19:16:18 -07:00
|
|
|
case (ast::maybe_mut) { word_nbsp(s, "mutable?"); }
|
2011-06-15 11:19:50 -07:00
|
|
|
case (ast::imm) {/* nothing */ }
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-15 10:25:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn print_mt(&ps s, &ast::mt mt) {
|
|
|
|
print_mutability(s, mt.mut);
|
2011-05-31 10:58:30 -07:00
|
|
|
print_type(s, *mt.ty);
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
|
|
|
|
2011-05-31 10:58:30 -07:00
|
|
|
fn print_ty_fn(&ps s, &ast::proto proto, &option::t[str] id,
|
|
|
|
&vec[ast::ty_arg] inputs, &@ast::ty output,
|
2011-06-09 09:48:16 -07:00
|
|
|
&ast::controlflow cf, &vec[@ast::constr] constrs) {
|
2011-06-01 15:29:38 -07:00
|
|
|
ibox(s, indent_unit);
|
2011-06-15 11:19:50 -07:00
|
|
|
if (proto == ast::proto_fn) {
|
|
|
|
word(s.s, "fn");
|
|
|
|
} else { word(s.s, "iter"); }
|
2011-03-24 16:33:20 +01:00
|
|
|
alt (id) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (some(?id)) { word(s.s, " "); word(s.s, id); }
|
|
|
|
case (_) { }
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-02 17:36:28 -07:00
|
|
|
zerobreak(s.s);
|
2011-05-28 19:16:18 -07:00
|
|
|
popen(s);
|
2011-05-31 10:58:30 -07:00
|
|
|
fn print_arg(&ps s, &ast::ty_arg input) {
|
2011-06-10 12:03:50 +02:00
|
|
|
print_alias(s, input.node.mode);
|
2011-06-03 15:26:03 -07:00
|
|
|
print_type(s, *input.node.ty);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-14 17:09:01 +02:00
|
|
|
commasep(s, inconsistent, inputs, print_arg);
|
2011-03-24 16:33:20 +01:00
|
|
|
pclose(s);
|
|
|
|
maybe_print_comment(s, output.span.lo);
|
2011-05-12 17:24:54 +02:00
|
|
|
if (output.node != ast::ty_nil) {
|
2011-06-20 10:45:05 -04:00
|
|
|
space_if_not_hardbreak(s);
|
2011-06-01 15:29:38 -07:00
|
|
|
ibox(s, indent_unit);
|
2011-05-28 19:16:18 -07:00
|
|
|
word_space(s, "->");
|
2011-05-20 14:15:39 -07:00
|
|
|
alt (cf) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (ast::return) { print_type(s, *output); }
|
|
|
|
case (ast::noreturn) { word_nbsp(s, "!"); }
|
2011-05-20 14:15:39 -07:00
|
|
|
}
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
2011-06-15 15:14:30 -07:00
|
|
|
word_space(s, ast_constrs_str(constrs));
|
2011-06-01 15:29:38 -07:00
|
|
|
end(s);
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
|
|
|
|
2011-07-05 11:48:19 +02:00
|
|
|
fn maybe_print_trailing_comment(&ps s, codemap::span span,
|
2011-06-03 17:24:05 -07:00
|
|
|
option::t[uint] next_pos) {
|
2011-05-30 16:22:58 -07:00
|
|
|
auto cm;
|
2011-06-15 11:19:50 -07:00
|
|
|
alt (s.cm) { case (some(?ccm)) { cm = ccm; } case (_) { ret; } }
|
2011-03-24 16:33:20 +01:00
|
|
|
alt (next_comment(s)) {
|
2011-06-02 17:18:43 -07:00
|
|
|
case (some(?cmnt)) {
|
2011-05-30 17:34:59 -07:00
|
|
|
if (cmnt.style != lexer::trailing) { ret; }
|
2011-05-30 16:22:58 -07:00
|
|
|
auto span_line = codemap::lookup_pos(cm, span.hi);
|
|
|
|
auto comment_line = codemap::lookup_pos(cm, cmnt.pos);
|
2011-06-03 17:24:05 -07:00
|
|
|
auto next = cmnt.pos + 1u;
|
2011-06-15 11:19:50 -07:00
|
|
|
alt (next_pos) { case (none) { } case (some(?p)) { next = p; } }
|
|
|
|
if (span.hi < cmnt.pos && cmnt.pos < next &&
|
2011-06-30 17:58:19 -07:00
|
|
|
span_line.line == comment_line.line) {
|
2011-05-30 14:10:54 -07:00
|
|
|
print_comment(s, cmnt);
|
2011-03-24 16:33:20 +01:00
|
|
|
s.cur_cmnt += 1u;
|
|
|
|
}
|
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
case (_) { }
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-31 10:58:30 -07:00
|
|
|
fn print_remaining_comments(&ps s) {
|
2011-03-24 16:33:20 +01:00
|
|
|
while (true) {
|
|
|
|
alt (next_comment(s)) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (some(?cmnt)) { print_comment(s, cmnt); s.cur_cmnt += 1u; }
|
|
|
|
case (_) { break; }
|
2011-03-24 16:33:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-01 15:29:38 -07:00
|
|
|
fn in_cbox(&ps s) -> bool {
|
|
|
|
auto len = vec::len(s.boxes);
|
|
|
|
if (len == 0u) { ret false; }
|
2011-06-15 11:19:50 -07:00
|
|
|
ret s.boxes.(len - 1u) == pp::consistent;
|
2011-06-01 15:29:38 -07:00
|
|
|
}
|
2011-07-05 11:48:19 +02:00
|
|
|
|
|
|
|
fn print_literal(&ps s, &@ast::lit lit) {
|
|
|
|
maybe_print_comment(s, lit.span.lo);
|
|
|
|
alt (next_lit(s)) {
|
|
|
|
case (some(?lt)) {
|
|
|
|
if (lt.pos == lit.span.lo) {
|
|
|
|
word(s.s, lt.lit);
|
|
|
|
s.cur_lit += 1u;
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case (_) { }
|
|
|
|
}
|
|
|
|
alt (lit.node) {
|
|
|
|
case (ast::lit_str(?st, ?kind)) {
|
|
|
|
if (kind == ast::sk_unique) { word(s.s, "~"); }
|
|
|
|
print_string(s, st);
|
|
|
|
}
|
|
|
|
case (ast::lit_char(?ch)) {
|
|
|
|
word(s.s,
|
|
|
|
"'" + escape_str(str::from_bytes([ch as u8]), '\'') + "'");
|
|
|
|
}
|
2011-07-06 16:46:17 +02:00
|
|
|
case (ast::lit_int(?val)) { word(s.s, int::str(val)); }
|
|
|
|
case (ast::lit_uint(?val)) { word(s.s, uint::str(val) + "u"); }
|
2011-07-05 11:48:19 +02:00
|
|
|
case (ast::lit_float(?fstr)) { word(s.s, fstr); }
|
|
|
|
case (ast::lit_mach_int(?mach, ?val)) {
|
2011-07-06 16:46:17 +02:00
|
|
|
word(s.s, int::str(val as int));
|
2011-07-05 11:48:19 +02:00
|
|
|
word(s.s, ast::ty_mach_to_str(mach));
|
|
|
|
}
|
|
|
|
case (ast::lit_mach_float(?mach, ?val)) {
|
|
|
|
// val is already a str
|
|
|
|
word(s.s, val);
|
|
|
|
word(s.s, ast::ty_mach_to_str(mach));
|
|
|
|
}
|
|
|
|
case (ast::lit_nil) { word(s.s, "()"); }
|
|
|
|
case (ast::lit_bool(?val)) {
|
|
|
|
if (val) { word(s.s, "true"); } else { word(s.s, "false"); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lit_to_str(&@ast::lit l) -> str { be to_str(l, print_literal); }
|
|
|
|
|
|
|
|
fn next_lit(&ps s) -> option::t[lexer::lit] {
|
|
|
|
alt (s.literals) {
|
|
|
|
case (some(?lits)) {
|
|
|
|
if (s.cur_lit < vec::len(lits)) {
|
|
|
|
ret some(lits.(s.cur_lit));
|
|
|
|
} else { ret none[lexer::lit]; }
|
|
|
|
}
|
|
|
|
case (_) { ret none[lexer::lit]; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn maybe_print_comment(&ps s, uint pos) {
|
|
|
|
while (true) {
|
|
|
|
alt (next_comment(s)) {
|
|
|
|
case (some(?cmnt)) {
|
|
|
|
if (cmnt.pos < pos) {
|
|
|
|
print_comment(s, cmnt);
|
|
|
|
s.cur_cmnt += 1u;
|
|
|
|
} else { break; }
|
|
|
|
}
|
|
|
|
case (_) { break; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_comment(&ps s, lexer::cmnt cmnt) {
|
|
|
|
alt (cmnt.style) {
|
|
|
|
case (lexer::mixed) {
|
|
|
|
assert (vec::len(cmnt.lines) == 1u);
|
|
|
|
zerobreak(s.s);
|
|
|
|
word(s.s, cmnt.lines.(0));
|
|
|
|
zerobreak(s.s);
|
|
|
|
}
|
|
|
|
case (lexer::isolated) {
|
|
|
|
pprust::hardbreak_if_not_bol(s);
|
|
|
|
for (str line in cmnt.lines) { word(s.s, line); hardbreak(s.s); }
|
|
|
|
}
|
|
|
|
case (lexer::trailing) {
|
|
|
|
word(s.s, " ");
|
|
|
|
if (vec::len(cmnt.lines) == 1u) {
|
|
|
|
word(s.s, cmnt.lines.(0));
|
|
|
|
hardbreak(s.s);
|
|
|
|
} else {
|
|
|
|
ibox(s, 0u);
|
|
|
|
for (str line in cmnt.lines) {
|
|
|
|
word(s.s, line);
|
|
|
|
hardbreak(s.s);
|
|
|
|
}
|
|
|
|
end(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case (lexer::blank_line) {
|
|
|
|
// We need to do at least one, possibly two hardbreaks.
|
|
|
|
pprust::hardbreak_if_not_bol(s);
|
|
|
|
hardbreak(s.s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_string(&ps s, &str st) {
|
|
|
|
word(s.s, "\"");
|
|
|
|
word(s.s, escape_str(st, '"'));
|
|
|
|
word(s.s, "\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn escape_str(str st, char to_escape) -> str {
|
|
|
|
let str out = "";
|
|
|
|
auto len = str::byte_len(st);
|
|
|
|
auto i = 0u;
|
|
|
|
while (i < len) {
|
|
|
|
alt (st.(i) as char) {
|
|
|
|
case ('\n') { out += "\\n"; }
|
|
|
|
case ('\t') { out += "\\t"; }
|
|
|
|
case ('\r') { out += "\\r"; }
|
|
|
|
case ('\\') { out += "\\\\"; }
|
|
|
|
case (?cur) {
|
|
|
|
if (cur == to_escape) { out += "\\"; }
|
|
|
|
// FIXME some (or all?) non-ascii things should be escaped
|
|
|
|
|
|
|
|
str::push_char(out, cur);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
ret out;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_str[T](&T t, fn(&ps, &T) f) -> str {
|
|
|
|
auto writer = io::string_writer();
|
|
|
|
auto s = rust_printer(writer.get_writer());
|
|
|
|
f(s, t);
|
|
|
|
eof(s.s);
|
|
|
|
ret writer.get_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_comment(&ps s) -> option::t[lexer::cmnt] {
|
|
|
|
alt (s.comments) {
|
|
|
|
case (some(?cmnts)) {
|
|
|
|
if (s.cur_cmnt < vec::len(cmnts)) {
|
|
|
|
ret some(cmnts.(s.cur_cmnt));
|
|
|
|
} else { ret none[lexer::cmnt]; }
|
|
|
|
}
|
|
|
|
case (_) { ret none[lexer::cmnt]; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn constr_args_to_str[T](fn(&T) -> str f,
|
2011-07-01 16:42:16 -07:00
|
|
|
&(@ast::constr_arg_general[T])[] args) -> str {
|
2011-07-05 11:48:19 +02:00
|
|
|
auto comma = false;
|
|
|
|
auto s = "(";
|
|
|
|
for (@ast::constr_arg_general[T] a in args) {
|
|
|
|
if (comma) { s += ", "; } else { comma = true; }
|
|
|
|
s += constr_arg_to_str[T](f, a.node);
|
|
|
|
}
|
|
|
|
s += ")";
|
|
|
|
ret s;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn constr_arg_to_str[T](fn(&T) -> str f, &ast::constr_arg_general_[T] c) ->
|
|
|
|
str {
|
|
|
|
alt (c) {
|
|
|
|
case (ast::carg_base) { ret "*"; }
|
|
|
|
case (ast::carg_ident(?i)) { ret f(i); }
|
|
|
|
case (ast::carg_lit(?l)) { ret lit_to_str(l); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// needed b/c constr_args_to_str needs
|
|
|
|
// something that takes an alias
|
|
|
|
// (argh)
|
2011-07-06 16:46:17 +02:00
|
|
|
fn uint_to_str(&uint i) -> str { ret uint::str(i); }
|
2011-07-05 11:48:19 +02:00
|
|
|
|
|
|
|
fn ast_constr_to_str(&@ast::constr c) -> str {
|
2011-07-01 16:42:16 -07:00
|
|
|
// TODO: Remove this vec->ivec conversion.
|
|
|
|
auto cag_ivec = ~[];
|
|
|
|
for (@ast::constr_arg_general[uint] cag in c.node.args) {
|
|
|
|
cag_ivec += ~[cag];
|
|
|
|
}
|
2011-07-05 11:48:19 +02:00
|
|
|
ret ast::path_to_str(c.node.path) +
|
2011-07-01 16:42:16 -07:00
|
|
|
constr_args_to_str(uint_to_str, cag_ivec);
|
2011-07-05 11:48:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ast_constrs_str(&vec[@ast::constr] constrs) -> str {
|
|
|
|
auto s = "";
|
|
|
|
auto colon = true;
|
|
|
|
for (@ast::constr c in constrs) {
|
|
|
|
if (colon) { s += " : "; colon = false; } else { s += ", "; }
|
|
|
|
s += ast_constr_to_str(c);
|
|
|
|
}
|
|
|
|
ret s;
|
|
|
|
}
|
|
|
|
|
Handle nested items correctly in typestate_check
Summary says it all. Actually, only nested objects and functions
are handled, but that's better than before. The fold that I was using
before to traverse a crate wasn't working correctly, because annotations
have to reflect the number of local variables of the nearest enclosing
function (in turn, because annotations are represented as bit vectors).
The fold was traversing the AST in the wrong order, first filling in
the annotations correctly, but then re-traversing them with the bit
vector length for any outer nested functions, and so on.
Remedying this required writing a lot of tedious boilerplate code
because I scrapped the idea of using a fold altogether.
I also made typestate_check handle unary, field, alt, and fail.
Also, some miscellaneous changes:
* added annotations to blocks in typeck
* fix pprust so it can handle spawn
* added more logging functions in util.common
* fixed _vec.or
* added maybe and from_maybe in option
* removed fold_block field from ast_fold, since it was never used
2011-04-18 15:33:10 -07:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|
|
|
|
//
|