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::ByteStr(v) => format!("b\"{}\"", v),
263 token::ByteStrRaw(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,
453 pub trait PrintState<'a> {
454 fn writer(&mut self) -> &mut pp::Printer<'a>;
455 fn boxes(&mut self) -> &mut Vec<pp::Breaks>;
456 fn comments(&mut self) -> &mut Option<Vec<comments::Comment>>;
457 fn cur_cmnt_and_lit(&mut self) -> &mut CurrentCommentAndLiteral;
458 fn literals(&self) -> &Option<Vec<comments::Literal>>;
460 fn word_space(&mut self, w: &str) -> io::Result<()> {
461 try!(word(self.writer(), w));
465 fn popen(&mut self) -> io::Result<()> { word(self.writer(), "(") }
467 fn pclose(&mut self) -> io::Result<()> { word(self.writer(), ")") }
469 fn is_begin(&mut self) -> bool {
470 match self.writer().last_token() {
471 pp::Token::Begin(_) => true,
476 fn is_end(&mut self) -> bool {
477 match self.writer().last_token() {
478 pp::Token::End => true,
483 // is this the beginning of a line?
484 fn is_bol(&mut self) -> bool {
485 self.writer().last_token().is_eof() || self.writer().last_token().is_hardbreak_tok()
488 fn hardbreak_if_not_bol(&mut self) -> io::Result<()> {
490 try!(hardbreak(self.writer()))
496 fn rbox(&mut self, u: usize, b: pp::Breaks) -> io::Result<()> {
497 self.boxes().push(b);
498 pp::rbox(self.writer(), u, b)
501 fn ibox(&mut self, u: usize) -> io::Result<()> {
502 self.boxes().push(pp::Breaks::Inconsistent);
503 pp::ibox(self.writer(), u)
506 fn end(&mut self) -> io::Result<()> {
507 self.boxes().pop().unwrap();
508 pp::end(self.writer())
511 fn commasep<T, F>(&mut self, b: Breaks, elts: &[T], mut op: F) -> io::Result<()>
512 where F: FnMut(&mut Self, &T) -> io::Result<()>,
514 try!(self.rbox(0, b));
515 let mut first = true;
517 if first { first = false; } else { try!(self.word_space(",")); }
523 fn commasep_iter<'it, T: 'it, F, I>(&mut self, b: Breaks, elts: I, mut op: F) -> io::Result<()>
524 where F: FnMut(&mut Self, &T) -> io::Result<()>,
525 I: Iterator<Item=&'it T>,
527 try!(self.rbox(0, b));
528 let mut first = true;
530 if first { first = false; } else { try!(self.word_space(",")); }
536 fn next_lit(&mut self, pos: BytePos) -> Option<comments::Literal> {
537 let mut cur_lit = self.cur_cmnt_and_lit().cur_lit;
539 let mut result = None;
541 if let &Some(ref lits) = self.literals()
543 while cur_lit < lits.len() {
544 let ltrl = (*lits)[cur_lit].clone();
545 if ltrl.pos > pos { break; }
554 self.cur_cmnt_and_lit().cur_lit = cur_lit;
558 fn maybe_print_comment(&mut self, pos: BytePos) -> io::Result<()> {
560 match self.next_comment() {
562 if (*cmnt).pos < pos {
563 try!(self.print_comment(cmnt));
564 self.cur_cmnt_and_lit().cur_cmnt += 1;
573 fn print_comment(&mut self,
574 cmnt: &comments::Comment) -> io::Result<()> {
577 assert_eq!(cmnt.lines.len(), 1);
578 try!(zerobreak(self.writer()));
579 try!(word(self.writer(), &cmnt.lines[0]));
580 zerobreak(self.writer())
582 comments::Isolated => {
583 try!(self.hardbreak_if_not_bol());
584 for line in &cmnt.lines {
585 // Don't print empty lines because they will end up as trailing
587 if !line.is_empty() {
588 try!(word(self.writer(), &line[..]));
590 try!(hardbreak(self.writer()));
594 comments::Trailing => {
595 try!(word(self.writer(), " "));
596 if cmnt.lines.len() == 1 {
597 try!(word(self.writer(), &cmnt.lines[0]));
598 hardbreak(self.writer())
601 for line in &cmnt.lines {
602 if !line.is_empty() {
603 try!(word(self.writer(), &line[..]));
605 try!(hardbreak(self.writer()));
610 comments::BlankLine => {
611 // We need to do at least one, possibly two hardbreaks.
612 let is_semi = match self.writer().last_token() {
613 pp::Token::String(s, _) => ";" == s,
616 if is_semi || self.is_begin() || self.is_end() {
617 try!(hardbreak(self.writer()));
619 hardbreak(self.writer())
624 fn next_comment(&mut self) -> Option<comments::Comment> {
625 let cur_cmnt = self.cur_cmnt_and_lit().cur_cmnt;
626 match *self.comments() {
628 if cur_cmnt < cmnts.len() {
629 Some(cmnts[cur_cmnt].clone())
638 fn print_literal(&mut self, lit: &ast::Lit) -> io::Result<()> {
639 try!(self.maybe_print_comment(lit.span.lo));
640 match self.next_lit(lit.span.lo) {
642 return word(self.writer(), &(*ltrl).lit);
647 ast::LitStr(ref st, style) => self.print_string(&st, style),
648 ast::LitByte(byte) => {
649 let mut res = String::from("b'");
650 res.extend(ascii::escape_default(byte).map(|c| c as char));
652 word(self.writer(), &res[..])
654 ast::LitChar(ch) => {
655 let mut res = String::from("'");
656 res.extend(ch.escape_default());
658 word(self.writer(), &res[..])
660 ast::LitInt(i, t) => {
662 ast::SignedIntLit(st, ast::Plus) => {
664 &ast_util::int_ty_to_string(st, Some(i as i64)))
666 ast::SignedIntLit(st, ast::Minus) => {
667 let istr = ast_util::int_ty_to_string(st, Some(-(i as i64)));
669 &format!("-{}", istr))
671 ast::UnsignedIntLit(ut) => {
672 word(self.writer(), &ast_util::uint_ty_to_string(ut, Some(i)))
674 ast::UnsuffixedIntLit(ast::Plus) => {
675 word(self.writer(), &format!("{}", i))
677 ast::UnsuffixedIntLit(ast::Minus) => {
678 word(self.writer(), &format!("-{}", i))
682 ast::LitFloat(ref f, t) => {
687 &ast_util::float_ty_to_string(t)))
689 ast::LitFloatUnsuffixed(ref f) => word(self.writer(), &f[..]),
690 ast::LitBool(val) => {
691 if val { word(self.writer(), "true") } else { word(self.writer(), "false") }
693 ast::LitByteStr(ref v) => {
694 let mut escaped: String = String::new();
695 for &ch in v.iter() {
696 escaped.extend(ascii::escape_default(ch)
697 .map(|c| c as char));
699 word(self.writer(), &format!("b\"{}\"", escaped))
704 fn print_string(&mut self, st: &str,
705 style: ast::StrStyle) -> io::Result<()> {
706 let st = match style {
708 (format!("\"{}\"", st.escape_default()))
711 (format!("r{delim}\"{string}\"{delim}",
712 delim=repeat("#", n),
716 word(self.writer(), &st[..])
719 fn print_inner_attributes(&mut self,
720 attrs: &[ast::Attribute]) -> io::Result<()> {
723 match attr.node.style {
724 ast::AttrStyle::Inner => {
725 try!(self.print_attribute(attr));
728 _ => {/* fallthrough */ }
732 try!(self.hardbreak_if_not_bol());
737 fn print_outer_attributes(&mut self,
738 attrs: &[ast::Attribute]) -> io::Result<()> {
741 match attr.node.style {
742 ast::AttrStyle::Outer => {
743 try!(self.print_attribute(attr));
746 _ => {/* fallthrough */ }
750 try!(self.hardbreak_if_not_bol());
755 fn print_attribute(&mut self, attr: &ast::Attribute) -> io::Result<()> {
756 try!(self.hardbreak_if_not_bol());
757 try!(self.maybe_print_comment(attr.span.lo));
758 if attr.node.is_sugared_doc {
759 word(self.writer(), &attr.value_str().unwrap())
761 match attr.node.style {
762 ast::AttrStyle::Inner => try!(word(self.writer(), "#![")),
763 ast::AttrStyle::Outer => try!(word(self.writer(), "#[")),
765 try!(self.print_meta_item(&*attr.meta()));
766 word(self.writer(), "]")
770 fn print_meta_item(&mut self, item: &ast::MetaItem) -> io::Result<()> {
771 try!(self.ibox(indent_unit));
773 ast::MetaWord(ref name) => {
774 try!(word(self.writer(), &name));
776 ast::MetaNameValue(ref name, ref value) => {
777 try!(self.word_space(&name[..]));
778 try!(self.word_space("="));
779 try!(self.print_literal(value));
781 ast::MetaList(ref name, ref items) => {
782 try!(word(self.writer(), &name));
784 try!(self.commasep(Consistent,
786 |s, i| s.print_meta_item(&**i)));
794 impl<'a> PrintState<'a> for State<'a> {
795 fn writer(&mut self) -> &mut pp::Printer<'a> {
799 fn boxes(&mut self) -> &mut Vec<pp::Breaks> {
803 fn comments(&mut self) -> &mut Option<Vec<comments::Comment>> {
807 fn cur_cmnt_and_lit(&mut self) -> &mut CurrentCommentAndLiteral {
808 &mut self.cur_cmnt_and_lit
811 fn literals(&self) -> &Option<Vec<comments::Literal>> {
817 pub fn cbox(&mut self, u: usize) -> io::Result<()> {
818 self.boxes.push(pp::Breaks::Consistent);
819 pp::cbox(&mut self.s, u)
822 pub fn nbsp(&mut self) -> io::Result<()> { word(&mut self.s, " ") }
824 pub fn word_nbsp(&mut self, w: &str) -> io::Result<()> {
825 try!(word(&mut self.s, w));
829 pub fn head(&mut self, w: &str) -> io::Result<()> {
830 // outer-box is consistent
831 try!(self.cbox(indent_unit));
832 // head-box is inconsistent
833 try!(self.ibox(w.len() + 1));
834 // keyword that starts the head
836 try!(self.word_nbsp(w));
841 pub fn bopen(&mut self) -> io::Result<()> {
842 try!(word(&mut self.s, "{"));
843 self.end() // close the head-box
846 pub fn bclose_(&mut self, span: codemap::Span,
847 indented: usize) -> io::Result<()> {
848 self.bclose_maybe_open(span, indented, true)
850 pub fn bclose_maybe_open (&mut self, span: codemap::Span,
851 indented: usize, close_box: bool) -> io::Result<()> {
852 try!(self.maybe_print_comment(span.hi));
853 try!(self.break_offset_if_not_bol(1, -(indented as isize)));
854 try!(word(&mut self.s, "}"));
856 try!(self.end()); // close the outer-box
860 pub fn bclose(&mut self, span: codemap::Span) -> io::Result<()> {
861 self.bclose_(span, indent_unit)
864 pub fn in_cbox(&self) -> bool {
865 match self.boxes.last() {
866 Some(&last_box) => last_box == pp::Breaks::Consistent,
871 pub fn space_if_not_bol(&mut self) -> io::Result<()> {
872 if !self.is_bol() { try!(space(&mut self.s)); }
875 pub fn break_offset_if_not_bol(&mut self, n: usize,
876 off: isize) -> io::Result<()> {
878 break_offset(&mut self.s, n, off)
880 if off != 0 && self.s.last_token().is_hardbreak_tok() {
881 // We do something pretty sketchy here: tuck the nonzero
882 // offset-adjustment we were going to deposit along with the
883 // break into the previous hardbreak.
884 self.s.replace_last_token(pp::hardbreak_tok_offset(off));
890 // Synthesizes a comment that was not textually present in the original source
892 pub fn synth_comment(&mut self, text: String) -> io::Result<()> {
893 try!(word(&mut self.s, "/*"));
894 try!(space(&mut self.s));
895 try!(word(&mut self.s, &text[..]));
896 try!(space(&mut self.s));
897 word(&mut self.s, "*/")
902 pub fn commasep_cmnt<T, F, G>(&mut self,
906 mut get_span: G) -> io::Result<()> where
907 F: FnMut(&mut State, &T) -> io::Result<()>,
908 G: FnMut(&T) -> codemap::Span,
910 try!(self.rbox(0, b));
911 let len = elts.len();
914 try!(self.maybe_print_comment(get_span(elt).hi));
918 try!(word(&mut self.s, ","));
919 try!(self.maybe_print_trailing_comment(get_span(elt),
920 Some(get_span(&elts[i]).hi)));
921 try!(self.space_if_not_bol());
927 pub fn commasep_exprs(&mut self, b: Breaks,
928 exprs: &[P<ast::Expr>]) -> io::Result<()> {
929 self.commasep_cmnt(b, exprs, |s, e| s.print_expr(&**e), |e| e.span)
932 pub fn print_mod(&mut self, _mod: &ast::Mod,
933 attrs: &[ast::Attribute]) -> io::Result<()> {
934 try!(self.print_inner_attributes(attrs));
935 for item in &_mod.items {
936 try!(self.print_item(&**item));
941 pub fn print_foreign_mod(&mut self, nmod: &ast::ForeignMod,
942 attrs: &[ast::Attribute]) -> io::Result<()> {
943 try!(self.print_inner_attributes(attrs));
944 for item in &nmod.items {
945 try!(self.print_foreign_item(&**item));
950 pub fn print_opt_lifetime(&mut self,
951 lifetime: &Option<ast::Lifetime>) -> io::Result<()> {
952 if let Some(l) = *lifetime {
953 try!(self.print_lifetime(&l));
959 pub fn print_type(&mut self, ty: &ast::Ty) -> io::Result<()> {
960 try!(self.maybe_print_comment(ty.span.lo));
963 ast::TyVec(ref ty) => {
964 try!(word(&mut self.s, "["));
965 try!(self.print_type(&**ty));
966 try!(word(&mut self.s, "]"));
968 ast::TyPtr(ref mt) => {
969 try!(word(&mut self.s, "*"));
971 ast::MutMutable => try!(self.word_nbsp("mut")),
972 ast::MutImmutable => try!(self.word_nbsp("const")),
974 try!(self.print_type(&*mt.ty));
976 ast::TyRptr(ref lifetime, ref mt) => {
977 try!(word(&mut self.s, "&"));
978 try!(self.print_opt_lifetime(lifetime));
979 try!(self.print_mt(mt));
981 ast::TyTup(ref elts) => {
983 try!(self.commasep(Inconsistent, &elts[..],
984 |s, ty| s.print_type(&**ty)));
986 try!(word(&mut self.s, ","));
990 ast::TyParen(ref typ) => {
992 try!(self.print_type(&**typ));
995 ast::TyBareFn(ref f) => {
996 let generics = ast::Generics {
997 lifetimes: f.lifetimes.clone(),
998 ty_params: OwnedSlice::empty(),
999 where_clause: ast::WhereClause {
1000 id: ast::DUMMY_NODE_ID,
1001 predicates: Vec::new(),
1004 try!(self.print_ty_fn(f.abi,
1011 ast::TyPath(None, ref path) => {
1012 try!(self.print_path(path, false, 0));
1014 ast::TyPath(Some(ref qself), ref path) => {
1015 try!(self.print_qpath(path, qself, false))
1017 ast::TyObjectSum(ref ty, ref bounds) => {
1018 try!(self.print_type(&**ty));
1019 try!(self.print_bounds("+", &bounds[..]));
1021 ast::TyPolyTraitRef(ref bounds) => {
1022 try!(self.print_bounds("", &bounds[..]));
1024 ast::TyFixedLengthVec(ref ty, ref v) => {
1025 try!(word(&mut self.s, "["));
1026 try!(self.print_type(&**ty));
1027 try!(word(&mut self.s, "; "));
1028 try!(self.print_expr(&**v));
1029 try!(word(&mut self.s, "]"));
1031 ast::TyTypeof(ref e) => {
1032 try!(word(&mut self.s, "typeof("));
1033 try!(self.print_expr(&**e));
1034 try!(word(&mut self.s, ")"));
1037 try!(word(&mut self.s, "_"));
1039 ast::TyMac(ref m) => {
1040 try!(self.print_mac(m, token::Paren));
1046 pub fn print_foreign_item(&mut self,
1047 item: &ast::ForeignItem) -> io::Result<()> {
1048 try!(self.hardbreak_if_not_bol());
1049 try!(self.maybe_print_comment(item.span.lo));
1050 try!(self.print_outer_attributes(&item.attrs));
1052 ast::ForeignItemFn(ref decl, ref generics) => {
1053 try!(self.head(""));
1054 try!(self.print_fn(decl, ast::Unsafety::Normal,
1055 ast::Constness::NotConst,
1056 abi::Rust, Some(item.ident),
1057 generics, None, item.vis));
1058 try!(self.end()); // end head-ibox
1059 try!(word(&mut self.s, ";"));
1060 self.end() // end the outer fn box
1062 ast::ForeignItemStatic(ref t, m) => {
1063 try!(self.head(&visibility_qualified(item.vis,
1066 try!(self.word_space("mut"));
1068 try!(self.print_ident(item.ident));
1069 try!(self.word_space(":"));
1070 try!(self.print_type(&**t));
1071 try!(word(&mut self.s, ";"));
1072 try!(self.end()); // end the head-ibox
1073 self.end() // end the outer cbox
1078 fn print_associated_const(&mut self,
1081 default: Option<&ast::Expr>,
1082 vis: ast::Visibility)
1085 try!(word(&mut self.s, &visibility_qualified(vis, "")));
1086 try!(self.word_space("const"));
1087 try!(self.print_ident(ident));
1088 try!(self.word_space(":"));
1089 try!(self.print_type(ty));
1090 if let Some(expr) = default {
1091 try!(space(&mut self.s));
1092 try!(self.word_space("="));
1093 try!(self.print_expr(expr));
1095 word(&mut self.s, ";")
1098 fn print_associated_type(&mut self,
1100 bounds: Option<&ast::TyParamBounds>,
1101 ty: Option<&ast::Ty>)
1103 try!(self.word_space("type"));
1104 try!(self.print_ident(ident));
1105 if let Some(bounds) = bounds {
1106 try!(self.print_bounds(":", bounds));
1108 if let Some(ty) = ty {
1109 try!(space(&mut self.s));
1110 try!(self.word_space("="));
1111 try!(self.print_type(ty));
1113 word(&mut self.s, ";")
1116 /// Pretty-print an item
1117 pub fn print_item(&mut self, item: &ast::Item) -> io::Result<()> {
1118 try!(self.hardbreak_if_not_bol());
1119 try!(self.maybe_print_comment(item.span.lo));
1120 try!(self.print_outer_attributes(&item.attrs));
1121 try!(self.ann.pre(self, NodeItem(item)));
1123 ast::ItemExternCrate(ref optional_path) => {
1124 try!(self.head(&visibility_qualified(item.vis,
1126 if let Some(p) = *optional_path {
1127 let val = p.as_str();
1128 if val.contains("-") {
1129 try!(self.print_string(&val, ast::CookedStr));
1131 try!(self.print_name(p));
1133 try!(space(&mut self.s));
1134 try!(word(&mut self.s, "as"));
1135 try!(space(&mut self.s));
1137 try!(self.print_ident(item.ident));
1138 try!(word(&mut self.s, ";"));
1139 try!(self.end()); // end inner head-block
1140 try!(self.end()); // end outer head-block
1142 ast::ItemUse(ref vp) => {
1143 try!(self.head(&visibility_qualified(item.vis,
1145 try!(self.print_view_path(&**vp));
1146 try!(word(&mut self.s, ";"));
1147 try!(self.end()); // end inner head-block
1148 try!(self.end()); // end outer head-block
1150 ast::ItemStatic(ref ty, m, ref expr) => {
1151 try!(self.head(&visibility_qualified(item.vis,
1153 if m == ast::MutMutable {
1154 try!(self.word_space("mut"));
1156 try!(self.print_ident(item.ident));
1157 try!(self.word_space(":"));
1158 try!(self.print_type(&**ty));
1159 try!(space(&mut self.s));
1160 try!(self.end()); // end the head-ibox
1162 try!(self.word_space("="));
1163 try!(self.print_expr(&**expr));
1164 try!(word(&mut self.s, ";"));
1165 try!(self.end()); // end the outer cbox
1167 ast::ItemConst(ref ty, ref expr) => {
1168 try!(self.head(&visibility_qualified(item.vis,
1170 try!(self.print_ident(item.ident));
1171 try!(self.word_space(":"));
1172 try!(self.print_type(&**ty));
1173 try!(space(&mut self.s));
1174 try!(self.end()); // end the head-ibox
1176 try!(self.word_space("="));
1177 try!(self.print_expr(&**expr));
1178 try!(word(&mut self.s, ";"));
1179 try!(self.end()); // end the outer cbox
1181 ast::ItemFn(ref decl, unsafety, constness, abi, ref typarams, ref body) => {
1182 try!(self.head(""));
1193 try!(word(&mut self.s, " "));
1194 try!(self.print_block_with_attrs(&**body, &item.attrs));
1196 ast::ItemMod(ref _mod) => {
1197 try!(self.head(&visibility_qualified(item.vis,
1199 try!(self.print_ident(item.ident));
1202 try!(self.print_mod(_mod, &item.attrs));
1203 try!(self.bclose(item.span));
1205 ast::ItemForeignMod(ref nmod) => {
1206 try!(self.head("extern"));
1207 try!(self.word_nbsp(&nmod.abi.to_string()));
1209 try!(self.print_foreign_mod(nmod, &item.attrs));
1210 try!(self.bclose(item.span));
1212 ast::ItemTy(ref ty, ref params) => {
1213 try!(self.ibox(indent_unit));
1215 try!(self.word_nbsp(&visibility_qualified(item.vis, "type")));
1216 try!(self.print_ident(item.ident));
1217 try!(self.print_generics(params));
1218 try!(self.end()); // end the inner ibox
1220 try!(self.print_where_clause(¶ms.where_clause));
1221 try!(space(&mut self.s));
1222 try!(self.word_space("="));
1223 try!(self.print_type(&**ty));
1224 try!(word(&mut self.s, ";"));
1225 try!(self.end()); // end the outer ibox
1227 ast::ItemEnum(ref enum_definition, ref params) => {
1228 try!(self.print_enum_def(
1236 ast::ItemStruct(ref struct_def, ref generics) => {
1237 try!(self.head(&visibility_qualified(item.vis,"struct")));
1238 try!(self.print_struct(&struct_def, generics, item.ident, item.span, true));
1241 ast::ItemDefaultImpl(unsafety, ref trait_ref) => {
1242 try!(self.head(""));
1243 try!(self.print_visibility(item.vis));
1244 try!(self.print_unsafety(unsafety));
1245 try!(self.word_nbsp("impl"));
1246 try!(self.print_trait_ref(trait_ref));
1247 try!(space(&mut self.s));
1248 try!(self.word_space("for"));
1249 try!(self.word_space(".."));
1251 try!(self.bclose(item.span));
1253 ast::ItemImpl(unsafety,
1258 ref impl_items) => {
1259 try!(self.head(""));
1260 try!(self.print_visibility(item.vis));
1261 try!(self.print_unsafety(unsafety));
1262 try!(self.word_nbsp("impl"));
1264 if generics.is_parameterized() {
1265 try!(self.print_generics(generics));
1266 try!(space(&mut self.s));
1270 ast::ImplPolarity::Negative => {
1271 try!(word(&mut self.s, "!"));
1278 try!(self.print_trait_ref(t));
1279 try!(space(&mut self.s));
1280 try!(self.word_space("for"));
1285 try!(self.print_type(&**ty));
1286 try!(self.print_where_clause(&generics.where_clause));
1288 try!(space(&mut self.s));
1290 try!(self.print_inner_attributes(&item.attrs));
1291 for impl_item in impl_items {
1292 try!(self.print_impl_item(impl_item));
1294 try!(self.bclose(item.span));
1296 ast::ItemTrait(unsafety, ref generics, ref bounds, ref trait_items) => {
1297 try!(self.head(""));
1298 try!(self.print_visibility(item.vis));
1299 try!(self.print_unsafety(unsafety));
1300 try!(self.word_nbsp("trait"));
1301 try!(self.print_ident(item.ident));
1302 try!(self.print_generics(generics));
1303 let mut real_bounds = Vec::with_capacity(bounds.len());
1304 for b in bounds.iter() {
1305 if let TraitTyParamBound(ref ptr, ast::TraitBoundModifier::Maybe) = *b {
1306 try!(space(&mut self.s));
1307 try!(self.word_space("for ?"));
1308 try!(self.print_trait_ref(&ptr.trait_ref));
1310 real_bounds.push(b.clone());
1313 try!(self.print_bounds(":", &real_bounds[..]));
1314 try!(self.print_where_clause(&generics.where_clause));
1315 try!(word(&mut self.s, " "));
1317 for trait_item in trait_items {
1318 try!(self.print_trait_item(trait_item));
1320 try!(self.bclose(item.span));
1322 ast::ItemMac(codemap::Spanned { ref node, .. }) => {
1323 try!(self.print_visibility(item.vis));
1324 try!(self.print_path(&node.path, false, 0));
1325 try!(word(&mut self.s, "! "));
1326 try!(self.print_ident(item.ident));
1327 try!(self.cbox(indent_unit));
1329 try!(self.print_tts(&node.tts[..]));
1330 try!(self.pclose());
1331 try!(word(&mut self.s, ";"));
1335 self.ann.post(self, NodeItem(item))
1338 fn print_trait_ref(&mut self, t: &ast::TraitRef) -> io::Result<()> {
1339 self.print_path(&t.path, false, 0)
1342 fn print_formal_lifetime_list(&mut self, lifetimes: &[ast::LifetimeDef]) -> io::Result<()> {
1343 if !lifetimes.is_empty() {
1344 try!(word(&mut self.s, "for<"));
1345 let mut comma = false;
1346 for lifetime_def in lifetimes {
1348 try!(self.word_space(","))
1350 try!(self.print_lifetime_def(lifetime_def));
1353 try!(word(&mut self.s, ">"));
1358 fn print_poly_trait_ref(&mut self, t: &ast::PolyTraitRef) -> io::Result<()> {
1359 try!(self.print_formal_lifetime_list(&t.bound_lifetimes));
1360 self.print_trait_ref(&t.trait_ref)
1363 pub fn print_enum_def(&mut self, enum_definition: &ast::EnumDef,
1364 generics: &ast::Generics, ident: ast::Ident,
1365 span: codemap::Span,
1366 visibility: ast::Visibility) -> io::Result<()> {
1367 try!(self.head(&visibility_qualified(visibility, "enum")));
1368 try!(self.print_ident(ident));
1369 try!(self.print_generics(generics));
1370 try!(self.print_where_clause(&generics.where_clause));
1371 try!(space(&mut self.s));
1372 self.print_variants(&enum_definition.variants, span)
1375 pub fn print_variants(&mut self,
1376 variants: &[P<ast::Variant>],
1377 span: codemap::Span) -> io::Result<()> {
1380 try!(self.space_if_not_bol());
1381 try!(self.maybe_print_comment(v.span.lo));
1382 try!(self.print_outer_attributes(&v.node.attrs));
1383 try!(self.ibox(indent_unit));
1384 try!(self.print_variant(&**v));
1385 try!(word(&mut self.s, ","));
1387 try!(self.maybe_print_trailing_comment(v.span, None));
1392 pub fn print_visibility(&mut self, vis: ast::Visibility) -> io::Result<()> {
1394 ast::Public => self.word_nbsp("pub"),
1395 ast::Inherited => Ok(())
1399 pub fn print_struct(&mut self,
1400 struct_def: &ast::VariantData,
1401 generics: &ast::Generics,
1403 span: codemap::Span,
1404 print_finalizer: bool) -> io::Result<()> {
1405 try!(self.print_ident(ident));
1406 try!(self.print_generics(generics));
1407 if !struct_def.is_struct() {
1408 if struct_def.is_tuple() {
1410 try!(self.commasep_iter(
1411 Inconsistent, struct_def.fields(),
1413 match field.node.kind {
1414 ast::NamedField(..) => panic!("unexpected named field"),
1415 ast::UnnamedField(vis) => {
1416 try!(s.print_visibility(vis));
1417 try!(s.maybe_print_comment(field.span.lo));
1418 s.print_type(&*field.node.ty)
1423 try!(self.pclose());
1425 try!(self.print_where_clause(&generics.where_clause));
1426 if print_finalizer {
1427 try!(word(&mut self.s, ";"));
1430 self.end() // close the outer-box
1432 try!(self.print_where_clause(&generics.where_clause));
1435 try!(self.hardbreak_if_not_bol());
1437 for field in struct_def.fields() {
1438 match field.node.kind {
1439 ast::UnnamedField(..) => panic!("unexpected unnamed field"),
1440 ast::NamedField(ident, visibility) => {
1441 try!(self.hardbreak_if_not_bol());
1442 try!(self.maybe_print_comment(field.span.lo));
1443 try!(self.print_outer_attributes(&field.node.attrs));
1444 try!(self.print_visibility(visibility));
1445 try!(self.print_ident(ident));
1446 try!(self.word_nbsp(":"));
1447 try!(self.print_type(&*field.node.ty));
1448 try!(word(&mut self.s, ","));
1457 /// This doesn't deserve to be called "pretty" printing, but it should be
1458 /// meaning-preserving. A quick hack that might help would be to look at the
1459 /// spans embedded in the TTs to decide where to put spaces and newlines.
1460 /// But it'd be better to parse these according to the grammar of the
1461 /// appropriate macro, transcribe back into the grammar we just parsed from,
1462 /// and then pretty-print the resulting AST nodes (so, e.g., we print
1463 /// expression arguments as expressions). It can be done! I think.
1464 pub fn print_tt(&mut self, tt: &ast::TokenTree) -> io::Result<()> {
1466 ast::TtToken(_, ref tk) => {
1467 try!(word(&mut self.s, &token_to_string(tk)));
1469 parse::token::DocComment(..) => {
1470 hardbreak(&mut self.s)
1475 ast::TtDelimited(_, ref delimed) => {
1476 try!(word(&mut self.s, &token_to_string(&delimed.open_token())));
1477 try!(space(&mut self.s));
1478 try!(self.print_tts(&delimed.tts));
1479 try!(space(&mut self.s));
1480 word(&mut self.s, &token_to_string(&delimed.close_token()))
1482 ast::TtSequence(_, ref seq) => {
1483 try!(word(&mut self.s, "$("));
1484 for tt_elt in &seq.tts {
1485 try!(self.print_tt(tt_elt));
1487 try!(word(&mut self.s, ")"));
1488 match seq.separator {
1490 try!(word(&mut self.s, &token_to_string(tk)));
1495 ast::ZeroOrMore => word(&mut self.s, "*"),
1496 ast::OneOrMore => word(&mut self.s, "+"),
1502 pub fn print_tts(&mut self, tts: &[ast::TokenTree]) -> io::Result<()> {
1504 let mut suppress_space = false;
1505 for (i, tt) in tts.iter().enumerate() {
1506 if i != 0 && !suppress_space {
1507 try!(space(&mut self.s));
1509 try!(self.print_tt(tt));
1510 // There should be no space between the module name and the following `::` in paths,
1511 // otherwise imported macros get re-parsed from crate metadata incorrectly (#20701)
1512 suppress_space = match tt {
1513 &ast::TtToken(_, token::Ident(_, token::ModName)) |
1514 &ast::TtToken(_, token::MatchNt(_, _, _, token::ModName)) |
1515 &ast::TtToken(_, token::SubstNt(_, token::ModName)) => true,
1522 pub fn print_variant(&mut self, v: &ast::Variant) -> io::Result<()> {
1523 try!(self.head(""));
1524 let generics = ast_util::empty_generics();
1525 try!(self.print_struct(&v.node.data, &generics, v.node.name, v.span, false));
1526 match v.node.disr_expr {
1528 try!(space(&mut self.s));
1529 try!(self.word_space("="));
1530 self.print_expr(&**d)
1536 pub fn print_method_sig(&mut self,
1539 vis: ast::Visibility)
1541 self.print_fn(&m.decl,
1547 Some(&m.explicit_self.node),
1551 pub fn print_trait_item(&mut self, ti: &ast::TraitItem)
1553 try!(self.ann.pre(self, NodeSubItem(ti.id)));
1554 try!(self.hardbreak_if_not_bol());
1555 try!(self.maybe_print_comment(ti.span.lo));
1556 try!(self.print_outer_attributes(&ti.attrs));
1558 ast::ConstTraitItem(ref ty, ref default) => {
1559 try!(self.print_associated_const(ti.ident, &ty,
1560 default.as_ref().map(|expr| &**expr),
1563 ast::MethodTraitItem(ref sig, ref body) => {
1565 try!(self.head(""));
1567 try!(self.print_method_sig(ti.ident, sig, ast::Inherited));
1568 if let Some(ref body) = *body {
1570 try!(self.print_block_with_attrs(body, &ti.attrs));
1572 try!(word(&mut self.s, ";"));
1575 ast::TypeTraitItem(ref bounds, ref default) => {
1576 try!(self.print_associated_type(ti.ident, Some(bounds),
1577 default.as_ref().map(|ty| &**ty)));
1580 self.ann.post(self, NodeSubItem(ti.id))
1583 pub fn print_impl_item(&mut self, ii: &ast::ImplItem) -> io::Result<()> {
1584 try!(self.ann.pre(self, NodeSubItem(ii.id)));
1585 try!(self.hardbreak_if_not_bol());
1586 try!(self.maybe_print_comment(ii.span.lo));
1587 try!(self.print_outer_attributes(&ii.attrs));
1589 ast::ConstImplItem(ref ty, ref expr) => {
1590 try!(self.print_associated_const(ii.ident, &ty, Some(&expr), ii.vis));
1592 ast::MethodImplItem(ref sig, ref body) => {
1593 try!(self.head(""));
1594 try!(self.print_method_sig(ii.ident, sig, ii.vis));
1596 try!(self.print_block_with_attrs(body, &ii.attrs));
1598 ast::TypeImplItem(ref ty) => {
1599 try!(self.print_associated_type(ii.ident, None, Some(ty)));
1601 ast::MacImplItem(codemap::Spanned { ref node, .. }) => {
1602 // code copied from ItemMac:
1603 try!(self.print_path(&node.path, false, 0));
1604 try!(word(&mut self.s, "! "));
1605 try!(self.cbox(indent_unit));
1607 try!(self.print_tts(&node.tts[..]));
1608 try!(self.pclose());
1609 try!(word(&mut self.s, ";"));
1613 self.ann.post(self, NodeSubItem(ii.id))
1616 pub fn print_stmt(&mut self, st: &ast::Stmt) -> io::Result<()> {
1617 try!(self.maybe_print_comment(st.span.lo));
1619 ast::StmtDecl(ref decl, _) => {
1620 try!(self.print_decl(&**decl));
1622 ast::StmtExpr(ref expr, _) => {
1623 try!(self.space_if_not_bol());
1624 try!(self.print_expr(&**expr));
1626 ast::StmtSemi(ref expr, _) => {
1627 try!(self.space_if_not_bol());
1628 try!(self.print_expr(&**expr));
1629 try!(word(&mut self.s, ";"));
1631 ast::StmtMac(ref mac, style) => {
1632 try!(self.space_if_not_bol());
1633 let delim = match style {
1634 ast::MacStmtWithBraces => token::Brace,
1637 try!(self.print_mac(&**mac, delim));
1639 ast::MacStmtWithBraces => {}
1640 _ => try!(word(&mut self.s, ";")),
1644 if parse::classify::stmt_ends_with_semi(&st.node) {
1645 try!(word(&mut self.s, ";"));
1647 self.maybe_print_trailing_comment(st.span, None)
1650 pub fn print_block(&mut self, blk: &ast::Block) -> io::Result<()> {
1651 self.print_block_with_attrs(blk, &[])
1654 pub fn print_block_unclosed(&mut self, blk: &ast::Block) -> io::Result<()> {
1655 self.print_block_unclosed_indent(blk, indent_unit)
1658 pub fn print_block_unclosed_indent(&mut self, blk: &ast::Block,
1659 indented: usize) -> io::Result<()> {
1660 self.print_block_maybe_unclosed(blk, indented, &[], false)
1663 pub fn print_block_with_attrs(&mut self,
1665 attrs: &[ast::Attribute]) -> io::Result<()> {
1666 self.print_block_maybe_unclosed(blk, indent_unit, attrs, true)
1669 pub fn print_block_maybe_unclosed(&mut self,
1672 attrs: &[ast::Attribute],
1673 close_box: bool) -> io::Result<()> {
1675 ast::UnsafeBlock(..) | ast::PushUnsafeBlock(..) => try!(self.word_space("unsafe")),
1676 ast::DefaultBlock | ast::PopUnsafeBlock(..) => ()
1678 try!(self.maybe_print_comment(blk.span.lo));
1679 try!(self.ann.pre(self, NodeBlock(blk)));
1682 try!(self.print_inner_attributes(attrs));
1684 for st in &blk.stmts {
1685 try!(self.print_stmt(&**st));
1689 try!(self.space_if_not_bol());
1690 try!(self.print_expr(&**expr));
1691 try!(self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi)));
1695 try!(self.bclose_maybe_open(blk.span, indented, close_box));
1696 self.ann.post(self, NodeBlock(blk))
1699 fn print_else(&mut self, els: Option<&ast::Expr>) -> io::Result<()> {
1703 // "another else-if"
1704 ast::ExprIf(ref i, ref then, ref e) => {
1705 try!(self.cbox(indent_unit - 1));
1707 try!(word(&mut self.s, " else if "));
1708 try!(self.print_expr(&**i));
1709 try!(space(&mut self.s));
1710 try!(self.print_block(&**then));
1711 self.print_else(e.as_ref().map(|e| &**e))
1713 // "another else-if-let"
1714 ast::ExprIfLet(ref pat, ref expr, ref then, ref e) => {
1715 try!(self.cbox(indent_unit - 1));
1717 try!(word(&mut self.s, " else if let "));
1718 try!(self.print_pat(&**pat));
1719 try!(space(&mut self.s));
1720 try!(self.word_space("="));
1721 try!(self.print_expr(&**expr));
1722 try!(space(&mut self.s));
1723 try!(self.print_block(&**then));
1724 self.print_else(e.as_ref().map(|e| &**e))
1727 ast::ExprBlock(ref b) => {
1728 try!(self.cbox(indent_unit - 1));
1730 try!(word(&mut self.s, " else "));
1731 self.print_block(&**b)
1733 // BLEAH, constraints would be great here
1735 panic!("print_if saw if with weird alternative");
1743 pub fn print_if(&mut self, test: &ast::Expr, blk: &ast::Block,
1744 elseopt: Option<&ast::Expr>) -> io::Result<()> {
1745 try!(self.head("if"));
1746 try!(self.print_expr(test));
1747 try!(space(&mut self.s));
1748 try!(self.print_block(blk));
1749 self.print_else(elseopt)
1752 pub fn print_if_let(&mut self, pat: &ast::Pat, expr: &ast::Expr, blk: &ast::Block,
1753 elseopt: Option<&ast::Expr>) -> io::Result<()> {
1754 try!(self.head("if let"));
1755 try!(self.print_pat(pat));
1756 try!(space(&mut self.s));
1757 try!(self.word_space("="));
1758 try!(self.print_expr(expr));
1759 try!(space(&mut self.s));
1760 try!(self.print_block(blk));
1761 self.print_else(elseopt)
1764 pub fn print_mac(&mut self, m: &ast::Mac, delim: token::DelimToken)
1766 try!(self.print_path(&m.node.path, false, 0));
1767 try!(word(&mut self.s, "!"));
1769 token::Paren => try!(self.popen()),
1770 token::Bracket => try!(word(&mut self.s, "[")),
1771 token::Brace => try!(self.bopen()),
1773 try!(self.print_tts(&m.node.tts));
1775 token::Paren => self.pclose(),
1776 token::Bracket => word(&mut self.s, "]"),
1777 token::Brace => self.bclose(m.span),
1782 fn print_call_post(&mut self, args: &[P<ast::Expr>]) -> io::Result<()> {
1784 try!(self.commasep_exprs(Inconsistent, args));
1788 pub fn check_expr_bin_needs_paren(&mut self, sub_expr: &ast::Expr,
1789 binop: ast::BinOp) -> bool {
1790 match sub_expr.node {
1791 ast::ExprBinary(ref sub_op, _, _) => {
1792 if ast_util::operator_prec(sub_op.node) <
1793 ast_util::operator_prec(binop.node) {
1803 pub fn print_expr_maybe_paren(&mut self, expr: &ast::Expr) -> io::Result<()> {
1804 let needs_par = needs_parentheses(expr);
1808 try!(self.print_expr(expr));
1810 try!(self.pclose());
1815 fn print_expr_in_place(&mut self,
1817 expr: &ast::Expr) -> io::Result<()> {
1818 try!(self.word_space("in"));
1819 try!(self.print_expr(place));
1820 try!(space(&mut self.s));
1821 self.print_expr(expr)
1824 fn print_expr_vec(&mut self, exprs: &[P<ast::Expr>]) -> io::Result<()> {
1825 try!(self.ibox(indent_unit));
1826 try!(word(&mut self.s, "["));
1827 try!(self.commasep_exprs(Inconsistent, &exprs[..]));
1828 try!(word(&mut self.s, "]"));
1832 fn print_expr_repeat(&mut self,
1833 element: &ast::Expr,
1834 count: &ast::Expr) -> io::Result<()> {
1835 try!(self.ibox(indent_unit));
1836 try!(word(&mut self.s, "["));
1837 try!(self.print_expr(element));
1838 try!(self.word_space(";"));
1839 try!(self.print_expr(count));
1840 try!(word(&mut self.s, "]"));
1844 fn print_expr_struct(&mut self,
1846 fields: &[ast::Field],
1847 wth: &Option<P<ast::Expr>>) -> io::Result<()> {
1848 try!(self.print_path(path, true, 0));
1849 try!(word(&mut self.s, "{"));
1850 try!(self.commasep_cmnt(
1854 try!(s.ibox(indent_unit));
1855 try!(s.print_ident(field.ident.node));
1856 try!(s.word_space(":"));
1857 try!(s.print_expr(&*field.expr));
1863 try!(self.ibox(indent_unit));
1864 if !fields.is_empty() {
1865 try!(word(&mut self.s, ","));
1866 try!(space(&mut self.s));
1868 try!(word(&mut self.s, ".."));
1869 try!(self.print_expr(&**expr));
1872 _ => if !fields.is_empty() {
1873 try!(word(&mut self.s, ","))
1876 try!(word(&mut self.s, "}"));
1880 fn print_expr_tup(&mut self, exprs: &[P<ast::Expr>]) -> io::Result<()> {
1882 try!(self.commasep_exprs(Inconsistent, &exprs[..]));
1883 if exprs.len() == 1 {
1884 try!(word(&mut self.s, ","));
1889 fn print_expr_call(&mut self,
1891 args: &[P<ast::Expr>]) -> io::Result<()> {
1892 try!(self.print_expr_maybe_paren(func));
1893 self.print_call_post(args)
1896 fn print_expr_method_call(&mut self,
1897 ident: ast::SpannedIdent,
1899 args: &[P<ast::Expr>]) -> io::Result<()> {
1900 let base_args = &args[1..];
1901 try!(self.print_expr(&*args[0]));
1902 try!(word(&mut self.s, "."));
1903 try!(self.print_ident(ident.node));
1904 if !tys.is_empty() {
1905 try!(word(&mut self.s, "::<"));
1906 try!(self.commasep(Inconsistent, tys,
1907 |s, ty| s.print_type(&**ty)));
1908 try!(word(&mut self.s, ">"));
1910 self.print_call_post(base_args)
1913 fn print_expr_binary(&mut self,
1916 rhs: &ast::Expr) -> io::Result<()> {
1917 if self.check_expr_bin_needs_paren(lhs, op) {
1918 try!(self.print_expr_maybe_paren(lhs));
1920 try!(self.print_expr(lhs));
1922 try!(space(&mut self.s));
1923 try!(self.word_space(ast_util::binop_to_string(op.node)));
1924 if self.check_expr_bin_needs_paren(rhs, op) {
1925 self.print_expr_maybe_paren(rhs)
1927 self.print_expr(rhs)
1931 fn print_expr_unary(&mut self,
1933 expr: &ast::Expr) -> io::Result<()> {
1934 try!(word(&mut self.s, ast_util::unop_to_string(op)));
1935 self.print_expr_maybe_paren(expr)
1938 fn print_expr_addr_of(&mut self,
1939 mutability: ast::Mutability,
1940 expr: &ast::Expr) -> io::Result<()> {
1941 try!(word(&mut self.s, "&"));
1942 try!(self.print_mutability(mutability));
1943 self.print_expr_maybe_paren(expr)
1946 pub fn print_expr(&mut self, expr: &ast::Expr) -> io::Result<()> {
1947 try!(self.maybe_print_comment(expr.span.lo));
1948 try!(self.ibox(indent_unit));
1949 try!(self.ann.pre(self, NodeExpr(expr)));
1951 ast::ExprBox(ref expr) => {
1952 try!(self.word_space("box"));
1953 try!(self.print_expr(expr));
1955 ast::ExprInPlace(ref place, ref expr) => {
1956 try!(self.print_expr_in_place(place, expr));
1958 ast::ExprVec(ref exprs) => {
1959 try!(self.print_expr_vec(&exprs[..]));
1961 ast::ExprRepeat(ref element, ref count) => {
1962 try!(self.print_expr_repeat(&**element, &**count));
1964 ast::ExprStruct(ref path, ref fields, ref wth) => {
1965 try!(self.print_expr_struct(path, &fields[..], wth));
1967 ast::ExprTup(ref exprs) => {
1968 try!(self.print_expr_tup(&exprs[..]));
1970 ast::ExprCall(ref func, ref args) => {
1971 try!(self.print_expr_call(&**func, &args[..]));
1973 ast::ExprMethodCall(ident, ref tys, ref args) => {
1974 try!(self.print_expr_method_call(ident, &tys[..], &args[..]));
1976 ast::ExprBinary(op, ref lhs, ref rhs) => {
1977 try!(self.print_expr_binary(op, &**lhs, &**rhs));
1979 ast::ExprUnary(op, ref expr) => {
1980 try!(self.print_expr_unary(op, &**expr));
1982 ast::ExprAddrOf(m, ref expr) => {
1983 try!(self.print_expr_addr_of(m, &**expr));
1985 ast::ExprLit(ref lit) => {
1986 try!(self.print_literal(&**lit));
1988 ast::ExprCast(ref expr, ref ty) => {
1989 if let ast::ExprCast(..) = expr.node {
1990 try!(self.print_expr(&**expr));
1992 try!(self.print_expr_maybe_paren(&**expr));
1994 try!(space(&mut self.s));
1995 try!(self.word_space("as"));
1996 try!(self.print_type(&**ty));
1998 ast::ExprIf(ref test, ref blk, ref elseopt) => {
1999 try!(self.print_if(&**test, &**blk, elseopt.as_ref().map(|e| &**e)));
2001 ast::ExprIfLet(ref pat, ref expr, ref blk, ref elseopt) => {
2002 try!(self.print_if_let(&**pat, &**expr, &** blk, elseopt.as_ref().map(|e| &**e)));
2004 ast::ExprWhile(ref test, ref blk, opt_ident) => {
2005 if let Some(ident) = opt_ident {
2006 try!(self.print_ident(ident));
2007 try!(self.word_space(":"));
2009 try!(self.head("while"));
2010 try!(self.print_expr(&**test));
2011 try!(space(&mut self.s));
2012 try!(self.print_block(&**blk));
2014 ast::ExprWhileLet(ref pat, ref expr, ref blk, opt_ident) => {
2015 if let Some(ident) = opt_ident {
2016 try!(self.print_ident(ident));
2017 try!(self.word_space(":"));
2019 try!(self.head("while let"));
2020 try!(self.print_pat(&**pat));
2021 try!(space(&mut self.s));
2022 try!(self.word_space("="));
2023 try!(self.print_expr(&**expr));
2024 try!(space(&mut self.s));
2025 try!(self.print_block(&**blk));
2027 ast::ExprForLoop(ref pat, ref iter, ref blk, opt_ident) => {
2028 if let Some(ident) = opt_ident {
2029 try!(self.print_ident(ident));
2030 try!(self.word_space(":"));
2032 try!(self.head("for"));
2033 try!(self.print_pat(&**pat));
2034 try!(space(&mut self.s));
2035 try!(self.word_space("in"));
2036 try!(self.print_expr(&**iter));
2037 try!(space(&mut self.s));
2038 try!(self.print_block(&**blk));
2040 ast::ExprLoop(ref blk, opt_ident) => {
2041 if let Some(ident) = opt_ident {
2042 try!(self.print_ident(ident));
2043 try!(self.word_space(":"));
2045 try!(self.head("loop"));
2046 try!(space(&mut self.s));
2047 try!(self.print_block(&**blk));
2049 ast::ExprMatch(ref expr, ref arms) => {
2050 try!(self.cbox(indent_unit));
2052 try!(self.word_nbsp("match"));
2053 try!(self.print_expr(&**expr));
2054 try!(space(&mut self.s));
2057 try!(self.print_arm(arm));
2059 try!(self.bclose_(expr.span, indent_unit));
2061 ast::ExprClosure(capture_clause, ref decl, ref body) => {
2062 try!(self.print_capture_clause(capture_clause));
2064 try!(self.print_fn_block_args(&**decl));
2065 try!(space(&mut self.s));
2067 let default_return = match decl.output {
2068 ast::DefaultReturn(..) => true,
2072 if !default_return || !body.stmts.is_empty() || body.expr.is_none() {
2073 try!(self.print_block_unclosed(&**body));
2075 // we extract the block, so as not to create another set of boxes
2076 match body.expr.as_ref().unwrap().node {
2077 ast::ExprBlock(ref blk) => {
2078 try!(self.print_block_unclosed(&**blk));
2081 // this is a bare expression
2082 try!(self.print_expr(body.expr.as_ref().map(|e| &**e).unwrap()));
2083 try!(self.end()); // need to close a box
2087 // a box will be closed by print_expr, but we didn't want an overall
2088 // wrapper so we closed the corresponding opening. so create an
2089 // empty box to satisfy the close.
2092 ast::ExprBlock(ref blk) => {
2093 // containing cbox, will be closed by print-block at }
2094 try!(self.cbox(indent_unit));
2095 // head-box, will be closed by print-block after {
2097 try!(self.print_block(&**blk));
2099 ast::ExprAssign(ref lhs, ref rhs) => {
2100 try!(self.print_expr(&**lhs));
2101 try!(space(&mut self.s));
2102 try!(self.word_space("="));
2103 try!(self.print_expr(&**rhs));
2105 ast::ExprAssignOp(op, ref lhs, ref rhs) => {
2106 try!(self.print_expr(&**lhs));
2107 try!(space(&mut self.s));
2108 try!(word(&mut self.s, ast_util::binop_to_string(op.node)));
2109 try!(self.word_space("="));
2110 try!(self.print_expr(&**rhs));
2112 ast::ExprField(ref expr, id) => {
2113 try!(self.print_expr(&**expr));
2114 try!(word(&mut self.s, "."));
2115 try!(self.print_ident(id.node));
2117 ast::ExprTupField(ref expr, id) => {
2118 try!(self.print_expr(&**expr));
2119 try!(word(&mut self.s, "."));
2120 try!(self.print_usize(id.node));
2122 ast::ExprIndex(ref expr, ref index) => {
2123 try!(self.print_expr(&**expr));
2124 try!(word(&mut self.s, "["));
2125 try!(self.print_expr(&**index));
2126 try!(word(&mut self.s, "]"));
2128 ast::ExprRange(ref start, ref end) => {
2129 if let &Some(ref e) = start {
2130 try!(self.print_expr(&**e));
2132 try!(word(&mut self.s, ".."));
2133 if let &Some(ref e) = end {
2134 try!(self.print_expr(&**e));
2137 ast::ExprPath(None, ref path) => {
2138 try!(self.print_path(path, true, 0))
2140 ast::ExprPath(Some(ref qself), ref path) => {
2141 try!(self.print_qpath(path, qself, true))
2143 ast::ExprBreak(opt_ident) => {
2144 try!(word(&mut self.s, "break"));
2145 try!(space(&mut self.s));
2146 if let Some(ident) = opt_ident {
2147 try!(self.print_ident(ident.node));
2148 try!(space(&mut self.s));
2151 ast::ExprAgain(opt_ident) => {
2152 try!(word(&mut self.s, "continue"));
2153 try!(space(&mut self.s));
2154 if let Some(ident) = opt_ident {
2155 try!(self.print_ident(ident.node));
2156 try!(space(&mut self.s))
2159 ast::ExprRet(ref result) => {
2160 try!(word(&mut self.s, "return"));
2163 try!(word(&mut self.s, " "));
2164 try!(self.print_expr(&**expr));
2169 ast::ExprInlineAsm(ref a) => {
2170 try!(word(&mut self.s, "asm!"));
2172 try!(self.print_string(&a.asm, a.asm_str_style));
2173 try!(self.word_space(":"));
2175 try!(self.commasep(Inconsistent, &a.outputs,
2176 |s, &(ref co, ref o, is_rw)| {
2177 match co.slice_shift_char() {
2178 Some(('=', operand)) if is_rw => {
2179 try!(s.print_string(&format!("+{}", operand),
2182 _ => try!(s.print_string(&co, ast::CookedStr))
2185 try!(s.print_expr(&**o));
2189 try!(space(&mut self.s));
2190 try!(self.word_space(":"));
2192 try!(self.commasep(Inconsistent, &a.inputs,
2193 |s, &(ref co, ref o)| {
2194 try!(s.print_string(&co, ast::CookedStr));
2196 try!(s.print_expr(&**o));
2200 try!(space(&mut self.s));
2201 try!(self.word_space(":"));
2203 try!(self.commasep(Inconsistent, &a.clobbers,
2205 try!(s.print_string(&co, ast::CookedStr));
2209 let mut options = vec!();
2211 options.push("volatile");
2214 options.push("alignstack");
2216 if a.dialect == ast::AsmDialect::Intel {
2217 options.push("intel");
2220 if !options.is_empty() {
2221 try!(space(&mut self.s));
2222 try!(self.word_space(":"));
2223 try!(self.commasep(Inconsistent, &*options,
2225 try!(s.print_string(co, ast::CookedStr));
2230 try!(self.pclose());
2232 ast::ExprMac(ref m) => try!(self.print_mac(m, token::Paren)),
2233 ast::ExprParen(ref e) => {
2235 try!(self.print_expr(&**e));
2236 try!(self.pclose());
2239 try!(self.ann.post(self, NodeExpr(expr)));
2243 pub fn print_local_decl(&mut self, loc: &ast::Local) -> io::Result<()> {
2244 try!(self.print_pat(&*loc.pat));
2245 if let Some(ref ty) = loc.ty {
2246 try!(self.word_space(":"));
2247 try!(self.print_type(&**ty));
2252 pub fn print_decl(&mut self, decl: &ast::Decl) -> io::Result<()> {
2253 try!(self.maybe_print_comment(decl.span.lo));
2255 ast::DeclLocal(ref loc) => {
2256 try!(self.space_if_not_bol());
2257 try!(self.ibox(indent_unit));
2258 try!(self.word_nbsp("let"));
2260 try!(self.ibox(indent_unit));
2261 try!(self.print_local_decl(&**loc));
2263 if let Some(ref init) = loc.init {
2265 try!(self.word_space("="));
2266 try!(self.print_expr(&**init));
2270 ast::DeclItem(ref item) => self.print_item(&**item)
2274 pub fn print_ident(&mut self, ident: ast::Ident) -> io::Result<()> {
2275 try!(word(&mut self.s, &ident.name.as_str()));
2276 self.ann.post(self, NodeIdent(&ident))
2279 pub fn print_usize(&mut self, i: usize) -> io::Result<()> {
2280 word(&mut self.s, &i.to_string())
2283 pub fn print_name(&mut self, name: ast::Name) -> io::Result<()> {
2284 try!(word(&mut self.s, &name.as_str()));
2285 self.ann.post(self, NodeName(&name))
2288 pub fn print_for_decl(&mut self, loc: &ast::Local,
2289 coll: &ast::Expr) -> io::Result<()> {
2290 try!(self.print_local_decl(loc));
2291 try!(space(&mut self.s));
2292 try!(self.word_space("in"));
2293 self.print_expr(coll)
2296 fn print_path(&mut self,
2298 colons_before_params: bool,
2302 try!(self.maybe_print_comment(path.span.lo));
2304 let mut first = !path.global;
2305 for segment in &path.segments[..path.segments.len()-depth] {
2309 try!(word(&mut self.s, "::"))
2312 try!(self.print_ident(segment.identifier));
2314 try!(self.print_path_parameters(&segment.parameters, colons_before_params));
2320 fn print_qpath(&mut self,
2323 colons_before_params: bool)
2326 try!(word(&mut self.s, "<"));
2327 try!(self.print_type(&qself.ty));
2328 if qself.position > 0 {
2329 try!(space(&mut self.s));
2330 try!(self.word_space("as"));
2331 let depth = path.segments.len() - qself.position;
2332 try!(self.print_path(&path, false, depth));
2334 try!(word(&mut self.s, ">"));
2335 try!(word(&mut self.s, "::"));
2336 let item_segment = path.segments.last().unwrap();
2337 try!(self.print_ident(item_segment.identifier));
2338 self.print_path_parameters(&item_segment.parameters, colons_before_params)
2341 fn print_path_parameters(&mut self,
2342 parameters: &ast::PathParameters,
2343 colons_before_params: bool)
2346 if parameters.is_empty() {
2350 if colons_before_params {
2351 try!(word(&mut self.s, "::"))
2355 ast::AngleBracketedParameters(ref data) => {
2356 try!(word(&mut self.s, "<"));
2358 let mut comma = false;
2359 for lifetime in &data.lifetimes {
2361 try!(self.word_space(","))
2363 try!(self.print_lifetime(lifetime));
2367 if !data.types.is_empty() {
2369 try!(self.word_space(","))
2374 |s, ty| s.print_type(&**ty)));
2378 for binding in data.bindings.iter() {
2380 try!(self.word_space(","))
2382 try!(self.print_ident(binding.ident));
2383 try!(space(&mut self.s));
2384 try!(self.word_space("="));
2385 try!(self.print_type(&*binding.ty));
2389 try!(word(&mut self.s, ">"))
2392 ast::ParenthesizedParameters(ref data) => {
2393 try!(word(&mut self.s, "("));
2397 |s, ty| s.print_type(&**ty)));
2398 try!(word(&mut self.s, ")"));
2403 try!(self.space_if_not_bol());
2404 try!(self.word_space("->"));
2405 try!(self.print_type(&**ty));
2414 pub fn print_pat(&mut self, pat: &ast::Pat) -> io::Result<()> {
2415 try!(self.maybe_print_comment(pat.span.lo));
2416 try!(self.ann.pre(self, NodePat(pat)));
2417 /* Pat isn't normalized, but the beauty of it
2418 is that it doesn't matter */
2420 ast::PatWild(ast::PatWildSingle) => try!(word(&mut self.s, "_")),
2421 ast::PatWild(ast::PatWildMulti) => try!(word(&mut self.s, "..")),
2422 ast::PatIdent(binding_mode, ref path1, ref sub) => {
2423 match binding_mode {
2424 ast::BindByRef(mutbl) => {
2425 try!(self.word_nbsp("ref"));
2426 try!(self.print_mutability(mutbl));
2428 ast::BindByValue(ast::MutImmutable) => {}
2429 ast::BindByValue(ast::MutMutable) => {
2430 try!(self.word_nbsp("mut"));
2433 try!(self.print_ident(path1.node));
2436 try!(word(&mut self.s, "@"));
2437 try!(self.print_pat(&**p));
2442 ast::PatEnum(ref path, ref args_) => {
2443 try!(self.print_path(path, true, 0));
2445 None => try!(word(&mut self.s, "(..)")),
2447 if !args.is_empty() {
2449 try!(self.commasep(Inconsistent, &args[..],
2450 |s, p| s.print_pat(&**p)));
2451 try!(self.pclose());
2456 ast::PatQPath(ref qself, ref path) => {
2457 try!(self.print_qpath(path, qself, false));
2459 ast::PatStruct(ref path, ref fields, etc) => {
2460 try!(self.print_path(path, true, 0));
2462 try!(self.word_space("{"));
2463 try!(self.commasep_cmnt(
2464 Consistent, &fields[..],
2466 try!(s.cbox(indent_unit));
2467 if !f.node.is_shorthand {
2468 try!(s.print_ident(f.node.ident));
2469 try!(s.word_nbsp(":"));
2471 try!(s.print_pat(&*f.node.pat));
2474 |f| f.node.pat.span));
2476 if !fields.is_empty() { try!(self.word_space(",")); }
2477 try!(word(&mut self.s, ".."));
2479 try!(space(&mut self.s));
2480 try!(word(&mut self.s, "}"));
2482 ast::PatTup(ref elts) => {
2484 try!(self.commasep(Inconsistent,
2486 |s, p| s.print_pat(&**p)));
2487 if elts.len() == 1 {
2488 try!(word(&mut self.s, ","));
2490 try!(self.pclose());
2492 ast::PatBox(ref inner) => {
2493 try!(word(&mut self.s, "box "));
2494 try!(self.print_pat(&**inner));
2496 ast::PatRegion(ref inner, mutbl) => {
2497 try!(word(&mut self.s, "&"));
2498 if mutbl == ast::MutMutable {
2499 try!(word(&mut self.s, "mut "));
2501 try!(self.print_pat(&**inner));
2503 ast::PatLit(ref e) => try!(self.print_expr(&**e)),
2504 ast::PatRange(ref begin, ref end) => {
2505 try!(self.print_expr(&**begin));
2506 try!(space(&mut self.s));
2507 try!(word(&mut self.s, "..."));
2508 try!(self.print_expr(&**end));
2510 ast::PatVec(ref before, ref slice, ref after) => {
2511 try!(word(&mut self.s, "["));
2512 try!(self.commasep(Inconsistent,
2514 |s, p| s.print_pat(&**p)));
2515 if let Some(ref p) = *slice {
2516 if !before.is_empty() { try!(self.word_space(",")); }
2517 try!(self.print_pat(&**p));
2519 ast::Pat { node: ast::PatWild(ast::PatWildMulti), .. } => {
2520 // this case is handled by print_pat
2522 _ => try!(word(&mut self.s, "..")),
2524 if !after.is_empty() { try!(self.word_space(",")); }
2526 try!(self.commasep(Inconsistent,
2528 |s, p| s.print_pat(&**p)));
2529 try!(word(&mut self.s, "]"));
2531 ast::PatMac(ref m) => try!(self.print_mac(m, token::Paren)),
2533 self.ann.post(self, NodePat(pat))
2536 fn print_arm(&mut self, arm: &ast::Arm) -> io::Result<()> {
2537 // I have no idea why this check is necessary, but here it
2539 if arm.attrs.is_empty() {
2540 try!(space(&mut self.s));
2542 try!(self.cbox(indent_unit));
2544 try!(self.print_outer_attributes(&arm.attrs));
2545 let mut first = true;
2546 for p in &arm.pats {
2550 try!(space(&mut self.s));
2551 try!(self.word_space("|"));
2553 try!(self.print_pat(&**p));
2555 try!(space(&mut self.s));
2556 if let Some(ref e) = arm.guard {
2557 try!(self.word_space("if"));
2558 try!(self.print_expr(&**e));
2559 try!(space(&mut self.s));
2561 try!(self.word_space("=>"));
2563 match arm.body.node {
2564 ast::ExprBlock(ref blk) => {
2565 // the block will close the pattern's ibox
2566 try!(self.print_block_unclosed_indent(&**blk, indent_unit));
2568 // If it is a user-provided unsafe block, print a comma after it
2569 if let ast::UnsafeBlock(ast::UserProvided) = blk.rules {
2570 try!(word(&mut self.s, ","));
2574 try!(self.end()); // close the ibox for the pattern
2575 try!(self.print_expr(&*arm.body));
2576 try!(word(&mut self.s, ","));
2579 self.end() // close enclosing cbox
2582 // Returns whether it printed anything
2583 fn print_explicit_self(&mut self,
2584 explicit_self: &ast::ExplicitSelf_,
2585 mutbl: ast::Mutability) -> io::Result<bool> {
2586 try!(self.print_mutability(mutbl));
2587 match *explicit_self {
2588 ast::SelfStatic => { return Ok(false); }
2589 ast::SelfValue(_) => {
2590 try!(word(&mut self.s, "self"));
2592 ast::SelfRegion(ref lt, m, _) => {
2593 try!(word(&mut self.s, "&"));
2594 try!(self.print_opt_lifetime(lt));
2595 try!(self.print_mutability(m));
2596 try!(word(&mut self.s, "self"));
2598 ast::SelfExplicit(ref typ, _) => {
2599 try!(word(&mut self.s, "self"));
2600 try!(self.word_space(":"));
2601 try!(self.print_type(&**typ));
2607 pub fn print_fn(&mut self,
2609 unsafety: ast::Unsafety,
2610 constness: ast::Constness,
2612 name: Option<ast::Ident>,
2613 generics: &ast::Generics,
2614 opt_explicit_self: Option<&ast::ExplicitSelf_>,
2615 vis: ast::Visibility) -> io::Result<()> {
2616 try!(self.print_fn_header_info(unsafety, constness, abi, vis));
2618 if let Some(name) = name {
2620 try!(self.print_ident(name));
2622 try!(self.print_generics(generics));
2623 try!(self.print_fn_args_and_ret(decl, opt_explicit_self));
2624 self.print_where_clause(&generics.where_clause)
2627 pub fn print_fn_args(&mut self, decl: &ast::FnDecl,
2628 opt_explicit_self: Option<&ast::ExplicitSelf_>)
2630 // It is unfortunate to duplicate the commasep logic, but we want the
2631 // self type and the args all in the same box.
2632 try!(self.rbox(0, Inconsistent));
2633 let mut first = true;
2634 if let Some(explicit_self) = opt_explicit_self {
2635 let m = match explicit_self {
2636 &ast::SelfStatic => ast::MutImmutable,
2637 _ => match decl.inputs[0].pat.node {
2638 ast::PatIdent(ast::BindByValue(m), _, _) => m,
2639 _ => ast::MutImmutable
2642 first = !try!(self.print_explicit_self(explicit_self, m));
2645 // HACK(eddyb) ignore the separately printed self argument.
2646 let args = if first {
2653 if first { first = false; } else { try!(self.word_space(",")); }
2654 try!(self.print_arg(arg));
2660 pub fn print_fn_args_and_ret(&mut self, decl: &ast::FnDecl,
2661 opt_explicit_self: Option<&ast::ExplicitSelf_>)
2664 try!(self.print_fn_args(decl, opt_explicit_self));
2666 try!(word(&mut self.s, ", ..."));
2668 try!(self.pclose());
2670 self.print_fn_output(decl)
2673 pub fn print_fn_block_args(
2677 try!(word(&mut self.s, "|"));
2678 try!(self.print_fn_args(decl, None));
2679 try!(word(&mut self.s, "|"));
2681 if let ast::DefaultReturn(..) = decl.output {
2685 try!(self.space_if_not_bol());
2686 try!(self.word_space("->"));
2688 ast::Return(ref ty) => {
2689 try!(self.print_type(&**ty));
2690 self.maybe_print_comment(ty.span.lo)
2692 ast::DefaultReturn(..) => unreachable!(),
2693 ast::NoReturn(span) => {
2694 try!(self.word_nbsp("!"));
2695 self.maybe_print_comment(span.lo)
2700 pub fn print_capture_clause(&mut self, capture_clause: ast::CaptureClause)
2702 match capture_clause {
2703 ast::CaptureByValue => self.word_space("move"),
2704 ast::CaptureByRef => Ok(()),
2708 pub fn print_bounds(&mut self,
2710 bounds: &[ast::TyParamBound])
2712 if !bounds.is_empty() {
2713 try!(word(&mut self.s, prefix));
2714 let mut first = true;
2715 for bound in bounds {
2720 try!(self.word_space("+"));
2724 TraitTyParamBound(ref tref, TraitBoundModifier::None) => {
2725 self.print_poly_trait_ref(tref)
2727 TraitTyParamBound(ref tref, TraitBoundModifier::Maybe) => {
2728 try!(word(&mut self.s, "?"));
2729 self.print_poly_trait_ref(tref)
2731 RegionTyParamBound(ref lt) => {
2732 self.print_lifetime(lt)
2742 pub fn print_lifetime(&mut self,
2743 lifetime: &ast::Lifetime)
2746 self.print_name(lifetime.name)
2749 pub fn print_lifetime_def(&mut self,
2750 lifetime: &ast::LifetimeDef)
2753 try!(self.print_lifetime(&lifetime.lifetime));
2755 for v in &lifetime.bounds {
2756 try!(word(&mut self.s, sep));
2757 try!(self.print_lifetime(v));
2763 pub fn print_generics(&mut self,
2764 generics: &ast::Generics)
2767 let total = generics.lifetimes.len() + generics.ty_params.len();
2772 try!(word(&mut self.s, "<"));
2774 let mut ints = Vec::new();
2779 try!(self.commasep(Inconsistent, &ints[..], |s, &idx| {
2780 if idx < generics.lifetimes.len() {
2781 let lifetime = &generics.lifetimes[idx];
2782 s.print_lifetime_def(lifetime)
2784 let idx = idx - generics.lifetimes.len();
2785 let param = &generics.ty_params[idx];
2786 s.print_ty_param(param)
2790 try!(word(&mut self.s, ">"));
2794 pub fn print_ty_param(&mut self, param: &ast::TyParam) -> io::Result<()> {
2795 try!(self.print_ident(param.ident));
2796 try!(self.print_bounds(":", ¶m.bounds));
2797 match param.default {
2798 Some(ref default) => {
2799 try!(space(&mut self.s));
2800 try!(self.word_space("="));
2801 self.print_type(&**default)
2807 pub fn print_where_clause(&mut self, where_clause: &ast::WhereClause)
2809 if where_clause.predicates.is_empty() {
2813 try!(space(&mut self.s));
2814 try!(self.word_space("where"));
2816 for (i, predicate) in where_clause.predicates.iter().enumerate() {
2818 try!(self.word_space(","));
2822 &ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate{ref bound_lifetimes,
2826 try!(self.print_formal_lifetime_list(bound_lifetimes));
2827 try!(self.print_type(&**bounded_ty));
2828 try!(self.print_bounds(":", bounds));
2830 &ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate{ref lifetime,
2833 try!(self.print_lifetime(lifetime));
2834 try!(word(&mut self.s, ":"));
2836 for (i, bound) in bounds.iter().enumerate() {
2837 try!(self.print_lifetime(bound));
2840 try!(word(&mut self.s, ":"));
2844 &ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{ref path, ref ty, ..}) => {
2845 try!(self.print_path(path, false, 0));
2846 try!(space(&mut self.s));
2847 try!(self.word_space("="));
2848 try!(self.print_type(&**ty));
2856 pub fn print_view_path(&mut self, vp: &ast::ViewPath) -> io::Result<()> {
2858 ast::ViewPathSimple(ident, ref path) => {
2859 try!(self.print_path(path, false, 0));
2861 if path.segments.last().unwrap().identifier.name !=
2863 try!(space(&mut self.s));
2864 try!(self.word_space("as"));
2865 try!(self.print_ident(ident));
2871 ast::ViewPathGlob(ref path) => {
2872 try!(self.print_path(path, false, 0));
2873 word(&mut self.s, "::*")
2876 ast::ViewPathList(ref path, ref idents) => {
2877 if path.segments.is_empty() {
2878 try!(word(&mut self.s, "{"));
2880 try!(self.print_path(path, false, 0));
2881 try!(word(&mut self.s, "::{"));
2883 try!(self.commasep(Inconsistent, &idents[..], |s, w| {
2885 ast::PathListIdent { name, rename, .. } => {
2886 try!(s.print_ident(name));
2887 if let Some(ident) = rename {
2888 try!(space(&mut s.s));
2889 try!(s.word_space("as"));
2890 try!(s.print_ident(ident));
2894 ast::PathListMod { rename, .. } => {
2895 try!(word(&mut s.s, "self"));
2896 if let Some(ident) = rename {
2897 try!(space(&mut s.s));
2898 try!(s.word_space("as"));
2899 try!(s.print_ident(ident));
2905 word(&mut self.s, "}")
2910 pub fn print_mutability(&mut self,
2911 mutbl: ast::Mutability) -> io::Result<()> {
2913 ast::MutMutable => self.word_nbsp("mut"),
2914 ast::MutImmutable => Ok(()),
2918 pub fn print_mt(&mut self, mt: &ast::MutTy) -> io::Result<()> {
2919 try!(self.print_mutability(mt.mutbl));
2920 self.print_type(&*mt.ty)
2923 pub fn print_arg(&mut self, input: &ast::Arg) -> io::Result<()> {
2924 try!(self.ibox(indent_unit));
2925 match input.ty.node {
2926 ast::TyInfer => try!(self.print_pat(&*input.pat)),
2928 match input.pat.node {
2929 ast::PatIdent(_, ref path1, _) if
2931 parse::token::special_idents::invalid.name => {
2935 try!(self.print_pat(&*input.pat));
2936 try!(word(&mut self.s, ":"));
2937 try!(space(&mut self.s));
2940 try!(self.print_type(&*input.ty));
2946 pub fn print_fn_output(&mut self, decl: &ast::FnDecl) -> io::Result<()> {
2947 if let ast::DefaultReturn(..) = decl.output {
2951 try!(self.space_if_not_bol());
2952 try!(self.ibox(indent_unit));
2953 try!(self.word_space("->"));
2956 try!(self.word_nbsp("!")),
2957 ast::DefaultReturn(..) => unreachable!(),
2958 ast::Return(ref ty) =>
2959 try!(self.print_type(&**ty))
2964 ast::Return(ref output) => self.maybe_print_comment(output.span.lo),
2969 pub fn print_ty_fn(&mut self,
2971 unsafety: ast::Unsafety,
2973 name: Option<ast::Ident>,
2974 generics: &ast::Generics,
2975 opt_explicit_self: Option<&ast::ExplicitSelf_>)
2977 try!(self.ibox(indent_unit));
2978 if !generics.lifetimes.is_empty() || !generics.ty_params.is_empty() {
2979 try!(word(&mut self.s, "for"));
2980 try!(self.print_generics(generics));
2982 let generics = ast::Generics {
2983 lifetimes: Vec::new(),
2984 ty_params: OwnedSlice::empty(),
2985 where_clause: ast::WhereClause {
2986 id: ast::DUMMY_NODE_ID,
2987 predicates: Vec::new(),
2990 try!(self.print_fn(decl,
2992 ast::Constness::NotConst,
3001 pub fn maybe_print_trailing_comment(&mut self, span: codemap::Span,
3002 next_pos: Option<BytePos>)
3004 let cm = match self.cm {
3008 match self.next_comment() {
3010 if (*cmnt).style != comments::Trailing { return Ok(()) }
3011 let span_line = cm.lookup_char_pos(span.hi);
3012 let comment_line = cm.lookup_char_pos((*cmnt).pos);
3013 let mut next = (*cmnt).pos + BytePos(1);
3014 match next_pos { None => (), Some(p) => next = p }
3015 if span.hi < (*cmnt).pos && (*cmnt).pos < next &&
3016 span_line.line == comment_line.line {
3017 try!(self.print_comment(cmnt));
3018 self.cur_cmnt_and_lit.cur_cmnt += 1;
3026 pub fn print_remaining_comments(&mut self) -> io::Result<()> {
3027 // If there aren't any remaining comments, then we need to manually
3028 // make sure there is a line break at the end.
3029 if self.next_comment().is_none() {
3030 try!(hardbreak(&mut self.s));
3033 match self.next_comment() {
3035 try!(self.print_comment(cmnt));
3036 self.cur_cmnt_and_lit.cur_cmnt += 1;
3044 pub fn print_opt_abi_and_extern_if_nondefault(&mut self,
3045 opt_abi: Option<abi::Abi>)
3048 Some(abi::Rust) => Ok(()),
3050 try!(self.word_nbsp("extern"));
3051 self.word_nbsp(&abi.to_string())
3057 pub fn print_extern_opt_abi(&mut self,
3058 opt_abi: Option<abi::Abi>) -> io::Result<()> {
3061 try!(self.word_nbsp("extern"));
3062 self.word_nbsp(&abi.to_string())
3068 pub fn print_fn_header_info(&mut self,
3069 unsafety: ast::Unsafety,
3070 constness: ast::Constness,
3072 vis: ast::Visibility) -> io::Result<()> {
3073 try!(word(&mut self.s, &visibility_qualified(vis, "")));
3074 try!(self.print_unsafety(unsafety));
3077 ast::Constness::NotConst => {}
3078 ast::Constness::Const => try!(self.word_nbsp("const"))
3081 if abi != abi::Rust {
3082 try!(self.word_nbsp("extern"));
3083 try!(self.word_nbsp(&abi.to_string()));
3086 word(&mut self.s, "fn")
3089 pub fn print_unsafety(&mut self, s: ast::Unsafety) -> io::Result<()> {
3091 ast::Unsafety::Normal => Ok(()),
3092 ast::Unsafety::Unsafe => self.word_nbsp("unsafe"),
3097 fn repeat(s: &str, n: usize) -> String { iter::repeat(s).take(n).collect() }
3110 fn test_fun_to_string() {
3111 let abba_ident = token::str_to_ident("abba");
3113 let decl = ast::FnDecl {
3115 output: ast::DefaultReturn(codemap::DUMMY_SP),
3118 let generics = ast_util::empty_generics();
3119 assert_eq!(fun_to_string(&decl, ast::Unsafety::Normal,
3120 ast::Constness::NotConst,
3127 fn test_variant_to_string() {
3128 let ident = token::str_to_ident("principal_skinner");
3130 let var = codemap::respan(codemap::DUMMY_SP, ast::Variant_ {
3133 // making this up as I go.... ?
3134 data: P(ast::VariantData::Unit(ast::DUMMY_NODE_ID)),
3138 let varstr = variant_to_string(&var);
3139 assert_eq!(varstr, "principal_skinner");
3143 fn test_signed_int_to_string() {
3144 let pos_int = ast::LitInt(42, ast::SignedIntLit(ast::TyI32, ast::Plus));
3145 let neg_int = ast::LitInt((!42 + 1) as u64, ast::SignedIntLit(ast::TyI32, ast::Minus));
3146 assert_eq!(format!("-{}", lit_to_string(&codemap::dummy_spanned(pos_int))),
3147 lit_to_string(&codemap::dummy_spanned(neg_int)));