pprust.rs 51.1 KB
Newer Older
1
import parse::classify::*;
2
import parse::comments;
3
import parse::lexer;
4
import codemap::codemap;
5
import pp::{break_offset, word, printer,
G
Graydon Hoare 已提交
6 7
            space, zerobreak, hardbreak, breaks, consistent,
            inconsistent, eof};
8
import diagnostic;
9

10
// The ps is stored here to prevent recursive type.
P
Patrick Walton 已提交
11
enum ann_node {
P
Patrick Walton 已提交
12 13 14 15
    node_block(ps, ast::blk),
    node_item(ps, @ast::item),
    node_expr(ps, @ast::expr),
    node_pat(ps, @ast::pat),
16
}
17
type pp_ann = {pre: fn@(ann_node), post: fn@(ann_node)};
18 19

fn no_ann() -> pp_ann {
20
    fn ignore(_node: ann_node) { }
M
Marijn Haverbeke 已提交
21
    ret {pre: ignore, post: ignore};
22 23 24
}

type ps =
M
Marijn Haverbeke 已提交
25
    @{s: pp::printer,
T
Tim Chevalier 已提交
26
      cm: option<codemap>,
27 28
      comments: option<[comments::cmnt]>,
      literals: option<[comments::lit]>,
G
Graydon Hoare 已提交
29 30 31
      mut cur_cmnt: uint,
      mut cur_lit: uint,
      mut boxes: [pp::breaks],
M
Marijn Haverbeke 已提交
32
      ann: pp_ann};
33

34
fn ibox(s: ps, u: uint) { s.boxes += [pp::inconsistent]; pp::ibox(s.s, u); }
35

36
fn end(s: ps) { vec::pop(s.boxes); pp::end(s.s); }
37

B
Brian Anderson 已提交
38
fn rust_printer(writer: io::writer) -> ps {
B
Brian Anderson 已提交
39
    let boxes: [pp::breaks] = [];
M
Marijn Haverbeke 已提交
40
    ret @{s: pp::mk_printer(writer, default_columns),
41
          cm: none::<codemap>,
42 43
          comments: none::<[comments::cmnt]>,
          literals: none::<[comments::lit]>,
G
Graydon Hoare 已提交
44 45 46
          mut cur_cmnt: 0u,
          mut cur_lit: 0u,
          mut boxes: boxes,
M
Marijn Haverbeke 已提交
47
          ann: no_ann()};
48 49
}

M
Marijn Haverbeke 已提交
50 51
const indent_unit: uint = 4u;
const alt_indent_unit: uint = 2u;
52

M
Marijn Haverbeke 已提交
53
const default_columns: uint = 78u;
54

55 56 57
// Requires you to pass an input filename and reader so that
// it can scan the input text for comments and literals to
// copy forward.
58
fn print_crate(cm: codemap, span_diagnostic: diagnostic::span_handler,
59
               crate: @ast::crate, filename: str, in: io::reader,
60
               out: io::writer, ann: pp_ann) {
B
Brian Anderson 已提交
61
    let boxes: [pp::breaks] = [];
62
    let r = comments::gather_comments_and_literals(span_diagnostic,
63
                                                   filename, in);
M
Marijn Haverbeke 已提交
64 65 66 67 68
    let s =
        @{s: pp::mk_printer(out, default_columns),
          cm: some(cm),
          comments: some(r.cmnts),
          literals: some(r.lits),
G
Graydon Hoare 已提交
69 70 71
          mut cur_cmnt: 0u,
          mut cur_lit: 0u,
          mut boxes: boxes,
M
Marijn Haverbeke 已提交
72
          ann: ann};
73 74 75 76
    print_crate_(s, crate);
}

fn print_crate_(s: ps, &&crate: @ast::crate) {
77
    print_mod(s, crate.node.module, crate.node.attrs);
78
    print_remaining_comments(s);
G
Graydon Hoare 已提交
79
    eof(s.s);
M
Marijn Haverbeke 已提交
80 81
}

82
fn ty_to_str(ty: @ast::ty) -> str { be to_str(ty, print_type); }
83

84
fn pat_to_str(pat: @ast::pat) -> str { be to_str(pat, print_pat); }
85

86
fn expr_to_str(e: @ast::expr) -> str { be to_str(e, print_expr); }
87

88
fn stmt_to_str(s: ast::stmt) -> str { be to_str(s, print_stmt); }
89

90
fn item_to_str(i: @ast::item) -> str { be to_str(i, print_item); }
91

92 93
fn attr_to_str(i: ast::attribute) -> str { be to_str(i, print_attribute); }

94 95 96 97
fn typarams_to_str(tps: [ast::ty_param]) -> str {
    be to_str(tps, print_type_params)
}

M
Marijn Haverbeke 已提交
98
fn path_to_str(&&p: @ast::path) -> str {
99 100
    be to_str(p, bind print_path(_, _, false));
}
101

102 103
fn fun_to_str(decl: ast::fn_decl, name: ast::ident,
              params: [ast::ty_param]) -> str {
104
    let buffer = io::mem_buffer();
M
Marijn Haverbeke 已提交
105
    let s = rust_printer(io::mem_buffer_writer(buffer));
106
    print_fn(s, decl, name, params);
B
Brian Anderson 已提交
107 108
    end(s); // Close the head box
    end(s); // Close the outer box
G
Graydon Hoare 已提交
109
    eof(s.s);
M
Marijn Haverbeke 已提交
110
    io::mem_buffer_str(buffer)
111 112
}

B
Brian Anderson 已提交
113 114 115 116
#[test]
fn test_fun_to_str() {
    let decl: ast::fn_decl = {
        inputs: [],
117 118 119
        output: @{id: 0,
                  node: ast::ty_nil,
                  span: ast_util::dummy_sp()},
B
Brian Anderson 已提交
120 121 122 123 124 125 126
        purity: ast::impure_fn,
        cf: ast::return_val,
        constraints: []
    };
    assert fun_to_str(decl, "a", []) == "fn a()";
}

B
Brian Anderson 已提交
127
fn res_to_str(decl: ast::fn_decl, name: ast::ident,
128
              params: [ast::ty_param], rp: ast::region_param) -> str {
129
    let buffer = io::mem_buffer();
B
Brian Anderson 已提交
130
    let s = rust_printer(io::mem_buffer_writer(buffer));
131
    print_res(s, decl, name, params, rp);
B
Brian Anderson 已提交
132 133 134 135 136 137 138 139 140 141
    end(s); // Close the head box
    end(s); // Close the outer box
    eof(s.s);
    io::mem_buffer_str(buffer)
}

#[test]
fn test_res_to_str() {
    let decl: ast::fn_decl = {
        inputs: [{
142
            mode: ast::expl(ast::by_val),
143 144 145
            ty: @{id: 0,
                  node: ast::ty_nil,
                  span: ast_util::dummy_sp()},
B
Brian Anderson 已提交
146 147 148
            ident: "b",
            id: 0
        }],
149 150 151
        output: @{id: 0,
                  node: ast::ty_nil,
                  span: ast_util::dummy_sp()},
B
Brian Anderson 已提交
152 153 154 155
        purity: ast::impure_fn,
        cf: ast::return_val,
        constraints: []
    };
156
    assert res_to_str(decl, "a", []) == "resource a(b: ())";
B
Brian Anderson 已提交
157 158
}

159
fn block_to_str(blk: ast::blk) -> str {
160
    let buffer = io::mem_buffer();
M
Marijn Haverbeke 已提交
161
    let s = rust_printer(io::mem_buffer_writer(buffer));
G
Graydon Hoare 已提交
162
    // containing cbox, will be closed by print-block at }
163
    cbox(s, indent_unit);
G
Graydon Hoare 已提交
164
    // head-ibox, will be closed by print-block after {
165
    ibox(s, 0u);
166
    print_block(s, blk);
G
Graydon Hoare 已提交
167
    eof(s.s);
M
Marijn Haverbeke 已提交
168
    io::mem_buffer_str(buffer)
169 170
}

171
fn meta_item_to_str(mi: ast::meta_item) -> str {
172 173 174
    ret to_str(@mi, print_meta_item);
}

175
fn attribute_to_str(attr: ast::attribute) -> str {
176 177 178
    be to_str(attr, print_attribute);
}

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
fn variant_to_str(var: ast::variant) -> str {
    be to_str(var, print_variant);
}

#[test]
fn test_variant_to_str() {
    let var = ast_util::respan(ast_util::dummy_sp(), {
        name: "principle_skinner",
        attrs: [],
        args: [],
        id: 0,
        disr_expr: none
    });

    let varstr = variant_to_str(var);
    assert varstr == "principle_skinner";
}

197
fn cbox(s: ps, u: uint) { s.boxes += [pp::consistent]; pp::cbox(s.s, u); }
198

199
fn box(s: ps, u: uint, b: pp::breaks) { s.boxes += [b]; pp::box(s.s, u, b); }
200

201
fn nbsp(s: ps) { word(s.s, " "); }
202

203
fn word_nbsp(s: ps, w: str) { word(s.s, w); nbsp(s); }
G
Graydon Hoare 已提交
204

205
fn word_space(s: ps, w: str) { word(s.s, w); space(s.s); }
G
Graydon Hoare 已提交
206

207
fn popen(s: ps) { word(s.s, "("); }
G
Graydon Hoare 已提交
208

209
fn pclose(s: ps) { word(s.s, ")"); }
G
Graydon Hoare 已提交
210

211
fn head(s: ps, w: str) {
G
Graydon Hoare 已提交
212
    // outer-box is consistent
213
    cbox(s, indent_unit);
G
Graydon Hoare 已提交
214
    // head-box is inconsistent
K
Kevin Cantu 已提交
215
    ibox(s, str::len(w) + 1u);
G
Graydon Hoare 已提交
216
    // keyword that starts the head
G
Graydon Hoare 已提交
217
    word_nbsp(s, w);
G
Graydon Hoare 已提交
218 219
}

220
fn bopen(s: ps) {
B
Brian Anderson 已提交
221
    word(s.s, "{");
222
    end(s); // close the head-box
223
}
G
Graydon Hoare 已提交
224

225
fn bclose_(s: ps, span: codemap::span, indented: uint) {
226
    maybe_print_comment(s, span.hi);
227
    break_offset_if_not_bol(s, 1u, -(indented as int));
B
Brian Anderson 已提交
228
    word(s.s, "}");
229
    end(s); // close the outer-box
230
}
231
fn bclose(s: ps, span: codemap::span) { bclose_(s, span, indent_unit); }
G
Graydon Hoare 已提交
232

233
fn is_begin(s: ps) -> bool {
B
Brian Anderson 已提交
234
    alt s.s.last_token() { pp::BEGIN(_) { true } _ { false } }
235 236
}

237
fn is_end(s: ps) -> bool {
238
    alt s.s.last_token() { pp::END { true } _ { false } }
239 240
}

241
fn is_bol(s: ps) -> bool {
M
Marijn Haverbeke 已提交
242
    ret s.s.last_token() == pp::EOF ||
B
Brian Anderson 已提交
243
            s.s.last_token() == pp::hardbreak_tok();
244 245
}

246 247 248 249 250 251
fn in_cbox(s: ps) -> bool {
    let len = vec::len(s.boxes);
    if len == 0u { ret false; }
    ret s.boxes[len - 1u] == pp::consistent;
}

252 253 254
fn hardbreak_if_not_bol(s: ps) { if !is_bol(s) { hardbreak(s.s); } }
fn space_if_not_bol(s: ps) { if !is_bol(s) { space(s.s); } }
fn break_offset_if_not_bol(s: ps, n: uint, off: int) {
M
Marijn Haverbeke 已提交
255
    if !is_bol(s) {
256 257
        break_offset(s.s, n, off);
    } else {
M
Marijn Haverbeke 已提交
258
        if off != 0 && s.s.last_token() == pp::hardbreak_tok() {
259 260 261 262 263
            // We do something pretty sketchy here: tuck the nonzero
            // offset-adjustment we were going to deposit along with the
            // break into the previous hardbreak.
            s.s.replace_last_token(pp::hardbreak_tok_offset(off));
        }
264 265
    }
}
266

267 268
// Synthesizes a comment that was not textually present in the original source
// file.
269
fn synth_comment(s: ps, text: str) {
B
Brian Anderson 已提交
270
    word(s.s, "/*");
271 272 273
    space(s.s);
    word(s.s, text);
    space(s.s);
B
Brian Anderson 已提交
274
    word(s.s, "*/");
275 276
}

N
Niko Matsakis 已提交
277
fn commasep<IN>(s: ps, b: breaks, elts: [IN], op: fn(ps, IN)) {
278
    box(s, 0u, b);
279
    let mut first = true;
280
    for elts.each {|elt|
B
Brian Anderson 已提交
281
        if first { first = false; } else { word_space(s, ","); }
282 283 284 285 286 287
        op(s, elt);
    }
    end(s);
}


N
Niko Matsakis 已提交
288 289
fn commasep_cmnt<IN>(s: ps, b: breaks, elts: [IN], op: fn(ps, IN),
                     get_span: fn(IN) -> codemap::span) {
290
    box(s, 0u, b);
291
    let len = vec::len::<IN>(elts);
292
    let mut i = 0u;
293
    for elts.each {|elt|
294 295 296
        maybe_print_comment(s, get_span(elt).hi);
        op(s, elt);
        i += 1u;
M
Marijn Haverbeke 已提交
297
        if i < len {
B
Brian Anderson 已提交
298
            word(s.s, ",");
299
            maybe_print_trailing_comment(s, get_span(elt),
B
Brian Anderson 已提交
300
                                         some(get_span(elts[i]).hi));
301
            space_if_not_bol(s);
302 303 304 305 306
        }
    }
    end(s);
}

307
fn commasep_exprs(s: ps, b: breaks, exprs: [@ast::expr]) {
308
    fn expr_span(&&expr: @ast::expr) -> codemap::span { ret expr.span; }
309
    commasep_cmnt(s, b, exprs, print_expr, expr_span);
310
}
M
Marijn Haverbeke 已提交
311

312
fn print_mod(s: ps, _mod: ast::_mod, attrs: [ast::attribute]) {
313
    print_inner_attributes(s, attrs);
314
    for _mod.view_items.each {|vitem|
315 316
        print_view_item(s, vitem);
    }
317
    for _mod.items.each {|item| print_item(s, item); }
318 319
}

320
fn print_native_mod(s: ps, nmod: ast::native_mod, attrs: [ast::attribute]) {
321
    print_inner_attributes(s, attrs);
322
    for nmod.view_items.each {|vitem|
323 324
        print_view_item(s, vitem);
    }
325
    for nmod.items.each {|item| print_native_item(s, item); }
326 327
}

328
fn print_region(s: ps, region: ast::region) {
329
    alt region.node {
330
      ast::re_anon { /* no-op */ }
331
      ast::re_named(name) { word(s.s, name); word(s.s, "."); }
332
      ast::re_static { word(s.s, "static"); word(s.s, "."); }
333 334 335
    }
}

336
fn print_type(s: ps, &&ty: @ast::ty) {
337
    maybe_print_comment(s, ty.span.lo);
338
    ibox(s, 0u);
M
Marijn Haverbeke 已提交
339
    alt ty.node {
340 341
      ast::ty_nil { word(s.s, "()"); }
      ast::ty_bot { word(s.s, "!"); }
B
Brian Anderson 已提交
342
      ast::ty_box(mt) { word(s.s, "@"); print_mt(s, mt); }
B
Brian Anderson 已提交
343
      ast::ty_uniq(mt) { word(s.s, "~"); print_mt(s, mt); }
344
      ast::ty_vec(mt) {
B
Brian Anderson 已提交
345
        word(s.s, "[");
346 347 348 349
        alt mt.mutbl {
          ast::m_mutbl { word_space(s, "mut"); }
          ast::m_const { word_space(s, "const"); }
          ast::m_imm { }
350
        }
351
        print_type(s, mt.ty);
B
Brian Anderson 已提交
352
        word(s.s, "]");
M
Marijn Haverbeke 已提交
353
      }
B
Brian Anderson 已提交
354
      ast::ty_ptr(mt) { word(s.s, "*"); print_mt(s, mt); }
355 356 357 358 359
      ast::ty_rptr(region, mt) {
        word(s.s, "&");
        print_region(s, region);
        print_mt(s, mt);
      }
M
Marijn Haverbeke 已提交
360
      ast::ty_rec(fields) {
B
Brian Anderson 已提交
361
        word(s.s, "{");
362
        fn print_field(s: ps, f: ast::ty_field) {
M
Marijn Haverbeke 已提交
363
            cbox(s, indent_unit);
364
            print_mutability(s, f.node.mt.mutbl);
365
            word(s.s, f.node.ident);
B
Brian Anderson 已提交
366
            word_space(s, ":");
367
            print_type(s, f.node.mt.ty);
M
Marijn Haverbeke 已提交
368
            end(s);
369
        }
370
        fn get_span(f: ast::ty_field) -> codemap::span { ret f.span; }
M
Marijn Haverbeke 已提交
371
        commasep_cmnt(s, consistent, fields, print_field, get_span);
372
        word(s.s, ",}");
M
Marijn Haverbeke 已提交
373
      }
374
      ast::ty_tup(elts) {
B
Brian Anderson 已提交
375 376 377
        popen(s);
        commasep(s, inconsistent, elts, print_type);
        pclose(s);
378
      }
379
      ast::ty_fn(proto, d) {
380
        print_ty_fn(s, some(proto), d, none, none);
M
Marijn Haverbeke 已提交
381
      }
382
      ast::ty_path(path, _) { print_path(s, path, false); }
M
Marijn Haverbeke 已提交
383
      ast::ty_constr(t, cs) {
384
        print_type(s, t);
M
Marijn Haverbeke 已提交
385
        space(s.s);
386
        word(s.s, constrs_str(cs, ty_constr_to_str));
M
Marijn Haverbeke 已提交
387
      }
388 389 390 391
      ast::ty_vstore(t, v) {
        print_type(s, t);
        print_vstore(s, v);
      }
T
Tim Chevalier 已提交
392 393 394 395 396 397 398
      ast::ty_mac(_) {
          fail "print_type doesn't know how to print a ty_mac";
      }
      ast::ty_infer {
          fail "print_type shouldn't see a ty_infer";
      }

M
Marijn Haverbeke 已提交
399
    }
400
    end(s);
M
Marijn Haverbeke 已提交
401 402
}

403
fn print_native_item(s: ps, item: @ast::native_item) {
404 405 406
    hardbreak_if_not_bol(s);
    maybe_print_comment(s, item.span.lo);
    print_outer_attributes(s, item.attrs);
M
Marijn Haverbeke 已提交
407
    alt item.node {
408
      ast::native_item_fn(decl, typarams) {
409
        print_fn(s, decl, item.ident, typarams);
410
        end(s); // end head-ibox
B
Brian Anderson 已提交
411
        word(s.s, ";");
412 413 414 415 416
        end(s); // end the outer fn box
      }
    }
}

417
fn print_item(s: ps, &&item: @ast::item) {
418
    hardbreak_if_not_bol(s);
419
    maybe_print_comment(s, item.span.lo);
420
    print_outer_attributes(s, item.attrs);
M
Marijn Haverbeke 已提交
421
    let ann_node = node_item(s, item);
422
    s.ann.pre(ann_node);
M
Marijn Haverbeke 已提交
423 424
    alt item.node {
      ast::item_const(ty, expr) {
B
Brian Anderson 已提交
425 426
        head(s, "const");
        word_space(s, item.ident + ":");
427
        print_type(s, ty);
M
Marijn Haverbeke 已提交
428 429 430
        space(s.s);
        end(s); // end the head-ibox

B
Brian Anderson 已提交
431
        word_space(s, "=");
M
Marijn Haverbeke 已提交
432
        print_expr(s, expr);
B
Brian Anderson 已提交
433
        word(s.s, ";");
M
Marijn Haverbeke 已提交
434
        end(s); // end the outer cbox
435

M
Marijn Haverbeke 已提交
436
      }
437 438
      ast::item_fn(decl, typarams, body) {
        print_fn(s, decl, item.ident, typarams);
B
Brian Anderson 已提交
439
        word(s.s, " ");
440
        print_block_with_attrs(s, body, item.attrs);
M
Marijn Haverbeke 已提交
441 442
      }
      ast::item_mod(_mod) {
B
Brian Anderson 已提交
443
        head(s, "mod");
444
        word_nbsp(s, item.ident);
M
Marijn Haverbeke 已提交
445 446 447 448 449
        bopen(s);
        print_mod(s, _mod, item.attrs);
        bclose(s, item.span);
      }
      ast::item_native_mod(nmod) {
B
Brian Anderson 已提交
450 451
        head(s, "native");
        word_nbsp(s, "mod");
452
        word_nbsp(s, item.ident);
M
Marijn Haverbeke 已提交
453 454 455 456
        bopen(s);
        print_native_mod(s, nmod, item.attrs);
        bclose(s, item.span);
      }
457
      ast::item_ty(ty, params, rp) {
M
Marijn Haverbeke 已提交
458 459
        ibox(s, indent_unit);
        ibox(s, 0u);
B
Brian Anderson 已提交
460
        word_nbsp(s, "type");
461
        word(s.s, item.ident);
462
        print_region_param(s, rp);
M
Marijn Haverbeke 已提交
463 464 465 466
        print_type_params(s, params);
        end(s); // end the inner ibox

        space(s.s);
B
Brian Anderson 已提交
467
        word_space(s, "=");
468
        print_type(s, ty);
B
Brian Anderson 已提交
469
        word(s.s, ";");
M
Marijn Haverbeke 已提交
470 471
        end(s); // end the outer ibox
      }
472
      ast::item_enum(variants, params, rp) {
M
Marijn Haverbeke 已提交
473
        let newtype =
B
Brian Anderson 已提交
474
            vec::len(variants) == 1u &&
475
                str::eq(item.ident, variants[0].node.name) &&
B
Brian Anderson 已提交
476
                vec::len(variants[0].node.args) == 1u;
M
Marijn Haverbeke 已提交
477 478
        if newtype {
            ibox(s, indent_unit);
479 480
            word_space(s, "enum");
        } else { head(s, "enum"); }
481
        word(s.s, item.ident);
482
        print_region_param(s, rp);
M
Marijn Haverbeke 已提交
483 484 485
        print_type_params(s, params);
        space(s.s);
        if newtype {
B
Brian Anderson 已提交
486
            word_space(s, "=");
B
Brian Anderson 已提交
487
            print_type(s, variants[0].node.args[0].ty);
B
Brian Anderson 已提交
488
            word(s.s, ";");
M
Marijn Haverbeke 已提交
489 490
            end(s);
        } else {
491
            bopen(s);
492
            for variants.each {|v|
493
                space_if_not_bol(s);
M
Marijn Haverbeke 已提交
494
                maybe_print_comment(s, v.span.lo);
495
                print_outer_attributes(s, v.node.attrs);
496
                ibox(s, indent_unit);
497
                print_variant(s, v);
498
                word(s.s, ",");
499
                end(s);
500
                maybe_print_trailing_comment(s, v.span, none::<uint>);
501
            }
G
Graydon Hoare 已提交
502
            bclose(s, item.span);
M
Marijn Haverbeke 已提交
503
        }
M
Marijn Haverbeke 已提交
504
      }
505
      ast::item_class(tps,ifaces,items,ctor, rp) {
506 507
          head(s, "class");
          word_nbsp(s, item.ident);
508
          print_region_param(s, rp);
509
          print_type_params(s, tps);
510 511 512
          word_space(s, "implements");
          commasep(s, inconsistent, ifaces, {|s, p|
                      print_path(s, p.path, false)});
513 514
          bopen(s);
          hardbreak_if_not_bol(s);
T
Tim Chevalier 已提交
515
          maybe_print_comment(s, ctor.span.lo);
516
          head(s, "new");
T
Tim Chevalier 已提交
517
          print_fn_args_and_ret(s, ctor.node.dec);
518
          space(s.s);
T
Tim Chevalier 已提交
519
          print_block(s, ctor.node.body);
520
          for items.each {|ci|
521
                  /*
522
                     FIXME: collect all private items and print them
523 524 525
                     in a single "priv" section
                   */
             hardbreak_if_not_bol(s);
T
Tim Chevalier 已提交
526
             maybe_print_comment(s, ci.span.lo);
527 528
             let pr = ast_util::class_member_privacy(ci);
             alt pr {
529 530 531 532 533 534 535
                ast::priv {
                    head(s, "priv");
                    bopen(s);
                    hardbreak_if_not_bol(s);
                }
                _ {}
             }
536 537
             alt ci.node {
                ast::instance_var(nm, t, mt, _,_) {
538 539
                    word_nbsp(s, "let");
                    alt mt {
G
Graydon Hoare 已提交
540
                      ast::class_mutable { word_nbsp(s, "mut"); }
541 542 543 544 545 546 547
                      _ {}
                    }
                    word(s.s, nm);
                    word_nbsp(s, ":");
                    print_type(s, t);
                    word(s.s, ";");
                }
T
Tim Chevalier 已提交
548 549
                ast::class_method(m) {
                    print_method(s, m);
550 551
                }
             }
552
             alt pr {
553 554 555 556
                 ast::priv { bclose(s, ci.span); }
                 _ {}
             }
          }
T
Tim Chevalier 已提交
557
          bclose(s, item.span);
558
       }
559
      ast::item_impl(tps, ifce, ty, methods) {
560
        head(s, "impl");
M
Marijn Haverbeke 已提交
561 562
        word(s.s, item.ident);
        print_type_params(s, tps);
563 564 565 566 567 568 569 570 571
        space(s.s);
        alt ifce {
          some(ty) {
            word_nbsp(s, "of");
            print_type(s, ty);
            space(s.s);
          }
          _ {}
        }
M
Marijn Haverbeke 已提交
572
        word_nbsp(s, "for");
573
        print_type(s, ty);
M
Marijn Haverbeke 已提交
574
        space(s.s);
575
        bopen(s);
576
        for methods.each {|meth|
T
Tim Chevalier 已提交
577
           print_method(s, meth);
578 579 580
        }
        bclose(s, item.span);
      }
581 582 583
      ast::item_iface(tps, methods) {
        head(s, "iface");
        word(s.s, item.ident);
M
Marijn Haverbeke 已提交
584
        print_type_params(s, tps);
585
        word(s.s, " ");
586
        bopen(s);
587
        for methods.each {|meth| print_ty_method(s, meth); }
588 589
        bclose(s, item.span);
      }
590 591
      ast::item_res(decl, tps, body, dt_id, ct_id, rp) {
        print_res(s, decl, item.ident, tps, rp);
592
        print_block(s, body);
M
Marijn Haverbeke 已提交
593
      }
M
Marijn Haverbeke 已提交
594
    }
595
    s.ann.post(ann_node);
M
Marijn Haverbeke 已提交
596 597
}

B
Brian Anderson 已提交
598
fn print_res(s: ps, decl: ast::fn_decl, name: ast::ident,
599
             typarams: [ast::ty_param], rp: ast::region_param) {
B
Brian Anderson 已提交
600 601
    head(s, "resource");
    word(s.s, name);
602
    print_region_param(s, rp);
B
Brian Anderson 已提交
603 604 605 606 607 608 609 610
    print_type_params(s, typarams);
    popen(s);
    word_space(s, decl.inputs[0].ident + ":");
    print_type(s, decl.inputs[0].ty);
    pclose(s);
    space(s.s);
}

611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
fn print_variant(s: ps, v: ast::variant) {
    word(s.s, v.node.name);
    if vec::len(v.node.args) > 0u {
        popen(s);
        fn print_variant_arg(s: ps, arg: ast::variant_arg) {
            print_type(s, arg.ty);
        }
        commasep(s, consistent, v.node.args, print_variant_arg);
        pclose(s);
    }
    alt v.node.disr_expr {
      some(d) {
        space(s.s);
        word_space(s, "=");
        print_expr(s, d);
      }
      _ {}
    }
}

631 632 633
fn print_ty_method(s: ps, m: ast::ty_method) {
    hardbreak_if_not_bol(s);
    maybe_print_comment(s, m.span.lo);
634
    print_outer_attributes(s, m.attrs);
635
    print_ty_fn(s, none, m.decl, some(m.ident), some(m.tps));
636 637 638
    word(s.s, ";");
}

T
Tim Chevalier 已提交
639 640 641 642 643 644 645 646 647
fn print_method(s: ps, meth: @ast::method) {
    hardbreak_if_not_bol(s);
    maybe_print_comment(s, meth.span.lo);
    print_outer_attributes(s, meth.attrs);
    print_fn(s, meth.decl, meth.ident, meth.tps);
    word(s.s, " ");
    print_block_with_attrs(s, meth.body, meth.attrs);
}

648
fn print_outer_attributes(s: ps, attrs: [ast::attribute]) {
649
    let mut count = 0;
650
    for attrs.each {|attr|
M
Marijn Haverbeke 已提交
651
        alt attr.node.style {
652
          ast::attr_outer { print_attribute(s, attr); count += 1; }
M
Marijn Haverbeke 已提交
653
          _ {/* fallthrough */ }
654 655
        }
    }
M
Marijn Haverbeke 已提交
656
    if count > 0 { hardbreak_if_not_bol(s); }
657 658
}

659
fn print_inner_attributes(s: ps, attrs: [ast::attribute]) {
660
    let mut count = 0;
661
    for attrs.each {|attr|
M
Marijn Haverbeke 已提交
662
        alt attr.node.style {
663
          ast::attr_inner {
M
Marijn Haverbeke 已提交
664
            print_attribute(s, attr);
B
Brian Anderson 已提交
665
            word(s.s, ";");
M
Marijn Haverbeke 已提交
666 667 668
            count += 1;
          }
          _ {/* fallthrough */ }
669 670
        }
    }
M
Marijn Haverbeke 已提交
671
    if count > 0 { hardbreak_if_not_bol(s); }
672 673
}

674
fn print_attribute(s: ps, attr: ast::attribute) {
675
    hardbreak_if_not_bol(s);
676
    maybe_print_comment(s, attr.span.lo);
B
Brian Anderson 已提交
677
    word(s.s, "#[");
678
    print_meta_item(s, @attr.node.value);
B
Brian Anderson 已提交
679
    word(s.s, "]");
680 681
}

682

683
fn print_stmt(s: ps, st: ast::stmt) {
684
    maybe_print_comment(s, st.span.lo);
M
Marijn Haverbeke 已提交
685
    alt st.node {
686 687 688 689 690
      ast::stmt_decl(decl, _) {
        print_decl(s, decl);
      }
      ast::stmt_expr(expr, _) {
        space_if_not_bol(s);
691
        print_expr(s, expr);
692
      }
693 694
      ast::stmt_semi(expr, _) {
        space_if_not_bol(s);
695
        print_expr(s, expr);
696 697
        word(s.s, ";");
      }
698
    }
699
    if parse::classify::stmt_ends_with_semi(st) { word(s.s, ";"); }
700
    maybe_print_trailing_comment(s, st.span, none::<uint>);
701 702
}

703
fn print_block(s: ps, blk: ast::blk) {
M
Michael Sullivan 已提交
704
    print_possibly_embedded_block(s, blk, block_normal, indent_unit);
705
}
706

707 708 709 710
fn print_block_with_attrs(s: ps, blk: ast::blk, attrs: [ast::attribute]) {
    print_possibly_embedded_block_(s, blk, block_normal, indent_unit, attrs);
}

P
Patrick Walton 已提交
711
enum embed_type { block_macro, block_block_fn, block_normal, }
M
Michael Sullivan 已提交
712

713
fn print_possibly_embedded_block(s: ps, blk: ast::blk, embedded: embed_type,
M
Marijn Haverbeke 已提交
714
                                 indented: uint) {
715 716 717 718 719 720
    print_possibly_embedded_block_(
        s, blk, embedded, indented, []);
}

fn print_possibly_embedded_block_(s: ps, blk: ast::blk, embedded: embed_type,
                                  indented: uint, attrs: [ast::attribute]) {
721
    alt blk.node.rules {
722 723 724
      ast::unchecked_blk { word(s.s, "unchecked"); }
      ast::unsafe_blk { word(s.s, "unsafe"); }
      ast::default_blk { }
725
    }
726
    maybe_print_comment(s, blk.span.lo);
M
Marijn Haverbeke 已提交
727
    let ann_node = node_block(s, blk);
728
    s.ann.pre(ann_node);
M
Michael Sullivan 已提交
729
    alt embedded {
730 731 732
      block_macro { word(s.s, "#{"); end(s); }
      block_block_fn { end(s); }
      block_normal { bopen(s); }
M
Michael Sullivan 已提交
733
    }
734

735 736
    print_inner_attributes(s, attrs);

737 738
    for blk.node.view_items.each {|vi| print_view_item(s, vi); }
    for blk.node.stmts.each {|st|
739 740
        print_stmt(s, *st);
    }
M
Marijn Haverbeke 已提交
741 742 743
    alt blk.node.expr {
      some(expr) {
        space_if_not_bol(s);
744
        print_expr(s, expr);
M
Marijn Haverbeke 已提交
745 746 747
        maybe_print_trailing_comment(s, expr.span, some(blk.span.hi));
      }
      _ { }
M
Marijn Haverbeke 已提交
748
    }
749
    bclose_(s, blk.span, indented);
750
    s.ann.post(ann_node);
M
Marijn Haverbeke 已提交
751 752
}

753 754
// ret and fail, without arguments cannot appear is the discriminant of if,
// alt, do, & while unambiguously without being parenthesized
755
fn print_maybe_parens_discrim(s: ps, e: @ast::expr) {
756
    let disambig = alt e.node {
757
      ast::expr_ret(none) | ast::expr_fail(none) { true }
758 759
      _ { false }
    };
760
    if disambig { popen(s); }
761
    print_expr(s, e);
762
    if disambig { pclose(s); }
763 764
}

765
fn print_if(s: ps, test: @ast::expr, blk: ast::blk,
T
Tim Chevalier 已提交
766
            elseopt: option<@ast::expr>, chk: bool) {
B
Brian Anderson 已提交
767 768
    head(s, "if");
    if chk { word_nbsp(s, "check"); }
769
    print_maybe_parens_discrim(s, test);
T
Tim Chevalier 已提交
770
    space(s.s);
771
    print_block(s, blk);
T
Tim Chevalier 已提交
772
    fn do_else(s: ps, els: option<@ast::expr>) {
M
Marijn Haverbeke 已提交
773 774 775 776 777 778 779
        alt els {
          some(_else) {
            alt _else.node {
              // "another else-if"
              ast::expr_if(i, t, e) {
                cbox(s, indent_unit - 1u);
                ibox(s, 0u);
B
Brian Anderson 已提交
780
                word(s.s, " else if ");
781
                print_maybe_parens_discrim(s, i);
M
Marijn Haverbeke 已提交
782 783 784 785 786 787 788 789
                space(s.s);
                print_block(s, t);
                do_else(s, e);
              }
              // "final else"
              ast::expr_block(b) {
                cbox(s, indent_unit - 1u);
                ibox(s, 0u);
B
Brian Anderson 已提交
790
                word(s.s, " else ");
M
Marijn Haverbeke 已提交
791 792
                print_block(s, b);
              }
T
Tim Chevalier 已提交
793 794 795 796
              // BLEAH, constraints would be great here
              _ {
                  fail "print_if saw if with weird alternative";
              }
T
Tim Chevalier 已提交
797
            }
M
Marijn Haverbeke 已提交
798 799
          }
          _ {/* fall through */ }
T
Tim Chevalier 已提交
800 801 802 803 804
        }
    }
    do_else(s, elseopt);
}

805
fn print_mac(s: ps, m: ast::mac) {
M
Marijn Haverbeke 已提交
806
    alt m.node {
807
      ast::mac_invoc(path, arg, body) {
B
Brian Anderson 已提交
808
        word(s.s, "#");
809
        print_path(s, path, false);
810 811 812 813
        alt arg {
          some(@{node: ast::expr_vec(_, _), _}) { }
          _ { word(s.s, " "); }
        }
T
Tim Chevalier 已提交
814
        option::iter(arg, bind print_expr(s, _));
M
Marijn Haverbeke 已提交
815 816 817
        // FIXME: extension 'body'
      }
      ast::mac_embed_type(ty) {
B
Brian Anderson 已提交
818
        word(s.s, "#<");
819
        print_type(s, ty);
B
Brian Anderson 已提交
820
        word(s.s, ">");
M
Marijn Haverbeke 已提交
821 822
      }
      ast::mac_embed_block(blk) {
M
Michael Sullivan 已提交
823
        print_possibly_embedded_block(s, blk, block_normal, indent_unit);
M
Marijn Haverbeke 已提交
824
      }
825
      ast::mac_ellipsis { word(s.s, "..."); }
826 827
      ast::mac_var(v) { word(s.s, #fmt("$%u", v)); }
      _ { /* fixme */ }
828 829 830
    }
}

831 832 833 834 835 836 837 838
fn print_vstore(s: ps, t: ast::vstore) {
    alt t {
      ast::vstore_fixed(some(i)) { word_space(s, #fmt("/%u", i)); }
      ast::vstore_fixed(none) { word_space(s, "/_"); }
      ast::vstore_uniq { word_space(s, "/~"); }
      ast::vstore_box { word_space(s, "/@"); }
      ast::vstore_slice(r) {
        alt r.node {
839
          ast::re_anon { word_space(s, "/&"); }
840 841 842 843 844 845 846 847 848 849
          ast::re_static { word_space(s, "/&static"); }
          ast::re_named(name) {
            word(s.s, "/&");
            word_space(s, name);
          }
        }
      }
    }
}

850
fn print_expr(s: ps, &&expr: @ast::expr) {
851
    maybe_print_comment(s, expr.span.lo);
852
    ibox(s, indent_unit);
M
Marijn Haverbeke 已提交
853
    let ann_node = node_expr(s, expr);
854
    s.ann.pre(ann_node);
M
Marijn Haverbeke 已提交
855
    alt expr.node {
856 857 858 859
      ast::expr_vstore(e, v) {
        print_expr(s, e);
        print_vstore(s, v);
      }
860
      ast::expr_vec(exprs, mutbl) {
M
Marijn Haverbeke 已提交
861
        ibox(s, indent_unit);
B
Brian Anderson 已提交
862
        word(s.s, "[");
863
        if mutbl == ast::m_mutbl {
G
Graydon Hoare 已提交
864
            word(s.s, "mut");
B
Brian Anderson 已提交
865
            if vec::len(exprs) > 0u { nbsp(s); }
866
        }
M
Marijn Haverbeke 已提交
867
        commasep_exprs(s, inconsistent, exprs);
B
Brian Anderson 已提交
868
        word(s.s, "]");
M
Marijn Haverbeke 已提交
869 870 871
        end(s);
      }
      ast::expr_rec(fields, wth) {
872
        fn print_field(s: ps, field: ast::field) {
M
Marijn Haverbeke 已提交
873
            ibox(s, indent_unit);
G
Graydon Hoare 已提交
874
            if field.node.mutbl == ast::m_mutbl { word_nbsp(s, "mut"); }
875
            word(s.s, field.node.ident);
B
Brian Anderson 已提交
876
            word_space(s, ":");
M
Marijn Haverbeke 已提交
877 878
            print_expr(s, field.node.expr);
            end(s);
879
        }
880
        fn get_span(field: ast::field) -> codemap::span { ret field.span; }
B
Brian Anderson 已提交
881
        word(s.s, "{");
M
Marijn Haverbeke 已提交
882 883 884
        commasep_cmnt(s, consistent, fields, print_field, get_span);
        alt wth {
          some(expr) {
B
Brian Anderson 已提交
885
            if vec::len(fields) > 0u { space(s.s); }
M
Marijn Haverbeke 已提交
886
            ibox(s, indent_unit);
B
Brian Anderson 已提交
887
            word_space(s, "with");
888
            print_expr(s, expr);
M
Marijn Haverbeke 已提交
889 890
            end(s);
          }
891
          _ { word(s.s, ","); }
892
        }
B
Brian Anderson 已提交
893
        word(s.s, "}");
M
Marijn Haverbeke 已提交
894
      }
895 896
      ast::expr_tup(exprs) {
        popen(s);
M
Marijn Haverbeke 已提交
897
        commasep_exprs(s, inconsistent, exprs);
898 899
        pclose(s);
      }
900
      ast::expr_call(func, args, has_block) {
901 902 903 904 905 906 907 908 909
        let mut base_args = args;
        let blk = if has_block {
            let blk_arg = vec::pop(base_args);
            alt blk_arg.node {
              ast::expr_loop_body(_) { word_nbsp(s, "for"); }
              _ {}
            }
            some(blk_arg)
        } else { none };
910
        print_expr_parens_if_not_bot(s, func);
911 912 913 914 915 916 917 918 919
        if !has_block || vec::len(base_args) > 0u {
            popen(s);
            commasep_exprs(s, inconsistent, base_args);
            pclose(s);
        }
        if has_block {
            nbsp(s);
            print_expr(s, option::get(blk));
        }
M
Marijn Haverbeke 已提交
920 921
      }
      ast::expr_bind(func, args) {
T
Tim Chevalier 已提交
922
        fn print_opt(s: ps, expr: option<@ast::expr>) {
M
Marijn Haverbeke 已提交
923 924
            alt expr {
              some(expr) { print_expr(s, expr); }
B
Brian Anderson 已提交
925
              _ { word(s.s, "_"); }
M
Marijn Haverbeke 已提交
926
            }
927
        }
928 929 930 931 932 933

        // "bind" keyword is only needed if there are no "_" arguments.
        if !vec::any(args) {|arg| option::is_none(arg) } {
            word_nbsp(s, "bind");
        }

M
Marijn Haverbeke 已提交
934 935 936 937 938 939 940
        print_expr(s, func);
        popen(s);
        commasep(s, inconsistent, args, print_opt);
        pclose(s);
      }
      ast::expr_binary(op, lhs, rhs) {
        let prec = operator_prec(op);
941
        print_op_maybe_parens(s, lhs, prec);
M
Marijn Haverbeke 已提交
942
        space(s.s);
943
        word_space(s, ast_util::binop_to_str(op));
944
        print_op_maybe_parens(s, rhs, prec + 1);
M
Marijn Haverbeke 已提交
945 946
      }
      ast::expr_unary(op, expr) {
947
        word(s.s, ast_util::unop_to_str(op));
948
        print_op_maybe_parens(s, expr, parse::prec::unop_prec);
M
Marijn Haverbeke 已提交
949
      }
950
      ast::expr_addr_of(m, expr) {
951
        word(s.s, "&");
952 953 954
        print_mutability(s, m);
        print_expr(s, expr);
      }
M
Marijn Haverbeke 已提交
955 956
      ast::expr_lit(lit) { print_literal(s, lit); }
      ast::expr_cast(expr, ty) {
957
        print_op_maybe_parens(s, expr, parse::prec::as_prec);
M
Marijn Haverbeke 已提交
958
        space(s.s);
B
Brian Anderson 已提交
959
        word_space(s, "as");
960
        print_type(s, ty);
M
Marijn Haverbeke 已提交
961 962 963 964 965 966 967 968
      }
      ast::expr_if(test, blk, elseopt) {
        print_if(s, test, blk, elseopt, false);
      }
      ast::expr_if_check(test, blk, elseopt) {
        print_if(s, test, blk, elseopt, true);
      }
      ast::expr_while(test, blk) {
B
Brian Anderson 已提交
969
        head(s, "while");
970
        print_maybe_parens_discrim(s, test);
M
Marijn Haverbeke 已提交
971 972 973
        space(s.s);
        print_block(s, blk);
      }
T
Tim Chevalier 已提交
974 975 976 977 978
      ast::expr_loop(blk) {
        head(s, "loop");
        space(s.s);
        print_block(s, blk);
      }
M
Marijn Haverbeke 已提交
979
      ast::expr_do_while(blk, expr) {
B
Brian Anderson 已提交
980
        head(s, "do");
M
Marijn Haverbeke 已提交
981 982 983
        space(s.s);
        print_block(s, blk);
        space(s.s);
B
Brian Anderson 已提交
984
        word_space(s, "while");
M
Marijn Haverbeke 已提交
985 986
        print_expr(s, expr);
      }
M
Marijn Haverbeke 已提交
987
      ast::expr_alt(expr, arms, mode) {
M
Marijn Haverbeke 已提交
988 989
        cbox(s, alt_indent_unit);
        ibox(s, 4u);
B
Brian Anderson 已提交
990
        word_nbsp(s, "alt");
M
Marijn Haverbeke 已提交
991
        if mode == ast::alt_check { word_nbsp(s, "check"); }
992
        print_maybe_parens_discrim(s, expr);
M
Marijn Haverbeke 已提交
993 994
        space(s.s);
        bopen(s);
995
        for arms.each {|arm|
996
            space(s.s);
997
            cbox(s, alt_indent_unit);
M
Marijn Haverbeke 已提交
998
            ibox(s, 0u);
999
            let mut first = true;
1000
            for arm.pats.each {|p|
M
Marijn Haverbeke 已提交
1001 1002
                if first {
                    first = false;
B
Brian Anderson 已提交
1003
                } else { space(s.s); word_space(s, "|"); }
M
Marijn Haverbeke 已提交
1004
                print_pat(s, p);
1005
            }
1006
            space(s.s);
M
Marijn Haverbeke 已提交
1007
            alt arm.guard {
1008
              some(e) { word_space(s, "if"); print_expr(s, e); space(s.s); }
1009
              none { }
M
Marijn Haverbeke 已提交
1010
            }
M
Michael Sullivan 已提交
1011
            print_possibly_embedded_block(s, arm.body, block_normal,
M
Marijn Haverbeke 已提交
1012
                                          alt_indent_unit);
1013
        }
M
Marijn Haverbeke 已提交
1014 1015
        bclose_(s, expr.span, alt_indent_unit);
      }
1016 1017 1018 1019 1020 1021 1022
      ast::expr_fn(proto, decl, body, cap_clause) {
        // containing cbox, will be closed by print-block at }
        cbox(s, indent_unit);
        // head-box, will be closed by print-block at start
        ibox(s, 0u);
        word(s.s, proto_to_str(proto));
        print_cap_clause(s, *cap_clause);
1023
        print_fn_args_and_ret(s, decl);
1024
        space(s.s);
1025
        print_block(s, body);
1026 1027 1028 1029 1030 1031 1032 1033 1034
      }
      ast::expr_fn_block(decl, body) {
        // containing cbox, will be closed by print-block at }
        cbox(s, indent_unit);
        // head-box, will be closed by print-block at start
        ibox(s, 0u);
        word(s.s, "{");
        print_fn_block_args(s, decl);
        print_possibly_embedded_block(s, body, block_block_fn, indent_unit);
M
Marijn Haverbeke 已提交
1035
      }
1036 1037 1038
      ast::expr_loop_body(body) {
        print_expr(s, body);
      }
M
Marijn Haverbeke 已提交
1039 1040 1041 1042 1043 1044 1045
      ast::expr_block(blk) {
        // containing cbox, will be closed by print-block at }
        cbox(s, indent_unit);
        // head-box, will be closed by print-block after {
        ibox(s, 0u);
        print_block(s, blk);
      }
B
Brian Anderson 已提交
1046
      ast::expr_copy(e) { word_space(s, "copy"); print_expr(s, e); }
M
Marijn Haverbeke 已提交
1047 1048 1049
      ast::expr_move(lhs, rhs) {
        print_expr(s, lhs);
        space(s.s);
B
Brian Anderson 已提交
1050
        word_space(s, "<-");
M
Marijn Haverbeke 已提交
1051 1052 1053 1054 1055
        print_expr(s, rhs);
      }
      ast::expr_assign(lhs, rhs) {
        print_expr(s, lhs);
        space(s.s);
B
Brian Anderson 已提交
1056
        word_space(s, "=");
M
Marijn Haverbeke 已提交
1057 1058 1059 1060 1061
        print_expr(s, rhs);
      }
      ast::expr_swap(lhs, rhs) {
        print_expr(s, lhs);
        space(s.s);
B
Brian Anderson 已提交
1062
        word_space(s, "<->");
M
Marijn Haverbeke 已提交
1063 1064 1065 1066 1067
        print_expr(s, rhs);
      }
      ast::expr_assign_op(op, lhs, rhs) {
        print_expr(s, lhs);
        space(s.s);
1068
        word(s.s, ast_util::binop_to_str(op));
B
Brian Anderson 已提交
1069
        word_space(s, "=");
M
Marijn Haverbeke 已提交
1070 1071
        print_expr(s, rhs);
      }
1072
      ast::expr_field(expr, id, tys) {
M
Marijn Haverbeke 已提交
1073
        // Deal with '10.x'
1074
        if ends_in_lit_int(expr) {
M
Marijn Haverbeke 已提交
1075
            popen(s); print_expr(s, expr); pclose(s);
1076
        } else {
1077
            print_expr_parens_if_not_bot(s, expr);
M
Marijn Haverbeke 已提交
1078
        }
B
Brian Anderson 已提交
1079
        word(s.s, ".");
1080
        word(s.s, id);
1081 1082 1083 1084 1085
        if vec::len(tys) > 0u {
            word(s.s, "::<");
            commasep(s, inconsistent, tys, print_type);
            word(s.s, ">");
        }
M
Marijn Haverbeke 已提交
1086 1087
      }
      ast::expr_index(expr, index) {
1088
        print_expr_parens_if_not_bot(s, expr);
B
Brian Anderson 已提交
1089
        word(s.s, "[");
M
Marijn Haverbeke 已提交
1090
        print_expr(s, index);
B
Brian Anderson 已提交
1091
        word(s.s, "]");
M
Marijn Haverbeke 已提交
1092
      }
1093
      ast::expr_path(path) { print_path(s, path, true); }
M
Marijn Haverbeke 已提交
1094
      ast::expr_fail(maybe_fail_val) {
B
Brian Anderson 已提交
1095
        word(s.s, "fail");
M
Marijn Haverbeke 已提交
1096
        alt maybe_fail_val {
B
Brian Anderson 已提交
1097
          some(expr) { word(s.s, " "); print_expr(s, expr); }
M
Marijn Haverbeke 已提交
1098
          _ { }
1099
        }
M
Marijn Haverbeke 已提交
1100
      }
1101 1102
      ast::expr_break { word(s.s, "break"); }
      ast::expr_cont { word(s.s, "cont"); }
M
Marijn Haverbeke 已提交
1103
      ast::expr_ret(result) {
B
Brian Anderson 已提交
1104
        word(s.s, "ret");
M
Marijn Haverbeke 已提交
1105
        alt result {
B
Brian Anderson 已提交
1106
          some(expr) { word(s.s, " "); print_expr(s, expr); }
M
Marijn Haverbeke 已提交
1107
          _ { }
1108
        }
M
Marijn Haverbeke 已提交
1109
      }
B
Brian Anderson 已提交
1110
      ast::expr_be(result) { word_nbsp(s, "be"); print_expr(s, result); }
1111
      ast::expr_log(lvl, lexp, expr) {
1112
        alt check lvl {
1113 1114 1115
          1 { word_nbsp(s, "log"); print_expr(s, expr); }
          0 { word_nbsp(s, "log_err"); print_expr(s, expr); }
          2 {
1116
            word_nbsp(s, "log");
1117
            popen(s);
1118
            print_expr(s, lexp);
1119 1120 1121 1122
            word(s.s, ",");
            space_if_not_bol(s);
            print_expr(s, expr);
            pclose(s);
1123 1124
          }
        }
M
Marijn Haverbeke 已提交
1125 1126 1127
      }
      ast::expr_check(m, expr) {
        alt m {
1128 1129
          ast::claimed_expr { word_nbsp(s, "claim"); }
          ast::checked_expr { word_nbsp(s, "check"); }
M
Marijn Haverbeke 已提交
1130 1131 1132 1133 1134 1135
        }
        popen(s);
        print_expr(s, expr);
        pclose(s);
      }
      ast::expr_assert(expr) {
B
Brian Anderson 已提交
1136
        word_nbsp(s, "assert");
M
Marijn Haverbeke 已提交
1137 1138
        print_expr(s, expr);
      }
1139 1140 1141 1142 1143 1144 1145
      ast::expr_new(p, _, v) {
        word_nbsp(s, "new");
        popen(s);
        print_expr(s, p);
        pclose(s);
        print_expr(s, v);
      }
M
Marijn Haverbeke 已提交
1146
      ast::expr_mac(m) { print_mac(s, m); }
1147
    }
1148
    s.ann.post(ann_node);
1149
    end(s);
M
Marijn Haverbeke 已提交
1150 1151
}

1152
fn print_expr_parens_if_not_bot(s: ps, ex: @ast::expr) {
1153
    let parens = alt ex.node {
1154
      ast::expr_fail(_) | ast::expr_ret(_) |
1155
      ast::expr_binary(_, _, _) | ast::expr_unary(_, _) |
P
Paul Woolcock 已提交
1156 1157
      ast::expr_move(_, _) | ast::expr_copy(_) |
      ast::expr_assign(_, _) | ast::expr_be(_) |
1158
      ast::expr_assign_op(_, _, _) | ast::expr_swap(_, _) |
1159
      ast::expr_log(_, _, _) | ast::expr_assert(_) |
1160
      ast::expr_call(_, _, true) |
1161
      ast::expr_check(_, _) { true }
1162 1163
      _ { false }
    };
1164 1165 1166 1167 1168
    if parens { popen(s); }
    print_expr(s, ex);
    if parens { pclose(s); }
}

1169
fn print_local_decl(s: ps, loc: @ast::local) {
1170 1171
    print_pat(s, loc.node.pat);
    alt loc.node.ty.node {
1172
      ast::ty_infer { }
B
Brian Anderson 已提交
1173
      _ { word_space(s, ":"); print_type(s, loc.node.ty); }
1174 1175 1176
    }
}

1177
fn print_decl(s: ps, decl: @ast::decl) {
1178
    maybe_print_comment(s, decl.span.lo);
M
Marijn Haverbeke 已提交
1179 1180 1181 1182
    alt decl.node {
      ast::decl_local(locs) {
        space_if_not_bol(s);
        ibox(s, indent_unit);
B
Brian Anderson 已提交
1183
        word_nbsp(s, "let");
1184

G
Graydon Hoare 已提交
1185
        // if any are mut, all are mut
1186 1187 1188 1189 1190
        if vec::any(locs) {|l| l.node.is_mutbl } {
            assert vec::all(locs) {|l| l.node.is_mutbl };
            word_nbsp(s, "mut");
        }

1191
        fn print_local(s: ps, &&loc: @ast::local) {
1192
            ibox(s, indent_unit);
1193
            print_local_decl(s, loc);
1194
            end(s);
M
Marijn Haverbeke 已提交
1195 1196 1197 1198
            alt loc.node.init {
              some(init) {
                nbsp(s);
                alt init.op {
1199 1200
                  ast::init_assign { word_space(s, "="); }
                  ast::init_move { word_space(s, "<-"); }
1201
                }
M
Marijn Haverbeke 已提交
1202 1203 1204
                print_expr(s, init.expr);
              }
              _ { }
1205
            }
M
Marijn Haverbeke 已提交
1206
        }
1207
        commasep(s, consistent, locs, print_local);
M
Marijn Haverbeke 已提交
1208 1209 1210
        end(s);
      }
      ast::decl_item(item) { print_item(s, item); }
M
Marijn Haverbeke 已提交
1211
    }
1212 1213
}

1214
fn print_ident(s: ps, ident: ast::ident) { word(s.s, ident); }
1215

1216
fn print_for_decl(s: ps, loc: @ast::local, coll: @ast::expr) {
1217
    print_local_decl(s, loc);
1218
    space(s.s);
B
Brian Anderson 已提交
1219
    word_space(s, "in");
1220
    print_expr(s, coll);
M
Marijn Haverbeke 已提交
1221 1222
}

M
Marijn Haverbeke 已提交
1223
fn print_path(s: ps, &&path: @ast::path, colons_before_params: bool) {
1224
    maybe_print_comment(s, path.span.lo);
B
Brian Anderson 已提交
1225
    if path.node.global { word(s.s, "::"); }
1226
    let mut first = true;
1227
    for path.node.idents.each {|id|
B
Brian Anderson 已提交
1228
        if first { first = false; } else { word(s.s, "::"); }
1229
        word(s.s, id);
1230
    }
B
Brian Anderson 已提交
1231
    if vec::len(path.node.types) > 0u {
B
Brian Anderson 已提交
1232 1233
        if colons_before_params { word(s.s, "::"); }
        word(s.s, "<");
1234
        commasep(s, inconsistent, path.node.types, print_type);
B
Brian Anderson 已提交
1235
        word(s.s, ">");
1236
    }
M
Marijn Haverbeke 已提交
1237 1238
}

1239
fn print_pat(s: ps, &&pat: @ast::pat) {
1240
    maybe_print_comment(s, pat.span.lo);
M
Marijn Haverbeke 已提交
1241
    let ann_node = node_pat(s, pat);
1242
    s.ann.pre(ann_node);
1243 1244
    /* Pat isn't normalized, but the beauty of it
     is that it doesn't matter */
M
Marijn Haverbeke 已提交
1245
    alt pat.node {
1246
      ast::pat_wild { word(s.s, "_"); }
1247 1248
      ast::pat_ident(path, sub) {
        print_path(s, path, true);
1249 1250 1251 1252 1253
        alt sub {
          some(p) { word(s.s, "@"); print_pat(s, p); }
          _ {}
        }
      }
1254
      ast::pat_enum(path, args) {
1255
        print_path(s, path, true);
B
Brian Anderson 已提交
1256
        if vec::len(args) > 0u {
M
Marijn Haverbeke 已提交
1257 1258 1259
            popen(s);
            commasep(s, inconsistent, args, print_pat);
            pclose(s);
1260
        } else { }
M
Marijn Haverbeke 已提交
1261 1262
      }
      ast::pat_rec(fields, etc) {
B
Brian Anderson 已提交
1263
        word(s.s, "{");
1264
        fn print_field(s: ps, f: ast::field_pat) {
M
Marijn Haverbeke 已提交
1265
            cbox(s, indent_unit);
1266
            word(s.s, f.ident);
B
Brian Anderson 已提交
1267
            word_space(s, ":");
M
Marijn Haverbeke 已提交
1268 1269
            print_pat(s, f.pat);
            end(s);
M
Marijn Haverbeke 已提交
1270
        }
1271
        fn get_span(f: ast::field_pat) -> codemap::span { ret f.pat.span; }
M
Marijn Haverbeke 已提交
1272 1273
        commasep_cmnt(s, consistent, fields, print_field, get_span);
        if etc {
B
Brian Anderson 已提交
1274 1275
            if vec::len(fields) != 0u { word_space(s, ","); }
            word(s.s, "_");
1276
        }
B
Brian Anderson 已提交
1277
        word(s.s, "}");
M
Marijn Haverbeke 已提交
1278
      }
M
Marijn Haverbeke 已提交
1279 1280 1281 1282 1283
      ast::pat_tup(elts) {
        popen(s);
        commasep(s, inconsistent, elts, print_pat);
        pclose(s);
      }
B
Brian Anderson 已提交
1284
      ast::pat_box(inner) { word(s.s, "@"); print_pat(s, inner); }
1285
      ast::pat_uniq(inner) { word(s.s, "~"); print_pat(s, inner); }
1286
      ast::pat_lit(e) { print_expr(s, e); }
1287
      ast::pat_range(begin, end) {
1288
        print_expr(s, begin);
1289 1290
        space(s.s);
        word_space(s, "to");
1291
        print_expr(s, end);
1292
      }
M
Marijn Haverbeke 已提交
1293
    }
1294
    s.ann.post(ann_node);
M
Marijn Haverbeke 已提交
1295 1296
}

1297 1298
fn print_fn(s: ps, decl: ast::fn_decl, name: ast::ident,
            typarams: [ast::ty_param]) {
M
Marijn Haverbeke 已提交
1299
    alt decl.purity {
1300 1301 1302
      ast::impure_fn { head(s, "fn"); }
      ast::unsafe_fn { head(s, "unsafe fn"); }
      ast::pure_fn { head(s, "pure fn"); }
1303
      ast::crust_fn { head(s, "crust fn"); }
1304
    }
1305
    word(s.s, name);
1306
    print_type_params(s, typarams);
1307
    print_fn_args_and_ret(s, decl);
1308 1309
}

1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
fn print_cap_clause(s: ps, cap_clause: ast::capture_clause) {
    fn print_cap_item(s: ps, &&cap_item: @ast::capture_item) {
        word(s.s, cap_item.name);
    }

    let has_copies = vec::is_not_empty(cap_clause.copies);
    let has_moves = vec::is_not_empty(cap_clause.moves);
    if !has_copies && !has_moves { ret; }

    word(s.s, "[");

    if has_copies {
        word_nbsp(s, "copy");
        commasep(s, inconsistent, cap_clause.copies, print_cap_item);
        if has_moves {
            word_space(s, ";");
        }
    }

    if has_moves {
        word_nbsp(s, "move");
        commasep(s, inconsistent, cap_clause.moves, print_cap_item);
    }

    word(s.s, "]");
}

1337
fn print_fn_args_and_ret(s: ps, decl: ast::fn_decl) {
1338
    popen(s);
1339
    fn print_arg(s: ps, x: ast::arg) {
1340
        ibox(s, indent_unit);
1341
        print_arg_mode(s, x.mode);
1342
        word_space(s, x.ident + ":");
1343
        print_type(s, x.ty);
1344
        end(s);
1345
    }
1346
    commasep(s, inconsistent, decl.inputs, print_arg);
1347
    pclose(s);
1348 1349 1350 1351
    word(s.s, constrs_str(decl.constraints, {|c|
        ast_fn_constr_to_str(decl, c)
    }));

1352
    maybe_print_comment(s, decl.output.span.lo);
M
Marijn Haverbeke 已提交
1353
    if decl.output.node != ast::ty_nil {
1354
        space_if_not_bol(s);
B
Brian Anderson 已提交
1355
        word_space(s, "->");
1356
        print_type(s, decl.output);
1357
    }
M
Marijn Haverbeke 已提交
1358 1359
}

1360
fn print_fn_block_args(s: ps, decl: ast::fn_decl) {
B
Brian Anderson 已提交
1361
    word(s.s, "|");
1362
    fn print_arg(s: ps, x: ast::arg) {
M
Michael Sullivan 已提交
1363
        ibox(s, indent_unit);
1364
        print_arg_mode(s, x.mode);
1365
        word(s.s, x.ident);
M
Michael Sullivan 已提交
1366 1367 1368
        end(s);
    }
    commasep(s, inconsistent, decl.inputs, print_arg);
B
Brian Anderson 已提交
1369
    word(s.s, "|");
1370 1371 1372 1373 1374
    if decl.output.node != ast::ty_infer {
        space_if_not_bol(s);
        word_space(s, "->");
        print_type(s, decl.output);
    }
M
Michael Sullivan 已提交
1375 1376 1377
    maybe_print_comment(s, decl.output.span.lo);
}

1378
fn mode_to_str(m: ast::mode) -> str {
M
Marijn Haverbeke 已提交
1379
    alt m {
1380
      ast::expl(ast::by_mutbl_ref) { "&" }
1381 1382 1383 1384 1385
      ast::expl(ast::by_move) { "-" }
      ast::expl(ast::by_ref) { "&&" }
      ast::expl(ast::by_val) { "++" }
      ast::expl(ast::by_copy) { "+" }
      ast::infer(_) { "" }
1386 1387 1388
    }
}

1389 1390 1391 1392 1393
fn print_arg_mode(s: ps, m: ast::mode) {
    let ms = mode_to_str(m);
    if ms != "" { word(s.s, ms); }
}

1394 1395 1396
fn print_bounds(s: ps, bounds: @[ast::ty_param_bound]) {
    if vec::len(*bounds) > 0u {
        word(s.s, ":");
1397
        for vec::each(*bounds) {|bound|
1398 1399
            nbsp(s);
            alt bound {
1400 1401
              ast::bound_copy { word(s.s, "copy"); }
              ast::bound_send { word(s.s, "send"); }
1402 1403 1404
              ast::bound_iface(t) { print_type(s, t); }
            }
        }
1405 1406 1407
    }
}

1408 1409
fn print_region_param(s: ps, rp: ast::region_param) {
    alt rp {
N
Niko Matsakis 已提交
1410
      ast::rp_self { word(s.s, "/&") }
1411 1412 1413 1414
      ast::rp_none { }
    }
}

1415
fn print_type_params(s: ps, &&params: [ast::ty_param]) {
B
Brian Anderson 已提交
1416
    if vec::len(params) > 0u {
B
Brian Anderson 已提交
1417
        word(s.s, "<");
1418
        fn printParam(s: ps, param: ast::ty_param) {
1419
            word(s.s, param.ident);
1420
            print_bounds(s, param.bounds);
1421
        }
1422
        commasep(s, inconsistent, params, printParam);
B
Brian Anderson 已提交
1423
        word(s.s, ">");
1424
    }
M
Marijn Haverbeke 已提交
1425 1426
}

1427
fn print_meta_item(s: ps, &&item: @ast::meta_item) {
1428
    ibox(s, indent_unit);
M
Marijn Haverbeke 已提交
1429
    alt item.node {
1430
      ast::meta_word(name) { word(s.s, name); }
M
Marijn Haverbeke 已提交
1431
      ast::meta_name_value(name, value) {
1432
        word_space(s, name);
B
Brian Anderson 已提交
1433
        word_space(s, "=");
M
Marijn Haverbeke 已提交
1434 1435 1436
        print_literal(s, @value);
      }
      ast::meta_list(name, items) {
1437
        word(s.s, name);
M
Marijn Haverbeke 已提交
1438 1439 1440 1441
        popen(s);
        commasep(s, consistent, items, print_meta_item);
        pclose(s);
      }
1442
    }
1443 1444 1445
    end(s);
}

1446 1447 1448
fn print_view_path(s: ps, &&vp: @ast::view_path) {
    alt vp.node {
      ast::view_path_simple(ident, path, _) {
1449
        if path.node.idents[vec::len(path.node.idents)-1u] != ident {
1450 1451 1452
            word_space(s, ident);
            word_space(s, "=");
        }
1453
        print_path(s, path, false);
1454 1455 1456
      }

      ast::view_path_glob(path, _) {
1457
        print_path(s, path, false);
1458 1459 1460 1461
        word(s.s, "::*");
      }

      ast::view_path_list(path, idents, _) {
1462
        print_path(s, path, false);
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
        word(s.s, "::{");
        commasep(s, inconsistent, idents) {|s, w|
            word(s.s, w.node.name)
        }
        word(s.s, "}");
      }
    }
}

fn print_view_paths(s: ps, vps: [@ast::view_path]) {
    commasep(s, inconsistent, vps, print_view_path);
}

1476
fn print_view_item(s: ps, item: @ast::view_item) {
1477
    hardbreak_if_not_bol(s);
1478
    maybe_print_comment(s, item.span.lo);
M
Marijn Haverbeke 已提交
1479 1480
    alt item.node {
      ast::view_item_use(id, mta, _) {
B
Brian Anderson 已提交
1481
        head(s, "use");
1482
        word(s.s, id);
B
Brian Anderson 已提交
1483
        if vec::len(mta) > 0u {
M
Marijn Haverbeke 已提交
1484 1485 1486
            popen(s);
            commasep(s, consistent, mta, print_meta_item);
            pclose(s);
1487
        }
M
Marijn Haverbeke 已提交
1488
      }
1489 1490

      ast::view_item_import(vps) {
B
Brian Anderson 已提交
1491
        head(s, "import");
1492
        print_view_paths(s, vps);
M
Marijn Haverbeke 已提交
1493
      }
1494 1495

      ast::view_item_export(vps) {
B
Brian Anderson 已提交
1496
        head(s, "export");
1497
        print_view_paths(s, vps);
1498
      }
M
Marijn Haverbeke 已提交
1499
    }
B
Brian Anderson 已提交
1500
    word(s.s, ";");
1501 1502
    end(s); // end inner head-block
    end(s); // end outer head-block
M
Marijn Haverbeke 已提交
1503 1504
}

1505
fn print_op_maybe_parens(s: ps, expr: @ast::expr, outer_prec: int) {
1506
    let add_them = need_parens(expr, outer_prec);
M
Marijn Haverbeke 已提交
1507
    if add_them { popen(s); }
1508
    print_expr(s, expr);
M
Marijn Haverbeke 已提交
1509
    if add_them { pclose(s); }
M
Marijn Haverbeke 已提交
1510 1511
}

1512 1513
fn print_mutability(s: ps, mutbl: ast::mutability) {
    alt mutbl {
G
Graydon Hoare 已提交
1514
      ast::m_mutbl { word_nbsp(s, "mut"); }
1515 1516
      ast::m_const { word_nbsp(s, "const"); }
      ast::m_imm {/* nothing */ }
1517
    }
G
Graydon Hoare 已提交
1518 1519
}

1520
fn print_mt(s: ps, mt: ast::mt) {
1521
    print_mutability(s, mt.mutbl);
1522
    print_type(s, mt.ty);
M
Marijn Haverbeke 已提交
1523 1524
}

1525
fn print_ty_fn(s: ps, opt_proto: option<ast::proto>,
T
Tim Chevalier 已提交
1526 1527
               decl: ast::fn_decl, id: option<ast::ident>,
               tps: option<[ast::ty_param]>) {
1528
    ibox(s, indent_unit);
1529
    word(s.s, opt_proto_to_str(opt_proto));
B
Brian Anderson 已提交
1530
    alt id { some(id) { word(s.s, " "); word(s.s, id); } _ { } }
1531
    alt tps { some(tps) { print_type_params(s, tps); } _ { } }
G
Graydon Hoare 已提交
1532
    zerobreak(s.s);
G
Graydon Hoare 已提交
1533
    popen(s);
1534 1535
    fn print_arg(s: ps, input: ast::arg) {
        print_arg_mode(s, input.mode);
K
Kevin Cantu 已提交
1536
        if str::len(input.ident) > 0u {
1537 1538 1539
            word_space(s, input.ident + ":");
        }
        print_type(s, input.ty);
1540
    }
1541
    commasep(s, inconsistent, decl.inputs, print_arg);
1542
    pclose(s);
1543 1544
    maybe_print_comment(s, decl.output.span.lo);
    if decl.output.node != ast::ty_nil {
1545
        space_if_not_bol(s);
1546
        ibox(s, indent_unit);
B
Brian Anderson 已提交
1547
        word_space(s, "->");
1548 1549
        if decl.cf == ast::noreturn { word_nbsp(s, "!"); }
        else { print_type(s, decl.output); }
1550
        end(s);
1551
    }
1552
    word(s.s, constrs_str(decl.constraints, ast_ty_fn_constr_to_str));
1553
    end(s);
1554 1555
}

1556
fn maybe_print_trailing_comment(s: ps, span: codemap::span,
T
Tim Chevalier 已提交
1557
                                next_pos: option<uint>) {
1558
    let mut cm;
M
Marijn Haverbeke 已提交
1559 1560 1561
    alt s.cm { some(ccm) { cm = ccm; } _ { ret; } }
    alt next_comment(s) {
      some(cmnt) {
1562
        if cmnt.style != comments::trailing { ret; }
M
Marijn Haverbeke 已提交
1563 1564
        let span_line = codemap::lookup_char_pos(cm, span.hi);
        let comment_line = codemap::lookup_char_pos(cm, cmnt.pos);
1565
        let mut next = cmnt.pos + 1u;
1566
        alt next_pos { none { } some(p) { next = p; } }
M
Marijn Haverbeke 已提交
1567 1568 1569 1570
        if span.hi < cmnt.pos && cmnt.pos < next &&
               span_line.line == comment_line.line {
            print_comment(s, cmnt);
            s.cur_cmnt += 1u;
1571
        }
M
Marijn Haverbeke 已提交
1572 1573
      }
      _ { }
1574 1575 1576
    }
}

1577
fn print_remaining_comments(s: ps) {
1578 1579 1580
    // If there aren't any remaining comments, then we need to manually
    // make sure there is a line break at the end.
    if option::is_none(next_comment(s)) { hardbreak(s.s); }
1581
    loop {
M
Marijn Haverbeke 已提交
1582 1583 1584
        alt next_comment(s) {
          some(cmnt) { print_comment(s, cmnt); s.cur_cmnt += 1u; }
          _ { break; }
1585 1586 1587 1588
        }
    }
}

1589
fn print_literal(s: ps, &&lit: @ast::lit) {
1590
    maybe_print_comment(s, lit.span.lo);
1591
    alt next_lit(s, lit.span.lo) {
M
Marijn Haverbeke 已提交
1592
      some(lt) {
1593 1594
        word(s.s, lt.lit);
        ret;
M
Marijn Haverbeke 已提交
1595
      }
1596
      _ {}
1597
    }
M
Marijn Haverbeke 已提交
1598
    alt lit.node {
B
Brian Anderson 已提交
1599
      ast::lit_str(st) { print_string(s, st); }
1600
      ast::lit_int(ch, ast::ty_char) {
1601 1602 1603
        word(s.s, "'" + escape_str(str::from_char(ch as char), '\'') + "'");
      }
      ast::lit_int(i, t) {
1604 1605 1606 1607 1608 1609 1610 1611 1612
        if i < 0_i64 {
            word(s.s,
                 "-" + u64::to_str(-i as u64, 10u)
                 + ast_util::int_ty_to_str(t));
        } else {
            word(s.s,
                 u64::to_str(i as u64, 10u)
                 + ast_util::int_ty_to_str(t));
        }
1613 1614
      }
      ast::lit_uint(u, t) {
1615 1616 1617
        word(s.s,
             u64::to_str(u, 10u)
             + ast_util::uint_ty_to_str(t));
1618 1619 1620
      }
      ast::lit_float(f, t) {
        word(s.s, f + ast_util::float_ty_to_str(t));
M
Marijn Haverbeke 已提交
1621
      }
1622
      ast::lit_nil { word(s.s, "()"); }
M
Marijn Haverbeke 已提交
1623
      ast::lit_bool(val) {
B
Brian Anderson 已提交
1624
        if val { word(s.s, "true"); } else { word(s.s, "false"); }
M
Marijn Haverbeke 已提交
1625
      }
1626 1627 1628
    }
}

1629
fn lit_to_str(l: @ast::lit) -> str { be to_str(l, print_literal); }
1630

1631
fn next_lit(s: ps, pos: uint) -> option<comments::lit> {
M
Marijn Haverbeke 已提交
1632 1633
    alt s.literals {
      some(lits) {
1634 1635 1636 1637 1638 1639 1640
        while s.cur_lit < vec::len(lits) {
            let lt = lits[s.cur_lit];
            if lt.pos > pos { ret none; }
            s.cur_lit += 1u;
            if lt.pos == pos { ret some(lt); }
        }
        ret none;
M
Marijn Haverbeke 已提交
1641
      }
1642
      _ { ret none; }
1643 1644 1645
    }
}

1646
fn maybe_print_comment(s: ps, pos: uint) {
1647
    loop {
M
Marijn Haverbeke 已提交
1648 1649 1650 1651 1652 1653 1654 1655
        alt next_comment(s) {
          some(cmnt) {
            if cmnt.pos < pos {
                print_comment(s, cmnt);
                s.cur_cmnt += 1u;
            } else { break; }
          }
          _ { break; }
1656 1657 1658 1659
        }
    }
}

1660
fn print_comment(s: ps, cmnt: comments::cmnt) {
M
Marijn Haverbeke 已提交
1661
    alt cmnt.style {
1662
      comments::mixed {
B
Brian Anderson 已提交
1663
        assert (vec::len(cmnt.lines) == 1u);
M
Marijn Haverbeke 已提交
1664
        zerobreak(s.s);
1665
        word(s.s, cmnt.lines[0]);
M
Marijn Haverbeke 已提交
1666 1667
        zerobreak(s.s);
      }
1668
      comments::isolated {
M
Marijn Haverbeke 已提交
1669
        pprust::hardbreak_if_not_bol(s);
1670
        for cmnt.lines.each {|line|
1671 1672
            // Don't print empty lines because they will end up as trailing
            // whitespace
B
Brian Anderson 已提交
1673
            if str::is_not_empty(line) { word(s.s, line); }
1674 1675
            hardbreak(s.s);
        }
M
Marijn Haverbeke 已提交
1676
      }
1677
      comments::trailing {
B
Brian Anderson 已提交
1678
        word(s.s, " ");
B
Brian Anderson 已提交
1679
        if vec::len(cmnt.lines) == 1u {
1680
            word(s.s, cmnt.lines[0]);
1681
            hardbreak(s.s);
M
Marijn Haverbeke 已提交
1682 1683
        } else {
            ibox(s, 0u);
1684
            for cmnt.lines.each {|line|
B
Brian Anderson 已提交
1685
                if str::is_not_empty(line) { word(s.s, line); }
1686 1687
                hardbreak(s.s);
            }
M
Marijn Haverbeke 已提交
1688
            end(s);
1689
        }
M
Marijn Haverbeke 已提交
1690
      }
1691
      comments::blank_line {
M
Marijn Haverbeke 已提交
1692
        // We need to do at least one, possibly two hardbreaks.
B
Brian Anderson 已提交
1693 1694
        let is_semi =
            alt s.s.last_token() {
B
Brian Anderson 已提交
1695
              pp::STRING(s, _) { s == ";" }
B
Brian Anderson 已提交
1696 1697
              _ { false }
            };
1698
        if is_semi || is_begin(s) || is_end(s) { hardbreak(s.s); }
M
Marijn Haverbeke 已提交
1699 1700
        hardbreak(s.s);
      }
1701 1702 1703
    }
}

1704
fn print_string(s: ps, st: str) {
B
Brian Anderson 已提交
1705
    word(s.s, "\"");
1706
    word(s.s, escape_str(st, '"'));
B
Brian Anderson 已提交
1707
    word(s.s, "\"");
1708 1709
}

1710
fn escape_str(st: str, to_escape: char) -> str {
1711
    let mut out: str = "";
K
Kevin Cantu 已提交
1712
    let len = str::len(st);
1713
    let mut i = 0u;
M
Marijn Haverbeke 已提交
1714
    while i < len {
B
Brian Anderson 已提交
1715
        alt st[i] as char {
B
Brian Anderson 已提交
1716 1717 1718 1719
          '\n' { out += "\\n"; }
          '\t' { out += "\\t"; }
          '\r' { out += "\\r"; }
          '\\' { out += "\\\\"; }
M
Marijn Haverbeke 已提交
1720
          cur {
B
Brian Anderson 已提交
1721
            if cur == to_escape { out += "\\"; }
M
Marijn Haverbeke 已提交
1722 1723
            // FIXME some (or all?) non-ascii things should be escaped

1724
            str::push_char(out, cur);
M
Marijn Haverbeke 已提交
1725
          }
1726 1727 1728 1729 1730 1731
        }
        i += 1u;
    }
    ret out;
}

1732
fn to_str<T>(t: T, f: fn@(ps, T)) -> str {
1733
    let buffer = io::mem_buffer();
M
Marijn Haverbeke 已提交
1734
    let s = rust_printer(io::mem_buffer_writer(buffer));
1735 1736
    f(s, t);
    eof(s.s);
M
Marijn Haverbeke 已提交
1737
    io::mem_buffer_str(buffer)
1738 1739
}

1740
fn next_comment(s: ps) -> option<comments::cmnt> {
M
Marijn Haverbeke 已提交
1741 1742
    alt s.comments {
      some(cmnts) {
B
Brian Anderson 已提交
1743
        if s.cur_cmnt < vec::len(cmnts) {
B
Brian Anderson 已提交
1744
            ret some(cmnts[s.cur_cmnt]);
1745
        } else { ret none::<comments::cmnt>; }
M
Marijn Haverbeke 已提交
1746
      }
1747
      _ { ret none::<comments::cmnt>; }
1748 1749 1750
    }
}

1751
fn constr_args_to_str<T>(f: fn@(T) -> str, args: [@ast::sp_constr_arg<T>]) ->
1752
   str {
1753 1754
    let mut comma = false;
    let mut s = "(";
1755
    for args.each {|a|
B
Brian Anderson 已提交
1756
        if comma { s += ", "; } else { comma = true; }
1757
        s += constr_arg_to_str::<T>(f, a.node);
1758
    }
B
Brian Anderson 已提交
1759
    s += ")";
1760 1761 1762
    ret s;
}

1763
fn constr_arg_to_str<T>(f: fn@(T) -> str, c: ast::constr_arg_general_<T>) ->
B
Brian Anderson 已提交
1764
   str {
M
Marijn Haverbeke 已提交
1765
    alt c {
1766
      ast::carg_base { ret "*"; }
M
Marijn Haverbeke 已提交
1767 1768
      ast::carg_ident(i) { ret f(i); }
      ast::carg_lit(l) { ret lit_to_str(l); }
1769 1770 1771 1772 1773 1774
    }
}

// needed b/c constr_args_to_str needs
// something that takes an alias
// (argh)
1775
fn uint_to_str(&&i: uint) -> str { ret uint::str(i); }
1776

1777
fn ast_ty_fn_constr_to_str(&&c: @ast::constr) -> str {
1778
    ret path_to_str(c.node.path) +
M
Marijn Haverbeke 已提交
1779
            constr_args_to_str(uint_to_str, c.node.args);
1780 1781
}

1782 1783 1784 1785
fn ast_fn_constr_to_str(decl: ast::fn_decl, &&c: @ast::constr) -> str {
    let arg_to_str = bind fn_arg_idx_to_str(decl, _);
    ret path_to_str(c.node.path) +
            constr_args_to_str(arg_to_str, c.node.args);
1786 1787
}

1788 1789
fn ty_constr_to_str(&&c: @ast::ty_constr) -> str {
    fn ty_constr_path_to_str(&&p: @ast::path) -> str { "*." + path_to_str(p) }
1790 1791

    ret path_to_str(c.node.path) +
1792 1793
            constr_args_to_str::<@ast::path>(ty_constr_path_to_str,
                                             c.node.args);
1794 1795
}

1796
fn constrs_str<T>(constrs: [T], elt: fn(T) -> str) -> str {
1797
    let mut s = "", colon = true;
1798
    for constrs.each {|c|
B
Brian Anderson 已提交
1799
        if colon { s += " : "; colon = false; } else { s += ", "; }
1800
        s += elt(c);
1801 1802 1803 1804
    }
    ret s;
}

1805 1806 1807 1808
fn fn_arg_idx_to_str(decl: ast::fn_decl, &&idx: uint) -> str {
    decl.inputs[idx].ident
}

1809 1810
fn opt_proto_to_str(opt_p: option<ast::proto>) -> str {
    alt opt_p {
1811
      none { "fn" }
1812 1813 1814 1815
      some(p) { proto_to_str(p) }
    }
}

1816
fn proto_to_str(p: ast::proto) -> str {
M
Marijn Haverbeke 已提交
1817
    ret alt p {
1818
      ast::proto_bare { "native fn" }
1819
      ast::proto_any { "fn" }
1820 1821 1822
      ast::proto_block { "fn&" }
      ast::proto_uniq { "fn~" }
      ast::proto_box { "fn@" }
1823
    };
1824 1825
}

1826 1827 1828 1829 1830 1831 1832 1833 1834
//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//