1 // Copyright 2012 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.
12 use ast::{P, StaticRegionTyParamBound, OtherRegionTyParamBound};
13 use ast::{TraitTyParamBound, UnboxedFnTyParamBound, Required, Provided};
16 use owned_slice::OwnedSlice;
17 use attr::{AttrMetaMethods, AttributeMethods};
18 use codemap::{CodeMap, BytePos};
21 use parse::classify::expr_is_simple_block;
23 use parse::lexer::comments;
25 use print::pp::{break_offset, word, space, zerobreak, hardbreak};
26 use print::pp::{Breaks, Consistent, Inconsistent, eof};
30 use std::io::{IoResult, MemWriter};
34 pub enum AnnNode<'a> {
35 NodeBlock(&'a ast::Block),
36 NodeItem(&'a ast::Item),
37 NodeExpr(&'a ast::Expr),
38 NodePat(&'a ast::Pat),
42 fn pre(&self, _state: &mut State, _node: AnnNode) -> IoResult<()> { Ok(()) }
43 fn post(&self, _state: &mut State, _node: AnnNode) -> IoResult<()> { Ok(()) }
48 impl PpAnn for NoAnn {}
50 pub struct CurrentCommentAndLiteral {
55 pub struct State<'a> {
57 cm: Option<&'a CodeMap>,
58 comments: Option<Vec<comments::Comment> >,
59 literals: Option<Vec<comments::Literal> >,
60 cur_cmnt_and_lit: CurrentCommentAndLiteral,
61 boxes: Vec<pp::Breaks>,
65 pub fn rust_printer(writer: Box<io::Writer>) -> State<'static> {
66 static NO_ANN: NoAnn = NoAnn;
67 rust_printer_annotated(writer, &NO_ANN)
70 pub fn rust_printer_annotated<'a>(writer: Box<io::Writer>,
71 ann: &'a PpAnn) -> State<'a> {
73 s: pp::mk_printer(writer, default_columns),
77 cur_cmnt_and_lit: CurrentCommentAndLiteral {
86 pub static indent_unit: uint = 4u;
88 pub static default_columns: uint = 78u;
90 /// Requires you to pass an input filename and reader so that
91 /// it can scan the input text for comments and literals to
93 pub fn print_crate<'a>(cm: &'a CodeMap,
94 span_diagnostic: &diagnostic::SpanHandler,
97 input: &mut io::Reader,
100 is_expanded: bool) -> IoResult<()> {
101 let (cmnts, lits) = comments::gather_comments_and_literals(
107 s: pp::mk_printer(out, default_columns),
109 comments: Some(cmnts),
110 // If the code is post expansion, don't use the table of
111 // literals, since it doesn't correspond with the literals
112 // in the AST anymore.
113 literals: if is_expanded {
118 cur_cmnt_and_lit: CurrentCommentAndLiteral {
125 try!(s.print_mod(&krate.module, krate.attrs.as_slice()));
126 try!(s.print_remaining_comments());
130 pub fn to_string(f: |&mut State| -> IoResult<()>) -> String {
131 let mut s = rust_printer(box MemWriter::new());
133 eof(&mut s.s).unwrap();
135 // FIXME(pcwalton): A nasty function to extract the string from an `io::Writer`
136 // that we "know" to be a `MemWriter` that works around the lack of checked
138 let (_, wr): (uint, Box<MemWriter>) = mem::transmute_copy(&s.s.out);
140 String::from_utf8(Vec::from_slice(wr.get_ref())).unwrap();
146 pub fn ty_to_string(ty: &ast::Ty) -> String {
147 to_string(|s| s.print_type(ty))
150 pub fn pat_to_string(pat: &ast::Pat) -> String {
151 to_string(|s| s.print_pat(pat))
154 pub fn expr_to_string(e: &ast::Expr) -> String {
155 to_string(|s| s.print_expr(e))
158 pub fn lifetime_to_string(e: &ast::Lifetime) -> String {
159 to_string(|s| s.print_lifetime(e))
162 pub fn tt_to_string(tt: &ast::TokenTree) -> String {
163 to_string(|s| s.print_tt(tt))
166 pub fn tts_to_string(tts: &[ast::TokenTree]) -> String {
167 to_string(|s| s.print_tts(tts))
170 pub fn stmt_to_string(stmt: &ast::Stmt) -> String {
171 to_string(|s| s.print_stmt(stmt))
174 pub fn item_to_string(i: &ast::Item) -> String {
175 to_string(|s| s.print_item(i))
178 pub fn generics_to_string(generics: &ast::Generics) -> String {
179 to_string(|s| s.print_generics(generics))
182 pub fn ty_method_to_string(p: &ast::TypeMethod) -> String {
183 to_string(|s| s.print_ty_method(p))
186 pub fn method_to_string(p: &ast::Method) -> String {
187 to_string(|s| s.print_method(p))
190 pub fn fn_block_to_string(p: &ast::FnDecl) -> String {
191 to_string(|s| s.print_fn_block_args(p))
194 pub fn path_to_string(p: &ast::Path) -> String {
195 to_string(|s| s.print_path(p, false))
198 pub fn ident_to_string(id: &ast::Ident) -> String {
199 to_string(|s| s.print_ident(*id))
202 pub fn fun_to_string(decl: &ast::FnDecl, fn_style: ast::FnStyle, name: ast::Ident,
203 opt_explicit_self: Option<ast::ExplicitSelf_>,
204 generics: &ast::Generics) -> String {
206 try!(s.print_fn(decl, Some(fn_style), abi::Rust,
207 name, generics, opt_explicit_self, ast::Inherited));
208 try!(s.end()); // Close the head box
209 s.end() // Close the outer box
213 pub fn block_to_string(blk: &ast::Block) -> String {
215 // containing cbox, will be closed by print-block at }
216 try!(s.cbox(indent_unit));
217 // head-ibox, will be closed by print-block after {
223 pub fn meta_item_to_string(mi: &ast::MetaItem) -> String {
224 to_string(|s| s.print_meta_item(mi))
227 pub fn attribute_to_string(attr: &ast::Attribute) -> String {
228 to_string(|s| s.print_attribute(attr))
231 pub fn lit_to_string(l: &ast::Lit) -> String {
232 to_string(|s| s.print_literal(l))
235 pub fn explicit_self_to_string(explicit_self: ast::ExplicitSelf_) -> String {
236 to_string(|s| s.print_explicit_self(explicit_self, ast::MutImmutable).map(|_| {}))
239 pub fn variant_to_string(var: &ast::Variant) -> String {
240 to_string(|s| s.print_variant(var))
243 pub fn arg_to_string(arg: &ast::Arg) -> String {
244 to_string(|s| s.print_arg(arg))
247 pub fn mac_to_string(arg: &ast::Mac) -> String {
248 to_string(|s| s.print_mac(arg))
251 pub fn visibility_qualified(vis: ast::Visibility, s: &str) -> String {
253 ast::Public => format!("pub {}", s),
254 ast::Inherited => s.to_string()
258 fn needs_parentheses(expr: &ast::Expr) -> bool {
260 ast::ExprAssign(..) | ast::ExprBinary(..) |
261 ast::ExprFnBlock(..) | ast::ExprProc(..) |
262 ast::ExprAssignOp(..) | ast::ExprCast(..) => true,
268 pub fn ibox(&mut self, u: uint) -> IoResult<()> {
269 self.boxes.push(pp::Inconsistent);
270 pp::ibox(&mut self.s, u)
273 pub fn end(&mut self) -> IoResult<()> {
274 self.boxes.pop().unwrap();
278 pub fn cbox(&mut self, u: uint) -> IoResult<()> {
279 self.boxes.push(pp::Consistent);
280 pp::cbox(&mut self.s, u)
284 pub fn rbox(&mut self, u: uint, b: pp::Breaks) -> IoResult<()> {
286 pp::rbox(&mut self.s, u, b)
289 pub fn nbsp(&mut self) -> IoResult<()> { word(&mut self.s, " ") }
291 pub fn word_nbsp(&mut self, w: &str) -> IoResult<()> {
292 try!(word(&mut self.s, w));
296 pub fn word_space(&mut self, w: &str) -> IoResult<()> {
297 try!(word(&mut self.s, w));
301 pub fn popen(&mut self) -> IoResult<()> { word(&mut self.s, "(") }
303 pub fn pclose(&mut self) -> IoResult<()> { word(&mut self.s, ")") }
305 pub fn head(&mut self, w: &str) -> IoResult<()> {
306 // outer-box is consistent
307 try!(self.cbox(indent_unit));
308 // head-box is inconsistent
309 try!(self.ibox(w.len() + 1));
310 // keyword that starts the head
312 try!(self.word_nbsp(w));
317 pub fn bopen(&mut self) -> IoResult<()> {
318 try!(word(&mut self.s, "{"));
319 self.end() // close the head-box
322 pub fn bclose_(&mut self, span: codemap::Span,
323 indented: uint) -> IoResult<()> {
324 self.bclose_maybe_open(span, indented, true)
326 pub fn bclose_maybe_open (&mut self, span: codemap::Span,
327 indented: uint, close_box: bool) -> IoResult<()> {
328 try!(self.maybe_print_comment(span.hi));
329 try!(self.break_offset_if_not_bol(1u, -(indented as int)));
330 try!(word(&mut self.s, "}"));
332 try!(self.end()); // close the outer-box
336 pub fn bclose(&mut self, span: codemap::Span) -> IoResult<()> {
337 self.bclose_(span, indent_unit)
340 pub fn is_begin(&mut self) -> bool {
341 match self.s.last_token() { pp::Begin(_) => true, _ => false }
344 pub fn is_end(&mut self) -> bool {
345 match self.s.last_token() { pp::End => true, _ => false }
348 // is this the beginning of a line?
349 pub fn is_bol(&mut self) -> bool {
350 self.s.last_token().is_eof() || self.s.last_token().is_hardbreak_tok()
353 pub fn in_cbox(&self) -> bool {
354 match self.boxes.last() {
355 Some(&last_box) => last_box == pp::Consistent,
360 pub fn hardbreak_if_not_bol(&mut self) -> IoResult<()> {
362 try!(hardbreak(&mut self.s))
366 pub fn space_if_not_bol(&mut self) -> IoResult<()> {
367 if !self.is_bol() { try!(space(&mut self.s)); }
370 pub fn break_offset_if_not_bol(&mut self, n: uint,
371 off: int) -> IoResult<()> {
373 break_offset(&mut self.s, n, off)
375 if off != 0 && self.s.last_token().is_hardbreak_tok() {
376 // We do something pretty sketchy here: tuck the nonzero
377 // offset-adjustment we were going to deposit along with the
378 // break into the previous hardbreak.
379 self.s.replace_last_token(pp::hardbreak_tok_offset(off));
385 // Synthesizes a comment that was not textually present in the original source
387 pub fn synth_comment(&mut self, text: String) -> IoResult<()> {
388 try!(word(&mut self.s, "/*"));
389 try!(space(&mut self.s));
390 try!(word(&mut self.s, text.as_slice()));
391 try!(space(&mut self.s));
392 word(&mut self.s, "*/")
395 pub fn commasep<T>(&mut self, b: Breaks, elts: &[T],
396 op: |&mut State, &T| -> IoResult<()>)
398 try!(self.rbox(0u, b));
399 let mut first = true;
400 for elt in elts.iter() {
401 if first { first = false; } else { try!(self.word_space(",")); }
408 pub fn commasep_cmnt<T>(
412 op: |&mut State, &T| -> IoResult<()>,
413 get_span: |&T| -> codemap::Span) -> IoResult<()> {
414 try!(self.rbox(0u, b));
415 let len = elts.len();
417 for elt in elts.iter() {
418 try!(self.maybe_print_comment(get_span(elt).hi));
422 try!(word(&mut self.s, ","));
423 try!(self.maybe_print_trailing_comment(get_span(elt),
424 Some(get_span(&elts[i]).hi)));
425 try!(self.space_if_not_bol());
431 pub fn commasep_exprs(&mut self, b: Breaks,
432 exprs: &[Gc<ast::Expr>]) -> IoResult<()> {
433 self.commasep_cmnt(b, exprs, |s, e| s.print_expr(&**e), |e| e.span)
436 pub fn print_mod(&mut self, _mod: &ast::Mod,
437 attrs: &[ast::Attribute]) -> IoResult<()> {
438 try!(self.print_inner_attributes(attrs));
439 for vitem in _mod.view_items.iter() {
440 try!(self.print_view_item(vitem));
442 for item in _mod.items.iter() {
443 try!(self.print_item(&**item));
448 pub fn print_foreign_mod(&mut self, nmod: &ast::ForeignMod,
449 attrs: &[ast::Attribute]) -> IoResult<()> {
450 try!(self.print_inner_attributes(attrs));
451 for vitem in nmod.view_items.iter() {
452 try!(self.print_view_item(vitem));
454 for item in nmod.items.iter() {
455 try!(self.print_foreign_item(&**item));
460 pub fn print_opt_lifetime(&mut self,
461 lifetime: &Option<ast::Lifetime>) -> IoResult<()> {
462 for l in lifetime.iter() {
463 try!(self.print_lifetime(l));
469 pub fn print_type(&mut self, ty: &ast::Ty) -> IoResult<()> {
470 try!(self.maybe_print_comment(ty.span.lo));
473 ast::TyNil => try!(word(&mut self.s, "()")),
474 ast::TyBot => try!(word(&mut self.s, "!")),
475 ast::TyBox(ref ty) => {
476 try!(word(&mut self.s, "@"));
477 try!(self.print_type(&**ty));
479 ast::TyUniq(ref ty) => {
480 try!(word(&mut self.s, "~"));
481 try!(self.print_type(&**ty));
483 ast::TyVec(ref ty) => {
484 try!(word(&mut self.s, "["));
485 try!(self.print_type(&**ty));
486 try!(word(&mut self.s, "]"));
488 ast::TyPtr(ref mt) => {
489 try!(word(&mut self.s, "*"));
491 ast::MutMutable => try!(self.word_nbsp("mut")),
492 ast::MutImmutable => try!(self.word_nbsp("const")),
494 try!(self.print_type(&*mt.ty));
496 ast::TyRptr(ref lifetime, ref mt) => {
497 try!(word(&mut self.s, "&"));
498 try!(self.print_opt_lifetime(lifetime));
499 try!(self.print_mt(mt));
501 ast::TyTup(ref elts) => {
503 try!(self.commasep(Inconsistent, elts.as_slice(),
504 |s, ty| s.print_type_ref(ty)));
506 try!(word(&mut self.s, ","));
510 ast::TyParen(ref typ) => {
512 try!(self.print_type(&**typ));
515 ast::TyBareFn(f) => {
516 let generics = ast::Generics {
517 lifetimes: f.lifetimes.clone(),
518 ty_params: OwnedSlice::empty()
520 try!(self.print_ty_fn(Some(f.abi),
532 ast::TyClosure(f, ref region) => {
533 let generics = ast::Generics {
534 lifetimes: f.lifetimes.clone(),
535 ty_params: OwnedSlice::empty()
537 try!(self.print_ty_fn(None,
549 ast::TyProc(ref f) => {
550 let generics = ast::Generics {
551 lifetimes: f.lifetimes.clone(),
552 ty_params: OwnedSlice::empty()
554 try!(self.print_ty_fn(None,
566 ast::TyUnboxedFn(f) => {
567 try!(self.print_ty_fn(None,
579 ast::TyPath(ref path, ref bounds, _) => {
580 try!(self.print_bounded_path(path, bounds));
582 ast::TyFixedLengthVec(ref ty, ref v) => {
583 try!(word(&mut self.s, "["));
584 try!(self.print_type(&**ty));
585 try!(word(&mut self.s, ", .."));
586 try!(self.print_expr(&**v));
587 try!(word(&mut self.s, "]"));
589 ast::TyTypeof(ref e) => {
590 try!(word(&mut self.s, "typeof("));
591 try!(self.print_expr(&**e));
592 try!(word(&mut self.s, ")"));
595 try!(word(&mut self.s, "_"));
601 pub fn print_type_ref(&mut self, ty: &P<ast::Ty>) -> IoResult<()> {
602 self.print_type(&**ty)
605 pub fn print_foreign_item(&mut self,
606 item: &ast::ForeignItem) -> IoResult<()> {
607 try!(self.hardbreak_if_not_bol());
608 try!(self.maybe_print_comment(item.span.lo));
609 try!(self.print_outer_attributes(item.attrs.as_slice()));
611 ast::ForeignItemFn(ref decl, ref generics) => {
612 try!(self.print_fn(&**decl, None, abi::Rust, item.ident, generics,
614 try!(self.end()); // end head-ibox
615 try!(word(&mut self.s, ";"));
616 self.end() // end the outer fn box
618 ast::ForeignItemStatic(ref t, m) => {
619 try!(self.head(visibility_qualified(item.vis,
620 "static").as_slice()));
622 try!(self.word_space("mut"));
624 try!(self.print_ident(item.ident));
625 try!(self.word_space(":"));
626 try!(self.print_type(&**t));
627 try!(word(&mut self.s, ";"));
628 try!(self.end()); // end the head-ibox
629 self.end() // end the outer cbox
634 /// Pretty-print an item
635 pub fn print_item(&mut self, item: &ast::Item) -> IoResult<()> {
636 try!(self.hardbreak_if_not_bol());
637 try!(self.maybe_print_comment(item.span.lo));
638 try!(self.print_outer_attributes(item.attrs.as_slice()));
639 try!(self.ann.pre(self, NodeItem(item)));
641 ast::ItemStatic(ref ty, m, ref expr) => {
642 try!(self.head(visibility_qualified(item.vis,
643 "static").as_slice()));
644 if m == ast::MutMutable {
645 try!(self.word_space("mut"));
647 try!(self.print_ident(item.ident));
648 try!(self.word_space(":"));
649 try!(self.print_type(&**ty));
650 try!(space(&mut self.s));
651 try!(self.end()); // end the head-ibox
653 try!(self.word_space("="));
654 try!(self.print_expr(&**expr));
655 try!(word(&mut self.s, ";"));
656 try!(self.end()); // end the outer cbox
658 ast::ItemFn(ref decl, fn_style, abi, ref typarams, ref body) => {
668 try!(word(&mut self.s, " "));
669 try!(self.print_block_with_attrs(&**body, item.attrs.as_slice()));
671 ast::ItemMod(ref _mod) => {
672 try!(self.head(visibility_qualified(item.vis,
674 try!(self.print_ident(item.ident));
677 try!(self.print_mod(_mod, item.attrs.as_slice()));
678 try!(self.bclose(item.span));
680 ast::ItemForeignMod(ref nmod) => {
681 try!(self.head("extern"));
682 try!(self.word_nbsp(nmod.abi.to_string().as_slice()));
684 try!(self.print_foreign_mod(nmod, item.attrs.as_slice()));
685 try!(self.bclose(item.span));
687 ast::ItemTy(ref ty, ref params) => {
688 try!(self.ibox(indent_unit));
690 try!(self.word_nbsp(visibility_qualified(item.vis,
691 "type").as_slice()));
692 try!(self.print_ident(item.ident));
693 try!(self.print_generics(params));
694 try!(self.end()); // end the inner ibox
696 try!(space(&mut self.s));
697 try!(self.word_space("="));
698 try!(self.print_type(&**ty));
699 try!(word(&mut self.s, ";"));
700 try!(self.end()); // end the outer ibox
702 ast::ItemEnum(ref enum_definition, ref params) => {
703 try!(self.print_enum_def(
711 ast::ItemStruct(ref struct_def, ref generics) => {
712 if struct_def.is_virtual {
713 try!(self.word_space("virtual"));
715 try!(self.head(visibility_qualified(item.vis,
716 "struct").as_slice()));
717 try!(self.print_struct(&**struct_def, generics, item.ident,
721 ast::ItemImpl(ref generics, ref opt_trait, ref ty, ref methods) => {
722 try!(self.head(visibility_qualified(item.vis,
723 "impl").as_slice()));
724 if generics.is_parameterized() {
725 try!(self.print_generics(generics));
726 try!(space(&mut self.s));
731 try!(self.print_trait_ref(t));
732 try!(space(&mut self.s));
733 try!(self.word_space("for"));
738 try!(self.print_type(&**ty));
740 try!(space(&mut self.s));
742 try!(self.print_inner_attributes(item.attrs.as_slice()));
743 for meth in methods.iter() {
744 try!(self.print_method(&**meth));
746 try!(self.bclose(item.span));
748 ast::ItemTrait(ref generics, ref unbound, ref traits, ref methods) => {
749 try!(self.head(visibility_qualified(item.vis,
750 "trait").as_slice()));
751 try!(self.print_ident(item.ident));
752 try!(self.print_generics(generics));
754 &Some(TraitTyParamBound(ref tref)) => {
755 try!(space(&mut self.s));
756 try!(self.word_space("for"));
757 try!(self.print_trait_ref(tref));
758 try!(word(&mut self.s, "?"));
762 if traits.len() != 0u {
763 try!(word(&mut self.s, ":"));
764 for (i, trait_) in traits.iter().enumerate() {
767 try!(self.word_space("+"));
769 try!(self.print_path(&trait_.path, false));
772 try!(word(&mut self.s, " "));
774 for meth in methods.iter() {
775 try!(self.print_trait_method(meth));
777 try!(self.bclose(item.span));
779 // I think it's reasonable to hide the context here:
780 ast::ItemMac(codemap::Spanned { node: ast::MacInvocTT(ref pth, ref tts, _),
782 try!(self.print_visibility(item.vis));
783 try!(self.print_path(pth, false));
784 try!(word(&mut self.s, "! "));
785 try!(self.print_ident(item.ident));
786 try!(self.cbox(indent_unit));
788 try!(self.print_tts(tts.as_slice()));
793 self.ann.post(self, NodeItem(item))
796 fn print_trait_ref(&mut self, t: &ast::TraitRef) -> IoResult<()> {
797 self.print_path(&t.path, false)
800 pub fn print_enum_def(&mut self, enum_definition: &ast::EnumDef,
801 generics: &ast::Generics, ident: ast::Ident,
803 visibility: ast::Visibility) -> IoResult<()> {
804 try!(self.head(visibility_qualified(visibility, "enum").as_slice()));
805 try!(self.print_ident(ident));
806 try!(self.print_generics(generics));
807 try!(space(&mut self.s));
808 self.print_variants(enum_definition.variants.as_slice(), span)
811 pub fn print_variants(&mut self,
812 variants: &[P<ast::Variant>],
813 span: codemap::Span) -> IoResult<()> {
815 for v in variants.iter() {
816 try!(self.space_if_not_bol());
817 try!(self.maybe_print_comment(v.span.lo));
818 try!(self.print_outer_attributes(v.node.attrs.as_slice()));
819 try!(self.ibox(indent_unit));
820 try!(self.print_variant(&**v));
821 try!(word(&mut self.s, ","));
823 try!(self.maybe_print_trailing_comment(v.span, None));
828 pub fn print_visibility(&mut self, vis: ast::Visibility) -> IoResult<()> {
830 ast::Public => self.word_nbsp("pub"),
831 ast::Inherited => Ok(())
835 pub fn print_struct(&mut self,
836 struct_def: &ast::StructDef,
837 generics: &ast::Generics,
839 span: codemap::Span) -> IoResult<()> {
840 try!(self.print_ident(ident));
841 try!(self.print_generics(generics));
842 match struct_def.super_struct {
844 try!(self.word_space(":"));
845 try!(self.print_type(&**t));
849 if ast_util::struct_def_is_tuple_like(struct_def) {
850 if !struct_def.fields.is_empty() {
853 Inconsistent, struct_def.fields.as_slice(),
855 match field.node.kind {
856 ast::NamedField(..) => fail!("unexpected named field"),
857 ast::UnnamedField(vis) => {
858 try!(s.print_visibility(vis));
859 try!(s.maybe_print_comment(field.span.lo));
860 s.print_type(&*field.node.ty)
867 try!(word(&mut self.s, ";"));
869 self.end() // close the outer-box
873 try!(self.hardbreak_if_not_bol());
875 for field in struct_def.fields.iter() {
876 match field.node.kind {
877 ast::UnnamedField(..) => fail!("unexpected unnamed field"),
878 ast::NamedField(ident, visibility) => {
879 try!(self.hardbreak_if_not_bol());
880 try!(self.maybe_print_comment(field.span.lo));
881 try!(self.print_outer_attributes(field.node.attrs.as_slice()));
882 try!(self.print_visibility(visibility));
883 try!(self.print_ident(ident));
884 try!(self.word_nbsp(":"));
885 try!(self.print_type(&*field.node.ty));
886 try!(word(&mut self.s, ","));
895 /// This doesn't deserve to be called "pretty" printing, but it should be
896 /// meaning-preserving. A quick hack that might help would be to look at the
897 /// spans embedded in the TTs to decide where to put spaces and newlines.
898 /// But it'd be better to parse these according to the grammar of the
899 /// appropriate macro, transcribe back into the grammar we just parsed from,
900 /// and then pretty-print the resulting AST nodes (so, e.g., we print
901 /// expression arguments as expressions). It can be done! I think.
902 pub fn print_tt(&mut self, tt: &ast::TokenTree) -> IoResult<()> {
904 ast::TTDelim(ref tts) => self.print_tts(tts.as_slice()),
905 ast::TTTok(_, ref tk) => {
906 try!(word(&mut self.s, parse::token::to_string(tk).as_slice()));
908 parse::token::DOC_COMMENT(..) => {
909 hardbreak(&mut self.s)
914 ast::TTSeq(_, ref tts, ref sep, zerok) => {
915 try!(word(&mut self.s, "$("));
916 for tt_elt in (*tts).iter() {
917 try!(self.print_tt(tt_elt));
919 try!(word(&mut self.s, ")"));
922 try!(word(&mut self.s,
923 parse::token::to_string(tk).as_slice()));
927 word(&mut self.s, if zerok { "*" } else { "+" })
929 ast::TTNonterminal(_, name) => {
930 try!(word(&mut self.s, "$"));
931 self.print_ident(name)
936 pub fn print_tts(&mut self, tts: &[ast::TokenTree]) -> IoResult<()> {
938 for (i, tt) in tts.iter().enumerate() {
940 try!(space(&mut self.s));
942 try!(self.print_tt(tt));
947 pub fn print_variant(&mut self, v: &ast::Variant) -> IoResult<()> {
948 try!(self.print_visibility(v.node.vis));
950 ast::TupleVariantKind(ref args) => {
951 try!(self.print_ident(v.node.name));
952 if !args.is_empty() {
954 try!(self.commasep(Consistent,
956 |s, arg| s.print_type(&*arg.ty)));
960 ast::StructVariantKind(ref struct_def) => {
962 let generics = ast_util::empty_generics();
963 try!(self.print_struct(&**struct_def, &generics, v.node.name, v.span));
966 match v.node.disr_expr {
968 try!(space(&mut self.s));
969 try!(self.word_space("="));
970 self.print_expr(&**d)
976 pub fn print_ty_method(&mut self, m: &ast::TypeMethod) -> IoResult<()> {
977 try!(self.hardbreak_if_not_bol());
978 try!(self.maybe_print_comment(m.span.lo));
979 try!(self.print_outer_attributes(m.attrs.as_slice()));
980 try!(self.print_ty_fn(None,
989 Some(m.explicit_self.node),
991 word(&mut self.s, ";")
994 pub fn print_trait_method(&mut self,
995 m: &ast::TraitMethod) -> IoResult<()> {
997 Required(ref ty_m) => self.print_ty_method(ty_m),
998 Provided(ref m) => self.print_method(&**m)
1002 pub fn print_method(&mut self, meth: &ast::Method) -> IoResult<()> {
1003 try!(self.hardbreak_if_not_bol());
1004 try!(self.maybe_print_comment(meth.span.lo));
1005 try!(self.print_outer_attributes(meth.attrs.as_slice()));
1007 ast::MethDecl(ident, ref generics, ref explicit_self, fn_style, decl, body, vis) => {
1008 try!(self.print_fn(&*decl, Some(fn_style), abi::Rust,
1009 ident, generics, Some(explicit_self.node),
1011 try!(word(&mut self.s, " "));
1012 self.print_block_with_attrs(&*body, meth.attrs.as_slice())
1014 ast::MethMac(codemap::Spanned { node: ast::MacInvocTT(ref pth, ref tts, _),
1016 // code copied from ItemMac:
1017 try!(self.print_path(pth, false));
1018 try!(word(&mut self.s, "! "));
1019 try!(self.cbox(indent_unit));
1021 try!(self.print_tts(tts.as_slice()));
1022 try!(self.pclose());
1028 pub fn print_outer_attributes(&mut self,
1029 attrs: &[ast::Attribute]) -> IoResult<()> {
1031 for attr in attrs.iter() {
1032 match attr.node.style {
1034 try!(self.print_attribute(attr));
1037 _ => {/* fallthrough */ }
1041 try!(self.hardbreak_if_not_bol());
1046 pub fn print_inner_attributes(&mut self,
1047 attrs: &[ast::Attribute]) -> IoResult<()> {
1049 for attr in attrs.iter() {
1050 match attr.node.style {
1052 try!(self.print_attribute(attr));
1055 _ => {/* fallthrough */ }
1059 try!(self.hardbreak_if_not_bol());
1064 pub fn print_attribute(&mut self, attr: &ast::Attribute) -> IoResult<()> {
1065 try!(self.hardbreak_if_not_bol());
1066 try!(self.maybe_print_comment(attr.span.lo));
1067 if attr.node.is_sugared_doc {
1068 word(&mut self.s, attr.value_str().unwrap().get())
1070 match attr.node.style {
1071 ast::AttrInner => try!(word(&mut self.s, "#![")),
1072 ast::AttrOuter => try!(word(&mut self.s, "#[")),
1074 try!(self.print_meta_item(&*attr.meta()));
1075 word(&mut self.s, "]")
1080 pub fn print_stmt(&mut self, st: &ast::Stmt) -> IoResult<()> {
1081 try!(self.maybe_print_comment(st.span.lo));
1083 ast::StmtDecl(ref decl, _) => {
1084 try!(self.print_decl(&**decl));
1086 ast::StmtExpr(ref expr, _) => {
1087 try!(self.space_if_not_bol());
1088 try!(self.print_expr(&**expr));
1090 ast::StmtSemi(ref expr, _) => {
1091 try!(self.space_if_not_bol());
1092 try!(self.print_expr(&**expr));
1093 try!(word(&mut self.s, ";"));
1095 ast::StmtMac(ref mac, semi) => {
1096 try!(self.space_if_not_bol());
1097 try!(self.print_mac(mac));
1099 try!(word(&mut self.s, ";"));
1103 if parse::classify::stmt_ends_with_semi(st) {
1104 try!(word(&mut self.s, ";"));
1106 self.maybe_print_trailing_comment(st.span, None)
1109 pub fn print_block(&mut self, blk: &ast::Block) -> IoResult<()> {
1110 self.print_block_with_attrs(blk, &[])
1113 pub fn print_block_unclosed(&mut self, blk: &ast::Block) -> IoResult<()> {
1114 self.print_block_unclosed_indent(blk, indent_unit)
1117 pub fn print_block_unclosed_indent(&mut self, blk: &ast::Block,
1118 indented: uint) -> IoResult<()> {
1119 self.print_block_maybe_unclosed(blk, indented, &[], false)
1122 pub fn print_block_with_attrs(&mut self,
1124 attrs: &[ast::Attribute]) -> IoResult<()> {
1125 self.print_block_maybe_unclosed(blk, indent_unit, attrs, true)
1128 pub fn print_block_maybe_unclosed(&mut self,
1131 attrs: &[ast::Attribute],
1132 close_box: bool) -> IoResult<()> {
1134 ast::UnsafeBlock(..) => try!(self.word_space("unsafe")),
1135 ast::DefaultBlock => ()
1137 try!(self.maybe_print_comment(blk.span.lo));
1138 try!(self.ann.pre(self, NodeBlock(blk)));
1141 try!(self.print_inner_attributes(attrs));
1143 for vi in blk.view_items.iter() {
1144 try!(self.print_view_item(vi));
1146 for st in blk.stmts.iter() {
1147 try!(self.print_stmt(&**st));
1151 try!(self.space_if_not_bol());
1152 try!(self.print_expr(&**expr));
1153 try!(self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi)));
1157 try!(self.bclose_maybe_open(blk.span, indented, close_box));
1158 self.ann.post(self, NodeBlock(blk))
1161 fn print_else(&mut self, els: Option<Gc<ast::Expr>>) -> IoResult<()> {
1165 // "another else-if"
1166 ast::ExprIf(ref i, ref t, e) => {
1167 try!(self.cbox(indent_unit - 1u));
1168 try!(self.ibox(0u));
1169 try!(word(&mut self.s, " else if "));
1170 try!(self.print_expr(&**i));
1171 try!(space(&mut self.s));
1172 try!(self.print_block(&**t));
1176 ast::ExprBlock(ref b) => {
1177 try!(self.cbox(indent_unit - 1u));
1178 try!(self.ibox(0u));
1179 try!(word(&mut self.s, " else "));
1180 self.print_block(&**b)
1182 // BLEAH, constraints would be great here
1184 fail!("print_if saw if with weird alternative");
1192 pub fn print_if(&mut self, test: &ast::Expr, blk: &ast::Block,
1193 elseopt: Option<Gc<ast::Expr>>, chk: bool) -> IoResult<()> {
1194 try!(self.head("if"));
1195 if chk { try!(self.word_nbsp("check")); }
1196 try!(self.print_expr(test));
1197 try!(space(&mut self.s));
1198 try!(self.print_block(blk));
1199 self.print_else(elseopt)
1202 pub fn print_mac(&mut self, m: &ast::Mac) -> IoResult<()> {
1204 // I think it's reasonable to hide the ctxt here:
1205 ast::MacInvocTT(ref pth, ref tts, _) => {
1206 try!(self.print_path(pth, false));
1207 try!(word(&mut self.s, "!"));
1209 try!(self.print_tts(tts.as_slice()));
1215 pub fn print_expr_vstore(&mut self, t: ast::ExprVstore) -> IoResult<()> {
1217 ast::ExprVstoreUniq => word(&mut self.s, "box "),
1218 ast::ExprVstoreSlice => word(&mut self.s, "&"),
1219 ast::ExprVstoreMutSlice => {
1220 try!(word(&mut self.s, "&"));
1221 word(&mut self.s, "mut")
1226 fn print_call_post(&mut self, args: &[Gc<ast::Expr>]) -> IoResult<()> {
1228 try!(self.commasep_exprs(Inconsistent, args));
1232 pub fn print_expr_maybe_paren(&mut self, expr: &ast::Expr) -> IoResult<()> {
1233 let needs_par = needs_parentheses(expr);
1237 try!(self.print_expr(expr));
1239 try!(self.pclose());
1244 pub fn print_expr(&mut self, expr: &ast::Expr) -> IoResult<()> {
1245 try!(self.maybe_print_comment(expr.span.lo));
1246 try!(self.ibox(indent_unit));
1247 try!(self.ann.pre(self, NodeExpr(expr)));
1249 ast::ExprVstore(ref e, v) => {
1250 try!(self.print_expr_vstore(v));
1251 try!(self.print_expr(&**e));
1253 ast::ExprBox(ref p, ref e) => {
1254 try!(word(&mut self.s, "box"));
1255 try!(word(&mut self.s, "("));
1256 try!(self.print_expr(&**p));
1257 try!(self.word_space(")"));
1258 try!(self.print_expr(&**e));
1260 ast::ExprVec(ref exprs) => {
1261 try!(self.ibox(indent_unit));
1262 try!(word(&mut self.s, "["));
1263 try!(self.commasep_exprs(Inconsistent, exprs.as_slice()));
1264 try!(word(&mut self.s, "]"));
1268 ast::ExprRepeat(ref element, ref count) => {
1269 try!(self.ibox(indent_unit));
1270 try!(word(&mut self.s, "["));
1271 try!(self.print_expr(&**element));
1272 try!(word(&mut self.s, ","));
1273 try!(word(&mut self.s, ".."));
1274 try!(self.print_expr(&**count));
1275 try!(word(&mut self.s, "]"));
1279 ast::ExprStruct(ref path, ref fields, wth) => {
1280 try!(self.print_path(path, true));
1281 try!(word(&mut self.s, "{"));
1282 try!(self.commasep_cmnt(
1286 try!(s.ibox(indent_unit));
1287 try!(s.print_ident(field.ident.node));
1288 try!(s.word_space(":"));
1289 try!(s.print_expr(&*field.expr));
1295 try!(self.ibox(indent_unit));
1296 if !fields.is_empty() {
1297 try!(word(&mut self.s, ","));
1298 try!(space(&mut self.s));
1300 try!(word(&mut self.s, ".."));
1301 try!(self.print_expr(&**expr));
1304 _ => try!(word(&mut self.s, ","))
1306 try!(word(&mut self.s, "}"));
1308 ast::ExprTup(ref exprs) => {
1310 try!(self.commasep_exprs(Inconsistent, exprs.as_slice()));
1311 if exprs.len() == 1 {
1312 try!(word(&mut self.s, ","));
1314 try!(self.pclose());
1316 ast::ExprCall(ref func, ref args) => {
1317 try!(self.print_expr_maybe_paren(&**func));
1318 try!(self.print_call_post(args.as_slice()));
1320 ast::ExprMethodCall(ident, ref tys, ref args) => {
1321 let base_args = args.slice_from(1);
1322 try!(self.print_expr(&**args.get(0)));
1323 try!(word(&mut self.s, "."));
1324 try!(self.print_ident(ident.node));
1326 try!(word(&mut self.s, "::<"));
1327 try!(self.commasep(Inconsistent, tys.as_slice(),
1328 |s, ty| s.print_type_ref(ty)));
1329 try!(word(&mut self.s, ">"));
1331 try!(self.print_call_post(base_args));
1333 ast::ExprBinary(op, ref lhs, ref rhs) => {
1334 try!(self.print_expr(&**lhs));
1335 try!(space(&mut self.s));
1336 try!(self.word_space(ast_util::binop_to_string(op)));
1337 try!(self.print_expr(&**rhs));
1339 ast::ExprUnary(op, ref expr) => {
1340 try!(word(&mut self.s, ast_util::unop_to_string(op)));
1341 try!(self.print_expr_maybe_paren(&**expr));
1343 ast::ExprAddrOf(m, ref expr) => {
1344 try!(word(&mut self.s, "&"));
1345 try!(self.print_mutability(m));
1346 try!(self.print_expr_maybe_paren(&**expr));
1348 ast::ExprLit(ref lit) => try!(self.print_literal(&**lit)),
1349 ast::ExprCast(ref expr, ref ty) => {
1350 try!(self.print_expr(&**expr));
1351 try!(space(&mut self.s));
1352 try!(self.word_space("as"));
1353 try!(self.print_type(&**ty));
1355 ast::ExprIf(ref test, ref blk, elseopt) => {
1356 try!(self.print_if(&**test, &**blk, elseopt, false));
1358 ast::ExprWhile(ref test, ref blk) => {
1359 try!(self.head("while"));
1360 try!(self.print_expr(&**test));
1361 try!(space(&mut self.s));
1362 try!(self.print_block(&**blk));
1364 ast::ExprForLoop(ref pat, ref iter, ref blk, opt_ident) => {
1365 for ident in opt_ident.iter() {
1366 try!(self.print_ident(*ident));
1367 try!(self.word_space(":"));
1369 try!(self.head("for"));
1370 try!(self.print_pat(&**pat));
1371 try!(space(&mut self.s));
1372 try!(self.word_space("in"));
1373 try!(self.print_expr(&**iter));
1374 try!(space(&mut self.s));
1375 try!(self.print_block(&**blk));
1377 ast::ExprLoop(ref blk, opt_ident) => {
1378 for ident in opt_ident.iter() {
1379 try!(self.print_ident(*ident));
1380 try!(self.word_space(":"));
1382 try!(self.head("loop"));
1383 try!(space(&mut self.s));
1384 try!(self.print_block(&**blk));
1386 ast::ExprMatch(ref expr, ref arms) => {
1387 try!(self.cbox(indent_unit));
1389 try!(self.word_nbsp("match"));
1390 try!(self.print_expr(&**expr));
1391 try!(space(&mut self.s));
1393 let len = arms.len();
1394 for (i, arm) in arms.iter().enumerate() {
1395 // I have no idea why this check is necessary, but here it
1397 if arm.attrs.is_empty() {
1398 try!(space(&mut self.s));
1400 try!(self.cbox(indent_unit));
1401 try!(self.ibox(0u));
1402 try!(self.print_outer_attributes(arm.attrs.as_slice()));
1403 let mut first = true;
1404 for p in arm.pats.iter() {
1408 try!(space(&mut self.s));
1409 try!(self.word_space("|"));
1411 try!(self.print_pat(&**p));
1413 try!(space(&mut self.s));
1416 try!(self.word_space("if"));
1417 try!(self.print_expr(&**e));
1418 try!(space(&mut self.s));
1422 try!(self.word_space("=>"));
1424 match arm.body.node {
1425 ast::ExprBlock(ref blk) => {
1426 // the block will close the pattern's ibox
1427 try!(self.print_block_unclosed_indent(&**blk,
1431 try!(self.end()); // close the ibox for the pattern
1432 try!(self.print_expr(&*arm.body));
1435 if !expr_is_simple_block(expr.clone())
1437 try!(word(&mut self.s, ","));
1439 try!(self.end()); // close enclosing cbox
1441 try!(self.bclose_(expr.span, indent_unit));
1443 ast::ExprFnBlock(ref decl, ref body) => {
1444 // in do/for blocks we don't want to show an empty
1445 // argument list, but at this point we don't know which
1448 // if !decl.inputs.is_empty() {
1449 try!(self.print_fn_block_args(&**decl));
1450 try!(space(&mut self.s));
1453 if !body.stmts.is_empty() || !body.expr.is_some() {
1454 try!(self.print_block_unclosed(&**body));
1456 // we extract the block, so as not to create another set of boxes
1457 match body.expr.unwrap().node {
1458 ast::ExprBlock(ref blk) => {
1459 try!(self.print_block_unclosed(&**blk));
1462 // this is a bare expression
1463 try!(self.print_expr(&*body.expr.unwrap()));
1464 try!(self.end()); // need to close a box
1468 // a box will be closed by print_expr, but we didn't want an overall
1469 // wrapper so we closed the corresponding opening. so create an
1470 // empty box to satisfy the close.
1473 ast::ExprProc(ref decl, ref body) => {
1474 // in do/for blocks we don't want to show an empty
1475 // argument list, but at this point we don't know which
1478 // if !decl.inputs.is_empty() {
1479 try!(self.print_proc_args(&**decl));
1480 try!(space(&mut self.s));
1482 assert!(body.stmts.is_empty());
1483 assert!(body.expr.is_some());
1484 // we extract the block, so as not to create another set of boxes
1485 match body.expr.unwrap().node {
1486 ast::ExprBlock(ref blk) => {
1487 try!(self.print_block_unclosed(&**blk));
1490 // this is a bare expression
1491 try!(self.print_expr(&*body.expr.unwrap()));
1492 try!(self.end()); // need to close a box
1495 // a box will be closed by print_expr, but we didn't want an overall
1496 // wrapper so we closed the corresponding opening. so create an
1497 // empty box to satisfy the close.
1500 ast::ExprBlock(ref blk) => {
1501 // containing cbox, will be closed by print-block at }
1502 try!(self.cbox(indent_unit));
1503 // head-box, will be closed by print-block after {
1504 try!(self.ibox(0u));
1505 try!(self.print_block(&**blk));
1507 ast::ExprAssign(ref lhs, ref rhs) => {
1508 try!(self.print_expr(&**lhs));
1509 try!(space(&mut self.s));
1510 try!(self.word_space("="));
1511 try!(self.print_expr(&**rhs));
1513 ast::ExprAssignOp(op, ref lhs, ref rhs) => {
1514 try!(self.print_expr(&**lhs));
1515 try!(space(&mut self.s));
1516 try!(word(&mut self.s, ast_util::binop_to_string(op)));
1517 try!(self.word_space("="));
1518 try!(self.print_expr(&**rhs));
1520 ast::ExprField(ref expr, id, ref tys) => {
1521 try!(self.print_expr(&**expr));
1522 try!(word(&mut self.s, "."));
1523 try!(self.print_ident(id.node));
1525 try!(word(&mut self.s, "::<"));
1527 Inconsistent, tys.as_slice(),
1528 |s, ty| s.print_type_ref(ty)));
1529 try!(word(&mut self.s, ">"));
1532 ast::ExprIndex(ref expr, ref index) => {
1533 try!(self.print_expr(&**expr));
1534 try!(word(&mut self.s, "["));
1535 try!(self.print_expr(&**index));
1536 try!(word(&mut self.s, "]"));
1538 ast::ExprPath(ref path) => try!(self.print_path(path, true)),
1539 ast::ExprBreak(opt_ident) => {
1540 try!(word(&mut self.s, "break"));
1541 try!(space(&mut self.s));
1542 for ident in opt_ident.iter() {
1543 try!(self.print_ident(*ident));
1544 try!(space(&mut self.s));
1547 ast::ExprAgain(opt_ident) => {
1548 try!(word(&mut self.s, "continue"));
1549 try!(space(&mut self.s));
1550 for ident in opt_ident.iter() {
1551 try!(self.print_ident(*ident));
1552 try!(space(&mut self.s))
1555 ast::ExprRet(ref result) => {
1556 try!(word(&mut self.s, "return"));
1559 try!(word(&mut self.s, " "));
1560 try!(self.print_expr(&**expr));
1565 ast::ExprInlineAsm(ref a) => {
1567 try!(word(&mut self.s, "__volatile__ asm!"));
1569 try!(word(&mut self.s, "asm!"));
1572 try!(self.print_string(a.asm.get(), a.asm_str_style));
1573 try!(self.word_space(":"));
1575 try!(self.commasep(Inconsistent, a.outputs.as_slice(),
1576 |s, &(ref co, ref o)| {
1577 try!(s.print_string(co.get(), ast::CookedStr));
1579 try!(s.print_expr(&**o));
1583 try!(space(&mut self.s));
1584 try!(self.word_space(":"));
1586 try!(self.commasep(Inconsistent, a.inputs.as_slice(),
1587 |s, &(ref co, ref o)| {
1588 try!(s.print_string(co.get(), ast::CookedStr));
1590 try!(s.print_expr(&**o));
1594 try!(space(&mut self.s));
1595 try!(self.word_space(":"));
1597 try!(self.print_string(a.clobbers.get(), ast::CookedStr));
1598 try!(self.pclose());
1600 ast::ExprMac(ref m) => try!(self.print_mac(m)),
1601 ast::ExprParen(ref e) => {
1603 try!(self.print_expr(&**e));
1604 try!(self.pclose());
1607 try!(self.ann.post(self, NodeExpr(expr)));
1611 pub fn print_local_decl(&mut self, loc: &ast::Local) -> IoResult<()> {
1612 try!(self.print_pat(&*loc.pat));
1614 ast::TyInfer => Ok(()),
1616 try!(self.word_space(":"));
1617 self.print_type(&*loc.ty)
1622 pub fn print_decl(&mut self, decl: &ast::Decl) -> IoResult<()> {
1623 try!(self.maybe_print_comment(decl.span.lo));
1625 ast::DeclLocal(ref loc) => {
1626 try!(self.space_if_not_bol());
1627 try!(self.ibox(indent_unit));
1628 try!(self.word_nbsp("let"));
1630 try!(self.ibox(indent_unit));
1631 try!(self.print_local_decl(&**loc));
1636 try!(self.word_space("="));
1637 try!(self.print_expr(&**init));
1643 ast::DeclItem(ref item) => self.print_item(&**item)
1647 pub fn print_ident(&mut self, ident: ast::Ident) -> IoResult<()> {
1648 word(&mut self.s, token::get_ident(ident).get())
1651 pub fn print_name(&mut self, name: ast::Name) -> IoResult<()> {
1652 word(&mut self.s, token::get_name(name).get())
1655 pub fn print_for_decl(&mut self, loc: &ast::Local,
1656 coll: &ast::Expr) -> IoResult<()> {
1657 try!(self.print_local_decl(loc));
1658 try!(space(&mut self.s));
1659 try!(self.word_space("in"));
1660 self.print_expr(coll)
1663 fn print_path_(&mut self,
1665 colons_before_params: bool,
1666 opt_bounds: &Option<OwnedSlice<ast::TyParamBound>>)
1668 try!(self.maybe_print_comment(path.span.lo));
1670 try!(word(&mut self.s, "::"));
1673 let mut first = true;
1674 for segment in path.segments.iter() {
1678 try!(word(&mut self.s, "::"))
1681 try!(self.print_ident(segment.identifier));
1683 if !segment.lifetimes.is_empty() || !segment.types.is_empty() {
1684 if colons_before_params {
1685 try!(word(&mut self.s, "::"))
1687 try!(word(&mut self.s, "<"));
1689 let mut comma = false;
1690 for lifetime in segment.lifetimes.iter() {
1692 try!(self.word_space(","))
1694 try!(self.print_lifetime(lifetime));
1698 if !segment.types.is_empty() {
1700 try!(self.word_space(","))
1704 segment.types.as_slice(),
1705 |s, ty| s.print_type_ref(ty)));
1708 try!(word(&mut self.s, ">"))
1714 Some(ref bounds) => self.print_bounds(&None, bounds, true, true),
1718 fn print_path(&mut self, path: &ast::Path,
1719 colons_before_params: bool) -> IoResult<()> {
1720 self.print_path_(path, colons_before_params, &None)
1723 fn print_bounded_path(&mut self, path: &ast::Path,
1724 bounds: &Option<OwnedSlice<ast::TyParamBound>>)
1726 self.print_path_(path, false, bounds)
1729 pub fn print_pat(&mut self, pat: &ast::Pat) -> IoResult<()> {
1730 try!(self.maybe_print_comment(pat.span.lo));
1731 try!(self.ann.pre(self, NodePat(pat)));
1732 /* Pat isn't normalized, but the beauty of it
1733 is that it doesn't matter */
1735 ast::PatWild => try!(word(&mut self.s, "_")),
1736 ast::PatWildMulti => try!(word(&mut self.s, "..")),
1737 ast::PatIdent(binding_mode, ref path1, sub) => {
1738 match binding_mode {
1739 ast::BindByRef(mutbl) => {
1740 try!(self.word_nbsp("ref"));
1741 try!(self.print_mutability(mutbl));
1743 ast::BindByValue(ast::MutImmutable) => {}
1744 ast::BindByValue(ast::MutMutable) => {
1745 try!(self.word_nbsp("mut"));
1748 try!(self.print_ident(path1.node));
1751 try!(word(&mut self.s, "@"));
1752 try!(self.print_pat(&**p));
1757 ast::PatEnum(ref path, ref args_) => {
1758 try!(self.print_path(path, true));
1760 None => try!(word(&mut self.s, "(..)")),
1762 if !args.is_empty() {
1764 try!(self.commasep(Inconsistent, args.as_slice(),
1765 |s, p| s.print_pat(&**p)));
1766 try!(self.pclose());
1771 ast::PatStruct(ref path, ref fields, etc) => {
1772 try!(self.print_path(path, true));
1774 try!(self.word_space("{"));
1775 try!(self.commasep_cmnt(
1776 Consistent, fields.as_slice(),
1778 try!(s.cbox(indent_unit));
1779 try!(s.print_ident(f.ident));
1780 try!(s.word_nbsp(":"));
1781 try!(s.print_pat(&*f.pat));
1786 if fields.len() != 0u { try!(self.word_space(",")); }
1787 try!(word(&mut self.s, ".."));
1789 try!(space(&mut self.s));
1790 try!(word(&mut self.s, "}"));
1792 ast::PatTup(ref elts) => {
1794 try!(self.commasep(Inconsistent,
1796 |s, p| s.print_pat(&**p)));
1797 if elts.len() == 1 {
1798 try!(word(&mut self.s, ","));
1800 try!(self.pclose());
1802 ast::PatBox(ref inner) => {
1803 try!(word(&mut self.s, "box "));
1804 try!(self.print_pat(&**inner));
1806 ast::PatRegion(ref inner) => {
1807 try!(word(&mut self.s, "&"));
1808 try!(self.print_pat(&**inner));
1810 ast::PatLit(ref e) => try!(self.print_expr(&**e)),
1811 ast::PatRange(ref begin, ref end) => {
1812 try!(self.print_expr(&**begin));
1813 try!(space(&mut self.s));
1814 try!(word(&mut self.s, ".."));
1815 try!(self.print_expr(&**end));
1817 ast::PatVec(ref before, slice, ref after) => {
1818 try!(word(&mut self.s, "["));
1819 try!(self.commasep(Inconsistent,
1821 |s, p| s.print_pat(&**p)));
1822 for p in slice.iter() {
1823 if !before.is_empty() { try!(self.word_space(",")); }
1825 ast::Pat { node: ast::PatWildMulti, .. } => {
1826 // this case is handled by print_pat
1828 _ => try!(word(&mut self.s, "..")),
1830 try!(self.print_pat(&**p));
1831 if !after.is_empty() { try!(self.word_space(",")); }
1833 try!(self.commasep(Inconsistent,
1835 |s, p| s.print_pat(&**p)));
1836 try!(word(&mut self.s, "]"));
1838 ast::PatMac(ref m) => try!(self.print_mac(m)),
1840 self.ann.post(self, NodePat(pat))
1843 // Returns whether it printed anything
1844 fn print_explicit_self(&mut self,
1845 explicit_self: ast::ExplicitSelf_,
1846 mutbl: ast::Mutability) -> IoResult<bool> {
1847 try!(self.print_mutability(mutbl));
1848 match explicit_self {
1849 ast::SelfStatic => { return Ok(false); }
1850 ast::SelfValue(_) => {
1851 try!(word(&mut self.s, "self"));
1853 ast::SelfUniq(_) => {
1854 try!(word(&mut self.s, "~self"));
1856 ast::SelfRegion(ref lt, m, _) => {
1857 try!(word(&mut self.s, "&"));
1858 try!(self.print_opt_lifetime(lt));
1859 try!(self.print_mutability(m));
1860 try!(word(&mut self.s, "self"));
1862 ast::SelfExplicit(ref typ, _) => {
1863 try!(word(&mut self.s, "self"));
1864 try!(self.word_space(":"));
1865 try!(self.print_type(&**typ));
1871 pub fn print_fn(&mut self,
1873 fn_style: Option<ast::FnStyle>,
1876 generics: &ast::Generics,
1877 opt_explicit_self: Option<ast::ExplicitSelf_>,
1878 vis: ast::Visibility) -> IoResult<()> {
1879 try!(self.head(""));
1880 try!(self.print_fn_header_info(opt_explicit_self, fn_style, abi, vis));
1882 try!(self.print_ident(name));
1883 try!(self.print_generics(generics));
1884 self.print_fn_args_and_ret(decl, opt_explicit_self)
1887 pub fn print_fn_args(&mut self, decl: &ast::FnDecl,
1888 opt_explicit_self: Option<ast::ExplicitSelf_>)
1890 // It is unfortunate to duplicate the commasep logic, but we want the
1891 // self type and the args all in the same box.
1892 try!(self.rbox(0u, Inconsistent));
1893 let mut first = true;
1894 for &explicit_self in opt_explicit_self.iter() {
1895 let m = match explicit_self {
1896 ast::SelfStatic => ast::MutImmutable,
1897 _ => match decl.inputs.get(0).pat.node {
1898 ast::PatIdent(ast::BindByValue(m), _, _) => m,
1899 _ => ast::MutImmutable
1902 first = !try!(self.print_explicit_self(explicit_self, m));
1905 // HACK(eddyb) ignore the separately printed self argument.
1906 let args = if first {
1907 decl.inputs.as_slice()
1909 decl.inputs.slice_from(1)
1912 for arg in args.iter() {
1913 if first { first = false; } else { try!(self.word_space(",")); }
1914 try!(self.print_arg(arg));
1920 pub fn print_fn_args_and_ret(&mut self, decl: &ast::FnDecl,
1921 opt_explicit_self: Option<ast::ExplicitSelf_>)
1924 try!(self.print_fn_args(decl, opt_explicit_self));
1926 try!(word(&mut self.s, ", ..."));
1928 try!(self.pclose());
1930 try!(self.maybe_print_comment(decl.output.span.lo));
1931 match decl.output.node {
1932 ast::TyNil => Ok(()),
1934 try!(self.space_if_not_bol());
1935 try!(self.word_space("->"));
1936 self.print_type(&*decl.output)
1941 pub fn print_fn_block_args(&mut self,
1942 decl: &ast::FnDecl) -> IoResult<()> {
1943 try!(word(&mut self.s, "|"));
1944 try!(self.print_fn_args(decl, None));
1945 try!(word(&mut self.s, "|"));
1947 match decl.output.node {
1950 try!(self.space_if_not_bol());
1951 try!(self.word_space("->"));
1952 try!(self.print_type(&*decl.output));
1956 self.maybe_print_comment(decl.output.span.lo)
1959 pub fn print_proc_args(&mut self, decl: &ast::FnDecl) -> IoResult<()> {
1960 try!(word(&mut self.s, "proc"));
1961 try!(word(&mut self.s, "("));
1962 try!(self.print_fn_args(decl, None));
1963 try!(word(&mut self.s, ")"));
1965 match decl.output.node {
1968 try!(self.space_if_not_bol());
1969 try!(self.word_space("->"));
1970 try!(self.print_type(&*decl.output));
1974 self.maybe_print_comment(decl.output.span.lo)
1977 pub fn print_bounds(&mut self,
1978 region: &Option<ast::Lifetime>,
1979 bounds: &OwnedSlice<ast::TyParamBound>,
1980 print_colon_anyway: bool,
1981 print_plus_before_bounds: bool)
1983 let separator = if print_plus_before_bounds {
1988 if !bounds.is_empty() || region.is_some() {
1989 try!(word(&mut self.s, separator));
1990 let mut first = true;
1993 let token = token::get_name(lt.name);
1994 if token.get() != "'static" {
1997 try!(self.print_lifetime(lt));
2002 for bound in bounds.iter() {
2007 try!(self.word_space("+"));
2011 TraitTyParamBound(ref tref) => self.print_trait_ref(tref),
2012 StaticRegionTyParamBound => word(&mut self.s, "'static"),
2013 UnboxedFnTyParamBound(ref unboxed_function_type) => {
2014 self.print_ty_fn(None,
2019 &*unboxed_function_type.decl,
2026 OtherRegionTyParamBound(_) => Ok(())
2030 } else if print_colon_anyway {
2031 word(&mut self.s, separator)
2037 pub fn print_lifetime(&mut self,
2038 lifetime: &ast::Lifetime) -> IoResult<()> {
2039 self.print_name(lifetime.name)
2042 pub fn print_generics(&mut self,
2043 generics: &ast::Generics) -> IoResult<()> {
2044 let total = generics.lifetimes.len() + generics.ty_params.len();
2046 try!(word(&mut self.s, "<"));
2048 let mut ints = Vec::new();
2049 for i in range(0u, total) {
2054 Inconsistent, ints.as_slice(),
2056 if idx < generics.lifetimes.len() {
2057 let lifetime = generics.lifetimes.get(idx);
2058 s.print_lifetime(lifetime)
2060 let idx = idx - generics.lifetimes.len();
2061 let param = generics.ty_params.get(idx);
2062 match param.unbound {
2063 Some(TraitTyParamBound(ref tref)) => {
2064 try!(s.print_trait_ref(tref));
2065 try!(s.word_space("?"));
2069 try!(s.print_ident(param.ident));
2070 try!(s.print_bounds(&None,
2074 match param.default {
2075 Some(ref default) => {
2076 try!(space(&mut s.s));
2077 try!(s.word_space("="));
2078 s.print_type(&**default)
2084 word(&mut self.s, ">")
2090 pub fn print_meta_item(&mut self, item: &ast::MetaItem) -> IoResult<()> {
2091 try!(self.ibox(indent_unit));
2093 ast::MetaWord(ref name) => {
2094 try!(word(&mut self.s, name.get()));
2096 ast::MetaNameValue(ref name, ref value) => {
2097 try!(self.word_space(name.get()));
2098 try!(self.word_space("="));
2099 try!(self.print_literal(value));
2101 ast::MetaList(ref name, ref items) => {
2102 try!(word(&mut self.s, name.get()));
2104 try!(self.commasep(Consistent,
2106 |s, i| s.print_meta_item(&**i)));
2107 try!(self.pclose());
2113 pub fn print_view_path(&mut self, vp: &ast::ViewPath) -> IoResult<()> {
2115 ast::ViewPathSimple(ident, ref path, _) => {
2116 // FIXME(#6993) can't compare identifiers directly here
2117 if path.segments.last().unwrap().identifier.name != ident.name {
2118 try!(self.print_ident(ident));
2119 try!(space(&mut self.s));
2120 try!(self.word_space("="));
2122 self.print_path(path, false)
2125 ast::ViewPathGlob(ref path, _) => {
2126 try!(self.print_path(path, false));
2127 word(&mut self.s, "::*")
2130 ast::ViewPathList(ref path, ref idents, _) => {
2131 if path.segments.is_empty() {
2132 try!(word(&mut self.s, "{"));
2134 try!(self.print_path(path, false));
2135 try!(word(&mut self.s, "::{"));
2137 try!(self.commasep(Inconsistent, idents.as_slice(), |s, w| {
2138 s.print_ident(w.node.name)
2140 word(&mut self.s, "}")
2145 pub fn print_view_item(&mut self, item: &ast::ViewItem) -> IoResult<()> {
2146 try!(self.hardbreak_if_not_bol());
2147 try!(self.maybe_print_comment(item.span.lo));
2148 try!(self.print_outer_attributes(item.attrs.as_slice()));
2149 try!(self.print_visibility(item.vis));
2151 ast::ViewItemExternCrate(id, ref optional_path, _) => {
2152 try!(self.head("extern crate"));
2153 try!(self.print_ident(id));
2154 for &(ref p, style) in optional_path.iter() {
2155 try!(space(&mut self.s));
2156 try!(word(&mut self.s, "="));
2157 try!(space(&mut self.s));
2158 try!(self.print_string(p.get(), style));
2162 ast::ViewItemUse(ref vp) => {
2163 try!(self.head("use"));
2164 try!(self.print_view_path(&**vp));
2167 try!(word(&mut self.s, ";"));
2168 try!(self.end()); // end inner head-block
2169 self.end() // end outer head-block
2172 pub fn print_mutability(&mut self,
2173 mutbl: ast::Mutability) -> IoResult<()> {
2175 ast::MutMutable => self.word_nbsp("mut"),
2176 ast::MutImmutable => Ok(()),
2180 pub fn print_mt(&mut self, mt: &ast::MutTy) -> IoResult<()> {
2181 try!(self.print_mutability(mt.mutbl));
2182 self.print_type(&*mt.ty)
2185 pub fn print_arg(&mut self, input: &ast::Arg) -> IoResult<()> {
2186 try!(self.ibox(indent_unit));
2187 match input.ty.node {
2188 ast::TyInfer => try!(self.print_pat(&*input.pat)),
2190 match input.pat.node {
2191 ast::PatIdent(_, ref path1, _) if
2193 parse::token::special_idents::invalid.name => {
2197 try!(self.print_pat(&*input.pat));
2198 try!(word(&mut self.s, ":"));
2199 try!(space(&mut self.s));
2202 try!(self.print_type(&*input.ty));
2208 pub fn print_ty_fn(&mut self,
2209 opt_abi: Option<abi::Abi>,
2210 opt_sigil: Option<char>,
2211 opt_region: &Option<ast::Lifetime>,
2212 fn_style: ast::FnStyle,
2213 onceness: ast::Onceness,
2215 id: Option<ast::Ident>,
2216 opt_bounds: &Option<OwnedSlice<ast::TyParamBound>>,
2217 generics: Option<&ast::Generics>,
2218 opt_explicit_self: Option<ast::ExplicitSelf_>,
2221 try!(self.ibox(indent_unit));
2223 // Duplicates the logic in `print_fn_header_info()`. This is because that
2224 // function prints the sigil in the wrong place. That should be fixed.
2225 if opt_sigil == Some('~') && onceness == ast::Once {
2226 try!(word(&mut self.s, "proc"));
2227 } else if opt_sigil == Some('&') {
2228 try!(self.print_fn_style(fn_style));
2229 try!(self.print_extern_opt_abi(opt_abi));
2230 try!(self.print_onceness(onceness));
2232 assert!(opt_sigil.is_none());
2233 try!(self.print_fn_style(fn_style));
2234 try!(self.print_opt_abi_and_extern_if_nondefault(opt_abi));
2235 try!(self.print_onceness(onceness));
2237 try!(word(&mut self.s, "fn"));
2243 try!(word(&mut self.s, " "));
2244 try!(self.print_ident(id));
2249 match generics { Some(g) => try!(self.print_generics(g)), _ => () }
2250 try!(zerobreak(&mut self.s));
2252 if is_unboxed || opt_sigil == Some('&') {
2253 try!(word(&mut self.s, "|"));
2259 try!(word(&mut self.s, "&mut"));
2260 try!(self.word_space(":"));
2263 try!(self.print_fn_args(decl, opt_explicit_self));
2265 if is_unboxed || opt_sigil == Some('&') {
2266 try!(word(&mut self.s, "|"));
2269 try!(word(&mut self.s, ", ..."));
2271 try!(self.pclose());
2274 opt_bounds.as_ref().map(|bounds| {
2275 self.print_bounds(opt_region, bounds, true, false)
2278 try!(self.maybe_print_comment(decl.output.span.lo));
2280 match decl.output.node {
2283 try!(self.space_if_not_bol());
2284 try!(self.ibox(indent_unit));
2285 try!(self.word_space("->"));
2286 if decl.cf == ast::NoReturn {
2287 try!(self.word_nbsp("!"));
2289 try!(self.print_type(&*decl.output));
2298 pub fn maybe_print_trailing_comment(&mut self, span: codemap::Span,
2299 next_pos: Option<BytePos>)
2301 let cm = match self.cm {
2305 match self.next_comment() {
2307 if (*cmnt).style != comments::Trailing { return Ok(()) }
2308 let span_line = cm.lookup_char_pos(span.hi);
2309 let comment_line = cm.lookup_char_pos((*cmnt).pos);
2310 let mut next = (*cmnt).pos + BytePos(1);
2311 match next_pos { None => (), Some(p) => next = p }
2312 if span.hi < (*cmnt).pos && (*cmnt).pos < next &&
2313 span_line.line == comment_line.line {
2314 try!(self.print_comment(cmnt));
2315 self.cur_cmnt_and_lit.cur_cmnt += 1u;
2323 pub fn print_remaining_comments(&mut self) -> IoResult<()> {
2324 // If there aren't any remaining comments, then we need to manually
2325 // make sure there is a line break at the end.
2326 if self.next_comment().is_none() {
2327 try!(hardbreak(&mut self.s));
2330 match self.next_comment() {
2332 try!(self.print_comment(cmnt));
2333 self.cur_cmnt_and_lit.cur_cmnt += 1u;
2341 pub fn print_literal(&mut self, lit: &ast::Lit) -> IoResult<()> {
2342 try!(self.maybe_print_comment(lit.span.lo));
2343 match self.next_lit(lit.span.lo) {
2345 return word(&mut self.s, (*ltrl).lit.as_slice());
2350 ast::LitStr(ref st, style) => self.print_string(st.get(), style),
2351 ast::LitByte(byte) => {
2352 let mut res = String::from_str("b'");
2353 (byte as char).escape_default(|c| res.push_char(c));
2354 res.push_char('\'');
2355 word(&mut self.s, res.as_slice())
2357 ast::LitChar(ch) => {
2358 let mut res = String::from_str("'");
2359 ch.escape_default(|c| res.push_char(c));
2360 res.push_char('\'');
2361 word(&mut self.s, res.as_slice())
2363 ast::LitInt(i, t) => {
2365 ast_util::int_ty_to_string(t, Some(i)).as_slice())
2367 ast::LitUint(u, t) => {
2369 ast_util::uint_ty_to_string(t, Some(u)).as_slice())
2371 ast::LitIntUnsuffixed(i) => {
2372 word(&mut self.s, format!("{}", i).as_slice())
2374 ast::LitFloat(ref f, t) => {
2379 ast_util::float_ty_to_string(t).as_slice()).as_slice())
2381 ast::LitFloatUnsuffixed(ref f) => word(&mut self.s, f.get()),
2382 ast::LitNil => word(&mut self.s, "()"),
2383 ast::LitBool(val) => {
2384 if val { word(&mut self.s, "true") } else { word(&mut self.s, "false") }
2386 ast::LitBinary(ref v) => {
2387 let escaped: String = v.iter().map(|&b| b as char).collect();
2388 word(&mut self.s, format!("b\"{}\"", escaped.escape_default()).as_slice())
2393 pub fn next_lit(&mut self, pos: BytePos) -> Option<comments::Literal> {
2394 match self.literals {
2396 while self.cur_cmnt_and_lit.cur_lit < lits.len() {
2397 let ltrl = (*(*lits).get(self.cur_cmnt_and_lit.cur_lit)).clone();
2398 if ltrl.pos > pos { return None; }
2399 self.cur_cmnt_and_lit.cur_lit += 1u;
2400 if ltrl.pos == pos { return Some(ltrl); }
2408 pub fn maybe_print_comment(&mut self, pos: BytePos) -> IoResult<()> {
2410 match self.next_comment() {
2412 if (*cmnt).pos < pos {
2413 try!(self.print_comment(cmnt));
2414 self.cur_cmnt_and_lit.cur_cmnt += 1u;
2423 pub fn print_comment(&mut self,
2424 cmnt: &comments::Comment) -> IoResult<()> {
2426 comments::Mixed => {
2427 assert_eq!(cmnt.lines.len(), 1u);
2428 try!(zerobreak(&mut self.s));
2429 try!(word(&mut self.s, cmnt.lines.get(0).as_slice()));
2430 zerobreak(&mut self.s)
2432 comments::Isolated => {
2433 try!(self.hardbreak_if_not_bol());
2434 for line in cmnt.lines.iter() {
2435 // Don't print empty lines because they will end up as trailing
2437 if !line.is_empty() {
2438 try!(word(&mut self.s, line.as_slice()));
2440 try!(hardbreak(&mut self.s));
2444 comments::Trailing => {
2445 try!(word(&mut self.s, " "));
2446 if cmnt.lines.len() == 1u {
2447 try!(word(&mut self.s, cmnt.lines.get(0).as_slice()));
2448 hardbreak(&mut self.s)
2450 try!(self.ibox(0u));
2451 for line in cmnt.lines.iter() {
2452 if !line.is_empty() {
2453 try!(word(&mut self.s, line.as_slice()));
2455 try!(hardbreak(&mut self.s));
2460 comments::BlankLine => {
2461 // We need to do at least one, possibly two hardbreaks.
2462 let is_semi = match self.s.last_token() {
2463 pp::String(s, _) => ";" == s.as_slice(),
2466 if is_semi || self.is_begin() || self.is_end() {
2467 try!(hardbreak(&mut self.s));
2469 hardbreak(&mut self.s)
2474 pub fn print_string(&mut self, st: &str,
2475 style: ast::StrStyle) -> IoResult<()> {
2476 let st = match style {
2478 (format!("\"{}\"", st.escape_default()))
2481 (format!("r{delim}\"{string}\"{delim}",
2482 delim="#".repeat(n),
2486 word(&mut self.s, st.as_slice())
2489 pub fn next_comment(&mut self) -> Option<comments::Comment> {
2490 match self.comments {
2491 Some(ref cmnts) => {
2492 if self.cur_cmnt_and_lit.cur_cmnt < cmnts.len() {
2493 Some((*cmnts.get(self.cur_cmnt_and_lit.cur_cmnt)).clone())
2502 pub fn print_opt_fn_style(&mut self,
2503 opt_fn_style: Option<ast::FnStyle>) -> IoResult<()> {
2504 match opt_fn_style {
2505 Some(fn_style) => self.print_fn_style(fn_style),
2510 pub fn print_opt_abi_and_extern_if_nondefault(&mut self,
2511 opt_abi: Option<abi::Abi>)
2514 Some(abi::Rust) => Ok(()),
2516 try!(self.word_nbsp("extern"));
2517 self.word_nbsp(abi.to_string().as_slice())
2523 pub fn print_extern_opt_abi(&mut self,
2524 opt_abi: Option<abi::Abi>) -> IoResult<()> {
2527 try!(self.word_nbsp("extern"));
2528 self.word_nbsp(abi.to_string().as_slice())
2534 pub fn print_fn_header_info(&mut self,
2535 _opt_explicit_self: Option<ast::ExplicitSelf_>,
2536 opt_fn_style: Option<ast::FnStyle>,
2538 vis: ast::Visibility) -> IoResult<()> {
2539 try!(word(&mut self.s, visibility_qualified(vis, "").as_slice()));
2540 try!(self.print_opt_fn_style(opt_fn_style));
2542 if abi != abi::Rust {
2543 try!(self.word_nbsp("extern"));
2544 try!(self.word_nbsp(abi.to_string().as_slice()));
2547 word(&mut self.s, "fn")
2550 pub fn print_fn_style(&mut self, s: ast::FnStyle) -> IoResult<()> {
2552 ast::NormalFn => Ok(()),
2553 ast::UnsafeFn => self.word_nbsp("unsafe"),
2557 pub fn print_onceness(&mut self, o: ast::Onceness) -> IoResult<()> {
2559 ast::Once => self.word_nbsp("once"),
2575 fn test_fun_to_string() {
2576 let abba_ident = token::str_to_ident("abba");
2578 let decl = ast::FnDecl {
2580 output: ast::P(ast::Ty {id: 0,
2582 span: codemap::DUMMY_SP}),
2586 let generics = ast_util::empty_generics();
2587 assert_eq!(&fun_to_string(&decl, ast::NormalFn, abba_ident,
2589 &"fn abba()".to_string());
2593 fn test_variant_to_string() {
2594 let ident = token::str_to_ident("principal_skinner");
2596 let var = codemap::respan(codemap::DUMMY_SP, ast::Variant_ {
2599 // making this up as I go.... ?
2600 kind: ast::TupleVariantKind(Vec::new()),
2606 let varstr = variant_to_string(&var);
2607 assert_eq!(&varstr,&"pub principal_skinner".to_string());