1
Fork 0

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:
Mark Simulacrum 2017-06-25 06:38:13 -06:00
parent bac4bb9613
commit 16b486ce6e
2 changed files with 55 additions and 65 deletions

View file

@ -17,7 +17,7 @@ use syntax::parse::ParseSess;
use syntax::parse::lexer::comments; use syntax::parse::lexer::comments;
use syntax::print::pp::{self, Breaks}; use syntax::print::pp::{self, Breaks};
use syntax::print::pp::Breaks::{Consistent, Inconsistent}; 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::ptr::P;
use syntax::symbol::keywords; use syntax::symbol::keywords;
use syntax_pos::{self, BytePos}; use syntax_pos::{self, BytePos};
@ -27,6 +27,8 @@ use hir::{PatKind, RegionTyParamBound, TraitTyParamBound, TraitBoundModifier, Ra
use std::cell::Cell; use std::cell::Cell;
use std::io::{self, Write, Read}; use std::io::{self, Write, Read};
use std::iter::Peekable;
use std::vec;
pub enum AnnNode<'a> { pub enum AnnNode<'a> {
NodeName(&'a ast::Name), NodeName(&'a ast::Name),
@ -77,8 +79,8 @@ pub struct State<'a> {
pub s: pp::Printer<'a>, pub s: pp::Printer<'a>,
cm: Option<&'a CodeMap>, cm: Option<&'a CodeMap>,
comments: Option<Vec<comments::Comment>>, comments: Option<Vec<comments::Comment>>,
literals: Option<Vec<comments::Literal>>, literals: Peekable<vec::IntoIter<comments::Literal>>,
cur_cmnt_and_lit: ast_pp::CurrentCommentAndLiteral, cur_cmnt: usize,
boxes: Vec<pp::Breaks>, boxes: Vec<pp::Breaks>,
ann: &'a (PpAnn + 'a), ann: &'a (PpAnn + 'a),
} }
@ -96,12 +98,16 @@ impl<'a> PrintState<'a> for State<'a> {
&mut self.comments &mut self.comments
} }
fn cur_cmnt_and_lit(&mut self) -> &mut ast_pp::CurrentCommentAndLiteral { fn cur_cmnt(&mut self) -> &mut usize {
&mut self.cur_cmnt_and_lit &mut self.cur_cmnt
} }
fn literals(&self) -> &Option<Vec<comments::Literal>> { fn cur_lit(&mut self) -> Option<&comments::Literal> {
&self.literals 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), s: pp::mk_printer(out, default_columns),
cm: Some(cm), cm: Some(cm),
comments: comments.clone(), comments: comments.clone(),
literals: literals.clone(), literals: literals.unwrap_or_default().into_iter().peekable(),
cur_cmnt_and_lit: ast_pp::CurrentCommentAndLiteral { cur_cmnt: 0,
cur_cmnt: 0,
cur_lit: 0,
},
boxes: Vec::new(), boxes: Vec::new(),
ann, 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), s: pp::mk_printer(Box::new(&mut wr), default_columns),
cm: None, cm: None,
comments: None, comments: None,
literals: None, literals: vec![].into_iter().peekable(),
cur_cmnt_and_lit: ast_pp::CurrentCommentAndLiteral { cur_cmnt: 0,
cur_cmnt: 0,
cur_lit: 0,
},
boxes: Vec::new(), boxes: Vec::new(),
ann, ann,
}; };
@ -2131,7 +2131,6 @@ impl<'a> State<'a> {
if span.hi < (*cmnt).pos && (*cmnt).pos < next && if span.hi < (*cmnt).pos && (*cmnt).pos < next &&
span_line.line == comment_line.line { span_line.line == comment_line.line {
self.print_comment(cmnt)?; self.print_comment(cmnt)?;
self.cur_cmnt_and_lit.cur_cmnt += 1;
} }
} }
Ok(()) Ok(())
@ -2147,7 +2146,6 @@ impl<'a> State<'a> {
match self.next_comment() { match self.next_comment() {
Some(ref cmnt) => { Some(ref cmnt) => {
self.print_comment(cmnt)?; self.print_comment(cmnt)?;
self.cur_cmnt_and_lit.cur_cmnt += 1;
} }
_ => break, _ => break,
} }

View file

@ -31,7 +31,8 @@ use tokenstream::{self, TokenStream, TokenTree};
use std::ascii; use std::ascii;
use std::io::{self, Write, Read}; use std::io::{self, Write, Read};
use std::iter; use std::iter::{self, Peekable};
use std::vec;
pub enum AnnNode<'a> { pub enum AnnNode<'a> {
NodeIdent(&'a ast::Ident), NodeIdent(&'a ast::Ident),
@ -53,18 +54,12 @@ pub struct NoAnn;
impl PpAnn for NoAnn {} impl PpAnn for NoAnn {}
#[derive(Copy, Clone)]
pub struct CurrentCommentAndLiteral {
pub cur_cmnt: usize,
pub cur_lit: usize,
}
pub struct State<'a> { pub struct State<'a> {
pub s: pp::Printer<'a>, pub s: pp::Printer<'a>,
cm: Option<&'a CodeMap>, cm: Option<&'a CodeMap>,
comments: Option<Vec<comments::Comment> >, comments: Option<Vec<comments::Comment> >,
literals: Option<Vec<comments::Literal> >, literals: Peekable<vec::IntoIter<comments::Literal>>,
cur_cmnt_and_lit: CurrentCommentAndLiteral, cur_cmnt: usize,
boxes: Vec<pp::Breaks>, boxes: Vec<pp::Breaks>,
ann: &'a (PpAnn+'a), 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), s: pp::mk_printer(writer, DEFAULT_COLUMNS),
cm: None, cm: None,
comments: None, comments: None,
literals: None, literals: vec![].into_iter().peekable(),
cur_cmnt_and_lit: CurrentCommentAndLiteral { cur_cmnt: 0,
cur_cmnt: 0,
cur_lit: 0
},
boxes: Vec::new(), boxes: Vec::new(),
ann: ann, ann: ann,
} }
@ -160,11 +152,8 @@ impl<'a> State<'a> {
s: pp::mk_printer(out, DEFAULT_COLUMNS), s: pp::mk_printer(out, DEFAULT_COLUMNS),
cm: Some(cm), cm: Some(cm),
comments: comments, comments: comments,
literals: literals, literals: literals.unwrap_or_default().into_iter().peekable(),
cur_cmnt_and_lit: CurrentCommentAndLiteral { cur_cmnt: 0,
cur_cmnt: 0,
cur_lit: 0
},
boxes: Vec::new(), boxes: Vec::new(),
ann: ann, ann: ann,
} }
@ -451,8 +440,9 @@ pub trait PrintState<'a> {
fn writer(&mut self) -> &mut pp::Printer<'a>; fn writer(&mut self) -> &mut pp::Printer<'a>;
fn boxes(&mut self) -> &mut Vec<pp::Breaks>; fn boxes(&mut self) -> &mut Vec<pp::Breaks>;
fn comments(&mut self) -> &mut Option<Vec<comments::Comment>>; fn comments(&mut self) -> &mut Option<Vec<comments::Comment>>;
fn cur_cmnt_and_lit(&mut self) -> &mut CurrentCommentAndLiteral; fn cur_cmnt(&mut self) -> &mut usize;
fn literals(&self) -> &Option<Vec<comments::Literal>>; 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<()> { fn word_space(&mut self, w: &str) -> io::Result<()> {
self.writer().word(w)?; self.writer().word(w)?;
@ -518,31 +508,24 @@ pub trait PrintState<'a> {
} }
fn next_lit(&mut self, pos: BytePos) -> Option<comments::Literal> { 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; // we don't need the value here since we're forced to clone cur_lit
// due to lack of NLL.
if let Some(ref lits) = *self.literals() { self.bump_lit();
while cur_lit < lits.len() { if ltrl.pos == pos {
let ltrl = (*lits)[cur_lit].clone(); return Some(ltrl);
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; None
result
} }
fn maybe_print_comment(&mut self, pos: BytePos) -> io::Result<()> { fn maybe_print_comment(&mut self, pos: BytePos) -> io::Result<()> {
while let Some(ref cmnt) = self.next_comment() { while let Some(ref cmnt) = self.next_comment() {
if cmnt.pos < pos { if cmnt.pos < pos {
self.print_comment(cmnt)?; self.print_comment(cmnt)?;
self.cur_cmnt_and_lit().cur_cmnt += 1;
} else { } else {
break break
} }
@ -552,7 +535,7 @@ pub trait PrintState<'a> {
fn print_comment(&mut self, fn print_comment(&mut self,
cmnt: &comments::Comment) -> io::Result<()> { cmnt: &comments::Comment) -> io::Result<()> {
match cmnt.style { let r = match cmnt.style {
comments::Mixed => { comments::Mixed => {
assert_eq!(cmnt.lines.len(), 1); assert_eq!(cmnt.lines.len(), 1);
self.writer().zerobreak()?; self.writer().zerobreak()?;
@ -600,11 +583,18 @@ pub trait PrintState<'a> {
} }
self.writer().hardbreak() 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> { 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() { match *self.comments() {
Some(ref cmnts) => { Some(ref cmnts) => {
if cur_cmnt < cmnts.len() { if cur_cmnt < cmnts.len() {
@ -619,8 +609,8 @@ pub trait PrintState<'a> {
fn print_literal(&mut self, lit: &ast::Lit) -> io::Result<()> { fn print_literal(&mut self, lit: &ast::Lit) -> io::Result<()> {
self.maybe_print_comment(lit.span.lo)?; self.maybe_print_comment(lit.span.lo)?;
if let Some(ref ltrl) = self.next_lit(lit.span.lo) { if let Some(ltrl) = self.next_lit(lit.span.lo) {
return self.writer().word(&(*ltrl).lit); return self.writer().word(&ltrl.lit);
} }
match lit.node { match lit.node {
ast::LitKind::Str(st, style) => self.print_string(&st.as_str(), style), 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 &mut self.comments
} }
fn cur_cmnt_and_lit(&mut self) -> &mut CurrentCommentAndLiteral { fn cur_cmnt(&mut self) -> &mut usize {
&mut self.cur_cmnt_and_lit &mut self.cur_cmnt
} }
fn literals(&self) -> &Option<Vec<comments::Literal>> { fn cur_lit(&mut self) -> Option<&comments::Literal> {
&self.literals 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)); let next = next_pos.unwrap_or(cmnt.pos + BytePos(1));
if span.hi < cmnt.pos && cmnt.pos < next && span_line.line == comment_line.line { if span.hi < cmnt.pos && cmnt.pos < next && span_line.line == comment_line.line {
self.print_comment(cmnt)?; self.print_comment(cmnt)?;
self.cur_cmnt_and_lit.cur_cmnt += 1;
} }
} }
Ok(()) Ok(())
@ -3035,7 +3028,6 @@ impl<'a> State<'a> {
} }
while let Some(ref cmnt) = self.next_comment() { while let Some(ref cmnt) = self.next_comment() {
self.print_comment(cmnt)?; self.print_comment(cmnt)?;
self.cur_cmnt_and_lit.cur_cmnt += 1;
} }
Ok(()) Ok(())
} }