2014-07-27 11:50:46 +00:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-04 00:48:01 +00:00
|
|
|
// 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.
|
|
|
|
|
2014-11-06 08:05:53 +00:00
|
|
|
pub use self::AnnNode::*;
|
|
|
|
|
2016-02-05 12:13:36 +00:00
|
|
|
use abi::{self, Abi};
|
2016-02-08 11:44:45 +00:00
|
|
|
use ast::{self, TokenTree, BlockCheckMode};
|
2015-03-05 02:48:54 +00:00
|
|
|
use ast::{RegionTyParamBound, TraitTyParamBound, TraitBoundModifier};
|
2015-11-15 16:17:50 +00:00
|
|
|
use ast::Attribute;
|
|
|
|
use attr::ThinAttributesExt;
|
2015-10-15 12:51:30 +00:00
|
|
|
use util::parser::AssocOp;
|
2015-02-06 01:04:11 +00:00
|
|
|
use attr;
|
2013-07-19 11:51:37 +00:00
|
|
|
use attr::{AttrMetaMethods, AttributeMethods};
|
2015-01-04 03:42:21 +00:00
|
|
|
use codemap::{self, CodeMap, BytePos};
|
2015-12-13 22:17:55 +00:00
|
|
|
use errors;
|
2015-02-06 01:04:11 +00:00
|
|
|
use parse::token::{self, BinOpToken, Token, InternedString};
|
2014-05-21 23:57:31 +00:00
|
|
|
use parse::lexer::comments;
|
2012-12-23 22:41:37 +00:00
|
|
|
use parse;
|
2015-01-04 03:42:21 +00:00
|
|
|
use print::pp::{self, break_offset, word, space, zerobreak, hardbreak};
|
2015-01-14 05:12:39 +00:00
|
|
|
use print::pp::{Breaks, eof};
|
|
|
|
use print::pp::Breaks::{Consistent, Inconsistent};
|
2014-09-13 16:06:01 +00:00
|
|
|
use ptr::P;
|
2015-02-06 01:04:11 +00:00
|
|
|
use std_inject;
|
2012-12-23 22:41:37 +00:00
|
|
|
|
2015-04-17 02:54:05 +00:00
|
|
|
use std::ascii;
|
2015-02-27 05:00:43 +00:00
|
|
|
use std::io::{self, Write, Read};
|
2014-12-11 03:46:38 +00:00
|
|
|
use std::iter;
|
2013-05-25 02:35:29 +00:00
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
pub enum AnnNode<'a> {
|
2014-08-11 12:01:37 +00:00
|
|
|
NodeIdent(&'a ast::Ident),
|
|
|
|
NodeName(&'a ast::Name),
|
2014-03-16 18:58:11 +00:00
|
|
|
NodeBlock(&'a ast::Block),
|
|
|
|
NodeItem(&'a ast::Item),
|
2015-03-24 01:52:55 +00:00
|
|
|
NodeSubItem(ast::NodeId),
|
2014-03-16 18:58:11 +00:00
|
|
|
NodeExpr(&'a ast::Expr),
|
|
|
|
NodePat(&'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 {
|
2015-02-27 05:00:43 +00:00
|
|
|
fn pre(&self, _state: &mut State, _node: AnnNode) -> io::Result<()> { Ok(()) }
|
|
|
|
fn post(&self, _state: &mut State, _node: AnnNode) -> io::Result<()> { Ok(()) }
|
2013-08-29 22:24:33 +00:00
|
|
|
}
|
|
|
|
|
2015-03-30 13:38:59 +00:00
|
|
|
#[derive(Copy, Clone)]
|
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
|
|
|
|
2015-03-30 13:38:59 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2013-02-04 22:02:01 +00:00
|
|
|
pub struct CurrentCommentAndLiteral {
|
2015-09-14 09:58:20 +00:00
|
|
|
pub cur_cmnt: usize,
|
|
|
|
pub cur_lit: usize,
|
2013-02-04 22:02:01 +00:00
|
|
|
}
|
|
|
|
|
2014-03-18 05:27:37 +00:00
|
|
|
pub struct State<'a> {
|
2015-02-27 05:00:43 +00:00
|
|
|
pub s: pp::Printer<'a>,
|
2014-03-16 18:58:11 +00:00
|
|
|
cm: Option<&'a CodeMap>,
|
2014-02-28 21:09:09 +00:00
|
|
|
comments: Option<Vec<comments::Comment> >,
|
|
|
|
literals: Option<Vec<comments::Literal> >,
|
2013-12-27 23:42:43 +00:00
|
|
|
cur_cmnt_and_lit: CurrentCommentAndLiteral,
|
2014-03-27 17:31:00 +00:00
|
|
|
boxes: Vec<pp::Breaks>,
|
2014-11-20 20:08:02 +00:00
|
|
|
ann: &'a (PpAnn+'a),
|
2013-02-04 22:02:01 +00:00
|
|
|
}
|
2011-07-05 09:48:19 +00:00
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn rust_printer<'a>(writer: Box<Write+'a>) -> State<'a> {
|
2014-03-16 18:58:11 +00:00
|
|
|
static NO_ANN: NoAnn = NoAnn;
|
|
|
|
rust_printer_annotated(writer, &NO_ANN)
|
2012-05-10 14:24:56 +00:00
|
|
|
}
|
2011-07-05 09:48:19 +00:00
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn rust_printer_annotated<'a>(writer: Box<Write+'a>,
|
2014-03-18 05:27:37 +00:00
|
|
|
ann: &'a PpAnn) -> State<'a> {
|
2014-02-06 22:38:33 +00:00
|
|
|
State {
|
2015-11-03 16:39:51 +00:00
|
|
|
s: pp::mk_printer(writer, DEFAULT_COLUMNS),
|
2014-01-09 13:05:33 +00:00
|
|
|
cm: None,
|
|
|
|
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
|
|
|
|
},
|
2014-03-27 17:31:00 +00:00
|
|
|
boxes: Vec::new(),
|
2014-08-01 15:11:53 +00:00
|
|
|
ann: ann,
|
2014-02-06 22:38:33 +00:00
|
|
|
}
|
2011-07-05 09:48:19 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 16:39:51 +00:00
|
|
|
pub const INDENT_UNIT: usize = 4;
|
2011-07-05 09:48:19 +00:00
|
|
|
|
2015-11-03 16:39:51 +00:00
|
|
|
pub const DEFAULT_COLUMNS: usize = 78;
|
2011-07-05 09:48:19 +00:00
|
|
|
|
2014-06-09 20:12:30 +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.
|
2014-03-18 05:27:37 +00:00
|
|
|
pub fn print_crate<'a>(cm: &'a CodeMap,
|
2015-12-13 22:17:55 +00:00
|
|
|
span_diagnostic: &errors::Handler,
|
2014-03-18 05:27:37 +00:00
|
|
|
krate: &ast::Crate,
|
2014-05-22 23:57:53 +00:00
|
|
|
filename: String,
|
2015-02-27 05:00:43 +00:00
|
|
|
input: &mut Read,
|
|
|
|
out: Box<Write+'a>,
|
2014-03-18 05:27:37 +00:00
|
|
|
ann: &'a PpAnn,
|
2015-02-27 05:00:43 +00:00
|
|
|
is_expanded: bool) -> io::Result<()> {
|
2014-08-07 09:25:31 +00:00
|
|
|
let mut s = State::new_from_input(cm,
|
|
|
|
span_diagnostic,
|
|
|
|
filename,
|
|
|
|
input,
|
|
|
|
out,
|
|
|
|
ann,
|
|
|
|
is_expanded);
|
2015-07-30 00:01:14 +00:00
|
|
|
if is_expanded && !std_inject::no_std(krate) {
|
2015-02-06 01:04:11 +00:00
|
|
|
// We need to print `#![no_std]` (and its feature gate) so that
|
|
|
|
// compiling pretty-printed source won't inject libstd again.
|
|
|
|
// However we don't want these attributes in the AST because
|
|
|
|
// of the feature gate, so we fake them up here.
|
|
|
|
|
2015-12-03 01:31:49 +00:00
|
|
|
// #![feature(prelude_import)]
|
2015-07-01 03:05:17 +00:00
|
|
|
let prelude_import_meta = attr::mk_word_item(InternedString::new("prelude_import"));
|
2015-12-03 01:31:49 +00:00
|
|
|
let list = attr::mk_list_item(InternedString::new("feature"),
|
|
|
|
vec![prelude_import_meta]);
|
|
|
|
let fake_attr = attr::mk_attr_inner(attr::mk_attr_id(), list);
|
2015-02-06 01:04:11 +00:00
|
|
|
try!(s.print_attribute(&fake_attr));
|
|
|
|
|
|
|
|
// #![no_std]
|
2015-12-03 01:31:49 +00:00
|
|
|
let no_std_meta = attr::mk_word_item(InternedString::new("no_std"));
|
2015-02-06 01:04:11 +00:00
|
|
|
let fake_attr = attr::mk_attr_inner(attr::mk_attr_id(), no_std_meta);
|
|
|
|
try!(s.print_attribute(&fake_attr));
|
|
|
|
}
|
|
|
|
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(s.print_mod(&krate.module, &krate.attrs));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(s.print_remaining_comments());
|
|
|
|
eof(&mut s.s)
|
2012-02-21 23:34:26 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 09:25:31 +00:00
|
|
|
impl<'a> State<'a> {
|
|
|
|
pub fn new_from_input(cm: &'a CodeMap,
|
2015-12-13 22:17:55 +00:00
|
|
|
span_diagnostic: &errors::Handler,
|
2014-08-07 09:25:31 +00:00
|
|
|
filename: String,
|
2015-02-27 05:00:43 +00:00
|
|
|
input: &mut Read,
|
|
|
|
out: Box<Write+'a>,
|
2014-08-07 09:25:31 +00:00
|
|
|
ann: &'a PpAnn,
|
|
|
|
is_expanded: bool) -> State<'a> {
|
|
|
|
let (cmnts, lits) = comments::gather_comments_and_literals(
|
|
|
|
span_diagnostic,
|
|
|
|
filename,
|
|
|
|
input);
|
|
|
|
|
|
|
|
State::new(
|
|
|
|
cm,
|
|
|
|
out,
|
|
|
|
ann,
|
|
|
|
Some(cmnts),
|
|
|
|
// 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.
|
|
|
|
if is_expanded { None } else { Some(lits) })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(cm: &'a CodeMap,
|
2015-02-27 05:00:43 +00:00
|
|
|
out: Box<Write+'a>,
|
2014-08-07 09:25:31 +00:00
|
|
|
ann: &'a PpAnn,
|
|
|
|
comments: Option<Vec<comments::Comment>>,
|
|
|
|
literals: Option<Vec<comments::Literal>>) -> State<'a> {
|
|
|
|
State {
|
2015-11-03 16:39:51 +00:00
|
|
|
s: pp::mk_printer(out, DEFAULT_COLUMNS),
|
2014-08-07 09:25:31 +00:00
|
|
|
cm: Some(cm),
|
|
|
|
comments: comments,
|
|
|
|
literals: literals,
|
|
|
|
cur_cmnt_and_lit: CurrentCommentAndLiteral {
|
|
|
|
cur_cmnt: 0,
|
|
|
|
cur_lit: 0
|
|
|
|
},
|
|
|
|
boxes: Vec::new(),
|
2014-08-01 15:11:53 +00:00
|
|
|
ann: ann,
|
2014-08-07 09:25:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 18:28:32 +00:00
|
|
|
pub fn to_string<F>(f: F) -> String where
|
2015-02-27 05:00:43 +00:00
|
|
|
F: FnOnce(&mut State) -> io::Result<()>,
|
2014-12-08 18:28:32 +00:00
|
|
|
{
|
2015-04-17 02:54:05 +00:00
|
|
|
let mut wr = Vec::new();
|
|
|
|
{
|
|
|
|
let mut printer = rust_printer(Box::new(&mut wr));
|
|
|
|
f(&mut printer).unwrap();
|
|
|
|
eof(&mut printer.s).unwrap();
|
|
|
|
}
|
|
|
|
String::from_utf8(wr).unwrap()
|
2014-06-21 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 00:05:28 +00:00
|
|
|
pub fn binop_to_string(op: BinOpToken) -> &'static str {
|
|
|
|
match op {
|
|
|
|
token::Plus => "+",
|
|
|
|
token::Minus => "-",
|
|
|
|
token::Star => "*",
|
|
|
|
token::Slash => "/",
|
|
|
|
token::Percent => "%",
|
|
|
|
token::Caret => "^",
|
|
|
|
token::And => "&",
|
|
|
|
token::Or => "|",
|
|
|
|
token::Shl => "<<",
|
|
|
|
token::Shr => ">>",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn token_to_string(tok: &Token) -> String {
|
|
|
|
match *tok {
|
2014-12-11 03:46:38 +00:00
|
|
|
token::Eq => "=".to_string(),
|
|
|
|
token::Lt => "<".to_string(),
|
|
|
|
token::Le => "<=".to_string(),
|
|
|
|
token::EqEq => "==".to_string(),
|
|
|
|
token::Ne => "!=".to_string(),
|
|
|
|
token::Ge => ">=".to_string(),
|
|
|
|
token::Gt => ">".to_string(),
|
|
|
|
token::Not => "!".to_string(),
|
|
|
|
token::Tilde => "~".to_string(),
|
|
|
|
token::OrOr => "||".to_string(),
|
|
|
|
token::AndAnd => "&&".to_string(),
|
|
|
|
token::BinOp(op) => binop_to_string(op).to_string(),
|
2014-10-28 00:05:28 +00:00
|
|
|
token::BinOpEq(op) => format!("{}=", binop_to_string(op)),
|
|
|
|
|
|
|
|
/* Structural symbols */
|
2014-12-11 03:46:38 +00:00
|
|
|
token::At => "@".to_string(),
|
|
|
|
token::Dot => ".".to_string(),
|
|
|
|
token::DotDot => "..".to_string(),
|
|
|
|
token::DotDotDot => "...".to_string(),
|
|
|
|
token::Comma => ",".to_string(),
|
|
|
|
token::Semi => ";".to_string(),
|
|
|
|
token::Colon => ":".to_string(),
|
|
|
|
token::ModSep => "::".to_string(),
|
|
|
|
token::RArrow => "->".to_string(),
|
|
|
|
token::LArrow => "<-".to_string(),
|
|
|
|
token::FatArrow => "=>".to_string(),
|
|
|
|
token::OpenDelim(token::Paren) => "(".to_string(),
|
|
|
|
token::CloseDelim(token::Paren) => ")".to_string(),
|
|
|
|
token::OpenDelim(token::Bracket) => "[".to_string(),
|
|
|
|
token::CloseDelim(token::Bracket) => "]".to_string(),
|
|
|
|
token::OpenDelim(token::Brace) => "{".to_string(),
|
|
|
|
token::CloseDelim(token::Brace) => "}".to_string(),
|
|
|
|
token::Pound => "#".to_string(),
|
|
|
|
token::Dollar => "$".to_string(),
|
|
|
|
token::Question => "?".to_string(),
|
2014-10-28 00:05:28 +00:00
|
|
|
|
|
|
|
/* Literals */
|
2014-11-19 04:48:38 +00:00
|
|
|
token::Literal(lit, suf) => {
|
|
|
|
let mut out = match lit {
|
2015-07-28 16:07:20 +00:00
|
|
|
token::Byte(b) => format!("b'{}'", b),
|
|
|
|
token::Char(c) => format!("'{}'", c),
|
|
|
|
token::Float(c) => c.to_string(),
|
|
|
|
token::Integer(c) => c.to_string(),
|
|
|
|
token::Str_(s) => format!("\"{}\"", s),
|
2014-11-19 04:48:38 +00:00
|
|
|
token::StrRaw(s, n) => format!("r{delim}\"{string}\"{delim}",
|
2014-12-11 03:46:38 +00:00
|
|
|
delim=repeat("#", n),
|
2015-07-28 16:07:20 +00:00
|
|
|
string=s),
|
2015-09-03 07:54:53 +00:00
|
|
|
token::ByteStr(v) => format!("b\"{}\"", v),
|
|
|
|
token::ByteStrRaw(s, n) => format!("br{delim}\"{string}\"{delim}",
|
2014-12-11 03:46:38 +00:00
|
|
|
delim=repeat("#", n),
|
2015-07-28 16:07:20 +00:00
|
|
|
string=s),
|
2014-11-19 04:48:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(s) = suf {
|
2015-07-28 16:07:20 +00:00
|
|
|
out.push_str(&s.as_str())
|
2014-11-19 04:48:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out
|
|
|
|
}
|
2014-10-28 00:05:28 +00:00
|
|
|
|
|
|
|
/* Name components */
|
2015-07-28 16:07:20 +00:00
|
|
|
token::Ident(s, _) => s.to_string(),
|
|
|
|
token::Lifetime(s) => s.to_string(),
|
2014-12-11 03:46:38 +00:00
|
|
|
token::Underscore => "_".to_string(),
|
2014-10-28 00:05:28 +00:00
|
|
|
|
|
|
|
/* Other */
|
2015-07-28 16:07:20 +00:00
|
|
|
token::DocComment(s) => s.to_string(),
|
2014-10-06 22:00:56 +00:00
|
|
|
token::SubstNt(s, _) => format!("${}", s),
|
|
|
|
token::MatchNt(s, t, _, _) => format!("${}:{}", s, t),
|
2014-12-11 03:46:38 +00:00
|
|
|
token::Eof => "<eof>".to_string(),
|
|
|
|
token::Whitespace => " ".to_string(),
|
|
|
|
token::Comment => "/* */".to_string(),
|
2015-07-28 16:07:20 +00:00
|
|
|
token::Shebang(s) => format!("/* shebang: {}*/", s),
|
2014-10-28 00:05:28 +00:00
|
|
|
|
2014-09-16 01:27:28 +00:00
|
|
|
token::SpecialVarNt(var) => format!("${}", var.as_str()),
|
|
|
|
|
2014-10-28 00:05:28 +00:00
|
|
|
token::Interpolated(ref nt) => match *nt {
|
2015-05-02 17:55:41 +00:00
|
|
|
token::NtExpr(ref e) => expr_to_string(&**e),
|
|
|
|
token::NtMeta(ref e) => meta_item_to_string(&**e),
|
|
|
|
token::NtTy(ref e) => ty_to_string(&**e),
|
|
|
|
token::NtPath(ref e) => path_to_string(&**e),
|
|
|
|
token::NtItem(ref e) => item_to_string(&**e),
|
|
|
|
token::NtBlock(ref e) => block_to_string(&**e),
|
|
|
|
token::NtStmt(ref e) => stmt_to_string(&**e),
|
|
|
|
token::NtPat(ref e) => pat_to_string(&**e),
|
2015-12-15 04:31:54 +00:00
|
|
|
token::NtIdent(ref e, _) => ident_to_string(e.node),
|
2015-05-02 17:55:41 +00:00
|
|
|
token::NtTT(ref e) => tt_to_string(&**e),
|
|
|
|
token::NtArm(ref e) => arm_to_string(&*e),
|
|
|
|
token::NtImplItem(ref e) => impl_item_to_string(&**e),
|
|
|
|
token::NtTraitItem(ref e) => trait_item_to_string(&**e),
|
|
|
|
token::NtGenerics(ref e) => generics_to_string(&*e),
|
|
|
|
token::NtWhereClause(ref e) => where_clause_to_string(&*e),
|
2015-11-11 20:19:01 +00:00
|
|
|
token::NtArg(ref e) => arg_to_string(&*e),
|
2014-10-28 00:05:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 10:39:03 +00:00
|
|
|
pub fn ty_to_string(ty: &ast::Ty) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_type(ty))
|
2014-06-21 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 20:05:29 +00:00
|
|
|
pub fn bounds_to_string(bounds: &[ast::TyParamBound]) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_bounds("", bounds))
|
2014-11-20 20:05:29 +00:00
|
|
|
}
|
|
|
|
|
2014-06-21 10:39:03 +00:00
|
|
|
pub fn pat_to_string(pat: &ast::Pat) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_pat(pat))
|
2014-06-21 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
2014-07-28 01:05:07 +00:00
|
|
|
pub fn arm_to_string(arm: &ast::Arm) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_arm(arm))
|
2014-07-28 01:05:07 +00:00
|
|
|
}
|
|
|
|
|
2014-06-21 10:39:03 +00:00
|
|
|
pub fn expr_to_string(e: &ast::Expr) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_expr(e))
|
2014-06-21 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lifetime_to_string(e: &ast::Lifetime) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_lifetime(e))
|
2014-06-21 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tt_to_string(tt: &ast::TokenTree) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_tt(tt))
|
2014-06-21 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tts_to_string(tts: &[ast::TokenTree]) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_tts(tts))
|
2014-06-21 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stmt_to_string(stmt: &ast::Stmt) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_stmt(stmt))
|
2014-06-21 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 19:18:37 +00:00
|
|
|
pub fn attr_to_string(attr: &ast::Attribute) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_attribute(attr))
|
2015-04-13 17:22:15 +00:00
|
|
|
}
|
|
|
|
|
2014-06-21 10:39:03 +00:00
|
|
|
pub fn item_to_string(i: &ast::Item) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_item(i))
|
2014-06-21 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 17:01:15 +00:00
|
|
|
pub fn impl_item_to_string(i: &ast::ImplItem) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_impl_item(i))
|
2015-03-19 17:01:15 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 17:01:46 +00:00
|
|
|
pub fn trait_item_to_string(i: &ast::TraitItem) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_trait_item(i))
|
2015-03-19 17:01:46 +00:00
|
|
|
}
|
|
|
|
|
2014-06-21 10:39:03 +00:00
|
|
|
pub fn generics_to_string(generics: &ast::Generics) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_generics(generics))
|
2014-06-21 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 04:54:29 +00:00
|
|
|
pub fn where_clause_to_string(i: &ast::WhereClause) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_where_clause(i))
|
2015-03-14 04:54:29 +00:00
|
|
|
}
|
|
|
|
|
2014-06-21 10:39:03 +00:00
|
|
|
pub fn fn_block_to_string(p: &ast::FnDecl) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_fn_block_args(p))
|
2014-06-21 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn path_to_string(p: &ast::Path) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_path(p, false, 0))
|
2014-06-21 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 20:05:02 +00:00
|
|
|
pub fn ident_to_string(id: ast::Ident) -> String {
|
|
|
|
to_string(|s| s.print_ident(id))
|
2014-06-21 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 20:05:07 +00:00
|
|
|
pub fn fun_to_string(decl: &ast::FnDecl,
|
|
|
|
unsafety: ast::Unsafety,
|
|
|
|
constness: ast::Constness,
|
|
|
|
name: ast::Ident,
|
2016-02-08 14:43:56 +00:00
|
|
|
opt_explicit_self: Option<&ast::SelfKind>,
|
2015-02-25 20:05:07 +00:00
|
|
|
generics: &ast::Generics)
|
|
|
|
-> String {
|
2015-05-05 12:47:04 +00:00
|
|
|
to_string(|s| {
|
2015-03-11 06:38:27 +00:00
|
|
|
try!(s.head(""));
|
2016-02-05 12:13:36 +00:00
|
|
|
try!(s.print_fn(decl, unsafety, constness, Abi::Rust, Some(name),
|
2015-03-11 06:38:27 +00:00
|
|
|
generics, opt_explicit_self, ast::Inherited));
|
2014-06-21 10:39:03 +00:00
|
|
|
try!(s.end()); // Close the head box
|
|
|
|
s.end() // Close the outer box
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn block_to_string(blk: &ast::Block) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| {
|
2014-06-21 10:39:03 +00:00
|
|
|
// containing cbox, will be closed by print-block at }
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(s.cbox(INDENT_UNIT));
|
2014-06-21 10:39:03 +00:00
|
|
|
// head-ibox, will be closed by print-block after {
|
2015-01-28 01:01:48 +00:00
|
|
|
try!(s.ibox(0));
|
2014-06-21 10:39:03 +00:00
|
|
|
s.print_block(blk)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn meta_item_to_string(mi: &ast::MetaItem) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_meta_item(mi))
|
2014-06-21 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn attribute_to_string(attr: &ast::Attribute) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_attribute(attr))
|
2014-06-21 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lit_to_string(l: &ast::Lit) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_literal(l))
|
2014-06-21 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 14:43:56 +00:00
|
|
|
pub fn explicit_self_to_string(explicit_self: &ast::SelfKind) -> String {
|
2016-02-09 16:44:47 +00:00
|
|
|
to_string(|s| s.print_explicit_self(explicit_self, ast::Mutability::Immutable).map(|_| {}))
|
2014-06-21 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn variant_to_string(var: &ast::Variant) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_variant(var))
|
2014-06-21 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn arg_to_string(arg: &ast::Arg) -> String {
|
2016-01-21 18:44:36 +00:00
|
|
|
to_string(|s| s.print_arg(arg, false))
|
2014-06-21 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
2014-07-12 04:22:11 +00:00
|
|
|
pub fn mac_to_string(arg: &ast::Mac) -> String {
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
to_string(|s| s.print_mac(arg, ::parse::token::Paren))
|
2014-07-12 04:22:11 +00:00
|
|
|
}
|
|
|
|
|
2014-05-22 23:57:53 +00:00
|
|
|
pub fn visibility_qualified(vis: ast::Visibility, s: &str) -> String {
|
2014-03-16 18:58:11 +00:00
|
|
|
match vis {
|
2014-06-26 06:15:14 +00:00
|
|
|
ast::Public => format!("pub {}", s),
|
2014-05-25 10:17:19 +00:00
|
|
|
ast::Inherited => s.to_string()
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2014-01-30 01:39:21 +00:00
|
|
|
}
|
2011-05-29 02:16:18 +00:00
|
|
|
|
2014-04-16 20:33:58 +00:00
|
|
|
fn needs_parentheses(expr: &ast::Expr) -> bool {
|
|
|
|
match expr.node {
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Assign(..) | ast::ExprKind::Binary(..) |
|
|
|
|
ast::ExprKind::Closure(..) |
|
|
|
|
ast::ExprKind::AssignOp(..) | ast::ExprKind::Cast(..) |
|
|
|
|
ast::ExprKind::InPlace(..) | ast::ExprKind::Type(..) => true,
|
2014-04-16 20:33:58 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 09:58:20 +00:00
|
|
|
pub trait PrintState<'a> {
|
|
|
|
fn writer(&mut self) -> &mut pp::Printer<'a>;
|
|
|
|
fn boxes(&mut self) -> &mut Vec<pp::Breaks>;
|
|
|
|
fn comments(&mut self) -> &mut Option<Vec<comments::Comment>>;
|
|
|
|
fn cur_cmnt_and_lit(&mut self) -> &mut CurrentCommentAndLiteral;
|
|
|
|
fn literals(&self) -> &Option<Vec<comments::Literal>>;
|
|
|
|
|
|
|
|
fn word_space(&mut self, w: &str) -> io::Result<()> {
|
|
|
|
try!(word(self.writer(), w));
|
|
|
|
space(self.writer())
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2011-05-29 02:16:18 +00:00
|
|
|
|
2015-09-14 09:58:20 +00:00
|
|
|
fn popen(&mut self) -> io::Result<()> { word(self.writer(), "(") }
|
|
|
|
|
|
|
|
fn pclose(&mut self) -> io::Result<()> { word(self.writer(), ")") }
|
|
|
|
|
|
|
|
fn is_begin(&mut self) -> bool {
|
|
|
|
match self.writer().last_token() {
|
|
|
|
pp::Token::Begin(_) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2011-05-29 02:16:18 +00:00
|
|
|
|
2015-09-14 09:58:20 +00:00
|
|
|
fn is_end(&mut self) -> bool {
|
|
|
|
match self.writer().last_token() {
|
|
|
|
pp::Token::End => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// is this the beginning of a line?
|
|
|
|
fn is_bol(&mut self) -> bool {
|
|
|
|
self.writer().last_token().is_eof() || self.writer().last_token().is_hardbreak_tok()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hardbreak_if_not_bol(&mut self) -> io::Result<()> {
|
|
|
|
if !self.is_bol() {
|
|
|
|
try!(hardbreak(self.writer()))
|
|
|
|
}
|
|
|
|
Ok(())
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2011-05-29 02:16:18 +00:00
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
// "raw box"
|
2015-09-14 09:58:20 +00:00
|
|
|
fn rbox(&mut self, u: usize, b: pp::Breaks) -> io::Result<()> {
|
|
|
|
self.boxes().push(b);
|
|
|
|
pp::rbox(self.writer(), u, b)
|
2012-11-05 04:41:00 +00:00
|
|
|
}
|
2011-05-29 02:16:18 +00:00
|
|
|
|
2015-09-14 09:58:20 +00:00
|
|
|
fn ibox(&mut self, u: usize) -> io::Result<()> {
|
|
|
|
self.boxes().push(pp::Breaks::Inconsistent);
|
|
|
|
pp::ibox(self.writer(), u)
|
|
|
|
}
|
2011-05-29 02:16:18 +00:00
|
|
|
|
2015-09-14 09:58:20 +00:00
|
|
|
fn end(&mut self) -> io::Result<()> {
|
|
|
|
self.boxes().pop().unwrap();
|
|
|
|
pp::end(self.writer())
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2011-05-29 02:16:18 +00:00
|
|
|
|
2015-09-14 09:58:20 +00:00
|
|
|
fn commasep<T, F>(&mut self, b: Breaks, elts: &[T], mut op: F) -> io::Result<()>
|
|
|
|
where F: FnMut(&mut Self, &T) -> io::Result<()>,
|
|
|
|
{
|
|
|
|
try!(self.rbox(0, b));
|
|
|
|
let mut first = true;
|
|
|
|
for elt in elts {
|
|
|
|
if first { first = false; } else { try!(self.word_space(",")); }
|
|
|
|
try!(op(self, elt));
|
|
|
|
}
|
|
|
|
self.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_lit(&mut self, pos: BytePos) -> Option<comments::Literal> {
|
|
|
|
let mut cur_lit = self.cur_cmnt_and_lit().cur_lit;
|
|
|
|
|
|
|
|
let mut result = None;
|
|
|
|
|
|
|
|
if let &Some(ref lits) = self.literals()
|
|
|
|
{
|
|
|
|
while cur_lit < lits.len() {
|
|
|
|
let ltrl = (*lits)[cur_lit].clone();
|
|
|
|
if ltrl.pos > pos { break; }
|
|
|
|
cur_lit += 1;
|
|
|
|
if ltrl.pos == pos {
|
|
|
|
result = Some(ltrl);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.cur_cmnt_and_lit().cur_lit = cur_lit;
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn maybe_print_comment(&mut self, pos: BytePos) -> io::Result<()> {
|
|
|
|
loop {
|
|
|
|
match self.next_comment() {
|
|
|
|
Some(ref cmnt) => {
|
|
|
|
if (*cmnt).pos < pos {
|
|
|
|
try!(self.print_comment(cmnt));
|
|
|
|
self.cur_cmnt_and_lit().cur_cmnt += 1;
|
|
|
|
} else { break; }
|
|
|
|
}
|
|
|
|
_ => break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_comment(&mut self,
|
|
|
|
cmnt: &comments::Comment) -> io::Result<()> {
|
|
|
|
match cmnt.style {
|
|
|
|
comments::Mixed => {
|
|
|
|
assert_eq!(cmnt.lines.len(), 1);
|
|
|
|
try!(zerobreak(self.writer()));
|
|
|
|
try!(word(self.writer(), &cmnt.lines[0]));
|
|
|
|
zerobreak(self.writer())
|
|
|
|
}
|
|
|
|
comments::Isolated => {
|
|
|
|
try!(self.hardbreak_if_not_bol());
|
|
|
|
for line in &cmnt.lines {
|
|
|
|
// Don't print empty lines because they will end up as trailing
|
|
|
|
// whitespace
|
|
|
|
if !line.is_empty() {
|
|
|
|
try!(word(self.writer(), &line[..]));
|
|
|
|
}
|
|
|
|
try!(hardbreak(self.writer()));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
comments::Trailing => {
|
|
|
|
try!(word(self.writer(), " "));
|
|
|
|
if cmnt.lines.len() == 1 {
|
|
|
|
try!(word(self.writer(), &cmnt.lines[0]));
|
|
|
|
hardbreak(self.writer())
|
|
|
|
} else {
|
|
|
|
try!(self.ibox(0));
|
|
|
|
for line in &cmnt.lines {
|
|
|
|
if !line.is_empty() {
|
|
|
|
try!(word(self.writer(), &line[..]));
|
|
|
|
}
|
|
|
|
try!(hardbreak(self.writer()));
|
|
|
|
}
|
|
|
|
self.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
comments::BlankLine => {
|
|
|
|
// We need to do at least one, possibly two hardbreaks.
|
|
|
|
let is_semi = match self.writer().last_token() {
|
|
|
|
pp::Token::String(s, _) => ";" == s,
|
|
|
|
_ => false
|
|
|
|
};
|
|
|
|
if is_semi || self.is_begin() || self.is_end() {
|
|
|
|
try!(hardbreak(self.writer()));
|
|
|
|
}
|
|
|
|
hardbreak(self.writer())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_comment(&mut self) -> Option<comments::Comment> {
|
|
|
|
let cur_cmnt = self.cur_cmnt_and_lit().cur_cmnt;
|
|
|
|
match *self.comments() {
|
|
|
|
Some(ref cmnts) => {
|
|
|
|
if cur_cmnt < cmnts.len() {
|
|
|
|
Some(cmnts[cur_cmnt].clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_literal(&mut self, lit: &ast::Lit) -> io::Result<()> {
|
|
|
|
try!(self.maybe_print_comment(lit.span.lo));
|
|
|
|
match self.next_lit(lit.span.lo) {
|
|
|
|
Some(ref ltrl) => {
|
|
|
|
return word(self.writer(), &(*ltrl).lit);
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
match lit.node {
|
2016-02-08 16:06:20 +00:00
|
|
|
ast::LitKind::Str(ref st, style) => self.print_string(&st, style),
|
|
|
|
ast::LitKind::Byte(byte) => {
|
2015-09-14 09:58:20 +00:00
|
|
|
let mut res = String::from("b'");
|
|
|
|
res.extend(ascii::escape_default(byte).map(|c| c as char));
|
|
|
|
res.push('\'');
|
|
|
|
word(self.writer(), &res[..])
|
|
|
|
}
|
2016-02-08 16:06:20 +00:00
|
|
|
ast::LitKind::Char(ch) => {
|
2015-09-14 09:58:20 +00:00
|
|
|
let mut res = String::from("'");
|
|
|
|
res.extend(ch.escape_default());
|
|
|
|
res.push('\'');
|
|
|
|
word(self.writer(), &res[..])
|
|
|
|
}
|
2016-02-08 16:06:20 +00:00
|
|
|
ast::LitKind::Int(i, t) => {
|
2015-09-14 09:58:20 +00:00
|
|
|
match t {
|
2016-02-08 16:16:23 +00:00
|
|
|
ast::LitIntType::Signed(st) => {
|
2015-09-14 09:58:20 +00:00
|
|
|
word(self.writer(),
|
2015-11-28 19:02:07 +00:00
|
|
|
&st.val_to_string(i as i64))
|
2015-09-14 09:58:20 +00:00
|
|
|
}
|
2016-02-08 16:16:23 +00:00
|
|
|
ast::LitIntType::Unsigned(ut) => {
|
2015-11-28 19:02:07 +00:00
|
|
|
word(self.writer(), &ut.val_to_string(i))
|
2015-09-14 09:58:20 +00:00
|
|
|
}
|
2016-02-08 16:16:23 +00:00
|
|
|
ast::LitIntType::Unsuffixed => {
|
2015-09-14 09:58:20 +00:00
|
|
|
word(self.writer(), &format!("{}", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-08 16:06:20 +00:00
|
|
|
ast::LitKind::Float(ref f, t) => {
|
2015-09-14 09:58:20 +00:00
|
|
|
word(self.writer(),
|
|
|
|
&format!(
|
|
|
|
"{}{}",
|
|
|
|
&f,
|
2015-11-28 19:02:07 +00:00
|
|
|
t.ty_to_string()))
|
2015-09-14 09:58:20 +00:00
|
|
|
}
|
2016-02-08 16:06:20 +00:00
|
|
|
ast::LitKind::FloatUnsuffixed(ref f) => word(self.writer(), &f[..]),
|
|
|
|
ast::LitKind::Bool(val) => {
|
2015-09-14 09:58:20 +00:00
|
|
|
if val { word(self.writer(), "true") } else { word(self.writer(), "false") }
|
|
|
|
}
|
2016-02-08 16:06:20 +00:00
|
|
|
ast::LitKind::ByteStr(ref v) => {
|
2015-09-14 09:58:20 +00:00
|
|
|
let mut escaped: String = String::new();
|
|
|
|
for &ch in v.iter() {
|
|
|
|
escaped.extend(ascii::escape_default(ch)
|
|
|
|
.map(|c| c as char));
|
|
|
|
}
|
|
|
|
word(self.writer(), &format!("b\"{}\"", escaped))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_string(&mut self, st: &str,
|
|
|
|
style: ast::StrStyle) -> io::Result<()> {
|
|
|
|
let st = match style {
|
|
|
|
ast::CookedStr => {
|
|
|
|
(format!("\"{}\"", st.escape_default()))
|
|
|
|
}
|
|
|
|
ast::RawStr(n) => {
|
|
|
|
(format!("r{delim}\"{string}\"{delim}",
|
|
|
|
delim=repeat("#", n),
|
|
|
|
string=st))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
word(self.writer(), &st[..])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_inner_attributes(&mut self,
|
2015-11-03 16:39:51 +00:00
|
|
|
attrs: &[ast::Attribute]) -> io::Result<()> {
|
|
|
|
self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_inner_attributes_no_trailing_hardbreak(&mut self,
|
|
|
|
attrs: &[ast::Attribute])
|
|
|
|
-> io::Result<()> {
|
|
|
|
self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, false)
|
2015-09-14 09:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn print_outer_attributes(&mut self,
|
|
|
|
attrs: &[ast::Attribute]) -> io::Result<()> {
|
2015-11-03 16:39:51 +00:00
|
|
|
self.print_either_attributes(attrs, ast::AttrStyle::Outer, false, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_inner_attributes_inline(&mut self,
|
|
|
|
attrs: &[ast::Attribute]) -> io::Result<()> {
|
|
|
|
self.print_either_attributes(attrs, ast::AttrStyle::Inner, true, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_outer_attributes_inline(&mut self,
|
|
|
|
attrs: &[ast::Attribute]) -> io::Result<()> {
|
|
|
|
self.print_either_attributes(attrs, ast::AttrStyle::Outer, true, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_either_attributes(&mut self,
|
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
kind: ast::AttrStyle,
|
|
|
|
is_inline: bool,
|
|
|
|
trailing_hardbreak: bool) -> io::Result<()> {
|
2015-09-14 09:58:20 +00:00
|
|
|
let mut count = 0;
|
|
|
|
for attr in attrs {
|
2015-11-03 16:39:51 +00:00
|
|
|
if attr.node.style == kind {
|
|
|
|
try!(self.print_attribute_inline(attr, is_inline));
|
|
|
|
if is_inline {
|
|
|
|
try!(self.nbsp());
|
|
|
|
}
|
2015-09-14 09:58:20 +00:00
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
2015-11-03 16:39:51 +00:00
|
|
|
if count > 0 && trailing_hardbreak && !is_inline {
|
2015-09-14 09:58:20 +00:00
|
|
|
try!(self.hardbreak_if_not_bol());
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_attribute(&mut self, attr: &ast::Attribute) -> io::Result<()> {
|
2015-11-03 16:39:51 +00:00
|
|
|
self.print_attribute_inline(attr, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_attribute_inline(&mut self, attr: &ast::Attribute,
|
|
|
|
is_inline: bool) -> io::Result<()> {
|
|
|
|
if !is_inline {
|
|
|
|
try!(self.hardbreak_if_not_bol());
|
|
|
|
}
|
2015-09-14 09:58:20 +00:00
|
|
|
try!(self.maybe_print_comment(attr.span.lo));
|
|
|
|
if attr.node.is_sugared_doc {
|
|
|
|
word(self.writer(), &attr.value_str().unwrap())
|
|
|
|
} else {
|
|
|
|
match attr.node.style {
|
2015-10-01 16:03:34 +00:00
|
|
|
ast::AttrStyle::Inner => try!(word(self.writer(), "#![")),
|
|
|
|
ast::AttrStyle::Outer => try!(word(self.writer(), "#[")),
|
2015-09-14 09:58:20 +00:00
|
|
|
}
|
|
|
|
try!(self.print_meta_item(&*attr.meta()));
|
|
|
|
word(self.writer(), "]")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_meta_item(&mut self, item: &ast::MetaItem) -> io::Result<()> {
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.ibox(INDENT_UNIT));
|
2015-09-14 09:58:20 +00:00
|
|
|
match item.node {
|
2016-02-09 11:05:20 +00:00
|
|
|
ast::MetaItemKind::Word(ref name) => {
|
2015-09-14 09:58:20 +00:00
|
|
|
try!(word(self.writer(), &name));
|
|
|
|
}
|
2016-02-09 11:05:20 +00:00
|
|
|
ast::MetaItemKind::NameValue(ref name, ref value) => {
|
2015-09-14 09:58:20 +00:00
|
|
|
try!(self.word_space(&name[..]));
|
|
|
|
try!(self.word_space("="));
|
|
|
|
try!(self.print_literal(value));
|
|
|
|
}
|
2016-02-09 11:05:20 +00:00
|
|
|
ast::MetaItemKind::List(ref name, ref items) => {
|
2015-09-14 09:58:20 +00:00
|
|
|
try!(word(self.writer(), &name));
|
|
|
|
try!(self.popen());
|
|
|
|
try!(self.commasep(Consistent,
|
|
|
|
&items[..],
|
|
|
|
|s, i| s.print_meta_item(&**i)));
|
|
|
|
try!(self.pclose());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.end()
|
|
|
|
}
|
2015-11-03 16:39:51 +00:00
|
|
|
|
|
|
|
fn space_if_not_bol(&mut self) -> io::Result<()> {
|
|
|
|
if !self.is_bol() { try!(space(self.writer())); }
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn nbsp(&mut self) -> io::Result<()> { word(self.writer(), " ") }
|
2015-09-14 09:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> PrintState<'a> for State<'a> {
|
|
|
|
fn writer(&mut self) -> &mut pp::Printer<'a> {
|
|
|
|
&mut self.s
|
|
|
|
}
|
|
|
|
|
|
|
|
fn boxes(&mut self) -> &mut Vec<pp::Breaks> {
|
|
|
|
&mut self.boxes
|
|
|
|
}
|
|
|
|
|
|
|
|
fn comments(&mut self) -> &mut Option<Vec<comments::Comment>> {
|
|
|
|
&mut self.comments
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cur_cmnt_and_lit(&mut self) -> &mut CurrentCommentAndLiteral {
|
|
|
|
&mut self.cur_cmnt_and_lit
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2011-08-02 22:25:06 +00:00
|
|
|
|
2015-09-14 09:58:20 +00:00
|
|
|
fn literals(&self) -> &Option<Vec<comments::Literal>> {
|
|
|
|
&self.literals
|
|
|
|
}
|
|
|
|
}
|
2011-08-02 22:25:06 +00:00
|
|
|
|
2015-09-14 09:58:20 +00:00
|
|
|
impl<'a> State<'a> {
|
|
|
|
pub fn cbox(&mut self, u: usize) -> io::Result<()> {
|
|
|
|
self.boxes.push(pp::Breaks::Consistent);
|
|
|
|
pp::cbox(&mut self.s, u)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn word_nbsp(&mut self, w: &str) -> io::Result<()> {
|
|
|
|
try!(word(&mut self.s, w));
|
|
|
|
self.nbsp()
|
|
|
|
}
|
2011-06-20 14:45:05 +00:00
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn head(&mut self, w: &str) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
// outer-box is consistent
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.cbox(INDENT_UNIT));
|
2014-03-16 18:58:11 +00:00
|
|
|
// head-box is inconsistent
|
|
|
|
try!(self.ibox(w.len() + 1));
|
|
|
|
// keyword that starts the head
|
|
|
|
if !w.is_empty() {
|
|
|
|
try!(self.word_nbsp(w));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2012-04-19 21:46:11 +00:00
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn bopen(&mut self) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "{"));
|
|
|
|
self.end() // close the head-box
|
2013-12-27 22:11:01 +00:00
|
|
|
}
|
2011-06-15 18:19:50 +00:00
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
pub fn bclose_(&mut self, span: codemap::Span,
|
2015-02-27 05:00:43 +00:00
|
|
|
indented: usize) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
self.bclose_maybe_open(span, indented, true)
|
|
|
|
}
|
2015-12-23 16:51:03 +00:00
|
|
|
pub fn bclose_maybe_open(&mut self, span: codemap::Span,
|
|
|
|
indented: usize, close_box: bool) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.maybe_print_comment(span.hi));
|
2015-01-28 01:01:48 +00:00
|
|
|
try!(self.break_offset_if_not_bol(1, -(indented as isize)));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "}"));
|
|
|
|
if close_box {
|
|
|
|
try!(self.end()); // close the outer-box
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn bclose(&mut self, span: codemap::Span) -> io::Result<()> {
|
2015-11-03 16:39:51 +00:00
|
|
|
self.bclose_(span, INDENT_UNIT)
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2014-01-30 01:39:21 +00:00
|
|
|
|
2014-03-27 17:31:00 +00:00
|
|
|
pub fn in_cbox(&self) -> bool {
|
|
|
|
match self.boxes.last() {
|
2015-01-14 05:12:39 +00:00
|
|
|
Some(&last_box) => last_box == pp::Breaks::Consistent,
|
2014-03-16 18:58:11 +00:00
|
|
|
None => false
|
|
|
|
}
|
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
|
2015-01-17 23:33:05 +00:00
|
|
|
pub fn break_offset_if_not_bol(&mut self, n: usize,
|
2015-02-27 05:00:43 +00:00
|
|
|
off: isize) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
if !self.is_bol() {
|
|
|
|
break_offset(&mut self.s, n, off)
|
|
|
|
} else {
|
|
|
|
if off != 0 && self.s.last_token().is_hardbreak_tok() {
|
|
|
|
// We do something pretty sketchy here: tuck the nonzero
|
|
|
|
// offset-adjustment we were going to deposit along with the
|
|
|
|
// break into the previous hardbreak.
|
|
|
|
self.s.replace_last_token(pp::hardbreak_tok_offset(off));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2014-01-30 01:39:21 +00:00
|
|
|
}
|
2011-03-18 00:39:47 +00:00
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
// Synthesizes a comment that was not textually present in the original source
|
|
|
|
// file.
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn synth_comment(&mut self, text: String) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "/*"));
|
|
|
|
try!(space(&mut self.s));
|
2015-02-18 19:48:57 +00:00
|
|
|
try!(word(&mut self.s, &text[..]));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(space(&mut self.s));
|
|
|
|
word(&mut self.s, "*/")
|
2014-01-30 01:39:21 +00:00
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-08 18:28:32 +00:00
|
|
|
pub fn commasep_cmnt<T, F, G>(&mut self,
|
|
|
|
b: Breaks,
|
|
|
|
elts: &[T],
|
|
|
|
mut op: F,
|
2015-02-27 05:00:43 +00:00
|
|
|
mut get_span: G) -> io::Result<()> where
|
|
|
|
F: FnMut(&mut State, &T) -> io::Result<()>,
|
2014-12-08 18:28:32 +00:00
|
|
|
G: FnMut(&T) -> codemap::Span,
|
|
|
|
{
|
2015-01-28 01:01:48 +00:00
|
|
|
try!(self.rbox(0, b));
|
2014-03-16 18:58:11 +00:00
|
|
|
let len = elts.len();
|
2015-01-28 01:01:48 +00:00
|
|
|
let mut i = 0;
|
2015-01-31 17:20:46 +00:00
|
|
|
for elt in elts {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.maybe_print_comment(get_span(elt).hi));
|
|
|
|
try!(op(self, elt));
|
2015-01-28 01:01:48 +00:00
|
|
|
i += 1;
|
2014-03-16 18:58:11 +00:00
|
|
|
if i < len {
|
|
|
|
try!(word(&mut self.s, ","));
|
|
|
|
try!(self.maybe_print_trailing_comment(get_span(elt),
|
|
|
|
Some(get_span(&elts[i]).hi)));
|
|
|
|
try!(self.space_if_not_bol());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.end()
|
2011-07-26 22:37:36 +00:00
|
|
|
}
|
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
pub fn commasep_exprs(&mut self, b: Breaks,
|
2015-02-27 05:00:43 +00:00
|
|
|
exprs: &[P<ast::Expr>]) -> io::Result<()> {
|
2014-05-16 07:16:13 +00:00
|
|
|
self.commasep_cmnt(b, exprs, |s, e| s.print_expr(&**e), |e| e.span)
|
2012-03-09 02:10:07 +00:00
|
|
|
}
|
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
pub fn print_mod(&mut self, _mod: &ast::Mod,
|
2015-02-27 05:00:43 +00:00
|
|
|
attrs: &[ast::Attribute]) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.print_inner_attributes(attrs));
|
2015-01-31 17:20:46 +00:00
|
|
|
for item in &_mod.items {
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_item(&**item));
|
2014-01-09 13:05:33 +00:00
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_foreign_mod(&mut self, nmod: &ast::ForeignMod,
|
2015-02-27 05:00:43 +00:00
|
|
|
attrs: &[ast::Attribute]) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.print_inner_attributes(attrs));
|
2015-01-31 17:20:46 +00:00
|
|
|
for item in &nmod.items {
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_foreign_item(&**item));
|
2014-01-09 13:05:33 +00:00
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_opt_lifetime(&mut self,
|
2015-02-27 05:00:43 +00:00
|
|
|
lifetime: &Option<ast::Lifetime>) -> io::Result<()> {
|
2015-01-31 17:20:46 +00:00
|
|
|
if let Some(l) = *lifetime {
|
|
|
|
try!(self.print_lifetime(&l));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.nbsp());
|
2013-02-19 01:45:56 +00:00
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
Ok(())
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_type(&mut self, ty: &ast::Ty) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.maybe_print_comment(ty.span.lo));
|
2015-01-28 01:01:48 +00:00
|
|
|
try!(self.ibox(0));
|
2014-03-16 18:58:11 +00:00
|
|
|
match ty.node {
|
2016-02-08 15:53:21 +00:00
|
|
|
ast::TyKind::Vec(ref ty) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "["));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_type(&**ty));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "]"));
|
|
|
|
}
|
2016-02-08 15:53:21 +00:00
|
|
|
ast::TyKind::Ptr(ref mt) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "*"));
|
2014-06-25 19:47:34 +00:00
|
|
|
match mt.mutbl {
|
2016-02-09 16:44:47 +00:00
|
|
|
ast::Mutability::Mutable => try!(self.word_nbsp("mut")),
|
|
|
|
ast::Mutability::Immutable => try!(self.word_nbsp("const")),
|
2014-06-25 19:47:34 +00:00
|
|
|
}
|
|
|
|
try!(self.print_type(&*mt.ty));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 15:53:21 +00:00
|
|
|
ast::TyKind::Rptr(ref lifetime, ref mt) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "&"));
|
|
|
|
try!(self.print_opt_lifetime(lifetime));
|
|
|
|
try!(self.print_mt(mt));
|
|
|
|
}
|
2016-02-08 15:53:21 +00:00
|
|
|
ast::TyKind::Tup(ref elts) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.popen());
|
2015-02-18 19:48:57 +00:00
|
|
|
try!(self.commasep(Inconsistent, &elts[..],
|
2014-09-13 16:06:01 +00:00
|
|
|
|s, ty| s.print_type(&**ty)));
|
2014-03-16 18:58:11 +00:00
|
|
|
if elts.len() == 1 {
|
|
|
|
try!(word(&mut self.s, ","));
|
|
|
|
}
|
|
|
|
try!(self.pclose());
|
|
|
|
}
|
2016-02-08 15:53:21 +00:00
|
|
|
ast::TyKind::Paren(ref typ) => {
|
2014-06-11 19:14:38 +00:00
|
|
|
try!(self.popen());
|
|
|
|
try!(self.print_type(&**typ));
|
|
|
|
try!(self.pclose());
|
|
|
|
}
|
2016-02-08 15:53:21 +00:00
|
|
|
ast::TyKind::BareFn(ref f) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
let generics = ast::Generics {
|
|
|
|
lifetimes: f.lifetimes.clone(),
|
2015-12-16 18:44:33 +00:00
|
|
|
ty_params: P::empty(),
|
2014-08-11 16:32:26 +00:00
|
|
|
where_clause: ast::WhereClause {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
predicates: Vec::new(),
|
|
|
|
},
|
2014-03-16 18:58:11 +00:00
|
|
|
};
|
2015-01-24 20:06:31 +00:00
|
|
|
try!(self.print_ty_fn(f.abi,
|
2014-12-09 15:36:46 +00:00
|
|
|
f.unsafety,
|
2014-05-16 07:16:13 +00:00
|
|
|
&*f.decl,
|
2014-06-02 01:41:46 +00:00
|
|
|
None,
|
2015-01-24 20:06:31 +00:00
|
|
|
&generics,
|
2014-07-30 05:08:39 +00:00
|
|
|
None));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 15:53:21 +00:00
|
|
|
ast::TyKind::Path(None, ref path) => {
|
2015-01-31 19:20:24 +00:00
|
|
|
try!(self.print_path(path, false, 0));
|
|
|
|
}
|
2016-02-08 15:53:21 +00:00
|
|
|
ast::TyKind::Path(Some(ref qself), ref path) => {
|
2015-02-17 17:29:13 +00:00
|
|
|
try!(self.print_qpath(path, qself, false))
|
2014-11-20 20:05:29 +00:00
|
|
|
}
|
2016-02-08 15:53:21 +00:00
|
|
|
ast::TyKind::ObjectSum(ref ty, ref bounds) => {
|
2014-11-20 20:05:29 +00:00
|
|
|
try!(self.print_type(&**ty));
|
2015-02-18 19:48:57 +00:00
|
|
|
try!(self.print_bounds("+", &bounds[..]));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 15:53:21 +00:00
|
|
|
ast::TyKind::PolyTraitRef(ref bounds) => {
|
2015-02-18 19:48:57 +00:00
|
|
|
try!(self.print_bounds("", &bounds[..]));
|
2014-11-07 11:53:45 +00:00
|
|
|
}
|
2016-02-08 15:53:21 +00:00
|
|
|
ast::TyKind::FixedLengthVec(ref ty, ref v) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "["));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_type(&**ty));
|
2014-12-20 02:20:51 +00:00
|
|
|
try!(word(&mut self.s, "; "));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_expr(&**v));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "]"));
|
|
|
|
}
|
2016-02-08 15:53:21 +00:00
|
|
|
ast::TyKind::Typeof(ref e) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "typeof("));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_expr(&**e));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, ")"));
|
|
|
|
}
|
2016-02-08 15:53:21 +00:00
|
|
|
ast::TyKind::Infer => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "_"));
|
|
|
|
}
|
2016-02-08 15:53:21 +00:00
|
|
|
ast::TyKind::Mac(ref m) => {
|
2015-07-26 04:30:35 +00:00
|
|
|
try!(self.print_mac(m, token::Paren));
|
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
self.end()
|
|
|
|
}
|
2013-11-30 22:00:39 +00:00
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
pub fn print_foreign_item(&mut self,
|
2015-02-27 05:00:43 +00:00
|
|
|
item: &ast::ForeignItem) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.hardbreak_if_not_bol());
|
|
|
|
try!(self.maybe_print_comment(item.span.lo));
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(self.print_outer_attributes(&item.attrs));
|
2014-03-16 18:58:11 +00:00
|
|
|
match item.node {
|
2016-02-09 10:31:19 +00:00
|
|
|
ast::ForeignItemKind::Fn(ref decl, ref generics) => {
|
2015-03-11 06:38:27 +00:00
|
|
|
try!(self.head(""));
|
2015-02-25 20:05:07 +00:00
|
|
|
try!(self.print_fn(decl, ast::Unsafety::Normal,
|
|
|
|
ast::Constness::NotConst,
|
2016-02-05 12:13:36 +00:00
|
|
|
Abi::Rust, Some(item.ident),
|
2015-03-11 06:38:27 +00:00
|
|
|
generics, None, item.vis));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.end()); // end head-ibox
|
|
|
|
try!(word(&mut self.s, ";"));
|
|
|
|
self.end() // end the outer fn box
|
|
|
|
}
|
2016-02-09 10:31:19 +00:00
|
|
|
ast::ForeignItemKind::Static(ref t, m) => {
|
2015-01-07 16:58:31 +00:00
|
|
|
try!(self.head(&visibility_qualified(item.vis,
|
2015-02-18 23:58:07 +00:00
|
|
|
"static")));
|
2014-03-16 18:58:11 +00:00
|
|
|
if m {
|
|
|
|
try!(self.word_space("mut"));
|
|
|
|
}
|
|
|
|
try!(self.print_ident(item.ident));
|
|
|
|
try!(self.word_space(":"));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_type(&**t));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, ";"));
|
|
|
|
try!(self.end()); // end the head-ibox
|
|
|
|
self.end() // end the outer cbox
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-11 07:15:45 +00:00
|
|
|
|
2015-03-16 01:35:25 +00:00
|
|
|
fn print_associated_const(&mut self,
|
|
|
|
ident: ast::Ident,
|
|
|
|
ty: &ast::Ty,
|
|
|
|
default: Option<&ast::Expr>,
|
|
|
|
vis: ast::Visibility)
|
|
|
|
-> io::Result<()>
|
|
|
|
{
|
|
|
|
try!(word(&mut self.s, &visibility_qualified(vis, "")));
|
|
|
|
try!(self.word_space("const"));
|
|
|
|
try!(self.print_ident(ident));
|
|
|
|
try!(self.word_space(":"));
|
|
|
|
try!(self.print_type(ty));
|
|
|
|
if let Some(expr) = default {
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space("="));
|
|
|
|
try!(self.print_expr(expr));
|
|
|
|
}
|
|
|
|
word(&mut self.s, ";")
|
|
|
|
}
|
|
|
|
|
2015-03-10 10:28:44 +00:00
|
|
|
fn print_associated_type(&mut self,
|
|
|
|
ident: ast::Ident,
|
|
|
|
bounds: Option<&ast::TyParamBounds>,
|
|
|
|
ty: Option<&ast::Ty>)
|
|
|
|
-> io::Result<()> {
|
2014-08-06 02:44:21 +00:00
|
|
|
try!(self.word_space("type"));
|
2015-03-10 10:28:44 +00:00
|
|
|
try!(self.print_ident(ident));
|
|
|
|
if let Some(bounds) = bounds {
|
|
|
|
try!(self.print_bounds(":", bounds));
|
|
|
|
}
|
|
|
|
if let Some(ty) = ty {
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space("="));
|
|
|
|
try!(self.print_type(ty));
|
|
|
|
}
|
2014-08-06 02:44:21 +00:00
|
|
|
word(&mut self.s, ";")
|
|
|
|
}
|
|
|
|
|
2014-07-12 04:22:11 +00:00
|
|
|
/// Pretty-print an item
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_item(&mut self, item: &ast::Item) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.hardbreak_if_not_bol());
|
|
|
|
try!(self.maybe_print_comment(item.span.lo));
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(self.print_outer_attributes(&item.attrs));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.ann.pre(self, NodeItem(item)));
|
|
|
|
match item.node {
|
2016-02-09 10:36:51 +00:00
|
|
|
ast::ItemKind::ExternCrate(ref optional_path) => {
|
2015-01-13 15:30:17 +00:00
|
|
|
try!(self.head(&visibility_qualified(item.vis,
|
2015-02-18 23:58:07 +00:00
|
|
|
"extern crate")));
|
2015-03-19 22:39:03 +00:00
|
|
|
if let Some(p) = *optional_path {
|
2015-07-28 16:07:20 +00:00
|
|
|
let val = p.as_str();
|
2015-03-19 22:39:03 +00:00
|
|
|
if val.contains("-") {
|
|
|
|
try!(self.print_string(&val, ast::CookedStr));
|
|
|
|
} else {
|
|
|
|
try!(self.print_name(p));
|
|
|
|
}
|
2015-01-13 15:30:17 +00:00
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(word(&mut self.s, "as"));
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
}
|
|
|
|
try!(self.print_ident(item.ident));
|
|
|
|
try!(word(&mut self.s, ";"));
|
|
|
|
try!(self.end()); // end inner head-block
|
|
|
|
try!(self.end()); // end outer head-block
|
|
|
|
}
|
2016-02-09 10:36:51 +00:00
|
|
|
ast::ItemKind::Use(ref vp) => {
|
2015-01-13 15:30:17 +00:00
|
|
|
try!(self.head(&visibility_qualified(item.vis,
|
2015-02-18 23:58:07 +00:00
|
|
|
"use")));
|
2015-01-13 15:30:17 +00:00
|
|
|
try!(self.print_view_path(&**vp));
|
|
|
|
try!(word(&mut self.s, ";"));
|
|
|
|
try!(self.end()); // end inner head-block
|
|
|
|
try!(self.end()); // end outer head-block
|
|
|
|
}
|
2016-02-09 10:36:51 +00:00
|
|
|
ast::ItemKind::Static(ref ty, m, ref expr) => {
|
2015-01-07 16:58:31 +00:00
|
|
|
try!(self.head(&visibility_qualified(item.vis,
|
2015-02-18 23:58:07 +00:00
|
|
|
"static")));
|
2016-02-09 16:44:47 +00:00
|
|
|
if m == ast::Mutability::Mutable {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.word_space("mut"));
|
2013-03-05 00:11:30 +00:00
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.print_ident(item.ident));
|
|
|
|
try!(self.word_space(":"));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_type(&**ty));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.end()); // end the head-ibox
|
|
|
|
|
|
|
|
try!(self.word_space("="));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_expr(&**expr));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, ";"));
|
|
|
|
try!(self.end()); // end the outer cbox
|
|
|
|
}
|
2016-02-09 10:36:51 +00:00
|
|
|
ast::ItemKind::Const(ref ty, ref expr) => {
|
2015-01-07 16:58:31 +00:00
|
|
|
try!(self.head(&visibility_qualified(item.vis,
|
2015-02-18 23:58:07 +00:00
|
|
|
"const")));
|
rustc: Add `const` globals to the language
This change is an implementation of [RFC 69][rfc] which adds a third kind of
global to the language, `const`. This global is most similar to what the old
`static` was, and if you're unsure about what to use then you should use a
`const`.
The semantics of these three kinds of globals are:
* A `const` does not represent a memory location, but only a value. Constants
are translated as rvalues, which means that their values are directly inlined
at usage location (similar to a #define in C/C++). Constant values are, well,
constant, and can not be modified. Any "modification" is actually a
modification to a local value on the stack rather than the actual constant
itself.
Almost all values are allowed inside constants, whether they have interior
mutability or not. There are a few minor restrictions listed in the RFC, but
they should in general not come up too often.
* A `static` now always represents a memory location (unconditionally). Any
references to the same `static` are actually a reference to the same memory
location. Only values whose types ascribe to `Sync` are allowed in a `static`.
This restriction is in place because many threads may access a `static`
concurrently. Lifting this restriction (and allowing unsafe access) is a
future extension not implemented at this time.
* A `static mut` continues to always represent a memory location. All references
to a `static mut` continue to be `unsafe`.
This is a large breaking change, and many programs will need to be updated
accordingly. A summary of the breaking changes is:
* Statics may no longer be used in patterns. Statics now always represent a
memory location, which can sometimes be modified. To fix code, repurpose the
matched-on-`static` to a `const`.
static FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
change this code to:
const FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
* Statics may no longer refer to other statics by value. Due to statics being
able to change at runtime, allowing them to reference one another could
possibly lead to confusing semantics. If you are in this situation, use a
constant initializer instead. Note, however, that statics may reference other
statics by address, however.
* Statics may no longer be used in constant expressions, such as array lengths.
This is due to the same restrictions as listed above. Use a `const` instead.
[breaking-change]
[rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-06 15:17:01 +00:00
|
|
|
try!(self.print_ident(item.ident));
|
|
|
|
try!(self.word_space(":"));
|
|
|
|
try!(self.print_type(&**ty));
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.end()); // end the head-ibox
|
|
|
|
|
|
|
|
try!(self.word_space("="));
|
|
|
|
try!(self.print_expr(&**expr));
|
|
|
|
try!(word(&mut self.s, ";"));
|
|
|
|
try!(self.end()); // end the outer cbox
|
|
|
|
}
|
2016-02-09 10:36:51 +00:00
|
|
|
ast::ItemKind::Fn(ref decl, unsafety, constness, abi, ref typarams, ref body) => {
|
2015-03-11 06:38:27 +00:00
|
|
|
try!(self.head(""));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.print_fn(
|
2015-03-11 06:38:27 +00:00
|
|
|
decl,
|
|
|
|
unsafety,
|
2015-02-25 20:05:07 +00:00
|
|
|
constness,
|
2014-03-16 18:58:11 +00:00
|
|
|
abi,
|
2015-03-11 06:38:27 +00:00
|
|
|
Some(item.ident),
|
2014-03-16 18:58:11 +00:00
|
|
|
typarams,
|
|
|
|
None,
|
|
|
|
item.vis
|
|
|
|
));
|
|
|
|
try!(word(&mut self.s, " "));
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(self.print_block_with_attrs(&**body, &item.attrs));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-09 10:36:51 +00:00
|
|
|
ast::ItemKind::Mod(ref _mod) => {
|
2015-01-07 16:58:31 +00:00
|
|
|
try!(self.head(&visibility_qualified(item.vis,
|
2015-02-18 23:58:07 +00:00
|
|
|
"mod")));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.print_ident(item.ident));
|
|
|
|
try!(self.nbsp());
|
|
|
|
try!(self.bopen());
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(self.print_mod(_mod, &item.attrs));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.bclose(item.span));
|
|
|
|
}
|
2016-02-09 10:36:51 +00:00
|
|
|
ast::ItemKind::ForeignMod(ref nmod) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.head("extern"));
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(self.word_nbsp(&nmod.abi.to_string()));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.bopen());
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(self.print_foreign_mod(nmod, &item.attrs));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.bclose(item.span));
|
|
|
|
}
|
2016-02-09 10:36:51 +00:00
|
|
|
ast::ItemKind::Ty(ref ty, ref params) => {
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.ibox(INDENT_UNIT));
|
2015-01-28 01:01:48 +00:00
|
|
|
try!(self.ibox(0));
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(self.word_nbsp(&visibility_qualified(item.vis, "type")));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.print_ident(item.ident));
|
|
|
|
try!(self.print_generics(params));
|
|
|
|
try!(self.end()); // end the inner ibox
|
|
|
|
|
2015-05-04 08:21:39 +00:00
|
|
|
try!(self.print_where_clause(¶ms.where_clause));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space("="));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_type(&**ty));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, ";"));
|
|
|
|
try!(self.end()); // end the outer ibox
|
|
|
|
}
|
2016-02-09 10:36:51 +00:00
|
|
|
ast::ItemKind::Enum(ref enum_definition, ref params) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.print_enum_def(
|
|
|
|
enum_definition,
|
|
|
|
params,
|
|
|
|
item.ident,
|
|
|
|
item.span,
|
|
|
|
item.vis
|
|
|
|
));
|
|
|
|
}
|
2016-02-09 10:36:51 +00:00
|
|
|
ast::ItemKind::Struct(ref struct_def, ref generics) => {
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(self.head(&visibility_qualified(item.vis,"struct")));
|
2015-10-01 15:47:27 +00:00
|
|
|
try!(self.print_struct(&struct_def, generics, item.ident, item.span, true));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2012-08-07 23:08:09 +00:00
|
|
|
|
2016-02-09 10:36:51 +00:00
|
|
|
ast::ItemKind::DefaultImpl(unsafety, ref trait_ref) => {
|
2015-01-22 21:14:52 +00:00
|
|
|
try!(self.head(""));
|
|
|
|
try!(self.print_visibility(item.vis));
|
|
|
|
try!(self.print_unsafety(unsafety));
|
|
|
|
try!(self.word_nbsp("impl"));
|
|
|
|
try!(self.print_trait_ref(trait_ref));
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space("for"));
|
|
|
|
try!(self.word_space(".."));
|
|
|
|
try!(self.bopen());
|
|
|
|
try!(self.bclose(item.span));
|
|
|
|
}
|
2016-02-09 10:36:51 +00:00
|
|
|
ast::ItemKind::Impl(unsafety,
|
2014-12-28 22:33:18 +00:00
|
|
|
polarity,
|
2014-12-10 11:15:06 +00:00
|
|
|
ref generics,
|
2014-08-04 20:56:56 +00:00
|
|
|
ref opt_trait,
|
|
|
|
ref ty,
|
|
|
|
ref impl_items) => {
|
2014-12-10 11:15:06 +00:00
|
|
|
try!(self.head(""));
|
|
|
|
try!(self.print_visibility(item.vis));
|
|
|
|
try!(self.print_unsafety(unsafety));
|
|
|
|
try!(self.word_nbsp("impl"));
|
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
if generics.is_parameterized() {
|
|
|
|
try!(self.print_generics(generics));
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
}
|
2013-03-27 10:16:28 +00:00
|
|
|
|
2014-12-28 22:33:18 +00:00
|
|
|
match polarity {
|
|
|
|
ast::ImplPolarity::Negative => {
|
|
|
|
try!(word(&mut self.s, "!"));
|
|
|
|
},
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2015-11-17 14:24:49 +00:00
|
|
|
match *opt_trait {
|
|
|
|
Some(ref t) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.print_trait_ref(t));
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space("for"));
|
|
|
|
}
|
2015-11-17 14:24:49 +00:00
|
|
|
None => {}
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2012-08-09 00:14:25 +00:00
|
|
|
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_type(&**ty));
|
2015-03-14 04:54:29 +00:00
|
|
|
try!(self.print_where_clause(&generics.where_clause));
|
2012-08-08 21:17:52 +00:00
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.bopen());
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(self.print_inner_attributes(&item.attrs));
|
2015-01-31 17:20:46 +00:00
|
|
|
for impl_item in impl_items {
|
2015-03-10 10:28:44 +00:00
|
|
|
try!(self.print_impl_item(impl_item));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
try!(self.bclose(item.span));
|
|
|
|
}
|
2016-02-09 10:36:51 +00:00
|
|
|
ast::ItemKind::Trait(unsafety, ref generics, ref bounds, ref trait_items) => {
|
2014-12-10 00:59:20 +00:00
|
|
|
try!(self.head(""));
|
|
|
|
try!(self.print_visibility(item.vis));
|
|
|
|
try!(self.print_unsafety(unsafety));
|
|
|
|
try!(self.word_nbsp("trait"));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.print_ident(item.ident));
|
|
|
|
try!(self.print_generics(generics));
|
2014-12-24 09:34:57 +00:00
|
|
|
let mut real_bounds = Vec::with_capacity(bounds.len());
|
2015-02-13 07:33:44 +00:00
|
|
|
for b in bounds.iter() {
|
|
|
|
if let TraitTyParamBound(ref ptr, ast::TraitBoundModifier::Maybe) = *b {
|
2014-12-24 09:34:57 +00:00
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space("for ?"));
|
|
|
|
try!(self.print_trait_ref(&ptr.trait_ref));
|
|
|
|
} else {
|
2015-02-13 07:33:44 +00:00
|
|
|
real_bounds.push(b.clone());
|
2014-12-24 09:34:57 +00:00
|
|
|
}
|
2014-04-03 00:38:45 +00:00
|
|
|
}
|
2015-02-18 19:48:57 +00:00
|
|
|
try!(self.print_bounds(":", &real_bounds[..]));
|
2015-03-14 04:54:29 +00:00
|
|
|
try!(self.print_where_clause(&generics.where_clause));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, " "));
|
|
|
|
try!(self.bopen());
|
2015-03-10 10:28:44 +00:00
|
|
|
for trait_item in trait_items {
|
|
|
|
try!(self.print_trait_item(trait_item));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
try!(self.bclose(item.span));
|
|
|
|
}
|
2016-02-09 10:36:51 +00:00
|
|
|
ast::ItemKind::Mac(codemap::Spanned { ref node, .. }) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.print_visibility(item.vis));
|
2015-09-20 20:15:37 +00:00
|
|
|
try!(self.print_path(&node.path, false, 0));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "! "));
|
|
|
|
try!(self.print_ident(item.ident));
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.cbox(INDENT_UNIT));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.popen());
|
2015-09-20 20:15:37 +00:00
|
|
|
try!(self.print_tts(&node.tts[..]));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.pclose());
|
2014-11-14 17:18:10 +00:00
|
|
|
try!(word(&mut self.s, ";"));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.ann.post(self, NodeItem(item))
|
|
|
|
}
|
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
fn print_trait_ref(&mut self, t: &ast::TraitRef) -> io::Result<()> {
|
2015-01-31 19:20:24 +00:00
|
|
|
self.print_path(&t.path, false, 0)
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
fn print_formal_lifetime_list(&mut self, lifetimes: &[ast::LifetimeDef]) -> io::Result<()> {
|
2015-02-09 03:49:27 +00:00
|
|
|
if !lifetimes.is_empty() {
|
2014-11-07 11:53:45 +00:00
|
|
|
try!(word(&mut self.s, "for<"));
|
2014-12-14 03:11:04 +00:00
|
|
|
let mut comma = false;
|
2015-02-09 03:49:27 +00:00
|
|
|
for lifetime_def in lifetimes {
|
2014-12-14 03:11:04 +00:00
|
|
|
if comma {
|
|
|
|
try!(self.word_space(","))
|
|
|
|
}
|
2014-11-07 11:53:45 +00:00
|
|
|
try!(self.print_lifetime_def(lifetime_def));
|
2014-12-14 03:11:04 +00:00
|
|
|
comma = true;
|
2014-11-07 11:53:45 +00:00
|
|
|
}
|
|
|
|
try!(word(&mut self.s, ">"));
|
|
|
|
}
|
2015-02-09 03:49:27 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2014-11-07 11:53:45 +00:00
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
fn print_poly_trait_ref(&mut self, t: &ast::PolyTraitRef) -> io::Result<()> {
|
2015-02-09 03:49:27 +00:00
|
|
|
try!(self.print_formal_lifetime_list(&t.bound_lifetimes));
|
2014-11-07 11:53:45 +00:00
|
|
|
self.print_trait_ref(&t.trait_ref)
|
|
|
|
}
|
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
pub fn print_enum_def(&mut self, enum_definition: &ast::EnumDef,
|
|
|
|
generics: &ast::Generics, ident: ast::Ident,
|
|
|
|
span: codemap::Span,
|
2015-02-27 05:00:43 +00:00
|
|
|
visibility: ast::Visibility) -> io::Result<()> {
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(self.head(&visibility_qualified(visibility, "enum")));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.print_ident(ident));
|
|
|
|
try!(self.print_generics(generics));
|
2015-03-14 04:54:29 +00:00
|
|
|
try!(self.print_where_clause(&generics.where_clause));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(space(&mut self.s));
|
2015-02-18 23:58:07 +00:00
|
|
|
self.print_variants(&enum_definition.variants, span)
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_variants(&mut self,
|
|
|
|
variants: &[P<ast::Variant>],
|
2015-02-27 05:00:43 +00:00
|
|
|
span: codemap::Span) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.bopen());
|
2015-01-31 17:20:46 +00:00
|
|
|
for v in variants {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.space_if_not_bol());
|
|
|
|
try!(self.maybe_print_comment(v.span.lo));
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(self.print_outer_attributes(&v.node.attrs));
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.ibox(INDENT_UNIT));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_variant(&**v));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, ","));
|
|
|
|
try!(self.end());
|
|
|
|
try!(self.maybe_print_trailing_comment(v.span, None));
|
|
|
|
}
|
|
|
|
self.bclose(span)
|
|
|
|
}
|
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_visibility(&mut self, vis: ast::Visibility) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
match vis {
|
|
|
|
ast::Public => self.word_nbsp("pub"),
|
|
|
|
ast::Inherited => Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_struct(&mut self,
|
2015-10-08 00:20:57 +00:00
|
|
|
struct_def: &ast::VariantData,
|
2014-03-16 18:58:11 +00:00
|
|
|
generics: &ast::Generics,
|
|
|
|
ident: ast::Ident,
|
2015-10-01 15:47:27 +00:00
|
|
|
span: codemap::Span,
|
|
|
|
print_finalizer: bool) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.print_ident(ident));
|
|
|
|
try!(self.print_generics(generics));
|
2015-10-08 20:45:46 +00:00
|
|
|
if !struct_def.is_struct() {
|
|
|
|
if struct_def.is_tuple() {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.popen());
|
2015-10-25 15:33:51 +00:00
|
|
|
try!(self.commasep(
|
2015-10-08 20:45:46 +00:00
|
|
|
Inconsistent, struct_def.fields(),
|
2014-03-16 18:58:11 +00:00
|
|
|
|s, field| {
|
|
|
|
match field.node.kind {
|
2014-10-09 19:17:22 +00:00
|
|
|
ast::NamedField(..) => panic!("unexpected named field"),
|
2014-03-25 23:53:52 +00:00
|
|
|
ast::UnnamedField(vis) => {
|
|
|
|
try!(s.print_visibility(vis));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(s.maybe_print_comment(field.span.lo));
|
2014-05-16 07:16:13 +00:00
|
|
|
s.print_type(&*field.node.ty)
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
));
|
|
|
|
try!(self.pclose());
|
|
|
|
}
|
2015-03-14 04:54:29 +00:00
|
|
|
try!(self.print_where_clause(&generics.where_clause));
|
2015-10-01 15:47:27 +00:00
|
|
|
if print_finalizer {
|
|
|
|
try!(word(&mut self.s, ";"));
|
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.end());
|
|
|
|
self.end() // close the outer-box
|
|
|
|
} else {
|
2015-03-14 04:54:29 +00:00
|
|
|
try!(self.print_where_clause(&generics.where_clause));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.nbsp());
|
|
|
|
try!(self.bopen());
|
|
|
|
try!(self.hardbreak_if_not_bol());
|
2012-09-22 01:10:45 +00:00
|
|
|
|
2015-10-08 20:45:46 +00:00
|
|
|
for field in struct_def.fields() {
|
2014-03-16 18:58:11 +00:00
|
|
|
match field.node.kind {
|
2014-10-09 19:17:22 +00:00
|
|
|
ast::UnnamedField(..) => panic!("unexpected unnamed field"),
|
2014-03-16 18:58:11 +00:00
|
|
|
ast::NamedField(ident, visibility) => {
|
|
|
|
try!(self.hardbreak_if_not_bol());
|
|
|
|
try!(self.maybe_print_comment(field.span.lo));
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(self.print_outer_attributes(&field.node.attrs));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.print_visibility(visibility));
|
|
|
|
try!(self.print_ident(ident));
|
|
|
|
try!(self.word_nbsp(":"));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_type(&*field.node.ty));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, ","));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-22 01:10:45 +00:00
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
self.bclose(span)
|
|
|
|
}
|
2012-09-22 01:10:45 +00:00
|
|
|
}
|
|
|
|
|
2014-03-16 18:58:11 +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.
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_tt(&mut self, tt: &ast::TokenTree) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
match *tt {
|
2015-11-06 13:52:02 +00:00
|
|
|
TokenTree::Token(_, ref tk) => {
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(word(&mut self.s, &token_to_string(tk)));
|
2014-05-11 08:34:28 +00:00
|
|
|
match *tk {
|
2014-10-27 08:22:52 +00:00
|
|
|
parse::token::DocComment(..) => {
|
2014-05-11 08:34:28 +00:00
|
|
|
hardbreak(&mut self.s)
|
|
|
|
}
|
|
|
|
_ => Ok(())
|
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2015-11-06 13:52:02 +00:00
|
|
|
TokenTree::Delimited(_, ref delimed) => {
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(word(&mut self.s, &token_to_string(&delimed.open_token())));
|
2014-10-06 22:00:56 +00:00
|
|
|
try!(space(&mut self.s));
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(self.print_tts(&delimed.tts));
|
2014-10-06 22:00:56 +00:00
|
|
|
try!(space(&mut self.s));
|
2015-02-18 23:58:07 +00:00
|
|
|
word(&mut self.s, &token_to_string(&delimed.close_token()))
|
2014-10-06 22:00:56 +00:00
|
|
|
},
|
2015-11-06 13:52:02 +00:00
|
|
|
TokenTree::Sequence(_, ref seq) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "$("));
|
2015-01-31 17:20:46 +00:00
|
|
|
for tt_elt in &seq.tts {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.print_tt(tt_elt));
|
2012-08-15 22:53:58 +00:00
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, ")"));
|
2014-11-02 11:21:16 +00:00
|
|
|
match seq.separator {
|
2014-03-16 18:58:11 +00:00
|
|
|
Some(ref tk) => {
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(word(&mut self.s, &token_to_string(tk)));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2014-10-28 00:05:28 +00:00
|
|
|
None => {},
|
2012-10-26 19:11:14 +00:00
|
|
|
}
|
2014-11-02 11:21:16 +00:00
|
|
|
match seq.op {
|
2016-02-09 10:45:01 +00:00
|
|
|
ast::KleeneOp::ZeroOrMore => word(&mut self.s, "*"),
|
|
|
|
ast::KleeneOp::OneOrMore => word(&mut self.s, "+"),
|
2014-10-23 00:24:20 +00:00
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2012-10-26 19:11:14 +00:00
|
|
|
}
|
2012-08-15 22:53:58 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_tts(&mut self, tts: &[ast::TokenTree]) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.ibox(0));
|
2015-01-23 01:34:31 +00:00
|
|
|
let mut suppress_space = false;
|
2014-03-16 18:58:11 +00:00
|
|
|
for (i, tt) in tts.iter().enumerate() {
|
2015-01-23 01:34:31 +00:00
|
|
|
if i != 0 && !suppress_space {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(space(&mut self.s));
|
|
|
|
}
|
|
|
|
try!(self.print_tt(tt));
|
2015-01-23 01:34:31 +00:00
|
|
|
// There should be no space between the module name and the following `::` in paths,
|
2015-01-23 19:27:16 +00:00
|
|
|
// otherwise imported macros get re-parsed from crate metadata incorrectly (#20701)
|
2015-11-17 14:24:49 +00:00
|
|
|
suppress_space = match *tt {
|
|
|
|
TokenTree::Token(_, token::Ident(_, token::ModName)) |
|
|
|
|
TokenTree::Token(_, token::MatchNt(_, _, _, token::ModName)) |
|
|
|
|
TokenTree::Token(_, token::SubstNt(_, token::ModName)) => true,
|
2015-01-23 01:34:31 +00:00
|
|
|
_ => false
|
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
self.end()
|
|
|
|
}
|
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_variant(&mut self, v: &ast::Variant) -> io::Result<()> {
|
2015-10-01 15:47:27 +00:00
|
|
|
try!(self.head(""));
|
2015-11-28 19:02:07 +00:00
|
|
|
let generics = ast::Generics::default();
|
2015-10-08 00:20:57 +00:00
|
|
|
try!(self.print_struct(&v.node.data, &generics, v.node.name, v.span, false));
|
2014-03-16 18:58:11 +00:00
|
|
|
match v.node.disr_expr {
|
2014-05-16 07:16:13 +00:00
|
|
|
Some(ref d) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space("="));
|
2014-05-16 07:16:13 +00:00
|
|
|
self.print_expr(&**d)
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
_ => Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 06:38:27 +00:00
|
|
|
pub fn print_method_sig(&mut self,
|
|
|
|
ident: ast::Ident,
|
|
|
|
m: &ast::MethodSig,
|
|
|
|
vis: ast::Visibility)
|
|
|
|
-> io::Result<()> {
|
|
|
|
self.print_fn(&m.decl,
|
|
|
|
m.unsafety,
|
2015-02-25 20:05:07 +00:00
|
|
|
m.constness,
|
2015-03-11 06:38:27 +00:00
|
|
|
m.abi,
|
|
|
|
Some(ident),
|
|
|
|
&m.generics,
|
|
|
|
Some(&m.explicit_self.node),
|
|
|
|
vis)
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
|
2015-03-10 10:28:44 +00:00
|
|
|
pub fn print_trait_item(&mut self, ti: &ast::TraitItem)
|
|
|
|
-> io::Result<()> {
|
2015-03-24 01:52:55 +00:00
|
|
|
try!(self.ann.pre(self, NodeSubItem(ti.id)));
|
2015-03-10 10:28:44 +00:00
|
|
|
try!(self.hardbreak_if_not_bol());
|
|
|
|
try!(self.maybe_print_comment(ti.span.lo));
|
|
|
|
try!(self.print_outer_attributes(&ti.attrs));
|
|
|
|
match ti.node {
|
2015-03-16 01:35:25 +00:00
|
|
|
ast::ConstTraitItem(ref ty, ref default) => {
|
|
|
|
try!(self.print_associated_const(ti.ident, &ty,
|
|
|
|
default.as_ref().map(|expr| &**expr),
|
|
|
|
ast::Inherited));
|
|
|
|
}
|
2015-03-11 21:38:58 +00:00
|
|
|
ast::MethodTraitItem(ref sig, ref body) => {
|
|
|
|
if body.is_some() {
|
|
|
|
try!(self.head(""));
|
|
|
|
}
|
2015-03-11 06:38:27 +00:00
|
|
|
try!(self.print_method_sig(ti.ident, sig, ast::Inherited));
|
2015-03-11 21:38:58 +00:00
|
|
|
if let Some(ref body) = *body {
|
|
|
|
try!(self.nbsp());
|
2015-03-24 01:52:55 +00:00
|
|
|
try!(self.print_block_with_attrs(body, &ti.attrs));
|
2015-03-11 21:38:58 +00:00
|
|
|
} else {
|
2015-03-24 01:52:55 +00:00
|
|
|
try!(word(&mut self.s, ";"));
|
2015-03-11 21:38:58 +00:00
|
|
|
}
|
2015-03-10 10:28:44 +00:00
|
|
|
}
|
|
|
|
ast::TypeTraitItem(ref bounds, ref default) => {
|
2015-03-24 01:52:55 +00:00
|
|
|
try!(self.print_associated_type(ti.ident, Some(bounds),
|
|
|
|
default.as_ref().map(|ty| &**ty)));
|
2015-03-10 10:28:44 +00:00
|
|
|
}
|
2014-08-04 20:56:56 +00:00
|
|
|
}
|
2015-03-24 01:52:55 +00:00
|
|
|
self.ann.post(self, NodeSubItem(ti.id))
|
2014-08-04 20:56:56 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_impl_item(&mut self, ii: &ast::ImplItem) -> io::Result<()> {
|
2015-03-24 01:52:55 +00:00
|
|
|
try!(self.ann.pre(self, NodeSubItem(ii.id)));
|
2015-03-10 10:28:44 +00:00
|
|
|
try!(self.hardbreak_if_not_bol());
|
|
|
|
try!(self.maybe_print_comment(ii.span.lo));
|
|
|
|
try!(self.print_outer_attributes(&ii.attrs));
|
|
|
|
match ii.node {
|
2015-11-13 13:15:04 +00:00
|
|
|
ast::ImplItemKind::Const(ref ty, ref expr) => {
|
2015-03-16 01:35:25 +00:00
|
|
|
try!(self.print_associated_const(ii.ident, &ty, Some(&expr), ii.vis));
|
|
|
|
}
|
2015-11-13 13:15:04 +00:00
|
|
|
ast::ImplItemKind::Method(ref sig, ref body) => {
|
2015-03-11 21:38:58 +00:00
|
|
|
try!(self.head(""));
|
|
|
|
try!(self.print_method_sig(ii.ident, sig, ii.vis));
|
|
|
|
try!(self.nbsp());
|
2015-03-24 01:52:55 +00:00
|
|
|
try!(self.print_block_with_attrs(body, &ii.attrs));
|
2015-03-10 10:28:44 +00:00
|
|
|
}
|
2015-11-13 13:15:04 +00:00
|
|
|
ast::ImplItemKind::Type(ref ty) => {
|
2015-03-24 01:52:55 +00:00
|
|
|
try!(self.print_associated_type(ii.ident, None, Some(ty)));
|
2015-03-10 10:28:44 +00:00
|
|
|
}
|
2015-11-13 13:15:04 +00:00
|
|
|
ast::ImplItemKind::Macro(codemap::Spanned { ref node, .. }) => {
|
2016-02-09 10:36:51 +00:00
|
|
|
// code copied from ItemKind::Mac:
|
2015-09-20 20:15:37 +00:00
|
|
|
try!(self.print_path(&node.path, false, 0));
|
2014-07-12 04:22:11 +00:00
|
|
|
try!(word(&mut self.s, "! "));
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.cbox(INDENT_UNIT));
|
2014-07-12 04:22:11 +00:00
|
|
|
try!(self.popen());
|
2015-09-20 20:15:37 +00:00
|
|
|
try!(self.print_tts(&node.tts[..]));
|
2014-07-12 04:22:11 +00:00
|
|
|
try!(self.pclose());
|
2014-11-14 17:18:10 +00:00
|
|
|
try!(word(&mut self.s, ";"));
|
2015-03-24 01:52:55 +00:00
|
|
|
try!(self.end())
|
2014-07-12 04:22:11 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-24 01:52:55 +00:00
|
|
|
self.ann.post(self, NodeSubItem(ii.id))
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_stmt(&mut self, st: &ast::Stmt) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.maybe_print_comment(st.span.lo));
|
|
|
|
match st.node {
|
2016-02-08 16:23:13 +00:00
|
|
|
ast::StmtKind::Decl(ref decl, _) => {
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_decl(&**decl));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 16:23:13 +00:00
|
|
|
ast::StmtKind::Expr(ref expr, _) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.space_if_not_bol());
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_expr_outer_attr_style(&**expr, false));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 16:23:13 +00:00
|
|
|
ast::StmtKind::Semi(ref expr, _) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.space_if_not_bol());
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_expr_outer_attr_style(&**expr, false));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, ";"));
|
|
|
|
}
|
2016-02-08 16:23:13 +00:00
|
|
|
ast::StmtKind::Mac(ref mac, style, ref attrs) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.space_if_not_bol());
|
2015-11-15 16:17:50 +00:00
|
|
|
try!(self.print_outer_attributes(attrs.as_attr_slice()));
|
2014-11-14 17:18:10 +00:00
|
|
|
let delim = match style {
|
2016-02-09 10:56:59 +00:00
|
|
|
ast::MacStmtStyle::Braces => token::Brace,
|
2014-11-14 17:18:10 +00:00
|
|
|
_ => token::Paren
|
|
|
|
};
|
2014-12-05 09:10:22 +00:00
|
|
|
try!(self.print_mac(&**mac, delim));
|
2014-11-14 17:18:10 +00:00
|
|
|
match style {
|
2016-02-09 10:56:59 +00:00
|
|
|
ast::MacStmtStyle::Braces => {}
|
2014-11-14 17:18:10 +00:00
|
|
|
_ => try!(word(&mut self.s, ";")),
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2012-06-30 10:54:54 +00:00
|
|
|
}
|
2011-06-16 20:00:19 +00:00
|
|
|
}
|
2014-09-13 16:06:01 +00:00
|
|
|
if parse::classify::stmt_ends_with_semi(&st.node) {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, ";"));
|
|
|
|
}
|
|
|
|
self.maybe_print_trailing_comment(st.span, None)
|
2011-06-16 20:00:19 +00:00
|
|
|
}
|
2011-07-08 23:35:09 +00:00
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_block(&mut self, blk: &ast::Block) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
self.print_block_with_attrs(blk, &[])
|
|
|
|
}
|
2012-08-14 01:13:41 +00:00
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_block_unclosed(&mut self, blk: &ast::Block) -> io::Result<()> {
|
2015-11-03 16:39:51 +00:00
|
|
|
self.print_block_unclosed_indent(blk, INDENT_UNIT)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_block_unclosed_with_attrs(&mut self, blk: &ast::Block,
|
|
|
|
attrs: &[ast::Attribute])
|
|
|
|
-> io::Result<()> {
|
|
|
|
self.print_block_maybe_unclosed(blk, INDENT_UNIT, attrs, false)
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2012-01-16 01:23:59 +00:00
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
pub fn print_block_unclosed_indent(&mut self, blk: &ast::Block,
|
2015-02-27 05:00:43 +00:00
|
|
|
indented: usize) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
self.print_block_maybe_unclosed(blk, indented, &[], false)
|
|
|
|
}
|
2011-08-15 21:42:33 +00:00
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
pub fn print_block_with_attrs(&mut self,
|
|
|
|
blk: &ast::Block,
|
2015-02-27 05:00:43 +00:00
|
|
|
attrs: &[ast::Attribute]) -> io::Result<()> {
|
2015-11-03 16:39:51 +00:00
|
|
|
self.print_block_maybe_unclosed(blk, INDENT_UNIT, attrs, true)
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2012-01-16 01:23:59 +00:00
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
pub fn print_block_maybe_unclosed(&mut self,
|
2013-07-19 05:38:55 +00:00
|
|
|
blk: &ast::Block,
|
2015-01-17 23:33:05 +00:00
|
|
|
indented: usize,
|
2013-07-19 11:51:37 +00:00
|
|
|
attrs: &[ast::Attribute],
|
2015-02-27 05:00:43 +00:00
|
|
|
close_box: bool) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
match blk.rules {
|
2016-02-08 11:44:45 +00:00
|
|
|
BlockCheckMode::Unsafe(..) => try!(self.word_space("unsafe")),
|
|
|
|
BlockCheckMode::Default => ()
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
try!(self.maybe_print_comment(blk.span.lo));
|
|
|
|
try!(self.ann.pre(self, NodeBlock(blk)));
|
|
|
|
try!(self.bopen());
|
|
|
|
|
|
|
|
try!(self.print_inner_attributes(attrs));
|
|
|
|
|
2015-01-31 17:20:46 +00:00
|
|
|
for st in &blk.stmts {
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_stmt(&**st));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
match blk.expr {
|
2014-05-16 07:16:13 +00:00
|
|
|
Some(ref expr) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.space_if_not_bol());
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_expr_outer_attr_style(&**expr, false));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi)));
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
try!(self.bclose_maybe_open(blk.span, indented, close_box));
|
|
|
|
self.ann.post(self, NodeBlock(blk))
|
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
fn print_else(&mut self, els: Option<&ast::Expr>) -> io::Result<()> {
|
2012-08-06 19:34:08 +00:00
|
|
|
match els {
|
2014-01-30 01:39:21 +00:00
|
|
|
Some(_else) => {
|
|
|
|
match _else.node {
|
|
|
|
// "another else-if"
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::If(ref i, ref then, ref e) => {
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.cbox(INDENT_UNIT - 1));
|
2015-01-28 01:01:48 +00:00
|
|
|
try!(self.ibox(0));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, " else if "));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_expr(&**i));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(space(&mut self.s));
|
2014-09-13 16:06:01 +00:00
|
|
|
try!(self.print_block(&**then));
|
|
|
|
self.print_else(e.as_ref().map(|e| &**e))
|
2014-01-30 01:39:21 +00:00
|
|
|
}
|
2014-08-25 01:04:29 +00:00
|
|
|
// "another else-if-let"
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::IfLet(ref pat, ref expr, ref then, ref e) => {
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.cbox(INDENT_UNIT - 1));
|
2015-01-28 01:01:48 +00:00
|
|
|
try!(self.ibox(0));
|
2014-08-25 01:04:29 +00:00
|
|
|
try!(word(&mut self.s, " else if let "));
|
|
|
|
try!(self.print_pat(&**pat));
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space("="));
|
|
|
|
try!(self.print_expr(&**expr));
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.print_block(&**then));
|
|
|
|
self.print_else(e.as_ref().map(|e| &**e))
|
|
|
|
}
|
2014-01-30 01:39:21 +00:00
|
|
|
// "final else"
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Block(ref b) => {
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.cbox(INDENT_UNIT - 1));
|
2015-01-28 01:01:48 +00:00
|
|
|
try!(self.ibox(0));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, " else "));
|
2014-05-16 07:16:13 +00:00
|
|
|
self.print_block(&**b)
|
2014-01-30 01:39:21 +00:00
|
|
|
}
|
|
|
|
// BLEAH, constraints would be great here
|
|
|
|
_ => {
|
2014-10-09 19:17:22 +00:00
|
|
|
panic!("print_if saw if with weird alternative");
|
2014-01-30 01:39:21 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-16 21:08:17 +00:00
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
_ => Ok(())
|
2011-06-16 21:08:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
pub fn print_if(&mut self, test: &ast::Expr, blk: &ast::Block,
|
2015-02-27 05:00:43 +00:00
|
|
|
elseopt: Option<&ast::Expr>) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.head("if"));
|
|
|
|
try!(self.print_expr(test));
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.print_block(blk));
|
|
|
|
self.print_else(elseopt)
|
2011-07-08 23:35:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-25 01:04:29 +00:00
|
|
|
pub fn print_if_let(&mut self, pat: &ast::Pat, expr: &ast::Expr, blk: &ast::Block,
|
2015-02-27 05:00:43 +00:00
|
|
|
elseopt: Option<&ast::Expr>) -> io::Result<()> {
|
2014-08-25 01:04:29 +00:00
|
|
|
try!(self.head("if let"));
|
|
|
|
try!(self.print_pat(pat));
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space("="));
|
|
|
|
try!(self.print_expr(expr));
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.print_block(blk));
|
|
|
|
self.print_else(elseopt)
|
|
|
|
}
|
|
|
|
|
2014-11-14 17:18:10 +00:00
|
|
|
pub fn print_mac(&mut self, m: &ast::Mac, delim: token::DelimToken)
|
2015-02-27 05:00:43 +00:00
|
|
|
-> io::Result<()> {
|
2015-09-20 20:15:37 +00:00
|
|
|
try!(self.print_path(&m.node.path, false, 0));
|
|
|
|
try!(word(&mut self.s, "!"));
|
|
|
|
match delim {
|
|
|
|
token::Paren => try!(self.popen()),
|
|
|
|
token::Bracket => try!(word(&mut self.s, "[")),
|
2015-11-03 16:39:51 +00:00
|
|
|
token::Brace => {
|
2016-01-28 06:21:32 +00:00
|
|
|
try!(self.head(""));
|
|
|
|
try!(self.bopen());
|
2015-11-03 16:39:51 +00:00
|
|
|
}
|
2015-09-20 20:15:37 +00:00
|
|
|
}
|
|
|
|
try!(self.print_tts(&m.node.tts));
|
|
|
|
match delim {
|
|
|
|
token::Paren => self.pclose(),
|
|
|
|
token::Bracket => word(&mut self.s, "]"),
|
|
|
|
token::Brace => self.bclose(m.span),
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2012-09-12 04:25:01 +00:00
|
|
|
}
|
2012-11-30 19:18:25 +00:00
|
|
|
|
2012-07-23 23:39:18 +00:00
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
fn print_call_post(&mut self, args: &[P<ast::Expr>]) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.popen());
|
|
|
|
try!(self.commasep_exprs(Inconsistent, args));
|
|
|
|
self.pclose()
|
|
|
|
}
|
|
|
|
|
2015-08-06 07:55:27 +00:00
|
|
|
pub fn check_expr_bin_needs_paren(&mut self, sub_expr: &ast::Expr,
|
|
|
|
binop: ast::BinOp) -> bool {
|
|
|
|
match sub_expr.node {
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Binary(ref sub_op, _, _) => {
|
2015-10-15 12:51:30 +00:00
|
|
|
if AssocOp::from_ast_binop(sub_op.node).precedence() <
|
|
|
|
AssocOp::from_ast_binop(binop.node).precedence() {
|
2015-08-06 07:55:27 +00:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_expr_maybe_paren(&mut self, expr: &ast::Expr) -> io::Result<()> {
|
2014-04-16 20:33:58 +00:00
|
|
|
let needs_par = needs_parentheses(expr);
|
|
|
|
if needs_par {
|
|
|
|
try!(self.popen());
|
|
|
|
}
|
|
|
|
try!(self.print_expr(expr));
|
|
|
|
if needs_par {
|
|
|
|
try!(self.pclose());
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2015-09-24 15:00:08 +00:00
|
|
|
fn print_expr_in_place(&mut self,
|
|
|
|
place: &ast::Expr,
|
|
|
|
expr: &ast::Expr) -> io::Result<()> {
|
2015-10-15 12:51:30 +00:00
|
|
|
try!(self.print_expr_maybe_paren(place));
|
2015-09-24 15:00:08 +00:00
|
|
|
try!(space(&mut self.s));
|
2015-10-15 12:51:30 +00:00
|
|
|
try!(self.word_space("<-"));
|
|
|
|
self.print_expr_maybe_paren(expr)
|
2015-01-14 05:16:56 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 16:39:51 +00:00
|
|
|
fn print_expr_vec(&mut self, exprs: &[P<ast::Expr>],
|
|
|
|
attrs: &[Attribute]) -> io::Result<()> {
|
|
|
|
try!(self.ibox(INDENT_UNIT));
|
2015-01-14 05:16:56 +00:00
|
|
|
try!(word(&mut self.s, "["));
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_inner_attributes_inline(attrs));
|
2015-02-18 19:48:57 +00:00
|
|
|
try!(self.commasep_exprs(Inconsistent, &exprs[..]));
|
2015-01-14 05:16:56 +00:00
|
|
|
try!(word(&mut self.s, "]"));
|
|
|
|
self.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_expr_repeat(&mut self,
|
|
|
|
element: &ast::Expr,
|
2015-11-03 16:39:51 +00:00
|
|
|
count: &ast::Expr,
|
|
|
|
attrs: &[Attribute]) -> io::Result<()> {
|
|
|
|
try!(self.ibox(INDENT_UNIT));
|
2015-01-14 05:16:56 +00:00
|
|
|
try!(word(&mut self.s, "["));
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_inner_attributes_inline(attrs));
|
2015-01-14 05:16:56 +00:00
|
|
|
try!(self.print_expr(element));
|
|
|
|
try!(self.word_space(";"));
|
|
|
|
try!(self.print_expr(count));
|
|
|
|
try!(word(&mut self.s, "]"));
|
|
|
|
self.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_expr_struct(&mut self,
|
|
|
|
path: &ast::Path,
|
|
|
|
fields: &[ast::Field],
|
2015-11-03 16:39:51 +00:00
|
|
|
wth: &Option<P<ast::Expr>>,
|
|
|
|
attrs: &[Attribute]) -> io::Result<()> {
|
2015-01-31 19:20:24 +00:00
|
|
|
try!(self.print_path(path, true, 0));
|
2015-09-10 19:46:52 +00:00
|
|
|
try!(word(&mut self.s, "{"));
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_inner_attributes_inline(attrs));
|
2015-09-10 19:46:52 +00:00
|
|
|
try!(self.commasep_cmnt(
|
|
|
|
Consistent,
|
|
|
|
&fields[..],
|
|
|
|
|s, field| {
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(s.ibox(INDENT_UNIT));
|
2015-09-10 19:46:52 +00:00
|
|
|
try!(s.print_ident(field.ident.node));
|
|
|
|
try!(s.word_space(":"));
|
|
|
|
try!(s.print_expr(&*field.expr));
|
|
|
|
s.end()
|
|
|
|
},
|
|
|
|
|f| f.span));
|
|
|
|
match *wth {
|
|
|
|
Some(ref expr) => {
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.ibox(INDENT_UNIT));
|
2015-09-10 19:46:52 +00:00
|
|
|
if !fields.is_empty() {
|
|
|
|
try!(word(&mut self.s, ","));
|
|
|
|
try!(space(&mut self.s));
|
2015-01-14 05:16:56 +00:00
|
|
|
}
|
2015-09-10 19:46:52 +00:00
|
|
|
try!(word(&mut self.s, ".."));
|
|
|
|
try!(self.print_expr(&**expr));
|
|
|
|
try!(self.end());
|
|
|
|
}
|
|
|
|
_ => if !fields.is_empty() {
|
|
|
|
try!(word(&mut self.s, ","))
|
2015-01-14 05:16:56 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-10 19:46:52 +00:00
|
|
|
try!(word(&mut self.s, "}"));
|
2015-01-14 05:16:56 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2015-11-03 16:39:51 +00:00
|
|
|
fn print_expr_tup(&mut self, exprs: &[P<ast::Expr>],
|
|
|
|
attrs: &[Attribute]) -> io::Result<()> {
|
2015-01-14 05:16:56 +00:00
|
|
|
try!(self.popen());
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_inner_attributes_inline(attrs));
|
2015-02-18 19:48:57 +00:00
|
|
|
try!(self.commasep_exprs(Inconsistent, &exprs[..]));
|
2015-01-14 05:16:56 +00:00
|
|
|
if exprs.len() == 1 {
|
|
|
|
try!(word(&mut self.s, ","));
|
|
|
|
}
|
|
|
|
self.pclose()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_expr_call(&mut self,
|
|
|
|
func: &ast::Expr,
|
2015-02-27 05:00:43 +00:00
|
|
|
args: &[P<ast::Expr>]) -> io::Result<()> {
|
2015-01-14 05:16:56 +00:00
|
|
|
try!(self.print_expr_maybe_paren(func));
|
|
|
|
self.print_call_post(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_expr_method_call(&mut self,
|
|
|
|
ident: ast::SpannedIdent,
|
|
|
|
tys: &[P<ast::Ty>],
|
2015-02-27 05:00:43 +00:00
|
|
|
args: &[P<ast::Expr>]) -> io::Result<()> {
|
2015-01-18 00:15:52 +00:00
|
|
|
let base_args = &args[1..];
|
2015-01-14 05:16:56 +00:00
|
|
|
try!(self.print_expr(&*args[0]));
|
|
|
|
try!(word(&mut self.s, "."));
|
|
|
|
try!(self.print_ident(ident.node));
|
2015-03-24 23:54:09 +00:00
|
|
|
if !tys.is_empty() {
|
2015-01-14 05:16:56 +00:00
|
|
|
try!(word(&mut self.s, "::<"));
|
|
|
|
try!(self.commasep(Inconsistent, tys,
|
|
|
|
|s, ty| s.print_type(&**ty)));
|
|
|
|
try!(word(&mut self.s, ">"));
|
|
|
|
}
|
|
|
|
self.print_call_post(base_args)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_expr_binary(&mut self,
|
|
|
|
op: ast::BinOp,
|
|
|
|
lhs: &ast::Expr,
|
2015-02-27 05:00:43 +00:00
|
|
|
rhs: &ast::Expr) -> io::Result<()> {
|
2015-08-06 07:55:27 +00:00
|
|
|
if self.check_expr_bin_needs_paren(lhs, op) {
|
|
|
|
try!(self.print_expr_maybe_paren(lhs));
|
|
|
|
} else {
|
|
|
|
try!(self.print_expr(lhs));
|
|
|
|
}
|
2015-01-14 05:16:56 +00:00
|
|
|
try!(space(&mut self.s));
|
2015-11-28 19:02:07 +00:00
|
|
|
try!(self.word_space(op.node.to_string()));
|
2015-08-06 07:55:27 +00:00
|
|
|
if self.check_expr_bin_needs_paren(rhs, op) {
|
|
|
|
self.print_expr_maybe_paren(rhs)
|
|
|
|
} else {
|
|
|
|
self.print_expr(rhs)
|
|
|
|
}
|
2015-01-14 05:16:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn print_expr_unary(&mut self,
|
|
|
|
op: ast::UnOp,
|
2015-02-27 05:00:43 +00:00
|
|
|
expr: &ast::Expr) -> io::Result<()> {
|
2015-11-28 19:02:07 +00:00
|
|
|
try!(word(&mut self.s, ast::UnOp::to_string(op)));
|
2015-01-14 05:16:56 +00:00
|
|
|
self.print_expr_maybe_paren(expr)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_expr_addr_of(&mut self,
|
|
|
|
mutability: ast::Mutability,
|
2015-02-27 05:00:43 +00:00
|
|
|
expr: &ast::Expr) -> io::Result<()> {
|
2015-01-14 05:16:56 +00:00
|
|
|
try!(word(&mut self.s, "&"));
|
|
|
|
try!(self.print_mutability(mutability));
|
|
|
|
self.print_expr_maybe_paren(expr)
|
|
|
|
}
|
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_expr(&mut self, expr: &ast::Expr) -> io::Result<()> {
|
2015-11-03 16:39:51 +00:00
|
|
|
self.print_expr_outer_attr_style(expr, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_expr_outer_attr_style(&mut self,
|
|
|
|
expr: &ast::Expr,
|
|
|
|
is_inline: bool) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.maybe_print_comment(expr.span.lo));
|
2015-11-03 16:39:51 +00:00
|
|
|
|
2015-11-15 16:17:50 +00:00
|
|
|
let attrs = expr.attrs.as_attr_slice();
|
2015-11-03 16:39:51 +00:00
|
|
|
if is_inline {
|
|
|
|
try!(self.print_outer_attributes_inline(attrs));
|
|
|
|
} else {
|
|
|
|
try!(self.print_outer_attributes(attrs));
|
|
|
|
}
|
|
|
|
|
|
|
|
try!(self.ibox(INDENT_UNIT));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.ann.pre(self, NodeExpr(expr)));
|
|
|
|
match expr.node {
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Box(ref expr) => {
|
2015-09-24 15:00:08 +00:00
|
|
|
try!(self.word_space("box"));
|
|
|
|
try!(self.print_expr(expr));
|
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::InPlace(ref place, ref expr) => {
|
2015-09-24 15:00:08 +00:00
|
|
|
try!(self.print_expr_in_place(place, expr));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Vec(ref exprs) => {
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_expr_vec(&exprs[..], attrs));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Repeat(ref element, ref count) => {
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_expr_repeat(&**element, &**count, attrs));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Struct(ref path, ref fields, ref wth) => {
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_expr_struct(path, &fields[..], wth, attrs));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Tup(ref exprs) => {
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_expr_tup(&exprs[..], attrs));
|
2012-07-10 17:37:05 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Call(ref func, ref args) => {
|
2015-02-18 19:48:57 +00:00
|
|
|
try!(self.print_expr_call(&**func, &args[..]));
|
2012-08-09 23:31:47 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::MethodCall(ident, ref tys, ref args) => {
|
2015-02-18 19:48:57 +00:00
|
|
|
try!(self.print_expr_method_call(ident, &tys[..], &args[..]));
|
2013-10-28 22:22:49 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Binary(op, ref lhs, ref rhs) => {
|
2015-01-14 05:16:56 +00:00
|
|
|
try!(self.print_expr_binary(op, &**lhs, &**rhs));
|
2013-10-28 22:22:49 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Unary(op, ref expr) => {
|
2015-01-14 05:16:56 +00:00
|
|
|
try!(self.print_expr_unary(op, &**expr));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::AddrOf(m, ref expr) => {
|
2015-01-14 05:16:56 +00:00
|
|
|
try!(self.print_expr_addr_of(m, &**expr));
|
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Lit(ref lit) => {
|
2015-01-14 05:16:56 +00:00
|
|
|
try!(self.print_literal(&**lit));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Cast(ref expr, ref ty) => {
|
|
|
|
if let ast::ExprKind::Cast(..) = expr.node {
|
2015-08-09 16:45:57 +00:00
|
|
|
try!(self.print_expr(&**expr));
|
|
|
|
} else {
|
|
|
|
try!(self.print_expr_maybe_paren(&**expr));
|
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space("as"));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_type(&**ty));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Type(ref expr, ref ty) => {
|
2015-02-01 07:59:46 +00:00
|
|
|
try!(self.print_expr(&**expr));
|
|
|
|
try!(self.word_space(":"));
|
|
|
|
try!(self.print_type(&**ty));
|
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::If(ref test, ref blk, ref elseopt) => {
|
2014-08-25 01:04:29 +00:00
|
|
|
try!(self.print_if(&**test, &**blk, elseopt.as_ref().map(|e| &**e)));
|
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::IfLet(ref pat, ref expr, ref blk, ref elseopt) => {
|
2014-08-25 01:04:29 +00:00
|
|
|
try!(self.print_if_let(&**pat, &**expr, &** blk, elseopt.as_ref().map(|e| &**e)));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::While(ref test, ref blk, opt_ident) => {
|
2015-01-31 17:20:46 +00:00
|
|
|
if let Some(ident) = opt_ident {
|
|
|
|
try!(self.print_ident(ident));
|
2014-07-26 00:12:51 +00:00
|
|
|
try!(self.word_space(":"));
|
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.head("while"));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_expr(&**test));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(space(&mut self.s));
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_block_with_attrs(&**blk, attrs));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::WhileLet(ref pat, ref expr, ref blk, opt_ident) => {
|
2015-01-31 17:20:46 +00:00
|
|
|
if let Some(ident) = opt_ident {
|
|
|
|
try!(self.print_ident(ident));
|
2014-10-03 02:45:46 +00:00
|
|
|
try!(self.word_space(":"));
|
|
|
|
}
|
|
|
|
try!(self.head("while let"));
|
|
|
|
try!(self.print_pat(&**pat));
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space("="));
|
|
|
|
try!(self.print_expr(&**expr));
|
|
|
|
try!(space(&mut self.s));
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_block_with_attrs(&**blk, attrs));
|
2014-10-03 02:45:46 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::ForLoop(ref pat, ref iter, ref blk, opt_ident) => {
|
2015-01-31 17:20:46 +00:00
|
|
|
if let Some(ident) = opt_ident {
|
|
|
|
try!(self.print_ident(ident));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.word_space(":"));
|
|
|
|
}
|
|
|
|
try!(self.head("for"));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_pat(&**pat));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space("in"));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_expr(&**iter));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(space(&mut self.s));
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_block_with_attrs(&**blk, attrs));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Loop(ref blk, opt_ident) => {
|
2015-01-31 17:20:46 +00:00
|
|
|
if let Some(ident) = opt_ident {
|
|
|
|
try!(self.print_ident(ident));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.word_space(":"));
|
|
|
|
}
|
|
|
|
try!(self.head("loop"));
|
|
|
|
try!(space(&mut self.s));
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_block_with_attrs(&**blk, attrs));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Match(ref expr, ref arms) => {
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.cbox(INDENT_UNIT));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.ibox(4));
|
|
|
|
try!(self.word_nbsp("match"));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_expr(&**expr));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.bopen());
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_inner_attributes_no_trailing_hardbreak(attrs));
|
2015-01-31 17:20:46 +00:00
|
|
|
for arm in arms {
|
2014-07-28 01:05:07 +00:00
|
|
|
try!(self.print_arm(arm));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.bclose_(expr.span, INDENT_UNIT));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Closure(capture_clause, ref decl, ref body) => {
|
2014-07-23 19:43:29 +00:00
|
|
|
try!(self.print_capture_clause(capture_clause));
|
|
|
|
|
2015-02-03 16:34:05 +00:00
|
|
|
try!(self.print_fn_block_args(&**decl));
|
2014-05-29 05:26:56 +00:00
|
|
|
try!(space(&mut self.s));
|
2014-04-15 07:58:50 +00:00
|
|
|
|
2015-03-18 13:22:38 +00:00
|
|
|
let default_return = match decl.output {
|
2016-02-08 14:04:11 +00:00
|
|
|
ast::FunctionRetTy::Default(..) => true,
|
2015-03-18 13:22:38 +00:00
|
|
|
_ => false
|
|
|
|
};
|
|
|
|
|
|
|
|
if !default_return || !body.stmts.is_empty() || body.expr.is_none() {
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_block_unclosed(&**body));
|
2014-04-15 07:58:50 +00:00
|
|
|
} else {
|
|
|
|
// we extract the block, so as not to create another set of boxes
|
2015-11-03 16:39:51 +00:00
|
|
|
let i_expr = body.expr.as_ref().unwrap();
|
|
|
|
match i_expr.node {
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Block(ref blk) => {
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_block_unclosed_with_attrs(
|
|
|
|
&**blk,
|
2015-11-15 16:17:50 +00:00
|
|
|
i_expr.attrs.as_attr_slice()));
|
2014-04-15 07:58:50 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// this is a bare expression
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_expr(&**i_expr));
|
2014-04-15 07:58:50 +00:00
|
|
|
try!(self.end()); // need to close a box
|
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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.
|
|
|
|
try!(self.ibox(0));
|
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Block(ref blk) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
// containing cbox, will be closed by print-block at }
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.cbox(INDENT_UNIT));
|
2014-03-16 18:58:11 +00:00
|
|
|
// head-box, will be closed by print-block after {
|
2015-01-28 01:01:48 +00:00
|
|
|
try!(self.ibox(0));
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_block_with_attrs(&**blk, attrs));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Assign(ref lhs, ref rhs) => {
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_expr(&**lhs));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space("="));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_expr(&**rhs));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::AssignOp(op, ref lhs, ref rhs) => {
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_expr(&**lhs));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(space(&mut self.s));
|
2015-11-28 19:02:07 +00:00
|
|
|
try!(word(&mut self.s, op.node.to_string()));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.word_space("="));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_expr(&**rhs));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Field(ref expr, id) => {
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_expr(&**expr));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "."));
|
2014-06-13 21:56:42 +00:00
|
|
|
try!(self.print_ident(id.node));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::TupField(ref expr, id) => {
|
2014-08-10 03:54:33 +00:00
|
|
|
try!(self.print_expr(&**expr));
|
|
|
|
try!(word(&mut self.s, "."));
|
2015-01-17 23:49:08 +00:00
|
|
|
try!(self.print_usize(id.node));
|
2014-08-10 03:54:33 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Index(ref expr, ref index) => {
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_expr(&**expr));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "["));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_expr(&**index));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "]"));
|
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Range(ref start, ref end) => {
|
2014-11-29 21:41:21 +00:00
|
|
|
if let &Some(ref e) = start {
|
|
|
|
try!(self.print_expr(&**e));
|
2014-09-15 08:48:58 +00:00
|
|
|
}
|
2015-02-05 16:58:30 +00:00
|
|
|
try!(word(&mut self.s, ".."));
|
2014-11-29 21:41:21 +00:00
|
|
|
if let &Some(ref e) = end {
|
|
|
|
try!(self.print_expr(&**e));
|
2014-09-15 08:48:58 +00:00
|
|
|
}
|
2014-12-13 05:41:02 +00:00
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Path(None, ref path) => {
|
2015-02-17 17:29:13 +00:00
|
|
|
try!(self.print_path(path, true, 0))
|
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Path(Some(ref qself), ref path) => {
|
2015-02-17 17:29:13 +00:00
|
|
|
try!(self.print_qpath(path, qself, true))
|
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Break(opt_ident) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "break"));
|
|
|
|
try!(space(&mut self.s));
|
2015-01-31 17:20:46 +00:00
|
|
|
if let Some(ident) = opt_ident {
|
2015-09-02 19:29:41 +00:00
|
|
|
try!(self.print_ident(ident.node));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(space(&mut self.s));
|
|
|
|
}
|
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Again(opt_ident) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "continue"));
|
|
|
|
try!(space(&mut self.s));
|
2015-01-31 17:20:46 +00:00
|
|
|
if let Some(ident) = opt_ident {
|
2015-09-02 19:29:41 +00:00
|
|
|
try!(self.print_ident(ident.node));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(space(&mut self.s))
|
|
|
|
}
|
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Ret(ref result) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "return"));
|
2014-05-16 07:16:13 +00:00
|
|
|
match *result {
|
|
|
|
Some(ref expr) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, " "));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_expr(&**expr));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::InlineAsm(ref a) => {
|
2014-12-17 13:02:50 +00:00
|
|
|
try!(word(&mut self.s, "asm!"));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.popen());
|
2015-02-04 20:48:12 +00:00
|
|
|
try!(self.print_string(&a.asm, a.asm_str_style));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.word_space(":"));
|
2014-04-17 08:35:40 +00:00
|
|
|
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(self.commasep(Inconsistent, &a.outputs,
|
2015-12-05 08:18:24 +00:00
|
|
|
|s, out| {
|
|
|
|
match out.constraint.slice_shift_char() {
|
|
|
|
Some(('=', operand)) if out.is_rw => {
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(s.print_string(&format!("+{}", operand),
|
2014-08-19 19:39:26 +00:00
|
|
|
ast::CookedStr))
|
|
|
|
}
|
2015-12-05 08:18:24 +00:00
|
|
|
_ => try!(s.print_string(&out.constraint, ast::CookedStr))
|
2014-08-19 19:39:26 +00:00
|
|
|
}
|
2014-04-17 08:35:40 +00:00
|
|
|
try!(s.popen());
|
2015-12-05 08:18:24 +00:00
|
|
|
try!(s.print_expr(&*out.expr));
|
2014-04-17 08:35:40 +00:00
|
|
|
try!(s.pclose());
|
|
|
|
Ok(())
|
|
|
|
}));
|
|
|
|
try!(space(&mut self.s));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.word_space(":"));
|
2014-04-17 08:35:40 +00:00
|
|
|
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(self.commasep(Inconsistent, &a.inputs,
|
2014-05-16 07:16:13 +00:00
|
|
|
|s, &(ref co, ref o)| {
|
2015-02-04 20:48:12 +00:00
|
|
|
try!(s.print_string(&co, ast::CookedStr));
|
2014-04-17 08:35:40 +00:00
|
|
|
try!(s.popen());
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(s.print_expr(&**o));
|
2014-04-17 08:35:40 +00:00
|
|
|
try!(s.pclose());
|
|
|
|
Ok(())
|
|
|
|
}));
|
|
|
|
try!(space(&mut self.s));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.word_space(":"));
|
2014-04-17 08:35:40 +00:00
|
|
|
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(self.commasep(Inconsistent, &a.clobbers,
|
2014-11-30 02:56:31 +00:00
|
|
|
|s, co| {
|
2015-02-04 20:48:12 +00:00
|
|
|
try!(s.print_string(&co, ast::CookedStr));
|
2014-11-30 02:56:31 +00:00
|
|
|
Ok(())
|
|
|
|
}));
|
2014-12-17 13:02:50 +00:00
|
|
|
|
|
|
|
let mut options = vec!();
|
|
|
|
if a.volatile {
|
|
|
|
options.push("volatile");
|
|
|
|
}
|
|
|
|
if a.alignstack {
|
|
|
|
options.push("alignstack");
|
|
|
|
}
|
2015-09-21 09:45:04 +00:00
|
|
|
if a.dialect == ast::AsmDialect::Intel {
|
2014-12-17 13:02:50 +00:00
|
|
|
options.push("intel");
|
|
|
|
}
|
|
|
|
|
2015-03-24 23:54:09 +00:00
|
|
|
if !options.is_empty() {
|
2014-12-17 13:02:50 +00:00
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space(":"));
|
|
|
|
try!(self.commasep(Inconsistent, &*options,
|
|
|
|
|s, &co| {
|
|
|
|
try!(s.print_string(co, ast::CookedStr));
|
|
|
|
Ok(())
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.pclose());
|
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Mac(ref m) => try!(self.print_mac(m, token::Paren)),
|
|
|
|
ast::ExprKind::Paren(ref e) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.popen());
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_inner_attributes_inline(attrs));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_expr(&**e));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.pclose());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try!(self.ann.post(self, NodeExpr(expr)));
|
|
|
|
self.end()
|
2011-08-10 19:51:50 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_local_decl(&mut self, loc: &ast::Local) -> io::Result<()> {
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_pat(&*loc.pat));
|
2015-01-02 11:55:31 +00:00
|
|
|
if let Some(ref ty) = loc.ty {
|
|
|
|
try!(self.word_space(":"));
|
|
|
|
try!(self.print_type(&**ty));
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
2015-01-02 11:55:31 +00:00
|
|
|
Ok(())
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
2011-03-24 15:33:20 +00:00
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_decl(&mut self, decl: &ast::Decl) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.maybe_print_comment(decl.span.lo));
|
|
|
|
match decl.node {
|
2016-02-08 14:34:47 +00:00
|
|
|
ast::DeclKind::Local(ref loc) => {
|
2015-11-15 16:17:50 +00:00
|
|
|
try!(self.print_outer_attributes(loc.attrs.as_attr_slice()));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.space_if_not_bol());
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.ibox(INDENT_UNIT));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.word_nbsp("let"));
|
2013-09-10 19:01:44 +00:00
|
|
|
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.ibox(INDENT_UNIT));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_local_decl(&**loc));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.end());
|
2014-11-29 21:41:21 +00:00
|
|
|
if let Some(ref init) = loc.init {
|
|
|
|
try!(self.nbsp());
|
|
|
|
try!(self.word_space("="));
|
|
|
|
try!(self.print_expr(&**init));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
self.end()
|
|
|
|
}
|
2016-02-08 14:34:47 +00:00
|
|
|
ast::DeclKind::Item(ref item) => self.print_item(&**item)
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-30 01:39:21 +00:00
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_ident(&mut self, ident: ast::Ident) -> io::Result<()> {
|
2015-07-28 16:07:20 +00:00
|
|
|
try!(word(&mut self.s, &ident.name.as_str()));
|
2014-08-11 12:01:37 +00:00
|
|
|
self.ann.post(self, NodeIdent(&ident))
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_usize(&mut self, i: usize) -> io::Result<()> {
|
2015-02-18 23:58:07 +00:00
|
|
|
word(&mut self.s, &i.to_string())
|
2014-08-10 03:54:33 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_name(&mut self, name: ast::Name) -> io::Result<()> {
|
2015-07-28 16:07:20 +00:00
|
|
|
try!(word(&mut self.s, &name.as_str()));
|
2014-08-11 12:01:37 +00:00
|
|
|
self.ann.post(self, NodeName(&name))
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2013-08-07 16:47:28 +00:00
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
pub fn print_for_decl(&mut self, loc: &ast::Local,
|
2015-02-27 05:00:43 +00:00
|
|
|
coll: &ast::Expr) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.print_local_decl(loc));
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space("in"));
|
|
|
|
self.print_expr(coll)
|
|
|
|
}
|
2013-08-07 16:47:28 +00:00
|
|
|
|
2014-11-20 20:08:48 +00:00
|
|
|
fn print_path(&mut self,
|
|
|
|
path: &ast::Path,
|
2015-01-31 19:20:24 +00:00
|
|
|
colons_before_params: bool,
|
|
|
|
depth: usize)
|
2015-02-27 05:00:43 +00:00
|
|
|
-> io::Result<()>
|
2014-11-20 20:08:48 +00:00
|
|
|
{
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.maybe_print_comment(path.span.lo));
|
2013-08-07 16:47:28 +00:00
|
|
|
|
2015-01-31 19:20:24 +00:00
|
|
|
let mut first = !path.global;
|
|
|
|
for segment in &path.segments[..path.segments.len()-depth] {
|
2014-03-16 18:58:11 +00:00
|
|
|
if first {
|
|
|
|
first = false
|
|
|
|
} else {
|
|
|
|
try!(word(&mut self.s, "::"))
|
2013-08-07 16:47:28 +00:00
|
|
|
}
|
2013-02-28 00:41:02 +00:00
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.print_ident(segment.identifier));
|
|
|
|
|
2014-11-04 02:52:52 +00:00
|
|
|
try!(self.print_path_parameters(&segment.parameters, colons_before_params));
|
|
|
|
}
|
|
|
|
|
2014-11-20 20:08:48 +00:00
|
|
|
Ok(())
|
2014-11-04 02:52:52 +00:00
|
|
|
}
|
|
|
|
|
2015-01-13 04:02:56 +00:00
|
|
|
fn print_qpath(&mut self,
|
2015-02-17 17:29:13 +00:00
|
|
|
path: &ast::Path,
|
|
|
|
qself: &ast::QSelf,
|
2015-01-13 04:02:56 +00:00
|
|
|
colons_before_params: bool)
|
2015-02-27 05:00:43 +00:00
|
|
|
-> io::Result<()>
|
2015-01-13 04:02:56 +00:00
|
|
|
{
|
|
|
|
try!(word(&mut self.s, "<"));
|
2015-02-17 17:29:13 +00:00
|
|
|
try!(self.print_type(&qself.ty));
|
2015-02-24 10:12:14 +00:00
|
|
|
if qself.position > 0 {
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space("as"));
|
|
|
|
let depth = path.segments.len() - qself.position;
|
|
|
|
try!(self.print_path(&path, false, depth));
|
|
|
|
}
|
2015-01-13 04:02:56 +00:00
|
|
|
try!(word(&mut self.s, ">"));
|
|
|
|
try!(word(&mut self.s, "::"));
|
2015-02-17 17:29:13 +00:00
|
|
|
let item_segment = path.segments.last().unwrap();
|
2015-01-31 19:20:24 +00:00
|
|
|
try!(self.print_ident(item_segment.identifier));
|
|
|
|
self.print_path_parameters(&item_segment.parameters, colons_before_params)
|
2015-01-13 04:02:56 +00:00
|
|
|
}
|
|
|
|
|
2014-11-04 02:52:52 +00:00
|
|
|
fn print_path_parameters(&mut self,
|
|
|
|
parameters: &ast::PathParameters,
|
|
|
|
colons_before_params: bool)
|
2015-02-27 05:00:43 +00:00
|
|
|
-> io::Result<()>
|
2014-11-04 02:52:52 +00:00
|
|
|
{
|
|
|
|
if parameters.is_empty() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if colons_before_params {
|
|
|
|
try!(word(&mut self.s, "::"))
|
|
|
|
}
|
|
|
|
|
|
|
|
match *parameters {
|
2015-12-22 15:56:13 +00:00
|
|
|
ast::PathParameters::AngleBracketed(ref data) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "<"));
|
|
|
|
|
|
|
|
let mut comma = false;
|
2015-01-31 17:20:46 +00:00
|
|
|
for lifetime in &data.lifetimes {
|
2014-03-16 18:58:11 +00:00
|
|
|
if comma {
|
|
|
|
try!(self.word_space(","))
|
|
|
|
}
|
|
|
|
try!(self.print_lifetime(lifetime));
|
|
|
|
comma = true;
|
2013-11-09 03:25:22 +00:00
|
|
|
}
|
2013-02-28 00:41:02 +00:00
|
|
|
|
2014-11-04 02:52:52 +00:00
|
|
|
if !data.types.is_empty() {
|
2014-03-16 18:58:11 +00:00
|
|
|
if comma {
|
|
|
|
try!(self.word_space(","))
|
|
|
|
}
|
|
|
|
try!(self.commasep(
|
|
|
|
Inconsistent,
|
2015-02-18 23:58:07 +00:00
|
|
|
&data.types,
|
2014-09-13 16:06:01 +00:00
|
|
|
|s, ty| s.print_type(&**ty)));
|
2014-11-29 04:08:30 +00:00
|
|
|
comma = true;
|
|
|
|
}
|
|
|
|
|
2015-06-11 12:56:07 +00:00
|
|
|
for binding in data.bindings.iter() {
|
2014-11-29 04:08:30 +00:00
|
|
|
if comma {
|
|
|
|
try!(self.word_space(","))
|
|
|
|
}
|
|
|
|
try!(self.print_ident(binding.ident));
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space("="));
|
|
|
|
try!(self.print_type(&*binding.ty));
|
|
|
|
comma = true;
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try!(word(&mut self.s, ">"))
|
|
|
|
}
|
2014-03-22 16:20:22 +00:00
|
|
|
|
2015-12-22 15:56:13 +00:00
|
|
|
ast::PathParameters::Parenthesized(ref data) => {
|
2014-11-04 02:52:52 +00:00
|
|
|
try!(word(&mut self.s, "("));
|
|
|
|
try!(self.commasep(
|
|
|
|
Inconsistent,
|
2015-02-18 23:58:07 +00:00
|
|
|
&data.inputs,
|
2014-11-04 02:52:52 +00:00
|
|
|
|s, ty| s.print_type(&**ty)));
|
|
|
|
try!(word(&mut self.s, ")"));
|
|
|
|
|
|
|
|
match data.output {
|
|
|
|
None => { }
|
|
|
|
Some(ref ty) => {
|
2014-11-26 15:07:22 +00:00
|
|
|
try!(self.space_if_not_bol());
|
2014-11-04 02:52:52 +00:00
|
|
|
try!(self.word_space("->"));
|
|
|
|
try!(self.print_type(&**ty));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-22 16:20:22 +00:00
|
|
|
}
|
2014-11-04 02:52:52 +00:00
|
|
|
|
|
|
|
Ok(())
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_pat(&mut self, pat: &ast::Pat) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.maybe_print_comment(pat.span.lo));
|
|
|
|
try!(self.ann.pre(self, NodePat(pat)));
|
|
|
|
/* Pat isn't normalized, but the beauty of it
|
|
|
|
is that it doesn't matter */
|
|
|
|
match pat.node {
|
2015-10-31 00:44:43 +00:00
|
|
|
ast::PatWild => try!(word(&mut self.s, "_")),
|
2014-09-13 16:06:01 +00:00
|
|
|
ast::PatIdent(binding_mode, ref path1, ref sub) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
match binding_mode {
|
2015-12-18 13:23:01 +00:00
|
|
|
ast::BindingMode::ByRef(mutbl) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.word_nbsp("ref"));
|
|
|
|
try!(self.print_mutability(mutbl));
|
|
|
|
}
|
2016-02-09 16:44:47 +00:00
|
|
|
ast::BindingMode::ByValue(ast::Mutability::Immutable) => {}
|
|
|
|
ast::BindingMode::ByValue(ast::Mutability::Mutable) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.word_nbsp("mut"));
|
|
|
|
}
|
|
|
|
}
|
2014-07-01 01:02:14 +00:00
|
|
|
try!(self.print_ident(path1.node));
|
2014-09-13 16:06:01 +00:00
|
|
|
match *sub {
|
2014-05-16 07:16:13 +00:00
|
|
|
Some(ref p) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "@"));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_pat(&**p));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
None => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::PatEnum(ref path, ref args_) => {
|
2015-01-31 19:20:24 +00:00
|
|
|
try!(self.print_path(path, true, 0));
|
2014-03-16 18:58:11 +00:00
|
|
|
match *args_ {
|
|
|
|
None => try!(word(&mut self.s, "(..)")),
|
|
|
|
Some(ref args) => {
|
|
|
|
if !args.is_empty() {
|
|
|
|
try!(self.popen());
|
2015-02-18 19:48:57 +00:00
|
|
|
try!(self.commasep(Inconsistent, &args[..],
|
2014-05-16 07:16:13 +00:00
|
|
|
|s, p| s.print_pat(&**p)));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.pclose());
|
|
|
|
}
|
|
|
|
}
|
2013-11-08 03:25:39 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-25 16:53:28 +00:00
|
|
|
ast::PatQPath(ref qself, ref path) => {
|
|
|
|
try!(self.print_qpath(path, qself, false));
|
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
ast::PatStruct(ref path, ref fields, etc) => {
|
2015-01-31 19:20:24 +00:00
|
|
|
try!(self.print_path(path, true, 0));
|
2014-07-07 18:54:50 +00:00
|
|
|
try!(self.nbsp());
|
|
|
|
try!(self.word_space("{"));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.commasep_cmnt(
|
2015-02-18 19:48:57 +00:00
|
|
|
Consistent, &fields[..],
|
2014-03-16 18:58:11 +00:00
|
|
|
|s, f| {
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(s.cbox(INDENT_UNIT));
|
2014-10-27 07:11:26 +00:00
|
|
|
if !f.node.is_shorthand {
|
|
|
|
try!(s.print_ident(f.node.ident));
|
|
|
|
try!(s.word_nbsp(":"));
|
|
|
|
}
|
2014-10-06 00:36:53 +00:00
|
|
|
try!(s.print_pat(&*f.node.pat));
|
2014-03-16 18:58:11 +00:00
|
|
|
s.end()
|
|
|
|
},
|
2014-10-06 00:36:53 +00:00
|
|
|
|f| f.node.pat.span));
|
2014-03-16 18:58:11 +00:00
|
|
|
if etc {
|
2015-03-24 23:54:09 +00:00
|
|
|
if !fields.is_empty() { try!(self.word_space(",")); }
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, ".."));
|
|
|
|
}
|
2014-07-07 18:54:50 +00:00
|
|
|
try!(space(&mut self.s));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "}"));
|
|
|
|
}
|
|
|
|
ast::PatTup(ref elts) => {
|
|
|
|
try!(self.popen());
|
|
|
|
try!(self.commasep(Inconsistent,
|
2015-02-18 19:48:57 +00:00
|
|
|
&elts[..],
|
2014-05-16 07:16:13 +00:00
|
|
|
|s, p| s.print_pat(&**p)));
|
2014-03-16 18:58:11 +00:00
|
|
|
if elts.len() == 1 {
|
|
|
|
try!(word(&mut self.s, ","));
|
|
|
|
}
|
|
|
|
try!(self.pclose());
|
|
|
|
}
|
2014-05-16 07:16:13 +00:00
|
|
|
ast::PatBox(ref inner) => {
|
2014-05-06 01:56:44 +00:00
|
|
|
try!(word(&mut self.s, "box "));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_pat(&**inner));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2014-12-05 23:56:25 +00:00
|
|
|
ast::PatRegion(ref inner, mutbl) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "&"));
|
2016-02-09 16:44:47 +00:00
|
|
|
if mutbl == ast::Mutability::Mutable {
|
2014-12-05 23:56:25 +00:00
|
|
|
try!(word(&mut self.s, "mut "));
|
|
|
|
}
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_pat(&**inner));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2014-05-16 07:16:13 +00:00
|
|
|
ast::PatLit(ref e) => try!(self.print_expr(&**e)),
|
|
|
|
ast::PatRange(ref begin, ref end) => {
|
|
|
|
try!(self.print_expr(&**begin));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(space(&mut self.s));
|
2014-09-15 08:48:58 +00:00
|
|
|
try!(word(&mut self.s, "..."));
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_expr(&**end));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2014-09-13 16:06:01 +00:00
|
|
|
ast::PatVec(ref before, ref slice, ref after) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "["));
|
|
|
|
try!(self.commasep(Inconsistent,
|
2015-02-18 19:48:57 +00:00
|
|
|
&before[..],
|
2014-05-16 07:16:13 +00:00
|
|
|
|s, p| s.print_pat(&**p)));
|
2015-01-31 17:20:46 +00:00
|
|
|
if let Some(ref p) = *slice {
|
2014-03-16 18:58:11 +00:00
|
|
|
if !before.is_empty() { try!(self.word_space(",")); }
|
2015-10-31 00:44:43 +00:00
|
|
|
if p.node != ast::PatWild {
|
|
|
|
try!(self.print_pat(&**p));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2015-10-31 00:44:43 +00:00
|
|
|
try!(word(&mut self.s, ".."));
|
2014-03-16 18:58:11 +00:00
|
|
|
if !after.is_empty() { try!(self.word_space(",")); }
|
|
|
|
}
|
|
|
|
try!(self.commasep(Inconsistent,
|
2015-02-18 19:48:57 +00:00
|
|
|
&after[..],
|
2014-05-16 07:16:13 +00:00
|
|
|
|s, p| s.print_pat(&**p)));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "]"));
|
|
|
|
}
|
2014-11-14 17:18:10 +00:00
|
|
|
ast::PatMac(ref m) => try!(self.print_mac(m, token::Paren)),
|
2013-02-26 18:58:46 +00:00
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
self.ann.post(self, NodePat(pat))
|
2013-12-27 22:11:01 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
fn print_arm(&mut self, arm: &ast::Arm) -> io::Result<()> {
|
2014-07-28 01:05:07 +00:00
|
|
|
// I have no idea why this check is necessary, but here it
|
|
|
|
// is :(
|
|
|
|
if arm.attrs.is_empty() {
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
}
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.cbox(INDENT_UNIT));
|
2015-01-28 01:01:48 +00:00
|
|
|
try!(self.ibox(0));
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(self.print_outer_attributes(&arm.attrs));
|
2014-07-28 01:05:07 +00:00
|
|
|
let mut first = true;
|
2015-01-31 17:20:46 +00:00
|
|
|
for p in &arm.pats {
|
2014-07-28 01:05:07 +00:00
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space("|"));
|
|
|
|
}
|
|
|
|
try!(self.print_pat(&**p));
|
|
|
|
}
|
|
|
|
try!(space(&mut self.s));
|
2014-11-22 15:24:58 +00:00
|
|
|
if let Some(ref e) = arm.guard {
|
|
|
|
try!(self.word_space("if"));
|
|
|
|
try!(self.print_expr(&**e));
|
|
|
|
try!(space(&mut self.s));
|
2014-07-28 01:05:07 +00:00
|
|
|
}
|
|
|
|
try!(self.word_space("=>"));
|
|
|
|
|
|
|
|
match arm.body.node {
|
2016-02-08 15:05:05 +00:00
|
|
|
ast::ExprKind::Block(ref blk) => {
|
2014-07-28 01:05:07 +00:00
|
|
|
// the block will close the pattern's ibox
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.print_block_unclosed_indent(&**blk, INDENT_UNIT));
|
2014-11-22 15:24:58 +00:00
|
|
|
|
|
|
|
// If it is a user-provided unsafe block, print a comma after it
|
2016-02-08 11:44:45 +00:00
|
|
|
if let BlockCheckMode::Unsafe(ast::UserProvided) = blk.rules {
|
2014-11-22 15:24:58 +00:00
|
|
|
try!(word(&mut self.s, ","));
|
|
|
|
}
|
2014-07-28 01:05:07 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
try!(self.end()); // close the ibox for the pattern
|
|
|
|
try!(self.print_expr(&*arm.body));
|
|
|
|
try!(word(&mut self.s, ","));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.end() // close enclosing cbox
|
|
|
|
}
|
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
// Returns whether it printed anything
|
|
|
|
fn print_explicit_self(&mut self,
|
2016-02-08 14:43:56 +00:00
|
|
|
explicit_self: &ast::SelfKind,
|
2015-02-27 05:00:43 +00:00
|
|
|
mutbl: ast::Mutability) -> io::Result<bool> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.print_mutability(mutbl));
|
2014-09-13 16:06:01 +00:00
|
|
|
match *explicit_self {
|
2016-02-08 14:43:56 +00:00
|
|
|
ast::SelfKind::Static => { return Ok(false); }
|
|
|
|
ast::SelfKind::Value(_) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "self"));
|
|
|
|
}
|
2016-02-08 14:43:56 +00:00
|
|
|
ast::SelfKind::Region(ref lt, m, _) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "&"));
|
|
|
|
try!(self.print_opt_lifetime(lt));
|
|
|
|
try!(self.print_mutability(m));
|
|
|
|
try!(word(&mut self.s, "self"));
|
|
|
|
}
|
2016-02-08 14:43:56 +00:00
|
|
|
ast::SelfKind::Explicit(ref typ, _) => {
|
2014-05-06 23:37:32 +00:00
|
|
|
try!(word(&mut self.s, "self"));
|
|
|
|
try!(self.word_space(":"));
|
2014-07-07 23:35:15 +00:00
|
|
|
try!(self.print_type(&**typ));
|
2014-05-06 23:37:32 +00:00
|
|
|
}
|
2013-03-10 00:43:53 +00:00
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
return Ok(true);
|
2012-08-17 23:14:57 +00:00
|
|
|
}
|
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
pub fn print_fn(&mut self,
|
|
|
|
decl: &ast::FnDecl,
|
2015-03-11 06:38:27 +00:00
|
|
|
unsafety: ast::Unsafety,
|
2015-02-25 20:05:07 +00:00
|
|
|
constness: ast::Constness,
|
2014-04-02 08:19:41 +00:00
|
|
|
abi: abi::Abi,
|
2015-03-11 06:38:27 +00:00
|
|
|
name: Option<ast::Ident>,
|
2014-03-16 18:58:11 +00:00
|
|
|
generics: &ast::Generics,
|
2016-02-08 14:43:56 +00:00
|
|
|
opt_explicit_self: Option<&ast::SelfKind>,
|
2015-02-27 05:00:43 +00:00
|
|
|
vis: ast::Visibility) -> io::Result<()> {
|
2015-02-25 20:05:07 +00:00
|
|
|
try!(self.print_fn_header_info(unsafety, constness, abi, vis));
|
2015-03-11 06:38:27 +00:00
|
|
|
|
|
|
|
if let Some(name) = name {
|
|
|
|
try!(self.nbsp());
|
|
|
|
try!(self.print_ident(name));
|
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.print_generics(generics));
|
2014-11-14 17:18:10 +00:00
|
|
|
try!(self.print_fn_args_and_ret(decl, opt_explicit_self));
|
2015-03-14 04:54:29 +00:00
|
|
|
self.print_where_clause(&generics.where_clause)
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_fn_args(&mut self, decl: &ast::FnDecl,
|
2016-02-08 14:43:56 +00:00
|
|
|
opt_explicit_self: Option<&ast::SelfKind>,
|
2016-01-21 18:44:36 +00:00
|
|
|
is_closure: bool) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
// It is unfortunate to duplicate the commasep logic, but we want the
|
|
|
|
// self type and the args all in the same box.
|
2015-01-28 01:01:48 +00:00
|
|
|
try!(self.rbox(0, Inconsistent));
|
2014-03-16 18:58:11 +00:00
|
|
|
let mut first = true;
|
2015-01-31 17:20:46 +00:00
|
|
|
if let Some(explicit_self) = opt_explicit_self {
|
2015-11-17 14:24:49 +00:00
|
|
|
let m = match *explicit_self {
|
2016-02-09 16:44:47 +00:00
|
|
|
ast::SelfKind::Static => ast::Mutability::Immutable,
|
2014-10-15 06:05:01 +00:00
|
|
|
_ => match decl.inputs[0].pat.node {
|
2015-12-18 13:23:01 +00:00
|
|
|
ast::PatIdent(ast::BindingMode::ByValue(m), _, _) => m,
|
2016-02-09 16:44:47 +00:00
|
|
|
_ => ast::Mutability::Immutable
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
first = !try!(self.print_explicit_self(explicit_self, m));
|
|
|
|
}
|
2011-06-14 13:20:04 +00:00
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
// HACK(eddyb) ignore the separately printed self argument.
|
|
|
|
let args = if first {
|
2015-02-20 19:08:14 +00:00
|
|
|
&decl.inputs[..]
|
2014-03-16 18:58:11 +00:00
|
|
|
} else {
|
2015-01-18 00:15:52 +00:00
|
|
|
&decl.inputs[1..]
|
2014-01-27 12:18:36 +00:00
|
|
|
};
|
2012-08-17 23:14:57 +00:00
|
|
|
|
2015-01-31 17:20:46 +00:00
|
|
|
for arg in args {
|
2014-03-16 18:58:11 +00:00
|
|
|
if first { first = false; } else { try!(self.word_space(",")); }
|
2016-01-21 18:44:36 +00:00
|
|
|
try!(self.print_arg(arg, is_closure));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2014-01-27 12:18:36 +00:00
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
self.end()
|
2011-12-30 21:32:42 +00:00
|
|
|
}
|
2012-08-17 23:14:57 +00:00
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
pub fn print_fn_args_and_ret(&mut self, decl: &ast::FnDecl,
|
2016-02-08 14:43:56 +00:00
|
|
|
opt_explicit_self: Option<&ast::SelfKind>)
|
2015-02-27 05:00:43 +00:00
|
|
|
-> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.popen());
|
2016-01-21 18:44:36 +00:00
|
|
|
try!(self.print_fn_args(decl, opt_explicit_self, false));
|
2014-03-16 18:58:11 +00:00
|
|
|
if decl.variadic {
|
|
|
|
try!(word(&mut self.s, ", ..."));
|
|
|
|
}
|
|
|
|
try!(self.pclose());
|
2011-12-30 21:32:42 +00:00
|
|
|
|
2014-11-09 15:14:15 +00:00
|
|
|
self.print_fn_output(decl)
|
2013-10-25 05:56:34 +00:00
|
|
|
}
|
2012-02-22 10:16:25 +00:00
|
|
|
|
2014-07-30 05:08:39 +00:00
|
|
|
pub fn print_fn_block_args(
|
|
|
|
&mut self,
|
2015-02-03 16:34:05 +00:00
|
|
|
decl: &ast::FnDecl)
|
2015-02-27 05:00:43 +00:00
|
|
|
-> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "|"));
|
2016-01-21 18:44:36 +00:00
|
|
|
try!(self.print_fn_args(decl, None, true));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "|"));
|
|
|
|
|
2016-02-08 14:04:11 +00:00
|
|
|
if let ast::FunctionRetTy::Default(..) = decl.output {
|
2015-01-18 13:49:19 +00:00
|
|
|
return Ok(());
|
2012-09-08 01:53:14 +00:00
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
|
2014-11-09 15:14:15 +00:00
|
|
|
try!(self.space_if_not_bol());
|
|
|
|
try!(self.word_space("->"));
|
|
|
|
match decl.output {
|
2016-02-08 14:04:11 +00:00
|
|
|
ast::FunctionRetTy::Ty(ref ty) => {
|
2014-11-09 15:14:15 +00:00
|
|
|
try!(self.print_type(&**ty));
|
|
|
|
self.maybe_print_comment(ty.span.lo)
|
|
|
|
}
|
2016-02-08 14:04:11 +00:00
|
|
|
ast::FunctionRetTy::Default(..) => unreachable!(),
|
|
|
|
ast::FunctionRetTy::None(span) => {
|
2014-11-09 15:14:15 +00:00
|
|
|
try!(self.word_nbsp("!"));
|
|
|
|
self.maybe_print_comment(span.lo)
|
|
|
|
}
|
|
|
|
}
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
|
2016-02-08 14:27:08 +00:00
|
|
|
pub fn print_capture_clause(&mut self, capture_clause: ast::CaptureBy)
|
2015-02-27 05:00:43 +00:00
|
|
|
-> io::Result<()> {
|
2014-07-23 19:43:29 +00:00
|
|
|
match capture_clause {
|
2016-02-08 14:27:08 +00:00
|
|
|
ast::CaptureBy::Value => self.word_space("move"),
|
|
|
|
ast::CaptureBy::Ref => Ok(()),
|
2014-07-23 19:43:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-02 16:47:11 +00:00
|
|
|
pub fn print_bounds(&mut self,
|
2014-08-28 01:46:52 +00:00
|
|
|
prefix: &str,
|
2014-11-20 20:05:29 +00:00
|
|
|
bounds: &[ast::TyParamBound])
|
2015-02-27 05:00:43 +00:00
|
|
|
-> io::Result<()> {
|
2014-08-28 01:46:52 +00:00
|
|
|
if !bounds.is_empty() {
|
|
|
|
try!(word(&mut self.s, prefix));
|
2014-03-16 18:58:11 +00:00
|
|
|
let mut first = true;
|
2015-01-31 17:20:46 +00:00
|
|
|
for bound in bounds {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.nbsp());
|
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
try!(self.word_space("+"));
|
|
|
|
}
|
2013-10-28 22:22:49 +00:00
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(match *bound {
|
2014-12-24 06:38:10 +00:00
|
|
|
TraitTyParamBound(ref tref, TraitBoundModifier::None) => {
|
|
|
|
self.print_poly_trait_ref(tref)
|
|
|
|
}
|
|
|
|
TraitTyParamBound(ref tref, TraitBoundModifier::Maybe) => {
|
|
|
|
try!(word(&mut self.s, "?"));
|
2014-11-07 11:53:45 +00:00
|
|
|
self.print_poly_trait_ref(tref)
|
2014-08-28 01:46:52 +00:00
|
|
|
}
|
|
|
|
RegionTyParamBound(ref lt) => {
|
|
|
|
self.print_lifetime(lt)
|
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Ok(())
|
2013-10-28 22:22:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
pub fn print_lifetime(&mut self,
|
2014-08-06 02:59:24 +00:00
|
|
|
lifetime: &ast::Lifetime)
|
2015-02-27 05:00:43 +00:00
|
|
|
-> io::Result<()>
|
2014-08-06 02:59:24 +00:00
|
|
|
{
|
2014-03-16 18:58:11 +00:00
|
|
|
self.print_name(lifetime.name)
|
|
|
|
}
|
2013-10-28 22:22:49 +00:00
|
|
|
|
2014-08-06 02:59:24 +00:00
|
|
|
pub fn print_lifetime_def(&mut self,
|
|
|
|
lifetime: &ast::LifetimeDef)
|
2015-02-27 05:00:43 +00:00
|
|
|
-> io::Result<()>
|
2014-08-06 02:59:24 +00:00
|
|
|
{
|
|
|
|
try!(self.print_lifetime(&lifetime.lifetime));
|
|
|
|
let mut sep = ":";
|
2015-01-31 17:20:46 +00:00
|
|
|
for v in &lifetime.bounds {
|
2014-08-06 02:59:24 +00:00
|
|
|
try!(word(&mut self.s, sep));
|
|
|
|
try!(self.print_lifetime(v));
|
|
|
|
sep = "+";
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2014-08-28 01:46:52 +00:00
|
|
|
pub fn print_generics(&mut self,
|
|
|
|
generics: &ast::Generics)
|
2015-02-27 05:00:43 +00:00
|
|
|
-> io::Result<()>
|
2014-08-28 01:46:52 +00:00
|
|
|
{
|
|
|
|
let total = generics.lifetimes.len() + generics.ty_params.len();
|
|
|
|
if total == 0 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
try!(word(&mut self.s, "<"));
|
|
|
|
|
2014-08-11 16:32:26 +00:00
|
|
|
let mut ints = Vec::new();
|
2015-01-28 01:01:48 +00:00
|
|
|
for i in 0..total {
|
2014-08-11 16:32:26 +00:00
|
|
|
ints.push(i);
|
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
|
2015-02-18 19:48:57 +00:00
|
|
|
try!(self.commasep(Inconsistent, &ints[..], |s, &idx| {
|
2014-08-28 01:46:52 +00:00
|
|
|
if idx < generics.lifetimes.len() {
|
2014-10-15 06:05:01 +00:00
|
|
|
let lifetime = &generics.lifetimes[idx];
|
2014-08-11 16:32:26 +00:00
|
|
|
s.print_lifetime_def(lifetime)
|
|
|
|
} else {
|
2014-08-28 01:46:52 +00:00
|
|
|
let idx = idx - generics.lifetimes.len();
|
2014-11-24 04:56:34 +00:00
|
|
|
let param = &generics.ty_params[idx];
|
2014-10-28 18:48:52 +00:00
|
|
|
s.print_ty_param(param)
|
2014-08-11 16:32:26 +00:00
|
|
|
}
|
2014-08-28 01:46:52 +00:00
|
|
|
}));
|
2014-08-11 16:32:26 +00:00
|
|
|
|
2014-08-28 01:46:52 +00:00
|
|
|
try!(word(&mut self.s, ">"));
|
|
|
|
Ok(())
|
2011-08-03 04:26:54 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_ty_param(&mut self, param: &ast::TyParam) -> io::Result<()> {
|
2014-10-28 18:48:52 +00:00
|
|
|
try!(self.print_ident(param.ident));
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(self.print_bounds(":", ¶m.bounds));
|
2014-10-28 18:48:52 +00:00
|
|
|
match param.default {
|
|
|
|
Some(ref default) => {
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space("="));
|
|
|
|
self.print_type(&**default)
|
|
|
|
}
|
|
|
|
_ => Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 04:54:29 +00:00
|
|
|
pub fn print_where_clause(&mut self, where_clause: &ast::WhereClause)
|
2015-02-27 05:00:43 +00:00
|
|
|
-> io::Result<()> {
|
2015-03-24 23:53:34 +00:00
|
|
|
if where_clause.predicates.is_empty() {
|
2014-08-11 16:32:26 +00:00
|
|
|
return Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space("where"));
|
|
|
|
|
2015-03-14 04:54:29 +00:00
|
|
|
for (i, predicate) in where_clause.predicates.iter().enumerate() {
|
2014-08-11 16:32:26 +00:00
|
|
|
if i != 0 {
|
|
|
|
try!(self.word_space(","));
|
|
|
|
}
|
|
|
|
|
2015-11-17 14:24:49 +00:00
|
|
|
match *predicate {
|
|
|
|
ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate{ref bound_lifetimes,
|
|
|
|
ref bounded_ty,
|
|
|
|
ref bounds,
|
|
|
|
..}) => {
|
2015-02-09 03:49:27 +00:00
|
|
|
try!(self.print_formal_lifetime_list(bound_lifetimes));
|
2014-12-20 10:29:19 +00:00
|
|
|
try!(self.print_type(&**bounded_ty));
|
2015-02-02 02:53:25 +00:00
|
|
|
try!(self.print_bounds(":", bounds));
|
2014-11-29 04:08:30 +00:00
|
|
|
}
|
2015-11-17 14:24:49 +00:00
|
|
|
ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate{ref lifetime,
|
|
|
|
ref bounds,
|
|
|
|
..}) => {
|
2014-12-20 10:29:19 +00:00
|
|
|
try!(self.print_lifetime(lifetime));
|
|
|
|
try!(word(&mut self.s, ":"));
|
2014-12-20 10:48:43 +00:00
|
|
|
|
|
|
|
for (i, bound) in bounds.iter().enumerate() {
|
|
|
|
try!(self.print_lifetime(bound));
|
|
|
|
|
|
|
|
if i != 0 {
|
|
|
|
try!(word(&mut self.s, ":"));
|
|
|
|
}
|
|
|
|
}
|
2014-12-20 10:29:19 +00:00
|
|
|
}
|
2015-11-17 14:24:49 +00:00
|
|
|
ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{ref path, ref ty, ..}) => {
|
2015-01-31 19:20:24 +00:00
|
|
|
try!(self.print_path(path, false, 0));
|
2014-11-29 04:08:30 +00:00
|
|
|
try!(space(&mut self.s));
|
|
|
|
try!(self.word_space("="));
|
|
|
|
try!(self.print_type(&**ty));
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 16:32:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_view_path(&mut self, vp: &ast::ViewPath) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
match vp.node {
|
2015-01-13 15:30:17 +00:00
|
|
|
ast::ViewPathSimple(ident, ref path) => {
|
2015-01-31 19:20:24 +00:00
|
|
|
try!(self.print_path(path, false, 0));
|
2014-08-13 02:25:05 +00:00
|
|
|
|
|
|
|
if path.segments.last().unwrap().identifier.name !=
|
|
|
|
ident.name {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(space(&mut self.s));
|
2014-08-13 02:25:05 +00:00
|
|
|
try!(self.word_space("as"));
|
|
|
|
try!(self.print_ident(ident));
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2014-08-13 02:25:05 +00:00
|
|
|
|
|
|
|
Ok(())
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2013-02-15 05:50:03 +00:00
|
|
|
|
2015-01-13 15:30:17 +00:00
|
|
|
ast::ViewPathGlob(ref path) => {
|
2015-01-31 19:20:24 +00:00
|
|
|
try!(self.print_path(path, false, 0));
|
2014-03-16 18:58:11 +00:00
|
|
|
word(&mut self.s, "::*")
|
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
|
2015-01-13 15:30:17 +00:00
|
|
|
ast::ViewPathList(ref path, ref idents) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
if path.segments.is_empty() {
|
|
|
|
try!(word(&mut self.s, "{"));
|
|
|
|
} else {
|
2015-01-31 19:20:24 +00:00
|
|
|
try!(self.print_path(path, false, 0));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, "::{"));
|
|
|
|
}
|
2015-02-18 19:48:57 +00:00
|
|
|
try!(self.commasep(Inconsistent, &idents[..], |s, w| {
|
2014-07-17 22:56:56 +00:00
|
|
|
match w.node {
|
2015-08-01 05:20:25 +00:00
|
|
|
ast::PathListIdent { name, rename, .. } => {
|
|
|
|
try!(s.print_ident(name));
|
|
|
|
if let Some(ident) = rename {
|
|
|
|
try!(space(&mut s.s));
|
|
|
|
try!(s.word_space("as"));
|
|
|
|
try!(s.print_ident(ident));
|
|
|
|
}
|
|
|
|
Ok(())
|
2014-07-17 22:56:56 +00:00
|
|
|
},
|
2015-08-01 05:20:25 +00:00
|
|
|
ast::PathListMod { rename, .. } => {
|
|
|
|
try!(word(&mut s.s, "self"));
|
|
|
|
if let Some(ident) = rename {
|
|
|
|
try!(space(&mut s.s));
|
|
|
|
try!(s.word_space("as"));
|
|
|
|
try!(s.print_ident(ident));
|
|
|
|
}
|
|
|
|
Ok(())
|
2014-07-17 22:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
}));
|
|
|
|
word(&mut self.s, "}")
|
|
|
|
}
|
2014-02-01 19:24:42 +00:00
|
|
|
}
|
2011-06-21 21:23:16 +00:00
|
|
|
}
|
2011-06-15 01:53:12 +00:00
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
pub fn print_mutability(&mut self,
|
2015-02-27 05:00:43 +00:00
|
|
|
mutbl: ast::Mutability) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
match mutbl {
|
2016-02-09 16:44:47 +00:00
|
|
|
ast::Mutability::Mutable => self.word_nbsp("mut"),
|
|
|
|
ast::Mutability::Immutable => Ok(()),
|
2013-01-31 01:20:02 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_mt(&mut self, mt: &ast::MutTy) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.print_mutability(mt.mutbl));
|
2014-05-16 07:16:13 +00:00
|
|
|
self.print_type(&*mt.ty)
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
|
2016-01-21 18:44:36 +00:00
|
|
|
pub fn print_arg(&mut self, input: &ast::Arg, is_closure: bool) -> io::Result<()> {
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.ibox(INDENT_UNIT));
|
2014-03-16 18:58:11 +00:00
|
|
|
match input.ty.node {
|
2016-02-08 15:53:21 +00:00
|
|
|
ast::TyKind::Infer if is_closure => try!(self.print_pat(&*input.pat)),
|
2014-03-16 18:58:11 +00:00
|
|
|
_ => {
|
|
|
|
match input.pat.node {
|
2014-07-01 01:02:14 +00:00
|
|
|
ast::PatIdent(_, ref path1, _) if
|
|
|
|
path1.node.name ==
|
2014-03-16 18:58:11 +00:00
|
|
|
parse::token::special_idents::invalid.name => {
|
|
|
|
// Do nothing.
|
|
|
|
}
|
|
|
|
_ => {
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_pat(&*input.pat));
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(word(&mut self.s, ":"));
|
|
|
|
try!(space(&mut self.s));
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
}
|
2014-05-16 07:16:13 +00:00
|
|
|
try!(self.print_type(&*input.ty));
|
2012-11-07 02:41:06 +00:00
|
|
|
}
|
2012-05-04 19:33:04 +00:00
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
self.end()
|
|
|
|
}
|
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_fn_output(&mut self, decl: &ast::FnDecl) -> io::Result<()> {
|
2016-02-08 14:04:11 +00:00
|
|
|
if let ast::FunctionRetTy::Default(..) = decl.output {
|
2015-01-18 13:49:19 +00:00
|
|
|
return Ok(());
|
2014-09-06 04:27:47 +00:00
|
|
|
}
|
2014-11-09 15:14:15 +00:00
|
|
|
|
|
|
|
try!(self.space_if_not_bol());
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.ibox(INDENT_UNIT));
|
2014-11-09 15:14:15 +00:00
|
|
|
try!(self.word_space("->"));
|
|
|
|
match decl.output {
|
2016-02-08 14:04:11 +00:00
|
|
|
ast::FunctionRetTy::None(_) =>
|
2014-11-09 15:14:15 +00:00
|
|
|
try!(self.word_nbsp("!")),
|
2016-02-08 14:04:11 +00:00
|
|
|
ast::FunctionRetTy::Default(..) => unreachable!(),
|
|
|
|
ast::FunctionRetTy::Ty(ref ty) =>
|
2014-11-09 15:14:15 +00:00
|
|
|
try!(self.print_type(&**ty))
|
|
|
|
}
|
|
|
|
try!(self.end());
|
|
|
|
|
|
|
|
match decl.output {
|
2016-02-08 14:04:11 +00:00
|
|
|
ast::FunctionRetTy::Ty(ref output) => self.maybe_print_comment(output.span.lo),
|
2014-11-09 15:14:15 +00:00
|
|
|
_ => Ok(())
|
|
|
|
}
|
2014-09-06 04:27:47 +00:00
|
|
|
}
|
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
pub fn print_ty_fn(&mut self,
|
2015-01-24 20:06:31 +00:00
|
|
|
abi: abi::Abi,
|
2014-12-09 15:36:46 +00:00
|
|
|
unsafety: ast::Unsafety,
|
2014-03-16 18:58:11 +00:00
|
|
|
decl: &ast::FnDecl,
|
2015-03-11 06:38:27 +00:00
|
|
|
name: Option<ast::Ident>,
|
2015-01-24 20:06:31 +00:00
|
|
|
generics: &ast::Generics,
|
2016-02-08 14:43:56 +00:00
|
|
|
opt_explicit_self: Option<&ast::SelfKind>)
|
2015-02-27 05:00:43 +00:00
|
|
|
-> io::Result<()> {
|
2015-11-03 16:39:51 +00:00
|
|
|
try!(self.ibox(INDENT_UNIT));
|
2015-03-24 23:54:09 +00:00
|
|
|
if !generics.lifetimes.is_empty() || !generics.ty_params.is_empty() {
|
2015-03-18 18:54:06 +00:00
|
|
|
try!(word(&mut self.s, "for"));
|
|
|
|
try!(self.print_generics(generics));
|
|
|
|
}
|
|
|
|
let generics = ast::Generics {
|
|
|
|
lifetimes: Vec::new(),
|
2015-12-16 18:44:33 +00:00
|
|
|
ty_params: P::empty(),
|
2015-03-18 18:54:06 +00:00
|
|
|
where_clause: ast::WhereClause {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
predicates: Vec::new(),
|
|
|
|
},
|
|
|
|
};
|
2015-02-25 20:05:07 +00:00
|
|
|
try!(self.print_fn(decl,
|
|
|
|
unsafety,
|
|
|
|
ast::Constness::NotConst,
|
|
|
|
abi,
|
|
|
|
name,
|
2015-05-05 12:47:04 +00:00
|
|
|
&generics,
|
2015-02-25 20:05:07 +00:00
|
|
|
opt_explicit_self,
|
2015-03-11 06:38:27 +00:00
|
|
|
ast::Inherited));
|
2014-03-16 18:58:11 +00:00
|
|
|
self.end()
|
2014-01-30 01:39:21 +00:00
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
|
|
|
|
pub fn maybe_print_trailing_comment(&mut self, span: codemap::Span,
|
|
|
|
next_pos: Option<BytePos>)
|
2015-02-27 05:00:43 +00:00
|
|
|
-> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
let cm = match self.cm {
|
|
|
|
Some(cm) => cm,
|
|
|
|
_ => return Ok(())
|
|
|
|
};
|
|
|
|
match self.next_comment() {
|
2014-01-30 01:39:21 +00:00
|
|
|
Some(ref cmnt) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
if (*cmnt).style != comments::Trailing { return Ok(()) }
|
|
|
|
let span_line = cm.lookup_char_pos(span.hi);
|
|
|
|
let comment_line = cm.lookup_char_pos((*cmnt).pos);
|
|
|
|
let mut next = (*cmnt).pos + BytePos(1);
|
|
|
|
match next_pos { None => (), Some(p) => next = p }
|
|
|
|
if span.hi < (*cmnt).pos && (*cmnt).pos < next &&
|
|
|
|
span_line.line == comment_line.line {
|
|
|
|
try!(self.print_comment(cmnt));
|
2015-01-28 01:01:48 +00:00
|
|
|
self.cur_cmnt_and_lit.cur_cmnt += 1;
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2014-01-30 01:39:21 +00:00
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
_ => ()
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
Ok(())
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_remaining_comments(&mut self) -> io::Result<()> {
|
2014-03-16 18:58:11 +00:00
|
|
|
// If there aren't any remaining comments, then we need to manually
|
|
|
|
// make sure there is a line break at the end.
|
|
|
|
if self.next_comment().is_none() {
|
|
|
|
try!(hardbreak(&mut self.s));
|
|
|
|
}
|
|
|
|
loop {
|
|
|
|
match self.next_comment() {
|
|
|
|
Some(ref cmnt) => {
|
|
|
|
try!(self.print_comment(cmnt));
|
2015-01-28 01:01:48 +00:00
|
|
|
self.cur_cmnt_and_lit.cur_cmnt += 1;
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
_ => break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
2011-07-05 09:48:19 +00:00
|
|
|
}
|
|
|
|
|
2014-04-02 08:19:41 +00:00
|
|
|
pub fn print_opt_abi_and_extern_if_nondefault(&mut self,
|
2016-02-05 12:13:36 +00:00
|
|
|
opt_abi: Option<Abi>)
|
2015-02-27 05:00:43 +00:00
|
|
|
-> io::Result<()> {
|
2014-04-02 08:19:41 +00:00
|
|
|
match opt_abi {
|
2016-02-05 12:13:36 +00:00
|
|
|
Some(Abi::Rust) => Ok(()),
|
2014-04-02 08:19:41 +00:00
|
|
|
Some(abi) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.word_nbsp("extern"));
|
2015-02-18 23:58:07 +00:00
|
|
|
self.word_nbsp(&abi.to_string())
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2014-04-02 08:19:41 +00:00
|
|
|
None => Ok(())
|
2013-10-29 22:06:13 +00:00
|
|
|
}
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2013-10-29 22:06:13 +00:00
|
|
|
|
2014-04-02 08:19:41 +00:00
|
|
|
pub fn print_extern_opt_abi(&mut self,
|
2016-02-05 12:13:36 +00:00
|
|
|
opt_abi: Option<Abi>) -> io::Result<()> {
|
2014-04-02 08:19:41 +00:00
|
|
|
match opt_abi {
|
|
|
|
Some(abi) => {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.word_nbsp("extern"));
|
2015-02-18 23:58:07 +00:00
|
|
|
self.word_nbsp(&abi.to_string())
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
|
|
|
None => Ok(())
|
2013-03-14 02:25:28 +00:00
|
|
|
}
|
2014-01-30 01:39:21 +00:00
|
|
|
}
|
2013-02-01 01:12:29 +00:00
|
|
|
|
2014-03-16 18:58:11 +00:00
|
|
|
pub fn print_fn_header_info(&mut self,
|
2015-03-11 06:38:27 +00:00
|
|
|
unsafety: ast::Unsafety,
|
2015-02-25 20:05:07 +00:00
|
|
|
constness: ast::Constness,
|
2016-02-05 12:13:36 +00:00
|
|
|
abi: Abi,
|
2015-02-27 05:00:43 +00:00
|
|
|
vis: ast::Visibility) -> io::Result<()> {
|
2015-02-02 02:53:25 +00:00
|
|
|
try!(word(&mut self.s, &visibility_qualified(vis, "")));
|
2014-05-07 23:33:43 +00:00
|
|
|
|
2015-02-25 20:05:07 +00:00
|
|
|
match constness {
|
|
|
|
ast::Constness::NotConst => {}
|
2015-05-22 12:45:05 +00:00
|
|
|
ast::Constness::Const => try!(self.word_nbsp("const"))
|
2015-02-25 20:05:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-24 08:52:07 +00:00
|
|
|
try!(self.print_unsafety(unsafety));
|
|
|
|
|
2016-02-05 12:13:36 +00:00
|
|
|
if abi != Abi::Rust {
|
2014-03-16 18:58:11 +00:00
|
|
|
try!(self.word_nbsp("extern"));
|
2015-02-18 23:58:07 +00:00
|
|
|
try!(self.word_nbsp(&abi.to_string()));
|
2013-03-14 02:25:28 +00:00
|
|
|
}
|
|
|
|
|
2014-04-09 12:33:42 +00:00
|
|
|
word(&mut self.s, "fn")
|
2012-05-25 06:44:58 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn print_unsafety(&mut self, s: ast::Unsafety) -> io::Result<()> {
|
2014-04-07 01:04:40 +00:00
|
|
|
match s {
|
2014-12-09 15:36:46 +00:00
|
|
|
ast::Unsafety::Normal => Ok(()),
|
|
|
|
ast::Unsafety::Unsafe => self.word_nbsp("unsafe"),
|
2014-03-16 18:58:11 +00:00
|
|
|
}
|
2012-05-25 06:44:58 +00:00
|
|
|
}
|
2012-11-05 04:41:00 +00:00
|
|
|
}
|
|
|
|
|
2015-01-17 23:33:05 +00:00
|
|
|
fn repeat(s: &str, n: usize) -> String { iter::repeat(s).take(n).collect() }
|
2014-12-11 03:46:38 +00:00
|
|
|
|
2013-01-24 22:10:38 +00:00
|
|
|
#[cfg(test)]
|
2015-04-24 15:30:41 +00:00
|
|
|
mod tests {
|
2013-02-25 19:11:21 +00:00
|
|
|
use super::*;
|
|
|
|
|
2013-01-24 22:10:38 +00:00
|
|
|
use ast;
|
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]
|
2014-06-21 10:39:03 +00:00
|
|
|
fn test_fun_to_string() {
|
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 {
|
2014-02-28 21:09:09 +00:00
|
|
|
inputs: Vec::new(),
|
2016-02-08 14:04:11 +00:00
|
|
|
output: ast::FunctionRetTy::Default(codemap::DUMMY_SP),
|
2013-10-25 05:56:34 +00:00
|
|
|
variadic: false
|
2013-01-24 22:10:38 +00:00
|
|
|
};
|
2015-11-28 19:02:07 +00:00
|
|
|
let generics = ast::Generics::default();
|
2015-02-25 20:05:07 +00:00
|
|
|
assert_eq!(fun_to_string(&decl, ast::Unsafety::Normal,
|
|
|
|
ast::Constness::NotConst,
|
|
|
|
abba_ident,
|
|
|
|
None, &generics),
|
2014-11-28 00:52:53 +00:00
|
|
|
"fn abba()");
|
2013-01-24 22:10:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2014-06-21 10:39:03 +00:00
|
|
|
fn test_variant_to_string() {
|
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,
|
2014-02-28 21:09:09 +00:00
|
|
|
attrs: Vec::new(),
|
2013-01-24 22:10:38 +00:00
|
|
|
// making this up as I go.... ?
|
2015-10-25 15:33:51 +00:00
|
|
|
data: ast::VariantData::Unit(ast::DUMMY_NODE_ID),
|
2013-01-24 22:10:38 +00:00
|
|
|
disr_expr: None,
|
|
|
|
});
|
|
|
|
|
2014-06-21 10:39:03 +00:00
|
|
|
let varstr = variant_to_string(&var);
|
2015-09-16 17:01:15 +00:00
|
|
|
assert_eq!(varstr, "principal_skinner");
|
2013-01-24 22:10:38 +00:00
|
|
|
}
|
|
|
|
}
|