Refactor cur_cmnt_and_lit away.
The literal index was increased in only next_lit, so it isn't necessary: code now uses an iterator. The cur_cmnt field is also moved to be increased in print_comment instead of after each call to print_comment.
This commit is contained in:
parent
bac4bb9613
commit
16b486ce6e
2 changed files with 55 additions and 65 deletions
|
@ -17,7 +17,7 @@ use syntax::parse::ParseSess;
|
|||
use syntax::parse::lexer::comments;
|
||||
use syntax::print::pp::{self, Breaks};
|
||||
use syntax::print::pp::Breaks::{Consistent, Inconsistent};
|
||||
use syntax::print::pprust::{self as ast_pp, PrintState};
|
||||
use syntax::print::pprust::PrintState;
|
||||
use syntax::ptr::P;
|
||||
use syntax::symbol::keywords;
|
||||
use syntax_pos::{self, BytePos};
|
||||
|
@ -27,6 +27,8 @@ use hir::{PatKind, RegionTyParamBound, TraitTyParamBound, TraitBoundModifier, Ra
|
|||
|
||||
use std::cell::Cell;
|
||||
use std::io::{self, Write, Read};
|
||||
use std::iter::Peekable;
|
||||
use std::vec;
|
||||
|
||||
pub enum AnnNode<'a> {
|
||||
NodeName(&'a ast::Name),
|
||||
|
@ -77,8 +79,8 @@ pub struct State<'a> {
|
|||
pub s: pp::Printer<'a>,
|
||||
cm: Option<&'a CodeMap>,
|
||||
comments: Option<Vec<comments::Comment>>,
|
||||
literals: Option<Vec<comments::Literal>>,
|
||||
cur_cmnt_and_lit: ast_pp::CurrentCommentAndLiteral,
|
||||
literals: Peekable<vec::IntoIter<comments::Literal>>,
|
||||
cur_cmnt: usize,
|
||||
boxes: Vec<pp::Breaks>,
|
||||
ann: &'a (PpAnn + 'a),
|
||||
}
|
||||
|
@ -96,12 +98,16 @@ impl<'a> PrintState<'a> for State<'a> {
|
|||
&mut self.comments
|
||||
}
|
||||
|
||||
fn cur_cmnt_and_lit(&mut self) -> &mut ast_pp::CurrentCommentAndLiteral {
|
||||
&mut self.cur_cmnt_and_lit
|
||||
fn cur_cmnt(&mut self) -> &mut usize {
|
||||
&mut self.cur_cmnt
|
||||
}
|
||||
|
||||
fn literals(&self) -> &Option<Vec<comments::Literal>> {
|
||||
&self.literals
|
||||
fn cur_lit(&mut self) -> Option<&comments::Literal> {
|
||||
self.literals.peek()
|
||||
}
|
||||
|
||||
fn bump_lit(&mut self) -> Option<comments::Literal> {
|
||||
self.literals.next()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -169,11 +175,8 @@ impl<'a> State<'a> {
|
|||
s: pp::mk_printer(out, default_columns),
|
||||
cm: Some(cm),
|
||||
comments: comments.clone(),
|
||||
literals: literals.clone(),
|
||||
cur_cmnt_and_lit: ast_pp::CurrentCommentAndLiteral {
|
||||
cur_cmnt: 0,
|
||||
cur_lit: 0,
|
||||
},
|
||||
literals: literals.unwrap_or_default().into_iter().peekable(),
|
||||
cur_cmnt: 0,
|
||||
boxes: Vec::new(),
|
||||
ann,
|
||||
}
|
||||
|
@ -189,11 +192,8 @@ pub fn to_string<F>(ann: &PpAnn, f: F) -> String
|
|||
s: pp::mk_printer(Box::new(&mut wr), default_columns),
|
||||
cm: None,
|
||||
comments: None,
|
||||
literals: None,
|
||||
cur_cmnt_and_lit: ast_pp::CurrentCommentAndLiteral {
|
||||
cur_cmnt: 0,
|
||||
cur_lit: 0,
|
||||
},
|
||||
literals: vec![].into_iter().peekable(),
|
||||
cur_cmnt: 0,
|
||||
boxes: Vec::new(),
|
||||
ann,
|
||||
};
|
||||
|
@ -2131,7 +2131,6 @@ impl<'a> State<'a> {
|
|||
if span.hi < (*cmnt).pos && (*cmnt).pos < next &&
|
||||
span_line.line == comment_line.line {
|
||||
self.print_comment(cmnt)?;
|
||||
self.cur_cmnt_and_lit.cur_cmnt += 1;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
@ -2147,7 +2146,6 @@ impl<'a> State<'a> {
|
|||
match self.next_comment() {
|
||||
Some(ref cmnt) => {
|
||||
self.print_comment(cmnt)?;
|
||||
self.cur_cmnt_and_lit.cur_cmnt += 1;
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
|
|
|
@ -31,7 +31,8 @@ use tokenstream::{self, TokenStream, TokenTree};
|
|||
|
||||
use std::ascii;
|
||||
use std::io::{self, Write, Read};
|
||||
use std::iter;
|
||||
use std::iter::{self, Peekable};
|
||||
use std::vec;
|
||||
|
||||
pub enum AnnNode<'a> {
|
||||
NodeIdent(&'a ast::Ident),
|
||||
|
@ -53,18 +54,12 @@ pub struct NoAnn;
|
|||
|
||||
impl PpAnn for NoAnn {}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct CurrentCommentAndLiteral {
|
||||
pub cur_cmnt: usize,
|
||||
pub cur_lit: usize,
|
||||
}
|
||||
|
||||
pub struct State<'a> {
|
||||
pub s: pp::Printer<'a>,
|
||||
cm: Option<&'a CodeMap>,
|
||||
comments: Option<Vec<comments::Comment> >,
|
||||
literals: Option<Vec<comments::Literal> >,
|
||||
cur_cmnt_and_lit: CurrentCommentAndLiteral,
|
||||
literals: Peekable<vec::IntoIter<comments::Literal>>,
|
||||
cur_cmnt: usize,
|
||||
boxes: Vec<pp::Breaks>,
|
||||
ann: &'a (PpAnn+'a),
|
||||
}
|
||||
|
@ -80,11 +75,8 @@ pub fn rust_printer_annotated<'a>(writer: Box<Write+'a>,
|
|||
s: pp::mk_printer(writer, DEFAULT_COLUMNS),
|
||||
cm: None,
|
||||
comments: None,
|
||||
literals: None,
|
||||
cur_cmnt_and_lit: CurrentCommentAndLiteral {
|
||||
cur_cmnt: 0,
|
||||
cur_lit: 0
|
||||
},
|
||||
literals: vec![].into_iter().peekable(),
|
||||
cur_cmnt: 0,
|
||||
boxes: Vec::new(),
|
||||
ann: ann,
|
||||
}
|
||||
|
@ -160,11 +152,8 @@ impl<'a> State<'a> {
|
|||
s: pp::mk_printer(out, DEFAULT_COLUMNS),
|
||||
cm: Some(cm),
|
||||
comments: comments,
|
||||
literals: literals,
|
||||
cur_cmnt_and_lit: CurrentCommentAndLiteral {
|
||||
cur_cmnt: 0,
|
||||
cur_lit: 0
|
||||
},
|
||||
literals: literals.unwrap_or_default().into_iter().peekable(),
|
||||
cur_cmnt: 0,
|
||||
boxes: Vec::new(),
|
||||
ann: ann,
|
||||
}
|
||||
|
@ -451,8 +440,9 @@ 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 cur_cmnt(&mut self) -> &mut usize;
|
||||
fn cur_lit(&mut self) -> Option<&comments::Literal>;
|
||||
fn bump_lit(&mut self) -> Option<comments::Literal>;
|
||||
|
||||
fn word_space(&mut self, w: &str) -> io::Result<()> {
|
||||
self.writer().word(w)?;
|
||||
|
@ -518,31 +508,24 @@ pub trait PrintState<'a> {
|
|||
}
|
||||
|
||||
fn next_lit(&mut self, pos: BytePos) -> Option<comments::Literal> {
|
||||
let mut cur_lit = self.cur_cmnt_and_lit().cur_lit;
|
||||
while let Some(ltrl) = self.cur_lit().cloned() {
|
||||
if ltrl.pos > pos { break; }
|
||||
|
||||
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;
|
||||
}
|
||||
// we don't need the value here since we're forced to clone cur_lit
|
||||
// due to lack of NLL.
|
||||
self.bump_lit();
|
||||
if ltrl.pos == pos {
|
||||
return Some(ltrl);
|
||||
}
|
||||
}
|
||||
|
||||
self.cur_cmnt_and_lit().cur_lit = cur_lit;
|
||||
result
|
||||
None
|
||||
}
|
||||
|
||||
fn maybe_print_comment(&mut self, pos: BytePos) -> io::Result<()> {
|
||||
while let Some(ref cmnt) = self.next_comment() {
|
||||
if cmnt.pos < pos {
|
||||
self.print_comment(cmnt)?;
|
||||
self.cur_cmnt_and_lit().cur_cmnt += 1;
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
@ -552,7 +535,7 @@ pub trait PrintState<'a> {
|
|||
|
||||
fn print_comment(&mut self,
|
||||
cmnt: &comments::Comment) -> io::Result<()> {
|
||||
match cmnt.style {
|
||||
let r = match cmnt.style {
|
||||
comments::Mixed => {
|
||||
assert_eq!(cmnt.lines.len(), 1);
|
||||
self.writer().zerobreak()?;
|
||||
|
@ -600,11 +583,18 @@ pub trait PrintState<'a> {
|
|||
}
|
||||
self.writer().hardbreak()
|
||||
}
|
||||
};
|
||||
match r {
|
||||
Ok(()) => {
|
||||
*self.cur_cmnt() = *self.cur_cmnt() + 1;
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
fn next_comment(&mut self) -> Option<comments::Comment> {
|
||||
let cur_cmnt = self.cur_cmnt_and_lit().cur_cmnt;
|
||||
let cur_cmnt = *self.cur_cmnt();
|
||||
match *self.comments() {
|
||||
Some(ref cmnts) => {
|
||||
if cur_cmnt < cmnts.len() {
|
||||
|
@ -619,8 +609,8 @@ pub trait PrintState<'a> {
|
|||
|
||||
fn print_literal(&mut self, lit: &ast::Lit) -> io::Result<()> {
|
||||
self.maybe_print_comment(lit.span.lo)?;
|
||||
if let Some(ref ltrl) = self.next_lit(lit.span.lo) {
|
||||
return self.writer().word(&(*ltrl).lit);
|
||||
if let Some(ltrl) = self.next_lit(lit.span.lo) {
|
||||
return self.writer().word(<rl.lit);
|
||||
}
|
||||
match lit.node {
|
||||
ast::LitKind::Str(st, style) => self.print_string(&st.as_str(), style),
|
||||
|
@ -860,12 +850,16 @@ impl<'a> PrintState<'a> for State<'a> {
|
|||
&mut self.comments
|
||||
}
|
||||
|
||||
fn cur_cmnt_and_lit(&mut self) -> &mut CurrentCommentAndLiteral {
|
||||
&mut self.cur_cmnt_and_lit
|
||||
fn cur_cmnt(&mut self) -> &mut usize {
|
||||
&mut self.cur_cmnt
|
||||
}
|
||||
|
||||
fn literals(&self) -> &Option<Vec<comments::Literal>> {
|
||||
&self.literals
|
||||
fn cur_lit(&mut self) -> Option<&comments::Literal> {
|
||||
self.literals.peek()
|
||||
}
|
||||
|
||||
fn bump_lit(&mut self) -> Option<comments::Literal> {
|
||||
self.literals.next()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3021,7 +3015,6 @@ impl<'a> State<'a> {
|
|||
let next = next_pos.unwrap_or(cmnt.pos + BytePos(1));
|
||||
if span.hi < cmnt.pos && cmnt.pos < next && span_line.line == comment_line.line {
|
||||
self.print_comment(cmnt)?;
|
||||
self.cur_cmnt_and_lit.cur_cmnt += 1;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
@ -3035,7 +3028,6 @@ impl<'a> State<'a> {
|
|||
}
|
||||
while let Some(ref cmnt) = self.next_comment() {
|
||||
self.print_comment(cmnt)?;
|
||||
self.cur_cmnt_and_lit.cur_cmnt += 1;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue