2012-12-04 00:48:01 +00:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-03-14 02:25:28 +00:00
|
|
|
use abi::AbiSet;
|
2014-01-09 13:05:33 +00:00
|
|
|
use ast::{P, RegionTyParamBound, TraitTyParamBound, Required, Provided};
|
2012-12-23 22:41:37 +00:00
|
|
|
use ast;
|
|
|
|
use ast_util;
|
2013-02-15 05:50:03 +00:00
|
|
|
use opt_vec::OptVec;
|
2013-03-27 16:55:18 +00:00
|
|
|
use opt_vec;
|
2013-07-19 11:51:37 +00:00
|
|
|
use attr::{AttrMetaMethods, AttributeMethods};
|
2012-12-23 22:41:37 +00:00
|
|
|
use codemap::{CodeMap, BytePos};
|
|
|
|
use codemap;
|
|
|
|
use diagnostic;
|
2013-03-26 20:38:07 +00:00
|
|
|
use parse::classify::expr_is_simple_block;
|
2014-01-09 13:05:33 +00:00
|
|
|
use parse::token::{IdentInterner, ident_to_str, interner_get};
|
2013-03-26 20:38:07 +00:00
|
|
|
use parse::{comments, token};
|
2012-12-23 22:41:37 +00:00
|
|
|
use parse;
|
2013-03-26 20:38:07 +00:00
|
|
|
use print::pp::{break_offset, word, space, zerobreak, hardbreak};
|
2014-01-09 13:05:33 +00:00
|
|
|
use print::pp::{Breaks, Consistent, Inconsistent, eof};
|
2012-12-23 22:41:37 +00:00
|
|
|
use print::pp;
|
|
|
|
use print::pprust;
|
|
|
|
|
2013-12-27 22:28:54 +00:00
|
|
|
use std::cast;
|
2013-12-31 00:37:45 +00:00
|
|
|
use std::cell::RefCell;
|
2013-09-03 23:24:12 +00:00
|
|
|
use std::char;
|
2013-10-14 01:48:47 +00:00
|
|
|
use std::str;
|
2013-11-11 06:46:32 +00:00
|
|
|
use std::io;
|
2014-01-15 21:25:09 +00:00
|
|
|
use std::io::MemWriter;
|
2013-05-25 02:35:29 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
// The &mut State is stored here to prevent recursive type.
|
|
|
|
pub enum AnnNode<'a,'b> {
|
|
|
|
NodeBlock(&'b mut State, &'a ast::Block),
|
|
|
|
NodeItem(&'b mut State, &'a ast::Item),
|
|
|
|
NodeExpr(&'b mut State, &'a ast::Expr),
|
|
|
|
NodePat(&'b mut State, &'a ast::Pat),
|
2011-07-05 09:48:19 +00:00
|
|
|
}
|
2013-08-29 22:24:33 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub trait PpAnn {
|
|
|
|
fn pre(&self, _node: AnnNode) {}
|
|
|
|
fn post(&self, _node: AnnNode) {}
|
2013-08-29 22:24:33 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub struct NoAnn;
|
2011-07-05 09:48:19 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
impl PpAnn for NoAnn {}
|
2013-08-29 22:24:33 +00:00
|
|
|
|
2013-02-04 22:02:01 +00:00
|
|
|
pub struct CurrentCommentAndLiteral {
|
|
|
|
cur_cmnt: uint,
|
|
|
|
cur_lit: uint,
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub struct State {
|
2013-12-27 22:11:01 +00:00
|
|
|
s: pp::Printer,
|
2013-02-04 22:02:01 +00:00
|
|
|
cm: Option<@CodeMap>,
|
2014-01-09 13:05:33 +00:00
|
|
|
intr: @token::IdentInterner,
|
|
|
|
comments: Option<~[comments::Comment]>,
|
|
|
|
literals: Option<~[comments::Literal]>,
|
2013-12-27 23:42:43 +00:00
|
|
|
cur_cmnt_and_lit: CurrentCommentAndLiteral,
|
2014-01-09 13:05:33 +00:00
|
|
|
boxes: RefCell<~[pp::Breaks]>,
|
|
|
|
ann: @PpAnn
|
2013-02-04 22:02:01 +00:00
|
|
|
}
|
2011-07-05 09:48:19 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn ibox(s: &mut State, u: uint) {
|
2013-12-31 00:37:45 +00:00
|
|
|
{
|
|
|
|
let mut boxes = s.boxes.borrow_mut();
|
2014-01-09 13:05:33 +00:00
|
|
|
boxes.get().push(pp::Inconsistent);
|
2013-12-31 00:37:45 +00:00
|
|
|
}
|
2013-12-27 22:11:01 +00:00
|
|
|
pp::ibox(&mut s.s, u);
|
2012-05-10 14:24:56 +00:00
|
|
|
}
|
2011-07-05 09:48:19 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn end(s: &mut State) {
|
2013-12-31 00:37:45 +00:00
|
|
|
{
|
|
|
|
let mut boxes = s.boxes.borrow_mut();
|
|
|
|
boxes.get().pop();
|
|
|
|
}
|
2013-12-27 22:11:01 +00:00
|
|
|
pp::end(&mut s.s);
|
2012-05-10 14:24:56 +00:00
|
|
|
}
|
2011-07-05 09:48:19 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn rust_printer(writer: ~io::Writer, intr: @IdentInterner) -> State {
|
|
|
|
return rust_printer_annotated(writer, intr, @NoAnn as @PpAnn);
|
2013-03-15 19:24:24 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 22:28:54 +00:00
|
|
|
pub fn rust_printer_annotated(writer: ~io::Writer,
|
2014-01-09 13:05:33 +00:00
|
|
|
intr: @IdentInterner,
|
|
|
|
ann: @PpAnn)
|
|
|
|
-> State {
|
|
|
|
return State {
|
2013-02-04 22:02:01 +00:00
|
|
|
s: pp::mk_printer(writer, default_columns),
|
2014-01-09 13:05:33 +00:00
|
|
|
cm: None,
|
2013-02-04 22:02:01 +00:00
|
|
|
intr: intr,
|
2014-01-09 13:05:33 +00:00
|
|
|
comments: None,
|
|
|
|
literals: None,
|
2013-12-27 23:42:43 +00:00
|
|
|
cur_cmnt_and_lit: CurrentCommentAndLiteral {
|
2013-02-04 22:02:01 +00:00
|
|
|
cur_cmnt: 0,
|
|
|
|
cur_lit: 0
|
|
|
|
},
|
2013-12-31 00:37:45 +00:00
|
|
|
boxes: RefCell::new(~[]),
|
2013-03-15 19:24:24 +00:00
|
|
|
ann: ann
|
2013-02-04 22:02:01 +00:00
|
|
|
};
|
2011-07-05 09:48:19 +00:00
|
|
|
}
|
|
|
|
|
2013-03-22 21:00:15 +00:00
|
|
|
pub static indent_unit: uint = 4u;
|
2011-07-05 09:48:19 +00:00
|
|
|
|
2013-03-22 21:00:15 +00:00
|
|
|
pub static default_columns: uint = 78u;
|
2011-07-05 09:48:19 +00:00
|
|
|
|
2011-07-25 21:04:38 +00:00
|
|
|
// Requires you to pass an input filename and reader so that
|
|
|
|
// it can scan the input text for comments and literals to
|
|
|
|
// copy forward.
|
2013-03-12 20:00:50 +00:00
|
|
|
pub fn print_crate(cm: @CodeMap,
|
2014-01-09 13:05:33 +00:00
|
|
|
intr: @IdentInterner,
|
2013-12-27 21:48:00 +00:00
|
|
|
span_diagnostic: @diagnostic::SpanHandler,
|
2013-07-19 05:38:55 +00:00
|
|
|
crate: &ast::Crate,
|
2013-06-12 17:02:55 +00:00
|
|
|
filename: @str,
|
2013-12-28 02:07:12 +00:00
|
|
|
input: &mut io::Reader,
|
2013-12-27 22:28:54 +00:00
|
|
|
out: ~io::Writer,
|
2014-01-09 13:05:33 +00:00
|
|
|
ann: @PpAnn,
|
2013-03-12 20:00:50 +00:00
|
|
|
is_expanded: bool) {
|
2013-02-26 15:43:53 +00:00
|
|
|
let (cmnts, lits) = comments::gather_comments_and_literals(
|
|
|
|
span_diagnostic,
|
2013-06-12 17:02:55 +00:00
|
|
|
filename,
|
2013-07-31 21:59:59 +00:00
|
|
|
input
|
2013-02-26 15:43:53 +00:00
|
|
|
);
|
2014-01-09 13:05:33 +00:00
|
|
|
let mut s = State {
|
2013-02-04 22:02:01 +00:00
|
|
|
s: pp::mk_printer(out, default_columns),
|
|
|
|
cm: Some(cm),
|
|
|
|
intr: intr,
|
2013-07-02 19:47:32 +00:00
|
|
|
comments: Some(cmnts),
|
2013-02-04 22:02:01 +00:00
|
|
|
// If the code is post expansion, don't use the table of
|
|
|
|
// literals, since it doesn't correspond with the literals
|
|
|
|
// in the AST anymore.
|
2013-07-02 19:47:32 +00:00
|
|
|
literals: if is_expanded {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(lits)
|
|
|
|
},
|
2013-12-27 23:42:43 +00:00
|
|
|
cur_cmnt_and_lit: CurrentCommentAndLiteral {
|
2013-02-04 22:02:01 +00:00
|
|
|
cur_cmnt: 0,
|
|
|
|
cur_lit: 0
|
|
|
|
},
|
2013-12-31 00:37:45 +00:00
|
|
|
boxes: RefCell::new(~[]),
|
2013-02-04 22:02:01 +00:00
|
|
|
ann: ann
|
|
|
|
};
|
2013-12-27 22:11:01 +00:00
|
|
|
print_crate_(&mut s, crate);
|
2012-02-21 23:34:26 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_crate_(s: &mut State, crate: &ast::Crate) {
|
2013-07-19 05:38:55 +00:00
|
|
|
print_mod(s, &crate.module, crate.attrs);
|
2011-07-05 21:34:34 +00:00
|
|
|
print_remaining_comments(s);
|
2013-12-27 22:11:01 +00:00
|
|
|
eof(&mut s.s);
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn ty_to_str(ty: &ast::Ty, intr: @IdentInterner) -> ~str {
|
2012-07-18 23:18:02 +00:00
|
|
|
to_str(ty, print_type, intr)
|
|
|
|
}
|
2011-06-15 18:19:50 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn pat_to_str(pat: &ast::Pat, intr: @IdentInterner) -> ~str {
|
2013-06-21 16:19:22 +00:00
|
|
|
to_str(pat, print_pat, intr)
|
2012-07-18 23:18:02 +00:00
|
|
|
}
|
2011-06-15 18:19:50 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn expr_to_str(e: &ast::Expr, intr: @IdentInterner) -> ~str {
|
2012-07-18 23:18:02 +00:00
|
|
|
to_str(e, print_expr, intr)
|
|
|
|
}
|
2011-06-15 18:19:50 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn lifetime_to_str(e: &ast::Lifetime, intr: @IdentInterner) -> ~str {
|
2013-02-28 00:41:02 +00:00
|
|
|
to_str(e, print_lifetime, intr)
|
2013-02-27 21:21:07 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn tt_to_str(tt: &ast::TokenTree, intr: @IdentInterner) -> ~str {
|
2013-07-06 00:47:42 +00:00
|
|
|
to_str(tt, print_tt, intr)
|
2012-08-07 00:50:45 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn tts_to_str(tts: &[ast::TokenTree], intr: @IdentInterner) -> ~str {
|
2013-07-06 00:47:42 +00:00
|
|
|
to_str(&tts, print_tts, intr)
|
2012-12-13 01:08:09 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn stmt_to_str(s: &ast::Stmt, intr: @IdentInterner) -> ~str {
|
2012-07-18 23:18:02 +00:00
|
|
|
to_str(s, print_stmt, intr)
|
|
|
|
}
|
2011-06-15 18:19:50 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn item_to_str(i: &ast::Item, intr: @IdentInterner) -> ~str {
|
2012-07-18 23:18:02 +00:00
|
|
|
to_str(i, print_item, intr)
|
|
|
|
}
|
2011-06-15 18:19:50 +00:00
|
|
|
|
2013-02-15 05:50:03 +00:00
|
|
|
pub fn generics_to_str(generics: &ast::Generics,
|
2014-01-09 13:05:33 +00:00
|
|
|
intr: @IdentInterner) -> ~str {
|
2013-02-15 05:50:03 +00:00
|
|
|
to_str(generics, print_generics, intr)
|
2012-02-02 06:41:41 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn path_to_str(p: &ast::Path, intr: @IdentInterner) -> ~str {
|
2012-07-18 23:18:02 +00:00
|
|
|
to_str(p, |a,b| print_path(a, b, false), intr)
|
2011-08-16 16:03:58 +00:00
|
|
|
}
|
2011-05-31 17:58:30 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn fun_to_str(decl: &ast::FnDecl, purity: ast::Purity, name: ast::Ident,
|
|
|
|
opt_explicit_self: Option<ast::ExplicitSelf_>,
|
|
|
|
generics: &ast::Generics, intr: @IdentInterner) -> ~str {
|
2013-12-27 22:28:54 +00:00
|
|
|
let wr = ~MemWriter::new();
|
|
|
|
let mut s = rust_printer(wr as ~io::Writer, intr);
|
2013-12-27 22:11:01 +00:00
|
|
|
print_fn(&mut s, decl, Some(purity), AbiSet::Rust(),
|
2014-01-09 13:05:33 +00:00
|
|
|
name, generics, opt_explicit_self, ast::Inherited);
|
2013-12-27 22:11:01 +00:00
|
|
|
end(&mut s); // Close the head box
|
|
|
|
end(&mut s); // Close the outer box
|
|
|
|
eof(&mut s.s);
|
2013-12-27 22:28:54 +00:00
|
|
|
unsafe {
|
2013-12-28 02:07:12 +00:00
|
|
|
get_mem_writer(&mut s.s.out)
|
2013-12-27 22:28:54 +00:00
|
|
|
}
|
2011-03-09 10:41:50 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn block_to_str(blk: &ast::Block, intr: @IdentInterner) -> ~str {
|
2013-12-27 22:28:54 +00:00
|
|
|
let wr = ~MemWriter::new();
|
|
|
|
let mut s = rust_printer(wr as ~io::Writer, intr);
|
2013-10-14 01:48:47 +00:00
|
|
|
// containing cbox, will be closed by print-block at }
|
2013-12-27 22:11:01 +00:00
|
|
|
cbox(&mut s, indent_unit);
|
2013-10-14 01:48:47 +00:00
|
|
|
// head-ibox, will be closed by print-block after {
|
2013-12-27 22:11:01 +00:00
|
|
|
ibox(&mut s, 0u);
|
|
|
|
print_block(&mut s, blk);
|
|
|
|
eof(&mut s.s);
|
2013-12-27 22:28:54 +00:00
|
|
|
unsafe {
|
2013-12-28 02:07:12 +00:00
|
|
|
get_mem_writer(&mut s.s.out)
|
2013-12-27 22:28:54 +00:00
|
|
|
}
|
2011-04-01 00:29:59 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn meta_item_to_str(mi: &ast::MetaItem, intr: @IdentInterner) -> ~str {
|
2012-08-22 23:43:23 +00:00
|
|
|
to_str(mi, print_meta_item, intr)
|
2011-06-28 01:32:15 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn attribute_to_str(attr: &ast::Attribute, intr: @IdentInterner) -> ~str {
|
2012-07-18 23:18:02 +00:00
|
|
|
to_str(attr, print_attribute, intr)
|
2011-06-28 02:41:48 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn variant_to_str(var: &ast::Variant, intr: @IdentInterner) -> ~str {
|
2012-07-18 23:18:02 +00:00
|
|
|
to_str(var, print_variant, intr)
|
2012-01-26 01:22:08 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn cbox(s: &mut State, u: uint) {
|
2013-12-31 00:37:45 +00:00
|
|
|
{
|
|
|
|
let mut boxes = s.boxes.borrow_mut();
|
2014-01-09 13:05:33 +00:00
|
|
|
boxes.get().push(pp::Consistent);
|
2013-12-31 00:37:45 +00:00
|
|
|
}
|
2013-12-27 22:11:01 +00:00
|
|
|
pp::cbox(&mut s.s, u);
|
2012-05-10 14:24:56 +00:00
|
|
|
}
|
2011-06-01 22:29:38 +00:00
|
|
|
|
2013-12-12 01:04:50 +00:00
|
|
|
// "raw box"
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn rbox(s: &mut State, u: uint, b: pp::Breaks) {
|
2013-12-31 00:37:45 +00:00
|
|
|
{
|
|
|
|
let mut boxes = s.boxes.borrow_mut();
|
|
|
|
boxes.get().push(b);
|
|
|
|
}
|
2013-12-27 22:11:01 +00:00
|
|
|
pp::rbox(&mut s.s, u, b);
|
2012-05-10 14:24:56 +00:00
|
|
|
}
|
2011-06-01 22:29:38 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn nbsp(s: &mut State) { word(&mut s.s, " "); }
|
2011-07-26 22:58:43 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn word_nbsp(s: &mut State, w: &str) { word(&mut s.s, w); nbsp(s); }
|
2011-05-29 02:16:18 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn word_space(s: &mut State, w: &str) { word(&mut s.s, w); space(&mut s.s); }
|
2011-05-29 02:16:18 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn popen(s: &mut State) { word(&mut s.s, "("); }
|
2011-05-29 02:16:18 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn pclose(s: &mut State) { word(&mut s.s, ")"); }
|
2011-05-29 02:16:18 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn head(s: &mut State, w: &str) {
|
2011-05-29 02:16:18 +00:00
|
|
|
// outer-box is consistent
|
2011-06-01 22:29:38 +00:00
|
|
|
cbox(s, indent_unit);
|
2011-05-29 02:16:18 +00:00
|
|
|
// head-box is inconsistent
|
2013-06-09 14:44:58 +00:00
|
|
|
ibox(s, w.len() + 1);
|
2011-05-29 02:16:18 +00:00
|
|
|
// keyword that starts the head
|
2012-11-05 04:41:00 +00:00
|
|
|
if !w.is_empty() {
|
|
|
|
word_nbsp(s, w);
|
|
|
|
}
|
2011-05-29 02:16:18 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn bopen(s: &mut State) {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "{");
|
2011-06-01 22:29:38 +00:00
|
|
|
end(s); // close the head-box
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
2011-05-29 02:16:18 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn bclose_(s: &mut State, span: codemap::Span, indented: uint) {
|
2012-08-09 23:31:47 +00:00
|
|
|
bclose_maybe_open(s, span, indented, true);
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn bclose_maybe_open (s: &mut State, span: codemap::Span,
|
|
|
|
indented: uint, close_box: bool) {
|
2011-03-24 15:33:20 +00:00
|
|
|
maybe_print_comment(s, span.hi);
|
2011-07-26 22:37:05 +00:00
|
|
|
break_offset_if_not_bol(s, 1u, -(indented as int));
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "}");
|
2012-08-09 23:31:47 +00:00
|
|
|
if close_box {
|
|
|
|
end(s); // close the outer-box
|
|
|
|
}
|
2011-07-21 13:36:41 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn bclose(s: &mut State, span: codemap::Span) {
|
|
|
|
bclose_(s, span, indent_unit);
|
|
|
|
}
|
2011-05-29 02:16:18 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn is_begin(s: &mut State) -> bool {
|
|
|
|
match s.s.last_token() { pp::Begin(_) => true, _ => false }
|
2011-08-02 22:25:06 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn is_end(s: &mut State) -> bool {
|
|
|
|
match s.s.last_token() { pp::End => true, _ => false }
|
2011-08-02 22:25:06 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn is_bol(s: &mut State) -> bool {
|
2012-08-27 23:26:35 +00:00
|
|
|
return s.s.last_token().is_eof() || s.s.last_token().is_hardbreak_tok();
|
2011-06-20 14:45:05 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn in_cbox(s: &mut State) -> bool {
|
2013-12-31 00:37:45 +00:00
|
|
|
let boxes = s.boxes.borrow();
|
|
|
|
let len = boxes.get().len();
|
2012-08-02 00:30:05 +00:00
|
|
|
if len == 0u { return false; }
|
2014-01-09 13:05:33 +00:00
|
|
|
return boxes.get()[len - 1u] == pp::Consistent;
|
2012-04-19 21:46:11 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn hardbreak_if_not_bol(s: &mut State) {
|
2011-07-27 12:19:39 +00:00
|
|
|
if !is_bol(s) {
|
2013-12-27 22:11:01 +00:00
|
|
|
hardbreak(&mut s.s)
|
|
|
|
}
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn space_if_not_bol(s: &mut State) { if !is_bol(s) { space(&mut s.s); } }
|
|
|
|
pub fn break_offset_if_not_bol(s: &mut State, n: uint, off: int) {
|
2013-12-27 22:11:01 +00:00
|
|
|
if !is_bol(s) {
|
|
|
|
break_offset(&mut s.s, n, off);
|
2011-07-26 22:37:05 +00:00
|
|
|
} else {
|
2012-08-27 23:26:35 +00:00
|
|
|
if off != 0 && s.s.last_token().is_hardbreak_tok() {
|
2011-07-26 22:37:05 +00:00
|
|
|
// We do something pretty sketchy here: tuck the nonzero
|
|
|
|
// offset-adjustment we were going to deposit along with the
|
|
|
|
// break into the previous hardbreak.
|
|
|
|
s.s.replace_last_token(pp::hardbreak_tok_offset(off));
|
|
|
|
}
|
2011-06-20 02:55:28 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-15 18:19:50 +00:00
|
|
|
|
2011-06-02 01:53:52 +00:00
|
|
|
// Synthesizes a comment that was not textually present in the original source
|
|
|
|
// file.
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn synth_comment(s: &mut State, text: ~str) {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "/*");
|
|
|
|
space(&mut s.s);
|
|
|
|
word(&mut s.s, text);
|
|
|
|
space(&mut s.s);
|
|
|
|
word(&mut s.s, "*/");
|
2011-06-02 01:53:52 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn commasep<T>(s: &mut State, b: Breaks, elts: &[T], op: |&mut State, &T|) {
|
2013-12-12 01:04:50 +00:00
|
|
|
rbox(s, 0u, b);
|
2012-03-15 13:47:03 +00:00
|
|
|
let mut first = true;
|
2013-08-03 16:45:23 +00:00
|
|
|
for elt in elts.iter() {
|
2013-05-19 05:07:44 +00:00
|
|
|
if first { first = false; } else { word_space(s, ","); }
|
2013-07-06 00:47:42 +00:00
|
|
|
op(s, elt);
|
2011-07-05 23:23:07 +00:00
|
|
|
}
|
|
|
|
end(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-19 20:21:21 +00:00
|
|
|
pub fn commasep_cmnt<T>(
|
2014-01-09 13:05:33 +00:00
|
|
|
s: &mut State,
|
|
|
|
b: Breaks,
|
2013-11-19 20:21:21 +00:00
|
|
|
elts: &[T],
|
2014-01-09 13:05:33 +00:00
|
|
|
op: |&mut State, &T|,
|
2013-11-19 20:21:21 +00:00
|
|
|
get_span: |&T| -> codemap::Span) {
|
2013-12-12 01:04:50 +00:00
|
|
|
rbox(s, 0u, b);
|
2013-03-27 14:01:45 +00:00
|
|
|
let len = elts.len();
|
2012-03-15 13:47:03 +00:00
|
|
|
let mut i = 0u;
|
2013-08-03 16:45:23 +00:00
|
|
|
for elt in elts.iter() {
|
2013-07-06 00:47:42 +00:00
|
|
|
maybe_print_comment(s, get_span(elt).hi);
|
|
|
|
op(s, elt);
|
2011-07-06 23:01:47 +00:00
|
|
|
i += 1u;
|
2011-07-27 12:19:39 +00:00
|
|
|
if i < len {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ",");
|
2013-07-06 00:47:42 +00:00
|
|
|
maybe_print_trailing_comment(s, get_span(elt),
|
|
|
|
Some(get_span(&elts[i]).hi));
|
2011-07-26 22:37:05 +00:00
|
|
|
space_if_not_bol(s);
|
2011-07-06 23:01:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end(s);
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn commasep_exprs(s: &mut State, b: Breaks, exprs: &[@ast::Expr]) {
|
2013-07-06 00:47:42 +00:00
|
|
|
commasep_cmnt(s, b, exprs, |p, &e| print_expr(p, e), |e| e.span);
|
2011-03-29 10:46:55 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_mod(s: &mut State, _mod: &ast::Mod, attrs: &[ast::Attribute]) {
|
2011-06-30 23:03:07 +00:00
|
|
|
print_inner_attributes(s, attrs);
|
2013-08-03 16:45:23 +00:00
|
|
|
for vitem in _mod.view_items.iter() {
|
2013-07-05 08:28:53 +00:00
|
|
|
print_view_item(s, vitem);
|
2011-05-12 15:24:54 +00:00
|
|
|
}
|
2013-08-03 16:45:23 +00:00
|
|
|
for item in _mod.items.iter() { print_item(s, *item); }
|
2011-03-18 00:39:47 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_foreign_mod(s: &mut State, nmod: &ast::ForeignMod,
|
2013-07-19 11:51:37 +00:00
|
|
|
attrs: &[ast::Attribute]) {
|
2011-07-26 22:37:36 +00:00
|
|
|
print_inner_attributes(s, attrs);
|
2013-08-03 16:45:23 +00:00
|
|
|
for vitem in nmod.view_items.iter() {
|
2013-07-05 08:28:53 +00:00
|
|
|
print_view_item(s, vitem);
|
2011-07-26 22:37:36 +00:00
|
|
|
}
|
2013-08-03 16:45:23 +00:00
|
|
|
for item in nmod.items.iter() { print_foreign_item(s, *item); }
|
2011-07-26 22:37:36 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_opt_lifetime(s: &mut State, lifetime: &Option<ast::Lifetime>) {
|
2013-08-03 16:45:23 +00:00
|
|
|
for l in lifetime.iter() {
|
2013-07-05 12:33:52 +00:00
|
|
|
print_lifetime(s, l);
|
2013-02-28 00:41:02 +00:00
|
|
|
nbsp(s);
|
2012-03-09 02:10:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_type(s: &mut State, ty: &ast::Ty) {
|
2011-03-24 15:33:20 +00:00
|
|
|
maybe_print_comment(s, ty.span.lo);
|
2011-06-01 22:29:38 +00:00
|
|
|
ibox(s, 0u);
|
2013-03-20 01:24:01 +00:00
|
|
|
match ty.node {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::TyNil => word(&mut s.s, "()"),
|
|
|
|
ast::TyBot => word(&mut s.s, "!"),
|
|
|
|
ast::TyBox(ty) => { word(&mut s.s, "@"); print_type(s, ty); }
|
|
|
|
ast::TyUniq(ty) => { word(&mut s.s, "~"); print_type(s, ty); }
|
|
|
|
ast::TyVec(ty) => {
|
|
|
|
word(&mut s.s, "[");
|
|
|
|
print_type(s, ty);
|
|
|
|
word(&mut s.s, "]");
|
|
|
|
}
|
|
|
|
ast::TyPtr(ref mt) => { word(&mut s.s, "*"); print_mt(s, mt); }
|
|
|
|
ast::TyRptr(ref lifetime, ref mt) => {
|
|
|
|
word(&mut s.s, "&");
|
|
|
|
print_opt_lifetime(s, lifetime);
|
|
|
|
print_mt(s, mt);
|
|
|
|
}
|
|
|
|
ast::TyTup(ref elts) => {
|
|
|
|
popen(s);
|
|
|
|
commasep(s, Inconsistent, *elts, print_type_ref);
|
|
|
|
if elts.len() == 1 {
|
|
|
|
word(&mut s.s, ",");
|
|
|
|
}
|
|
|
|
pclose(s);
|
|
|
|
}
|
|
|
|
ast::TyBareFn(f) => {
|
|
|
|
let generics = ast::Generics {
|
|
|
|
lifetimes: f.lifetimes.clone(),
|
|
|
|
ty_params: opt_vec::Empty
|
|
|
|
};
|
|
|
|
print_ty_fn(s, Some(f.abis), None, &None,
|
|
|
|
f.purity, ast::Many, f.decl, None, &None,
|
|
|
|
Some(&generics), None);
|
|
|
|
}
|
|
|
|
ast::TyClosure(f) => {
|
|
|
|
let generics = ast::Generics {
|
|
|
|
lifetimes: f.lifetimes.clone(),
|
|
|
|
ty_params: opt_vec::Empty
|
|
|
|
};
|
|
|
|
print_ty_fn(s, None, Some(f.sigil), &f.region,
|
|
|
|
f.purity, f.onceness, f.decl, None, &f.bounds,
|
|
|
|
Some(&generics), None);
|
|
|
|
}
|
|
|
|
ast::TyPath(ref path, ref bounds, _) => print_bounded_path(s, path, bounds),
|
|
|
|
ast::TyFixedLengthVec(ty, v) => {
|
|
|
|
word(&mut s.s, "[");
|
|
|
|
print_type(s, ty);
|
|
|
|
word(&mut s.s, ", ..");
|
|
|
|
print_expr(s, v);
|
|
|
|
word(&mut s.s, "]");
|
|
|
|
}
|
|
|
|
ast::TyTypeof(e) => {
|
|
|
|
word(&mut s.s, "typeof(");
|
|
|
|
print_expr(s, e);
|
|
|
|
word(&mut s.s, ")");
|
|
|
|
}
|
|
|
|
ast::TyInfer => {
|
|
|
|
fail!("print_type shouldn't see a ty_infer");
|
2013-02-19 01:45:56 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
2011-06-01 22:29:38 +00:00
|
|
|
end(s);
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_type_ref(s: &mut State, ty: &P<ast::Ty>) {
|
2013-11-30 22:00:39 +00:00
|
|
|
print_type(s, *ty);
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_foreign_item(s: &mut State, item: &ast::ForeignItem) {
|
2011-07-26 22:37:36 +00:00
|
|
|
hardbreak_if_not_bol(s);
|
|
|
|
maybe_print_comment(s, item.span.lo);
|
|
|
|
print_outer_attributes(s, item.attrs);
|
2013-03-20 01:24:01 +00:00
|
|
|
match item.node {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ForeignItemFn(decl, ref generics) => {
|
2013-08-02 21:30:00 +00:00
|
|
|
print_fn(s, decl, None, AbiSet::Rust(), item.ident, generics, None,
|
2013-07-21 07:35:02 +00:00
|
|
|
item.vis);
|
2011-07-26 22:37:36 +00:00
|
|
|
end(s); // end head-ibox
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ";");
|
2011-07-26 22:37:36 +00:00
|
|
|
end(s); // end the outer fn box
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ForeignItemStatic(t, m) => {
|
2013-07-21 07:35:02 +00:00
|
|
|
head(s, visibility_qualified(item.vis, "static"));
|
2013-06-22 05:46:27 +00:00
|
|
|
if m {
|
|
|
|
word_space(s, "mut");
|
|
|
|
}
|
2012-08-25 22:09:33 +00:00
|
|
|
print_ident(s, item.ident);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, ":");
|
2012-08-25 22:09:33 +00:00
|
|
|
print_type(s, t);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ";");
|
2012-08-25 22:09:33 +00:00
|
|
|
end(s); // end the head-ibox
|
2012-10-08 08:36:09 +00:00
|
|
|
end(s); // end the outer cbox
|
2012-08-25 22:09:33 +00:00
|
|
|
}
|
2011-07-26 22:37:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_item(s: &mut State, item: &ast::Item) {
|
2011-06-20 16:15:11 +00:00
|
|
|
hardbreak_if_not_bol(s);
|
2011-03-24 15:33:20 +00:00
|
|
|
maybe_print_comment(s, item.span.lo);
|
2011-06-16 20:00:19 +00:00
|
|
|
print_outer_attributes(s, item.attrs);
|
2013-12-27 22:11:01 +00:00
|
|
|
{
|
2014-01-09 13:05:33 +00:00
|
|
|
let ann_node = NodeItem(s, item);
|
2013-12-27 22:11:01 +00:00
|
|
|
s.ann.pre(ann_node);
|
|
|
|
}
|
2013-03-20 01:24:01 +00:00
|
|
|
match item.node {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ItemStatic(ty, m, expr) => {
|
2013-05-21 17:48:56 +00:00
|
|
|
head(s, visibility_qualified(item.vis, "static"));
|
2013-09-02 01:45:37 +00:00
|
|
|
if m == ast::MutMutable {
|
2013-06-22 01:46:34 +00:00
|
|
|
word_space(s, "mut");
|
|
|
|
}
|
2012-07-18 23:18:02 +00:00
|
|
|
print_ident(s, item.ident);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, ":");
|
2011-08-15 11:45:04 +00:00
|
|
|
print_type(s, ty);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2011-07-27 12:19:39 +00:00
|
|
|
end(s); // end the head-ibox
|
|
|
|
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, "=");
|
2011-07-27 12:19:39 +00:00
|
|
|
print_expr(s, expr);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ";");
|
2011-07-27 12:19:39 +00:00
|
|
|
end(s); // end the outer cbox
|
2011-06-15 18:19:50 +00:00
|
|
|
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ItemFn(decl, purity, abi, ref typarams, body) => {
|
2013-02-17 18:59:09 +00:00
|
|
|
print_fn(
|
|
|
|
s,
|
2013-02-28 15:25:31 +00:00
|
|
|
decl,
|
2013-03-14 02:25:28 +00:00
|
|
|
Some(purity),
|
|
|
|
abi,
|
2013-02-17 18:59:09 +00:00
|
|
|
item.ident,
|
2013-02-15 05:50:03 +00:00
|
|
|
typarams,
|
2013-02-17 18:59:09 +00:00
|
|
|
None,
|
|
|
|
item.vis
|
|
|
|
);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, " ");
|
2013-02-18 06:20:36 +00:00
|
|
|
print_block_with_attrs(s, body, item.attrs);
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ItemMod(ref _mod) => {
|
2013-05-21 17:48:56 +00:00
|
|
|
head(s, visibility_qualified(item.vis, "mod"));
|
2012-07-18 23:18:02 +00:00
|
|
|
print_ident(s, item.ident);
|
|
|
|
nbsp(s);
|
2011-07-27 12:19:39 +00:00
|
|
|
bopen(s);
|
|
|
|
print_mod(s, _mod, item.attrs);
|
|
|
|
bclose(s, item.span);
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ItemForeignMod(ref nmod) => {
|
2013-07-19 02:08:57 +00:00
|
|
|
head(s, "extern");
|
2013-03-14 02:25:28 +00:00
|
|
|
word_nbsp(s, nmod.abis.to_str());
|
2011-07-27 12:19:39 +00:00
|
|
|
bopen(s);
|
2012-06-26 23:18:37 +00:00
|
|
|
print_foreign_mod(s, nmod, item.attrs);
|
2011-07-27 12:19:39 +00:00
|
|
|
bclose(s, item.span);
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ItemTy(ty, ref params) => {
|
2011-07-27 12:19:39 +00:00
|
|
|
ibox(s, indent_unit);
|
|
|
|
ibox(s, 0u);
|
2013-05-21 17:48:56 +00:00
|
|
|
word_nbsp(s, visibility_qualified(item.vis, "type"));
|
2012-07-18 23:18:02 +00:00
|
|
|
print_ident(s, item.ident);
|
2013-02-15 05:50:03 +00:00
|
|
|
print_generics(s, params);
|
2011-07-27 12:19:39 +00:00
|
|
|
end(s); // end the inner ibox
|
|
|
|
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, "=");
|
2011-08-15 11:45:04 +00:00
|
|
|
print_type(s, ty);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ";");
|
2011-07-27 12:19:39 +00:00
|
|
|
end(s); // end the outer ibox
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ItemEnum(ref enum_definition, ref params) => {
|
2013-02-17 18:59:09 +00:00
|
|
|
print_enum_def(
|
|
|
|
s,
|
2013-05-12 04:25:31 +00:00
|
|
|
enum_definition,
|
2013-02-15 05:50:03 +00:00
|
|
|
params,
|
2013-02-17 18:59:09 +00:00
|
|
|
item.ident,
|
|
|
|
item.span,
|
|
|
|
item.vis
|
|
|
|
);
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ItemStruct(struct_def, ref generics) => {
|
2013-05-21 17:48:56 +00:00
|
|
|
head(s, visibility_qualified(item.vis, "struct"));
|
2013-02-15 05:50:03 +00:00
|
|
|
print_struct(s, struct_def, generics, item.ident, item.span);
|
2012-08-07 23:08:09 +00:00
|
|
|
}
|
2012-07-18 23:18:02 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ItemImpl(ref generics, ref opt_trait, ty, ref methods) => {
|
2013-05-21 17:48:56 +00:00
|
|
|
head(s, visibility_qualified(item.vis, "impl"));
|
2013-03-06 17:27:23 +00:00
|
|
|
if generics.is_parameterized() {
|
2013-02-15 05:50:03 +00:00
|
|
|
print_generics(s, generics);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2012-08-08 23:26:10 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 22:11:26 +00:00
|
|
|
match opt_trait {
|
2013-07-06 00:47:42 +00:00
|
|
|
&Some(ref t) => {
|
2013-03-27 10:16:28 +00:00
|
|
|
print_trait_ref(s, t);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, "for");
|
2012-07-18 16:31:53 +00:00
|
|
|
}
|
2013-07-06 00:47:42 +00:00
|
|
|
&None => ()
|
2012-09-07 22:11:26 +00:00
|
|
|
};
|
2013-02-11 07:15:45 +00:00
|
|
|
|
|
|
|
print_type(s, ty);
|
2012-08-08 23:26:10 +00:00
|
|
|
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2013-09-19 19:09:52 +00:00
|
|
|
bopen(s);
|
2013-11-24 00:48:46 +00:00
|
|
|
print_inner_attributes(s, item.attrs);
|
2013-09-19 19:09:52 +00:00
|
|
|
for meth in methods.iter() {
|
|
|
|
print_method(s, *meth);
|
2011-12-13 12:19:56 +00:00
|
|
|
}
|
2013-09-19 19:09:52 +00:00
|
|
|
bclose(s, item.span);
|
2011-12-13 12:19:56 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ItemTrait(ref generics, ref traits, ref methods) => {
|
2013-05-21 17:48:56 +00:00
|
|
|
head(s, visibility_qualified(item.vis, "trait"));
|
2012-07-18 23:18:02 +00:00
|
|
|
print_ident(s, item.ident);
|
2013-02-15 05:50:03 +00:00
|
|
|
print_generics(s, generics);
|
2013-02-17 18:59:09 +00:00
|
|
|
if traits.len() != 0u {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ":");
|
2013-08-03 16:45:23 +00:00
|
|
|
for (i, trait_) in traits.iter().enumerate() {
|
2012-12-08 01:55:34 +00:00
|
|
|
nbsp(s);
|
2013-03-05 00:11:30 +00:00
|
|
|
if i != 0 {
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, "+");
|
2013-03-05 00:11:30 +00:00
|
|
|
}
|
2013-07-05 10:15:21 +00:00
|
|
|
print_path(s, &trait_.path, false);
|
2012-12-08 01:55:34 +00:00
|
|
|
}
|
2012-08-03 22:02:01 +00:00
|
|
|
}
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, " ");
|
2011-12-20 15:33:55 +00:00
|
|
|
bopen(s);
|
2013-08-03 16:45:23 +00:00
|
|
|
for meth in methods.iter() {
|
2013-02-18 06:20:36 +00:00
|
|
|
print_trait_method(s, meth);
|
2012-09-19 23:55:01 +00:00
|
|
|
}
|
2011-12-20 15:33:55 +00:00
|
|
|
bclose(s, item.span);
|
|
|
|
}
|
2013-07-12 06:07:34 +00:00
|
|
|
// I think it's reasonable to hide the context here:
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ItemMac(codemap::Spanned { node: ast::MacInvocTT(ref pth, ref tts, _),
|
2013-11-28 20:22:53 +00:00
|
|
|
..}) => {
|
2012-10-14 16:19:54 +00:00
|
|
|
print_visibility(s, item.vis);
|
2012-07-18 23:18:02 +00:00
|
|
|
print_path(s, pth, false);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "! ");
|
2012-07-18 23:18:02 +00:00
|
|
|
print_ident(s, item.ident);
|
2012-08-23 02:08:21 +00:00
|
|
|
cbox(s, indent_unit);
|
|
|
|
popen(s);
|
2013-07-06 00:47:42 +00:00
|
|
|
print_tts(s, &(tts.as_slice()));
|
2012-08-23 02:08:21 +00:00
|
|
|
pclose(s);
|
|
|
|
end(s);
|
2012-07-11 17:42:25 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
2013-12-27 22:11:01 +00:00
|
|
|
{
|
2014-01-09 13:05:33 +00:00
|
|
|
let ann_node = NodeItem(s, item);
|
2013-12-27 22:11:01 +00:00
|
|
|
s.ann.post(ann_node);
|
|
|
|
}
|
2012-08-07 23:08:09 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
fn print_trait_ref(s: &mut State, t: &ast::TraitRef) {
|
2013-07-05 10:15:21 +00:00
|
|
|
print_path(s, &t.path, false);
|
2013-03-27 10:16:28 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_enum_def(s: &mut State, enum_definition: &ast::EnumDef,
|
2013-09-02 00:50:59 +00:00
|
|
|
generics: &ast::Generics, ident: ast::Ident,
|
2014-01-09 13:05:33 +00:00
|
|
|
span: codemap::Span, visibility: ast::Visibility) {
|
2013-05-21 17:48:56 +00:00
|
|
|
head(s, visibility_qualified(visibility, "enum"));
|
2012-07-18 23:18:02 +00:00
|
|
|
print_ident(s, ident);
|
2013-02-15 05:50:03 +00:00
|
|
|
print_generics(s, generics);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2013-03-09 00:21:58 +00:00
|
|
|
print_variants(s, enum_definition.variants, span);
|
2012-08-09 00:14:25 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_variants(s: &mut State,
|
|
|
|
variants: &[P<ast::Variant>],
|
2013-08-31 16:13:04 +00:00
|
|
|
span: codemap::Span) {
|
2012-08-08 21:17:52 +00:00
|
|
|
bopen(s);
|
2013-11-30 22:00:39 +00:00
|
|
|
for &v in variants.iter() {
|
2012-08-08 21:17:52 +00:00
|
|
|
space_if_not_bol(s);
|
|
|
|
maybe_print_comment(s, v.span.lo);
|
|
|
|
print_outer_attributes(s, v.node.attrs);
|
|
|
|
ibox(s, indent_unit);
|
2013-05-12 04:25:31 +00:00
|
|
|
print_variant(s, v);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ",");
|
2012-08-08 21:17:52 +00:00
|
|
|
end(s);
|
2012-11-13 03:32:48 +00:00
|
|
|
maybe_print_trailing_comment(s, v.span, None);
|
2012-08-08 21:17:52 +00:00
|
|
|
}
|
|
|
|
bclose(s, span);
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn visibility_to_str(vis: ast::Visibility) -> ~str {
|
2012-09-22 01:10:45 +00:00
|
|
|
match vis {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::Private => ~"priv",
|
|
|
|
ast::Public => ~"pub",
|
|
|
|
ast::Inherited => ~""
|
2012-09-22 01:10:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn visibility_qualified(vis: ast::Visibility, s: &str) -> ~str {
|
2012-09-22 01:10:45 +00:00
|
|
|
match vis {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::Private | ast::Public => visibility_to_str(vis) + " " + s,
|
|
|
|
ast::Inherited => s.to_owned()
|
2012-09-22 01:10:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_visibility(s: &mut State, vis: ast::Visibility) {
|
2012-09-22 01:10:45 +00:00
|
|
|
match vis {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::Private | ast::Public =>
|
2012-09-22 01:10:45 +00:00
|
|
|
word_nbsp(s, visibility_to_str(vis)),
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::Inherited => ()
|
2012-09-22 01:10:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_struct(s: &mut State,
|
|
|
|
struct_def: &ast::StructDef,
|
2013-02-15 05:50:03 +00:00
|
|
|
generics: &ast::Generics,
|
2013-09-02 00:50:59 +00:00
|
|
|
ident: ast::Ident,
|
2013-08-31 16:13:04 +00:00
|
|
|
span: codemap::Span) {
|
2012-07-18 23:18:02 +00:00
|
|
|
print_ident(s, ident);
|
2013-02-15 05:50:03 +00:00
|
|
|
print_generics(s, generics);
|
2012-10-26 19:11:14 +00:00
|
|
|
if ast_util::struct_def_is_tuple_like(struct_def) {
|
2013-03-02 02:42:02 +00:00
|
|
|
if !struct_def.fields.is_empty() {
|
|
|
|
popen(s);
|
2014-01-09 13:05:33 +00:00
|
|
|
commasep(s, Inconsistent, struct_def.fields, |s, field| {
|
2013-03-02 02:42:02 +00:00
|
|
|
match field.node.kind {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::NamedField(..) => fail!("unexpected named field"),
|
|
|
|
ast::UnnamedField => {
|
2013-03-02 02:42:02 +00:00
|
|
|
maybe_print_comment(s, field.span.lo);
|
2013-11-30 22:00:39 +00:00
|
|
|
print_type(s, field.node.ty);
|
2013-03-02 02:42:02 +00:00
|
|
|
}
|
2012-08-15 22:53:58 +00:00
|
|
|
}
|
2013-11-21 00:23:04 +00:00
|
|
|
});
|
2013-03-02 02:42:02 +00:00
|
|
|
pclose(s);
|
2012-08-15 22:53:58 +00:00
|
|
|
}
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ";");
|
2013-03-02 02:42:02 +00:00
|
|
|
end(s);
|
2012-10-26 19:11:14 +00:00
|
|
|
end(s); // close the outer-box
|
|
|
|
} else {
|
2013-03-02 02:42:02 +00:00
|
|
|
nbsp(s);
|
2012-10-26 19:11:14 +00:00
|
|
|
bopen(s);
|
|
|
|
hardbreak_if_not_bol(s);
|
|
|
|
|
2013-08-03 16:45:23 +00:00
|
|
|
for field in struct_def.fields.iter() {
|
2012-10-26 19:11:14 +00:00
|
|
|
match field.node.kind {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::UnnamedField => fail!("unexpected unnamed field"),
|
|
|
|
ast::NamedField(ident, visibility) => {
|
2012-10-26 19:11:14 +00:00
|
|
|
hardbreak_if_not_bol(s);
|
|
|
|
maybe_print_comment(s, field.span.lo);
|
2013-05-01 03:20:08 +00:00
|
|
|
print_outer_attributes(s, field.node.attrs);
|
2012-10-26 19:11:14 +00:00
|
|
|
print_visibility(s, visibility);
|
|
|
|
print_ident(s, ident);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_nbsp(s, ":");
|
2013-11-30 22:00:39 +00:00
|
|
|
print_type(s, field.node.ty);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ",");
|
2012-10-26 19:11:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bclose(s, span);
|
2012-08-15 22:53:58 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
|
|
|
|
2012-07-28 00:38:01 +00:00
|
|
|
/// This doesn't deserve to be called "pretty" printing, but it should be
|
|
|
|
/// meaning-preserving. A quick hack that might help would be to look at the
|
|
|
|
/// spans embedded in the TTs to decide where to put spaces and newlines.
|
|
|
|
/// But it'd be better to parse these according to the grammar of the
|
|
|
|
/// appropriate macro, transcribe back into the grammar we just parsed from,
|
|
|
|
/// and then pretty-print the resulting AST nodes (so, e.g., we print
|
|
|
|
/// expression arguments as expressions). It can be done! I think.
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_tt(s: &mut State, tt: &ast::TokenTree) {
|
2013-05-12 04:25:31 +00:00
|
|
|
match *tt {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::TTDelim(ref tts) => print_tts(s, &(tts.as_slice())),
|
|
|
|
ast::TTTok(_, ref tk) => {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, parse::token::to_str(s.intr, tk));
|
2012-07-28 00:38:01 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::TTSeq(_, ref tts, ref sep, zerok) => {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "$(");
|
2013-08-03 16:45:23 +00:00
|
|
|
for tt_elt in (*tts).iter() { print_tt(s, tt_elt); }
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ")");
|
2014-01-19 08:21:14 +00:00
|
|
|
match *sep {
|
2013-12-27 22:11:01 +00:00
|
|
|
Some(ref tk) => word(&mut s.s, parse::token::to_str(s.intr, tk)),
|
2012-08-20 19:23:37 +00:00
|
|
|
None => ()
|
2012-07-28 00:38:01 +00:00
|
|
|
}
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, if zerok { "*" } else { "+" });
|
2012-07-28 00:38:01 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::TTNonterminal(_, name) => {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "$");
|
2012-07-18 23:18:02 +00:00
|
|
|
print_ident(s, name);
|
2012-07-28 00:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-11 17:42:25 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_tts(s: &mut State, tts: & &[ast::TokenTree]) {
|
2012-12-13 01:08:09 +00:00
|
|
|
ibox(s, 0);
|
2013-08-03 16:45:23 +00:00
|
|
|
for (i, tt) in tts.iter().enumerate() {
|
2012-12-13 01:08:09 +00:00
|
|
|
if i != 0 {
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2012-12-13 01:08:09 +00:00
|
|
|
}
|
2013-05-12 04:25:31 +00:00
|
|
|
print_tt(s, tt);
|
2012-12-13 01:08:09 +00:00
|
|
|
}
|
|
|
|
end(s);
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_variant(s: &mut State, v: &ast::Variant) {
|
2012-09-22 01:10:45 +00:00
|
|
|
print_visibility(s, v.node.vis);
|
2013-03-20 01:24:01 +00:00
|
|
|
match v.node.kind {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::TupleVariantKind(ref args) => {
|
2012-07-18 23:18:02 +00:00
|
|
|
print_ident(s, v.node.name);
|
2013-01-25 03:19:44 +00:00
|
|
|
if !args.is_empty() {
|
2012-08-07 21:24:04 +00:00
|
|
|
popen(s);
|
2014-01-09 13:05:33 +00:00
|
|
|
fn print_variant_arg(s: &mut State, arg: &ast::VariantArg) {
|
2013-11-30 22:00:39 +00:00
|
|
|
print_type(s, arg.ty);
|
2012-08-07 21:24:04 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
commasep(s, Consistent, *args, print_variant_arg);
|
2012-08-07 21:24:04 +00:00
|
|
|
pclose(s);
|
|
|
|
}
|
2012-01-26 01:22:08 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::StructVariantKind(struct_def) => {
|
2013-05-19 05:07:44 +00:00
|
|
|
head(s, "");
|
2013-02-15 05:50:03 +00:00
|
|
|
let generics = ast_util::empty_generics();
|
|
|
|
print_struct(s, struct_def, &generics, v.node.name, v.span);
|
2012-08-08 01:54:44 +00:00
|
|
|
}
|
2012-01-26 01:22:08 +00:00
|
|
|
}
|
2012-08-06 19:34:08 +00:00
|
|
|
match v.node.disr_expr {
|
2012-08-20 19:23:37 +00:00
|
|
|
Some(d) => {
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, "=");
|
2012-01-26 01:22:08 +00:00
|
|
|
print_expr(s, d);
|
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => ()
|
2012-01-26 01:22:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_ty_method(s: &mut State, m: &ast::TypeMethod) {
|
2011-12-20 15:33:55 +00:00
|
|
|
hardbreak_if_not_bol(s);
|
|
|
|
maybe_print_comment(s, m.span.lo);
|
2012-01-30 19:43:45 +00:00
|
|
|
print_outer_attributes(s, m.attrs);
|
2013-06-28 00:41:35 +00:00
|
|
|
print_ty_fn(s,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
&None,
|
|
|
|
m.purity,
|
|
|
|
ast::Many,
|
2013-11-30 22:00:39 +00:00
|
|
|
m.decl,
|
2013-06-28 00:41:35 +00:00
|
|
|
Some(m.ident),
|
|
|
|
&None,
|
|
|
|
Some(&m.generics),
|
|
|
|
Some(m.explicit_self.node));
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ";");
|
2011-12-20 15:33:55 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_trait_method(s: &mut State, m: &ast::TraitMethod) {
|
2013-02-18 06:20:36 +00:00
|
|
|
match *m {
|
2014-01-09 13:05:33 +00:00
|
|
|
Required(ref ty_m) => print_ty_method(s, ty_m),
|
|
|
|
Provided(m) => print_method(s, m)
|
2012-07-10 20:44:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_method(s: &mut State, meth: &ast::Method) {
|
2012-03-19 17:19:00 +00:00
|
|
|
hardbreak_if_not_bol(s);
|
|
|
|
maybe_print_comment(s, meth.span.lo);
|
|
|
|
print_outer_attributes(s, meth.attrs);
|
2013-11-30 22:00:39 +00:00
|
|
|
print_fn(s, meth.decl, Some(meth.purity), AbiSet::Rust(),
|
2013-04-30 15:49:48 +00:00
|
|
|
meth.ident, &meth.generics, Some(meth.explicit_self.node),
|
2012-09-22 01:10:45 +00:00
|
|
|
meth.vis);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, " ");
|
2013-11-30 22:00:39 +00:00
|
|
|
print_block_with_attrs(s, meth.body, meth.attrs);
|
2012-03-19 17:19:00 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_outer_attributes(s: &mut State, attrs: &[ast::Attribute]) {
|
2012-03-15 13:47:03 +00:00
|
|
|
let mut count = 0;
|
2013-08-03 16:45:23 +00:00
|
|
|
for attr in attrs.iter() {
|
2012-08-06 19:34:08 +00:00
|
|
|
match attr.node.style {
|
2013-07-19 11:51:37 +00:00
|
|
|
ast::AttrOuter => { print_attribute(s, attr); count += 1; }
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => {/* fallthrough */ }
|
2011-06-15 01:53:12 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-27 12:19:39 +00:00
|
|
|
if count > 0 { hardbreak_if_not_bol(s); }
|
2011-06-15 01:53:12 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_inner_attributes(s: &mut State, attrs: &[ast::Attribute]) {
|
2012-03-15 13:47:03 +00:00
|
|
|
let mut count = 0;
|
2013-08-03 16:45:23 +00:00
|
|
|
for attr in attrs.iter() {
|
2012-08-06 19:34:08 +00:00
|
|
|
match attr.node.style {
|
2013-07-19 11:51:37 +00:00
|
|
|
ast::AttrInner => {
|
2013-07-06 00:47:42 +00:00
|
|
|
print_attribute(s, attr);
|
2012-06-30 10:54:54 +00:00
|
|
|
if !attr.node.is_sugared_doc {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ";");
|
2012-06-30 10:54:54 +00:00
|
|
|
}
|
2011-07-27 12:19:39 +00:00
|
|
|
count += 1;
|
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => {/* fallthrough */ }
|
2011-06-16 20:00:19 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-27 12:19:39 +00:00
|
|
|
if count > 0 { hardbreak_if_not_bol(s); }
|
2011-06-16 20:00:19 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_attribute(s: &mut State, attr: &ast::Attribute) {
|
2011-07-01 00:25:13 +00:00
|
|
|
hardbreak_if_not_bol(s);
|
2011-06-15 01:53:12 +00:00
|
|
|
maybe_print_comment(s, attr.span.lo);
|
2012-06-30 10:54:54 +00:00
|
|
|
if attr.node.is_sugared_doc {
|
2013-08-03 23:59:24 +00:00
|
|
|
let comment = attr.value_str().unwrap();
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, comment);
|
2012-06-30 10:54:54 +00:00
|
|
|
} else {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "#[");
|
2013-07-19 11:51:37 +00:00
|
|
|
print_meta_item(s, attr.meta());
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "]");
|
2012-06-30 10:54:54 +00:00
|
|
|
}
|
2011-06-15 01:53:12 +00:00
|
|
|
}
|
|
|
|
|
2011-12-29 20:03:39 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_stmt(s: &mut State, st: &ast::Stmt) {
|
2011-05-31 17:58:30 +00:00
|
|
|
maybe_print_comment(s, st.span.lo);
|
2012-08-06 19:34:08 +00:00
|
|
|
match st.node {
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::StmtDecl(decl, _) => {
|
2011-12-29 20:03:39 +00:00
|
|
|
print_decl(s, decl);
|
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::StmtExpr(expr, _) => {
|
2011-12-29 20:03:39 +00:00
|
|
|
space_if_not_bol(s);
|
2012-01-05 06:01:58 +00:00
|
|
|
print_expr(s, expr);
|
2012-07-04 00:30:25 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::StmtSemi(expr, _) => {
|
2012-07-04 00:30:25 +00:00
|
|
|
space_if_not_bol(s);
|
|
|
|
print_expr(s, expr);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ";");
|
2012-01-04 22:16:41 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::StmtMac(ref mac, semi) => {
|
2012-11-13 04:06:55 +00:00
|
|
|
space_if_not_bol(s);
|
2013-05-12 04:25:31 +00:00
|
|
|
print_mac(s, mac);
|
2013-12-27 22:11:01 +00:00
|
|
|
if semi { word(&mut s.s, ";"); }
|
2012-11-13 04:06:55 +00:00
|
|
|
}
|
2011-05-31 17:58:30 +00:00
|
|
|
}
|
2013-12-27 22:11:01 +00:00
|
|
|
if parse::classify::stmt_ends_with_semi(st) { word(&mut s.s, ";"); }
|
2012-11-13 03:32:48 +00:00
|
|
|
maybe_print_trailing_comment(s, st.span, None);
|
2011-05-31 17:58:30 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_block(s: &mut State, blk: &ast::Block) {
|
|
|
|
print_possibly_embedded_block(s, blk, BlockNormal, indent_unit);
|
2011-07-13 22:44:09 +00:00
|
|
|
}
|
2011-07-08 23:35:09 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_block_unclosed(s: &mut State, blk: &ast::Block) {
|
|
|
|
print_possibly_embedded_block_(s, blk, BlockNormal, indent_unit, &[],
|
2012-08-09 23:31:47 +00:00
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_block_unclosed_indent(s: &mut State, blk: &ast::Block, indented: uint) {
|
|
|
|
print_possibly_embedded_block_(s, blk, BlockNormal, indented, &[], false);
|
2012-08-14 01:13:41 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_block_with_attrs(s: &mut State,
|
2013-07-19 05:38:55 +00:00
|
|
|
blk: &ast::Block,
|
2013-07-19 11:51:37 +00:00
|
|
|
attrs: &[ast::Attribute]) {
|
2014-01-09 13:05:33 +00:00
|
|
|
print_possibly_embedded_block_(s, blk, BlockNormal, indent_unit, attrs,
|
2012-08-09 23:31:47 +00:00
|
|
|
true);
|
2012-01-16 01:23:59 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
enum EmbedType {
|
|
|
|
BlockBlockFn,
|
|
|
|
BlockNormal,
|
|
|
|
}
|
2011-08-15 21:42:33 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_possibly_embedded_block(s: &mut State,
|
2013-07-19 05:38:55 +00:00
|
|
|
blk: &ast::Block,
|
2014-01-09 13:05:33 +00:00
|
|
|
embedded: EmbedType,
|
2013-01-29 22:41:40 +00:00
|
|
|
indented: uint) {
|
2012-01-16 01:23:59 +00:00
|
|
|
print_possibly_embedded_block_(
|
2013-05-12 04:25:31 +00:00
|
|
|
s, blk, embedded, indented, &[], true);
|
2012-01-16 01:23:59 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_possibly_embedded_block_(s: &mut State,
|
2013-07-19 05:38:55 +00:00
|
|
|
blk: &ast::Block,
|
2014-01-09 13:05:33 +00:00
|
|
|
embedded: EmbedType,
|
2013-01-29 22:41:40 +00:00
|
|
|
indented: uint,
|
2013-07-19 11:51:37 +00:00
|
|
|
attrs: &[ast::Attribute],
|
2013-01-29 22:41:40 +00:00
|
|
|
close_box: bool) {
|
2013-07-16 18:08:35 +00:00
|
|
|
match blk.rules {
|
2013-11-28 20:22:53 +00:00
|
|
|
ast::UnsafeBlock(..) => word_space(s, "unsafe"),
|
2013-07-27 08:25:59 +00:00
|
|
|
ast::DefaultBlock => ()
|
2011-10-06 23:42:27 +00:00
|
|
|
}
|
2011-03-24 15:33:20 +00:00
|
|
|
maybe_print_comment(s, blk.span.lo);
|
2013-12-27 22:11:01 +00:00
|
|
|
{
|
2014-01-09 13:05:33 +00:00
|
|
|
let ann_node = NodeBlock(s, blk);
|
2013-12-27 22:11:01 +00:00
|
|
|
s.ann.pre(ann_node);
|
|
|
|
}
|
2012-08-06 19:34:08 +00:00
|
|
|
match embedded {
|
2014-01-09 13:05:33 +00:00
|
|
|
BlockBlockFn => end(s),
|
|
|
|
BlockNormal => bopen(s)
|
2011-08-15 21:42:33 +00:00
|
|
|
}
|
2011-08-03 03:46:36 +00:00
|
|
|
|
2012-01-16 01:23:59 +00:00
|
|
|
print_inner_attributes(s, attrs);
|
|
|
|
|
2013-08-03 16:45:23 +00:00
|
|
|
for vi in blk.view_items.iter() { print_view_item(s, vi); }
|
|
|
|
for st in blk.stmts.iter() {
|
2013-04-17 16:15:08 +00:00
|
|
|
print_stmt(s, *st);
|
2011-08-03 03:46:36 +00:00
|
|
|
}
|
2013-07-16 18:08:35 +00:00
|
|
|
match blk.expr {
|
2012-08-20 19:23:37 +00:00
|
|
|
Some(expr) => {
|
2011-07-27 12:19:39 +00:00
|
|
|
space_if_not_bol(s);
|
2012-01-05 06:01:58 +00:00
|
|
|
print_expr(s, expr);
|
2012-08-20 19:23:37 +00:00
|
|
|
maybe_print_trailing_comment(s, expr.span, Some(blk.span.hi));
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => ()
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
2012-08-09 23:31:47 +00:00
|
|
|
bclose_maybe_open(s, blk.span, indented, close_box);
|
2013-12-27 22:11:01 +00:00
|
|
|
{
|
2014-01-09 13:05:33 +00:00
|
|
|
let ann_node = NodeBlock(s, blk);
|
2013-12-27 22:11:01 +00:00
|
|
|
s.ann.post(ann_node);
|
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_if(s: &mut State, test: &ast::Expr, blk: &ast::Block,
|
2013-09-02 01:45:37 +00:00
|
|
|
elseopt: Option<@ast::Expr>, chk: bool) {
|
2013-05-19 05:07:44 +00:00
|
|
|
head(s, "if");
|
|
|
|
if chk { word_nbsp(s, "check"); }
|
2012-10-28 00:14:09 +00:00
|
|
|
print_expr(s, test);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2011-07-25 20:42:38 +00:00
|
|
|
print_block(s, blk);
|
2014-01-09 13:05:33 +00:00
|
|
|
fn do_else(s: &mut State, els: Option<@ast::Expr>) {
|
2012-08-06 19:34:08 +00:00
|
|
|
match els {
|
2012-08-20 19:23:37 +00:00
|
|
|
Some(_else) => {
|
2012-08-06 19:34:08 +00:00
|
|
|
match _else.node {
|
2011-07-27 12:19:39 +00:00
|
|
|
// "another else-if"
|
2013-11-30 22:00:39 +00:00
|
|
|
ast::ExprIf(i, t, e) => {
|
2011-07-27 12:19:39 +00:00
|
|
|
cbox(s, indent_unit - 1u);
|
|
|
|
ibox(s, 0u);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, " else if ");
|
2012-10-28 00:14:09 +00:00
|
|
|
print_expr(s, i);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2013-02-18 06:20:36 +00:00
|
|
|
print_block(s, t);
|
2011-07-27 12:19:39 +00:00
|
|
|
do_else(s, e);
|
|
|
|
}
|
|
|
|
// "final else"
|
2013-11-30 22:00:39 +00:00
|
|
|
ast::ExprBlock(b) => {
|
2011-07-27 12:19:39 +00:00
|
|
|
cbox(s, indent_unit - 1u);
|
|
|
|
ibox(s, 0u);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, " else ");
|
2013-02-18 06:20:36 +00:00
|
|
|
print_block(s, b);
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2012-01-31 05:00:57 +00:00
|
|
|
// BLEAH, constraints would be great here
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => {
|
2013-10-21 20:08:31 +00:00
|
|
|
fail!("print_if saw if with weird alternative");
|
2012-01-31 05:00:57 +00:00
|
|
|
}
|
2011-06-16 21:08:17 +00:00
|
|
|
}
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => {/* fall through */ }
|
2011-06-16 21:08:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
do_else(s, elseopt);
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_mac(s: &mut State, m: &ast::Mac) {
|
2012-08-06 19:34:08 +00:00
|
|
|
match m.node {
|
2013-07-12 06:07:34 +00:00
|
|
|
// I think it's reasonable to hide the ctxt here:
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::MacInvocTT(ref pth, ref tts, _) => {
|
2012-07-18 23:18:02 +00:00
|
|
|
print_path(s, pth, false);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "!");
|
2012-08-23 02:08:21 +00:00
|
|
|
popen(s);
|
2013-07-06 00:47:42 +00:00
|
|
|
print_tts(s, &tts.as_slice());
|
2012-08-23 02:08:21 +00:00
|
|
|
pclose(s);
|
2012-07-11 17:42:25 +00:00
|
|
|
}
|
2011-07-08 23:35:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_vstore(s: &mut State, t: ast::Vstore) {
|
2012-08-06 19:34:08 +00:00
|
|
|
match t {
|
2013-12-27 22:11:01 +00:00
|
|
|
ast::VstoreFixed(Some(i)) => word(&mut s.s, format!("{}", i)),
|
|
|
|
ast::VstoreFixed(None) => word(&mut s.s, "_"),
|
|
|
|
ast::VstoreUniq => word(&mut s.s, "~"),
|
|
|
|
ast::VstoreBox => word(&mut s.s, "@"),
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::VstoreSlice(ref r) => {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "&");
|
2013-02-28 00:41:02 +00:00
|
|
|
print_opt_lifetime(s, r);
|
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
}
|
2012-04-10 00:32:49 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_expr_vstore(s: &mut State, t: ast::ExprVstore) {
|
2012-09-12 04:25:01 +00:00
|
|
|
match t {
|
2013-12-27 22:11:01 +00:00
|
|
|
ast::ExprVstoreUniq => word(&mut s.s, "~"),
|
|
|
|
ast::ExprVstoreBox => word(&mut s.s, "@"),
|
|
|
|
ast::ExprVstoreSlice => word(&mut s.s, "&"),
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprVstoreMutSlice => {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "&");
|
|
|
|
word(&mut s.s, "mut");
|
2012-12-08 00:26:52 +00:00
|
|
|
}
|
2012-09-12 04:25:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_call_pre(s: &mut State,
|
2013-02-01 01:12:29 +00:00
|
|
|
sugar: ast::CallSugar,
|
2013-09-02 01:45:37 +00:00
|
|
|
base_args: &mut ~[@ast::Expr])
|
|
|
|
-> Option<@ast::Expr> {
|
2013-02-01 01:12:29 +00:00
|
|
|
match sugar {
|
|
|
|
ast::DoSugar => {
|
2013-05-19 05:07:44 +00:00
|
|
|
head(s, "do");
|
2013-02-01 01:12:29 +00:00
|
|
|
Some(base_args.pop())
|
|
|
|
}
|
|
|
|
ast::ForSugar => {
|
2013-05-19 05:07:44 +00:00
|
|
|
head(s, "for");
|
2013-02-01 01:12:29 +00:00
|
|
|
Some(base_args.pop())
|
|
|
|
}
|
|
|
|
ast::NoSugar => None
|
2012-11-30 19:18:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_call_post(s: &mut State,
|
2013-02-01 01:12:29 +00:00
|
|
|
sugar: ast::CallSugar,
|
2013-09-02 01:45:37 +00:00
|
|
|
blk: &Option<@ast::Expr>,
|
|
|
|
base_args: &mut ~[@ast::Expr]) {
|
2013-02-01 01:12:29 +00:00
|
|
|
if sugar == ast::NoSugar || !base_args.is_empty() {
|
2012-11-30 19:18:25 +00:00
|
|
|
popen(s);
|
2014-01-09 13:05:33 +00:00
|
|
|
commasep_exprs(s, Inconsistent, *base_args);
|
2012-11-30 19:18:25 +00:00
|
|
|
pclose(s);
|
|
|
|
}
|
2013-02-01 01:12:29 +00:00
|
|
|
if sugar != ast::NoSugar {
|
2012-11-30 19:18:25 +00:00
|
|
|
nbsp(s);
|
2013-08-03 23:59:24 +00:00
|
|
|
match blk.unwrap().node {
|
2012-11-30 19:18:25 +00:00
|
|
|
// need to handle closures specifically
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprDoBody(e) => {
|
2012-11-30 19:18:25 +00:00
|
|
|
end(s); // we close our head box; closure
|
|
|
|
// will create it's own.
|
|
|
|
print_expr(s, e);
|
|
|
|
end(s); // close outer box, as closures don't
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// not sure if this can happen.
|
2013-08-03 23:59:24 +00:00
|
|
|
print_expr(s, blk.unwrap());
|
2012-11-30 19:18:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_expr(s: &mut State, expr: &ast::Expr) {
|
|
|
|
fn print_field(s: &mut State, field: &ast::Field) {
|
2012-07-23 23:39:18 +00:00
|
|
|
ibox(s, indent_unit);
|
2013-10-29 02:22:42 +00:00
|
|
|
print_ident(s, field.ident.node);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, ":");
|
2013-07-19 14:24:22 +00:00
|
|
|
print_expr(s, field.expr);
|
2012-07-23 23:39:18 +00:00
|
|
|
end(s);
|
|
|
|
}
|
2013-08-31 16:13:04 +00:00
|
|
|
fn get_span(field: &ast::Field) -> codemap::Span { return field.span; }
|
2012-07-23 23:39:18 +00:00
|
|
|
|
2011-03-24 15:33:20 +00:00
|
|
|
maybe_print_comment(s, expr.span.lo);
|
2011-06-01 22:29:38 +00:00
|
|
|
ibox(s, indent_unit);
|
2013-12-27 22:11:01 +00:00
|
|
|
{
|
2014-01-09 13:05:33 +00:00
|
|
|
let ann_node = NodeExpr(s, expr);
|
2013-12-27 22:11:01 +00:00
|
|
|
s.ann.pre(ann_node);
|
|
|
|
}
|
2013-03-20 01:24:01 +00:00
|
|
|
match expr.node {
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprVstore(e, v) => {
|
2013-03-27 05:24:32 +00:00
|
|
|
print_expr_vstore(s, v);
|
|
|
|
print_expr(s, e);
|
2012-09-12 04:25:01 +00:00
|
|
|
},
|
2013-12-18 00:46:18 +00:00
|
|
|
ast::ExprBox(p, e) => {
|
|
|
|
word(&mut s.s, "box");
|
|
|
|
word(&mut s.s, "(");
|
|
|
|
print_expr(s, p);
|
|
|
|
word_space(s, ")");
|
|
|
|
print_expr(s, e);
|
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprVec(ref exprs, mutbl) => {
|
2011-07-27 12:19:39 +00:00
|
|
|
ibox(s, indent_unit);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "[");
|
2013-09-02 01:45:37 +00:00
|
|
|
if mutbl == ast::MutMutable {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "mut");
|
2013-03-20 01:24:01 +00:00
|
|
|
if exprs.len() > 0u { nbsp(s); }
|
2011-07-27 19:20:51 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
commasep_exprs(s, Inconsistent, *exprs);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "]");
|
2011-07-27 12:19:39 +00:00
|
|
|
end(s);
|
|
|
|
}
|
2012-08-04 01:01:30 +00:00
|
|
|
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprRepeat(element, count, mutbl) => {
|
2012-08-04 01:01:30 +00:00
|
|
|
ibox(s, indent_unit);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "[");
|
2013-09-02 01:45:37 +00:00
|
|
|
if mutbl == ast::MutMutable {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "mut");
|
2012-08-04 01:01:30 +00:00
|
|
|
nbsp(s);
|
|
|
|
}
|
|
|
|
print_expr(s, element);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ",");
|
|
|
|
word(&mut s.s, "..");
|
2012-08-04 01:01:30 +00:00
|
|
|
print_expr(s, count);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "]");
|
2012-08-04 01:01:30 +00:00
|
|
|
end(s);
|
|
|
|
}
|
|
|
|
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprStruct(ref path, ref fields, wth) => {
|
2012-07-23 23:39:18 +00:00
|
|
|
print_path(s, path, true);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "{");
|
2014-01-09 13:05:33 +00:00
|
|
|
commasep_cmnt(s, Consistent, (*fields), print_field, get_span);
|
2012-08-06 19:34:08 +00:00
|
|
|
match wth {
|
2012-08-20 19:23:37 +00:00
|
|
|
Some(expr) => {
|
2012-08-06 20:15:40 +00:00
|
|
|
ibox(s, indent_unit);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ",");
|
|
|
|
space(&mut s.s);
|
|
|
|
word(&mut s.s, "..");
|
2012-08-06 20:15:40 +00:00
|
|
|
print_expr(s, expr);
|
|
|
|
end(s);
|
|
|
|
}
|
2013-12-27 22:11:01 +00:00
|
|
|
_ => (word(&mut s.s, ","))
|
2012-08-06 20:15:40 +00:00
|
|
|
}
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "}");
|
2012-07-23 23:39:18 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprTup(ref exprs) => {
|
2011-08-15 09:40:26 +00:00
|
|
|
popen(s);
|
2014-01-09 13:05:33 +00:00
|
|
|
commasep_exprs(s, Inconsistent, *exprs);
|
2013-02-17 23:41:47 +00:00
|
|
|
if exprs.len() == 1 {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ",");
|
2013-02-17 23:41:47 +00:00
|
|
|
}
|
2011-08-15 09:40:26 +00:00
|
|
|
pclose(s);
|
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprCall(func, ref args, sugar) => {
|
2013-07-02 19:47:32 +00:00
|
|
|
let mut base_args = (*args).clone();
|
2013-02-01 01:12:29 +00:00
|
|
|
let blk = print_call_pre(s, sugar, &mut base_args);
|
2012-10-28 00:14:09 +00:00
|
|
|
print_expr(s, func);
|
2013-02-01 01:12:29 +00:00
|
|
|
print_call_post(s, sugar, &blk, &mut base_args);
|
2012-11-30 19:18:25 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprMethodCall(_, func, ident, ref tys, ref args, sugar) => {
|
2013-07-02 19:47:32 +00:00
|
|
|
let mut base_args = (*args).clone();
|
2013-02-01 01:12:29 +00:00
|
|
|
let blk = print_call_pre(s, sugar, &mut base_args);
|
2012-11-30 19:18:25 +00:00
|
|
|
print_expr(s, func);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ".");
|
2012-11-30 19:18:25 +00:00
|
|
|
print_ident(s, ident);
|
2013-03-20 01:24:01 +00:00
|
|
|
if tys.len() > 0u {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "::<");
|
2014-01-09 13:05:33 +00:00
|
|
|
commasep(s, Inconsistent, *tys, print_type_ref);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ">");
|
2011-10-21 12:11:24 +00:00
|
|
|
}
|
2013-02-01 01:12:29 +00:00
|
|
|
print_call_post(s, sugar, &blk, &mut base_args);
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprBinary(_, op, lhs, rhs) => {
|
2012-10-28 00:14:09 +00:00
|
|
|
print_expr(s, lhs);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2011-08-27 21:42:29 +00:00
|
|
|
word_space(s, ast_util::binop_to_str(op));
|
2012-10-28 00:14:09 +00:00
|
|
|
print_expr(s, rhs);
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprUnary(_, op, expr) => {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ast_util::unop_to_str(op));
|
2012-10-28 00:14:09 +00:00
|
|
|
print_expr(s, expr);
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprAddrOf(m, expr) => {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "&");
|
2012-03-09 00:34:36 +00:00
|
|
|
print_mutability(s, m);
|
2013-03-09 02:43:40 +00:00
|
|
|
// Avoid `& &e` => `&&e`.
|
|
|
|
match (m, &expr.node) {
|
2013-12-27 22:11:01 +00:00
|
|
|
(ast::MutImmutable, &ast::ExprAddrOf(..)) => space(&mut s.s),
|
2013-03-09 02:43:40 +00:00
|
|
|
_ => { }
|
|
|
|
}
|
2012-03-09 00:34:36 +00:00
|
|
|
print_expr(s, expr);
|
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprLit(lit) => print_literal(s, lit),
|
2013-11-30 22:00:39 +00:00
|
|
|
ast::ExprCast(expr, ty) => {
|
2012-10-28 00:14:09 +00:00
|
|
|
print_expr(s, expr);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, "as");
|
2013-03-20 20:24:09 +00:00
|
|
|
print_type(s, ty);
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2013-11-30 22:00:39 +00:00
|
|
|
ast::ExprIf(test, blk, elseopt) => {
|
2013-02-18 06:20:36 +00:00
|
|
|
print_if(s, test, blk, elseopt, false);
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2013-11-30 22:00:39 +00:00
|
|
|
ast::ExprWhile(test, blk) => {
|
2013-05-19 05:07:44 +00:00
|
|
|
head(s, "while");
|
2012-10-28 00:14:09 +00:00
|
|
|
print_expr(s, test);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2013-02-18 06:20:36 +00:00
|
|
|
print_block(s, blk);
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2013-11-30 22:00:39 +00:00
|
|
|
ast::ExprForLoop(pat, iter, blk, opt_ident) => {
|
2013-09-08 12:08:01 +00:00
|
|
|
for ident in opt_ident.iter() {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "'");
|
2013-09-08 12:08:01 +00:00
|
|
|
print_ident(s, *ident);
|
|
|
|
word_space(s, ":");
|
|
|
|
}
|
2013-08-03 16:45:23 +00:00
|
|
|
head(s, "for");
|
2013-07-30 00:25:00 +00:00
|
|
|
print_pat(s, pat);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2013-07-30 00:25:00 +00:00
|
|
|
word_space(s, "in");
|
|
|
|
print_expr(s, iter);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2013-07-30 00:25:00 +00:00
|
|
|
print_block(s, blk);
|
|
|
|
}
|
2013-11-30 22:00:39 +00:00
|
|
|
ast::ExprLoop(blk, opt_ident) => {
|
2013-08-03 16:45:23 +00:00
|
|
|
for ident in opt_ident.iter() {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "'");
|
2012-10-18 19:20:18 +00:00
|
|
|
print_ident(s, *ident);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, ":");
|
2013-03-03 12:33:39 +00:00
|
|
|
}
|
2013-05-19 05:07:44 +00:00
|
|
|
head(s, "loop");
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2013-02-18 06:20:36 +00:00
|
|
|
print_block(s, blk);
|
2012-03-10 00:11:56 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprMatch(expr, ref arms) => {
|
2013-04-07 05:29:37 +00:00
|
|
|
cbox(s, indent_unit);
|
2012-10-28 00:14:09 +00:00
|
|
|
ibox(s, 4);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_nbsp(s, "match");
|
2012-10-28 00:14:09 +00:00
|
|
|
print_expr(s, expr);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2011-07-27 12:19:39 +00:00
|
|
|
bopen(s);
|
2013-04-07 05:29:37 +00:00
|
|
|
let len = arms.len();
|
2013-08-03 16:45:23 +00:00
|
|
|
for (i, arm) in arms.iter().enumerate() {
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2013-04-07 05:29:37 +00:00
|
|
|
cbox(s, indent_unit);
|
2011-07-27 12:19:39 +00:00
|
|
|
ibox(s, 0u);
|
2012-03-15 13:47:03 +00:00
|
|
|
let mut first = true;
|
2013-08-03 16:45:23 +00:00
|
|
|
for p in arm.pats.iter() {
|
2011-07-27 12:19:39 +00:00
|
|
|
if first {
|
|
|
|
first = false;
|
2013-12-27 22:11:01 +00:00
|
|
|
} else { space(&mut s.s); word_space(s, "|"); }
|
2013-06-21 16:19:22 +00:00
|
|
|
print_pat(s, *p);
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2012-08-06 19:34:08 +00:00
|
|
|
match arm.guard {
|
2012-08-20 19:23:37 +00:00
|
|
|
Some(e) => {
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, "if");
|
2012-08-04 02:59:04 +00:00
|
|
|
print_expr(s, e);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2012-08-04 02:59:04 +00:00
|
|
|
}
|
2012-08-20 19:23:37 +00:00
|
|
|
None => ()
|
2011-08-22 12:38:48 +00:00
|
|
|
}
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, "=>");
|
2012-08-28 22:54:45 +00:00
|
|
|
|
2012-08-01 23:16:53 +00:00
|
|
|
// Extract the expression from the extra block the parser adds
|
2012-08-28 22:54:45 +00:00
|
|
|
// in the case of foo => expr
|
2013-07-16 18:08:35 +00:00
|
|
|
if arm.body.view_items.is_empty() &&
|
|
|
|
arm.body.stmts.is_empty() &&
|
2013-07-27 08:25:59 +00:00
|
|
|
arm.body.rules == ast::DefaultBlock &&
|
2013-07-16 18:08:35 +00:00
|
|
|
arm.body.expr.is_some()
|
2012-08-28 22:54:45 +00:00
|
|
|
{
|
2013-07-16 18:08:35 +00:00
|
|
|
match arm.body.expr {
|
2012-08-28 22:54:45 +00:00
|
|
|
Some(expr) => {
|
|
|
|
match expr.node {
|
2013-11-30 22:00:39 +00:00
|
|
|
ast::ExprBlock(blk) => {
|
2012-08-28 22:54:45 +00:00
|
|
|
// the block will close the pattern's ibox
|
|
|
|
print_block_unclosed_indent(
|
2013-04-07 05:29:37 +00:00
|
|
|
s, blk, indent_unit);
|
2012-08-28 22:54:45 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
end(s); // close the ibox for the pattern
|
|
|
|
print_expr(s, expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !expr_is_simple_block(expr)
|
|
|
|
&& i < len - 1 {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ",");
|
2012-08-28 22:54:45 +00:00
|
|
|
}
|
|
|
|
end(s); // close enclosing cbox
|
|
|
|
}
|
2013-10-21 20:08:31 +00:00
|
|
|
None => fail!()
|
2012-08-01 23:16:53 +00:00
|
|
|
}
|
2012-08-28 22:54:45 +00:00
|
|
|
} else {
|
|
|
|
// the block will close the pattern's ibox
|
2013-11-30 22:00:39 +00:00
|
|
|
print_block_unclosed_indent(s, arm.body, indent_unit);
|
2012-07-10 17:37:05 +00:00
|
|
|
}
|
2011-06-14 13:20:04 +00:00
|
|
|
}
|
2013-04-07 05:29:37 +00:00
|
|
|
bclose_(s, expr.span, indent_unit);
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2013-11-30 22:00:39 +00:00
|
|
|
ast::ExprFnBlock(decl, body) => {
|
2012-08-09 23:31:47 +00:00
|
|
|
// in do/for blocks we don't want to show an empty
|
|
|
|
// argument list, but at this point we don't know which
|
|
|
|
// we are inside.
|
|
|
|
//
|
|
|
|
// if !decl.inputs.is_empty() {
|
2013-02-28 15:25:31 +00:00
|
|
|
print_fn_block_args(s, decl);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2012-08-09 23:31:47 +00:00
|
|
|
// }
|
2013-07-16 18:08:35 +00:00
|
|
|
assert!(body.stmts.is_empty());
|
|
|
|
assert!(body.expr.is_some());
|
2012-08-09 23:31:47 +00:00
|
|
|
// we extract the block, so as not to create another set of boxes
|
2013-08-03 23:59:24 +00:00
|
|
|
match body.expr.unwrap().node {
|
2013-11-30 22:00:39 +00:00
|
|
|
ast::ExprBlock(blk) => {
|
2013-02-18 06:20:36 +00:00
|
|
|
print_block_unclosed(s, blk);
|
2012-08-09 23:31:47 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// this is a bare expression
|
2013-08-03 23:59:24 +00:00
|
|
|
print_expr(s, body.expr.unwrap());
|
2012-08-09 23:31:47 +00:00
|
|
|
end(s); // need to close a box
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// a box will be closed by print_expr, but we didn't want an overall
|
|
|
|
// wrapper so we closed the corresponding opening. so create an
|
|
|
|
// empty box to satisfy the close.
|
|
|
|
ibox(s, 0);
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2013-11-30 22:00:39 +00:00
|
|
|
ast::ExprProc(decl, body) => {
|
2013-10-28 22:22:49 +00:00
|
|
|
// in do/for blocks we don't want to show an empty
|
|
|
|
// argument list, but at this point we don't know which
|
|
|
|
// we are inside.
|
|
|
|
//
|
|
|
|
// if !decl.inputs.is_empty() {
|
|
|
|
print_proc_args(s, decl);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2013-10-28 22:22:49 +00:00
|
|
|
// }
|
|
|
|
assert!(body.stmts.is_empty());
|
|
|
|
assert!(body.expr.is_some());
|
|
|
|
// we extract the block, so as not to create another set of boxes
|
|
|
|
match body.expr.unwrap().node {
|
2013-11-30 22:00:39 +00:00
|
|
|
ast::ExprBlock(blk) => {
|
2013-10-28 22:22:49 +00:00
|
|
|
print_block_unclosed(s, blk);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// this is a bare expression
|
|
|
|
print_expr(s, body.expr.unwrap());
|
|
|
|
end(s); // need to close a box
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// a box will be closed by print_expr, but we didn't want an overall
|
|
|
|
// wrapper so we closed the corresponding opening. so create an
|
|
|
|
// empty box to satisfy the close.
|
|
|
|
ibox(s, 0);
|
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprDoBody(body) => {
|
2012-06-19 00:42:09 +00:00
|
|
|
print_expr(s, body);
|
|
|
|
}
|
2013-11-30 22:00:39 +00:00
|
|
|
ast::ExprBlock(blk) => {
|
2011-07-27 12:19:39 +00:00
|
|
|
// containing cbox, will be closed by print-block at }
|
|
|
|
cbox(s, indent_unit);
|
|
|
|
// head-box, will be closed by print-block after {
|
|
|
|
ibox(s, 0u);
|
2013-02-18 06:20:36 +00:00
|
|
|
print_block(s, blk);
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprAssign(lhs, rhs) => {
|
2011-07-27 12:19:39 +00:00
|
|
|
print_expr(s, lhs);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, "=");
|
2011-07-27 12:19:39 +00:00
|
|
|
print_expr(s, rhs);
|
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprAssignOp(_, op, lhs, rhs) => {
|
2011-07-27 12:19:39 +00:00
|
|
|
print_expr(s, lhs);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
|
|
|
word(&mut s.s, ast_util::binop_to_str(op));
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, "=");
|
2011-07-27 12:19:39 +00:00
|
|
|
print_expr(s, rhs);
|
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprField(expr, id, ref tys) => {
|
2012-10-28 00:14:09 +00:00
|
|
|
print_expr(s, expr);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ".");
|
2012-07-18 23:18:02 +00:00
|
|
|
print_ident(s, id);
|
2013-03-20 01:24:01 +00:00
|
|
|
if tys.len() > 0u {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "::<");
|
2014-01-09 13:05:33 +00:00
|
|
|
commasep(s, Inconsistent, *tys, print_type_ref);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ">");
|
2011-12-19 09:21:31 +00:00
|
|
|
}
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprIndex(_, expr, index) => {
|
2012-10-28 00:14:09 +00:00
|
|
|
print_expr(s, expr);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "[");
|
2011-07-27 12:19:39 +00:00
|
|
|
print_expr(s, index);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "]");
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprPath(ref path) => print_path(s, path, true),
|
2013-12-27 22:11:01 +00:00
|
|
|
ast::ExprSelf => word(&mut s.s, "self"),
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprBreak(opt_ident) => {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "break");
|
|
|
|
space(&mut s.s);
|
2013-08-03 16:45:23 +00:00
|
|
|
for ident in opt_ident.iter() {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "'");
|
2013-09-10 19:01:44 +00:00
|
|
|
print_name(s, *ident);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2013-04-26 23:19:26 +00:00
|
|
|
}
|
2012-08-15 02:20:56 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprAgain(opt_ident) => {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "continue");
|
|
|
|
space(&mut s.s);
|
2013-08-03 16:45:23 +00:00
|
|
|
for ident in opt_ident.iter() {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "'");
|
2013-09-10 19:01:44 +00:00
|
|
|
print_name(s, *ident);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s)
|
2013-04-26 23:19:26 +00:00
|
|
|
}
|
2012-08-15 02:20:56 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprRet(result) => {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "return");
|
2012-08-06 19:34:08 +00:00
|
|
|
match result {
|
2013-12-27 22:11:01 +00:00
|
|
|
Some(expr) => { word(&mut s.s, " "); print_expr(s, expr); }
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => ()
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2013-08-28 06:12:05 +00:00
|
|
|
ast::ExprLogLevel => {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "__log_level");
|
2013-03-27 04:42:01 +00:00
|
|
|
popen(s);
|
|
|
|
pclose(s);
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprInlineAsm(ref a) => {
|
2013-03-27 20:42:21 +00:00
|
|
|
if a.volatile {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "__volatile__ asm!");
|
2013-03-12 07:01:09 +00:00
|
|
|
} else {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "asm!");
|
2013-03-12 07:01:09 +00:00
|
|
|
}
|
2013-03-10 06:37:50 +00:00
|
|
|
popen(s);
|
2013-10-08 00:49:10 +00:00
|
|
|
print_string(s, a.asm, a.asm_str_style);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, ":");
|
2013-08-03 16:45:23 +00:00
|
|
|
for &(co, o) in a.outputs.iter() {
|
2013-10-08 00:49:10 +00:00
|
|
|
print_string(s, co, ast::CookedStr);
|
2013-03-13 00:53:25 +00:00
|
|
|
popen(s);
|
|
|
|
print_expr(s, o);
|
|
|
|
pclose(s);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, ",");
|
2013-03-13 00:53:25 +00:00
|
|
|
}
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, ":");
|
2013-08-03 16:45:23 +00:00
|
|
|
for &(co, o) in a.inputs.iter() {
|
2013-10-08 00:49:10 +00:00
|
|
|
print_string(s, co, ast::CookedStr);
|
2013-03-13 00:53:25 +00:00
|
|
|
popen(s);
|
|
|
|
print_expr(s, o);
|
|
|
|
pclose(s);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, ",");
|
2013-03-13 00:53:25 +00:00
|
|
|
}
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, ":");
|
2013-10-08 00:49:10 +00:00
|
|
|
print_string(s, a.clobbers, ast::CookedStr);
|
2013-03-10 06:37:50 +00:00
|
|
|
pclose(s);
|
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::ExprMac(ref m) => print_mac(s, m),
|
|
|
|
ast::ExprParen(e) => {
|
2012-10-28 00:14:09 +00:00
|
|
|
popen(s);
|
|
|
|
print_expr(s, e);
|
|
|
|
pclose(s);
|
|
|
|
}
|
2011-03-25 03:03:12 +00:00
|
|
|
}
|
2013-12-27 22:11:01 +00:00
|
|
|
{
|
2014-01-09 13:05:33 +00:00
|
|
|
let ann_node = NodeExpr(s, expr);
|
2013-12-27 22:11:01 +00:00
|
|
|
s.ann.post(ann_node);
|
|
|
|
}
|
2011-06-01 22:29:38 +00:00
|
|
|
end(s);
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_local_decl(s: &mut State, loc: &ast::Local) {
|
2013-07-19 05:38:55 +00:00
|
|
|
print_pat(s, loc.pat);
|
|
|
|
match loc.ty.node {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::TyInfer => {}
|
|
|
|
_ => { word_space(s, ":"); print_type(s, loc.ty); }
|
2011-08-10 19:51:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_decl(s: &mut State, decl: &ast::Decl) {
|
2011-03-24 15:33:20 +00:00
|
|
|
maybe_print_comment(s, decl.span.lo);
|
2013-03-20 01:24:01 +00:00
|
|
|
match decl.node {
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::DeclLocal(ref loc) => {
|
2011-07-27 12:19:39 +00:00
|
|
|
space_if_not_bol(s);
|
|
|
|
ibox(s, indent_unit);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_nbsp(s, "let");
|
2012-03-22 15:39:41 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
fn print_local(s: &mut State, loc: &ast::Local) {
|
2011-07-28 10:01:45 +00:00
|
|
|
ibox(s, indent_unit);
|
2011-08-10 19:51:50 +00:00
|
|
|
print_local_decl(s, loc);
|
2011-07-28 10:01:45 +00:00
|
|
|
end(s);
|
2013-07-19 05:38:55 +00:00
|
|
|
match loc.init {
|
2012-08-20 19:23:37 +00:00
|
|
|
Some(init) => {
|
2011-07-27 12:19:39 +00:00
|
|
|
nbsp(s);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, "=");
|
2012-10-23 18:28:20 +00:00
|
|
|
print_expr(s, init);
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => ()
|
2011-03-25 03:03:12 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
2013-06-05 04:43:41 +00:00
|
|
|
|
|
|
|
print_local(s, *loc);
|
2011-07-27 12:19:39 +00:00
|
|
|
end(s);
|
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::DeclItem(item) => print_item(s, item)
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_ident(s: &mut State, ident: ast::Ident) {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ident_to_str(&ident));
|
2013-01-29 22:41:40 +00:00
|
|
|
}
|
2011-04-05 21:18:44 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_name(s: &mut State, name: ast::Name) {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, interner_get(name));
|
2013-09-10 19:01:44 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_for_decl(s: &mut State, loc: &ast::Local, coll: &ast::Expr) {
|
2011-08-10 19:51:50 +00:00
|
|
|
print_local_decl(s, loc);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, "in");
|
2011-07-27 15:18:53 +00:00
|
|
|
print_expr(s, coll);
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
fn print_path_(s: &mut State,
|
2013-08-07 16:47:28 +00:00
|
|
|
path: &ast::Path,
|
|
|
|
colons_before_params: bool,
|
2013-06-20 22:23:25 +00:00
|
|
|
opt_bounds: &Option<OptVec<ast::TyParamBound>>) {
|
2011-03-24 15:33:20 +00:00
|
|
|
maybe_print_comment(s, path.span.lo);
|
2013-08-07 16:47:28 +00:00
|
|
|
if path.global {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "::");
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
2012-04-24 22:52:52 +00:00
|
|
|
|
2013-08-07 16:47:28 +00:00
|
|
|
let mut first = true;
|
|
|
|
for (i, segment) in path.segments.iter().enumerate() {
|
|
|
|
if first {
|
|
|
|
first = false
|
|
|
|
} else {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "::")
|
2013-08-07 16:47:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
print_ident(s, segment.identifier);
|
|
|
|
|
2013-09-17 20:13:47 +00:00
|
|
|
// If this is the last segment, print the bounds.
|
|
|
|
if i == path.segments.len() - 1 {
|
|
|
|
match *opt_bounds {
|
|
|
|
None => {}
|
|
|
|
Some(ref bounds) => print_bounds(s, bounds, true),
|
2013-08-07 16:47:28 +00:00
|
|
|
}
|
2013-09-17 20:13:47 +00:00
|
|
|
}
|
2013-08-07 16:47:28 +00:00
|
|
|
|
2013-10-29 10:03:32 +00:00
|
|
|
if !segment.lifetimes.is_empty() || !segment.types.is_empty() {
|
2013-08-07 16:47:28 +00:00
|
|
|
if colons_before_params {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "::")
|
2013-08-07 16:47:28 +00:00
|
|
|
}
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "<");
|
2013-02-28 00:41:02 +00:00
|
|
|
|
2013-11-09 03:25:22 +00:00
|
|
|
let mut comma = false;
|
2013-10-29 10:03:32 +00:00
|
|
|
for lifetime in segment.lifetimes.iter() {
|
2013-11-09 03:25:22 +00:00
|
|
|
if comma {
|
2013-08-07 16:47:28 +00:00
|
|
|
word_space(s, ",")
|
2013-02-28 00:41:02 +00:00
|
|
|
}
|
2013-11-09 03:25:22 +00:00
|
|
|
print_lifetime(s, lifetime);
|
|
|
|
comma = true;
|
2013-02-28 00:41:02 +00:00
|
|
|
}
|
|
|
|
|
2013-11-09 03:25:22 +00:00
|
|
|
if !segment.types.is_empty() {
|
|
|
|
if comma {
|
|
|
|
word_space(s, ",")
|
|
|
|
}
|
|
|
|
commasep(s,
|
2014-01-09 13:05:33 +00:00
|
|
|
Inconsistent,
|
2013-11-30 22:00:39 +00:00
|
|
|
segment.types.map_to_vec(|&t| t),
|
|
|
|
print_type_ref);
|
2013-11-09 03:25:22 +00:00
|
|
|
}
|
2013-02-28 00:41:02 +00:00
|
|
|
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ">")
|
2012-04-24 22:52:52 +00:00
|
|
|
}
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_path(s: &mut State, path: &ast::Path, colons_before_params: bool) {
|
2013-06-20 22:23:25 +00:00
|
|
|
print_path_(s, path, colons_before_params, &None)
|
2013-06-17 19:16:30 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_bounded_path(s: &mut State, path: &ast::Path,
|
2013-06-24 17:30:35 +00:00
|
|
|
bounds: &Option<OptVec<ast::TyParamBound>>) {
|
|
|
|
print_path_(s, path, false, bounds)
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_pat(s: &mut State, pat: &ast::Pat) {
|
2011-03-24 15:33:20 +00:00
|
|
|
maybe_print_comment(s, pat.span.lo);
|
2013-12-27 22:11:01 +00:00
|
|
|
{
|
2014-01-09 13:05:33 +00:00
|
|
|
let ann_node = NodePat(s, pat);
|
2013-12-27 22:11:01 +00:00
|
|
|
s.ann.pre(ann_node);
|
|
|
|
}
|
2012-01-15 00:05:07 +00:00
|
|
|
/* Pat isn't normalized, but the beauty of it
|
|
|
|
is that it doesn't matter */
|
2013-03-20 01:24:01 +00:00
|
|
|
match pat.node {
|
2013-12-27 22:11:01 +00:00
|
|
|
ast::PatWild => word(&mut s.s, "_"),
|
|
|
|
ast::PatWildMulti => word(&mut s.s, ".."),
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::PatIdent(binding_mode, ref path, sub) => {
|
2013-06-21 16:19:22 +00:00
|
|
|
match binding_mode {
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::BindByRef(mutbl) => {
|
2013-06-21 16:19:22 +00:00
|
|
|
word_nbsp(s, "ref");
|
|
|
|
print_mutability(s, mutbl);
|
2012-12-07 20:52:01 +00:00
|
|
|
}
|
2013-10-20 12:31:23 +00:00
|
|
|
ast::BindByValue(ast::MutImmutable) => {}
|
|
|
|
ast::BindByValue(ast::MutMutable) => {
|
|
|
|
word_nbsp(s, "mut");
|
|
|
|
}
|
2012-08-24 18:04:07 +00:00
|
|
|
}
|
2012-12-07 20:52:01 +00:00
|
|
|
print_path(s, path, true);
|
|
|
|
match sub {
|
|
|
|
Some(p) => {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "@");
|
2013-06-21 16:19:22 +00:00
|
|
|
print_pat(s, p);
|
2012-12-07 20:52:01 +00:00
|
|
|
}
|
|
|
|
None => ()
|
2012-12-07 20:29:46 +00:00
|
|
|
}
|
2011-12-08 10:56:16 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::PatEnum(ref path, ref args_) => {
|
2011-08-16 16:03:58 +00:00
|
|
|
print_path(s, path, true);
|
2013-03-20 01:24:01 +00:00
|
|
|
match *args_ {
|
2013-12-27 22:11:01 +00:00
|
|
|
None => word(&mut s.s, "(..)"),
|
2013-03-20 01:24:01 +00:00
|
|
|
Some(ref args) => {
|
2013-01-25 03:19:44 +00:00
|
|
|
if !args.is_empty() {
|
2012-04-20 07:54:42 +00:00
|
|
|
popen(s);
|
2014-01-09 13:05:33 +00:00
|
|
|
commasep(s, Inconsistent, *args,
|
2013-06-21 16:19:22 +00:00
|
|
|
|s, &p| print_pat(s, p));
|
2012-04-20 07:54:42 +00:00
|
|
|
pclose(s);
|
|
|
|
} else { }
|
|
|
|
}
|
|
|
|
}
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::PatStruct(ref path, ref fields, etc) => {
|
2012-08-07 00:01:14 +00:00
|
|
|
print_path(s, path, true);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "{");
|
2014-01-09 13:05:33 +00:00
|
|
|
fn print_field(s: &mut State, f: &ast::FieldPat) {
|
2012-08-07 00:01:14 +00:00
|
|
|
cbox(s, indent_unit);
|
2012-07-18 23:18:02 +00:00
|
|
|
print_ident(s, f.ident);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, ":");
|
2013-06-21 16:19:22 +00:00
|
|
|
print_pat(s, f.pat);
|
2011-07-27 12:19:39 +00:00
|
|
|
end(s);
|
2011-07-11 12:13:20 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
fn get_span(f: &ast::FieldPat) -> codemap::Span { return f.pat.span; }
|
2014-01-09 13:05:33 +00:00
|
|
|
commasep_cmnt(s, Consistent, *fields,
|
2013-06-21 16:19:22 +00:00
|
|
|
|s, f| print_field(s,f),
|
2012-12-07 22:39:29 +00:00
|
|
|
get_span);
|
2011-07-27 12:19:39 +00:00
|
|
|
if etc {
|
2013-05-19 05:07:44 +00:00
|
|
|
if fields.len() != 0u { word_space(s, ","); }
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "..");
|
2011-07-13 08:50:16 +00:00
|
|
|
}
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "}");
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::PatTup(ref elts) => {
|
2011-08-15 11:15:19 +00:00
|
|
|
popen(s);
|
2014-01-09 13:05:33 +00:00
|
|
|
commasep(s, Inconsistent, *elts, |s, &p| print_pat(s, p));
|
2013-02-17 23:41:47 +00:00
|
|
|
if elts.len() == 1 {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ",");
|
2013-02-17 23:41:47 +00:00
|
|
|
}
|
2011-08-15 11:15:19 +00:00
|
|
|
pclose(s);
|
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::PatUniq(inner) => {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "~");
|
2013-06-21 16:19:22 +00:00
|
|
|
print_pat(s, inner);
|
2012-12-07 20:52:01 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::PatRegion(inner) => {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "&");
|
2013-06-21 16:19:22 +00:00
|
|
|
print_pat(s, inner);
|
2012-09-08 00:07:32 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::PatLit(e) => print_expr(s, e),
|
|
|
|
ast::PatRange(begin, end) => {
|
2011-12-02 12:42:51 +00:00
|
|
|
print_expr(s, begin);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
|
|
|
word(&mut s.s, "..");
|
2011-12-02 12:42:51 +00:00
|
|
|
print_expr(s, end);
|
2011-09-28 19:07:33 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::PatVec(ref before, slice, ref after) => {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "[");
|
2014-01-09 13:05:33 +00:00
|
|
|
commasep(s, Inconsistent, *before, |s, &p| print_pat(s, p));
|
2013-08-03 16:45:23 +00:00
|
|
|
for &p in slice.iter() {
|
2013-05-19 05:07:44 +00:00
|
|
|
if !before.is_empty() { word_space(s, ","); }
|
2014-01-03 23:08:48 +00:00
|
|
|
match *p {
|
|
|
|
ast::Pat { node: ast::PatWildMulti, .. } => {
|
2013-11-08 03:25:39 +00:00
|
|
|
// this case is handled by print_pat
|
|
|
|
}
|
2013-12-27 22:11:01 +00:00
|
|
|
_ => word(&mut s.s, ".."),
|
2013-11-08 03:25:39 +00:00
|
|
|
}
|
2013-06-21 16:19:22 +00:00
|
|
|
print_pat(s, p);
|
2013-05-19 05:07:44 +00:00
|
|
|
if !after.is_empty() { word_space(s, ","); }
|
2013-02-26 18:58:46 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
commasep(s, Inconsistent, *after, |s, &p| print_pat(s, p));
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "]");
|
2012-12-08 20:22:43 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
2013-12-27 22:11:01 +00:00
|
|
|
{
|
2014-01-09 13:05:33 +00:00
|
|
|
let ann_node = NodePat(s, pat);
|
2013-12-27 22:11:01 +00:00
|
|
|
s.ann.post(ann_node);
|
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn explicit_self_to_str(explicit_self: &ast::ExplicitSelf_, intr: @IdentInterner) -> ~str {
|
2013-07-06 00:47:42 +00:00
|
|
|
to_str(explicit_self, |a, &b| { print_explicit_self(a, b); () }, intr)
|
2013-04-10 20:11:27 +00:00
|
|
|
}
|
|
|
|
|
2012-08-16 23:44:22 +00:00
|
|
|
// Returns whether it printed anything
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_explicit_self(s: &mut State, explicit_self: ast::ExplicitSelf_) -> bool {
|
2013-04-30 15:49:48 +00:00
|
|
|
match explicit_self {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::SelfStatic => { return false; }
|
|
|
|
ast::SelfValue(m) => {
|
2013-10-20 05:55:23 +00:00
|
|
|
print_mutability(s, m);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "self");
|
2013-10-20 05:55:23 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::SelfUniq(m) => {
|
2013-10-20 06:34:01 +00:00
|
|
|
print_mutability(s, m);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "~self");
|
2013-10-20 06:34:01 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::SelfRegion(ref lt, m) => {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "&");
|
2013-03-10 00:43:53 +00:00
|
|
|
print_opt_lifetime(s, lt);
|
|
|
|
print_mutability(s, m);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "self");
|
2013-03-10 00:43:53 +00:00
|
|
|
}
|
2014-01-12 00:25:51 +00:00
|
|
|
ast::SelfBox => {
|
|
|
|
word(&mut s.s, "@self");
|
2013-03-10 00:43:53 +00:00
|
|
|
}
|
2012-08-17 23:14:57 +00:00
|
|
|
}
|
2012-08-16 23:44:22 +00:00
|
|
|
return true;
|
2012-08-17 23:14:57 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_fn(s: &mut State,
|
|
|
|
decl: &ast::FnDecl,
|
|
|
|
purity: Option<ast::Purity>,
|
2013-03-14 02:25:28 +00:00
|
|
|
abis: AbiSet,
|
2013-09-02 00:50:59 +00:00
|
|
|
name: ast::Ident,
|
2013-02-15 05:50:03 +00:00
|
|
|
generics: &ast::Generics,
|
2014-01-09 13:05:33 +00:00
|
|
|
opt_explicit_self: Option<ast::ExplicitSelf_>,
|
|
|
|
vis: ast::Visibility) {
|
2013-05-19 05:07:44 +00:00
|
|
|
head(s, "");
|
2013-04-30 15:49:48 +00:00
|
|
|
print_fn_header_info(s, opt_explicit_self, purity, abis, ast::Many, None, vis);
|
2012-11-05 04:41:00 +00:00
|
|
|
nbsp(s);
|
2012-07-18 23:18:02 +00:00
|
|
|
print_ident(s, name);
|
2013-02-15 05:50:03 +00:00
|
|
|
print_generics(s, generics);
|
2013-04-30 15:49:48 +00:00
|
|
|
print_fn_args_and_ret(s, decl, opt_explicit_self);
|
2011-06-14 13:20:04 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_fn_args(s: &mut State, decl: &ast::FnDecl,
|
|
|
|
opt_explicit_self: Option<ast::ExplicitSelf_>) {
|
2013-06-06 07:38:41 +00:00
|
|
|
// It is unfortunate to duplicate the commasep logic, but we want the
|
2013-01-10 18:59:58 +00:00
|
|
|
// self type and the args all in the same box.
|
2014-01-09 13:05:33 +00:00
|
|
|
rbox(s, 0u, Inconsistent);
|
2012-08-17 23:14:57 +00:00
|
|
|
let mut first = true;
|
2013-08-03 16:45:23 +00:00
|
|
|
for explicit_self in opt_explicit_self.iter() {
|
2013-04-30 15:49:48 +00:00
|
|
|
first = !print_explicit_self(s, *explicit_self);
|
2012-08-17 23:14:57 +00:00
|
|
|
}
|
|
|
|
|
2013-08-03 16:45:23 +00:00
|
|
|
for arg in decl.inputs.iter() {
|
2013-05-19 05:07:44 +00:00
|
|
|
if first { first = false; } else { word_space(s, ","); }
|
2013-07-06 04:57:11 +00:00
|
|
|
print_arg(s, arg);
|
2011-12-30 21:32:42 +00:00
|
|
|
}
|
2012-08-17 23:14:57 +00:00
|
|
|
|
|
|
|
end(s);
|
2011-12-30 21:32:42 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_fn_args_and_ret(s: &mut State, decl: &ast::FnDecl,
|
|
|
|
opt_explicit_self: Option<ast::ExplicitSelf_>) {
|
2011-03-24 15:33:20 +00:00
|
|
|
popen(s);
|
2013-04-30 15:49:48 +00:00
|
|
|
print_fn_args(s, decl, opt_explicit_self);
|
2013-10-25 05:56:34 +00:00
|
|
|
if decl.variadic {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ", ...");
|
2013-10-25 05:56:34 +00:00
|
|
|
}
|
2011-03-24 15:33:20 +00:00
|
|
|
pclose(s);
|
2012-02-22 10:16:25 +00:00
|
|
|
|
2011-03-24 15:33:20 +00:00
|
|
|
maybe_print_comment(s, decl.output.span.lo);
|
2012-09-08 01:53:14 +00:00
|
|
|
match decl.output.node {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::TyNil => {}
|
2012-09-08 01:53:14 +00:00
|
|
|
_ => {
|
|
|
|
space_if_not_bol(s);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, "->");
|
2013-11-30 22:00:39 +00:00
|
|
|
print_type(s, decl.output);
|
2012-09-08 01:53:14 +00:00
|
|
|
}
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_fn_block_args(s: &mut State, decl: &ast::FnDecl) {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "|");
|
2013-01-10 18:59:58 +00:00
|
|
|
print_fn_args(s, decl, None);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "|");
|
2012-09-08 01:53:14 +00:00
|
|
|
|
|
|
|
match decl.output.node {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::TyInfer => {}
|
2012-09-08 01:53:14 +00:00
|
|
|
_ => {
|
|
|
|
space_if_not_bol(s);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, "->");
|
2013-11-30 22:00:39 +00:00
|
|
|
print_type(s, decl.output);
|
2012-09-08 01:53:14 +00:00
|
|
|
}
|
2011-12-21 00:18:18 +00:00
|
|
|
}
|
2012-09-08 01:53:14 +00:00
|
|
|
|
2011-08-15 21:42:33 +00:00
|
|
|
maybe_print_comment(s, decl.output.span.lo);
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_proc_args(s: &mut State, decl: &ast::FnDecl) {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "proc");
|
|
|
|
word(&mut s.s, "(");
|
2013-10-28 22:22:49 +00:00
|
|
|
print_fn_args(s, decl, None);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ")");
|
2013-10-28 22:22:49 +00:00
|
|
|
|
|
|
|
match decl.output.node {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::TyInfer => {}
|
2013-10-28 22:22:49 +00:00
|
|
|
_ => {
|
|
|
|
space_if_not_bol(s);
|
|
|
|
word_space(s, "->");
|
2013-11-30 22:00:39 +00:00
|
|
|
print_type(s, decl.output);
|
2013-10-28 22:22:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
maybe_print_comment(s, decl.output.span.lo);
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_bounds(s: &mut State, bounds: &OptVec<ast::TyParamBound>,
|
2013-06-24 18:32:05 +00:00
|
|
|
print_colon_anyway: bool) {
|
2013-01-25 03:19:44 +00:00
|
|
|
if !bounds.is_empty() {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ":");
|
2013-01-22 22:37:32 +00:00
|
|
|
let mut first = true;
|
2013-08-03 16:45:23 +00:00
|
|
|
for bound in bounds.iter() {
|
2011-12-28 16:50:12 +00:00
|
|
|
nbsp(s);
|
2013-01-22 22:37:32 +00:00
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
} else {
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, "+");
|
2013-01-22 22:37:32 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 05:50:03 +00:00
|
|
|
match *bound {
|
2013-07-06 00:47:42 +00:00
|
|
|
TraitTyParamBound(ref tref) => print_trait_ref(s, tref),
|
2013-12-27 22:11:01 +00:00
|
|
|
RegionTyParamBound => word(&mut s.s, "'static"),
|
2013-01-10 19:16:54 +00:00
|
|
|
}
|
2011-12-28 16:50:12 +00:00
|
|
|
}
|
2013-06-24 18:32:05 +00:00
|
|
|
} else if print_colon_anyway {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ":");
|
2011-08-03 04:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_lifetime(s: &mut State, lifetime: &ast::Lifetime) {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "'");
|
2013-02-15 05:50:03 +00:00
|
|
|
print_ident(s, lifetime.ident);
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_generics(s: &mut State, generics: &ast::Generics) {
|
2013-02-15 05:50:03 +00:00
|
|
|
let total = generics.lifetimes.len() + generics.ty_params.len();
|
|
|
|
if total > 0 {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "<");
|
2014-01-09 13:05:33 +00:00
|
|
|
fn print_item(s: &mut State, generics: &ast::Generics, idx: uint) {
|
2013-02-15 05:50:03 +00:00
|
|
|
if idx < generics.lifetimes.len() {
|
|
|
|
let lifetime = generics.lifetimes.get(idx);
|
|
|
|
print_lifetime(s, lifetime);
|
|
|
|
} else {
|
2013-03-01 16:25:51 +00:00
|
|
|
let idx = idx - generics.lifetimes.len();
|
2013-02-15 05:50:03 +00:00
|
|
|
let param = generics.ty_params.get(idx);
|
|
|
|
print_ident(s, param.ident);
|
2013-07-06 01:38:56 +00:00
|
|
|
print_bounds(s, ¶m.bounds, false);
|
2013-02-15 05:50:03 +00:00
|
|
|
}
|
2011-07-28 17:22:59 +00:00
|
|
|
}
|
2013-02-15 05:50:03 +00:00
|
|
|
|
|
|
|
let mut ints = ~[];
|
2013-08-03 16:45:23 +00:00
|
|
|
for i in range(0u, total) {
|
2013-02-15 05:50:03 +00:00
|
|
|
ints.push(i);
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
commasep(s, Inconsistent, ints,
|
2013-07-06 00:47:42 +00:00
|
|
|
|s, &i| print_item(s, generics, i));
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ">");
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_meta_item(s: &mut State, item: &ast::MetaItem) {
|
2011-06-15 01:53:12 +00:00
|
|
|
ibox(s, indent_unit);
|
2012-08-06 19:34:08 +00:00
|
|
|
match item.node {
|
2013-12-27 22:11:01 +00:00
|
|
|
ast::MetaWord(name) => word(&mut s.s, name),
|
2013-07-19 11:51:37 +00:00
|
|
|
ast::MetaNameValue(name, value) => {
|
2013-06-12 17:02:55 +00:00
|
|
|
word_space(s, name);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, "=");
|
2013-12-09 14:18:24 +00:00
|
|
|
print_literal(s, &value);
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2013-07-19 11:51:37 +00:00
|
|
|
ast::MetaList(name, ref items) => {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, name);
|
2011-07-27 12:19:39 +00:00
|
|
|
popen(s);
|
2013-07-02 19:47:32 +00:00
|
|
|
commasep(s,
|
2014-01-09 13:05:33 +00:00
|
|
|
Consistent,
|
2013-07-02 19:47:32 +00:00
|
|
|
items.as_slice(),
|
|
|
|
|p, &i| print_meta_item(p, i));
|
2011-07-27 12:19:39 +00:00
|
|
|
pclose(s);
|
|
|
|
}
|
2011-06-21 21:23:16 +00:00
|
|
|
}
|
2011-06-15 01:53:12 +00:00
|
|
|
end(s);
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_view_path(s: &mut State, vp: &ast::ViewPath) {
|
2012-08-06 19:34:08 +00:00
|
|
|
match vp.node {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ViewPathSimple(ident, ref path, _) => {
|
2013-10-29 15:19:28 +00:00
|
|
|
// FIXME(#6993) can't compare identifiers directly here
|
2013-10-29 10:03:32 +00:00
|
|
|
if path.segments.last().identifier.name != ident.name {
|
2012-07-18 23:18:02 +00:00
|
|
|
print_ident(s, ident);
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, "=");
|
2012-02-18 07:05:20 +00:00
|
|
|
}
|
2012-04-13 08:46:56 +00:00
|
|
|
print_path(s, path, false);
|
2012-02-18 07:05:20 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ViewPathGlob(ref path, _) => {
|
2012-04-13 08:46:56 +00:00
|
|
|
print_path(s, path, false);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "::*");
|
2012-02-18 07:05:20 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ViewPathList(ref path, ref idents, _) => {
|
2013-12-04 21:08:42 +00:00
|
|
|
if path.segments.is_empty() {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "{");
|
2013-12-04 21:08:42 +00:00
|
|
|
} else {
|
|
|
|
print_path(s, path, false);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "::{");
|
2013-12-04 21:08:42 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
commasep(s, Inconsistent, (*idents), |s, w| {
|
2012-07-18 23:18:02 +00:00
|
|
|
print_ident(s, w.node.name);
|
2013-11-21 00:23:04 +00:00
|
|
|
});
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "}");
|
2012-02-18 07:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_view_paths(s: &mut State, vps: &[@ast::ViewPath]) {
|
|
|
|
commasep(s, Inconsistent, vps, |p, &vp| print_view_path(p, vp));
|
2012-02-18 07:05:20 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_view_item(s: &mut State, item: &ast::ViewItem) {
|
2011-06-20 16:15:11 +00:00
|
|
|
hardbreak_if_not_bol(s);
|
2011-03-24 15:33:20 +00:00
|
|
|
maybe_print_comment(s, item.span.lo);
|
2012-07-09 22:39:56 +00:00
|
|
|
print_outer_attributes(s, item.attrs);
|
2012-09-22 01:10:45 +00:00
|
|
|
print_visibility(s, item.vis);
|
2013-03-20 01:24:01 +00:00
|
|
|
match item.node {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ViewItemExternMod(id, ref optional_path, _) => {
|
2013-05-19 05:07:44 +00:00
|
|
|
head(s, "extern mod");
|
2013-01-31 01:20:02 +00:00
|
|
|
print_ident(s, id);
|
2013-10-08 00:49:10 +00:00
|
|
|
for &(ref p, style) in optional_path.iter() {
|
2013-12-27 22:11:01 +00:00
|
|
|
space(&mut s.s);
|
|
|
|
word(&mut s.s, "=");
|
|
|
|
space(&mut s.s);
|
2013-10-08 00:49:10 +00:00
|
|
|
print_string(s, *p, style);
|
2013-07-31 20:47:32 +00:00
|
|
|
}
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
2012-02-18 07:05:20 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ViewItemUse(ref vps) => {
|
2013-05-19 05:07:44 +00:00
|
|
|
head(s, "use");
|
2013-03-20 01:24:01 +00:00
|
|
|
print_view_paths(s, *vps);
|
2013-01-31 01:20:02 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ";");
|
2011-06-01 22:29:38 +00:00
|
|
|
end(s); // end inner head-block
|
|
|
|
end(s); // end outer head-block
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_mutability(s: &mut State, mutbl: ast::Mutability) {
|
2012-08-06 19:34:08 +00:00
|
|
|
match mutbl {
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::MutMutable => word_nbsp(s, "mut"),
|
|
|
|
ast::MutImmutable => {/* nothing */ }
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
2011-06-15 17:25:23 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_mt(s: &mut State, mt: &ast::MutTy) {
|
2012-02-15 19:25:39 +00:00
|
|
|
print_mutability(s, mt.mutbl);
|
2011-08-15 11:45:04 +00:00
|
|
|
print_type(s, mt.ty);
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_arg(s: &mut State, input: &ast::Arg) {
|
2012-05-04 19:33:04 +00:00
|
|
|
ibox(s, indent_unit);
|
2012-08-06 19:34:08 +00:00
|
|
|
match input.ty.node {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::TyInfer => print_pat(s, input.pat),
|
|
|
|
_ => {
|
|
|
|
match input.pat.node {
|
|
|
|
ast::PatIdent(_, ref path, _) if
|
|
|
|
path.segments.len() == 1 &&
|
|
|
|
path.segments[0].identifier.name ==
|
|
|
|
parse::token::special_idents::invalid.name => {
|
|
|
|
// Do nothing.
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
print_pat(s, input.pat);
|
|
|
|
word(&mut s.s, ":");
|
|
|
|
space(&mut s.s);
|
|
|
|
}
|
2012-11-07 02:41:06 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
print_type(s, input.ty);
|
2012-05-04 19:33:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end(s);
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_ty_fn(s: &mut State,
|
2013-03-14 02:25:28 +00:00
|
|
|
opt_abis: Option<AbiSet>,
|
2013-02-01 01:12:29 +00:00
|
|
|
opt_sigil: Option<ast::Sigil>,
|
2013-07-05 12:33:52 +00:00
|
|
|
opt_region: &Option<ast::Lifetime>,
|
2014-01-09 13:05:33 +00:00
|
|
|
purity: ast::Purity,
|
2013-01-29 22:41:40 +00:00
|
|
|
onceness: ast::Onceness,
|
2014-01-09 13:05:33 +00:00
|
|
|
decl: &ast::FnDecl,
|
2013-09-02 00:50:59 +00:00
|
|
|
id: Option<ast::Ident>,
|
2013-06-24 18:32:05 +00:00
|
|
|
opt_bounds: &Option<OptVec<ast::TyParamBound>>,
|
2013-02-15 05:50:03 +00:00
|
|
|
generics: Option<&ast::Generics>,
|
2014-01-09 13:05:33 +00:00
|
|
|
opt_explicit_self: Option<ast::ExplicitSelf_>) {
|
2011-06-01 22:29:38 +00:00
|
|
|
ibox(s, indent_unit);
|
2012-11-05 04:41:00 +00:00
|
|
|
|
|
|
|
// Duplicates the logic in `print_fn_header_info()`. This is because that
|
2013-02-01 01:12:29 +00:00
|
|
|
// function prints the sigil in the wrong place. That should be fixed.
|
2013-10-28 22:22:49 +00:00
|
|
|
if opt_sigil == Some(ast::OwnedSigil) && onceness == ast::Once {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "proc");
|
2013-10-29 22:06:13 +00:00
|
|
|
} else if opt_sigil == Some(ast::BorrowedSigil) {
|
2013-10-28 22:22:49 +00:00
|
|
|
print_extern_opt_abis(s, opt_abis);
|
2013-10-29 22:06:13 +00:00
|
|
|
for lifetime in opt_region.iter() {
|
|
|
|
print_lifetime(s, lifetime);
|
|
|
|
}
|
|
|
|
print_purity(s, purity);
|
|
|
|
print_onceness(s, onceness);
|
|
|
|
} else {
|
|
|
|
print_opt_abis_and_extern_if_nondefault(s, opt_abis);
|
2013-10-28 22:22:49 +00:00
|
|
|
print_opt_sigil(s, opt_sigil);
|
|
|
|
print_opt_lifetime(s, opt_region);
|
|
|
|
print_purity(s, purity);
|
|
|
|
print_onceness(s, onceness);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "fn");
|
2013-10-28 22:22:49 +00:00
|
|
|
}
|
2013-10-29 22:06:13 +00:00
|
|
|
|
2013-12-27 22:11:01 +00:00
|
|
|
match id { Some(id) => { word(&mut s.s, " "); print_ident(s, id); } _ => () }
|
2013-10-29 22:06:13 +00:00
|
|
|
|
|
|
|
if opt_sigil != Some(ast::BorrowedSigil) {
|
2013-11-21 00:23:04 +00:00
|
|
|
opt_bounds.as_ref().map(|bounds| print_bounds(s, bounds, true));
|
2013-10-29 22:06:13 +00:00
|
|
|
}
|
|
|
|
|
2013-03-20 01:24:01 +00:00
|
|
|
match generics { Some(g) => print_generics(s, g), _ => () }
|
2013-12-27 22:11:01 +00:00
|
|
|
zerobreak(&mut s.s);
|
2012-08-17 23:14:57 +00:00
|
|
|
|
2013-10-29 22:06:13 +00:00
|
|
|
if opt_sigil == Some(ast::BorrowedSigil) {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "|");
|
2013-10-29 22:06:13 +00:00
|
|
|
} else {
|
|
|
|
popen(s);
|
|
|
|
}
|
|
|
|
|
2013-06-06 07:38:41 +00:00
|
|
|
// It is unfortunate to duplicate the commasep logic, but we want the
|
2013-02-01 01:12:29 +00:00
|
|
|
// self type and the args all in the same box.
|
2014-01-09 13:05:33 +00:00
|
|
|
rbox(s, 0u, Inconsistent);
|
2012-08-17 23:14:57 +00:00
|
|
|
let mut first = true;
|
2013-08-03 16:45:23 +00:00
|
|
|
for explicit_self in opt_explicit_self.iter() {
|
2013-04-30 15:49:48 +00:00
|
|
|
first = !print_explicit_self(s, *explicit_self);
|
2012-08-17 23:14:57 +00:00
|
|
|
}
|
2013-08-03 16:45:23 +00:00
|
|
|
for arg in decl.inputs.iter() {
|
2013-05-19 05:07:44 +00:00
|
|
|
if first { first = false; } else { word_space(s, ","); }
|
2013-07-06 04:57:11 +00:00
|
|
|
print_arg(s, arg);
|
2012-08-17 23:14:57 +00:00
|
|
|
}
|
|
|
|
end(s);
|
2013-10-29 22:06:13 +00:00
|
|
|
|
|
|
|
if opt_sigil == Some(ast::BorrowedSigil) {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "|");
|
2013-10-29 22:06:13 +00:00
|
|
|
|
|
|
|
opt_bounds.as_ref().map(|bounds| print_bounds(s, bounds, true));
|
|
|
|
} else {
|
2013-10-25 05:56:34 +00:00
|
|
|
if decl.variadic {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ", ...");
|
2013-10-25 05:56:34 +00:00
|
|
|
}
|
2013-10-29 22:06:13 +00:00
|
|
|
pclose(s);
|
|
|
|
}
|
2012-08-17 23:14:57 +00:00
|
|
|
|
2011-12-23 12:32:17 +00:00
|
|
|
maybe_print_comment(s, decl.output.span.lo);
|
2012-09-08 01:53:14 +00:00
|
|
|
|
|
|
|
match decl.output.node {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::TyNil => {}
|
2012-09-08 01:53:14 +00:00
|
|
|
_ => {
|
|
|
|
space_if_not_bol(s);
|
|
|
|
ibox(s, indent_unit);
|
2013-05-19 05:07:44 +00:00
|
|
|
word_space(s, "->");
|
2014-01-09 13:05:33 +00:00
|
|
|
if decl.cf == ast::NoReturn { word_nbsp(s, "!"); }
|
2013-11-30 22:00:39 +00:00
|
|
|
else { print_type(s, decl.output); }
|
2012-09-08 01:53:14 +00:00
|
|
|
end(s);
|
|
|
|
}
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
2012-09-08 01:53:14 +00:00
|
|
|
|
2011-06-01 22:29:38 +00:00
|
|
|
end(s);
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn maybe_print_trailing_comment(s: &mut State, span: codemap::Span,
|
2013-01-29 22:41:40 +00:00
|
|
|
next_pos: Option<BytePos>) {
|
2013-04-12 05:10:31 +00:00
|
|
|
let cm;
|
2012-08-20 19:23:37 +00:00
|
|
|
match s.cm { Some(ccm) => cm = ccm, _ => return }
|
2012-08-06 19:34:08 +00:00
|
|
|
match next_comment(s) {
|
2012-12-04 18:50:00 +00:00
|
|
|
Some(ref cmnt) => {
|
2014-01-09 13:05:33 +00:00
|
|
|
if (*cmnt).style != comments::Trailing { return; }
|
2012-11-13 02:24:56 +00:00
|
|
|
let span_line = cm.lookup_char_pos(span.hi);
|
2012-12-04 18:50:00 +00:00
|
|
|
let comment_line = cm.lookup_char_pos((*cmnt).pos);
|
2013-11-19 17:15:49 +00:00
|
|
|
let mut next = (*cmnt).pos + BytePos(1);
|
2012-08-20 19:23:37 +00:00
|
|
|
match next_pos { None => (), Some(p) => next = p }
|
2012-12-04 18:50:00 +00:00
|
|
|
if span.hi < (*cmnt).pos && (*cmnt).pos < next &&
|
2011-07-27 12:19:39 +00:00
|
|
|
span_line.line == comment_line.line {
|
2013-05-12 04:25:31 +00:00
|
|
|
print_comment(s, cmnt);
|
2013-02-04 22:02:01 +00:00
|
|
|
s.cur_cmnt_and_lit.cur_cmnt += 1u;
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => ()
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_remaining_comments(s: &mut State) {
|
2011-08-04 00:52:25 +00:00
|
|
|
// If there aren't any remaining comments, then we need to manually
|
|
|
|
// make sure there is a line break at the end.
|
2013-12-27 22:11:01 +00:00
|
|
|
if next_comment(s).is_none() { hardbreak(&mut s.s); }
|
2012-03-11 04:34:17 +00:00
|
|
|
loop {
|
2012-08-06 19:34:08 +00:00
|
|
|
match next_comment(s) {
|
2013-02-04 22:02:01 +00:00
|
|
|
Some(ref cmnt) => {
|
2013-05-12 04:25:31 +00:00
|
|
|
print_comment(s, cmnt);
|
2013-02-04 22:02:01 +00:00
|
|
|
s.cur_cmnt_and_lit.cur_cmnt += 1u;
|
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => break
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_literal(s: &mut State, lit: &ast::Lit) {
|
2011-07-05 09:48:19 +00:00
|
|
|
maybe_print_comment(s, lit.span.lo);
|
2012-08-06 19:34:08 +00:00
|
|
|
match next_lit(s, lit.span.lo) {
|
2012-12-04 18:50:00 +00:00
|
|
|
Some(ref ltrl) => {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, (*ltrl).lit);
|
2012-08-02 00:30:05 +00:00
|
|
|
return;
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => ()
|
2011-07-05 09:48:19 +00:00
|
|
|
}
|
2012-08-06 19:34:08 +00:00
|
|
|
match lit.node {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::LitStr(st, style) => print_string(s, st, style),
|
|
|
|
ast::LitChar(ch) => {
|
2013-06-28 21:04:13 +00:00
|
|
|
let mut res = ~"'";
|
2013-11-21 00:23:04 +00:00
|
|
|
char::from_u32(ch).unwrap().escape_default(|c| res.push_char(c));
|
2013-06-28 21:04:13 +00:00
|
|
|
res.push_char('\'');
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, res);
|
2011-12-07 20:06:12 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::LitInt(i, t) => {
|
2012-02-04 01:39:39 +00:00
|
|
|
if i < 0_i64 {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s,
|
2013-08-18 02:47:54 +00:00
|
|
|
~"-" + (-i as u64).to_str_radix(10u)
|
2012-02-04 01:39:39 +00:00
|
|
|
+ ast_util::int_ty_to_str(t));
|
|
|
|
} else {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s,
|
2013-08-18 02:47:54 +00:00
|
|
|
(i as u64).to_str_radix(10u)
|
2012-02-04 01:39:39 +00:00
|
|
|
+ ast_util::int_ty_to_str(t));
|
|
|
|
}
|
2011-12-07 20:06:12 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::LitUint(u, t) => {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s,
|
2013-08-18 02:47:54 +00:00
|
|
|
u.to_str_radix(10u)
|
2012-02-04 01:39:39 +00:00
|
|
|
+ ast_util::uint_ty_to_str(t));
|
2011-12-07 20:06:12 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::LitIntUnsuffixed(i) => {
|
2012-06-11 23:31:03 +00:00
|
|
|
if i < 0_i64 {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, ~"-" + (-i as u64).to_str_radix(10u));
|
2012-06-11 23:31:03 +00:00
|
|
|
} else {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, (i as u64).to_str_radix(10u));
|
2012-06-11 23:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::LitFloat(f, t) => {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, f.to_owned() + ast_util::float_ty_to_str(t));
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::LitFloatUnsuffixed(f) => word(&mut s.s, f),
|
|
|
|
ast::LitNil => word(&mut s.s, "()"),
|
|
|
|
ast::LitBool(val) => {
|
2013-12-27 22:11:01 +00:00
|
|
|
if val { word(&mut s.s, "true"); } else { word(&mut s.s, "false"); }
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::LitBinary(arr) => {
|
2013-10-14 15:24:17 +00:00
|
|
|
ibox(s, indent_unit);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "[");
|
2014-01-09 13:05:33 +00:00
|
|
|
commasep_cmnt(s, Inconsistent, arr, |s, u| word(&mut s.s, format!("{}", *u)),
|
2013-10-14 15:24:17 +00:00
|
|
|
|_| lit.span);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "]");
|
2013-10-14 15:24:17 +00:00
|
|
|
end(s);
|
|
|
|
}
|
2011-07-05 09:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn lit_to_str(l: &ast::Lit) -> ~str {
|
2012-07-18 23:18:02 +00:00
|
|
|
return to_str(l, print_literal, parse::token::mk_fake_ident_interner());
|
|
|
|
}
|
2011-07-05 09:48:19 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn next_lit(s: &mut State, pos: BytePos) -> Option<comments::Literal> {
|
2012-08-06 19:34:08 +00:00
|
|
|
match s.literals {
|
2012-12-04 18:50:00 +00:00
|
|
|
Some(ref lits) => {
|
2013-06-09 01:38:47 +00:00
|
|
|
while s.cur_cmnt_and_lit.cur_lit < lits.len() {
|
2013-07-02 19:47:32 +00:00
|
|
|
let ltrl = (*lits)[s.cur_cmnt_and_lit.cur_lit].clone();
|
2012-08-20 19:23:37 +00:00
|
|
|
if ltrl.pos > pos { return None; }
|
2013-02-04 22:02:01 +00:00
|
|
|
s.cur_cmnt_and_lit.cur_lit += 1u;
|
2012-08-20 19:23:37 +00:00
|
|
|
if ltrl.pos == pos { return Some(ltrl); }
|
2012-01-16 09:50:34 +00:00
|
|
|
}
|
2012-08-20 19:23:37 +00:00
|
|
|
return None;
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2012-08-20 19:23:37 +00:00
|
|
|
_ => return None
|
2011-07-05 09:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn maybe_print_comment(s: &mut State, pos: BytePos) {
|
2012-03-11 04:34:17 +00:00
|
|
|
loop {
|
2012-08-06 19:34:08 +00:00
|
|
|
match next_comment(s) {
|
2012-12-04 18:50:00 +00:00
|
|
|
Some(ref cmnt) => {
|
|
|
|
if (*cmnt).pos < pos {
|
2013-05-12 04:25:31 +00:00
|
|
|
print_comment(s, cmnt);
|
2013-02-04 22:02:01 +00:00
|
|
|
s.cur_cmnt_and_lit.cur_cmnt += 1u;
|
2011-07-27 12:19:39 +00:00
|
|
|
} else { break; }
|
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => break
|
2011-07-05 09:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_comment(s: &mut State, cmnt: &comments::Comment) {
|
2012-08-06 19:34:08 +00:00
|
|
|
match cmnt.style {
|
2014-01-09 13:05:33 +00:00
|
|
|
comments::Mixed => {
|
|
|
|
assert_eq!(cmnt.lines.len(), 1u);
|
|
|
|
zerobreak(&mut s.s);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, cmnt.lines[0]);
|
2014-01-09 13:05:33 +00:00
|
|
|
zerobreak(&mut s.s);
|
|
|
|
}
|
|
|
|
comments::Isolated => {
|
|
|
|
pprust::hardbreak_if_not_bol(s);
|
2013-08-03 16:45:23 +00:00
|
|
|
for line in cmnt.lines.iter() {
|
2014-01-09 13:05:33 +00:00
|
|
|
// Don't print empty lines because they will end up as trailing
|
|
|
|
// whitespace
|
2013-12-27 22:11:01 +00:00
|
|
|
if !line.is_empty() { word(&mut s.s, *line); }
|
|
|
|
hardbreak(&mut s.s);
|
2011-08-19 02:19:38 +00:00
|
|
|
}
|
2011-07-05 09:48:19 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
comments::Trailing => {
|
|
|
|
word(&mut s.s, " ");
|
|
|
|
if cmnt.lines.len() == 1u {
|
|
|
|
word(&mut s.s, cmnt.lines[0]);
|
|
|
|
hardbreak(&mut s.s);
|
|
|
|
} else {
|
|
|
|
ibox(s, 0u);
|
|
|
|
for line in cmnt.lines.iter() {
|
|
|
|
if !line.is_empty() { word(&mut s.s, *line); }
|
|
|
|
hardbreak(&mut s.s);
|
|
|
|
}
|
|
|
|
end(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
comments::BlankLine => {
|
|
|
|
// We need to do at least one, possibly two hardbreaks.
|
|
|
|
let is_semi = match s.s.last_token() {
|
|
|
|
pp::String(s, _) => ";" == s,
|
|
|
|
_ => false
|
2011-08-19 22:16:48 +00:00
|
|
|
};
|
2014-01-09 13:05:33 +00:00
|
|
|
if is_semi || is_begin(s) || is_end(s) { hardbreak(&mut s.s); }
|
|
|
|
hardbreak(&mut s.s);
|
|
|
|
}
|
2011-07-05 09:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_string(s: &mut State, st: &str, style: ast::StrStyle) {
|
2013-10-08 00:49:10 +00:00
|
|
|
let st = match style {
|
|
|
|
ast::CookedStr => format!("\"{}\"", st.escape_default()),
|
|
|
|
ast::RawStr(n) => format!("r{delim}\"{string}\"{delim}",
|
|
|
|
delim="#".repeat(n), string=st)
|
|
|
|
};
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, st);
|
2011-07-05 09:48:19 +00:00
|
|
|
}
|
|
|
|
|
2013-12-28 02:07:12 +00:00
|
|
|
// XXX(pcwalton): A nasty function to extract the string from an `io::Writer`
|
|
|
|
// that we "know" to be a `MemWriter` that works around the lack of checked
|
|
|
|
// downcasts.
|
|
|
|
unsafe fn get_mem_writer(writer: &mut ~io::Writer) -> ~str {
|
|
|
|
let (_, wr): (uint, ~MemWriter) = cast::transmute_copy(writer);
|
2014-01-08 04:05:33 +00:00
|
|
|
let result = str::from_utf8_owned(wr.get_ref().to_owned());
|
2013-12-28 02:07:12 +00:00
|
|
|
cast::forget(wr);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn to_str<T>(t: &T, f: |&mut State, &T|, intr: @IdentInterner) -> ~str {
|
2013-12-27 22:28:54 +00:00
|
|
|
let wr = ~MemWriter::new();
|
|
|
|
let mut s = rust_printer(wr as ~io::Writer, intr);
|
2013-12-27 22:11:01 +00:00
|
|
|
f(&mut s, t);
|
|
|
|
eof(&mut s.s);
|
2013-12-27 22:28:54 +00:00
|
|
|
unsafe {
|
2013-12-28 02:07:12 +00:00
|
|
|
get_mem_writer(&mut s.s.out)
|
2013-12-27 22:28:54 +00:00
|
|
|
}
|
2011-07-05 09:48:19 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn next_comment(s: &mut State) -> Option<comments::Comment> {
|
2012-08-06 19:34:08 +00:00
|
|
|
match s.comments {
|
2014-01-09 13:05:33 +00:00
|
|
|
Some(ref cmnts) => {
|
|
|
|
if s.cur_cmnt_and_lit.cur_cmnt < cmnts.len() {
|
|
|
|
Some(cmnts[s.cur_cmnt_and_lit.cur_cmnt].clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2013-07-02 19:47:32 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
_ => None
|
2011-07-05 09:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_opt_purity(s: &mut State, opt_purity: Option<ast::Purity>) {
|
2013-03-14 02:25:28 +00:00
|
|
|
match opt_purity {
|
2014-01-09 13:05:33 +00:00
|
|
|
Some(ast::ImpureFn) => { }
|
2013-03-14 02:25:28 +00:00
|
|
|
Some(purity) => {
|
|
|
|
word_nbsp(s, purity_to_str(purity));
|
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_opt_abis_and_extern_if_nondefault(s: &mut State,
|
2013-10-29 22:06:13 +00:00
|
|
|
opt_abis: Option<AbiSet>) {
|
|
|
|
match opt_abis {
|
|
|
|
Some(abis) if !abis.is_rust() => {
|
|
|
|
word_nbsp(s, "extern");
|
|
|
|
word_nbsp(s, abis.to_str());
|
|
|
|
}
|
|
|
|
Some(_) | None => {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_extern_opt_abis(s: &mut State, opt_abis: Option<AbiSet>) {
|
2013-03-14 02:25:28 +00:00
|
|
|
match opt_abis {
|
|
|
|
Some(abis) => {
|
2013-05-19 05:07:44 +00:00
|
|
|
word_nbsp(s, "extern");
|
2013-03-14 02:25:28 +00:00
|
|
|
word_nbsp(s, abis.to_str());
|
|
|
|
}
|
2013-02-01 01:12:29 +00:00
|
|
|
None => {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_opt_sigil(s: &mut State, opt_sigil: Option<ast::Sigil>) {
|
2013-02-01 01:12:29 +00:00
|
|
|
match opt_sigil {
|
2013-12-27 22:11:01 +00:00
|
|
|
Some(ast::BorrowedSigil) => { word(&mut s.s, "&"); }
|
|
|
|
Some(ast::OwnedSigil) => { word(&mut s.s, "~"); }
|
|
|
|
Some(ast::ManagedSigil) => { word(&mut s.s, "@"); }
|
2012-11-05 04:41:00 +00:00
|
|
|
None => {}
|
|
|
|
};
|
|
|
|
}
|
2012-11-02 20:33:51 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_fn_header_info(s: &mut State,
|
|
|
|
_opt_explicit_self: Option<ast::ExplicitSelf_>,
|
|
|
|
opt_purity: Option<ast::Purity>,
|
2013-03-14 02:25:28 +00:00
|
|
|
abis: AbiSet,
|
2013-01-29 22:41:40 +00:00
|
|
|
onceness: ast::Onceness,
|
2013-02-01 01:12:29 +00:00
|
|
|
opt_sigil: Option<ast::Sigil>,
|
2014-01-09 13:05:33 +00:00
|
|
|
vis: ast::Visibility) {
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, visibility_qualified(vis, ""));
|
2013-03-14 02:25:28 +00:00
|
|
|
|
|
|
|
if abis != AbiSet::Rust() {
|
2013-05-19 05:07:44 +00:00
|
|
|
word_nbsp(s, "extern");
|
2013-03-14 02:25:28 +00:00
|
|
|
word_nbsp(s, abis.to_str());
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
if opt_purity != Some(ast::ExternFn) {
|
2013-03-14 02:25:28 +00:00
|
|
|
print_opt_purity(s, opt_purity);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
print_opt_purity(s, opt_purity);
|
|
|
|
}
|
|
|
|
|
2012-11-05 04:41:00 +00:00
|
|
|
print_onceness(s, onceness);
|
2013-12-27 22:11:01 +00:00
|
|
|
word(&mut s.s, "fn");
|
2013-02-01 01:12:29 +00:00
|
|
|
print_opt_sigil(s, opt_sigil);
|
2012-08-02 23:01:38 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn purity_to_str(p: ast::Purity) -> &'static str {
|
2012-08-06 19:34:08 +00:00
|
|
|
match p {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ImpureFn => "impure",
|
|
|
|
ast::UnsafeFn => "unsafe",
|
|
|
|
ast::ExternFn => "extern"
|
2012-05-25 06:44:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 22:10:23 +00:00
|
|
|
pub fn onceness_to_str(o: ast::Onceness) -> &'static str {
|
2012-11-02 20:33:51 +00:00
|
|
|
match o {
|
2013-06-21 22:10:23 +00:00
|
|
|
ast::Once => "once",
|
|
|
|
ast::Many => "many"
|
2012-11-02 20:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_purity(s: &mut State, p: ast::Purity) {
|
2012-08-06 19:34:08 +00:00
|
|
|
match p {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ImpureFn => (),
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => word_nbsp(s, purity_to_str(p))
|
2012-05-25 06:44:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn print_onceness(s: &mut State, o: ast::Onceness) {
|
2012-11-05 04:41:00 +00:00
|
|
|
match o {
|
2013-05-19 05:07:44 +00:00
|
|
|
ast::Once => { word_nbsp(s, "once"); }
|
2012-11-05 04:41:00 +00:00
|
|
|
ast::Many => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-24 22:10:38 +00:00
|
|
|
#[cfg(test)]
|
2013-04-15 15:08:52 +00:00
|
|
|
mod test {
|
2013-02-25 19:11:21 +00:00
|
|
|
use super::*;
|
|
|
|
|
2013-01-24 22:10:38 +00:00
|
|
|
use ast;
|
|
|
|
use ast_util;
|
2013-02-25 19:11:21 +00:00
|
|
|
use codemap;
|
2013-06-04 19:34:25 +00:00
|
|
|
use parse::token;
|
2013-01-24 22:10:38 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fun_to_str() {
|
2013-06-04 19:34:25 +00:00
|
|
|
let abba_ident = token::str_to_ident("abba");
|
2013-01-24 22:10:38 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
let decl = ast::FnDecl {
|
2013-01-24 22:10:38 +00:00
|
|
|
inputs: ~[],
|
2013-11-30 22:00:39 +00:00
|
|
|
output: ast::P(ast::Ty {id: 0,
|
2014-01-09 13:05:33 +00:00
|
|
|
node: ast::TyNil,
|
2014-01-01 06:53:22 +00:00
|
|
|
span: codemap::DUMMY_SP}),
|
2014-01-09 13:05:33 +00:00
|
|
|
cf: ast::Return,
|
2013-10-25 05:56:34 +00:00
|
|
|
variadic: false
|
2013-01-24 22:10:38 +00:00
|
|
|
};
|
2013-02-15 05:50:03 +00:00
|
|
|
let generics = ast_util::empty_generics();
|
2014-01-09 13:05:33 +00:00
|
|
|
assert_eq!(&fun_to_str(&decl, ast::ImpureFn, abba_ident,
|
2013-06-04 19:34:25 +00:00
|
|
|
None, &generics, token::get_ident_interner()),
|
2013-03-25 06:02:42 +00:00
|
|
|
&~"fn abba()");
|
2013-01-24 22:10:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_variant_to_str() {
|
2013-06-04 19:34:25 +00:00
|
|
|
let ident = token::str_to_ident("principal_skinner");
|
2013-01-24 22:10:38 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
let var = codemap::respan(codemap::DUMMY_SP, ast::Variant_ {
|
2013-01-24 22:10:38 +00:00
|
|
|
name: ident,
|
|
|
|
attrs: ~[],
|
|
|
|
// making this up as I go.... ?
|
2014-01-09 13:05:33 +00:00
|
|
|
kind: ast::TupleVariantKind(~[]),
|
2013-01-24 22:10:38 +00:00
|
|
|
id: 0,
|
|
|
|
disr_expr: None,
|
2014-01-09 13:05:33 +00:00
|
|
|
vis: ast::Public,
|
2013-01-24 22:10:38 +00:00
|
|
|
});
|
|
|
|
|
2013-06-04 19:34:25 +00:00
|
|
|
let varstr = variant_to_str(&var,token::get_ident_interner());
|
2013-03-13 22:30:37 +00:00
|
|
|
assert_eq!(&varstr,&~"pub principal_skinner");
|
2013-01-24 22:10:38 +00:00
|
|
|
}
|
|
|
|
}
|