pprust.rs 51.5 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
import ast_util::operator_prec;
10

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

B
Brian Anderson 已提交
128
fn res_to_str(decl: ast::fn_decl, name: ast::ident,
129
              params: [ast::ty_param], rp: ast::region_param) -> str {
130
    let buffer = io::mem_buffer();
B
Brian Anderson 已提交
131
    let s = rust_printer(io::mem_buffer_writer(buffer));
132
    print_res(s, decl, name, params, rp);
B
Brian Anderson 已提交
133 134 135 136 137 138 139 140 141 142
    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: [{
143
            mode: ast::expl(ast::by_val),
144 145 146
            ty: @{id: 0,
                  node: ast::ty_nil,
                  span: ast_util::dummy_sp()},
B
Brian Anderson 已提交
147 148 149
            ident: "b",
            id: 0
        }],
150 151 152
        output: @{id: 0,
                  node: ast::ty_nil,
                  span: ast_util::dummy_sp()},
B
Brian Anderson 已提交
153 154 155 156
        purity: ast::impure_fn,
        cf: ast::return_val,
        constraints: []
    };
157
    assert res_to_str(decl, "a", []) == "resource a(b: ())";
B
Brian Anderson 已提交
158 159
}

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

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

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

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
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";
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

253 254 255
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 已提交
256
    if !is_bol(s) {
257 258
        break_offset(s.s, n, off);
    } else {
M
Marijn Haverbeke 已提交
259
        if off != 0 && s.s.last_token() == pp::hardbreak_tok() {
260 261 262 263 264
            // 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));
        }
265 266
    }
}
267

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

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


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

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

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

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

329
fn print_region(s: ps, region: @ast::region) {
330
    alt region.node {
331 332 333 334 335
      ast::re_anon { word_space(s, "&"); }
      ast::re_named(name) {
        word(s.s, "&");
        word_space(s, name);
      }
336 337 338
    }
}

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

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

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

B
Brian Anderson 已提交
436
        word_space(s, "=");
M
Marijn Haverbeke 已提交
437
        print_expr(s, expr);
B
Brian Anderson 已提交
438
        word(s.s, ";");
M
Marijn Haverbeke 已提交
439
        end(s); // end the outer cbox
440

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

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

B
Brian Anderson 已提交
602
fn print_res(s: ps, decl: ast::fn_decl, name: ast::ident,
603
             typarams: [ast::ty_param], rp: ast::region_param) {
B
Brian Anderson 已提交
604 605
    head(s, "resource");
    word(s.s, name);
606
    print_region_param(s, rp);
B
Brian Anderson 已提交
607 608 609 610 611 612 613 614
    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);
}

615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
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);
      }
      _ {}
    }
}

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

T
Tim Chevalier 已提交
643 644 645 646 647 648 649 650 651
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);
}

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

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

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

686

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

707
fn print_block(s: ps, blk: ast::blk) {
M
Michael Sullivan 已提交
708
    print_possibly_embedded_block(s, blk, block_normal, indent_unit);
709
}
710

711 712 713 714
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 已提交
715
enum embed_type { block_macro, block_block_fn, block_normal, }
M
Michael Sullivan 已提交
716

717
fn print_possibly_embedded_block(s: ps, blk: ast::blk, embedded: embed_type,
M
Marijn Haverbeke 已提交
718
                                 indented: uint) {
719 720 721 722 723 724
    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]) {
725
    alt blk.node.rules {
726 727 728
      ast::unchecked_blk { word(s.s, "unchecked"); }
      ast::unsafe_blk { word(s.s, "unsafe"); }
      ast::default_blk { }
729
    }
730
    maybe_print_comment(s, blk.span.lo);
M
Marijn Haverbeke 已提交
731
    let ann_node = node_block(s, blk);
732
    s.ann.pre(ann_node);
M
Michael Sullivan 已提交
733
    alt embedded {
734 735 736
      block_macro { word(s.s, "#{"); end(s); }
      block_block_fn { end(s); }
      block_normal { bopen(s); }
M
Michael Sullivan 已提交
737
    }
738

739 740
    print_inner_attributes(s, attrs);

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

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

769
fn print_if(s: ps, test: @ast::expr, blk: ast::blk,
T
Tim Chevalier 已提交
770
            elseopt: option<@ast::expr>, chk: bool) {
B
Brian Anderson 已提交
771 772
    head(s, "if");
    if chk { word_nbsp(s, "check"); }
773
    print_maybe_parens_discrim(s, test);
T
Tim Chevalier 已提交
774
    space(s.s);
775
    print_block(s, blk);
T
Tim Chevalier 已提交
776
    fn do_else(s: ps, els: option<@ast::expr>) {
M
Marijn Haverbeke 已提交
777 778 779 780 781 782 783
        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 已提交
784
                word(s.s, " else if ");
785
                print_maybe_parens_discrim(s, i);
M
Marijn Haverbeke 已提交
786 787 788 789 790 791 792 793
                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 已提交
794
                word(s.s, " else ");
M
Marijn Haverbeke 已提交
795 796
                print_block(s, b);
              }
T
Tim Chevalier 已提交
797 798 799 800
              // BLEAH, constraints would be great here
              _ {
                  fail "print_if saw if with weird alternative";
              }
T
Tim Chevalier 已提交
801
            }
M
Marijn Haverbeke 已提交
802 803
          }
          _ {/* fall through */ }
T
Tim Chevalier 已提交
804 805 806 807 808
        }
    }
    do_else(s, elseopt);
}

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

835 836 837 838 839 840
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, "/@"); }
841
      ast::vstore_slice(r) { word(s.s, "/"); print_region(s, r); }
842 843 844
    }
}

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

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

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

1164
fn print_local_decl(s: ps, loc: @ast::local) {
1165 1166
    print_pat(s, loc.node.pat);
    alt loc.node.ty.node {
1167
      ast::ty_infer { }
B
Brian Anderson 已提交
1168
      _ { word_space(s, ":"); print_type(s, loc.node.ty); }
1169 1170 1171
    }
}

1172
fn print_decl(s: ps, decl: @ast::decl) {
1173
    maybe_print_comment(s, decl.span.lo);
M
Marijn Haverbeke 已提交
1174 1175 1176 1177
    alt decl.node {
      ast::decl_local(locs) {
        space_if_not_bol(s);
        ibox(s, indent_unit);
B
Brian Anderson 已提交
1178
        word_nbsp(s, "let");
1179

G
Graydon Hoare 已提交
1180
        // if any are mut, all are mut
1181 1182 1183 1184 1185
        if vec::any(locs) {|l| l.node.is_mutbl } {
            assert vec::all(locs) {|l| l.node.is_mutbl };
            word_nbsp(s, "mut");
        }

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

1209
fn print_ident(s: ps, ident: ast::ident) { word(s.s, ident); }
1210

1211
fn print_for_decl(s: ps, loc: @ast::local, coll: @ast::expr) {
1212
    print_local_decl(s, loc);
1213
    space(s.s);
B
Brian Anderson 已提交
1214
    word_space(s, "in");
1215
    print_expr(s, coll);
M
Marijn Haverbeke 已提交
1216 1217
}

M
Marijn Haverbeke 已提交
1218
fn print_path(s: ps, &&path: @ast::path, colons_before_params: bool) {
1219
    maybe_print_comment(s, path.span.lo);
1220
    if path.global { word(s.s, "::"); }
1221
    let mut first = true;
1222
    for path.idents.each {|id|
B
Brian Anderson 已提交
1223
        if first { first = false; } else { word(s.s, "::"); }
1224
        word(s.s, id);
1225
    }
1226
    if path.rp.is_some() || !path.types.is_empty() {
B
Brian Anderson 已提交
1227
        if colons_before_params { word(s.s, "::"); }
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241

        alt path.rp {
          none { /* ok */ }
          some(r) {
            word(s.s, "/");
            print_region(s, r);
          }
        }

        if !path.types.is_empty() {
            word(s.s, "<");
            commasep(s, inconsistent, path.types, print_type);
            word(s.s, ">");
        }
1242
    }
M
Marijn Haverbeke 已提交
1243 1244
}

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

1308 1309
fn print_fn(s: ps, decl: ast::fn_decl, name: ast::ident,
            typarams: [ast::ty_param]) {
M
Marijn Haverbeke 已提交
1310
    alt decl.purity {
1311 1312 1313
      ast::impure_fn { head(s, "fn"); }
      ast::unsafe_fn { head(s, "unsafe fn"); }
      ast::pure_fn { head(s, "pure fn"); }
1314
      ast::crust_fn { head(s, "crust fn"); }
1315
    }
1316
    word(s.s, name);
1317
    print_type_params(s, typarams);
1318
    print_fn_args_and_ret(s, decl);
1319 1320
}

1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
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, "]");
}

1348
fn print_fn_args_and_ret(s: ps, decl: ast::fn_decl) {
1349
    popen(s);
1350
    fn print_arg(s: ps, x: ast::arg) {
1351
        ibox(s, indent_unit);
1352
        print_arg_mode(s, x.mode);
1353
        word_space(s, x.ident + ":");
1354
        print_type(s, x.ty);
1355
        end(s);
1356
    }
1357
    commasep(s, inconsistent, decl.inputs, print_arg);
1358
    pclose(s);
1359 1360 1361 1362
    word(s.s, constrs_str(decl.constraints, {|c|
        ast_fn_constr_to_str(decl, c)
    }));

1363
    maybe_print_comment(s, decl.output.span.lo);
M
Marijn Haverbeke 已提交
1364
    if decl.output.node != ast::ty_nil {
1365
        space_if_not_bol(s);
B
Brian Anderson 已提交
1366
        word_space(s, "->");
1367
        print_type(s, decl.output);
1368
    }
M
Marijn Haverbeke 已提交
1369 1370
}

1371
fn print_fn_block_args(s: ps, decl: ast::fn_decl) {
B
Brian Anderson 已提交
1372
    word(s.s, "|");
1373
    fn print_arg(s: ps, x: ast::arg) {
M
Michael Sullivan 已提交
1374
        ibox(s, indent_unit);
1375
        print_arg_mode(s, x.mode);
1376
        word(s.s, x.ident);
1377 1378 1379 1380
        if x.ty.node != ast::ty_infer {
            word_space(s, ":");
            print_type(s, x.ty);
        }
M
Michael Sullivan 已提交
1381 1382 1383
        end(s);
    }
    commasep(s, inconsistent, decl.inputs, print_arg);
B
Brian Anderson 已提交
1384
    word(s.s, "|");
1385 1386 1387 1388 1389
    if decl.output.node != ast::ty_infer {
        space_if_not_bol(s);
        word_space(s, "->");
        print_type(s, decl.output);
    }
M
Michael Sullivan 已提交
1390 1391 1392
    maybe_print_comment(s, decl.output.span.lo);
}

1393
fn mode_to_str(m: ast::mode) -> str {
M
Marijn Haverbeke 已提交
1394
    alt m {
1395
      ast::expl(ast::by_mutbl_ref) { "&" }
1396 1397 1398 1399 1400
      ast::expl(ast::by_move) { "-" }
      ast::expl(ast::by_ref) { "&&" }
      ast::expl(ast::by_val) { "++" }
      ast::expl(ast::by_copy) { "+" }
      ast::infer(_) { "" }
1401 1402 1403
    }
}

1404 1405 1406 1407 1408
fn print_arg_mode(s: ps, m: ast::mode) {
    let ms = mode_to_str(m);
    if ms != "" { word(s.s, ms); }
}

1409 1410 1411
fn print_bounds(s: ps, bounds: @[ast::ty_param_bound]) {
    if vec::len(*bounds) > 0u {
        word(s.s, ":");
1412
        for vec::each(*bounds) {|bound|
1413 1414
            nbsp(s);
            alt bound {
1415 1416
              ast::bound_copy { word(s.s, "copy"); }
              ast::bound_send { word(s.s, "send"); }
1417 1418 1419
              ast::bound_iface(t) { print_type(s, t); }
            }
        }
1420 1421 1422
    }
}

1423 1424
fn print_region_param(s: ps, rp: ast::region_param) {
    alt rp {
N
Niko Matsakis 已提交
1425
      ast::rp_self { word(s.s, "/&") }
1426 1427 1428 1429
      ast::rp_none { }
    }
}

1430
fn print_type_params(s: ps, &&params: [ast::ty_param]) {
B
Brian Anderson 已提交
1431
    if vec::len(params) > 0u {
B
Brian Anderson 已提交
1432
        word(s.s, "<");
1433
        fn printParam(s: ps, param: ast::ty_param) {
1434
            word(s.s, param.ident);
1435
            print_bounds(s, param.bounds);
1436
        }
1437
        commasep(s, inconsistent, params, printParam);
B
Brian Anderson 已提交
1438
        word(s.s, ">");
1439
    }
M
Marijn Haverbeke 已提交
1440 1441
}

1442
fn print_meta_item(s: ps, &&item: @ast::meta_item) {
1443
    ibox(s, indent_unit);
M
Marijn Haverbeke 已提交
1444
    alt item.node {
1445
      ast::meta_word(name) { word(s.s, name); }
M
Marijn Haverbeke 已提交
1446
      ast::meta_name_value(name, value) {
1447
        word_space(s, name);
B
Brian Anderson 已提交
1448
        word_space(s, "=");
M
Marijn Haverbeke 已提交
1449 1450 1451
        print_literal(s, @value);
      }
      ast::meta_list(name, items) {
1452
        word(s.s, name);
M
Marijn Haverbeke 已提交
1453 1454 1455 1456
        popen(s);
        commasep(s, consistent, items, print_meta_item);
        pclose(s);
      }
1457
    }
1458 1459 1460
    end(s);
}

1461 1462 1463
fn print_view_path(s: ps, &&vp: @ast::view_path) {
    alt vp.node {
      ast::view_path_simple(ident, path, _) {
1464
        if path.idents[vec::len(path.idents)-1u] != ident {
1465 1466 1467
            word_space(s, ident);
            word_space(s, "=");
        }
1468
        print_path(s, path, false);
1469 1470 1471
      }

      ast::view_path_glob(path, _) {
1472
        print_path(s, path, false);
1473 1474 1475 1476
        word(s.s, "::*");
      }

      ast::view_path_list(path, idents, _) {
1477
        print_path(s, path, false);
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
        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);
}

1491
fn print_view_item(s: ps, item: @ast::view_item) {
1492
    hardbreak_if_not_bol(s);
1493
    maybe_print_comment(s, item.span.lo);
M
Marijn Haverbeke 已提交
1494 1495
    alt item.node {
      ast::view_item_use(id, mta, _) {
B
Brian Anderson 已提交
1496
        head(s, "use");
1497
        word(s.s, id);
B
Brian Anderson 已提交
1498
        if vec::len(mta) > 0u {
M
Marijn Haverbeke 已提交
1499 1500 1501
            popen(s);
            commasep(s, consistent, mta, print_meta_item);
            pclose(s);
1502
        }
M
Marijn Haverbeke 已提交
1503
      }
1504 1505

      ast::view_item_import(vps) {
B
Brian Anderson 已提交
1506
        head(s, "import");
1507
        print_view_paths(s, vps);
M
Marijn Haverbeke 已提交
1508
      }
1509 1510

      ast::view_item_export(vps) {
B
Brian Anderson 已提交
1511
        head(s, "export");
1512
        print_view_paths(s, vps);
1513
      }
M
Marijn Haverbeke 已提交
1514
    }
B
Brian Anderson 已提交
1515
    word(s.s, ";");
1516 1517
    end(s); // end inner head-block
    end(s); // end outer head-block
M
Marijn Haverbeke 已提交
1518 1519
}

1520
fn print_op_maybe_parens(s: ps, expr: @ast::expr, outer_prec: uint) {
1521
    let add_them = need_parens(expr, outer_prec);
M
Marijn Haverbeke 已提交
1522
    if add_them { popen(s); }
1523
    print_expr(s, expr);
M
Marijn Haverbeke 已提交
1524
    if add_them { pclose(s); }
M
Marijn Haverbeke 已提交
1525 1526
}

1527 1528
fn print_mutability(s: ps, mutbl: ast::mutability) {
    alt mutbl {
G
Graydon Hoare 已提交
1529
      ast::m_mutbl { word_nbsp(s, "mut"); }
1530 1531
      ast::m_const { word_nbsp(s, "const"); }
      ast::m_imm {/* nothing */ }
1532
    }
G
Graydon Hoare 已提交
1533 1534
}

1535
fn print_mt(s: ps, mt: ast::mt) {
1536
    print_mutability(s, mt.mutbl);
1537
    print_type(s, mt.ty);
M
Marijn Haverbeke 已提交
1538 1539
}

1540
fn print_ty_fn(s: ps, opt_proto: option<ast::proto>,
T
Tim Chevalier 已提交
1541 1542
               decl: ast::fn_decl, id: option<ast::ident>,
               tps: option<[ast::ty_param]>) {
1543
    ibox(s, indent_unit);
1544
    word(s.s, opt_proto_to_str(opt_proto));
B
Brian Anderson 已提交
1545
    alt id { some(id) { word(s.s, " "); word(s.s, id); } _ { } }
1546
    alt tps { some(tps) { print_type_params(s, tps); } _ { } }
G
Graydon Hoare 已提交
1547
    zerobreak(s.s);
G
Graydon Hoare 已提交
1548
    popen(s);
1549 1550
    fn print_arg(s: ps, input: ast::arg) {
        print_arg_mode(s, input.mode);
K
Kevin Cantu 已提交
1551
        if str::len(input.ident) > 0u {
1552 1553 1554
            word_space(s, input.ident + ":");
        }
        print_type(s, input.ty);
1555
    }
1556
    commasep(s, inconsistent, decl.inputs, print_arg);
1557
    pclose(s);
1558 1559
    maybe_print_comment(s, decl.output.span.lo);
    if decl.output.node != ast::ty_nil {
1560
        space_if_not_bol(s);
1561
        ibox(s, indent_unit);
B
Brian Anderson 已提交
1562
        word_space(s, "->");
1563 1564
        if decl.cf == ast::noreturn { word_nbsp(s, "!"); }
        else { print_type(s, decl.output); }
1565
        end(s);
1566
    }
1567
    word(s.s, constrs_str(decl.constraints, ast_ty_fn_constr_to_str));
1568
    end(s);
1569 1570
}

1571
fn maybe_print_trailing_comment(s: ps, span: codemap::span,
T
Tim Chevalier 已提交
1572
                                next_pos: option<uint>) {
1573
    let mut cm;
M
Marijn Haverbeke 已提交
1574 1575 1576
    alt s.cm { some(ccm) { cm = ccm; } _ { ret; } }
    alt next_comment(s) {
      some(cmnt) {
1577
        if cmnt.style != comments::trailing { ret; }
M
Marijn Haverbeke 已提交
1578 1579
        let span_line = codemap::lookup_char_pos(cm, span.hi);
        let comment_line = codemap::lookup_char_pos(cm, cmnt.pos);
1580
        let mut next = cmnt.pos + 1u;
1581
        alt next_pos { none { } some(p) { next = p; } }
M
Marijn Haverbeke 已提交
1582 1583 1584 1585
        if span.hi < cmnt.pos && cmnt.pos < next &&
               span_line.line == comment_line.line {
            print_comment(s, cmnt);
            s.cur_cmnt += 1u;
1586
        }
M
Marijn Haverbeke 已提交
1587 1588
      }
      _ { }
1589 1590 1591
    }
}

1592
fn print_remaining_comments(s: ps) {
1593 1594 1595
    // 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); }
1596
    loop {
M
Marijn Haverbeke 已提交
1597 1598 1599
        alt next_comment(s) {
          some(cmnt) { print_comment(s, cmnt); s.cur_cmnt += 1u; }
          _ { break; }
1600 1601 1602 1603
        }
    }
}

1604
fn print_literal(s: ps, &&lit: @ast::lit) {
1605
    maybe_print_comment(s, lit.span.lo);
1606
    alt next_lit(s, lit.span.lo) {
M
Marijn Haverbeke 已提交
1607
      some(lt) {
1608 1609
        word(s.s, lt.lit);
        ret;
M
Marijn Haverbeke 已提交
1610
      }
1611
      _ {}
1612
    }
M
Marijn Haverbeke 已提交
1613
    alt lit.node {
B
Brian Anderson 已提交
1614
      ast::lit_str(st) { print_string(s, st); }
1615
      ast::lit_int(ch, ast::ty_char) {
1616 1617 1618
        word(s.s, "'" + escape_str(str::from_char(ch as char), '\'') + "'");
      }
      ast::lit_int(i, t) {
1619 1620 1621 1622 1623 1624 1625 1626 1627
        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));
        }
1628 1629
      }
      ast::lit_uint(u, t) {
1630 1631 1632
        word(s.s,
             u64::to_str(u, 10u)
             + ast_util::uint_ty_to_str(t));
1633 1634 1635
      }
      ast::lit_float(f, t) {
        word(s.s, f + ast_util::float_ty_to_str(t));
M
Marijn Haverbeke 已提交
1636
      }
1637
      ast::lit_nil { word(s.s, "()"); }
M
Marijn Haverbeke 已提交
1638
      ast::lit_bool(val) {
B
Brian Anderson 已提交
1639
        if val { word(s.s, "true"); } else { word(s.s, "false"); }
M
Marijn Haverbeke 已提交
1640
      }
1641 1642 1643
    }
}

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

1646
fn next_lit(s: ps, pos: uint) -> option<comments::lit> {
M
Marijn Haverbeke 已提交
1647 1648
    alt s.literals {
      some(lits) {
1649 1650 1651 1652 1653 1654 1655
        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 已提交
1656
      }
1657
      _ { ret none; }
1658 1659 1660
    }
}

1661
fn maybe_print_comment(s: ps, pos: uint) {
1662
    loop {
M
Marijn Haverbeke 已提交
1663 1664 1665 1666 1667 1668 1669 1670
        alt next_comment(s) {
          some(cmnt) {
            if cmnt.pos < pos {
                print_comment(s, cmnt);
                s.cur_cmnt += 1u;
            } else { break; }
          }
          _ { break; }
1671 1672 1673 1674
        }
    }
}

1675
fn print_comment(s: ps, cmnt: comments::cmnt) {
M
Marijn Haverbeke 已提交
1676
    alt cmnt.style {
1677
      comments::mixed {
B
Brian Anderson 已提交
1678
        assert (vec::len(cmnt.lines) == 1u);
M
Marijn Haverbeke 已提交
1679
        zerobreak(s.s);
1680
        word(s.s, cmnt.lines[0]);
M
Marijn Haverbeke 已提交
1681 1682
        zerobreak(s.s);
      }
1683
      comments::isolated {
M
Marijn Haverbeke 已提交
1684
        pprust::hardbreak_if_not_bol(s);
1685
        for cmnt.lines.each {|line|
1686 1687
            // Don't print empty lines because they will end up as trailing
            // whitespace
B
Brian Anderson 已提交
1688
            if str::is_not_empty(line) { word(s.s, line); }
1689 1690
            hardbreak(s.s);
        }
M
Marijn Haverbeke 已提交
1691
      }
1692
      comments::trailing {
B
Brian Anderson 已提交
1693
        word(s.s, " ");
B
Brian Anderson 已提交
1694
        if vec::len(cmnt.lines) == 1u {
1695
            word(s.s, cmnt.lines[0]);
1696
            hardbreak(s.s);
M
Marijn Haverbeke 已提交
1697 1698
        } else {
            ibox(s, 0u);
1699
            for cmnt.lines.each {|line|
B
Brian Anderson 已提交
1700
                if str::is_not_empty(line) { word(s.s, line); }
1701 1702
                hardbreak(s.s);
            }
M
Marijn Haverbeke 已提交
1703
            end(s);
1704
        }
M
Marijn Haverbeke 已提交
1705
      }
1706
      comments::blank_line {
M
Marijn Haverbeke 已提交
1707
        // We need to do at least one, possibly two hardbreaks.
B
Brian Anderson 已提交
1708 1709
        let is_semi =
            alt s.s.last_token() {
B
Brian Anderson 已提交
1710
              pp::STRING(s, _) { s == ";" }
B
Brian Anderson 已提交
1711 1712
              _ { false }
            };
1713
        if is_semi || is_begin(s) || is_end(s) { hardbreak(s.s); }
M
Marijn Haverbeke 已提交
1714 1715
        hardbreak(s.s);
      }
1716 1717 1718
    }
}

1719
fn print_string(s: ps, st: str) {
B
Brian Anderson 已提交
1720
    word(s.s, "\"");
1721
    word(s.s, escape_str(st, '"'));
B
Brian Anderson 已提交
1722
    word(s.s, "\"");
1723 1724
}

1725
fn escape_str(st: str, to_escape: char) -> str {
1726
    let mut out: str = "";
K
Kevin Cantu 已提交
1727
    let len = str::len(st);
1728
    let mut i = 0u;
M
Marijn Haverbeke 已提交
1729
    while i < len {
B
Brian Anderson 已提交
1730
        alt st[i] as char {
B
Brian Anderson 已提交
1731 1732 1733 1734
          '\n' { out += "\\n"; }
          '\t' { out += "\\t"; }
          '\r' { out += "\\r"; }
          '\\' { out += "\\\\"; }
M
Marijn Haverbeke 已提交
1735
          cur {
B
Brian Anderson 已提交
1736
            if cur == to_escape { out += "\\"; }
M
Marijn Haverbeke 已提交
1737 1738
            // FIXME some (or all?) non-ascii things should be escaped

1739
            str::push_char(out, cur);
M
Marijn Haverbeke 已提交
1740
          }
1741 1742 1743 1744 1745 1746
        }
        i += 1u;
    }
    ret out;
}

1747
fn to_str<T>(t: T, f: fn@(ps, T)) -> str {
1748
    let buffer = io::mem_buffer();
M
Marijn Haverbeke 已提交
1749
    let s = rust_printer(io::mem_buffer_writer(buffer));
1750 1751
    f(s, t);
    eof(s.s);
M
Marijn Haverbeke 已提交
1752
    io::mem_buffer_str(buffer)
1753 1754
}

1755
fn next_comment(s: ps) -> option<comments::cmnt> {
M
Marijn Haverbeke 已提交
1756 1757
    alt s.comments {
      some(cmnts) {
B
Brian Anderson 已提交
1758
        if s.cur_cmnt < vec::len(cmnts) {
B
Brian Anderson 已提交
1759
            ret some(cmnts[s.cur_cmnt]);
1760
        } else { ret none::<comments::cmnt>; }
M
Marijn Haverbeke 已提交
1761
      }
1762
      _ { ret none::<comments::cmnt>; }
1763 1764 1765
    }
}

1766
fn constr_args_to_str<T>(f: fn@(T) -> str, args: [@ast::sp_constr_arg<T>]) ->
1767
   str {
1768 1769
    let mut comma = false;
    let mut s = "(";
1770
    for args.each {|a|
B
Brian Anderson 已提交
1771
        if comma { s += ", "; } else { comma = true; }
1772
        s += constr_arg_to_str::<T>(f, a.node);
1773
    }
B
Brian Anderson 已提交
1774
    s += ")";
1775 1776 1777
    ret s;
}

1778
fn constr_arg_to_str<T>(f: fn@(T) -> str, c: ast::constr_arg_general_<T>) ->
B
Brian Anderson 已提交
1779
   str {
M
Marijn Haverbeke 已提交
1780
    alt c {
1781
      ast::carg_base { ret "*"; }
M
Marijn Haverbeke 已提交
1782 1783
      ast::carg_ident(i) { ret f(i); }
      ast::carg_lit(l) { ret lit_to_str(l); }
1784 1785 1786 1787 1788 1789
    }
}

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

1792
fn ast_ty_fn_constr_to_str(&&c: @ast::constr) -> str {
1793
    ret path_to_str(c.node.path) +
M
Marijn Haverbeke 已提交
1794
            constr_args_to_str(uint_to_str, c.node.args);
1795 1796
}

1797 1798 1799 1800
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);
1801 1802
}

1803 1804
fn ty_constr_to_str(&&c: @ast::ty_constr) -> str {
    fn ty_constr_path_to_str(&&p: @ast::path) -> str { "*." + path_to_str(p) }
1805 1806

    ret path_to_str(c.node.path) +
1807 1808
            constr_args_to_str::<@ast::path>(ty_constr_path_to_str,
                                             c.node.args);
1809 1810
}

1811
fn constrs_str<T>(constrs: [T], elt: fn(T) -> str) -> str {
1812
    let mut s = "", colon = true;
1813
    for constrs.each {|c|
B
Brian Anderson 已提交
1814
        if colon { s += " : "; colon = false; } else { s += ", "; }
1815
        s += elt(c);
1816 1817 1818 1819
    }
    ret s;
}

1820 1821 1822 1823
fn fn_arg_idx_to_str(decl: ast::fn_decl, &&idx: uint) -> str {
    decl.inputs[idx].ident
}

1824 1825
fn opt_proto_to_str(opt_p: option<ast::proto>) -> str {
    alt opt_p {
1826
      none { "fn" }
1827 1828 1829 1830
      some(p) { proto_to_str(p) }
    }
}

1831
fn proto_to_str(p: ast::proto) -> str {
M
Marijn Haverbeke 已提交
1832
    ret alt p {
1833
      ast::proto_bare { "native fn" }
1834
      ast::proto_any { "fn" }
1835 1836 1837
      ast::proto_block { "fn&" }
      ast::proto_uniq { "fn~" }
      ast::proto_box { "fn@" }
1838
    };
1839 1840
}

1841 1842 1843 1844 1845 1846 1847 1848 1849
//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//