]> git.proxmox.com Git - rustc.git/blob - src/libsyntax/print/pprust.rs
Imported Upstream version 1.7.0+dfsg1
[rustc.git] / src / libsyntax / print / pprust.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 pub use self::AnnNode::*;
12
13 use abi;
14 use ast::{self, TokenTree};
15 use ast::{RegionTyParamBound, TraitTyParamBound, TraitBoundModifier};
16 use ast::Attribute;
17 use attr::ThinAttributesExt;
18 use util::parser::AssocOp;
19 use attr;
20 use attr::{AttrMetaMethods, AttributeMethods};
21 use codemap::{self, CodeMap, BytePos};
22 use errors;
23 use parse::token::{self, BinOpToken, Token, InternedString};
24 use parse::lexer::comments;
25 use parse;
26 use print::pp::{self, break_offset, word, space, zerobreak, hardbreak};
27 use print::pp::{Breaks, eof};
28 use print::pp::Breaks::{Consistent, Inconsistent};
29 use ptr::P;
30 use std_inject;
31
32 use std::ascii;
33 use std::io::{self, Write, Read};
34 use std::iter;
35
36 pub enum AnnNode<'a> {
37 NodeIdent(&'a ast::Ident),
38 NodeName(&'a ast::Name),
39 NodeBlock(&'a ast::Block),
40 NodeItem(&'a ast::Item),
41 NodeSubItem(ast::NodeId),
42 NodeExpr(&'a ast::Expr),
43 NodePat(&'a ast::Pat),
44 }
45
46 pub trait PpAnn {
47 fn pre(&self, _state: &mut State, _node: AnnNode) -> io::Result<()> { Ok(()) }
48 fn post(&self, _state: &mut State, _node: AnnNode) -> io::Result<()> { Ok(()) }
49 }
50
51 #[derive(Copy, Clone)]
52 pub struct NoAnn;
53
54 impl PpAnn for NoAnn {}
55
56 #[derive(Copy, Clone)]
57 pub struct CurrentCommentAndLiteral {
58 pub cur_cmnt: usize,
59 pub cur_lit: usize,
60 }
61
62 pub struct State<'a> {
63 pub s: pp::Printer<'a>,
64 cm: Option<&'a CodeMap>,
65 comments: Option<Vec<comments::Comment> >,
66 literals: Option<Vec<comments::Literal> >,
67 cur_cmnt_and_lit: CurrentCommentAndLiteral,
68 boxes: Vec<pp::Breaks>,
69 ann: &'a (PpAnn+'a),
70 }
71
72 pub fn rust_printer<'a>(writer: Box<Write+'a>) -> State<'a> {
73 static NO_ANN: NoAnn = NoAnn;
74 rust_printer_annotated(writer, &NO_ANN)
75 }
76
77 pub fn rust_printer_annotated<'a>(writer: Box<Write+'a>,
78 ann: &'a PpAnn) -> State<'a> {
79 State {
80 s: pp::mk_printer(writer, DEFAULT_COLUMNS),
81 cm: None,
82 comments: None,
83 literals: None,
84 cur_cmnt_and_lit: CurrentCommentAndLiteral {
85 cur_cmnt: 0,
86 cur_lit: 0
87 },
88 boxes: Vec::new(),
89 ann: ann,
90 }
91 }
92
93 pub const INDENT_UNIT: usize = 4;
94
95 pub const DEFAULT_COLUMNS: usize = 78;
96
97 /// Requires you to pass an input filename and reader so that
98 /// it can scan the input text for comments and literals to
99 /// copy forward.
100 pub fn print_crate<'a>(cm: &'a CodeMap,
101 span_diagnostic: &errors::Handler,
102 krate: &ast::Crate,
103 filename: String,
104 input: &mut Read,
105 out: Box<Write+'a>,
106 ann: &'a PpAnn,
107 is_expanded: bool) -> io::Result<()> {
108 let mut s = State::new_from_input(cm,
109 span_diagnostic,
110 filename,
111 input,
112 out,
113 ann,
114 is_expanded);
115 if is_expanded && !std_inject::no_std(krate) {
116 // We need to print `#![no_std]` (and its feature gate) so that
117 // compiling pretty-printed source won't inject libstd again.
118 // However we don't want these attributes in the AST because
119 // of the feature gate, so we fake them up here.
120
121 // #![feature(prelude_import)]
122 let prelude_import_meta = attr::mk_word_item(InternedString::new("prelude_import"));
123 let list = attr::mk_list_item(InternedString::new("feature"),
124 vec![prelude_import_meta]);
125 let fake_attr = attr::mk_attr_inner(attr::mk_attr_id(), list);
126 try!(s.print_attribute(&fake_attr));
127
128 // #![no_std]
129 let no_std_meta = attr::mk_word_item(InternedString::new("no_std"));
130 let fake_attr = attr::mk_attr_inner(attr::mk_attr_id(), no_std_meta);
131 try!(s.print_attribute(&fake_attr));
132 }
133
134 try!(s.print_mod(&krate.module, &krate.attrs));
135 try!(s.print_remaining_comments());
136 eof(&mut s.s)
137 }
138
139 impl<'a> State<'a> {
140 pub fn new_from_input(cm: &'a CodeMap,
141 span_diagnostic: &errors::Handler,
142 filename: String,
143 input: &mut Read,
144 out: Box<Write+'a>,
145 ann: &'a PpAnn,
146 is_expanded: bool) -> State<'a> {
147 let (cmnts, lits) = comments::gather_comments_and_literals(
148 span_diagnostic,
149 filename,
150 input);
151
152 State::new(
153 cm,
154 out,
155 ann,
156 Some(cmnts),
157 // If the code is post expansion, don't use the table of
158 // literals, since it doesn't correspond with the literals
159 // in the AST anymore.
160 if is_expanded { None } else { Some(lits) })
161 }
162
163 pub fn new(cm: &'a CodeMap,
164 out: Box<Write+'a>,
165 ann: &'a PpAnn,
166 comments: Option<Vec<comments::Comment>>,
167 literals: Option<Vec<comments::Literal>>) -> State<'a> {
168 State {
169 s: pp::mk_printer(out, DEFAULT_COLUMNS),
170 cm: Some(cm),
171 comments: comments,
172 literals: literals,
173 cur_cmnt_and_lit: CurrentCommentAndLiteral {
174 cur_cmnt: 0,
175 cur_lit: 0
176 },
177 boxes: Vec::new(),
178 ann: ann,
179 }
180 }
181 }
182
183 pub fn to_string<F>(f: F) -> String where
184 F: FnOnce(&mut State) -> io::Result<()>,
185 {
186 let mut wr = Vec::new();
187 {
188 let mut printer = rust_printer(Box::new(&mut wr));
189 f(&mut printer).unwrap();
190 eof(&mut printer.s).unwrap();
191 }
192 String::from_utf8(wr).unwrap()
193 }
194
195 pub fn binop_to_string(op: BinOpToken) -> &'static str {
196 match op {
197 token::Plus => "+",
198 token::Minus => "-",
199 token::Star => "*",
200 token::Slash => "/",
201 token::Percent => "%",
202 token::Caret => "^",
203 token::And => "&",
204 token::Or => "|",
205 token::Shl => "<<",
206 token::Shr => ">>",
207 }
208 }
209
210 pub fn token_to_string(tok: &Token) -> String {
211 match *tok {
212 token::Eq => "=".to_string(),
213 token::Lt => "<".to_string(),
214 token::Le => "<=".to_string(),
215 token::EqEq => "==".to_string(),
216 token::Ne => "!=".to_string(),
217 token::Ge => ">=".to_string(),
218 token::Gt => ">".to_string(),
219 token::Not => "!".to_string(),
220 token::Tilde => "~".to_string(),
221 token::OrOr => "||".to_string(),
222 token::AndAnd => "&&".to_string(),
223 token::BinOp(op) => binop_to_string(op).to_string(),
224 token::BinOpEq(op) => format!("{}=", binop_to_string(op)),
225
226 /* Structural symbols */
227 token::At => "@".to_string(),
228 token::Dot => ".".to_string(),
229 token::DotDot => "..".to_string(),
230 token::DotDotDot => "...".to_string(),
231 token::Comma => ",".to_string(),
232 token::Semi => ";".to_string(),
233 token::Colon => ":".to_string(),
234 token::ModSep => "::".to_string(),
235 token::RArrow => "->".to_string(),
236 token::LArrow => "<-".to_string(),
237 token::FatArrow => "=>".to_string(),
238 token::OpenDelim(token::Paren) => "(".to_string(),
239 token::CloseDelim(token::Paren) => ")".to_string(),
240 token::OpenDelim(token::Bracket) => "[".to_string(),
241 token::CloseDelim(token::Bracket) => "]".to_string(),
242 token::OpenDelim(token::Brace) => "{".to_string(),
243 token::CloseDelim(token::Brace) => "}".to_string(),
244 token::Pound => "#".to_string(),
245 token::Dollar => "$".to_string(),
246 token::Question => "?".to_string(),
247
248 /* Literals */
249 token::Literal(lit, suf) => {
250 let mut out = match lit {
251 token::Byte(b) => format!("b'{}'", b),
252 token::Char(c) => format!("'{}'", c),
253 token::Float(c) => c.to_string(),
254 token::Integer(c) => c.to_string(),
255 token::Str_(s) => format!("\"{}\"", s),
256 token::StrRaw(s, n) => format!("r{delim}\"{string}\"{delim}",
257 delim=repeat("#", n),
258 string=s),
259 token::ByteStr(v) => format!("b\"{}\"", v),
260 token::ByteStrRaw(s, n) => format!("br{delim}\"{string}\"{delim}",
261 delim=repeat("#", n),
262 string=s),
263 };
264
265 if let Some(s) = suf {
266 out.push_str(&s.as_str())
267 }
268
269 out
270 }
271
272 /* Name components */
273 token::Ident(s, _) => s.to_string(),
274 token::Lifetime(s) => s.to_string(),
275 token::Underscore => "_".to_string(),
276
277 /* Other */
278 token::DocComment(s) => s.to_string(),
279 token::SubstNt(s, _) => format!("${}", s),
280 token::MatchNt(s, t, _, _) => format!("${}:{}", s, t),
281 token::Eof => "<eof>".to_string(),
282 token::Whitespace => " ".to_string(),
283 token::Comment => "/* */".to_string(),
284 token::Shebang(s) => format!("/* shebang: {}*/", s),
285
286 token::SpecialVarNt(var) => format!("${}", var.as_str()),
287
288 token::Interpolated(ref nt) => match *nt {
289 token::NtExpr(ref e) => expr_to_string(&**e),
290 token::NtMeta(ref e) => meta_item_to_string(&**e),
291 token::NtTy(ref e) => ty_to_string(&**e),
292 token::NtPath(ref e) => path_to_string(&**e),
293 token::NtItem(ref e) => item_to_string(&**e),
294 token::NtBlock(ref e) => block_to_string(&**e),
295 token::NtStmt(ref e) => stmt_to_string(&**e),
296 token::NtPat(ref e) => pat_to_string(&**e),
297 token::NtIdent(ref e, _) => ident_to_string(e.node),
298 token::NtTT(ref e) => tt_to_string(&**e),
299 token::NtArm(ref e) => arm_to_string(&*e),
300 token::NtImplItem(ref e) => impl_item_to_string(&**e),
301 token::NtTraitItem(ref e) => trait_item_to_string(&**e),
302 token::NtGenerics(ref e) => generics_to_string(&*e),
303 token::NtWhereClause(ref e) => where_clause_to_string(&*e),
304 token::NtArg(ref e) => arg_to_string(&*e),
305 }
306 }
307 }
308
309 pub fn ty_to_string(ty: &ast::Ty) -> String {
310 to_string(|s| s.print_type(ty))
311 }
312
313 pub fn bounds_to_string(bounds: &[ast::TyParamBound]) -> String {
314 to_string(|s| s.print_bounds("", bounds))
315 }
316
317 pub fn pat_to_string(pat: &ast::Pat) -> String {
318 to_string(|s| s.print_pat(pat))
319 }
320
321 pub fn arm_to_string(arm: &ast::Arm) -> String {
322 to_string(|s| s.print_arm(arm))
323 }
324
325 pub fn expr_to_string(e: &ast::Expr) -> String {
326 to_string(|s| s.print_expr(e))
327 }
328
329 pub fn lifetime_to_string(e: &ast::Lifetime) -> String {
330 to_string(|s| s.print_lifetime(e))
331 }
332
333 pub fn tt_to_string(tt: &ast::TokenTree) -> String {
334 to_string(|s| s.print_tt(tt))
335 }
336
337 pub fn tts_to_string(tts: &[ast::TokenTree]) -> String {
338 to_string(|s| s.print_tts(tts))
339 }
340
341 pub fn stmt_to_string(stmt: &ast::Stmt) -> String {
342 to_string(|s| s.print_stmt(stmt))
343 }
344
345 pub fn attr_to_string(attr: &ast::Attribute) -> String {
346 to_string(|s| s.print_attribute(attr))
347 }
348
349 pub fn item_to_string(i: &ast::Item) -> String {
350 to_string(|s| s.print_item(i))
351 }
352
353 pub fn impl_item_to_string(i: &ast::ImplItem) -> String {
354 to_string(|s| s.print_impl_item(i))
355 }
356
357 pub fn trait_item_to_string(i: &ast::TraitItem) -> String {
358 to_string(|s| s.print_trait_item(i))
359 }
360
361 pub fn generics_to_string(generics: &ast::Generics) -> String {
362 to_string(|s| s.print_generics(generics))
363 }
364
365 pub fn where_clause_to_string(i: &ast::WhereClause) -> String {
366 to_string(|s| s.print_where_clause(i))
367 }
368
369 pub fn fn_block_to_string(p: &ast::FnDecl) -> String {
370 to_string(|s| s.print_fn_block_args(p))
371 }
372
373 pub fn path_to_string(p: &ast::Path) -> String {
374 to_string(|s| s.print_path(p, false, 0))
375 }
376
377 pub fn ident_to_string(id: ast::Ident) -> String {
378 to_string(|s| s.print_ident(id))
379 }
380
381 pub fn fun_to_string(decl: &ast::FnDecl,
382 unsafety: ast::Unsafety,
383 constness: ast::Constness,
384 name: ast::Ident,
385 opt_explicit_self: Option<&ast::ExplicitSelf_>,
386 generics: &ast::Generics)
387 -> String {
388 to_string(|s| {
389 try!(s.head(""));
390 try!(s.print_fn(decl, unsafety, constness, abi::Rust, Some(name),
391 generics, opt_explicit_self, ast::Inherited));
392 try!(s.end()); // Close the head box
393 s.end() // Close the outer box
394 })
395 }
396
397 pub fn block_to_string(blk: &ast::Block) -> String {
398 to_string(|s| {
399 // containing cbox, will be closed by print-block at }
400 try!(s.cbox(INDENT_UNIT));
401 // head-ibox, will be closed by print-block after {
402 try!(s.ibox(0));
403 s.print_block(blk)
404 })
405 }
406
407 pub fn meta_item_to_string(mi: &ast::MetaItem) -> String {
408 to_string(|s| s.print_meta_item(mi))
409 }
410
411 pub fn attribute_to_string(attr: &ast::Attribute) -> String {
412 to_string(|s| s.print_attribute(attr))
413 }
414
415 pub fn lit_to_string(l: &ast::Lit) -> String {
416 to_string(|s| s.print_literal(l))
417 }
418
419 pub fn explicit_self_to_string(explicit_self: &ast::ExplicitSelf_) -> String {
420 to_string(|s| s.print_explicit_self(explicit_self, ast::MutImmutable).map(|_| {}))
421 }
422
423 pub fn variant_to_string(var: &ast::Variant) -> String {
424 to_string(|s| s.print_variant(var))
425 }
426
427 pub fn arg_to_string(arg: &ast::Arg) -> String {
428 to_string(|s| s.print_arg(arg))
429 }
430
431 pub fn mac_to_string(arg: &ast::Mac) -> String {
432 to_string(|s| s.print_mac(arg, ::parse::token::Paren))
433 }
434
435 pub fn visibility_qualified(vis: ast::Visibility, s: &str) -> String {
436 match vis {
437 ast::Public => format!("pub {}", s),
438 ast::Inherited => s.to_string()
439 }
440 }
441
442 fn needs_parentheses(expr: &ast::Expr) -> bool {
443 match expr.node {
444 ast::ExprAssign(..) | ast::ExprBinary(..) |
445 ast::ExprClosure(..) |
446 ast::ExprAssignOp(..) | ast::ExprCast(..) |
447 ast::ExprInPlace(..) | ast::ExprType(..) => true,
448 _ => false,
449 }
450 }
451
452 pub trait PrintState<'a> {
453 fn writer(&mut self) -> &mut pp::Printer<'a>;
454 fn boxes(&mut self) -> &mut Vec<pp::Breaks>;
455 fn comments(&mut self) -> &mut Option<Vec<comments::Comment>>;
456 fn cur_cmnt_and_lit(&mut self) -> &mut CurrentCommentAndLiteral;
457 fn literals(&self) -> &Option<Vec<comments::Literal>>;
458
459 fn word_space(&mut self, w: &str) -> io::Result<()> {
460 try!(word(self.writer(), w));
461 space(self.writer())
462 }
463
464 fn popen(&mut self) -> io::Result<()> { word(self.writer(), "(") }
465
466 fn pclose(&mut self) -> io::Result<()> { word(self.writer(), ")") }
467
468 fn is_begin(&mut self) -> bool {
469 match self.writer().last_token() {
470 pp::Token::Begin(_) => true,
471 _ => false,
472 }
473 }
474
475 fn is_end(&mut self) -> bool {
476 match self.writer().last_token() {
477 pp::Token::End => true,
478 _ => false,
479 }
480 }
481
482 // is this the beginning of a line?
483 fn is_bol(&mut self) -> bool {
484 self.writer().last_token().is_eof() || self.writer().last_token().is_hardbreak_tok()
485 }
486
487 fn hardbreak_if_not_bol(&mut self) -> io::Result<()> {
488 if !self.is_bol() {
489 try!(hardbreak(self.writer()))
490 }
491 Ok(())
492 }
493
494 // "raw box"
495 fn rbox(&mut self, u: usize, b: pp::Breaks) -> io::Result<()> {
496 self.boxes().push(b);
497 pp::rbox(self.writer(), u, b)
498 }
499
500 fn ibox(&mut self, u: usize) -> io::Result<()> {
501 self.boxes().push(pp::Breaks::Inconsistent);
502 pp::ibox(self.writer(), u)
503 }
504
505 fn end(&mut self) -> io::Result<()> {
506 self.boxes().pop().unwrap();
507 pp::end(self.writer())
508 }
509
510 fn commasep<T, F>(&mut self, b: Breaks, elts: &[T], mut op: F) -> io::Result<()>
511 where F: FnMut(&mut Self, &T) -> io::Result<()>,
512 {
513 try!(self.rbox(0, b));
514 let mut first = true;
515 for elt in elts {
516 if first { first = false; } else { try!(self.word_space(",")); }
517 try!(op(self, elt));
518 }
519 self.end()
520 }
521
522 fn next_lit(&mut self, pos: BytePos) -> Option<comments::Literal> {
523 let mut cur_lit = self.cur_cmnt_and_lit().cur_lit;
524
525 let mut result = None;
526
527 if let &Some(ref lits) = self.literals()
528 {
529 while cur_lit < lits.len() {
530 let ltrl = (*lits)[cur_lit].clone();
531 if ltrl.pos > pos { break; }
532 cur_lit += 1;
533 if ltrl.pos == pos {
534 result = Some(ltrl);
535 break;
536 }
537 }
538 }
539
540 self.cur_cmnt_and_lit().cur_lit = cur_lit;
541 result
542 }
543
544 fn maybe_print_comment(&mut self, pos: BytePos) -> io::Result<()> {
545 loop {
546 match self.next_comment() {
547 Some(ref cmnt) => {
548 if (*cmnt).pos < pos {
549 try!(self.print_comment(cmnt));
550 self.cur_cmnt_and_lit().cur_cmnt += 1;
551 } else { break; }
552 }
553 _ => break
554 }
555 }
556 Ok(())
557 }
558
559 fn print_comment(&mut self,
560 cmnt: &comments::Comment) -> io::Result<()> {
561 match cmnt.style {
562 comments::Mixed => {
563 assert_eq!(cmnt.lines.len(), 1);
564 try!(zerobreak(self.writer()));
565 try!(word(self.writer(), &cmnt.lines[0]));
566 zerobreak(self.writer())
567 }
568 comments::Isolated => {
569 try!(self.hardbreak_if_not_bol());
570 for line in &cmnt.lines {
571 // Don't print empty lines because they will end up as trailing
572 // whitespace
573 if !line.is_empty() {
574 try!(word(self.writer(), &line[..]));
575 }
576 try!(hardbreak(self.writer()));
577 }
578 Ok(())
579 }
580 comments::Trailing => {
581 try!(word(self.writer(), " "));
582 if cmnt.lines.len() == 1 {
583 try!(word(self.writer(), &cmnt.lines[0]));
584 hardbreak(self.writer())
585 } else {
586 try!(self.ibox(0));
587 for line in &cmnt.lines {
588 if !line.is_empty() {
589 try!(word(self.writer(), &line[..]));
590 }
591 try!(hardbreak(self.writer()));
592 }
593 self.end()
594 }
595 }
596 comments::BlankLine => {
597 // We need to do at least one, possibly two hardbreaks.
598 let is_semi = match self.writer().last_token() {
599 pp::Token::String(s, _) => ";" == s,
600 _ => false
601 };
602 if is_semi || self.is_begin() || self.is_end() {
603 try!(hardbreak(self.writer()));
604 }
605 hardbreak(self.writer())
606 }
607 }
608 }
609
610 fn next_comment(&mut self) -> Option<comments::Comment> {
611 let cur_cmnt = self.cur_cmnt_and_lit().cur_cmnt;
612 match *self.comments() {
613 Some(ref cmnts) => {
614 if cur_cmnt < cmnts.len() {
615 Some(cmnts[cur_cmnt].clone())
616 } else {
617 None
618 }
619 }
620 _ => None
621 }
622 }
623
624 fn print_literal(&mut self, lit: &ast::Lit) -> io::Result<()> {
625 try!(self.maybe_print_comment(lit.span.lo));
626 match self.next_lit(lit.span.lo) {
627 Some(ref ltrl) => {
628 return word(self.writer(), &(*ltrl).lit);
629 }
630 _ => ()
631 }
632 match lit.node {
633 ast::LitStr(ref st, style) => self.print_string(&st, style),
634 ast::LitByte(byte) => {
635 let mut res = String::from("b'");
636 res.extend(ascii::escape_default(byte).map(|c| c as char));
637 res.push('\'');
638 word(self.writer(), &res[..])
639 }
640 ast::LitChar(ch) => {
641 let mut res = String::from("'");
642 res.extend(ch.escape_default());
643 res.push('\'');
644 word(self.writer(), &res[..])
645 }
646 ast::LitInt(i, t) => {
647 match t {
648 ast::SignedIntLit(st, ast::Plus) => {
649 word(self.writer(),
650 &st.val_to_string(i as i64))
651 }
652 ast::SignedIntLit(st, ast::Minus) => {
653 let istr = st.val_to_string(-(i as i64));
654 word(self.writer(),
655 &format!("-{}", istr))
656 }
657 ast::UnsignedIntLit(ut) => {
658 word(self.writer(), &ut.val_to_string(i))
659 }
660 ast::UnsuffixedIntLit(ast::Plus) => {
661 word(self.writer(), &format!("{}", i))
662 }
663 ast::UnsuffixedIntLit(ast::Minus) => {
664 word(self.writer(), &format!("-{}", i))
665 }
666 }
667 }
668 ast::LitFloat(ref f, t) => {
669 word(self.writer(),
670 &format!(
671 "{}{}",
672 &f,
673 t.ty_to_string()))
674 }
675 ast::LitFloatUnsuffixed(ref f) => word(self.writer(), &f[..]),
676 ast::LitBool(val) => {
677 if val { word(self.writer(), "true") } else { word(self.writer(), "false") }
678 }
679 ast::LitByteStr(ref v) => {
680 let mut escaped: String = String::new();
681 for &ch in v.iter() {
682 escaped.extend(ascii::escape_default(ch)
683 .map(|c| c as char));
684 }
685 word(self.writer(), &format!("b\"{}\"", escaped))
686 }
687 }
688 }
689
690 fn print_string(&mut self, st: &str,
691 style: ast::StrStyle) -> io::Result<()> {
692 let st = match style {
693 ast::CookedStr => {
694 (format!("\"{}\"", st.escape_default()))
695 }
696 ast::RawStr(n) => {
697 (format!("r{delim}\"{string}\"{delim}",
698 delim=repeat("#", n),
699 string=st))
700 }
701 };
702 word(self.writer(), &st[..])
703 }
704
705 fn print_inner_attributes(&mut self,
706 attrs: &[ast::Attribute]) -> io::Result<()> {
707 self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, true)
708 }
709
710 fn print_inner_attributes_no_trailing_hardbreak(&mut self,
711 attrs: &[ast::Attribute])
712 -> io::Result<()> {
713 self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, false)
714 }
715
716 fn print_outer_attributes(&mut self,
717 attrs: &[ast::Attribute]) -> io::Result<()> {
718 self.print_either_attributes(attrs, ast::AttrStyle::Outer, false, true)
719 }
720
721 fn print_inner_attributes_inline(&mut self,
722 attrs: &[ast::Attribute]) -> io::Result<()> {
723 self.print_either_attributes(attrs, ast::AttrStyle::Inner, true, true)
724 }
725
726 fn print_outer_attributes_inline(&mut self,
727 attrs: &[ast::Attribute]) -> io::Result<()> {
728 self.print_either_attributes(attrs, ast::AttrStyle::Outer, true, true)
729 }
730
731 fn print_either_attributes(&mut self,
732 attrs: &[ast::Attribute],
733 kind: ast::AttrStyle,
734 is_inline: bool,
735 trailing_hardbreak: bool) -> io::Result<()> {
736 let mut count = 0;
737 for attr in attrs {
738 if attr.node.style == kind {
739 try!(self.print_attribute_inline(attr, is_inline));
740 if is_inline {
741 try!(self.nbsp());
742 }
743 count += 1;
744 }
745 }
746 if count > 0 && trailing_hardbreak && !is_inline {
747 try!(self.hardbreak_if_not_bol());
748 }
749 Ok(())
750 }
751
752 fn print_attribute(&mut self, attr: &ast::Attribute) -> io::Result<()> {
753 self.print_attribute_inline(attr, false)
754 }
755
756 fn print_attribute_inline(&mut self, attr: &ast::Attribute,
757 is_inline: bool) -> io::Result<()> {
758 if !is_inline {
759 try!(self.hardbreak_if_not_bol());
760 }
761 try!(self.maybe_print_comment(attr.span.lo));
762 if attr.node.is_sugared_doc {
763 word(self.writer(), &attr.value_str().unwrap())
764 } else {
765 match attr.node.style {
766 ast::AttrStyle::Inner => try!(word(self.writer(), "#![")),
767 ast::AttrStyle::Outer => try!(word(self.writer(), "#[")),
768 }
769 try!(self.print_meta_item(&*attr.meta()));
770 word(self.writer(), "]")
771 }
772 }
773
774 fn print_meta_item(&mut self, item: &ast::MetaItem) -> io::Result<()> {
775 try!(self.ibox(INDENT_UNIT));
776 match item.node {
777 ast::MetaWord(ref name) => {
778 try!(word(self.writer(), &name));
779 }
780 ast::MetaNameValue(ref name, ref value) => {
781 try!(self.word_space(&name[..]));
782 try!(self.word_space("="));
783 try!(self.print_literal(value));
784 }
785 ast::MetaList(ref name, ref items) => {
786 try!(word(self.writer(), &name));
787 try!(self.popen());
788 try!(self.commasep(Consistent,
789 &items[..],
790 |s, i| s.print_meta_item(&**i)));
791 try!(self.pclose());
792 }
793 }
794 self.end()
795 }
796
797 fn space_if_not_bol(&mut self) -> io::Result<()> {
798 if !self.is_bol() { try!(space(self.writer())); }
799 Ok(())
800 }
801
802 fn nbsp(&mut self) -> io::Result<()> { word(self.writer(), " ") }
803 }
804
805 impl<'a> PrintState<'a> for State<'a> {
806 fn writer(&mut self) -> &mut pp::Printer<'a> {
807 &mut self.s
808 }
809
810 fn boxes(&mut self) -> &mut Vec<pp::Breaks> {
811 &mut self.boxes
812 }
813
814 fn comments(&mut self) -> &mut Option<Vec<comments::Comment>> {
815 &mut self.comments
816 }
817
818 fn cur_cmnt_and_lit(&mut self) -> &mut CurrentCommentAndLiteral {
819 &mut self.cur_cmnt_and_lit
820 }
821
822 fn literals(&self) -> &Option<Vec<comments::Literal>> {
823 &self.literals
824 }
825 }
826
827 impl<'a> State<'a> {
828 pub fn cbox(&mut self, u: usize) -> io::Result<()> {
829 self.boxes.push(pp::Breaks::Consistent);
830 pp::cbox(&mut self.s, u)
831 }
832
833 pub fn word_nbsp(&mut self, w: &str) -> io::Result<()> {
834 try!(word(&mut self.s, w));
835 self.nbsp()
836 }
837
838 pub fn head(&mut self, w: &str) -> io::Result<()> {
839 // outer-box is consistent
840 try!(self.cbox(INDENT_UNIT));
841 // head-box is inconsistent
842 try!(self.ibox(w.len() + 1));
843 // keyword that starts the head
844 if !w.is_empty() {
845 try!(self.word_nbsp(w));
846 }
847 Ok(())
848 }
849
850 pub fn bopen(&mut self) -> io::Result<()> {
851 try!(word(&mut self.s, "{"));
852 self.end() // close the head-box
853 }
854
855 pub fn bclose_(&mut self, span: codemap::Span,
856 indented: usize) -> io::Result<()> {
857 self.bclose_maybe_open(span, indented, true)
858 }
859 pub fn bclose_maybe_open(&mut self, span: codemap::Span,
860 indented: usize, close_box: bool) -> io::Result<()> {
861 try!(self.maybe_print_comment(span.hi));
862 try!(self.break_offset_if_not_bol(1, -(indented as isize)));
863 try!(word(&mut self.s, "}"));
864 if close_box {
865 try!(self.end()); // close the outer-box
866 }
867 Ok(())
868 }
869 pub fn bclose(&mut self, span: codemap::Span) -> io::Result<()> {
870 self.bclose_(span, INDENT_UNIT)
871 }
872
873 pub fn in_cbox(&self) -> bool {
874 match self.boxes.last() {
875 Some(&last_box) => last_box == pp::Breaks::Consistent,
876 None => false
877 }
878 }
879
880 pub fn break_offset_if_not_bol(&mut self, n: usize,
881 off: isize) -> io::Result<()> {
882 if !self.is_bol() {
883 break_offset(&mut self.s, n, off)
884 } else {
885 if off != 0 && self.s.last_token().is_hardbreak_tok() {
886 // We do something pretty sketchy here: tuck the nonzero
887 // offset-adjustment we were going to deposit along with the
888 // break into the previous hardbreak.
889 self.s.replace_last_token(pp::hardbreak_tok_offset(off));
890 }
891 Ok(())
892 }
893 }
894
895 // Synthesizes a comment that was not textually present in the original source
896 // file.
897 pub fn synth_comment(&mut self, text: String) -> io::Result<()> {
898 try!(word(&mut self.s, "/*"));
899 try!(space(&mut self.s));
900 try!(word(&mut self.s, &text[..]));
901 try!(space(&mut self.s));
902 word(&mut self.s, "*/")
903 }
904
905
906
907 pub fn commasep_cmnt<T, F, G>(&mut self,
908 b: Breaks,
909 elts: &[T],
910 mut op: F,
911 mut get_span: G) -> io::Result<()> where
912 F: FnMut(&mut State, &T) -> io::Result<()>,
913 G: FnMut(&T) -> codemap::Span,
914 {
915 try!(self.rbox(0, b));
916 let len = elts.len();
917 let mut i = 0;
918 for elt in elts {
919 try!(self.maybe_print_comment(get_span(elt).hi));
920 try!(op(self, elt));
921 i += 1;
922 if i < len {
923 try!(word(&mut self.s, ","));
924 try!(self.maybe_print_trailing_comment(get_span(elt),
925 Some(get_span(&elts[i]).hi)));
926 try!(self.space_if_not_bol());
927 }
928 }
929 self.end()
930 }
931
932 pub fn commasep_exprs(&mut self, b: Breaks,
933 exprs: &[P<ast::Expr>]) -> io::Result<()> {
934 self.commasep_cmnt(b, exprs, |s, e| s.print_expr(&**e), |e| e.span)
935 }
936
937 pub fn print_mod(&mut self, _mod: &ast::Mod,
938 attrs: &[ast::Attribute]) -> io::Result<()> {
939 try!(self.print_inner_attributes(attrs));
940 for item in &_mod.items {
941 try!(self.print_item(&**item));
942 }
943 Ok(())
944 }
945
946 pub fn print_foreign_mod(&mut self, nmod: &ast::ForeignMod,
947 attrs: &[ast::Attribute]) -> io::Result<()> {
948 try!(self.print_inner_attributes(attrs));
949 for item in &nmod.items {
950 try!(self.print_foreign_item(&**item));
951 }
952 Ok(())
953 }
954
955 pub fn print_opt_lifetime(&mut self,
956 lifetime: &Option<ast::Lifetime>) -> io::Result<()> {
957 if let Some(l) = *lifetime {
958 try!(self.print_lifetime(&l));
959 try!(self.nbsp());
960 }
961 Ok(())
962 }
963
964 pub fn print_type(&mut self, ty: &ast::Ty) -> io::Result<()> {
965 try!(self.maybe_print_comment(ty.span.lo));
966 try!(self.ibox(0));
967 match ty.node {
968 ast::TyVec(ref ty) => {
969 try!(word(&mut self.s, "["));
970 try!(self.print_type(&**ty));
971 try!(word(&mut self.s, "]"));
972 }
973 ast::TyPtr(ref mt) => {
974 try!(word(&mut self.s, "*"));
975 match mt.mutbl {
976 ast::MutMutable => try!(self.word_nbsp("mut")),
977 ast::MutImmutable => try!(self.word_nbsp("const")),
978 }
979 try!(self.print_type(&*mt.ty));
980 }
981 ast::TyRptr(ref lifetime, ref mt) => {
982 try!(word(&mut self.s, "&"));
983 try!(self.print_opt_lifetime(lifetime));
984 try!(self.print_mt(mt));
985 }
986 ast::TyTup(ref elts) => {
987 try!(self.popen());
988 try!(self.commasep(Inconsistent, &elts[..],
989 |s, ty| s.print_type(&**ty)));
990 if elts.len() == 1 {
991 try!(word(&mut self.s, ","));
992 }
993 try!(self.pclose());
994 }
995 ast::TyParen(ref typ) => {
996 try!(self.popen());
997 try!(self.print_type(&**typ));
998 try!(self.pclose());
999 }
1000 ast::TyBareFn(ref f) => {
1001 let generics = ast::Generics {
1002 lifetimes: f.lifetimes.clone(),
1003 ty_params: P::empty(),
1004 where_clause: ast::WhereClause {
1005 id: ast::DUMMY_NODE_ID,
1006 predicates: Vec::new(),
1007 },
1008 };
1009 try!(self.print_ty_fn(f.abi,
1010 f.unsafety,
1011 &*f.decl,
1012 None,
1013 &generics,
1014 None));
1015 }
1016 ast::TyPath(None, ref path) => {
1017 try!(self.print_path(path, false, 0));
1018 }
1019 ast::TyPath(Some(ref qself), ref path) => {
1020 try!(self.print_qpath(path, qself, false))
1021 }
1022 ast::TyObjectSum(ref ty, ref bounds) => {
1023 try!(self.print_type(&**ty));
1024 try!(self.print_bounds("+", &bounds[..]));
1025 }
1026 ast::TyPolyTraitRef(ref bounds) => {
1027 try!(self.print_bounds("", &bounds[..]));
1028 }
1029 ast::TyFixedLengthVec(ref ty, ref v) => {
1030 try!(word(&mut self.s, "["));
1031 try!(self.print_type(&**ty));
1032 try!(word(&mut self.s, "; "));
1033 try!(self.print_expr(&**v));
1034 try!(word(&mut self.s, "]"));
1035 }
1036 ast::TyTypeof(ref e) => {
1037 try!(word(&mut self.s, "typeof("));
1038 try!(self.print_expr(&**e));
1039 try!(word(&mut self.s, ")"));
1040 }
1041 ast::TyInfer => {
1042 try!(word(&mut self.s, "_"));
1043 }
1044 ast::TyMac(ref m) => {
1045 try!(self.print_mac(m, token::Paren));
1046 }
1047 }
1048 self.end()
1049 }
1050
1051 pub fn print_foreign_item(&mut self,
1052 item: &ast::ForeignItem) -> io::Result<()> {
1053 try!(self.hardbreak_if_not_bol());
1054 try!(self.maybe_print_comment(item.span.lo));
1055 try!(self.print_outer_attributes(&item.attrs));
1056 match item.node {
1057 ast::ForeignItemFn(ref decl, ref generics) => {
1058 try!(self.head(""));
1059 try!(self.print_fn(decl, ast::Unsafety::Normal,
1060 ast::Constness::NotConst,
1061 abi::Rust, Some(item.ident),
1062 generics, None, item.vis));
1063 try!(self.end()); // end head-ibox
1064 try!(word(&mut self.s, ";"));
1065 self.end() // end the outer fn box
1066 }
1067 ast::ForeignItemStatic(ref t, m) => {
1068 try!(self.head(&visibility_qualified(item.vis,
1069 "static")));
1070 if m {
1071 try!(self.word_space("mut"));
1072 }
1073 try!(self.print_ident(item.ident));
1074 try!(self.word_space(":"));
1075 try!(self.print_type(&**t));
1076 try!(word(&mut self.s, ";"));
1077 try!(self.end()); // end the head-ibox
1078 self.end() // end the outer cbox
1079 }
1080 }
1081 }
1082
1083 fn print_associated_const(&mut self,
1084 ident: ast::Ident,
1085 ty: &ast::Ty,
1086 default: Option<&ast::Expr>,
1087 vis: ast::Visibility)
1088 -> io::Result<()>
1089 {
1090 try!(word(&mut self.s, &visibility_qualified(vis, "")));
1091 try!(self.word_space("const"));
1092 try!(self.print_ident(ident));
1093 try!(self.word_space(":"));
1094 try!(self.print_type(ty));
1095 if let Some(expr) = default {
1096 try!(space(&mut self.s));
1097 try!(self.word_space("="));
1098 try!(self.print_expr(expr));
1099 }
1100 word(&mut self.s, ";")
1101 }
1102
1103 fn print_associated_type(&mut self,
1104 ident: ast::Ident,
1105 bounds: Option<&ast::TyParamBounds>,
1106 ty: Option<&ast::Ty>)
1107 -> io::Result<()> {
1108 try!(self.word_space("type"));
1109 try!(self.print_ident(ident));
1110 if let Some(bounds) = bounds {
1111 try!(self.print_bounds(":", bounds));
1112 }
1113 if let Some(ty) = ty {
1114 try!(space(&mut self.s));
1115 try!(self.word_space("="));
1116 try!(self.print_type(ty));
1117 }
1118 word(&mut self.s, ";")
1119 }
1120
1121 /// Pretty-print an item
1122 pub fn print_item(&mut self, item: &ast::Item) -> io::Result<()> {
1123 try!(self.hardbreak_if_not_bol());
1124 try!(self.maybe_print_comment(item.span.lo));
1125 try!(self.print_outer_attributes(&item.attrs));
1126 try!(self.ann.pre(self, NodeItem(item)));
1127 match item.node {
1128 ast::ItemExternCrate(ref optional_path) => {
1129 try!(self.head(&visibility_qualified(item.vis,
1130 "extern crate")));
1131 if let Some(p) = *optional_path {
1132 let val = p.as_str();
1133 if val.contains("-") {
1134 try!(self.print_string(&val, ast::CookedStr));
1135 } else {
1136 try!(self.print_name(p));
1137 }
1138 try!(space(&mut self.s));
1139 try!(word(&mut self.s, "as"));
1140 try!(space(&mut self.s));
1141 }
1142 try!(self.print_ident(item.ident));
1143 try!(word(&mut self.s, ";"));
1144 try!(self.end()); // end inner head-block
1145 try!(self.end()); // end outer head-block
1146 }
1147 ast::ItemUse(ref vp) => {
1148 try!(self.head(&visibility_qualified(item.vis,
1149 "use")));
1150 try!(self.print_view_path(&**vp));
1151 try!(word(&mut self.s, ";"));
1152 try!(self.end()); // end inner head-block
1153 try!(self.end()); // end outer head-block
1154 }
1155 ast::ItemStatic(ref ty, m, ref expr) => {
1156 try!(self.head(&visibility_qualified(item.vis,
1157 "static")));
1158 if m == ast::MutMutable {
1159 try!(self.word_space("mut"));
1160 }
1161 try!(self.print_ident(item.ident));
1162 try!(self.word_space(":"));
1163 try!(self.print_type(&**ty));
1164 try!(space(&mut self.s));
1165 try!(self.end()); // end the head-ibox
1166
1167 try!(self.word_space("="));
1168 try!(self.print_expr(&**expr));
1169 try!(word(&mut self.s, ";"));
1170 try!(self.end()); // end the outer cbox
1171 }
1172 ast::ItemConst(ref ty, ref expr) => {
1173 try!(self.head(&visibility_qualified(item.vis,
1174 "const")));
1175 try!(self.print_ident(item.ident));
1176 try!(self.word_space(":"));
1177 try!(self.print_type(&**ty));
1178 try!(space(&mut self.s));
1179 try!(self.end()); // end the head-ibox
1180
1181 try!(self.word_space("="));
1182 try!(self.print_expr(&**expr));
1183 try!(word(&mut self.s, ";"));
1184 try!(self.end()); // end the outer cbox
1185 }
1186 ast::ItemFn(ref decl, unsafety, constness, abi, ref typarams, ref body) => {
1187 try!(self.head(""));
1188 try!(self.print_fn(
1189 decl,
1190 unsafety,
1191 constness,
1192 abi,
1193 Some(item.ident),
1194 typarams,
1195 None,
1196 item.vis
1197 ));
1198 try!(word(&mut self.s, " "));
1199 try!(self.print_block_with_attrs(&**body, &item.attrs));
1200 }
1201 ast::ItemMod(ref _mod) => {
1202 try!(self.head(&visibility_qualified(item.vis,
1203 "mod")));
1204 try!(self.print_ident(item.ident));
1205 try!(self.nbsp());
1206 try!(self.bopen());
1207 try!(self.print_mod(_mod, &item.attrs));
1208 try!(self.bclose(item.span));
1209 }
1210 ast::ItemForeignMod(ref nmod) => {
1211 try!(self.head("extern"));
1212 try!(self.word_nbsp(&nmod.abi.to_string()));
1213 try!(self.bopen());
1214 try!(self.print_foreign_mod(nmod, &item.attrs));
1215 try!(self.bclose(item.span));
1216 }
1217 ast::ItemTy(ref ty, ref params) => {
1218 try!(self.ibox(INDENT_UNIT));
1219 try!(self.ibox(0));
1220 try!(self.word_nbsp(&visibility_qualified(item.vis, "type")));
1221 try!(self.print_ident(item.ident));
1222 try!(self.print_generics(params));
1223 try!(self.end()); // end the inner ibox
1224
1225 try!(self.print_where_clause(&params.where_clause));
1226 try!(space(&mut self.s));
1227 try!(self.word_space("="));
1228 try!(self.print_type(&**ty));
1229 try!(word(&mut self.s, ";"));
1230 try!(self.end()); // end the outer ibox
1231 }
1232 ast::ItemEnum(ref enum_definition, ref params) => {
1233 try!(self.print_enum_def(
1234 enum_definition,
1235 params,
1236 item.ident,
1237 item.span,
1238 item.vis
1239 ));
1240 }
1241 ast::ItemStruct(ref struct_def, ref generics) => {
1242 try!(self.head(&visibility_qualified(item.vis,"struct")));
1243 try!(self.print_struct(&struct_def, generics, item.ident, item.span, true));
1244 }
1245
1246 ast::ItemDefaultImpl(unsafety, ref trait_ref) => {
1247 try!(self.head(""));
1248 try!(self.print_visibility(item.vis));
1249 try!(self.print_unsafety(unsafety));
1250 try!(self.word_nbsp("impl"));
1251 try!(self.print_trait_ref(trait_ref));
1252 try!(space(&mut self.s));
1253 try!(self.word_space("for"));
1254 try!(self.word_space(".."));
1255 try!(self.bopen());
1256 try!(self.bclose(item.span));
1257 }
1258 ast::ItemImpl(unsafety,
1259 polarity,
1260 ref generics,
1261 ref opt_trait,
1262 ref ty,
1263 ref impl_items) => {
1264 try!(self.head(""));
1265 try!(self.print_visibility(item.vis));
1266 try!(self.print_unsafety(unsafety));
1267 try!(self.word_nbsp("impl"));
1268
1269 if generics.is_parameterized() {
1270 try!(self.print_generics(generics));
1271 try!(space(&mut self.s));
1272 }
1273
1274 match polarity {
1275 ast::ImplPolarity::Negative => {
1276 try!(word(&mut self.s, "!"));
1277 },
1278 _ => {}
1279 }
1280
1281 match *opt_trait {
1282 Some(ref t) => {
1283 try!(self.print_trait_ref(t));
1284 try!(space(&mut self.s));
1285 try!(self.word_space("for"));
1286 }
1287 None => {}
1288 }
1289
1290 try!(self.print_type(&**ty));
1291 try!(self.print_where_clause(&generics.where_clause));
1292
1293 try!(space(&mut self.s));
1294 try!(self.bopen());
1295 try!(self.print_inner_attributes(&item.attrs));
1296 for impl_item in impl_items {
1297 try!(self.print_impl_item(impl_item));
1298 }
1299 try!(self.bclose(item.span));
1300 }
1301 ast::ItemTrait(unsafety, ref generics, ref bounds, ref trait_items) => {
1302 try!(self.head(""));
1303 try!(self.print_visibility(item.vis));
1304 try!(self.print_unsafety(unsafety));
1305 try!(self.word_nbsp("trait"));
1306 try!(self.print_ident(item.ident));
1307 try!(self.print_generics(generics));
1308 let mut real_bounds = Vec::with_capacity(bounds.len());
1309 for b in bounds.iter() {
1310 if let TraitTyParamBound(ref ptr, ast::TraitBoundModifier::Maybe) = *b {
1311 try!(space(&mut self.s));
1312 try!(self.word_space("for ?"));
1313 try!(self.print_trait_ref(&ptr.trait_ref));
1314 } else {
1315 real_bounds.push(b.clone());
1316 }
1317 }
1318 try!(self.print_bounds(":", &real_bounds[..]));
1319 try!(self.print_where_clause(&generics.where_clause));
1320 try!(word(&mut self.s, " "));
1321 try!(self.bopen());
1322 for trait_item in trait_items {
1323 try!(self.print_trait_item(trait_item));
1324 }
1325 try!(self.bclose(item.span));
1326 }
1327 ast::ItemMac(codemap::Spanned { ref node, .. }) => {
1328 try!(self.print_visibility(item.vis));
1329 try!(self.print_path(&node.path, false, 0));
1330 try!(word(&mut self.s, "! "));
1331 try!(self.print_ident(item.ident));
1332 try!(self.cbox(INDENT_UNIT));
1333 try!(self.popen());
1334 try!(self.print_tts(&node.tts[..]));
1335 try!(self.pclose());
1336 try!(word(&mut self.s, ";"));
1337 try!(self.end());
1338 }
1339 }
1340 self.ann.post(self, NodeItem(item))
1341 }
1342
1343 fn print_trait_ref(&mut self, t: &ast::TraitRef) -> io::Result<()> {
1344 self.print_path(&t.path, false, 0)
1345 }
1346
1347 fn print_formal_lifetime_list(&mut self, lifetimes: &[ast::LifetimeDef]) -> io::Result<()> {
1348 if !lifetimes.is_empty() {
1349 try!(word(&mut self.s, "for<"));
1350 let mut comma = false;
1351 for lifetime_def in lifetimes {
1352 if comma {
1353 try!(self.word_space(","))
1354 }
1355 try!(self.print_lifetime_def(lifetime_def));
1356 comma = true;
1357 }
1358 try!(word(&mut self.s, ">"));
1359 }
1360 Ok(())
1361 }
1362
1363 fn print_poly_trait_ref(&mut self, t: &ast::PolyTraitRef) -> io::Result<()> {
1364 try!(self.print_formal_lifetime_list(&t.bound_lifetimes));
1365 self.print_trait_ref(&t.trait_ref)
1366 }
1367
1368 pub fn print_enum_def(&mut self, enum_definition: &ast::EnumDef,
1369 generics: &ast::Generics, ident: ast::Ident,
1370 span: codemap::Span,
1371 visibility: ast::Visibility) -> io::Result<()> {
1372 try!(self.head(&visibility_qualified(visibility, "enum")));
1373 try!(self.print_ident(ident));
1374 try!(self.print_generics(generics));
1375 try!(self.print_where_clause(&generics.where_clause));
1376 try!(space(&mut self.s));
1377 self.print_variants(&enum_definition.variants, span)
1378 }
1379
1380 pub fn print_variants(&mut self,
1381 variants: &[P<ast::Variant>],
1382 span: codemap::Span) -> io::Result<()> {
1383 try!(self.bopen());
1384 for v in variants {
1385 try!(self.space_if_not_bol());
1386 try!(self.maybe_print_comment(v.span.lo));
1387 try!(self.print_outer_attributes(&v.node.attrs));
1388 try!(self.ibox(INDENT_UNIT));
1389 try!(self.print_variant(&**v));
1390 try!(word(&mut self.s, ","));
1391 try!(self.end());
1392 try!(self.maybe_print_trailing_comment(v.span, None));
1393 }
1394 self.bclose(span)
1395 }
1396
1397 pub fn print_visibility(&mut self, vis: ast::Visibility) -> io::Result<()> {
1398 match vis {
1399 ast::Public => self.word_nbsp("pub"),
1400 ast::Inherited => Ok(())
1401 }
1402 }
1403
1404 pub fn print_struct(&mut self,
1405 struct_def: &ast::VariantData,
1406 generics: &ast::Generics,
1407 ident: ast::Ident,
1408 span: codemap::Span,
1409 print_finalizer: bool) -> io::Result<()> {
1410 try!(self.print_ident(ident));
1411 try!(self.print_generics(generics));
1412 if !struct_def.is_struct() {
1413 if struct_def.is_tuple() {
1414 try!(self.popen());
1415 try!(self.commasep(
1416 Inconsistent, struct_def.fields(),
1417 |s, field| {
1418 match field.node.kind {
1419 ast::NamedField(..) => panic!("unexpected named field"),
1420 ast::UnnamedField(vis) => {
1421 try!(s.print_visibility(vis));
1422 try!(s.maybe_print_comment(field.span.lo));
1423 s.print_type(&*field.node.ty)
1424 }
1425 }
1426 }
1427 ));
1428 try!(self.pclose());
1429 }
1430 try!(self.print_where_clause(&generics.where_clause));
1431 if print_finalizer {
1432 try!(word(&mut self.s, ";"));
1433 }
1434 try!(self.end());
1435 self.end() // close the outer-box
1436 } else {
1437 try!(self.print_where_clause(&generics.where_clause));
1438 try!(self.nbsp());
1439 try!(self.bopen());
1440 try!(self.hardbreak_if_not_bol());
1441
1442 for field in struct_def.fields() {
1443 match field.node.kind {
1444 ast::UnnamedField(..) => panic!("unexpected unnamed field"),
1445 ast::NamedField(ident, visibility) => {
1446 try!(self.hardbreak_if_not_bol());
1447 try!(self.maybe_print_comment(field.span.lo));
1448 try!(self.print_outer_attributes(&field.node.attrs));
1449 try!(self.print_visibility(visibility));
1450 try!(self.print_ident(ident));
1451 try!(self.word_nbsp(":"));
1452 try!(self.print_type(&*field.node.ty));
1453 try!(word(&mut self.s, ","));
1454 }
1455 }
1456 }
1457
1458 self.bclose(span)
1459 }
1460 }
1461
1462 /// This doesn't deserve to be called "pretty" printing, but it should be
1463 /// meaning-preserving. A quick hack that might help would be to look at the
1464 /// spans embedded in the TTs to decide where to put spaces and newlines.
1465 /// But it'd be better to parse these according to the grammar of the
1466 /// appropriate macro, transcribe back into the grammar we just parsed from,
1467 /// and then pretty-print the resulting AST nodes (so, e.g., we print
1468 /// expression arguments as expressions). It can be done! I think.
1469 pub fn print_tt(&mut self, tt: &ast::TokenTree) -> io::Result<()> {
1470 match *tt {
1471 TokenTree::Token(_, ref tk) => {
1472 try!(word(&mut self.s, &token_to_string(tk)));
1473 match *tk {
1474 parse::token::DocComment(..) => {
1475 hardbreak(&mut self.s)
1476 }
1477 _ => Ok(())
1478 }
1479 }
1480 TokenTree::Delimited(_, ref delimed) => {
1481 try!(word(&mut self.s, &token_to_string(&delimed.open_token())));
1482 try!(space(&mut self.s));
1483 try!(self.print_tts(&delimed.tts));
1484 try!(space(&mut self.s));
1485 word(&mut self.s, &token_to_string(&delimed.close_token()))
1486 },
1487 TokenTree::Sequence(_, ref seq) => {
1488 try!(word(&mut self.s, "$("));
1489 for tt_elt in &seq.tts {
1490 try!(self.print_tt(tt_elt));
1491 }
1492 try!(word(&mut self.s, ")"));
1493 match seq.separator {
1494 Some(ref tk) => {
1495 try!(word(&mut self.s, &token_to_string(tk)));
1496 }
1497 None => {},
1498 }
1499 match seq.op {
1500 ast::ZeroOrMore => word(&mut self.s, "*"),
1501 ast::OneOrMore => word(&mut self.s, "+"),
1502 }
1503 }
1504 }
1505 }
1506
1507 pub fn print_tts(&mut self, tts: &[ast::TokenTree]) -> io::Result<()> {
1508 try!(self.ibox(0));
1509 let mut suppress_space = false;
1510 for (i, tt) in tts.iter().enumerate() {
1511 if i != 0 && !suppress_space {
1512 try!(space(&mut self.s));
1513 }
1514 try!(self.print_tt(tt));
1515 // There should be no space between the module name and the following `::` in paths,
1516 // otherwise imported macros get re-parsed from crate metadata incorrectly (#20701)
1517 suppress_space = match *tt {
1518 TokenTree::Token(_, token::Ident(_, token::ModName)) |
1519 TokenTree::Token(_, token::MatchNt(_, _, _, token::ModName)) |
1520 TokenTree::Token(_, token::SubstNt(_, token::ModName)) => true,
1521 _ => false
1522 }
1523 }
1524 self.end()
1525 }
1526
1527 pub fn print_variant(&mut self, v: &ast::Variant) -> io::Result<()> {
1528 try!(self.head(""));
1529 let generics = ast::Generics::default();
1530 try!(self.print_struct(&v.node.data, &generics, v.node.name, v.span, false));
1531 match v.node.disr_expr {
1532 Some(ref d) => {
1533 try!(space(&mut self.s));
1534 try!(self.word_space("="));
1535 self.print_expr(&**d)
1536 }
1537 _ => Ok(())
1538 }
1539 }
1540
1541 pub fn print_method_sig(&mut self,
1542 ident: ast::Ident,
1543 m: &ast::MethodSig,
1544 vis: ast::Visibility)
1545 -> io::Result<()> {
1546 self.print_fn(&m.decl,
1547 m.unsafety,
1548 m.constness,
1549 m.abi,
1550 Some(ident),
1551 &m.generics,
1552 Some(&m.explicit_self.node),
1553 vis)
1554 }
1555
1556 pub fn print_trait_item(&mut self, ti: &ast::TraitItem)
1557 -> io::Result<()> {
1558 try!(self.ann.pre(self, NodeSubItem(ti.id)));
1559 try!(self.hardbreak_if_not_bol());
1560 try!(self.maybe_print_comment(ti.span.lo));
1561 try!(self.print_outer_attributes(&ti.attrs));
1562 match ti.node {
1563 ast::ConstTraitItem(ref ty, ref default) => {
1564 try!(self.print_associated_const(ti.ident, &ty,
1565 default.as_ref().map(|expr| &**expr),
1566 ast::Inherited));
1567 }
1568 ast::MethodTraitItem(ref sig, ref body) => {
1569 if body.is_some() {
1570 try!(self.head(""));
1571 }
1572 try!(self.print_method_sig(ti.ident, sig, ast::Inherited));
1573 if let Some(ref body) = *body {
1574 try!(self.nbsp());
1575 try!(self.print_block_with_attrs(body, &ti.attrs));
1576 } else {
1577 try!(word(&mut self.s, ";"));
1578 }
1579 }
1580 ast::TypeTraitItem(ref bounds, ref default) => {
1581 try!(self.print_associated_type(ti.ident, Some(bounds),
1582 default.as_ref().map(|ty| &**ty)));
1583 }
1584 }
1585 self.ann.post(self, NodeSubItem(ti.id))
1586 }
1587
1588 pub fn print_impl_item(&mut self, ii: &ast::ImplItem) -> io::Result<()> {
1589 try!(self.ann.pre(self, NodeSubItem(ii.id)));
1590 try!(self.hardbreak_if_not_bol());
1591 try!(self.maybe_print_comment(ii.span.lo));
1592 try!(self.print_outer_attributes(&ii.attrs));
1593 match ii.node {
1594 ast::ImplItemKind::Const(ref ty, ref expr) => {
1595 try!(self.print_associated_const(ii.ident, &ty, Some(&expr), ii.vis));
1596 }
1597 ast::ImplItemKind::Method(ref sig, ref body) => {
1598 try!(self.head(""));
1599 try!(self.print_method_sig(ii.ident, sig, ii.vis));
1600 try!(self.nbsp());
1601 try!(self.print_block_with_attrs(body, &ii.attrs));
1602 }
1603 ast::ImplItemKind::Type(ref ty) => {
1604 try!(self.print_associated_type(ii.ident, None, Some(ty)));
1605 }
1606 ast::ImplItemKind::Macro(codemap::Spanned { ref node, .. }) => {
1607 // code copied from ItemMac:
1608 try!(self.print_path(&node.path, false, 0));
1609 try!(word(&mut self.s, "! "));
1610 try!(self.cbox(INDENT_UNIT));
1611 try!(self.popen());
1612 try!(self.print_tts(&node.tts[..]));
1613 try!(self.pclose());
1614 try!(word(&mut self.s, ";"));
1615 try!(self.end())
1616 }
1617 }
1618 self.ann.post(self, NodeSubItem(ii.id))
1619 }
1620
1621 pub fn print_stmt(&mut self, st: &ast::Stmt) -> io::Result<()> {
1622 try!(self.maybe_print_comment(st.span.lo));
1623 match st.node {
1624 ast::StmtDecl(ref decl, _) => {
1625 try!(self.print_decl(&**decl));
1626 }
1627 ast::StmtExpr(ref expr, _) => {
1628 try!(self.space_if_not_bol());
1629 try!(self.print_expr_outer_attr_style(&**expr, false));
1630 }
1631 ast::StmtSemi(ref expr, _) => {
1632 try!(self.space_if_not_bol());
1633 try!(self.print_expr_outer_attr_style(&**expr, false));
1634 try!(word(&mut self.s, ";"));
1635 }
1636 ast::StmtMac(ref mac, style, ref attrs) => {
1637 try!(self.space_if_not_bol());
1638 try!(self.print_outer_attributes(attrs.as_attr_slice()));
1639 let delim = match style {
1640 ast::MacStmtWithBraces => token::Brace,
1641 _ => token::Paren
1642 };
1643 try!(self.print_mac(&**mac, delim));
1644 match style {
1645 ast::MacStmtWithBraces => {}
1646 _ => try!(word(&mut self.s, ";")),
1647 }
1648 }
1649 }
1650 if parse::classify::stmt_ends_with_semi(&st.node) {
1651 try!(word(&mut self.s, ";"));
1652 }
1653 self.maybe_print_trailing_comment(st.span, None)
1654 }
1655
1656 pub fn print_block(&mut self, blk: &ast::Block) -> io::Result<()> {
1657 self.print_block_with_attrs(blk, &[])
1658 }
1659
1660 pub fn print_block_unclosed(&mut self, blk: &ast::Block) -> io::Result<()> {
1661 self.print_block_unclosed_indent(blk, INDENT_UNIT)
1662 }
1663
1664 pub fn print_block_unclosed_with_attrs(&mut self, blk: &ast::Block,
1665 attrs: &[ast::Attribute])
1666 -> io::Result<()> {
1667 self.print_block_maybe_unclosed(blk, INDENT_UNIT, attrs, false)
1668 }
1669
1670 pub fn print_block_unclosed_indent(&mut self, blk: &ast::Block,
1671 indented: usize) -> io::Result<()> {
1672 self.print_block_maybe_unclosed(blk, indented, &[], false)
1673 }
1674
1675 pub fn print_block_with_attrs(&mut self,
1676 blk: &ast::Block,
1677 attrs: &[ast::Attribute]) -> io::Result<()> {
1678 self.print_block_maybe_unclosed(blk, INDENT_UNIT, attrs, true)
1679 }
1680
1681 pub fn print_block_maybe_unclosed(&mut self,
1682 blk: &ast::Block,
1683 indented: usize,
1684 attrs: &[ast::Attribute],
1685 close_box: bool) -> io::Result<()> {
1686 match blk.rules {
1687 ast::UnsafeBlock(..) => try!(self.word_space("unsafe")),
1688 ast::DefaultBlock => ()
1689 }
1690 try!(self.maybe_print_comment(blk.span.lo));
1691 try!(self.ann.pre(self, NodeBlock(blk)));
1692 try!(self.bopen());
1693
1694 try!(self.print_inner_attributes(attrs));
1695
1696 for st in &blk.stmts {
1697 try!(self.print_stmt(&**st));
1698 }
1699 match blk.expr {
1700 Some(ref expr) => {
1701 try!(self.space_if_not_bol());
1702 try!(self.print_expr_outer_attr_style(&**expr, false));
1703 try!(self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi)));
1704 }
1705 _ => ()
1706 }
1707 try!(self.bclose_maybe_open(blk.span, indented, close_box));
1708 self.ann.post(self, NodeBlock(blk))
1709 }
1710
1711 fn print_else(&mut self, els: Option<&ast::Expr>) -> io::Result<()> {
1712 match els {
1713 Some(_else) => {
1714 match _else.node {
1715 // "another else-if"
1716 ast::ExprIf(ref i, ref then, ref e) => {
1717 try!(self.cbox(INDENT_UNIT - 1));
1718 try!(self.ibox(0));
1719 try!(word(&mut self.s, " else if "));
1720 try!(self.print_expr(&**i));
1721 try!(space(&mut self.s));
1722 try!(self.print_block(&**then));
1723 self.print_else(e.as_ref().map(|e| &**e))
1724 }
1725 // "another else-if-let"
1726 ast::ExprIfLet(ref pat, ref expr, ref then, ref e) => {
1727 try!(self.cbox(INDENT_UNIT - 1));
1728 try!(self.ibox(0));
1729 try!(word(&mut self.s, " else if let "));
1730 try!(self.print_pat(&**pat));
1731 try!(space(&mut self.s));
1732 try!(self.word_space("="));
1733 try!(self.print_expr(&**expr));
1734 try!(space(&mut self.s));
1735 try!(self.print_block(&**then));
1736 self.print_else(e.as_ref().map(|e| &**e))
1737 }
1738 // "final else"
1739 ast::ExprBlock(ref b) => {
1740 try!(self.cbox(INDENT_UNIT - 1));
1741 try!(self.ibox(0));
1742 try!(word(&mut self.s, " else "));
1743 self.print_block(&**b)
1744 }
1745 // BLEAH, constraints would be great here
1746 _ => {
1747 panic!("print_if saw if with weird alternative");
1748 }
1749 }
1750 }
1751 _ => Ok(())
1752 }
1753 }
1754
1755 pub fn print_if(&mut self, test: &ast::Expr, blk: &ast::Block,
1756 elseopt: Option<&ast::Expr>) -> io::Result<()> {
1757 try!(self.head("if"));
1758 try!(self.print_expr(test));
1759 try!(space(&mut self.s));
1760 try!(self.print_block(blk));
1761 self.print_else(elseopt)
1762 }
1763
1764 pub fn print_if_let(&mut self, pat: &ast::Pat, expr: &ast::Expr, blk: &ast::Block,
1765 elseopt: Option<&ast::Expr>) -> io::Result<()> {
1766 try!(self.head("if let"));
1767 try!(self.print_pat(pat));
1768 try!(space(&mut self.s));
1769 try!(self.word_space("="));
1770 try!(self.print_expr(expr));
1771 try!(space(&mut self.s));
1772 try!(self.print_block(blk));
1773 self.print_else(elseopt)
1774 }
1775
1776 pub fn print_mac(&mut self, m: &ast::Mac, delim: token::DelimToken)
1777 -> io::Result<()> {
1778 try!(self.print_path(&m.node.path, false, 0));
1779 try!(word(&mut self.s, "!"));
1780 match delim {
1781 token::Paren => try!(self.popen()),
1782 token::Bracket => try!(word(&mut self.s, "[")),
1783 token::Brace => {
1784 // head-ibox, will be closed by bopen()
1785 try!(self.ibox(0));
1786 // Don't ask me why the regular bopen() does
1787 // more then just opening a brace...
1788 try!(self.bopen())
1789 }
1790 }
1791 try!(self.print_tts(&m.node.tts));
1792 match delim {
1793 token::Paren => self.pclose(),
1794 token::Bracket => word(&mut self.s, "]"),
1795 token::Brace => self.bclose(m.span),
1796 }
1797 }
1798
1799
1800 fn print_call_post(&mut self, args: &[P<ast::Expr>]) -> io::Result<()> {
1801 try!(self.popen());
1802 try!(self.commasep_exprs(Inconsistent, args));
1803 self.pclose()
1804 }
1805
1806 pub fn check_expr_bin_needs_paren(&mut self, sub_expr: &ast::Expr,
1807 binop: ast::BinOp) -> bool {
1808 match sub_expr.node {
1809 ast::ExprBinary(ref sub_op, _, _) => {
1810 if AssocOp::from_ast_binop(sub_op.node).precedence() <
1811 AssocOp::from_ast_binop(binop.node).precedence() {
1812 true
1813 } else {
1814 false
1815 }
1816 }
1817 _ => true
1818 }
1819 }
1820
1821 pub fn print_expr_maybe_paren(&mut self, expr: &ast::Expr) -> io::Result<()> {
1822 let needs_par = needs_parentheses(expr);
1823 if needs_par {
1824 try!(self.popen());
1825 }
1826 try!(self.print_expr(expr));
1827 if needs_par {
1828 try!(self.pclose());
1829 }
1830 Ok(())
1831 }
1832
1833 fn print_expr_in_place(&mut self,
1834 place: &ast::Expr,
1835 expr: &ast::Expr) -> io::Result<()> {
1836 try!(self.print_expr_maybe_paren(place));
1837 try!(space(&mut self.s));
1838 try!(self.word_space("<-"));
1839 self.print_expr_maybe_paren(expr)
1840 }
1841
1842 fn print_expr_vec(&mut self, exprs: &[P<ast::Expr>],
1843 attrs: &[Attribute]) -> io::Result<()> {
1844 try!(self.ibox(INDENT_UNIT));
1845 try!(word(&mut self.s, "["));
1846 try!(self.print_inner_attributes_inline(attrs));
1847 try!(self.commasep_exprs(Inconsistent, &exprs[..]));
1848 try!(word(&mut self.s, "]"));
1849 self.end()
1850 }
1851
1852 fn print_expr_repeat(&mut self,
1853 element: &ast::Expr,
1854 count: &ast::Expr,
1855 attrs: &[Attribute]) -> io::Result<()> {
1856 try!(self.ibox(INDENT_UNIT));
1857 try!(word(&mut self.s, "["));
1858 try!(self.print_inner_attributes_inline(attrs));
1859 try!(self.print_expr(element));
1860 try!(self.word_space(";"));
1861 try!(self.print_expr(count));
1862 try!(word(&mut self.s, "]"));
1863 self.end()
1864 }
1865
1866 fn print_expr_struct(&mut self,
1867 path: &ast::Path,
1868 fields: &[ast::Field],
1869 wth: &Option<P<ast::Expr>>,
1870 attrs: &[Attribute]) -> io::Result<()> {
1871 try!(self.print_path(path, true, 0));
1872 try!(word(&mut self.s, "{"));
1873 try!(self.print_inner_attributes_inline(attrs));
1874 try!(self.commasep_cmnt(
1875 Consistent,
1876 &fields[..],
1877 |s, field| {
1878 try!(s.ibox(INDENT_UNIT));
1879 try!(s.print_ident(field.ident.node));
1880 try!(s.word_space(":"));
1881 try!(s.print_expr(&*field.expr));
1882 s.end()
1883 },
1884 |f| f.span));
1885 match *wth {
1886 Some(ref expr) => {
1887 try!(self.ibox(INDENT_UNIT));
1888 if !fields.is_empty() {
1889 try!(word(&mut self.s, ","));
1890 try!(space(&mut self.s));
1891 }
1892 try!(word(&mut self.s, ".."));
1893 try!(self.print_expr(&**expr));
1894 try!(self.end());
1895 }
1896 _ => if !fields.is_empty() {
1897 try!(word(&mut self.s, ","))
1898 }
1899 }
1900 try!(word(&mut self.s, "}"));
1901 Ok(())
1902 }
1903
1904 fn print_expr_tup(&mut self, exprs: &[P<ast::Expr>],
1905 attrs: &[Attribute]) -> io::Result<()> {
1906 try!(self.popen());
1907 try!(self.print_inner_attributes_inline(attrs));
1908 try!(self.commasep_exprs(Inconsistent, &exprs[..]));
1909 if exprs.len() == 1 {
1910 try!(word(&mut self.s, ","));
1911 }
1912 self.pclose()
1913 }
1914
1915 fn print_expr_call(&mut self,
1916 func: &ast::Expr,
1917 args: &[P<ast::Expr>]) -> io::Result<()> {
1918 try!(self.print_expr_maybe_paren(func));
1919 self.print_call_post(args)
1920 }
1921
1922 fn print_expr_method_call(&mut self,
1923 ident: ast::SpannedIdent,
1924 tys: &[P<ast::Ty>],
1925 args: &[P<ast::Expr>]) -> io::Result<()> {
1926 let base_args = &args[1..];
1927 try!(self.print_expr(&*args[0]));
1928 try!(word(&mut self.s, "."));
1929 try!(self.print_ident(ident.node));
1930 if !tys.is_empty() {
1931 try!(word(&mut self.s, "::<"));
1932 try!(self.commasep(Inconsistent, tys,
1933 |s, ty| s.print_type(&**ty)));
1934 try!(word(&mut self.s, ">"));
1935 }
1936 self.print_call_post(base_args)
1937 }
1938
1939 fn print_expr_binary(&mut self,
1940 op: ast::BinOp,
1941 lhs: &ast::Expr,
1942 rhs: &ast::Expr) -> io::Result<()> {
1943 if self.check_expr_bin_needs_paren(lhs, op) {
1944 try!(self.print_expr_maybe_paren(lhs));
1945 } else {
1946 try!(self.print_expr(lhs));
1947 }
1948 try!(space(&mut self.s));
1949 try!(self.word_space(op.node.to_string()));
1950 if self.check_expr_bin_needs_paren(rhs, op) {
1951 self.print_expr_maybe_paren(rhs)
1952 } else {
1953 self.print_expr(rhs)
1954 }
1955 }
1956
1957 fn print_expr_unary(&mut self,
1958 op: ast::UnOp,
1959 expr: &ast::Expr) -> io::Result<()> {
1960 try!(word(&mut self.s, ast::UnOp::to_string(op)));
1961 self.print_expr_maybe_paren(expr)
1962 }
1963
1964 fn print_expr_addr_of(&mut self,
1965 mutability: ast::Mutability,
1966 expr: &ast::Expr) -> io::Result<()> {
1967 try!(word(&mut self.s, "&"));
1968 try!(self.print_mutability(mutability));
1969 self.print_expr_maybe_paren(expr)
1970 }
1971
1972 pub fn print_expr(&mut self, expr: &ast::Expr) -> io::Result<()> {
1973 self.print_expr_outer_attr_style(expr, true)
1974 }
1975
1976 fn print_expr_outer_attr_style(&mut self,
1977 expr: &ast::Expr,
1978 is_inline: bool) -> io::Result<()> {
1979 try!(self.maybe_print_comment(expr.span.lo));
1980
1981 let attrs = expr.attrs.as_attr_slice();
1982 if is_inline {
1983 try!(self.print_outer_attributes_inline(attrs));
1984 } else {
1985 try!(self.print_outer_attributes(attrs));
1986 }
1987
1988 try!(self.ibox(INDENT_UNIT));
1989 try!(self.ann.pre(self, NodeExpr(expr)));
1990 match expr.node {
1991 ast::ExprBox(ref expr) => {
1992 try!(self.word_space("box"));
1993 try!(self.print_expr(expr));
1994 }
1995 ast::ExprInPlace(ref place, ref expr) => {
1996 try!(self.print_expr_in_place(place, expr));
1997 }
1998 ast::ExprVec(ref exprs) => {
1999 try!(self.print_expr_vec(&exprs[..], attrs));
2000 }
2001 ast::ExprRepeat(ref element, ref count) => {
2002 try!(self.print_expr_repeat(&**element, &**count, attrs));
2003 }
2004 ast::ExprStruct(ref path, ref fields, ref wth) => {
2005 try!(self.print_expr_struct(path, &fields[..], wth, attrs));
2006 }
2007 ast::ExprTup(ref exprs) => {
2008 try!(self.print_expr_tup(&exprs[..], attrs));
2009 }
2010 ast::ExprCall(ref func, ref args) => {
2011 try!(self.print_expr_call(&**func, &args[..]));
2012 }
2013 ast::ExprMethodCall(ident, ref tys, ref args) => {
2014 try!(self.print_expr_method_call(ident, &tys[..], &args[..]));
2015 }
2016 ast::ExprBinary(op, ref lhs, ref rhs) => {
2017 try!(self.print_expr_binary(op, &**lhs, &**rhs));
2018 }
2019 ast::ExprUnary(op, ref expr) => {
2020 try!(self.print_expr_unary(op, &**expr));
2021 }
2022 ast::ExprAddrOf(m, ref expr) => {
2023 try!(self.print_expr_addr_of(m, &**expr));
2024 }
2025 ast::ExprLit(ref lit) => {
2026 try!(self.print_literal(&**lit));
2027 }
2028 ast::ExprCast(ref expr, ref ty) => {
2029 if let ast::ExprCast(..) = expr.node {
2030 try!(self.print_expr(&**expr));
2031 } else {
2032 try!(self.print_expr_maybe_paren(&**expr));
2033 }
2034 try!(space(&mut self.s));
2035 try!(self.word_space("as"));
2036 try!(self.print_type(&**ty));
2037 }
2038 ast::ExprType(ref expr, ref ty) => {
2039 try!(self.print_expr(&**expr));
2040 try!(self.word_space(":"));
2041 try!(self.print_type(&**ty));
2042 }
2043 ast::ExprIf(ref test, ref blk, ref elseopt) => {
2044 try!(self.print_if(&**test, &**blk, elseopt.as_ref().map(|e| &**e)));
2045 }
2046 ast::ExprIfLet(ref pat, ref expr, ref blk, ref elseopt) => {
2047 try!(self.print_if_let(&**pat, &**expr, &** blk, elseopt.as_ref().map(|e| &**e)));
2048 }
2049 ast::ExprWhile(ref test, ref blk, opt_ident) => {
2050 if let Some(ident) = opt_ident {
2051 try!(self.print_ident(ident));
2052 try!(self.word_space(":"));
2053 }
2054 try!(self.head("while"));
2055 try!(self.print_expr(&**test));
2056 try!(space(&mut self.s));
2057 try!(self.print_block_with_attrs(&**blk, attrs));
2058 }
2059 ast::ExprWhileLet(ref pat, ref expr, ref blk, opt_ident) => {
2060 if let Some(ident) = opt_ident {
2061 try!(self.print_ident(ident));
2062 try!(self.word_space(":"));
2063 }
2064 try!(self.head("while let"));
2065 try!(self.print_pat(&**pat));
2066 try!(space(&mut self.s));
2067 try!(self.word_space("="));
2068 try!(self.print_expr(&**expr));
2069 try!(space(&mut self.s));
2070 try!(self.print_block_with_attrs(&**blk, attrs));
2071 }
2072 ast::ExprForLoop(ref pat, ref iter, ref blk, opt_ident) => {
2073 if let Some(ident) = opt_ident {
2074 try!(self.print_ident(ident));
2075 try!(self.word_space(":"));
2076 }
2077 try!(self.head("for"));
2078 try!(self.print_pat(&**pat));
2079 try!(space(&mut self.s));
2080 try!(self.word_space("in"));
2081 try!(self.print_expr(&**iter));
2082 try!(space(&mut self.s));
2083 try!(self.print_block_with_attrs(&**blk, attrs));
2084 }
2085 ast::ExprLoop(ref blk, opt_ident) => {
2086 if let Some(ident) = opt_ident {
2087 try!(self.print_ident(ident));
2088 try!(self.word_space(":"));
2089 }
2090 try!(self.head("loop"));
2091 try!(space(&mut self.s));
2092 try!(self.print_block_with_attrs(&**blk, attrs));
2093 }
2094 ast::ExprMatch(ref expr, ref arms) => {
2095 try!(self.cbox(INDENT_UNIT));
2096 try!(self.ibox(4));
2097 try!(self.word_nbsp("match"));
2098 try!(self.print_expr(&**expr));
2099 try!(space(&mut self.s));
2100 try!(self.bopen());
2101 try!(self.print_inner_attributes_no_trailing_hardbreak(attrs));
2102 for arm in arms {
2103 try!(self.print_arm(arm));
2104 }
2105 try!(self.bclose_(expr.span, INDENT_UNIT));
2106 }
2107 ast::ExprClosure(capture_clause, ref decl, ref body) => {
2108 try!(self.print_capture_clause(capture_clause));
2109
2110 try!(self.print_fn_block_args(&**decl));
2111 try!(space(&mut self.s));
2112
2113 let default_return = match decl.output {
2114 ast::DefaultReturn(..) => true,
2115 _ => false
2116 };
2117
2118 if !default_return || !body.stmts.is_empty() || body.expr.is_none() {
2119 try!(self.print_block_unclosed(&**body));
2120 } else {
2121 // we extract the block, so as not to create another set of boxes
2122 let i_expr = body.expr.as_ref().unwrap();
2123 match i_expr.node {
2124 ast::ExprBlock(ref blk) => {
2125 try!(self.print_block_unclosed_with_attrs(
2126 &**blk,
2127 i_expr.attrs.as_attr_slice()));
2128 }
2129 _ => {
2130 // this is a bare expression
2131 try!(self.print_expr(&**i_expr));
2132 try!(self.end()); // need to close a box
2133 }
2134 }
2135 }
2136 // a box will be closed by print_expr, but we didn't want an overall
2137 // wrapper so we closed the corresponding opening. so create an
2138 // empty box to satisfy the close.
2139 try!(self.ibox(0));
2140 }
2141 ast::ExprBlock(ref blk) => {
2142 // containing cbox, will be closed by print-block at }
2143 try!(self.cbox(INDENT_UNIT));
2144 // head-box, will be closed by print-block after {
2145 try!(self.ibox(0));
2146 try!(self.print_block_with_attrs(&**blk, attrs));
2147 }
2148 ast::ExprAssign(ref lhs, ref rhs) => {
2149 try!(self.print_expr(&**lhs));
2150 try!(space(&mut self.s));
2151 try!(self.word_space("="));
2152 try!(self.print_expr(&**rhs));
2153 }
2154 ast::ExprAssignOp(op, ref lhs, ref rhs) => {
2155 try!(self.print_expr(&**lhs));
2156 try!(space(&mut self.s));
2157 try!(word(&mut self.s, op.node.to_string()));
2158 try!(self.word_space("="));
2159 try!(self.print_expr(&**rhs));
2160 }
2161 ast::ExprField(ref expr, id) => {
2162 try!(self.print_expr(&**expr));
2163 try!(word(&mut self.s, "."));
2164 try!(self.print_ident(id.node));
2165 }
2166 ast::ExprTupField(ref expr, id) => {
2167 try!(self.print_expr(&**expr));
2168 try!(word(&mut self.s, "."));
2169 try!(self.print_usize(id.node));
2170 }
2171 ast::ExprIndex(ref expr, ref index) => {
2172 try!(self.print_expr(&**expr));
2173 try!(word(&mut self.s, "["));
2174 try!(self.print_expr(&**index));
2175 try!(word(&mut self.s, "]"));
2176 }
2177 ast::ExprRange(ref start, ref end) => {
2178 if let &Some(ref e) = start {
2179 try!(self.print_expr(&**e));
2180 }
2181 try!(word(&mut self.s, ".."));
2182 if let &Some(ref e) = end {
2183 try!(self.print_expr(&**e));
2184 }
2185 }
2186 ast::ExprPath(None, ref path) => {
2187 try!(self.print_path(path, true, 0))
2188 }
2189 ast::ExprPath(Some(ref qself), ref path) => {
2190 try!(self.print_qpath(path, qself, true))
2191 }
2192 ast::ExprBreak(opt_ident) => {
2193 try!(word(&mut self.s, "break"));
2194 try!(space(&mut self.s));
2195 if let Some(ident) = opt_ident {
2196 try!(self.print_ident(ident.node));
2197 try!(space(&mut self.s));
2198 }
2199 }
2200 ast::ExprAgain(opt_ident) => {
2201 try!(word(&mut self.s, "continue"));
2202 try!(space(&mut self.s));
2203 if let Some(ident) = opt_ident {
2204 try!(self.print_ident(ident.node));
2205 try!(space(&mut self.s))
2206 }
2207 }
2208 ast::ExprRet(ref result) => {
2209 try!(word(&mut self.s, "return"));
2210 match *result {
2211 Some(ref expr) => {
2212 try!(word(&mut self.s, " "));
2213 try!(self.print_expr(&**expr));
2214 }
2215 _ => ()
2216 }
2217 }
2218 ast::ExprInlineAsm(ref a) => {
2219 try!(word(&mut self.s, "asm!"));
2220 try!(self.popen());
2221 try!(self.print_string(&a.asm, a.asm_str_style));
2222 try!(self.word_space(":"));
2223
2224 try!(self.commasep(Inconsistent, &a.outputs,
2225 |s, out| {
2226 match out.constraint.slice_shift_char() {
2227 Some(('=', operand)) if out.is_rw => {
2228 try!(s.print_string(&format!("+{}", operand),
2229 ast::CookedStr))
2230 }
2231 _ => try!(s.print_string(&out.constraint, ast::CookedStr))
2232 }
2233 try!(s.popen());
2234 try!(s.print_expr(&*out.expr));
2235 try!(s.pclose());
2236 Ok(())
2237 }));
2238 try!(space(&mut self.s));
2239 try!(self.word_space(":"));
2240
2241 try!(self.commasep(Inconsistent, &a.inputs,
2242 |s, &(ref co, ref o)| {
2243 try!(s.print_string(&co, ast::CookedStr));
2244 try!(s.popen());
2245 try!(s.print_expr(&**o));
2246 try!(s.pclose());
2247 Ok(())
2248 }));
2249 try!(space(&mut self.s));
2250 try!(self.word_space(":"));
2251
2252 try!(self.commasep(Inconsistent, &a.clobbers,
2253 |s, co| {
2254 try!(s.print_string(&co, ast::CookedStr));
2255 Ok(())
2256 }));
2257
2258 let mut options = vec!();
2259 if a.volatile {
2260 options.push("volatile");
2261 }
2262 if a.alignstack {
2263 options.push("alignstack");
2264 }
2265 if a.dialect == ast::AsmDialect::Intel {
2266 options.push("intel");
2267 }
2268
2269 if !options.is_empty() {
2270 try!(space(&mut self.s));
2271 try!(self.word_space(":"));
2272 try!(self.commasep(Inconsistent, &*options,
2273 |s, &co| {
2274 try!(s.print_string(co, ast::CookedStr));
2275 Ok(())
2276 }));
2277 }
2278
2279 try!(self.pclose());
2280 }
2281 ast::ExprMac(ref m) => try!(self.print_mac(m, token::Paren)),
2282 ast::ExprParen(ref e) => {
2283 try!(self.popen());
2284 try!(self.print_inner_attributes_inline(attrs));
2285 try!(self.print_expr(&**e));
2286 try!(self.pclose());
2287 }
2288 }
2289 try!(self.ann.post(self, NodeExpr(expr)));
2290 self.end()
2291 }
2292
2293 pub fn print_local_decl(&mut self, loc: &ast::Local) -> io::Result<()> {
2294 try!(self.print_pat(&*loc.pat));
2295 if let Some(ref ty) = loc.ty {
2296 try!(self.word_space(":"));
2297 try!(self.print_type(&**ty));
2298 }
2299 Ok(())
2300 }
2301
2302 pub fn print_decl(&mut self, decl: &ast::Decl) -> io::Result<()> {
2303 try!(self.maybe_print_comment(decl.span.lo));
2304 match decl.node {
2305 ast::DeclLocal(ref loc) => {
2306 try!(self.print_outer_attributes(loc.attrs.as_attr_slice()));
2307 try!(self.space_if_not_bol());
2308 try!(self.ibox(INDENT_UNIT));
2309 try!(self.word_nbsp("let"));
2310
2311 try!(self.ibox(INDENT_UNIT));
2312 try!(self.print_local_decl(&**loc));
2313 try!(self.end());
2314 if let Some(ref init) = loc.init {
2315 try!(self.nbsp());
2316 try!(self.word_space("="));
2317 try!(self.print_expr(&**init));
2318 }
2319 self.end()
2320 }
2321 ast::DeclItem(ref item) => self.print_item(&**item)
2322 }
2323 }
2324
2325 pub fn print_ident(&mut self, ident: ast::Ident) -> io::Result<()> {
2326 try!(word(&mut self.s, &ident.name.as_str()));
2327 self.ann.post(self, NodeIdent(&ident))
2328 }
2329
2330 pub fn print_usize(&mut self, i: usize) -> io::Result<()> {
2331 word(&mut self.s, &i.to_string())
2332 }
2333
2334 pub fn print_name(&mut self, name: ast::Name) -> io::Result<()> {
2335 try!(word(&mut self.s, &name.as_str()));
2336 self.ann.post(self, NodeName(&name))
2337 }
2338
2339 pub fn print_for_decl(&mut self, loc: &ast::Local,
2340 coll: &ast::Expr) -> io::Result<()> {
2341 try!(self.print_local_decl(loc));
2342 try!(space(&mut self.s));
2343 try!(self.word_space("in"));
2344 self.print_expr(coll)
2345 }
2346
2347 fn print_path(&mut self,
2348 path: &ast::Path,
2349 colons_before_params: bool,
2350 depth: usize)
2351 -> io::Result<()>
2352 {
2353 try!(self.maybe_print_comment(path.span.lo));
2354
2355 let mut first = !path.global;
2356 for segment in &path.segments[..path.segments.len()-depth] {
2357 if first {
2358 first = false
2359 } else {
2360 try!(word(&mut self.s, "::"))
2361 }
2362
2363 try!(self.print_ident(segment.identifier));
2364
2365 try!(self.print_path_parameters(&segment.parameters, colons_before_params));
2366 }
2367
2368 Ok(())
2369 }
2370
2371 fn print_qpath(&mut self,
2372 path: &ast::Path,
2373 qself: &ast::QSelf,
2374 colons_before_params: bool)
2375 -> io::Result<()>
2376 {
2377 try!(word(&mut self.s, "<"));
2378 try!(self.print_type(&qself.ty));
2379 if qself.position > 0 {
2380 try!(space(&mut self.s));
2381 try!(self.word_space("as"));
2382 let depth = path.segments.len() - qself.position;
2383 try!(self.print_path(&path, false, depth));
2384 }
2385 try!(word(&mut self.s, ">"));
2386 try!(word(&mut self.s, "::"));
2387 let item_segment = path.segments.last().unwrap();
2388 try!(self.print_ident(item_segment.identifier));
2389 self.print_path_parameters(&item_segment.parameters, colons_before_params)
2390 }
2391
2392 fn print_path_parameters(&mut self,
2393 parameters: &ast::PathParameters,
2394 colons_before_params: bool)
2395 -> io::Result<()>
2396 {
2397 if parameters.is_empty() {
2398 return Ok(());
2399 }
2400
2401 if colons_before_params {
2402 try!(word(&mut self.s, "::"))
2403 }
2404
2405 match *parameters {
2406 ast::PathParameters::AngleBracketed(ref data) => {
2407 try!(word(&mut self.s, "<"));
2408
2409 let mut comma = false;
2410 for lifetime in &data.lifetimes {
2411 if comma {
2412 try!(self.word_space(","))
2413 }
2414 try!(self.print_lifetime(lifetime));
2415 comma = true;
2416 }
2417
2418 if !data.types.is_empty() {
2419 if comma {
2420 try!(self.word_space(","))
2421 }
2422 try!(self.commasep(
2423 Inconsistent,
2424 &data.types,
2425 |s, ty| s.print_type(&**ty)));
2426 comma = true;
2427 }
2428
2429 for binding in data.bindings.iter() {
2430 if comma {
2431 try!(self.word_space(","))
2432 }
2433 try!(self.print_ident(binding.ident));
2434 try!(space(&mut self.s));
2435 try!(self.word_space("="));
2436 try!(self.print_type(&*binding.ty));
2437 comma = true;
2438 }
2439
2440 try!(word(&mut self.s, ">"))
2441 }
2442
2443 ast::PathParameters::Parenthesized(ref data) => {
2444 try!(word(&mut self.s, "("));
2445 try!(self.commasep(
2446 Inconsistent,
2447 &data.inputs,
2448 |s, ty| s.print_type(&**ty)));
2449 try!(word(&mut self.s, ")"));
2450
2451 match data.output {
2452 None => { }
2453 Some(ref ty) => {
2454 try!(self.space_if_not_bol());
2455 try!(self.word_space("->"));
2456 try!(self.print_type(&**ty));
2457 }
2458 }
2459 }
2460 }
2461
2462 Ok(())
2463 }
2464
2465 pub fn print_pat(&mut self, pat: &ast::Pat) -> io::Result<()> {
2466 try!(self.maybe_print_comment(pat.span.lo));
2467 try!(self.ann.pre(self, NodePat(pat)));
2468 /* Pat isn't normalized, but the beauty of it
2469 is that it doesn't matter */
2470 match pat.node {
2471 ast::PatWild => try!(word(&mut self.s, "_")),
2472 ast::PatIdent(binding_mode, ref path1, ref sub) => {
2473 match binding_mode {
2474 ast::BindingMode::ByRef(mutbl) => {
2475 try!(self.word_nbsp("ref"));
2476 try!(self.print_mutability(mutbl));
2477 }
2478 ast::BindingMode::ByValue(ast::MutImmutable) => {}
2479 ast::BindingMode::ByValue(ast::MutMutable) => {
2480 try!(self.word_nbsp("mut"));
2481 }
2482 }
2483 try!(self.print_ident(path1.node));
2484 match *sub {
2485 Some(ref p) => {
2486 try!(word(&mut self.s, "@"));
2487 try!(self.print_pat(&**p));
2488 }
2489 None => ()
2490 }
2491 }
2492 ast::PatEnum(ref path, ref args_) => {
2493 try!(self.print_path(path, true, 0));
2494 match *args_ {
2495 None => try!(word(&mut self.s, "(..)")),
2496 Some(ref args) => {
2497 if !args.is_empty() {
2498 try!(self.popen());
2499 try!(self.commasep(Inconsistent, &args[..],
2500 |s, p| s.print_pat(&**p)));
2501 try!(self.pclose());
2502 }
2503 }
2504 }
2505 }
2506 ast::PatQPath(ref qself, ref path) => {
2507 try!(self.print_qpath(path, qself, false));
2508 }
2509 ast::PatStruct(ref path, ref fields, etc) => {
2510 try!(self.print_path(path, true, 0));
2511 try!(self.nbsp());
2512 try!(self.word_space("{"));
2513 try!(self.commasep_cmnt(
2514 Consistent, &fields[..],
2515 |s, f| {
2516 try!(s.cbox(INDENT_UNIT));
2517 if !f.node.is_shorthand {
2518 try!(s.print_ident(f.node.ident));
2519 try!(s.word_nbsp(":"));
2520 }
2521 try!(s.print_pat(&*f.node.pat));
2522 s.end()
2523 },
2524 |f| f.node.pat.span));
2525 if etc {
2526 if !fields.is_empty() { try!(self.word_space(",")); }
2527 try!(word(&mut self.s, ".."));
2528 }
2529 try!(space(&mut self.s));
2530 try!(word(&mut self.s, "}"));
2531 }
2532 ast::PatTup(ref elts) => {
2533 try!(self.popen());
2534 try!(self.commasep(Inconsistent,
2535 &elts[..],
2536 |s, p| s.print_pat(&**p)));
2537 if elts.len() == 1 {
2538 try!(word(&mut self.s, ","));
2539 }
2540 try!(self.pclose());
2541 }
2542 ast::PatBox(ref inner) => {
2543 try!(word(&mut self.s, "box "));
2544 try!(self.print_pat(&**inner));
2545 }
2546 ast::PatRegion(ref inner, mutbl) => {
2547 try!(word(&mut self.s, "&"));
2548 if mutbl == ast::MutMutable {
2549 try!(word(&mut self.s, "mut "));
2550 }
2551 try!(self.print_pat(&**inner));
2552 }
2553 ast::PatLit(ref e) => try!(self.print_expr(&**e)),
2554 ast::PatRange(ref begin, ref end) => {
2555 try!(self.print_expr(&**begin));
2556 try!(space(&mut self.s));
2557 try!(word(&mut self.s, "..."));
2558 try!(self.print_expr(&**end));
2559 }
2560 ast::PatVec(ref before, ref slice, ref after) => {
2561 try!(word(&mut self.s, "["));
2562 try!(self.commasep(Inconsistent,
2563 &before[..],
2564 |s, p| s.print_pat(&**p)));
2565 if let Some(ref p) = *slice {
2566 if !before.is_empty() { try!(self.word_space(",")); }
2567 if p.node != ast::PatWild {
2568 try!(self.print_pat(&**p));
2569 }
2570 try!(word(&mut self.s, ".."));
2571 if !after.is_empty() { try!(self.word_space(",")); }
2572 }
2573 try!(self.commasep(Inconsistent,
2574 &after[..],
2575 |s, p| s.print_pat(&**p)));
2576 try!(word(&mut self.s, "]"));
2577 }
2578 ast::PatMac(ref m) => try!(self.print_mac(m, token::Paren)),
2579 }
2580 self.ann.post(self, NodePat(pat))
2581 }
2582
2583 fn print_arm(&mut self, arm: &ast::Arm) -> io::Result<()> {
2584 // I have no idea why this check is necessary, but here it
2585 // is :(
2586 if arm.attrs.is_empty() {
2587 try!(space(&mut self.s));
2588 }
2589 try!(self.cbox(INDENT_UNIT));
2590 try!(self.ibox(0));
2591 try!(self.print_outer_attributes(&arm.attrs));
2592 let mut first = true;
2593 for p in &arm.pats {
2594 if first {
2595 first = false;
2596 } else {
2597 try!(space(&mut self.s));
2598 try!(self.word_space("|"));
2599 }
2600 try!(self.print_pat(&**p));
2601 }
2602 try!(space(&mut self.s));
2603 if let Some(ref e) = arm.guard {
2604 try!(self.word_space("if"));
2605 try!(self.print_expr(&**e));
2606 try!(space(&mut self.s));
2607 }
2608 try!(self.word_space("=>"));
2609
2610 match arm.body.node {
2611 ast::ExprBlock(ref blk) => {
2612 // the block will close the pattern's ibox
2613 try!(self.print_block_unclosed_indent(&**blk, INDENT_UNIT));
2614
2615 // If it is a user-provided unsafe block, print a comma after it
2616 if let ast::UnsafeBlock(ast::UserProvided) = blk.rules {
2617 try!(word(&mut self.s, ","));
2618 }
2619 }
2620 _ => {
2621 try!(self.end()); // close the ibox for the pattern
2622 try!(self.print_expr(&*arm.body));
2623 try!(word(&mut self.s, ","));
2624 }
2625 }
2626 self.end() // close enclosing cbox
2627 }
2628
2629 // Returns whether it printed anything
2630 fn print_explicit_self(&mut self,
2631 explicit_self: &ast::ExplicitSelf_,
2632 mutbl: ast::Mutability) -> io::Result<bool> {
2633 try!(self.print_mutability(mutbl));
2634 match *explicit_self {
2635 ast::SelfStatic => { return Ok(false); }
2636 ast::SelfValue(_) => {
2637 try!(word(&mut self.s, "self"));
2638 }
2639 ast::SelfRegion(ref lt, m, _) => {
2640 try!(word(&mut self.s, "&"));
2641 try!(self.print_opt_lifetime(lt));
2642 try!(self.print_mutability(m));
2643 try!(word(&mut self.s, "self"));
2644 }
2645 ast::SelfExplicit(ref typ, _) => {
2646 try!(word(&mut self.s, "self"));
2647 try!(self.word_space(":"));
2648 try!(self.print_type(&**typ));
2649 }
2650 }
2651 return Ok(true);
2652 }
2653
2654 pub fn print_fn(&mut self,
2655 decl: &ast::FnDecl,
2656 unsafety: ast::Unsafety,
2657 constness: ast::Constness,
2658 abi: abi::Abi,
2659 name: Option<ast::Ident>,
2660 generics: &ast::Generics,
2661 opt_explicit_self: Option<&ast::ExplicitSelf_>,
2662 vis: ast::Visibility) -> io::Result<()> {
2663 try!(self.print_fn_header_info(unsafety, constness, abi, vis));
2664
2665 if let Some(name) = name {
2666 try!(self.nbsp());
2667 try!(self.print_ident(name));
2668 }
2669 try!(self.print_generics(generics));
2670 try!(self.print_fn_args_and_ret(decl, opt_explicit_self));
2671 self.print_where_clause(&generics.where_clause)
2672 }
2673
2674 pub fn print_fn_args(&mut self, decl: &ast::FnDecl,
2675 opt_explicit_self: Option<&ast::ExplicitSelf_>)
2676 -> io::Result<()> {
2677 // It is unfortunate to duplicate the commasep logic, but we want the
2678 // self type and the args all in the same box.
2679 try!(self.rbox(0, Inconsistent));
2680 let mut first = true;
2681 if let Some(explicit_self) = opt_explicit_self {
2682 let m = match *explicit_self {
2683 ast::SelfStatic => ast::MutImmutable,
2684 _ => match decl.inputs[0].pat.node {
2685 ast::PatIdent(ast::BindingMode::ByValue(m), _, _) => m,
2686 _ => ast::MutImmutable
2687 }
2688 };
2689 first = !try!(self.print_explicit_self(explicit_self, m));
2690 }
2691
2692 // HACK(eddyb) ignore the separately printed self argument.
2693 let args = if first {
2694 &decl.inputs[..]
2695 } else {
2696 &decl.inputs[1..]
2697 };
2698
2699 for arg in args {
2700 if first { first = false; } else { try!(self.word_space(",")); }
2701 try!(self.print_arg(arg));
2702 }
2703
2704 self.end()
2705 }
2706
2707 pub fn print_fn_args_and_ret(&mut self, decl: &ast::FnDecl,
2708 opt_explicit_self: Option<&ast::ExplicitSelf_>)
2709 -> io::Result<()> {
2710 try!(self.popen());
2711 try!(self.print_fn_args(decl, opt_explicit_self));
2712 if decl.variadic {
2713 try!(word(&mut self.s, ", ..."));
2714 }
2715 try!(self.pclose());
2716
2717 self.print_fn_output(decl)
2718 }
2719
2720 pub fn print_fn_block_args(
2721 &mut self,
2722 decl: &ast::FnDecl)
2723 -> io::Result<()> {
2724 try!(word(&mut self.s, "|"));
2725 try!(self.print_fn_args(decl, None));
2726 try!(word(&mut self.s, "|"));
2727
2728 if let ast::DefaultReturn(..) = decl.output {
2729 return Ok(());
2730 }
2731
2732 try!(self.space_if_not_bol());
2733 try!(self.word_space("->"));
2734 match decl.output {
2735 ast::Return(ref ty) => {
2736 try!(self.print_type(&**ty));
2737 self.maybe_print_comment(ty.span.lo)
2738 }
2739 ast::DefaultReturn(..) => unreachable!(),
2740 ast::NoReturn(span) => {
2741 try!(self.word_nbsp("!"));
2742 self.maybe_print_comment(span.lo)
2743 }
2744 }
2745 }
2746
2747 pub fn print_capture_clause(&mut self, capture_clause: ast::CaptureClause)
2748 -> io::Result<()> {
2749 match capture_clause {
2750 ast::CaptureByValue => self.word_space("move"),
2751 ast::CaptureByRef => Ok(()),
2752 }
2753 }
2754
2755 pub fn print_bounds(&mut self,
2756 prefix: &str,
2757 bounds: &[ast::TyParamBound])
2758 -> io::Result<()> {
2759 if !bounds.is_empty() {
2760 try!(word(&mut self.s, prefix));
2761 let mut first = true;
2762 for bound in bounds {
2763 try!(self.nbsp());
2764 if first {
2765 first = false;
2766 } else {
2767 try!(self.word_space("+"));
2768 }
2769
2770 try!(match *bound {
2771 TraitTyParamBound(ref tref, TraitBoundModifier::None) => {
2772 self.print_poly_trait_ref(tref)
2773 }
2774 TraitTyParamBound(ref tref, TraitBoundModifier::Maybe) => {
2775 try!(word(&mut self.s, "?"));
2776 self.print_poly_trait_ref(tref)
2777 }
2778 RegionTyParamBound(ref lt) => {
2779 self.print_lifetime(lt)
2780 }
2781 })
2782 }
2783 Ok(())
2784 } else {
2785 Ok(())
2786 }
2787 }
2788
2789 pub fn print_lifetime(&mut self,
2790 lifetime: &ast::Lifetime)
2791 -> io::Result<()>
2792 {
2793 self.print_name(lifetime.name)
2794 }
2795
2796 pub fn print_lifetime_def(&mut self,
2797 lifetime: &ast::LifetimeDef)
2798 -> io::Result<()>
2799 {
2800 try!(self.print_lifetime(&lifetime.lifetime));
2801 let mut sep = ":";
2802 for v in &lifetime.bounds {
2803 try!(word(&mut self.s, sep));
2804 try!(self.print_lifetime(v));
2805 sep = "+";
2806 }
2807 Ok(())
2808 }
2809
2810 pub fn print_generics(&mut self,
2811 generics: &ast::Generics)
2812 -> io::Result<()>
2813 {
2814 let total = generics.lifetimes.len() + generics.ty_params.len();
2815 if total == 0 {
2816 return Ok(());
2817 }
2818
2819 try!(word(&mut self.s, "<"));
2820
2821 let mut ints = Vec::new();
2822 for i in 0..total {
2823 ints.push(i);
2824 }
2825
2826 try!(self.commasep(Inconsistent, &ints[..], |s, &idx| {
2827 if idx < generics.lifetimes.len() {
2828 let lifetime = &generics.lifetimes[idx];
2829 s.print_lifetime_def(lifetime)
2830 } else {
2831 let idx = idx - generics.lifetimes.len();
2832 let param = &generics.ty_params[idx];
2833 s.print_ty_param(param)
2834 }
2835 }));
2836
2837 try!(word(&mut self.s, ">"));
2838 Ok(())
2839 }
2840
2841 pub fn print_ty_param(&mut self, param: &ast::TyParam) -> io::Result<()> {
2842 try!(self.print_ident(param.ident));
2843 try!(self.print_bounds(":", &param.bounds));
2844 match param.default {
2845 Some(ref default) => {
2846 try!(space(&mut self.s));
2847 try!(self.word_space("="));
2848 self.print_type(&**default)
2849 }
2850 _ => Ok(())
2851 }
2852 }
2853
2854 pub fn print_where_clause(&mut self, where_clause: &ast::WhereClause)
2855 -> io::Result<()> {
2856 if where_clause.predicates.is_empty() {
2857 return Ok(())
2858 }
2859
2860 try!(space(&mut self.s));
2861 try!(self.word_space("where"));
2862
2863 for (i, predicate) in where_clause.predicates.iter().enumerate() {
2864 if i != 0 {
2865 try!(self.word_space(","));
2866 }
2867
2868 match *predicate {
2869 ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate{ref bound_lifetimes,
2870 ref bounded_ty,
2871 ref bounds,
2872 ..}) => {
2873 try!(self.print_formal_lifetime_list(bound_lifetimes));
2874 try!(self.print_type(&**bounded_ty));
2875 try!(self.print_bounds(":", bounds));
2876 }
2877 ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate{ref lifetime,
2878 ref bounds,
2879 ..}) => {
2880 try!(self.print_lifetime(lifetime));
2881 try!(word(&mut self.s, ":"));
2882
2883 for (i, bound) in bounds.iter().enumerate() {
2884 try!(self.print_lifetime(bound));
2885
2886 if i != 0 {
2887 try!(word(&mut self.s, ":"));
2888 }
2889 }
2890 }
2891 ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{ref path, ref ty, ..}) => {
2892 try!(self.print_path(path, false, 0));
2893 try!(space(&mut self.s));
2894 try!(self.word_space("="));
2895 try!(self.print_type(&**ty));
2896 }
2897 }
2898 }
2899
2900 Ok(())
2901 }
2902
2903 pub fn print_view_path(&mut self, vp: &ast::ViewPath) -> io::Result<()> {
2904 match vp.node {
2905 ast::ViewPathSimple(ident, ref path) => {
2906 try!(self.print_path(path, false, 0));
2907
2908 if path.segments.last().unwrap().identifier.name !=
2909 ident.name {
2910 try!(space(&mut self.s));
2911 try!(self.word_space("as"));
2912 try!(self.print_ident(ident));
2913 }
2914
2915 Ok(())
2916 }
2917
2918 ast::ViewPathGlob(ref path) => {
2919 try!(self.print_path(path, false, 0));
2920 word(&mut self.s, "::*")
2921 }
2922
2923 ast::ViewPathList(ref path, ref idents) => {
2924 if path.segments.is_empty() {
2925 try!(word(&mut self.s, "{"));
2926 } else {
2927 try!(self.print_path(path, false, 0));
2928 try!(word(&mut self.s, "::{"));
2929 }
2930 try!(self.commasep(Inconsistent, &idents[..], |s, w| {
2931 match w.node {
2932 ast::PathListIdent { name, rename, .. } => {
2933 try!(s.print_ident(name));
2934 if let Some(ident) = rename {
2935 try!(space(&mut s.s));
2936 try!(s.word_space("as"));
2937 try!(s.print_ident(ident));
2938 }
2939 Ok(())
2940 },
2941 ast::PathListMod { rename, .. } => {
2942 try!(word(&mut s.s, "self"));
2943 if let Some(ident) = rename {
2944 try!(space(&mut s.s));
2945 try!(s.word_space("as"));
2946 try!(s.print_ident(ident));
2947 }
2948 Ok(())
2949 }
2950 }
2951 }));
2952 word(&mut self.s, "}")
2953 }
2954 }
2955 }
2956
2957 pub fn print_mutability(&mut self,
2958 mutbl: ast::Mutability) -> io::Result<()> {
2959 match mutbl {
2960 ast::MutMutable => self.word_nbsp("mut"),
2961 ast::MutImmutable => Ok(()),
2962 }
2963 }
2964
2965 pub fn print_mt(&mut self, mt: &ast::MutTy) -> io::Result<()> {
2966 try!(self.print_mutability(mt.mutbl));
2967 self.print_type(&*mt.ty)
2968 }
2969
2970 pub fn print_arg(&mut self, input: &ast::Arg) -> io::Result<()> {
2971 try!(self.ibox(INDENT_UNIT));
2972 match input.ty.node {
2973 ast::TyInfer => try!(self.print_pat(&*input.pat)),
2974 _ => {
2975 match input.pat.node {
2976 ast::PatIdent(_, ref path1, _) if
2977 path1.node.name ==
2978 parse::token::special_idents::invalid.name => {
2979 // Do nothing.
2980 }
2981 _ => {
2982 try!(self.print_pat(&*input.pat));
2983 try!(word(&mut self.s, ":"));
2984 try!(space(&mut self.s));
2985 }
2986 }
2987 try!(self.print_type(&*input.ty));
2988 }
2989 }
2990 self.end()
2991 }
2992
2993 pub fn print_fn_output(&mut self, decl: &ast::FnDecl) -> io::Result<()> {
2994 if let ast::DefaultReturn(..) = decl.output {
2995 return Ok(());
2996 }
2997
2998 try!(self.space_if_not_bol());
2999 try!(self.ibox(INDENT_UNIT));
3000 try!(self.word_space("->"));
3001 match decl.output {
3002 ast::NoReturn(_) =>
3003 try!(self.word_nbsp("!")),
3004 ast::DefaultReturn(..) => unreachable!(),
3005 ast::Return(ref ty) =>
3006 try!(self.print_type(&**ty))
3007 }
3008 try!(self.end());
3009
3010 match decl.output {
3011 ast::Return(ref output) => self.maybe_print_comment(output.span.lo),
3012 _ => Ok(())
3013 }
3014 }
3015
3016 pub fn print_ty_fn(&mut self,
3017 abi: abi::Abi,
3018 unsafety: ast::Unsafety,
3019 decl: &ast::FnDecl,
3020 name: Option<ast::Ident>,
3021 generics: &ast::Generics,
3022 opt_explicit_self: Option<&ast::ExplicitSelf_>)
3023 -> io::Result<()> {
3024 try!(self.ibox(INDENT_UNIT));
3025 if !generics.lifetimes.is_empty() || !generics.ty_params.is_empty() {
3026 try!(word(&mut self.s, "for"));
3027 try!(self.print_generics(generics));
3028 }
3029 let generics = ast::Generics {
3030 lifetimes: Vec::new(),
3031 ty_params: P::empty(),
3032 where_clause: ast::WhereClause {
3033 id: ast::DUMMY_NODE_ID,
3034 predicates: Vec::new(),
3035 },
3036 };
3037 try!(self.print_fn(decl,
3038 unsafety,
3039 ast::Constness::NotConst,
3040 abi,
3041 name,
3042 &generics,
3043 opt_explicit_self,
3044 ast::Inherited));
3045 self.end()
3046 }
3047
3048 pub fn maybe_print_trailing_comment(&mut self, span: codemap::Span,
3049 next_pos: Option<BytePos>)
3050 -> io::Result<()> {
3051 let cm = match self.cm {
3052 Some(cm) => cm,
3053 _ => return Ok(())
3054 };
3055 match self.next_comment() {
3056 Some(ref cmnt) => {
3057 if (*cmnt).style != comments::Trailing { return Ok(()) }
3058 let span_line = cm.lookup_char_pos(span.hi);
3059 let comment_line = cm.lookup_char_pos((*cmnt).pos);
3060 let mut next = (*cmnt).pos + BytePos(1);
3061 match next_pos { None => (), Some(p) => next = p }
3062 if span.hi < (*cmnt).pos && (*cmnt).pos < next &&
3063 span_line.line == comment_line.line {
3064 try!(self.print_comment(cmnt));
3065 self.cur_cmnt_and_lit.cur_cmnt += 1;
3066 }
3067 }
3068 _ => ()
3069 }
3070 Ok(())
3071 }
3072
3073 pub fn print_remaining_comments(&mut self) -> io::Result<()> {
3074 // If there aren't any remaining comments, then we need to manually
3075 // make sure there is a line break at the end.
3076 if self.next_comment().is_none() {
3077 try!(hardbreak(&mut self.s));
3078 }
3079 loop {
3080 match self.next_comment() {
3081 Some(ref cmnt) => {
3082 try!(self.print_comment(cmnt));
3083 self.cur_cmnt_and_lit.cur_cmnt += 1;
3084 }
3085 _ => break
3086 }
3087 }
3088 Ok(())
3089 }
3090
3091 pub fn print_opt_abi_and_extern_if_nondefault(&mut self,
3092 opt_abi: Option<abi::Abi>)
3093 -> io::Result<()> {
3094 match opt_abi {
3095 Some(abi::Rust) => Ok(()),
3096 Some(abi) => {
3097 try!(self.word_nbsp("extern"));
3098 self.word_nbsp(&abi.to_string())
3099 }
3100 None => Ok(())
3101 }
3102 }
3103
3104 pub fn print_extern_opt_abi(&mut self,
3105 opt_abi: Option<abi::Abi>) -> io::Result<()> {
3106 match opt_abi {
3107 Some(abi) => {
3108 try!(self.word_nbsp("extern"));
3109 self.word_nbsp(&abi.to_string())
3110 }
3111 None => Ok(())
3112 }
3113 }
3114
3115 pub fn print_fn_header_info(&mut self,
3116 unsafety: ast::Unsafety,
3117 constness: ast::Constness,
3118 abi: abi::Abi,
3119 vis: ast::Visibility) -> io::Result<()> {
3120 try!(word(&mut self.s, &visibility_qualified(vis, "")));
3121
3122 match constness {
3123 ast::Constness::NotConst => {}
3124 ast::Constness::Const => try!(self.word_nbsp("const"))
3125 }
3126
3127 try!(self.print_unsafety(unsafety));
3128
3129 if abi != abi::Rust {
3130 try!(self.word_nbsp("extern"));
3131 try!(self.word_nbsp(&abi.to_string()));
3132 }
3133
3134 word(&mut self.s, "fn")
3135 }
3136
3137 pub fn print_unsafety(&mut self, s: ast::Unsafety) -> io::Result<()> {
3138 match s {
3139 ast::Unsafety::Normal => Ok(()),
3140 ast::Unsafety::Unsafe => self.word_nbsp("unsafe"),
3141 }
3142 }
3143 }
3144
3145 fn repeat(s: &str, n: usize) -> String { iter::repeat(s).take(n).collect() }
3146
3147 #[cfg(test)]
3148 mod tests {
3149 use super::*;
3150
3151 use ast;
3152 use ast_util;
3153 use codemap;
3154 use parse::token;
3155
3156 #[test]
3157 fn test_fun_to_string() {
3158 let abba_ident = token::str_to_ident("abba");
3159
3160 let decl = ast::FnDecl {
3161 inputs: Vec::new(),
3162 output: ast::DefaultReturn(codemap::DUMMY_SP),
3163 variadic: false
3164 };
3165 let generics = ast::Generics::default();
3166 assert_eq!(fun_to_string(&decl, ast::Unsafety::Normal,
3167 ast::Constness::NotConst,
3168 abba_ident,
3169 None, &generics),
3170 "fn abba()");
3171 }
3172
3173 #[test]
3174 fn test_variant_to_string() {
3175 let ident = token::str_to_ident("principal_skinner");
3176
3177 let var = codemap::respan(codemap::DUMMY_SP, ast::Variant_ {
3178 name: ident,
3179 attrs: Vec::new(),
3180 // making this up as I go.... ?
3181 data: ast::VariantData::Unit(ast::DUMMY_NODE_ID),
3182 disr_expr: None,
3183 });
3184
3185 let varstr = variant_to_string(&var);
3186 assert_eq!(varstr, "principal_skinner");
3187 }
3188
3189 #[test]
3190 fn test_signed_int_to_string() {
3191 let pos_int = ast::LitInt(42, ast::SignedIntLit(ast::TyI32, ast::Plus));
3192 let neg_int = ast::LitInt((!42 + 1) as u64, ast::SignedIntLit(ast::TyI32, ast::Minus));
3193 assert_eq!(format!("-{}", lit_to_string(&codemap::dummy_spanned(pos_int))),
3194 lit_to_string(&codemap::dummy_spanned(neg_int)));
3195 }
3196 }