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.
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.
11 pub use self::AnnNode::*;
15 use ast::{RegionTyParamBound, TraitTyParamBound, TraitBoundModifier};
18 use owned_slice::OwnedSlice;
19 use attr::{AttrMetaMethods, AttributeMethods};
20 use codemap::{self, CodeMap, BytePos};
22 use parse::token::{self, BinOpToken, Token, InternedString};
23 use parse::lexer::comments;
25 use print::pp::{self, break_offset, word, space, zerobreak, hardbreak};
26 use print::pp::{Breaks, eof};
27 use print::pp::Breaks::{Consistent, Inconsistent};
32 use std::io::{self, Write, Read};
35 pub enum AnnNode<'a> {
36 NodeIdent(&'a ast::Ident),
37 NodeName(&'a ast::Name),
38 NodeBlock(&'a ast::Block),
39 NodeItem(&'a ast::Item),
40 NodeSubItem(ast::NodeId),
41 NodeExpr(&'a ast::Expr),
42 NodePat(&'a ast::Pat),
46 fn pre(&self, _state: &mut State, _node: AnnNode) -> io::Result<()> { Ok(()) }
47 fn post(&self, _state: &mut State, _node: AnnNode) -> io::Result<()> { Ok(()) }
50 #[derive(Copy, Clone)]
53 impl PpAnn for NoAnn {}
55 #[derive(Copy, Clone)]
56 pub struct CurrentCommentAndLiteral {
61 pub struct State<'a> {
62 pub s: pp::Printer<'a>,
63 cm: Option<&'a CodeMap>,
64 comments: Option<Vec<comments::Comment> >,
65 literals: Option<Vec<comments::Literal> >,
66 cur_cmnt_and_lit: CurrentCommentAndLiteral,
67 boxes: Vec<pp::Breaks>,
71 pub fn rust_printer<'a>(writer: Box<Write+'a>) -> State<'a> {
72 static NO_ANN: NoAnn = NoAnn;
73 rust_printer_annotated(writer, &NO_ANN)
76 pub fn rust_printer_annotated<'a>(writer: Box<Write+'a>,
77 ann: &'a PpAnn) -> State<'a> {
79 s: pp::mk_printer(writer, default_columns),
83 cur_cmnt_and_lit: CurrentCommentAndLiteral {
92 #[allow(non_upper_case_globals)]
93 pub const indent_unit: usize = 4;
95 #[allow(non_upper_case_globals)]
96 pub const default_columns: usize = 78;
98 /// Requires you to pass an input filename and reader so that
99 /// it can scan the input text for comments and literals to
101 pub fn print_crate<'a>(cm: &'a CodeMap,
102 span_diagnostic: &diagnostic::SpanHandler,
108 is_expanded: bool) -> io::Result<()> {
109 let mut s = State::new_from_input(cm,
116 if is_expanded && !std_inject::no_std(krate) {
117 // We need to print `#![no_std]` (and its feature gate) so that
118 // compiling pretty-printed source won't inject libstd again.
119 // However we don't want these attributes in the AST because
120 // of the feature gate, so we fake them up here.
122 let no_std_meta = attr::mk_word_item(InternedString::new("no_std"));
123 let prelude_import_meta = attr::mk_word_item(InternedString::new("prelude_import"));
125 // #![feature(no_std)]
126 let fake_attr = attr::mk_attr_inner(attr::mk_attr_id(),
127 attr::mk_list_item(InternedString::new("feature"),
128 vec![no_std_meta.clone(),
129 prelude_import_meta]));
130 try!(s.print_attribute(&fake_attr));
133 let fake_attr = attr::mk_attr_inner(attr::mk_attr_id(), no_std_meta);
134 try!(s.print_attribute(&fake_attr));
137 try!(s.print_mod(&krate.module, &krate.attrs));
138 try!(s.print_remaining_comments());
143 pub fn new_from_input(cm: &'a CodeMap,
144 span_diagnostic: &diagnostic::SpanHandler,
149 is_expanded: bool) -> State<'a> {
150 let (cmnts, lits) = comments::gather_comments_and_literals(
160 // If the code is post expansion, don't use the table of
161 // literals, since it doesn't correspond with the literals
162 // in the AST anymore.
163 if is_expanded { None } else { Some(lits) })
166 pub fn new(cm: &'a CodeMap,
169 comments: Option<Vec<comments::Comment>>,
170 literals: Option<Vec<comments::Literal>>) -> State<'a> {
172 s: pp::mk_printer(out, default_columns),
176 cur_cmnt_and_lit: CurrentCommentAndLiteral {
186 pub fn to_string<F>(f: F) -> String where
187 F: FnOnce(&mut State) -> io::Result<()>,
189 let mut wr = Vec::new();
191 let mut printer = rust_printer(Box::new(&mut wr));
192 f(&mut printer).unwrap();
193 eof(&mut printer.s).unwrap();
195 String::from_utf8(wr).unwrap()
198 pub fn binop_to_string(op: BinOpToken) -> &'static str {
204 token::Percent => "%",
213 pub fn token_to_string(tok: &Token) -> String {
215 token::Eq => "=".to_string(),
216 token::Lt => "<".to_string(),
217 token::Le => "<=".to_string(),
218 token::EqEq => "==".to_string(),
219 token::Ne => "!=".to_string(),
220 token::Ge => ">=".to_string(),
221 token::Gt => ">".to_string(),
222 token::Not => "!".to_string(),
223 token::Tilde => "~".to_string(),
224 token::OrOr => "||".to_string(),
225 token::AndAnd => "&&".to_string(),
226 token::BinOp(op) => binop_to_string(op).to_string(),
227 token::BinOpEq(op) => format!("{}=", binop_to_string(op)),
229 /* Structural symbols */
230 token::At => "@".to_string(),
231 token::Dot => ".".to_string(),
232 token::DotDot => "..".to_string(),
233 token::DotDotDot => "...".to_string(),
234 token::Comma => ",".to_string(),
235 token::Semi => ";".to_string(),
236 token::Colon => ":".to_string(),
237 token::ModSep => "::".to_string(),
238 token::RArrow => "->".to_string(),
239 token::LArrow => "<-".to_string(),
240 token::FatArrow => "=>".to_string(),
241 token::OpenDelim(token::Paren) => "(".to_string(),
242 token::CloseDelim(token::Paren) => ")".to_string(),
243 token::OpenDelim(token::Bracket) => "[".to_string(),
244 token::CloseDelim(token::Bracket) => "]".to_string(),
245 token::OpenDelim(token::Brace) => "{".to_string(),
246 token::CloseDelim(token::Brace) => "}".to_string(),
247 token::Pound => "#".to_string(),
248 token::Dollar => "$".to_string(),
249 token::Question => "?".to_string(),
252 token::Literal(lit, suf) => {
253 let mut out = match lit {
254 token::Byte(b) => format!("b'{}'", b),
255 token::Char(c) => format!("'{}'", c),
256 token::Float(c) => c.to_string(),
257 token::Integer(c) => c.to_string(),
258 token::Str_(s) => format!("\"{}\"", s),
259 token::StrRaw(s, n) => format!("r{delim}\"{string}\"{delim}",
260 delim=repeat("#", n),
262 token::Binary(v) => format!("b\"{}\"", v),
263 token::BinaryRaw(s, n) => format!("br{delim}\"{string}\"{delim}",
264 delim=repeat("#", n),
268 if let Some(s) = suf {
269 out.push_str(&s.as_str())
275 /* Name components */
276 token::Ident(s, _) => s.to_string(),
277 token::Lifetime(s) => s.to_string(),
278 token::Underscore => "_".to_string(),
281 token::DocComment(s) => s.to_string(),
282 token::SubstNt(s, _) => format!("${}", s),
283 token::MatchNt(s, t, _, _) => format!("${}:{}", s, t),
284 token::Eof => "<eof>".to_string(),
285 token::Whitespace => " ".to_string(),
286 token::Comment => "/* */".to_string(),
287 token::Shebang(s) => format!("/* shebang: {}*/", s),
289 token::SpecialVarNt(var) => format!("${}", var.as_str()),
291 token::Interpolated(ref nt) => match *nt {
292 token::NtExpr(ref e) => expr_to_string(&**e),
293 token::NtMeta(ref e) => meta_item_to_string(&**e),
294 token::NtTy(ref e) => ty_to_string(&**e),
295 token::NtPath(ref e) => path_to_string(&**e),
296 token::NtItem(ref e) => item_to_string(&**e),
297 token::NtBlock(ref e) => block_to_string(&**e),
298 token::NtStmt(ref e) => stmt_to_string(&**e),
299 token::NtPat(ref e) => pat_to_string(&**e),
300 token::NtIdent(ref e, _) => ident_to_string(&**e),
301 token::NtTT(ref e) => tt_to_string(&**e),
302 token::NtArm(ref e) => arm_to_string(&*e),
303 token::NtImplItem(ref e) => impl_item_to_string(&**e),
304 token::NtTraitItem(ref e) => trait_item_to_string(&**e),
305 token::NtGenerics(ref e) => generics_to_string(&*e),
306 token::NtWhereClause(ref e) => where_clause_to_string(&*e),
311 pub fn ty_to_string(ty: &ast::Ty) -> String {
312 to_string(|s| s.print_type(ty))
315 pub fn bounds_to_string(bounds: &[ast::TyParamBound]) -> String {
316 to_string(|s| s.print_bounds("", bounds))
319 pub fn pat_to_string(pat: &ast::Pat) -> String {
320 to_string(|s| s.print_pat(pat))
323 pub fn arm_to_string(arm: &ast::Arm) -> String {
324 to_string(|s| s.print_arm(arm))
327 pub fn expr_to_string(e: &ast::Expr) -> String {
328 to_string(|s| s.print_expr(e))
331 pub fn lifetime_to_string(e: &ast::Lifetime) -> String {
332 to_string(|s| s.print_lifetime(e))
335 pub fn tt_to_string(tt: &ast::TokenTree) -> String {
336 to_string(|s| s.print_tt(tt))
339 pub fn tts_to_string(tts: &[ast::TokenTree]) -> String {
340 to_string(|s| s.print_tts(tts))
343 pub fn stmt_to_string(stmt: &ast::Stmt) -> String {
344 to_string(|s| s.print_stmt(stmt))
347 pub fn attr_to_string(attr: &ast::Attribute) -> String {
348 to_string(|s| s.print_attribute(attr))
351 pub fn item_to_string(i: &ast::Item) -> String {
352 to_string(|s| s.print_item(i))
355 pub fn impl_item_to_string(i: &ast::ImplItem) -> String {
356 to_string(|s| s.print_impl_item(i))
359 pub fn trait_item_to_string(i: &ast::TraitItem) -> String {
360 to_string(|s| s.print_trait_item(i))
363 pub fn generics_to_string(generics: &ast::Generics) -> String {
364 to_string(|s| s.print_generics(generics))
367 pub fn where_clause_to_string(i: &ast::WhereClause) -> String {
368 to_string(|s| s.print_where_clause(i))
371 pub fn fn_block_to_string(p: &ast::FnDecl) -> String {
372 to_string(|s| s.print_fn_block_args(p))
375 pub fn path_to_string(p: &ast::Path) -> String {
376 to_string(|s| s.print_path(p, false, 0))
379 pub fn ident_to_string(id: &ast::Ident) -> String {
380 to_string(|s| s.print_ident(*id))
383 pub fn fun_to_string(decl: &ast::FnDecl,
384 unsafety: ast::Unsafety,
385 constness: ast::Constness,
387 opt_explicit_self: Option<&ast::ExplicitSelf_>,
388 generics: &ast::Generics)
392 try!(s.print_fn(decl, unsafety, constness, abi::Rust, Some(name),
393 generics, opt_explicit_self, ast::Inherited));
394 try!(s.end()); // Close the head box
395 s.end() // Close the outer box
399 pub fn block_to_string(blk: &ast::Block) -> String {
401 // containing cbox, will be closed by print-block at }
402 try!(s.cbox(indent_unit));
403 // head-ibox, will be closed by print-block after {
409 pub fn meta_item_to_string(mi: &ast::MetaItem) -> String {
410 to_string(|s| s.print_meta_item(mi))
413 pub fn attribute_to_string(attr: &ast::Attribute) -> String {
414 to_string(|s| s.print_attribute(attr))
417 pub fn lit_to_string(l: &ast::Lit) -> String {
418 to_string(|s| s.print_literal(l))
421 pub fn explicit_self_to_string(explicit_self: &ast::ExplicitSelf_) -> String {
422 to_string(|s| s.print_explicit_self(explicit_self, ast::MutImmutable).map(|_| {}))
425 pub fn variant_to_string(var: &ast::Variant) -> String {
426 to_string(|s| s.print_variant(var))
429 pub fn arg_to_string(arg: &ast::Arg) -> String {
430 to_string(|s| s.print_arg(arg))
433 pub fn mac_to_string(arg: &ast::Mac) -> String {
434 to_string(|s| s.print_mac(arg, ::parse::token::Paren))
437 pub fn visibility_qualified(vis: ast::Visibility, s: &str) -> String {
439 ast::Public => format!("pub {}", s),
440 ast::Inherited => s.to_string()
444 fn needs_parentheses(expr: &ast::Expr) -> bool {
446 ast::ExprAssign(..) | ast::ExprBinary(..) |
447 ast::ExprClosure(..) |
448 ast::ExprAssignOp(..) | ast::ExprCast(..) => true,
454 pub fn ibox(&mut self, u: usize) -> io::Result<()> {
455 self.boxes.push(pp::Breaks::Inconsistent);
456 pp::ibox(&mut self.s, u)
459 pub fn end(&mut self) -> io::Result<()> {
460 self.boxes.pop().unwrap();
464 pub fn cbox(&mut self, u: usize) -> io::Result<()> {
465 self.boxes.push(pp::Breaks::Consistent);
466 pp::cbox(&mut self.s, u)
470 pub fn rbox(&mut self, u: usize, b: pp::Breaks) -> io::Result<()> {
472 pp::rbox(&mut self.s, u, b)
475 pub fn nbsp(&mut self) -> io::Result<()> { word(&mut self.s, " ") }
477 pub fn word_nbsp(&mut self, w: &str) -> io::Result<()> {
478 try!(word(&mut self.s, w));
482 pub fn word_space(&mut self, w: &str) -> io::Result<()> {
483 try!(word(&mut self.s, w));
487 pub fn popen(&mut self) -> io::Result<()> { word(&mut self.s, "(") }
489 pub fn pclose(&mut self) -> io::Result<()> { word(&mut self.s, ")") }
491 pub fn head(&mut self, w: &str) -> io::Result<()> {
492 // outer-box is consistent
493 try!(self.cbox(indent_unit));
494 // head-box is inconsistent
495 try!(self.ibox(w.len() + 1));
496 // keyword that starts the head
498 try!(self.word_nbsp(w));
503 pub fn bopen(&mut self) -> io::Result<()> {
504 try!(word(&mut self.s, "{"));
505 self.end() // close the head-box
508 pub fn bclose_(&mut self, span: codemap::Span,
509 indented: usize) -> io::Result<()> {
510 self.bclose_maybe_open(span, indented, true)
512 pub fn bclose_maybe_open (&mut self, span: codemap::Span,
513 indented: usize, close_box: bool) -> io::Result<()> {
514 try!(self.maybe_print_comment(span.hi));
515 try!(self.break_offset_if_not_bol(1, -(indented as isize)));
516 try!(word(&mut self.s, "}"));
518 try!(self.end()); // close the outer-box
522 pub fn bclose(&mut self, span: codemap::Span) -> io::Result<()> {
523 self.bclose_(span, indent_unit)
526 pub fn is_begin(&mut self) -> bool {
527 match self.s.last_token() {
528 pp::Token::Begin(_) => true,
533 pub fn is_end(&mut self) -> bool {
534 match self.s.last_token() {
535 pp::Token::End => true,
540 // is this the beginning of a line?
541 pub fn is_bol(&mut self) -> bool {
542 self.s.last_token().is_eof() || self.s.last_token().is_hardbreak_tok()
545 pub fn in_cbox(&self) -> bool {
546 match self.boxes.last() {
547 Some(&last_box) => last_box == pp::Breaks::Consistent,
552 pub fn hardbreak_if_not_bol(&mut self) -> io::Result<()> {
554 try!(hardbreak(&mut self.s))
558 pub fn space_if_not_bol(&mut self) -> io::Result<()> {
559 if !self.is_bol() { try!(space(&mut self.s)); }
562 pub fn break_offset_if_not_bol(&mut self, n: usize,
563 off: isize) -> io::Result<()> {
565 break_offset(&mut self.s, n, off)
567 if off != 0 && self.s.last_token().is_hardbreak_tok() {
568 // We do something pretty sketchy here: tuck the nonzero
569 // offset-adjustment we were going to deposit along with the
570 // break into the previous hardbreak.
571 self.s.replace_last_token(pp::hardbreak_tok_offset(off));
577 // Synthesizes a comment that was not textually present in the original source
579 pub fn synth_comment(&mut self, text: String) -> io::Result<()> {
580 try!(word(&mut self.s, "/*"));
581 try!(space(&mut self.s));
582 try!(word(&mut self.s, &text[..]));
583 try!(space(&mut self.s));
584 word(&mut self.s, "*/")
587 pub fn commasep<T, F>(&mut self, b: Breaks, elts: &[T], mut op: F) -> io::Result<()> where
588 F: FnMut(&mut State, &T) -> io::Result<()>,
590 try!(self.rbox(0, b));
591 let mut first = true;
593 if first { first = false; } else { try!(self.word_space(",")); }
600 pub fn commasep_cmnt<T, F, G>(&mut self,
604 mut get_span: G) -> io::Result<()> where
605 F: FnMut(&mut State, &T) -> io::Result<()>,
606 G: FnMut(&T) -> codemap::Span,
608 try!(self.rbox(0, b));
609 let len = elts.len();
612 try!(self.maybe_print_comment(get_span(elt).hi));
616 try!(word(&mut self.s, ","));
617 try!(self.maybe_print_trailing_comment(get_span(elt),
618 Some(get_span(&elts[i]).hi)));
619 try!(self.space_if_not_bol());
625 pub fn commasep_exprs(&mut self, b: Breaks,
626 exprs: &[P<ast::Expr>]) -> io::Result<()> {
627 self.commasep_cmnt(b, exprs, |s, e| s.print_expr(&**e), |e| e.span)
630 pub fn print_mod(&mut self, _mod: &ast::Mod,
631 attrs: &[ast::Attribute]) -> io::Result<()> {
632 try!(self.print_inner_attributes(attrs));
633 for item in &_mod.items {
634 try!(self.print_item(&**item));
639 pub fn print_foreign_mod(&mut self, nmod: &ast::ForeignMod,
640 attrs: &[ast::Attribute]) -> io::Result<()> {
641 try!(self.print_inner_attributes(attrs));
642 for item in &nmod.items {
643 try!(self.print_foreign_item(&**item));
648 pub fn print_opt_lifetime(&mut self,
649 lifetime: &Option<ast::Lifetime>) -> io::Result<()> {
650 if let Some(l) = *lifetime {
651 try!(self.print_lifetime(&l));
657 pub fn print_type(&mut self, ty: &ast::Ty) -> io::Result<()> {
658 try!(self.maybe_print_comment(ty.span.lo));
661 ast::TyVec(ref ty) => {
662 try!(word(&mut self.s, "["));
663 try!(self.print_type(&**ty));
664 try!(word(&mut self.s, "]"));
666 ast::TyPtr(ref mt) => {
667 try!(word(&mut self.s, "*"));
669 ast::MutMutable => try!(self.word_nbsp("mut")),
670 ast::MutImmutable => try!(self.word_nbsp("const")),
672 try!(self.print_type(&*mt.ty));
674 ast::TyRptr(ref lifetime, ref mt) => {
675 try!(word(&mut self.s, "&"));
676 try!(self.print_opt_lifetime(lifetime));
677 try!(self.print_mt(mt));
679 ast::TyTup(ref elts) => {
681 try!(self.commasep(Inconsistent, &elts[..],
682 |s, ty| s.print_type(&**ty)));
684 try!(word(&mut self.s, ","));
688 ast::TyParen(ref typ) => {
690 try!(self.print_type(&**typ));
693 ast::TyBareFn(ref f) => {
694 let generics = ast::Generics {
695 lifetimes: f.lifetimes.clone(),
696 ty_params: OwnedSlice::empty(),
697 where_clause: ast::WhereClause {
698 id: ast::DUMMY_NODE_ID,
699 predicates: Vec::new(),
702 try!(self.print_ty_fn(f.abi,
709 ast::TyPath(None, ref path) => {
710 try!(self.print_path(path, false, 0));
712 ast::TyPath(Some(ref qself), ref path) => {
713 try!(self.print_qpath(path, qself, false))
715 ast::TyObjectSum(ref ty, ref bounds) => {
716 try!(self.print_type(&**ty));
717 try!(self.print_bounds("+", &bounds[..]));
719 ast::TyPolyTraitRef(ref bounds) => {
720 try!(self.print_bounds("", &bounds[..]));
722 ast::TyFixedLengthVec(ref ty, ref v) => {
723 try!(word(&mut self.s, "["));
724 try!(self.print_type(&**ty));
725 try!(word(&mut self.s, "; "));
726 try!(self.print_expr(&**v));
727 try!(word(&mut self.s, "]"));
729 ast::TyTypeof(ref e) => {
730 try!(word(&mut self.s, "typeof("));
731 try!(self.print_expr(&**e));
732 try!(word(&mut self.s, ")"));
735 try!(word(&mut self.s, "_"));
737 ast::TyMac(ref m) => {
738 try!(self.print_mac(m, token::Paren));
744 pub fn print_foreign_item(&mut self,
745 item: &ast::ForeignItem) -> io::Result<()> {
746 try!(self.hardbreak_if_not_bol());
747 try!(self.maybe_print_comment(item.span.lo));
748 try!(self.print_outer_attributes(&item.attrs));
750 ast::ForeignItemFn(ref decl, ref generics) => {
752 try!(self.print_fn(decl, ast::Unsafety::Normal,
753 ast::Constness::NotConst,
754 abi::Rust, Some(item.ident),
755 generics, None, item.vis));
756 try!(self.end()); // end head-ibox
757 try!(word(&mut self.s, ";"));
758 self.end() // end the outer fn box
760 ast::ForeignItemStatic(ref t, m) => {
761 try!(self.head(&visibility_qualified(item.vis,
764 try!(self.word_space("mut"));
766 try!(self.print_ident(item.ident));
767 try!(self.word_space(":"));
768 try!(self.print_type(&**t));
769 try!(word(&mut self.s, ";"));
770 try!(self.end()); // end the head-ibox
771 self.end() // end the outer cbox
776 fn print_associated_const(&mut self,
779 default: Option<&ast::Expr>,
780 vis: ast::Visibility)
783 try!(word(&mut self.s, &visibility_qualified(vis, "")));
784 try!(self.word_space("const"));
785 try!(self.print_ident(ident));
786 try!(self.word_space(":"));
787 try!(self.print_type(ty));
788 if let Some(expr) = default {
789 try!(space(&mut self.s));
790 try!(self.word_space("="));
791 try!(self.print_expr(expr));
793 word(&mut self.s, ";")
796 fn print_associated_type(&mut self,
798 bounds: Option<&ast::TyParamBounds>,
799 ty: Option<&ast::Ty>)
801 try!(self.word_space("type"));
802 try!(self.print_ident(ident));
803 if let Some(bounds) = bounds {
804 try!(self.print_bounds(":", bounds));
806 if let Some(ty) = ty {
807 try!(space(&mut self.s));
808 try!(self.word_space("="));
809 try!(self.print_type(ty));
811 word(&mut self.s, ";")
814 /// Pretty-print an item
815 pub fn print_item(&mut self, item: &ast::Item) -> io::Result<()> {
816 try!(self.hardbreak_if_not_bol());
817 try!(self.maybe_print_comment(item.span.lo));
818 try!(self.print_outer_attributes(&item.attrs));
819 try!(self.ann.pre(self, NodeItem(item)));
821 ast::ItemExternCrate(ref optional_path) => {
822 try!(self.head(&visibility_qualified(item.vis,
824 if let Some(p) = *optional_path {
825 let val = p.as_str();
826 if val.contains("-") {
827 try!(self.print_string(&val, ast::CookedStr));
829 try!(self.print_name(p));
831 try!(space(&mut self.s));
832 try!(word(&mut self.s, "as"));
833 try!(space(&mut self.s));
835 try!(self.print_ident(item.ident));
836 try!(word(&mut self.s, ";"));
837 try!(self.end()); // end inner head-block
838 try!(self.end()); // end outer head-block
840 ast::ItemUse(ref vp) => {
841 try!(self.head(&visibility_qualified(item.vis,
843 try!(self.print_view_path(&**vp));
844 try!(word(&mut self.s, ";"));
845 try!(self.end()); // end inner head-block
846 try!(self.end()); // end outer head-block
848 ast::ItemStatic(ref ty, m, ref expr) => {
849 try!(self.head(&visibility_qualified(item.vis,
851 if m == ast::MutMutable {
852 try!(self.word_space("mut"));
854 try!(self.print_ident(item.ident));
855 try!(self.word_space(":"));
856 try!(self.print_type(&**ty));
857 try!(space(&mut self.s));
858 try!(self.end()); // end the head-ibox
860 try!(self.word_space("="));
861 try!(self.print_expr(&**expr));
862 try!(word(&mut self.s, ";"));
863 try!(self.end()); // end the outer cbox
865 ast::ItemConst(ref ty, ref expr) => {
866 try!(self.head(&visibility_qualified(item.vis,
868 try!(self.print_ident(item.ident));
869 try!(self.word_space(":"));
870 try!(self.print_type(&**ty));
871 try!(space(&mut self.s));
872 try!(self.end()); // end the head-ibox
874 try!(self.word_space("="));
875 try!(self.print_expr(&**expr));
876 try!(word(&mut self.s, ";"));
877 try!(self.end()); // end the outer cbox
879 ast::ItemFn(ref decl, unsafety, constness, abi, ref typarams, ref body) => {
891 try!(word(&mut self.s, " "));
892 try!(self.print_block_with_attrs(&**body, &item.attrs));
894 ast::ItemMod(ref _mod) => {
895 try!(self.head(&visibility_qualified(item.vis,
897 try!(self.print_ident(item.ident));
900 try!(self.print_mod(_mod, &item.attrs));
901 try!(self.bclose(item.span));
903 ast::ItemForeignMod(ref nmod) => {
904 try!(self.head("extern"));
905 try!(self.word_nbsp(&nmod.abi.to_string()));
907 try!(self.print_foreign_mod(nmod, &item.attrs));
908 try!(self.bclose(item.span));
910 ast::ItemTy(ref ty, ref params) => {
911 try!(self.ibox(indent_unit));
913 try!(self.word_nbsp(&visibility_qualified(item.vis, "type")));
914 try!(self.print_ident(item.ident));
915 try!(self.print_generics(params));
916 try!(self.end()); // end the inner ibox
918 try!(self.print_where_clause(¶ms.where_clause));
919 try!(space(&mut self.s));
920 try!(self.word_space("="));
921 try!(self.print_type(&**ty));
922 try!(word(&mut self.s, ";"));
923 try!(self.end()); // end the outer ibox
925 ast::ItemEnum(ref enum_definition, ref params) => {
926 try!(self.print_enum_def(
934 ast::ItemStruct(ref struct_def, ref generics) => {
935 try!(self.head(&visibility_qualified(item.vis,"struct")));
936 try!(self.print_struct(&**struct_def, generics, item.ident, item.span));
939 ast::ItemDefaultImpl(unsafety, ref trait_ref) => {
941 try!(self.print_visibility(item.vis));
942 try!(self.print_unsafety(unsafety));
943 try!(self.word_nbsp("impl"));
944 try!(self.print_trait_ref(trait_ref));
945 try!(space(&mut self.s));
946 try!(self.word_space("for"));
947 try!(self.word_space(".."));
949 try!(self.bclose(item.span));
951 ast::ItemImpl(unsafety,
958 try!(self.print_visibility(item.vis));
959 try!(self.print_unsafety(unsafety));
960 try!(self.word_nbsp("impl"));
962 if generics.is_parameterized() {
963 try!(self.print_generics(generics));
964 try!(space(&mut self.s));
968 ast::ImplPolarity::Negative => {
969 try!(word(&mut self.s, "!"));
976 try!(self.print_trait_ref(t));
977 try!(space(&mut self.s));
978 try!(self.word_space("for"));
983 try!(self.print_type(&**ty));
984 try!(self.print_where_clause(&generics.where_clause));
986 try!(space(&mut self.s));
988 try!(self.print_inner_attributes(&item.attrs));
989 for impl_item in impl_items {
990 try!(self.print_impl_item(impl_item));
992 try!(self.bclose(item.span));
994 ast::ItemTrait(unsafety, ref generics, ref bounds, ref trait_items) => {
996 try!(self.print_visibility(item.vis));
997 try!(self.print_unsafety(unsafety));
998 try!(self.word_nbsp("trait"));
999 try!(self.print_ident(item.ident));
1000 try!(self.print_generics(generics));
1001 let mut real_bounds = Vec::with_capacity(bounds.len());
1002 for b in bounds.iter() {
1003 if let TraitTyParamBound(ref ptr, ast::TraitBoundModifier::Maybe) = *b {
1004 try!(space(&mut self.s));
1005 try!(self.word_space("for ?"));
1006 try!(self.print_trait_ref(&ptr.trait_ref));
1008 real_bounds.push(b.clone());
1011 try!(self.print_bounds(":", &real_bounds[..]));
1012 try!(self.print_where_clause(&generics.where_clause));
1013 try!(word(&mut self.s, " "));
1015 for trait_item in trait_items {
1016 try!(self.print_trait_item(trait_item));
1018 try!(self.bclose(item.span));
1020 // I think it's reasonable to hide the context here:
1021 ast::ItemMac(codemap::Spanned { node: ast::MacInvocTT(ref pth, ref tts, _),
1023 try!(self.print_visibility(item.vis));
1024 try!(self.print_path(pth, false, 0));
1025 try!(word(&mut self.s, "! "));
1026 try!(self.print_ident(item.ident));
1027 try!(self.cbox(indent_unit));
1029 try!(self.print_tts(&tts[..]));
1030 try!(self.pclose());
1031 try!(word(&mut self.s, ";"));
1035 self.ann.post(self, NodeItem(item))
1038 fn print_trait_ref(&mut self, t: &ast::TraitRef) -> io::Result<()> {
1039 self.print_path(&t.path, false, 0)
1042 fn print_formal_lifetime_list(&mut self, lifetimes: &[ast::LifetimeDef]) -> io::Result<()> {
1043 if !lifetimes.is_empty() {
1044 try!(word(&mut self.s, "for<"));
1045 let mut comma = false;
1046 for lifetime_def in lifetimes {
1048 try!(self.word_space(","))
1050 try!(self.print_lifetime_def(lifetime_def));
1053 try!(word(&mut self.s, ">"));
1058 fn print_poly_trait_ref(&mut self, t: &ast::PolyTraitRef) -> io::Result<()> {
1059 try!(self.print_formal_lifetime_list(&t.bound_lifetimes));
1060 self.print_trait_ref(&t.trait_ref)
1063 pub fn print_enum_def(&mut self, enum_definition: &ast::EnumDef,
1064 generics: &ast::Generics, ident: ast::Ident,
1065 span: codemap::Span,
1066 visibility: ast::Visibility) -> io::Result<()> {
1067 try!(self.head(&visibility_qualified(visibility, "enum")));
1068 try!(self.print_ident(ident));
1069 try!(self.print_generics(generics));
1070 try!(self.print_where_clause(&generics.where_clause));
1071 try!(space(&mut self.s));
1072 self.print_variants(&enum_definition.variants, span)
1075 pub fn print_variants(&mut self,
1076 variants: &[P<ast::Variant>],
1077 span: codemap::Span) -> io::Result<()> {
1080 try!(self.space_if_not_bol());
1081 try!(self.maybe_print_comment(v.span.lo));
1082 try!(self.print_outer_attributes(&v.node.attrs));
1083 try!(self.ibox(indent_unit));
1084 try!(self.print_variant(&**v));
1085 try!(word(&mut self.s, ","));
1087 try!(self.maybe_print_trailing_comment(v.span, None));
1092 pub fn print_visibility(&mut self, vis: ast::Visibility) -> io::Result<()> {
1094 ast::Public => self.word_nbsp("pub"),
1095 ast::Inherited => Ok(())
1099 pub fn print_struct(&mut self,
1100 struct_def: &ast::StructDef,
1101 generics: &ast::Generics,
1103 span: codemap::Span) -> io::Result<()> {
1104 try!(self.print_ident(ident));
1105 try!(self.print_generics(generics));
1106 if ast_util::struct_def_is_tuple_like(struct_def) {
1107 if !struct_def.fields.is_empty() {
1110 Inconsistent, &struct_def.fields,
1112 match field.node.kind {
1113 ast::NamedField(..) => panic!("unexpected named field"),
1114 ast::UnnamedField(vis) => {
1115 try!(s.print_visibility(vis));
1116 try!(s.maybe_print_comment(field.span.lo));
1117 s.print_type(&*field.node.ty)
1122 try!(self.pclose());
1124 try!(self.print_where_clause(&generics.where_clause));
1125 try!(word(&mut self.s, ";"));
1127 self.end() // close the outer-box
1129 try!(self.print_where_clause(&generics.where_clause));
1132 try!(self.hardbreak_if_not_bol());
1134 for field in &struct_def.fields {
1135 match field.node.kind {
1136 ast::UnnamedField(..) => panic!("unexpected unnamed field"),
1137 ast::NamedField(ident, visibility) => {
1138 try!(self.hardbreak_if_not_bol());
1139 try!(self.maybe_print_comment(field.span.lo));
1140 try!(self.print_outer_attributes(&field.node.attrs));
1141 try!(self.print_visibility(visibility));
1142 try!(self.print_ident(ident));
1143 try!(self.word_nbsp(":"));
1144 try!(self.print_type(&*field.node.ty));
1145 try!(word(&mut self.s, ","));
1154 /// This doesn't deserve to be called "pretty" printing, but it should be
1155 /// meaning-preserving. A quick hack that might help would be to look at the
1156 /// spans embedded in the TTs to decide where to put spaces and newlines.
1157 /// But it'd be better to parse these according to the grammar of the
1158 /// appropriate macro, transcribe back into the grammar we just parsed from,
1159 /// and then pretty-print the resulting AST nodes (so, e.g., we print
1160 /// expression arguments as expressions). It can be done! I think.
1161 pub fn print_tt(&mut self, tt: &ast::TokenTree) -> io::Result<()> {
1163 ast::TtToken(_, ref tk) => {
1164 try!(word(&mut self.s, &token_to_string(tk)));
1166 parse::token::DocComment(..) => {
1167 hardbreak(&mut self.s)
1172 ast::TtDelimited(_, ref delimed) => {
1173 try!(word(&mut self.s, &token_to_string(&delimed.open_token())));
1174 try!(space(&mut self.s));
1175 try!(self.print_tts(&delimed.tts));
1176 try!(space(&mut self.s));
1177 word(&mut self.s, &token_to_string(&delimed.close_token()))
1179 ast::TtSequence(_, ref seq) => {
1180 try!(word(&mut self.s, "$("));
1181 for tt_elt in &seq.tts {
1182 try!(self.print_tt(tt_elt));
1184 try!(word(&mut self.s, ")"));
1185 match seq.separator {
1187 try!(word(&mut self.s, &token_to_string(tk)));
1192 ast::ZeroOrMore => word(&mut self.s, "*"),
1193 ast::OneOrMore => word(&mut self.s, "+"),
1199 pub fn print_tts(&mut self, tts: &[ast::TokenTree]) -> io::Result<()> {
1201 let mut suppress_space = false;
1202 for (i, tt) in tts.iter().enumerate() {
1203 if i != 0 && !suppress_space {
1204 try!(space(&mut self.s));
1206 try!(self.print_tt(tt));
1207 // There should be no space between the module name and the following `::` in paths,
1208 // otherwise imported macros get re-parsed from crate metadata incorrectly (#20701)
1209 suppress_space = match tt {
1210 &ast::TtToken(_, token::Ident(_, token::ModName)) |
1211 &ast::TtToken(_, token::MatchNt(_, _, _, token::ModName)) |
1212 &ast::TtToken(_, token::SubstNt(_, token::ModName)) => true,
1219 pub fn print_variant(&mut self, v: &ast::Variant) -> io::Result<()> {
1220 try!(self.print_visibility(v.node.vis));
1222 ast::TupleVariantKind(ref args) => {
1223 try!(self.print_ident(v.node.name));
1224 if !args.is_empty() {
1226 try!(self.commasep(Consistent,
1228 |s, arg| s.print_type(&*arg.ty)));
1229 try!(self.pclose());
1232 ast::StructVariantKind(ref struct_def) => {
1233 try!(self.head(""));
1234 let generics = ast_util::empty_generics();
1235 try!(self.print_struct(&**struct_def, &generics, v.node.name, v.span));
1238 match v.node.disr_expr {
1240 try!(space(&mut self.s));
1241 try!(self.word_space("="));
1242 self.print_expr(&**d)
1248 pub fn print_method_sig(&mut self,
1251 vis: ast::Visibility)
1253 self.print_fn(&m.decl,
1259 Some(&m.explicit_self.node),
1263 pub fn print_trait_item(&mut self, ti: &ast::TraitItem)
1265 try!(self.ann.pre(self, NodeSubItem(ti.id)));
1266 try!(self.hardbreak_if_not_bol());
1267 try!(self.maybe_print_comment(ti.span.lo));
1268 try!(self.print_outer_attributes(&ti.attrs));
1270 ast::ConstTraitItem(ref ty, ref default) => {
1271 try!(self.print_associated_const(ti.ident, &ty,
1272 default.as_ref().map(|expr| &**expr),
1275 ast::MethodTraitItem(ref sig, ref body) => {
1277 try!(self.head(""));
1279 try!(self.print_method_sig(ti.ident, sig, ast::Inherited));
1280 if let Some(ref body) = *body {
1282 try!(self.print_block_with_attrs(body, &ti.attrs));
1284 try!(word(&mut self.s, ";"));
1287 ast::TypeTraitItem(ref bounds, ref default) => {
1288 try!(self.print_associated_type(ti.ident, Some(bounds),
1289 default.as_ref().map(|ty| &**ty)));
1292 self.ann.post(self, NodeSubItem(ti.id))
1295 pub fn print_impl_item(&mut self, ii: &ast::ImplItem) -> io::Result<()> {
1296 try!(self.ann.pre(self, NodeSubItem(ii.id)));
1297 try!(self.hardbreak_if_not_bol());
1298 try!(self.maybe_print_comment(ii.span.lo));
1299 try!(self.print_outer_attributes(&ii.attrs));
1301 ast::ConstImplItem(ref ty, ref expr) => {
1302 try!(self.print_associated_const(ii.ident, &ty, Some(&expr), ii.vis));
1304 ast::MethodImplItem(ref sig, ref body) => {
1305 try!(self.head(""));
1306 try!(self.print_method_sig(ii.ident, sig, ii.vis));
1308 try!(self.print_block_with_attrs(body, &ii.attrs));
1310 ast::TypeImplItem(ref ty) => {
1311 try!(self.print_associated_type(ii.ident, None, Some(ty)));
1313 ast::MacImplItem(codemap::Spanned { node: ast::MacInvocTT(ref pth, ref tts, _),
1315 // code copied from ItemMac:
1316 try!(self.print_path(pth, false, 0));
1317 try!(word(&mut self.s, "! "));
1318 try!(self.cbox(indent_unit));
1320 try!(self.print_tts(&tts[..]));
1321 try!(self.pclose());
1322 try!(word(&mut self.s, ";"));
1326 self.ann.post(self, NodeSubItem(ii.id))
1329 pub fn print_outer_attributes(&mut self,
1330 attrs: &[ast::Attribute]) -> io::Result<()> {
1333 match attr.node.style {
1335 try!(self.print_attribute(attr));
1338 _ => {/* fallthrough */ }
1342 try!(self.hardbreak_if_not_bol());
1347 pub fn print_inner_attributes(&mut self,
1348 attrs: &[ast::Attribute]) -> io::Result<()> {
1351 match attr.node.style {
1353 try!(self.print_attribute(attr));
1356 _ => {/* fallthrough */ }
1360 try!(self.hardbreak_if_not_bol());
1365 pub fn print_attribute(&mut self, attr: &ast::Attribute) -> io::Result<()> {
1366 try!(self.hardbreak_if_not_bol());
1367 try!(self.maybe_print_comment(attr.span.lo));
1368 if attr.node.is_sugared_doc {
1369 word(&mut self.s, &attr.value_str().unwrap())
1371 match attr.node.style {
1372 ast::AttrInner => try!(word(&mut self.s, "#![")),
1373 ast::AttrOuter => try!(word(&mut self.s, "#[")),
1375 try!(self.print_meta_item(&*attr.meta()));
1376 word(&mut self.s, "]")
1381 pub fn print_stmt(&mut self, st: &ast::Stmt) -> io::Result<()> {
1382 try!(self.maybe_print_comment(st.span.lo));
1384 ast::StmtDecl(ref decl, _) => {
1385 try!(self.print_decl(&**decl));
1387 ast::StmtExpr(ref expr, _) => {
1388 try!(self.space_if_not_bol());
1389 try!(self.print_expr(&**expr));
1391 ast::StmtSemi(ref expr, _) => {
1392 try!(self.space_if_not_bol());
1393 try!(self.print_expr(&**expr));
1394 try!(word(&mut self.s, ";"));
1396 ast::StmtMac(ref mac, style) => {
1397 try!(self.space_if_not_bol());
1398 let delim = match style {
1399 ast::MacStmtWithBraces => token::Brace,
1402 try!(self.print_mac(&**mac, delim));
1404 ast::MacStmtWithBraces => {}
1405 _ => try!(word(&mut self.s, ";")),
1409 if parse::classify::stmt_ends_with_semi(&st.node) {
1410 try!(word(&mut self.s, ";"));
1412 self.maybe_print_trailing_comment(st.span, None)
1415 pub fn print_block(&mut self, blk: &ast::Block) -> io::Result<()> {
1416 self.print_block_with_attrs(blk, &[])
1419 pub fn print_block_unclosed(&mut self, blk: &ast::Block) -> io::Result<()> {
1420 self.print_block_unclosed_indent(blk, indent_unit)
1423 pub fn print_block_unclosed_indent(&mut self, blk: &ast::Block,
1424 indented: usize) -> io::Result<()> {
1425 self.print_block_maybe_unclosed(blk, indented, &[], false)
1428 pub fn print_block_with_attrs(&mut self,
1430 attrs: &[ast::Attribute]) -> io::Result<()> {
1431 self.print_block_maybe_unclosed(blk, indent_unit, attrs, true)
1434 pub fn print_block_maybe_unclosed(&mut self,
1437 attrs: &[ast::Attribute],
1438 close_box: bool) -> io::Result<()> {
1440 ast::UnsafeBlock(..) | ast::PushUnsafeBlock(..) => try!(self.word_space("unsafe")),
1441 ast::DefaultBlock | ast::PopUnsafeBlock(..) => ()
1443 try!(self.maybe_print_comment(blk.span.lo));
1444 try!(self.ann.pre(self, NodeBlock(blk)));
1447 try!(self.print_inner_attributes(attrs));
1449 for st in &blk.stmts {
1450 try!(self.print_stmt(&**st));
1454 try!(self.space_if_not_bol());
1455 try!(self.print_expr(&**expr));
1456 try!(self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi)));
1460 try!(self.bclose_maybe_open(blk.span, indented, close_box));
1461 self.ann.post(self, NodeBlock(blk))
1464 fn print_else(&mut self, els: Option<&ast::Expr>) -> io::Result<()> {
1468 // "another else-if"
1469 ast::ExprIf(ref i, ref then, ref e) => {
1470 try!(self.cbox(indent_unit - 1));
1472 try!(word(&mut self.s, " else if "));
1473 try!(self.print_expr(&**i));
1474 try!(space(&mut self.s));
1475 try!(self.print_block(&**then));
1476 self.print_else(e.as_ref().map(|e| &**e))
1478 // "another else-if-let"
1479 ast::ExprIfLet(ref pat, ref expr, ref then, ref e) => {
1480 try!(self.cbox(indent_unit - 1));
1482 try!(word(&mut self.s, " else if let "));
1483 try!(self.print_pat(&**pat));
1484 try!(space(&mut self.s));
1485 try!(self.word_space("="));
1486 try!(self.print_expr(&**expr));
1487 try!(space(&mut self.s));
1488 try!(self.print_block(&**then));
1489 self.print_else(e.as_ref().map(|e| &**e))
1492 ast::ExprBlock(ref b) => {
1493 try!(self.cbox(indent_unit - 1));
1495 try!(word(&mut self.s, " else "));
1496 self.print_block(&**b)
1498 // BLEAH, constraints would be great here
1500 panic!("print_if saw if with weird alternative");
1508 pub fn print_if(&mut self, test: &ast::Expr, blk: &ast::Block,
1509 elseopt: Option<&ast::Expr>) -> io::Result<()> {
1510 try!(self.head("if"));
1511 try!(self.print_expr(test));
1512 try!(space(&mut self.s));
1513 try!(self.print_block(blk));
1514 self.print_else(elseopt)
1517 pub fn print_if_let(&mut self, pat: &ast::Pat, expr: &ast::Expr, blk: &ast::Block,
1518 elseopt: Option<&ast::Expr>) -> io::Result<()> {
1519 try!(self.head("if let"));
1520 try!(self.print_pat(pat));
1521 try!(space(&mut self.s));
1522 try!(self.word_space("="));
1523 try!(self.print_expr(expr));
1524 try!(space(&mut self.s));
1525 try!(self.print_block(blk));
1526 self.print_else(elseopt)
1529 pub fn print_mac(&mut self, m: &ast::Mac, delim: token::DelimToken)
1532 // I think it's reasonable to hide the ctxt here:
1533 ast::MacInvocTT(ref pth, ref tts, _) => {
1534 try!(self.print_path(pth, false, 0));
1535 try!(word(&mut self.s, "!"));
1537 token::Paren => try!(self.popen()),
1538 token::Bracket => try!(word(&mut self.s, "[")),
1539 token::Brace => try!(self.bopen()),
1541 try!(self.print_tts(tts));
1543 token::Paren => self.pclose(),
1544 token::Bracket => word(&mut self.s, "]"),
1545 token::Brace => self.bclose(m.span),
1552 fn print_call_post(&mut self, args: &[P<ast::Expr>]) -> io::Result<()> {
1554 try!(self.commasep_exprs(Inconsistent, args));
1558 pub fn print_expr_maybe_paren(&mut self, expr: &ast::Expr) -> io::Result<()> {
1559 let needs_par = needs_parentheses(expr);
1563 try!(self.print_expr(expr));
1565 try!(self.pclose());
1570 fn print_expr_box(&mut self,
1571 place: &Option<P<ast::Expr>>,
1572 expr: &ast::Expr) -> io::Result<()> {
1573 try!(word(&mut self.s, "box"));
1574 try!(word(&mut self.s, "("));
1575 try!(place.as_ref().map_or(Ok(()), |e|self.print_expr(&**e)));
1576 try!(self.word_space(")"));
1577 self.print_expr(expr)
1580 fn print_expr_vec(&mut self, exprs: &[P<ast::Expr>]) -> io::Result<()> {
1581 try!(self.ibox(indent_unit));
1582 try!(word(&mut self.s, "["));
1583 try!(self.commasep_exprs(Inconsistent, &exprs[..]));
1584 try!(word(&mut self.s, "]"));
1588 fn print_expr_repeat(&mut self,
1589 element: &ast::Expr,
1590 count: &ast::Expr) -> io::Result<()> {
1591 try!(self.ibox(indent_unit));
1592 try!(word(&mut self.s, "["));
1593 try!(self.print_expr(element));
1594 try!(self.word_space(";"));
1595 try!(self.print_expr(count));
1596 try!(word(&mut self.s, "]"));
1600 fn print_expr_struct(&mut self,
1602 fields: &[ast::Field],
1603 wth: &Option<P<ast::Expr>>) -> io::Result<()> {
1604 try!(self.print_path(path, true, 0));
1605 if !(fields.is_empty() && wth.is_none()) {
1606 try!(word(&mut self.s, "{"));
1607 try!(self.commasep_cmnt(
1611 try!(s.ibox(indent_unit));
1612 try!(s.print_ident(field.ident.node));
1613 try!(s.word_space(":"));
1614 try!(s.print_expr(&*field.expr));
1620 try!(self.ibox(indent_unit));
1621 if !fields.is_empty() {
1622 try!(word(&mut self.s, ","));
1623 try!(space(&mut self.s));
1625 try!(word(&mut self.s, ".."));
1626 try!(self.print_expr(&**expr));
1629 _ => try!(word(&mut self.s, ",")),
1631 try!(word(&mut self.s, "}"));
1636 fn print_expr_tup(&mut self, exprs: &[P<ast::Expr>]) -> io::Result<()> {
1638 try!(self.commasep_exprs(Inconsistent, &exprs[..]));
1639 if exprs.len() == 1 {
1640 try!(word(&mut self.s, ","));
1645 fn print_expr_call(&mut self,
1647 args: &[P<ast::Expr>]) -> io::Result<()> {
1648 try!(self.print_expr_maybe_paren(func));
1649 self.print_call_post(args)
1652 fn print_expr_method_call(&mut self,
1653 ident: ast::SpannedIdent,
1655 args: &[P<ast::Expr>]) -> io::Result<()> {
1656 let base_args = &args[1..];
1657 try!(self.print_expr(&*args[0]));
1658 try!(word(&mut self.s, "."));
1659 try!(self.print_ident(ident.node));
1660 if !tys.is_empty() {
1661 try!(word(&mut self.s, "::<"));
1662 try!(self.commasep(Inconsistent, tys,
1663 |s, ty| s.print_type(&**ty)));
1664 try!(word(&mut self.s, ">"));
1666 self.print_call_post(base_args)
1669 fn print_expr_binary(&mut self,
1672 rhs: &ast::Expr) -> io::Result<()> {
1673 try!(self.print_expr(lhs));
1674 try!(space(&mut self.s));
1675 try!(self.word_space(ast_util::binop_to_string(op.node)));
1676 self.print_expr(rhs)
1679 fn print_expr_unary(&mut self,
1681 expr: &ast::Expr) -> io::Result<()> {
1682 try!(word(&mut self.s, ast_util::unop_to_string(op)));
1683 self.print_expr_maybe_paren(expr)
1686 fn print_expr_addr_of(&mut self,
1687 mutability: ast::Mutability,
1688 expr: &ast::Expr) -> io::Result<()> {
1689 try!(word(&mut self.s, "&"));
1690 try!(self.print_mutability(mutability));
1691 self.print_expr_maybe_paren(expr)
1694 pub fn print_expr(&mut self, expr: &ast::Expr) -> io::Result<()> {
1695 try!(self.maybe_print_comment(expr.span.lo));
1696 try!(self.ibox(indent_unit));
1697 try!(self.ann.pre(self, NodeExpr(expr)));
1699 ast::ExprBox(ref place, ref expr) => {
1700 try!(self.print_expr_box(place, &**expr));
1702 ast::ExprVec(ref exprs) => {
1703 try!(self.print_expr_vec(&exprs[..]));
1705 ast::ExprRepeat(ref element, ref count) => {
1706 try!(self.print_expr_repeat(&**element, &**count));
1708 ast::ExprStruct(ref path, ref fields, ref wth) => {
1709 try!(self.print_expr_struct(path, &fields[..], wth));
1711 ast::ExprTup(ref exprs) => {
1712 try!(self.print_expr_tup(&exprs[..]));
1714 ast::ExprCall(ref func, ref args) => {
1715 try!(self.print_expr_call(&**func, &args[..]));
1717 ast::ExprMethodCall(ident, ref tys, ref args) => {
1718 try!(self.print_expr_method_call(ident, &tys[..], &args[..]));
1720 ast::ExprBinary(op, ref lhs, ref rhs) => {
1721 try!(self.print_expr_binary(op, &**lhs, &**rhs));
1723 ast::ExprUnary(op, ref expr) => {
1724 try!(self.print_expr_unary(op, &**expr));
1726 ast::ExprAddrOf(m, ref expr) => {
1727 try!(self.print_expr_addr_of(m, &**expr));
1729 ast::ExprLit(ref lit) => {
1730 try!(self.print_literal(&**lit));
1732 ast::ExprCast(ref expr, ref ty) => {
1733 try!(self.print_expr(&**expr));
1734 try!(space(&mut self.s));
1735 try!(self.word_space("as"));
1736 try!(self.print_type(&**ty));
1738 ast::ExprIf(ref test, ref blk, ref elseopt) => {
1739 try!(self.print_if(&**test, &**blk, elseopt.as_ref().map(|e| &**e)));
1741 ast::ExprIfLet(ref pat, ref expr, ref blk, ref elseopt) => {
1742 try!(self.print_if_let(&**pat, &**expr, &** blk, elseopt.as_ref().map(|e| &**e)));
1744 ast::ExprWhile(ref test, ref blk, opt_ident) => {
1745 if let Some(ident) = opt_ident {
1746 try!(self.print_ident(ident));
1747 try!(self.word_space(":"));
1749 try!(self.head("while"));
1750 try!(self.print_expr(&**test));
1751 try!(space(&mut self.s));
1752 try!(self.print_block(&**blk));
1754 ast::ExprWhileLet(ref pat, ref expr, ref blk, opt_ident) => {
1755 if let Some(ident) = opt_ident {
1756 try!(self.print_ident(ident));
1757 try!(self.word_space(":"));
1759 try!(self.head("while let"));
1760 try!(self.print_pat(&**pat));
1761 try!(space(&mut self.s));
1762 try!(self.word_space("="));
1763 try!(self.print_expr(&**expr));
1764 try!(space(&mut self.s));
1765 try!(self.print_block(&**blk));
1767 ast::ExprForLoop(ref pat, ref iter, ref blk, opt_ident) => {
1768 if let Some(ident) = opt_ident {
1769 try!(self.print_ident(ident));
1770 try!(self.word_space(":"));
1772 try!(self.head("for"));
1773 try!(self.print_pat(&**pat));
1774 try!(space(&mut self.s));
1775 try!(self.word_space("in"));
1776 try!(self.print_expr(&**iter));
1777 try!(space(&mut self.s));
1778 try!(self.print_block(&**blk));
1780 ast::ExprLoop(ref blk, opt_ident) => {
1781 if let Some(ident) = opt_ident {
1782 try!(self.print_ident(ident));
1783 try!(self.word_space(":"));
1785 try!(self.head("loop"));
1786 try!(space(&mut self.s));
1787 try!(self.print_block(&**blk));
1789 ast::ExprMatch(ref expr, ref arms, _) => {
1790 try!(self.cbox(indent_unit));
1792 try!(self.word_nbsp("match"));
1793 try!(self.print_expr(&**expr));
1794 try!(space(&mut self.s));
1797 try!(self.print_arm(arm));
1799 try!(self.bclose_(expr.span, indent_unit));
1801 ast::ExprClosure(capture_clause, ref decl, ref body) => {
1802 try!(self.print_capture_clause(capture_clause));
1804 try!(self.print_fn_block_args(&**decl));
1805 try!(space(&mut self.s));
1807 let default_return = match decl.output {
1808 ast::DefaultReturn(..) => true,
1812 if !default_return || !body.stmts.is_empty() || body.expr.is_none() {
1813 try!(self.print_block_unclosed(&**body));
1815 // we extract the block, so as not to create another set of boxes
1816 match body.expr.as_ref().unwrap().node {
1817 ast::ExprBlock(ref blk) => {
1818 try!(self.print_block_unclosed(&**blk));
1821 // this is a bare expression
1822 try!(self.print_expr(body.expr.as_ref().map(|e| &**e).unwrap()));
1823 try!(self.end()); // need to close a box
1827 // a box will be closed by print_expr, but we didn't want an overall
1828 // wrapper so we closed the corresponding opening. so create an
1829 // empty box to satisfy the close.
1832 ast::ExprBlock(ref blk) => {
1833 // containing cbox, will be closed by print-block at }
1834 try!(self.cbox(indent_unit));
1835 // head-box, will be closed by print-block after {
1837 try!(self.print_block(&**blk));
1839 ast::ExprAssign(ref lhs, ref rhs) => {
1840 try!(self.print_expr(&**lhs));
1841 try!(space(&mut self.s));
1842 try!(self.word_space("="));
1843 try!(self.print_expr(&**rhs));
1845 ast::ExprAssignOp(op, ref lhs, ref rhs) => {
1846 try!(self.print_expr(&**lhs));
1847 try!(space(&mut self.s));
1848 try!(word(&mut self.s, ast_util::binop_to_string(op.node)));
1849 try!(self.word_space("="));
1850 try!(self.print_expr(&**rhs));
1852 ast::ExprField(ref expr, id) => {
1853 try!(self.print_expr(&**expr));
1854 try!(word(&mut self.s, "."));
1855 try!(self.print_ident(id.node));
1857 ast::ExprTupField(ref expr, id) => {
1858 try!(self.print_expr(&**expr));
1859 try!(word(&mut self.s, "."));
1860 try!(self.print_usize(id.node));
1862 ast::ExprIndex(ref expr, ref index) => {
1863 try!(self.print_expr(&**expr));
1864 try!(word(&mut self.s, "["));
1865 try!(self.print_expr(&**index));
1866 try!(word(&mut self.s, "]"));
1868 ast::ExprRange(ref start, ref end) => {
1869 if let &Some(ref e) = start {
1870 try!(self.print_expr(&**e));
1872 try!(word(&mut self.s, ".."));
1873 if let &Some(ref e) = end {
1874 try!(self.print_expr(&**e));
1877 ast::ExprPath(None, ref path) => {
1878 try!(self.print_path(path, true, 0))
1880 ast::ExprPath(Some(ref qself), ref path) => {
1881 try!(self.print_qpath(path, qself, true))
1883 ast::ExprBreak(opt_ident) => {
1884 try!(word(&mut self.s, "break"));
1885 try!(space(&mut self.s));
1886 if let Some(ident) = opt_ident {
1887 try!(self.print_ident(ident));
1888 try!(space(&mut self.s));
1891 ast::ExprAgain(opt_ident) => {
1892 try!(word(&mut self.s, "continue"));
1893 try!(space(&mut self.s));
1894 if let Some(ident) = opt_ident {
1895 try!(self.print_ident(ident));
1896 try!(space(&mut self.s))
1899 ast::ExprRet(ref result) => {
1900 try!(word(&mut self.s, "return"));
1903 try!(word(&mut self.s, " "));
1904 try!(self.print_expr(&**expr));
1909 ast::ExprInlineAsm(ref a) => {
1910 try!(word(&mut self.s, "asm!"));
1912 try!(self.print_string(&a.asm, a.asm_str_style));
1913 try!(self.word_space(":"));
1915 try!(self.commasep(Inconsistent, &a.outputs,
1916 |s, &(ref co, ref o, is_rw)| {
1917 match co.slice_shift_char() {
1918 Some(('=', operand)) if is_rw => {
1919 try!(s.print_string(&format!("+{}", operand),
1922 _ => try!(s.print_string(&co, ast::CookedStr))
1925 try!(s.print_expr(&**o));
1929 try!(space(&mut self.s));
1930 try!(self.word_space(":"));
1932 try!(self.commasep(Inconsistent, &a.inputs,
1933 |s, &(ref co, ref o)| {
1934 try!(s.print_string(&co, ast::CookedStr));
1936 try!(s.print_expr(&**o));
1940 try!(space(&mut self.s));
1941 try!(self.word_space(":"));
1943 try!(self.commasep(Inconsistent, &a.clobbers,
1945 try!(s.print_string(&co, ast::CookedStr));
1949 let mut options = vec!();
1951 options.push("volatile");
1954 options.push("alignstack");
1956 if a.dialect == ast::AsmDialect::AsmIntel {
1957 options.push("intel");
1960 if !options.is_empty() {
1961 try!(space(&mut self.s));
1962 try!(self.word_space(":"));
1963 try!(self.commasep(Inconsistent, &*options,
1965 try!(s.print_string(co, ast::CookedStr));
1970 try!(self.pclose());
1972 ast::ExprMac(ref m) => try!(self.print_mac(m, token::Paren)),
1973 ast::ExprParen(ref e) => {
1975 try!(self.print_expr(&**e));
1976 try!(self.pclose());
1979 try!(self.ann.post(self, NodeExpr(expr)));
1983 pub fn print_local_decl(&mut self, loc: &ast::Local) -> io::Result<()> {
1984 try!(self.print_pat(&*loc.pat));
1985 if let Some(ref ty) = loc.ty {
1986 try!(self.word_space(":"));
1987 try!(self.print_type(&**ty));
1992 pub fn print_decl(&mut self, decl: &ast::Decl) -> io::Result<()> {
1993 try!(self.maybe_print_comment(decl.span.lo));
1995 ast::DeclLocal(ref loc) => {
1996 try!(self.space_if_not_bol());
1997 try!(self.ibox(indent_unit));
1998 try!(self.word_nbsp("let"));
2000 try!(self.ibox(indent_unit));
2001 try!(self.print_local_decl(&**loc));
2003 if let Some(ref init) = loc.init {
2005 try!(self.word_space("="));
2006 try!(self.print_expr(&**init));
2010 ast::DeclItem(ref item) => self.print_item(&**item)
2014 pub fn print_ident(&mut self, ident: ast::Ident) -> io::Result<()> {
2015 try!(word(&mut self.s, &ident.name.as_str()));
2016 self.ann.post(self, NodeIdent(&ident))
2019 pub fn print_usize(&mut self, i: usize) -> io::Result<()> {
2020 word(&mut self.s, &i.to_string())
2023 pub fn print_name(&mut self, name: ast::Name) -> io::Result<()> {
2024 try!(word(&mut self.s, &name.as_str()));
2025 self.ann.post(self, NodeName(&name))
2028 pub fn print_for_decl(&mut self, loc: &ast::Local,
2029 coll: &ast::Expr) -> io::Result<()> {
2030 try!(self.print_local_decl(loc));
2031 try!(space(&mut self.s));
2032 try!(self.word_space("in"));
2033 self.print_expr(coll)
2036 fn print_path(&mut self,
2038 colons_before_params: bool,
2042 try!(self.maybe_print_comment(path.span.lo));
2044 let mut first = !path.global;
2045 for segment in &path.segments[..path.segments.len()-depth] {
2049 try!(word(&mut self.s, "::"))
2052 try!(self.print_ident(segment.identifier));
2054 try!(self.print_path_parameters(&segment.parameters, colons_before_params));
2060 fn print_qpath(&mut self,
2063 colons_before_params: bool)
2066 try!(word(&mut self.s, "<"));
2067 try!(self.print_type(&qself.ty));
2068 if qself.position > 0 {
2069 try!(space(&mut self.s));
2070 try!(self.word_space("as"));
2071 let depth = path.segments.len() - qself.position;
2072 try!(self.print_path(&path, false, depth));
2074 try!(word(&mut self.s, ">"));
2075 try!(word(&mut self.s, "::"));
2076 let item_segment = path.segments.last().unwrap();
2077 try!(self.print_ident(item_segment.identifier));
2078 self.print_path_parameters(&item_segment.parameters, colons_before_params)
2081 fn print_path_parameters(&mut self,
2082 parameters: &ast::PathParameters,
2083 colons_before_params: bool)
2086 if parameters.is_empty() {
2090 if colons_before_params {
2091 try!(word(&mut self.s, "::"))
2095 ast::AngleBracketedParameters(ref data) => {
2096 try!(word(&mut self.s, "<"));
2098 let mut comma = false;
2099 for lifetime in &data.lifetimes {
2101 try!(self.word_space(","))
2103 try!(self.print_lifetime(lifetime));
2107 if !data.types.is_empty() {
2109 try!(self.word_space(","))
2114 |s, ty| s.print_type(&**ty)));
2118 for binding in data.bindings.iter() {
2120 try!(self.word_space(","))
2122 try!(self.print_ident(binding.ident));
2123 try!(space(&mut self.s));
2124 try!(self.word_space("="));
2125 try!(self.print_type(&*binding.ty));
2129 try!(word(&mut self.s, ">"))
2132 ast::ParenthesizedParameters(ref data) => {
2133 try!(word(&mut self.s, "("));
2137 |s, ty| s.print_type(&**ty)));
2138 try!(word(&mut self.s, ")"));
2143 try!(self.space_if_not_bol());
2144 try!(self.word_space("->"));
2145 try!(self.print_type(&**ty));
2154 pub fn print_pat(&mut self, pat: &ast::Pat) -> io::Result<()> {
2155 try!(self.maybe_print_comment(pat.span.lo));
2156 try!(self.ann.pre(self, NodePat(pat)));
2157 /* Pat isn't normalized, but the beauty of it
2158 is that it doesn't matter */
2160 ast::PatWild(ast::PatWildSingle) => try!(word(&mut self.s, "_")),
2161 ast::PatWild(ast::PatWildMulti) => try!(word(&mut self.s, "..")),
2162 ast::PatIdent(binding_mode, ref path1, ref sub) => {
2163 match binding_mode {
2164 ast::BindByRef(mutbl) => {
2165 try!(self.word_nbsp("ref"));
2166 try!(self.print_mutability(mutbl));
2168 ast::BindByValue(ast::MutImmutable) => {}
2169 ast::BindByValue(ast::MutMutable) => {
2170 try!(self.word_nbsp("mut"));
2173 try!(self.print_ident(path1.node));
2176 try!(word(&mut self.s, "@"));
2177 try!(self.print_pat(&**p));
2182 ast::PatEnum(ref path, ref args_) => {
2183 try!(self.print_path(path, true, 0));
2185 None => try!(word(&mut self.s, "(..)")),
2187 if !args.is_empty() {
2189 try!(self.commasep(Inconsistent, &args[..],
2190 |s, p| s.print_pat(&**p)));
2191 try!(self.pclose());
2196 ast::PatQPath(ref qself, ref path) => {
2197 try!(self.print_qpath(path, qself, false));
2199 ast::PatStruct(ref path, ref fields, etc) => {
2200 try!(self.print_path(path, true, 0));
2202 try!(self.word_space("{"));
2203 try!(self.commasep_cmnt(
2204 Consistent, &fields[..],
2206 try!(s.cbox(indent_unit));
2207 if !f.node.is_shorthand {
2208 try!(s.print_ident(f.node.ident));
2209 try!(s.word_nbsp(":"));
2211 try!(s.print_pat(&*f.node.pat));
2214 |f| f.node.pat.span));
2216 if !fields.is_empty() { try!(self.word_space(",")); }
2217 try!(word(&mut self.s, ".."));
2219 try!(space(&mut self.s));
2220 try!(word(&mut self.s, "}"));
2222 ast::PatTup(ref elts) => {
2224 try!(self.commasep(Inconsistent,
2226 |s, p| s.print_pat(&**p)));
2227 if elts.len() == 1 {
2228 try!(word(&mut self.s, ","));
2230 try!(self.pclose());
2232 ast::PatBox(ref inner) => {
2233 try!(word(&mut self.s, "box "));
2234 try!(self.print_pat(&**inner));
2236 ast::PatRegion(ref inner, mutbl) => {
2237 try!(word(&mut self.s, "&"));
2238 if mutbl == ast::MutMutable {
2239 try!(word(&mut self.s, "mut "));
2241 try!(self.print_pat(&**inner));
2243 ast::PatLit(ref e) => try!(self.print_expr(&**e)),
2244 ast::PatRange(ref begin, ref end) => {
2245 try!(self.print_expr(&**begin));
2246 try!(space(&mut self.s));
2247 try!(word(&mut self.s, "..."));
2248 try!(self.print_expr(&**end));
2250 ast::PatVec(ref before, ref slice, ref after) => {
2251 try!(word(&mut self.s, "["));
2252 try!(self.commasep(Inconsistent,
2254 |s, p| s.print_pat(&**p)));
2255 if let Some(ref p) = *slice {
2256 if !before.is_empty() { try!(self.word_space(",")); }
2257 try!(self.print_pat(&**p));
2259 ast::Pat { node: ast::PatWild(ast::PatWildMulti), .. } => {
2260 // this case is handled by print_pat
2262 _ => try!(word(&mut self.s, "..")),
2264 if !after.is_empty() { try!(self.word_space(",")); }
2266 try!(self.commasep(Inconsistent,
2268 |s, p| s.print_pat(&**p)));
2269 try!(word(&mut self.s, "]"));
2271 ast::PatMac(ref m) => try!(self.print_mac(m, token::Paren)),
2273 self.ann.post(self, NodePat(pat))
2276 fn print_arm(&mut self, arm: &ast::Arm) -> io::Result<()> {
2277 // I have no idea why this check is necessary, but here it
2279 if arm.attrs.is_empty() {
2280 try!(space(&mut self.s));
2282 try!(self.cbox(indent_unit));
2284 try!(self.print_outer_attributes(&arm.attrs));
2285 let mut first = true;
2286 for p in &arm.pats {
2290 try!(space(&mut self.s));
2291 try!(self.word_space("|"));
2293 try!(self.print_pat(&**p));
2295 try!(space(&mut self.s));
2296 if let Some(ref e) = arm.guard {
2297 try!(self.word_space("if"));
2298 try!(self.print_expr(&**e));
2299 try!(space(&mut self.s));
2301 try!(self.word_space("=>"));
2303 match arm.body.node {
2304 ast::ExprBlock(ref blk) => {
2305 // the block will close the pattern's ibox
2306 try!(self.print_block_unclosed_indent(&**blk, indent_unit));
2308 // If it is a user-provided unsafe block, print a comma after it
2309 if let ast::UnsafeBlock(ast::UserProvided) = blk.rules {
2310 try!(word(&mut self.s, ","));
2314 try!(self.end()); // close the ibox for the pattern
2315 try!(self.print_expr(&*arm.body));
2316 try!(word(&mut self.s, ","));
2319 self.end() // close enclosing cbox
2322 // Returns whether it printed anything
2323 fn print_explicit_self(&mut self,
2324 explicit_self: &ast::ExplicitSelf_,
2325 mutbl: ast::Mutability) -> io::Result<bool> {
2326 try!(self.print_mutability(mutbl));
2327 match *explicit_self {
2328 ast::SelfStatic => { return Ok(false); }
2329 ast::SelfValue(_) => {
2330 try!(word(&mut self.s, "self"));
2332 ast::SelfRegion(ref lt, m, _) => {
2333 try!(word(&mut self.s, "&"));
2334 try!(self.print_opt_lifetime(lt));
2335 try!(self.print_mutability(m));
2336 try!(word(&mut self.s, "self"));
2338 ast::SelfExplicit(ref typ, _) => {
2339 try!(word(&mut self.s, "self"));
2340 try!(self.word_space(":"));
2341 try!(self.print_type(&**typ));
2347 pub fn print_fn(&mut self,
2349 unsafety: ast::Unsafety,
2350 constness: ast::Constness,
2352 name: Option<ast::Ident>,
2353 generics: &ast::Generics,
2354 opt_explicit_self: Option<&ast::ExplicitSelf_>,
2355 vis: ast::Visibility) -> io::Result<()> {
2356 try!(self.print_fn_header_info(unsafety, constness, abi, vis));
2358 if let Some(name) = name {
2360 try!(self.print_ident(name));
2362 try!(self.print_generics(generics));
2363 try!(self.print_fn_args_and_ret(decl, opt_explicit_self));
2364 self.print_where_clause(&generics.where_clause)
2367 pub fn print_fn_args(&mut self, decl: &ast::FnDecl,
2368 opt_explicit_self: Option<&ast::ExplicitSelf_>)
2370 // It is unfortunate to duplicate the commasep logic, but we want the
2371 // self type and the args all in the same box.
2372 try!(self.rbox(0, Inconsistent));
2373 let mut first = true;
2374 if let Some(explicit_self) = opt_explicit_self {
2375 let m = match explicit_self {
2376 &ast::SelfStatic => ast::MutImmutable,
2377 _ => match decl.inputs[0].pat.node {
2378 ast::PatIdent(ast::BindByValue(m), _, _) => m,
2379 _ => ast::MutImmutable
2382 first = !try!(self.print_explicit_self(explicit_self, m));
2385 // HACK(eddyb) ignore the separately printed self argument.
2386 let args = if first {
2393 if first { first = false; } else { try!(self.word_space(",")); }
2394 try!(self.print_arg(arg));
2400 pub fn print_fn_args_and_ret(&mut self, decl: &ast::FnDecl,
2401 opt_explicit_self: Option<&ast::ExplicitSelf_>)
2404 try!(self.print_fn_args(decl, opt_explicit_self));
2406 try!(word(&mut self.s, ", ..."));
2408 try!(self.pclose());
2410 self.print_fn_output(decl)
2413 pub fn print_fn_block_args(
2417 try!(word(&mut self.s, "|"));
2418 try!(self.print_fn_args(decl, None));
2419 try!(word(&mut self.s, "|"));
2421 if let ast::DefaultReturn(..) = decl.output {
2425 try!(self.space_if_not_bol());
2426 try!(self.word_space("->"));
2428 ast::Return(ref ty) => {
2429 try!(self.print_type(&**ty));
2430 self.maybe_print_comment(ty.span.lo)
2432 ast::DefaultReturn(..) => unreachable!(),
2433 ast::NoReturn(span) => {
2434 try!(self.word_nbsp("!"));
2435 self.maybe_print_comment(span.lo)
2440 pub fn print_capture_clause(&mut self, capture_clause: ast::CaptureClause)
2442 match capture_clause {
2443 ast::CaptureByValue => self.word_space("move"),
2444 ast::CaptureByRef => Ok(()),
2448 pub fn print_bounds(&mut self,
2450 bounds: &[ast::TyParamBound])
2452 if !bounds.is_empty() {
2453 try!(word(&mut self.s, prefix));
2454 let mut first = true;
2455 for bound in bounds {
2460 try!(self.word_space("+"));
2464 TraitTyParamBound(ref tref, TraitBoundModifier::None) => {
2465 self.print_poly_trait_ref(tref)
2467 TraitTyParamBound(ref tref, TraitBoundModifier::Maybe) => {
2468 try!(word(&mut self.s, "?"));
2469 self.print_poly_trait_ref(tref)
2471 RegionTyParamBound(ref lt) => {
2472 self.print_lifetime(lt)
2482 pub fn print_lifetime(&mut self,
2483 lifetime: &ast::Lifetime)
2486 self.print_name(lifetime.name)
2489 pub fn print_lifetime_def(&mut self,
2490 lifetime: &ast::LifetimeDef)
2493 try!(self.print_lifetime(&lifetime.lifetime));
2495 for v in &lifetime.bounds {
2496 try!(word(&mut self.s, sep));
2497 try!(self.print_lifetime(v));
2503 pub fn print_generics(&mut self,
2504 generics: &ast::Generics)
2507 let total = generics.lifetimes.len() + generics.ty_params.len();
2512 try!(word(&mut self.s, "<"));
2514 let mut ints = Vec::new();
2519 try!(self.commasep(Inconsistent, &ints[..], |s, &idx| {
2520 if idx < generics.lifetimes.len() {
2521 let lifetime = &generics.lifetimes[idx];
2522 s.print_lifetime_def(lifetime)
2524 let idx = idx - generics.lifetimes.len();
2525 let param = &generics.ty_params[idx];
2526 s.print_ty_param(param)
2530 try!(word(&mut self.s, ">"));
2534 pub fn print_ty_param(&mut self, param: &ast::TyParam) -> io::Result<()> {
2535 try!(self.print_ident(param.ident));
2536 try!(self.print_bounds(":", ¶m.bounds));
2537 match param.default {
2538 Some(ref default) => {
2539 try!(space(&mut self.s));
2540 try!(self.word_space("="));
2541 self.print_type(&**default)
2547 pub fn print_where_clause(&mut self, where_clause: &ast::WhereClause)
2549 if where_clause.predicates.is_empty() {
2553 try!(space(&mut self.s));
2554 try!(self.word_space("where"));
2556 for (i, predicate) in where_clause.predicates.iter().enumerate() {
2558 try!(self.word_space(","));
2562 &ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate{ref bound_lifetimes,
2566 try!(self.print_formal_lifetime_list(bound_lifetimes));
2567 try!(self.print_type(&**bounded_ty));
2568 try!(self.print_bounds(":", bounds));
2570 &ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate{ref lifetime,
2573 try!(self.print_lifetime(lifetime));
2574 try!(word(&mut self.s, ":"));
2576 for (i, bound) in bounds.iter().enumerate() {
2577 try!(self.print_lifetime(bound));
2580 try!(word(&mut self.s, ":"));
2584 &ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{ref path, ref ty, ..}) => {
2585 try!(self.print_path(path, false, 0));
2586 try!(space(&mut self.s));
2587 try!(self.word_space("="));
2588 try!(self.print_type(&**ty));
2596 pub fn print_meta_item(&mut self, item: &ast::MetaItem) -> io::Result<()> {
2597 try!(self.ibox(indent_unit));
2599 ast::MetaWord(ref name) => {
2600 try!(word(&mut self.s, &name));
2602 ast::MetaNameValue(ref name, ref value) => {
2603 try!(self.word_space(&name[..]));
2604 try!(self.word_space("="));
2605 try!(self.print_literal(value));
2607 ast::MetaList(ref name, ref items) => {
2608 try!(word(&mut self.s, &name));
2610 try!(self.commasep(Consistent,
2612 |s, i| s.print_meta_item(&**i)));
2613 try!(self.pclose());
2619 pub fn print_view_path(&mut self, vp: &ast::ViewPath) -> io::Result<()> {
2621 ast::ViewPathSimple(ident, ref path) => {
2622 try!(self.print_path(path, false, 0));
2624 // FIXME(#6993) can't compare identifiers directly here
2625 if path.segments.last().unwrap().identifier.name !=
2627 try!(space(&mut self.s));
2628 try!(self.word_space("as"));
2629 try!(self.print_ident(ident));
2635 ast::ViewPathGlob(ref path) => {
2636 try!(self.print_path(path, false, 0));
2637 word(&mut self.s, "::*")
2640 ast::ViewPathList(ref path, ref idents) => {
2641 if path.segments.is_empty() {
2642 try!(word(&mut self.s, "{"));
2644 try!(self.print_path(path, false, 0));
2645 try!(word(&mut self.s, "::{"));
2647 try!(self.commasep(Inconsistent, &idents[..], |s, w| {
2649 ast::PathListIdent { name, rename, .. } => {
2650 try!(s.print_ident(name));
2651 if let Some(ident) = rename {
2652 try!(space(&mut s.s));
2653 try!(s.word_space("as"));
2654 try!(s.print_ident(ident));
2658 ast::PathListMod { rename, .. } => {
2659 try!(word(&mut s.s, "self"));
2660 if let Some(ident) = rename {
2661 try!(space(&mut s.s));
2662 try!(s.word_space("as"));
2663 try!(s.print_ident(ident));
2669 word(&mut self.s, "}")
2674 pub fn print_mutability(&mut self,
2675 mutbl: ast::Mutability) -> io::Result<()> {
2677 ast::MutMutable => self.word_nbsp("mut"),
2678 ast::MutImmutable => Ok(()),
2682 pub fn print_mt(&mut self, mt: &ast::MutTy) -> io::Result<()> {
2683 try!(self.print_mutability(mt.mutbl));
2684 self.print_type(&*mt.ty)
2687 pub fn print_arg(&mut self, input: &ast::Arg) -> io::Result<()> {
2688 try!(self.ibox(indent_unit));
2689 match input.ty.node {
2690 ast::TyInfer => try!(self.print_pat(&*input.pat)),
2692 match input.pat.node {
2693 ast::PatIdent(_, ref path1, _) if
2695 parse::token::special_idents::invalid.name => {
2699 try!(self.print_pat(&*input.pat));
2700 try!(word(&mut self.s, ":"));
2701 try!(space(&mut self.s));
2704 try!(self.print_type(&*input.ty));
2710 pub fn print_fn_output(&mut self, decl: &ast::FnDecl) -> io::Result<()> {
2711 if let ast::DefaultReturn(..) = decl.output {
2715 try!(self.space_if_not_bol());
2716 try!(self.ibox(indent_unit));
2717 try!(self.word_space("->"));
2720 try!(self.word_nbsp("!")),
2721 ast::DefaultReturn(..) => unreachable!(),
2722 ast::Return(ref ty) =>
2723 try!(self.print_type(&**ty))
2728 ast::Return(ref output) => self.maybe_print_comment(output.span.lo),
2733 pub fn print_ty_fn(&mut self,
2735 unsafety: ast::Unsafety,
2737 name: Option<ast::Ident>,
2738 generics: &ast::Generics,
2739 opt_explicit_self: Option<&ast::ExplicitSelf_>)
2741 try!(self.ibox(indent_unit));
2742 if !generics.lifetimes.is_empty() || !generics.ty_params.is_empty() {
2743 try!(word(&mut self.s, "for"));
2744 try!(self.print_generics(generics));
2746 let generics = ast::Generics {
2747 lifetimes: Vec::new(),
2748 ty_params: OwnedSlice::empty(),
2749 where_clause: ast::WhereClause {
2750 id: ast::DUMMY_NODE_ID,
2751 predicates: Vec::new(),
2754 try!(self.print_fn(decl,
2756 ast::Constness::NotConst,
2765 pub fn maybe_print_trailing_comment(&mut self, span: codemap::Span,
2766 next_pos: Option<BytePos>)
2768 let cm = match self.cm {
2772 match self.next_comment() {
2774 if (*cmnt).style != comments::Trailing { return Ok(()) }
2775 let span_line = cm.lookup_char_pos(span.hi);
2776 let comment_line = cm.lookup_char_pos((*cmnt).pos);
2777 let mut next = (*cmnt).pos + BytePos(1);
2778 match next_pos { None => (), Some(p) => next = p }
2779 if span.hi < (*cmnt).pos && (*cmnt).pos < next &&
2780 span_line.line == comment_line.line {
2781 try!(self.print_comment(cmnt));
2782 self.cur_cmnt_and_lit.cur_cmnt += 1;
2790 pub fn print_remaining_comments(&mut self) -> io::Result<()> {
2791 // If there aren't any remaining comments, then we need to manually
2792 // make sure there is a line break at the end.
2793 if self.next_comment().is_none() {
2794 try!(hardbreak(&mut self.s));
2797 match self.next_comment() {
2799 try!(self.print_comment(cmnt));
2800 self.cur_cmnt_and_lit.cur_cmnt += 1;
2808 pub fn print_literal(&mut self, lit: &ast::Lit) -> io::Result<()> {
2809 try!(self.maybe_print_comment(lit.span.lo));
2810 match self.next_lit(lit.span.lo) {
2812 return word(&mut self.s, &(*ltrl).lit);
2817 ast::LitStr(ref st, style) => self.print_string(&st, style),
2818 ast::LitByte(byte) => {
2819 let mut res = String::from("b'");
2820 res.extend(ascii::escape_default(byte).map(|c| c as char));
2822 word(&mut self.s, &res[..])
2824 ast::LitChar(ch) => {
2825 let mut res = String::from("'");
2826 res.extend(ch.escape_default());
2828 word(&mut self.s, &res[..])
2830 ast::LitInt(i, t) => {
2832 ast::SignedIntLit(st, ast::Plus) => {
2834 &ast_util::int_ty_to_string(st, Some(i as i64)))
2836 ast::SignedIntLit(st, ast::Minus) => {
2837 let istr = ast_util::int_ty_to_string(st, Some(-(i as i64)));
2839 &format!("-{}", istr))
2841 ast::UnsignedIntLit(ut) => {
2842 word(&mut self.s, &ast_util::uint_ty_to_string(ut, Some(i)))
2844 ast::UnsuffixedIntLit(ast::Plus) => {
2845 word(&mut self.s, &format!("{}", i))
2847 ast::UnsuffixedIntLit(ast::Minus) => {
2848 word(&mut self.s, &format!("-{}", i))
2852 ast::LitFloat(ref f, t) => {
2857 &ast_util::float_ty_to_string(t)))
2859 ast::LitFloatUnsuffixed(ref f) => word(&mut self.s, &f[..]),
2860 ast::LitBool(val) => {
2861 if val { word(&mut self.s, "true") } else { word(&mut self.s, "false") }
2863 ast::LitBinary(ref v) => {
2864 let mut escaped: String = String::new();
2865 for &ch in v.iter() {
2866 escaped.extend(ascii::escape_default(ch)
2867 .map(|c| c as char));
2869 word(&mut self.s, &format!("b\"{}\"", escaped))
2874 pub fn next_lit(&mut self, pos: BytePos) -> Option<comments::Literal> {
2875 match self.literals {
2877 while self.cur_cmnt_and_lit.cur_lit < lits.len() {
2878 let ltrl = (*lits)[self.cur_cmnt_and_lit.cur_lit].clone();
2879 if ltrl.pos > pos { return None; }
2880 self.cur_cmnt_and_lit.cur_lit += 1;
2881 if ltrl.pos == pos { return Some(ltrl); }
2889 pub fn maybe_print_comment(&mut self, pos: BytePos) -> io::Result<()> {
2891 match self.next_comment() {
2893 if (*cmnt).pos < pos {
2894 try!(self.print_comment(cmnt));
2895 self.cur_cmnt_and_lit.cur_cmnt += 1;
2904 pub fn print_comment(&mut self,
2905 cmnt: &comments::Comment) -> io::Result<()> {
2907 comments::Mixed => {
2908 assert_eq!(cmnt.lines.len(), 1);
2909 try!(zerobreak(&mut self.s));
2910 try!(word(&mut self.s, &cmnt.lines[0]));
2911 zerobreak(&mut self.s)
2913 comments::Isolated => {
2914 try!(self.hardbreak_if_not_bol());
2915 for line in &cmnt.lines {
2916 // Don't print empty lines because they will end up as trailing
2918 if !line.is_empty() {
2919 try!(word(&mut self.s, &line[..]));
2921 try!(hardbreak(&mut self.s));
2925 comments::Trailing => {
2926 try!(word(&mut self.s, " "));
2927 if cmnt.lines.len() == 1 {
2928 try!(word(&mut self.s, &cmnt.lines[0]));
2929 hardbreak(&mut self.s)
2932 for line in &cmnt.lines {
2933 if !line.is_empty() {
2934 try!(word(&mut self.s, &line[..]));
2936 try!(hardbreak(&mut self.s));
2941 comments::BlankLine => {
2942 // We need to do at least one, possibly two hardbreaks.
2943 let is_semi = match self.s.last_token() {
2944 pp::Token::String(s, _) => ";" == s,
2947 if is_semi || self.is_begin() || self.is_end() {
2948 try!(hardbreak(&mut self.s));
2950 hardbreak(&mut self.s)
2955 pub fn print_string(&mut self, st: &str,
2956 style: ast::StrStyle) -> io::Result<()> {
2957 let st = match style {
2959 (format!("\"{}\"", st.escape_default()))
2962 (format!("r{delim}\"{string}\"{delim}",
2963 delim=repeat("#", n),
2967 word(&mut self.s, &st[..])
2970 pub fn next_comment(&mut self) -> Option<comments::Comment> {
2971 match self.comments {
2972 Some(ref cmnts) => {
2973 if self.cur_cmnt_and_lit.cur_cmnt < cmnts.len() {
2974 Some(cmnts[self.cur_cmnt_and_lit.cur_cmnt].clone())
2983 pub fn print_opt_abi_and_extern_if_nondefault(&mut self,
2984 opt_abi: Option<abi::Abi>)
2987 Some(abi::Rust) => Ok(()),
2989 try!(self.word_nbsp("extern"));
2990 self.word_nbsp(&abi.to_string())
2996 pub fn print_extern_opt_abi(&mut self,
2997 opt_abi: Option<abi::Abi>) -> io::Result<()> {
3000 try!(self.word_nbsp("extern"));
3001 self.word_nbsp(&abi.to_string())
3007 pub fn print_fn_header_info(&mut self,
3008 unsafety: ast::Unsafety,
3009 constness: ast::Constness,
3011 vis: ast::Visibility) -> io::Result<()> {
3012 try!(word(&mut self.s, &visibility_qualified(vis, "")));
3013 try!(self.print_unsafety(unsafety));
3016 ast::Constness::NotConst => {}
3017 ast::Constness::Const => try!(self.word_nbsp("const"))
3020 if abi != abi::Rust {
3021 try!(self.word_nbsp("extern"));
3022 try!(self.word_nbsp(&abi.to_string()));
3025 word(&mut self.s, "fn")
3028 pub fn print_unsafety(&mut self, s: ast::Unsafety) -> io::Result<()> {
3030 ast::Unsafety::Normal => Ok(()),
3031 ast::Unsafety::Unsafe => self.word_nbsp("unsafe"),
3036 fn repeat(s: &str, n: usize) -> String { iter::repeat(s).take(n).collect() }
3048 fn test_fun_to_string() {
3049 let abba_ident = token::str_to_ident("abba");
3051 let decl = ast::FnDecl {
3053 output: ast::DefaultReturn(codemap::DUMMY_SP),
3056 let generics = ast_util::empty_generics();
3057 assert_eq!(fun_to_string(&decl, ast::Unsafety::Normal,
3058 ast::Constness::NotConst,
3065 fn test_variant_to_string() {
3066 let ident = token::str_to_ident("principal_skinner");
3068 let var = codemap::respan(codemap::DUMMY_SP, ast::Variant_ {
3071 // making this up as I go.... ?
3072 kind: ast::TupleVariantKind(Vec::new()),
3078 let varstr = variant_to_string(&var);
3079 assert_eq!(varstr, "pub principal_skinner");
3083 fn test_signed_int_to_string() {
3084 let pos_int = ast::LitInt(42, ast::SignedIntLit(ast::TyI32, ast::Plus));
3085 let neg_int = ast::LitInt((!42 + 1) as u64, ast::SignedIntLit(ast::TyI32, ast::Minus));
3086 assert_eq!(format!("-{}", lit_to_string(&codemap::dummy_spanned(pos_int))),
3087 lit_to_string(&codemap::dummy_spanned(neg_int)));