Remove io::Result from syntax::print
Since we're now writing directly to the vector, there's no need to thread results through the whole printing infrastructure
This commit is contained in:
parent
1aff8af213
commit
da5c835c8b
9 changed files with 1987 additions and 2118 deletions
|
|
@ -25,7 +25,6 @@ use crate::hir::print::Nested;
|
|||
use crate::util::nodemap::FxHashMap;
|
||||
use crate::util::common::time;
|
||||
|
||||
use std::io;
|
||||
use std::result::Result::Err;
|
||||
use crate::ty::query::Providers;
|
||||
|
||||
|
|
@ -1187,7 +1186,7 @@ pub fn map_crate<'hir>(sess: &crate::session::Session,
|
|||
/// Identical to the `PpAnn` implementation for `hir::Crate`,
|
||||
/// except it avoids creating a dependency on the whole crate.
|
||||
impl<'hir> print::PpAnn for Map<'hir> {
|
||||
fn nested(&self, state: &mut print::State<'_>, nested: print::Nested) -> io::Result<()> {
|
||||
fn nested(&self, state: &mut print::State<'_>, nested: print::Nested) {
|
||||
match nested {
|
||||
Nested::Item(id) => state.print_item(self.expect_item(id.id)),
|
||||
Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
|
||||
|
|
@ -1199,7 +1198,7 @@ impl<'hir> print::PpAnn for Map<'hir> {
|
|||
}
|
||||
|
||||
impl<'a> print::State<'a> {
|
||||
pub fn print_node(&mut self, node: Node<'_>) -> io::Result<()> {
|
||||
pub fn print_node(&mut self, node: Node<'_>) {
|
||||
match node {
|
||||
Node::Item(a) => self.print_item(&a),
|
||||
Node::ForeignItem(a) => self.print_foreign_item(&a),
|
||||
|
|
@ -1219,9 +1218,9 @@ impl<'a> print::State<'a> {
|
|||
use syntax::print::pprust::PrintState;
|
||||
|
||||
// containing cbox, will be closed by print-block at }
|
||||
self.cbox(print::indent_unit)?;
|
||||
self.cbox(print::indent_unit);
|
||||
// head-ibox, will be closed by print-block after {
|
||||
self.ibox(0)?;
|
||||
self.ibox(0);
|
||||
self.print_block(&a)
|
||||
}
|
||||
Node::Lifetime(a) => self.print_lifetime(&a),
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -6,7 +6,6 @@
|
|||
use rustc::cfg;
|
||||
use rustc::cfg::CFGIndex;
|
||||
use rustc::ty::TyCtxt;
|
||||
use std::io;
|
||||
use std::mem;
|
||||
use std::usize;
|
||||
use syntax::print::pprust::PrintState;
|
||||
|
|
@ -98,23 +97,23 @@ impl<'tcx, O: DataFlowOperator> DataFlowContext<'tcx, O> {
|
|||
}
|
||||
|
||||
impl<'tcx, O: DataFlowOperator> pprust::PpAnn for DataFlowContext<'tcx, O> {
|
||||
fn nested(&self, state: &mut pprust::State<'_>, nested: pprust::Nested) -> io::Result<()> {
|
||||
fn nested(&self, state: &mut pprust::State<'_>, nested: pprust::Nested) {
|
||||
pprust::PpAnn::nested(self.tcx.hir(), state, nested)
|
||||
}
|
||||
fn pre(&self,
|
||||
ps: &mut pprust::State<'_>,
|
||||
node: pprust::AnnNode<'_>) -> io::Result<()> {
|
||||
node: pprust::AnnNode<'_>) {
|
||||
let id = match node {
|
||||
pprust::AnnNode::Name(_) => return Ok(()),
|
||||
pprust::AnnNode::Name(_) => return,
|
||||
pprust::AnnNode::Expr(expr) => expr.hir_id.local_id,
|
||||
pprust::AnnNode::Block(blk) => blk.hir_id.local_id,
|
||||
pprust::AnnNode::Item(_) |
|
||||
pprust::AnnNode::SubItem(_) => return Ok(()),
|
||||
pprust::AnnNode::SubItem(_) => return,
|
||||
pprust::AnnNode::Pat(pat) => pat.hir_id.local_id
|
||||
};
|
||||
|
||||
if !self.has_bitset_for_local_id(id) {
|
||||
return Ok(());
|
||||
return;
|
||||
}
|
||||
|
||||
assert!(self.bits_per_id > 0);
|
||||
|
|
@ -147,10 +146,9 @@ impl<'tcx, O: DataFlowOperator> pprust::PpAnn for DataFlowContext<'tcx, O> {
|
|||
|
||||
ps.synth_comment(
|
||||
format!("id {}: {}{}{}{}", id.as_usize(), entry_str,
|
||||
gens_str, action_kills_str, scope_kills_str))?;
|
||||
ps.s.space()?;
|
||||
gens_str, action_kills_str, scope_kills_str));
|
||||
ps.s.space();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -531,8 +529,8 @@ impl<'tcx, O: DataFlowOperator + Clone + 'static> DataFlowContext<'tcx, O> {
|
|||
|
||||
debug!("Dataflow result for {}:", self.analysis_name);
|
||||
debug!("{}", pprust::to_string(self, |s| {
|
||||
s.cbox(pprust::indent_unit)?;
|
||||
s.ibox(0)?;
|
||||
s.cbox(pprust::indent_unit);
|
||||
s.ibox(0);
|
||||
s.print_expr(&body.value)
|
||||
}));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -297,12 +297,9 @@ impl<'hir> HirPrinterSupport<'hir> for NoAnn<'hir> {
|
|||
|
||||
impl<'hir> pprust::PpAnn for NoAnn<'hir> {}
|
||||
impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> {
|
||||
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested)
|
||||
-> io::Result<()> {
|
||||
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
|
||||
if let Some(tcx) = self.tcx {
|
||||
pprust_hir::PpAnn::nested(tcx.hir(), state, nested)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -323,37 +320,37 @@ impl<'hir> PrinterSupport for IdentifiedAnnotation<'hir> {
|
|||
}
|
||||
|
||||
impl<'hir> pprust::PpAnn for IdentifiedAnnotation<'hir> {
|
||||
fn pre(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) -> io::Result<()> {
|
||||
fn pre(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
|
||||
match node {
|
||||
pprust::AnnNode::Expr(_) => s.popen(),
|
||||
_ => Ok(()),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) -> io::Result<()> {
|
||||
fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
|
||||
match node {
|
||||
pprust::AnnNode::Ident(_) |
|
||||
pprust::AnnNode::Name(_) => Ok(()),
|
||||
pprust::AnnNode::Name(_) => {},
|
||||
|
||||
pprust::AnnNode::Item(item) => {
|
||||
s.s.space()?;
|
||||
s.s.space();
|
||||
s.synth_comment(item.id.to_string())
|
||||
}
|
||||
pprust::AnnNode::SubItem(id) => {
|
||||
s.s.space()?;
|
||||
s.s.space();
|
||||
s.synth_comment(id.to_string())
|
||||
}
|
||||
pprust::AnnNode::Block(blk) => {
|
||||
s.s.space()?;
|
||||
s.s.space();
|
||||
s.synth_comment(format!("block {}", blk.id))
|
||||
}
|
||||
pprust::AnnNode::Expr(expr) => {
|
||||
s.s.space()?;
|
||||
s.synth_comment(expr.id.to_string())?;
|
||||
s.s.space();
|
||||
s.synth_comment(expr.id.to_string());
|
||||
s.pclose()
|
||||
}
|
||||
pprust::AnnNode::Pat(pat) => {
|
||||
s.s.space()?;
|
||||
s.synth_comment(format!("pat {}", pat.id))
|
||||
s.s.space();
|
||||
s.synth_comment(format!("pat {}", pat.id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -374,45 +371,42 @@ impl<'hir> HirPrinterSupport<'hir> for IdentifiedAnnotation<'hir> {
|
|||
}
|
||||
|
||||
impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
|
||||
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested)
|
||||
-> io::Result<()> {
|
||||
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
|
||||
if let Some(ref tcx) = self.tcx {
|
||||
pprust_hir::PpAnn::nested(tcx.hir(), state, nested)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) -> io::Result<()> {
|
||||
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
|
||||
match node {
|
||||
pprust_hir::AnnNode::Expr(_) => s.popen(),
|
||||
_ => Ok(()),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) -> io::Result<()> {
|
||||
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
|
||||
match node {
|
||||
pprust_hir::AnnNode::Name(_) => Ok(()),
|
||||
pprust_hir::AnnNode::Name(_) => {},
|
||||
pprust_hir::AnnNode::Item(item) => {
|
||||
s.s.space()?;
|
||||
s.s.space();
|
||||
s.synth_comment(format!("hir_id: {} hir local_id: {}",
|
||||
item.hir_id, item.hir_id.local_id.as_u32()))
|
||||
}
|
||||
pprust_hir::AnnNode::SubItem(id) => {
|
||||
s.s.space()?;
|
||||
s.s.space();
|
||||
s.synth_comment(id.to_string())
|
||||
}
|
||||
pprust_hir::AnnNode::Block(blk) => {
|
||||
s.s.space()?;
|
||||
s.s.space();
|
||||
s.synth_comment(format!("block hir_id: {} hir local_id: {}",
|
||||
blk.hir_id, blk.hir_id.local_id.as_u32()))
|
||||
}
|
||||
pprust_hir::AnnNode::Expr(expr) => {
|
||||
s.s.space()?;
|
||||
s.s.space();
|
||||
s.synth_comment(format!("expr hir_id: {} hir local_id: {}",
|
||||
expr.hir_id, expr.hir_id.local_id.as_u32()))?;
|
||||
expr.hir_id, expr.hir_id.local_id.as_u32()));
|
||||
s.pclose()
|
||||
}
|
||||
pprust_hir::AnnNode::Pat(pat) => {
|
||||
s.s.space()?;
|
||||
s.s.space();
|
||||
s.synth_comment(format!("pat hir_id: {} hir local_id: {}",
|
||||
pat.hir_id, pat.hir_id.local_id.as_u32()))
|
||||
}
|
||||
|
|
@ -435,19 +429,19 @@ impl<'a> PrinterSupport for HygieneAnnotation<'a> {
|
|||
}
|
||||
|
||||
impl<'a> pprust::PpAnn for HygieneAnnotation<'a> {
|
||||
fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) -> io::Result<()> {
|
||||
fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
|
||||
match node {
|
||||
pprust::AnnNode::Ident(&ast::Ident { name, span }) => {
|
||||
s.s.space()?;
|
||||
s.s.space();
|
||||
// FIXME #16420: this doesn't display the connections
|
||||
// between syntax contexts
|
||||
s.synth_comment(format!("{}{:?}", name.as_u32(), span.ctxt()))
|
||||
}
|
||||
pprust::AnnNode::Name(&name) => {
|
||||
s.s.space()?;
|
||||
s.s.space();
|
||||
s.synth_comment(name.as_u32().to_string())
|
||||
}
|
||||
_ => Ok(()),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -476,32 +470,30 @@ impl<'b, 'tcx> HirPrinterSupport<'tcx> for TypedAnnotation<'b, 'tcx> {
|
|||
}
|
||||
|
||||
impl<'a, 'tcx> pprust_hir::PpAnn for TypedAnnotation<'a, 'tcx> {
|
||||
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested)
|
||||
-> io::Result<()> {
|
||||
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
|
||||
let old_tables = self.tables.get();
|
||||
if let pprust_hir::Nested::Body(id) = nested {
|
||||
self.tables.set(self.tcx.body_tables(id));
|
||||
}
|
||||
pprust_hir::PpAnn::nested(self.tcx.hir(), state, nested)?;
|
||||
pprust_hir::PpAnn::nested(self.tcx.hir(), state, nested);
|
||||
self.tables.set(old_tables);
|
||||
Ok(())
|
||||
}
|
||||
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) -> io::Result<()> {
|
||||
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
|
||||
match node {
|
||||
pprust_hir::AnnNode::Expr(_) => s.popen(),
|
||||
_ => Ok(()),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) -> io::Result<()> {
|
||||
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
|
||||
match node {
|
||||
pprust_hir::AnnNode::Expr(expr) => {
|
||||
s.s.space()?;
|
||||
s.s.word("as")?;
|
||||
s.s.space()?;
|
||||
s.s.word(self.tables.get().expr_ty(expr).to_string())?;
|
||||
s.pclose()
|
||||
s.s.space();
|
||||
s.s.word("as");
|
||||
s.s.space();
|
||||
s.s.word(self.tables.get().expr_ty(expr).to_string());
|
||||
s.pclose();
|
||||
}
|
||||
_ => Ok(()),
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -744,7 +736,7 @@ pub fn print_after_parsing(sess: &Session,
|
|||
out,
|
||||
annotation.pp_ann(),
|
||||
false)
|
||||
}).unwrap()
|
||||
})
|
||||
} else {
|
||||
unreachable!();
|
||||
};
|
||||
|
|
@ -831,14 +823,14 @@ pub fn print_after_hir_lowering<'tcx>(
|
|||
for node_id in uii.all_matching_node_ids(hir_map) {
|
||||
let hir_id = tcx.hir().node_to_hir_id(node_id);
|
||||
let node = hir_map.get(hir_id);
|
||||
pp_state.print_node(node)?;
|
||||
pp_state.s.space()?;
|
||||
pp_state.print_node(node);
|
||||
pp_state.s.space();
|
||||
let path = annotation.node_path(hir_id)
|
||||
.expect("-Z unpretty missing node paths");
|
||||
pp_state.synth_comment(path)?;
|
||||
pp_state.s.hardbreak()?;
|
||||
pp_state.synth_comment(path);
|
||||
pp_state.s.hardbreak();
|
||||
}
|
||||
pp_state.s.eof()
|
||||
pp_state.s.eof();
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -851,13 +843,11 @@ pub fn print_after_hir_lowering<'tcx>(
|
|||
let node = tcx.hir().get(hir_id);
|
||||
out.push_str(&format!("{:#?}", node));
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
_ => unreachable!(),
|
||||
}
|
||||
.unwrap();
|
||||
|
||||
write_output(out.into_bytes(), ofile);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1261,7 +1261,6 @@ pub fn noop_visit_vis<T: MutVisitor>(Spanned { node, span }: &mut Visibility, vi
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::io;
|
||||
use crate::ast::{self, Ident};
|
||||
use crate::util::parser_testing::{string_to_crate, matches_codepattern};
|
||||
use crate::print::pprust;
|
||||
|
|
@ -1271,7 +1270,7 @@ mod tests {
|
|||
|
||||
// this version doesn't care about getting comments or docstrings in.
|
||||
fn fake_print_crate(s: &mut pprust::State<'_>,
|
||||
krate: &ast::Crate) -> io::Result<()> {
|
||||
krate: &ast::Crate) {
|
||||
s.print_mod(&krate.module, &krate.attrs)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -613,12 +613,12 @@ impl<'a> Parser<'a> {
|
|||
let sum_with_parens = pprust::to_string(|s| {
|
||||
use crate::print::pprust::PrintState;
|
||||
|
||||
s.s.word("&")?;
|
||||
s.print_opt_lifetime(lifetime)?;
|
||||
s.print_mutability(mut_ty.mutbl)?;
|
||||
s.popen()?;
|
||||
s.print_type(&mut_ty.ty)?;
|
||||
s.print_type_bounds(" +", &bounds)?;
|
||||
s.s.word("&");
|
||||
s.print_opt_lifetime(lifetime);
|
||||
s.print_mutability(mut_ty.mutbl);
|
||||
s.popen();
|
||||
s.print_type(&mut_ty.ty);
|
||||
s.print_type_bounds(" +", &bounds);
|
||||
s.pclose()
|
||||
});
|
||||
err.span_suggestion(
|
||||
|
|
|
|||
|
|
@ -2572,12 +2572,12 @@ impl<'a> Parser<'a> {
|
|||
};
|
||||
let sugg = pprust::to_string(|s| {
|
||||
use crate::print::pprust::PrintState;
|
||||
s.popen()?;
|
||||
s.print_expr(&e)?;
|
||||
s.s.word( ".")?;
|
||||
s.print_usize(float.trunc() as usize)?;
|
||||
s.pclose()?;
|
||||
s.s.word(".")?;
|
||||
s.popen();
|
||||
s.print_expr(&e);
|
||||
s.s.word( ".");
|
||||
s.print_usize(float.trunc() as usize);
|
||||
s.pclose();
|
||||
s.s.word(".");
|
||||
s.s.word(fstr.splitn(2, ".").last().unwrap().to_string())
|
||||
});
|
||||
err.span_suggestion(
|
||||
|
|
@ -4634,9 +4634,9 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
let sugg = pprust::to_string(|s| {
|
||||
use crate::print::pprust::{PrintState, INDENT_UNIT};
|
||||
s.ibox(INDENT_UNIT)?;
|
||||
s.bopen()?;
|
||||
s.print_stmt(&stmt)?;
|
||||
s.ibox(INDENT_UNIT);
|
||||
s.bopen();
|
||||
s.print_stmt(&stmt);
|
||||
s.bclose_maybe_open(stmt.span, INDENT_UNIT, false)
|
||||
});
|
||||
e.span_suggestion(
|
||||
|
|
|
|||
|
|
@ -136,7 +136,6 @@
|
|||
|
||||
use std::collections::VecDeque;
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
use std::borrow::Cow;
|
||||
use log::debug;
|
||||
|
||||
|
|
@ -311,16 +310,15 @@ impl<'a> Printer<'a> {
|
|||
self.buf[self.right].token = t;
|
||||
}
|
||||
|
||||
fn pretty_print_eof(&mut self) -> io::Result<()> {
|
||||
fn pretty_print_eof(&mut self) {
|
||||
if !self.scan_stack.is_empty() {
|
||||
self.check_stack(0);
|
||||
self.advance_left()?;
|
||||
self.advance_left();
|
||||
}
|
||||
self.indent(0);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn pretty_print_begin(&mut self, b: BeginToken) -> io::Result<()> {
|
||||
fn pretty_print_begin(&mut self, b: BeginToken) {
|
||||
if self.scan_stack.is_empty() {
|
||||
self.left_total = 1;
|
||||
self.right_total = 1;
|
||||
|
|
@ -334,24 +332,22 @@ impl<'a> Printer<'a> {
|
|||
self.buf[self.right] = BufEntry { token: Token::Begin(b), size: -self.right_total };
|
||||
let right = self.right;
|
||||
self.scan_push(right);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn pretty_print_end(&mut self) -> io::Result<()> {
|
||||
fn pretty_print_end(&mut self) {
|
||||
if self.scan_stack.is_empty() {
|
||||
debug!("pp End/print Vec<{},{}>", self.left, self.right);
|
||||
self.print_end()
|
||||
self.print_end();
|
||||
} else {
|
||||
debug!("pp End/buffer Vec<{},{}>", self.left, self.right);
|
||||
self.advance_right();
|
||||
self.buf[self.right] = BufEntry { token: Token::End, size: -1 };
|
||||
let right = self.right;
|
||||
self.scan_push(right);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn pretty_print_break(&mut self, b: BreakToken) -> io::Result<()> {
|
||||
fn pretty_print_break(&mut self, b: BreakToken) {
|
||||
if self.scan_stack.is_empty() {
|
||||
self.left_total = 1;
|
||||
self.right_total = 1;
|
||||
|
|
@ -367,25 +363,24 @@ impl<'a> Printer<'a> {
|
|||
self.scan_push(right);
|
||||
self.buf[self.right] = BufEntry { token: Token::Break(b), size: -self.right_total };
|
||||
self.right_total += b.blank_space;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn pretty_print_string(&mut self, s: Cow<'static, str>, len: isize) -> io::Result<()> {
|
||||
fn pretty_print_string(&mut self, s: Cow<'static, str>, len: isize) {
|
||||
if self.scan_stack.is_empty() {
|
||||
debug!("pp String('{}')/print Vec<{},{}>",
|
||||
s, self.left, self.right);
|
||||
self.print_string(s, len)
|
||||
self.print_string(s, len);
|
||||
} else {
|
||||
debug!("pp String('{}')/buffer Vec<{},{}>",
|
||||
s, self.left, self.right);
|
||||
self.advance_right();
|
||||
self.buf[self.right] = BufEntry { token: Token::String(s, len), size: len };
|
||||
self.right_total += len;
|
||||
self.check_stream()
|
||||
self.check_stream();
|
||||
}
|
||||
}
|
||||
|
||||
crate fn check_stream(&mut self) -> io::Result<()> {
|
||||
crate fn check_stream(&mut self) {
|
||||
debug!("check_stream Vec<{}, {}> with left_total={}, right_total={}",
|
||||
self.left, self.right, self.left_total, self.right_total);
|
||||
if self.right_total - self.left_total > self.space {
|
||||
|
|
@ -396,12 +391,11 @@ impl<'a> Printer<'a> {
|
|||
let scanned = self.scan_pop_bottom();
|
||||
self.buf[scanned].size = SIZE_INFINITY;
|
||||
}
|
||||
self.advance_left()?;
|
||||
self.advance_left();
|
||||
if self.left != self.right {
|
||||
self.check_stream()?;
|
||||
self.check_stream();
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
crate fn scan_push(&mut self, x: usize) {
|
||||
|
|
@ -431,7 +425,7 @@ impl<'a> Printer<'a> {
|
|||
assert_ne!(self.right, self.left);
|
||||
}
|
||||
|
||||
crate fn advance_left(&mut self) -> io::Result<()> {
|
||||
crate fn advance_left(&mut self) {
|
||||
debug!("advance_left Vec<{},{}>, sizeof({})={}", self.left, self.right,
|
||||
self.left, self.buf[self.left].size);
|
||||
|
||||
|
|
@ -449,7 +443,7 @@ impl<'a> Printer<'a> {
|
|||
_ => 0
|
||||
};
|
||||
|
||||
self.print(left, left_size)?;
|
||||
self.print(left, left_size);
|
||||
|
||||
self.left_total += len;
|
||||
|
||||
|
|
@ -462,8 +456,6 @@ impl<'a> Printer<'a> {
|
|||
|
||||
left_size = self.buf[self.left].size;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
crate fn check_stack(&mut self, k: isize) {
|
||||
|
|
@ -494,12 +486,11 @@ impl<'a> Printer<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
crate fn print_newline(&mut self, amount: isize) -> io::Result<()> {
|
||||
crate fn print_newline(&mut self, amount: isize) {
|
||||
debug!("NEWLINE {}", amount);
|
||||
self.out.push('\n');
|
||||
self.pending_indentation = 0;
|
||||
self.indent(amount);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
crate fn indent(&mut self, amount: isize) {
|
||||
|
|
@ -517,7 +508,7 @@ impl<'a> Printer<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
crate fn print_begin(&mut self, b: BeginToken, l: isize) -> io::Result<()> {
|
||||
crate fn print_begin(&mut self, b: BeginToken, l: isize) {
|
||||
if l > self.space {
|
||||
let col = self.margin - self.space + b.offset;
|
||||
debug!("print Begin -> push broken block at col {}", col);
|
||||
|
|
@ -532,52 +523,46 @@ impl<'a> Printer<'a> {
|
|||
pbreak: PrintStackBreak::Fits
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
crate fn print_end(&mut self) -> io::Result<()> {
|
||||
crate fn print_end(&mut self) {
|
||||
debug!("print End -> pop End");
|
||||
let print_stack = &mut self.print_stack;
|
||||
assert!(!print_stack.is_empty());
|
||||
print_stack.pop().unwrap();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
crate fn print_break(&mut self, b: BreakToken, l: isize) -> io::Result<()> {
|
||||
crate fn print_break(&mut self, b: BreakToken, l: isize) {
|
||||
let top = self.get_top();
|
||||
match top.pbreak {
|
||||
PrintStackBreak::Fits => {
|
||||
debug!("print Break({}) in fitting block", b.blank_space);
|
||||
self.space -= b.blank_space;
|
||||
self.indent(b.blank_space);
|
||||
Ok(())
|
||||
}
|
||||
PrintStackBreak::Broken(Breaks::Consistent) => {
|
||||
debug!("print Break({}+{}) in consistent block",
|
||||
top.offset, b.offset);
|
||||
let ret = self.print_newline(top.offset + b.offset);
|
||||
self.print_newline(top.offset + b.offset);
|
||||
self.space = self.margin - (top.offset + b.offset);
|
||||
ret
|
||||
}
|
||||
PrintStackBreak::Broken(Breaks::Inconsistent) => {
|
||||
if l > self.space {
|
||||
debug!("print Break({}+{}) w/ newline in inconsistent",
|
||||
top.offset, b.offset);
|
||||
let ret = self.print_newline(top.offset + b.offset);
|
||||
self.print_newline(top.offset + b.offset);
|
||||
self.space = self.margin - (top.offset + b.offset);
|
||||
ret
|
||||
} else {
|
||||
debug!("print Break({}) w/o newline in inconsistent",
|
||||
b.blank_space);
|
||||
self.indent(b.blank_space);
|
||||
self.space -= b.blank_space;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
crate fn print_string(&mut self, s: Cow<'static, str>, len: isize) -> io::Result<()> {
|
||||
crate fn print_string(&mut self, s: Cow<'static, str>, len: isize) {
|
||||
debug!("print String({})", s);
|
||||
// assert!(len <= space);
|
||||
self.space -= len;
|
||||
|
|
@ -592,11 +577,9 @@ impl<'a> Printer<'a> {
|
|||
self.out.extend(std::iter::repeat(' ').take(self.pending_indentation as usize));
|
||||
self.pending_indentation = 0;
|
||||
self.out.push_str(&s);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
crate fn print(&mut self, token: Token, l: isize) -> io::Result<()> {
|
||||
crate fn print(&mut self, token: Token, l: isize) {
|
||||
debug!("print {} {} (remaining line space={})", token, l,
|
||||
self.space);
|
||||
debug!("{}", buf_str(&self.buf,
|
||||
|
|
@ -609,7 +592,7 @@ impl<'a> Printer<'a> {
|
|||
Token::Break(b) => self.print_break(b, l),
|
||||
Token::String(s, len) => {
|
||||
assert_eq!(len, l);
|
||||
self.print_string(s, len)
|
||||
self.print_string(s, len);
|
||||
}
|
||||
Token::Eof => panic!(), // Eof should never get here.
|
||||
}
|
||||
|
|
@ -618,7 +601,7 @@ impl<'a> Printer<'a> {
|
|||
// Convenience functions to talk to the printer.
|
||||
|
||||
/// "raw box"
|
||||
crate fn rbox(&mut self, indent: usize, b: Breaks) -> io::Result<()> {
|
||||
crate fn rbox(&mut self, indent: usize, b: Breaks) {
|
||||
self.pretty_print_begin(BeginToken {
|
||||
offset: indent as isize,
|
||||
breaks: b
|
||||
|
|
@ -626,49 +609,49 @@ impl<'a> Printer<'a> {
|
|||
}
|
||||
|
||||
/// Inconsistent breaking box
|
||||
crate fn ibox(&mut self, indent: usize) -> io::Result<()> {
|
||||
crate fn ibox(&mut self, indent: usize) {
|
||||
self.rbox(indent, Breaks::Inconsistent)
|
||||
}
|
||||
|
||||
/// Consistent breaking box
|
||||
pub fn cbox(&mut self, indent: usize) -> io::Result<()> {
|
||||
pub fn cbox(&mut self, indent: usize) {
|
||||
self.rbox(indent, Breaks::Consistent)
|
||||
}
|
||||
|
||||
pub fn break_offset(&mut self, n: usize, off: isize) -> io::Result<()> {
|
||||
pub fn break_offset(&mut self, n: usize, off: isize) {
|
||||
self.pretty_print_break(BreakToken {
|
||||
offset: off,
|
||||
blank_space: n as isize
|
||||
})
|
||||
}
|
||||
|
||||
crate fn end(&mut self) -> io::Result<()> {
|
||||
crate fn end(&mut self) {
|
||||
self.pretty_print_end()
|
||||
}
|
||||
|
||||
pub fn eof(&mut self) -> io::Result<()> {
|
||||
pub fn eof(&mut self) {
|
||||
self.pretty_print_eof()
|
||||
}
|
||||
|
||||
pub fn word<S: Into<Cow<'static, str>>>(&mut self, wrd: S) -> io::Result<()> {
|
||||
pub fn word<S: Into<Cow<'static, str>>>(&mut self, wrd: S) {
|
||||
let s = wrd.into();
|
||||
let len = s.len() as isize;
|
||||
self.pretty_print_string(s, len)
|
||||
}
|
||||
|
||||
fn spaces(&mut self, n: usize) -> io::Result<()> {
|
||||
fn spaces(&mut self, n: usize) {
|
||||
self.break_offset(n, 0)
|
||||
}
|
||||
|
||||
crate fn zerobreak(&mut self) -> io::Result<()> {
|
||||
crate fn zerobreak(&mut self) {
|
||||
self.spaces(0)
|
||||
}
|
||||
|
||||
pub fn space(&mut self) -> io::Result<()> {
|
||||
pub fn space(&mut self) {
|
||||
self.spaces(1)
|
||||
}
|
||||
|
||||
pub fn hardbreak(&mut self) -> io::Result<()> {
|
||||
pub fn hardbreak(&mut self) {
|
||||
self.spaces(SIZE_INFINITY as usize)
|
||||
}
|
||||
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue