run rustfmt

This commit is contained in:
Niko Matsakis 2017-04-04 10:22:05 -04:00
parent 382491d596
commit df0292aa64

View File

@ -6,11 +6,9 @@ use collections::*;
use super::lookahead::*; use super::lookahead::*;
pub fn generate_report<'grammar, W : Write + 'grammar> pub fn generate_report<'grammar, W: Write + 'grammar>(out: &'grammar mut W,
( out: &'grammar mut W lr1result: &LR1Result<'grammar>)
, lr1result: &LR1Result<'grammar> -> io::Result<()> {
) -> io::Result<()>
{
let mut generator = ReportGenerator::new(out); let mut generator = ReportGenerator::new(out);
generator.report_lr_table_construction(lr1result) generator.report_lr_table_construction(lr1result)
} }
@ -28,13 +26,8 @@ type ConflictStateMap<'report, 'grammar, L> = Map<StateIndex, Vec<&'report Conf
impl<'report, W> ReportGenerator<'report, W> impl<'report, W> ReportGenerator<'report, W>
where W: Write + 'report where W: Write + 'report
{ {
pub fn new pub fn new(out: &'report mut W) -> Self {
( out: &'report mut W ReportGenerator { out: out }
) -> Self
{
ReportGenerator
{ out: out
}
} }
pub fn report_lr_table_construction<'grammar : 'report, L> pub fn report_lr_table_construction<'grammar : 'report, L>
@ -53,9 +46,14 @@ impl <'report, W> ReportGenerator<'report, W>
} }
&Err(ref table_construction_error) => { &Err(ref table_construction_error) => {
try!(writeln!(self.out, "Failure")); try!(writeln!(self.out, "Failure"));
try!(writeln!(self.out, "Constructed {} states", table_construction_error.states.len())); try!(writeln!(self.out,
try!(writeln!(self.out, "Has {} conflicts", table_construction_error.conflicts.len())); "Constructed {} states",
let (sr, rr, conflict_map) = self.process_conflicts(&table_construction_error.conflicts); table_construction_error.states.len()));
try!(writeln!(self.out,
"Has {} conflicts",
table_construction_error.conflicts.len()));
let (sr, rr, conflict_map) =
self.process_conflicts(&table_construction_error.conflicts);
if (sr > 0) { if (sr > 0) {
try!(writeln!(self.out, "{}shift/reduce: {}", INDENT_STRING, sr)); try!(writeln!(self.out, "{}shift/reduce: {}", INDENT_STRING, sr));
} }
@ -73,7 +71,8 @@ impl <'report, W> ReportGenerator<'report, W>
Ok(()) Ok(())
} }
fn process_conflicts<'grammar, L>(&mut self, conflicts : &'report Vec<Conflict<'grammar, L>>) fn process_conflicts<'grammar, L>(&mut self,
conflicts: &'report Vec<Conflict<'grammar, L>>)
-> (usize, usize, ConflictStateMap<'report, 'grammar, L>) -> (usize, usize, ConflictStateMap<'report, 'grammar, L>)
where L: Lookahead where L: Lookahead
{ {
@ -85,12 +84,18 @@ impl <'report, W> ReportGenerator<'report, W>
Action::Shift(_, _) => sr = sr + 1, Action::Shift(_, _) => sr = sr + 1,
Action::Reduce(_) => rr = rr + 1, Action::Reduce(_) => rr = rr + 1,
} }
conflict_map.entry(conflict.state).or_insert(vec![]).push(conflict); conflict_map
.entry(conflict.state)
.or_insert(vec![])
.push(conflict);
} }
(sr, rr, conflict_map) (sr, rr, conflict_map)
} }
fn report_states<'grammar, L>(&mut self, states: &Vec<State<'grammar, L>>, conflict_map: &ConflictStateMap<'report, 'grammar, L>) -> io::Result<()> fn report_states<'grammar, L>(&mut self,
states: &Vec<State<'grammar, L>>,
conflict_map: &ConflictStateMap<'report, 'grammar, L>)
-> io::Result<()>
where L: Lookahead + LookaheadPrinter<W> where L: Lookahead + LookaheadPrinter<W>
{ {
try!(self.write_section_header("State Table")); try!(self.write_section_header("State Table"));
@ -101,7 +106,10 @@ impl <'report, W> ReportGenerator<'report, W>
Ok(()) Ok(())
} }
fn report_state<'grammar, L>(&mut self, state : &State<'grammar, L>, conflicts_opt: Option<&Vec<&'report Conflict<'grammar, L>>>) -> io::Result<()> fn report_state<'grammar, L>(&mut self,
state: &State<'grammar, L>,
conflicts_opt: Option<&Vec<&'report Conflict<'grammar, L>>>)
-> io::Result<()>
where L: Lookahead + LookaheadPrinter<W> where L: Lookahead + LookaheadPrinter<W>
{ {
try!(writeln!(self.out, "State {} {{", state.index)); try!(writeln!(self.out, "State {} {{", state.index));
@ -139,15 +147,23 @@ impl <'report, W> ReportGenerator<'report, W>
try!(writeln!(self.out, "")); try!(writeln!(self.out, ""));
match conflict.action { match conflict.action {
Action::Shift(terminal, state) => { Action::Shift(terminal, state) => {
let max_width = max(terminal.display_len(), conflict.production.nonterminal.len()); let max_width = max(terminal.display_len(),
conflict.production.nonterminal.len());
try!(writeln!(self.out, "{}shift/reduce conflict", INDENT_STRING)); try!(writeln!(self.out, "{}shift/reduce conflict", INDENT_STRING));
try!(write!(self.out, "{}{}reduction ", INDENT_STRING, INDENT_STRING)); try!(write!(self.out, "{}{}reduction ", INDENT_STRING, INDENT_STRING));
try!(self.write_production(conflict.production, max_width)); try!(self.write_production(conflict.production, max_width));
let sterminal = format!("{}", terminal); let sterminal = format!("{}", terminal);
try!(writeln!(self.out, "{}{}shift {:width$} shift and goto {}", INDENT_STRING, INDENT_STRING, sterminal, state, width = max_width)); try!(writeln!(self.out,
"{}{}shift {:width$} shift and goto {}",
INDENT_STRING,
INDENT_STRING,
sterminal,
state,
width = max_width));
} }
Action::Reduce(other_production) => { Action::Reduce(other_production) => {
let max_width = max(other_production.nonterminal.len(), conflict.production.nonterminal.len()); let max_width = max(other_production.nonterminal.len(),
conflict.production.nonterminal.len());
try!(writeln!(self.out, "{}reduce/reduce conflict", INDENT_STRING)); try!(writeln!(self.out, "{}reduce/reduce conflict", INDENT_STRING));
try!(write!(self.out, "{}{}reduction ", INDENT_STRING, INDENT_STRING)); try!(write!(self.out, "{}{}reduction ", INDENT_STRING, INDENT_STRING));
try!(self.write_production(conflict.production, max_width)); try!(self.write_production(conflict.production, max_width));
@ -162,7 +178,10 @@ impl <'report, W> ReportGenerator<'report, W>
fn write_items<'grammar, L>(&mut self, items: &Items<'grammar, L>) -> io::Result<()> fn write_items<'grammar, L>(&mut self, items: &Items<'grammar, L>) -> io::Result<()>
where L: Lookahead + LookaheadPrinter<W> where L: Lookahead + LookaheadPrinter<W>
{ {
let max_width = get_max_length(items.vec.iter().map(|item| &item.production.nonterminal)); let max_width = get_max_length(items
.vec
.iter()
.map(|item| &item.production.nonterminal));
for item in items.vec.iter() { for item in items.vec.iter() {
try!(writeln!(self.out, "")); try!(writeln!(self.out, ""));
@ -171,11 +190,15 @@ impl <'report, W> ReportGenerator<'report, W>
Ok(()) Ok(())
} }
fn write_item<'grammar, L>(&mut self, item: &Item<'grammar, L>, max_width : usize) -> io::Result<()> fn write_item<'grammar, L>(&mut self,
item: &Item<'grammar, L>,
max_width: usize)
-> io::Result<()>
where L: Lookahead + LookaheadPrinter<W> where L: Lookahead + LookaheadPrinter<W>
{ {
try!(write!(self.out, "{}", INDENT_STRING)); try!(write!(self.out, "{}", INDENT_STRING));
let s = format!("{}", item.production.nonterminal); // stringize it first to allow handle :width by Display for string // stringize it first to allow handle :width by Display for string
let s = format!("{}", item.production.nonterminal);
try!(write!(self.out, "{:width$} ->", s, width = max_width)); try!(write!(self.out, "{:width$} ->", s, width = max_width));
for i in 0..item.index { for i in 0..item.index {
try!(write!(self.out, " {}", item.production.symbols[i])); try!(write!(self.out, " {}", item.production.symbols[i]));
@ -189,17 +212,26 @@ impl <'report, W> ReportGenerator<'report, W>
Ok(()) Ok(())
} }
fn write_shifts(&mut self, shifts: &Map<TerminalString, StateIndex>, max_width: usize) -> io::Result<()> fn write_shifts(&mut self,
{ shifts: &Map<TerminalString, StateIndex>,
max_width: usize)
-> io::Result<()> {
for entry in shifts { for entry in shifts {
try!(write!(self.out, "{}", INDENT_STRING)); try!(write!(self.out, "{}", INDENT_STRING));
let s = format!("{}", entry.0); // stringize it first to allow handle :width by Display for string // stringize it first to allow handle :width by Display for string
try!(writeln!(self.out, "{:width$} shift and goto {}", s, entry.1, width = max_width)); let s = format!("{}", entry.0);
try!(writeln!(self.out,
"{:width$} shift and goto {}",
s,
entry.1,
width = max_width));
} }
Ok(()) Ok(())
} }
fn write_reductions<'grammar, L>(&mut self, reductions: &Vec<(L, &'grammar Production)>) -> io::Result<()> fn write_reductions<'grammar, L>(&mut self,
reductions: &Vec<(L, &'grammar Production)>)
-> io::Result<()>
where L: Lookahead + LookaheadPrinter<W> where L: Lookahead + LookaheadPrinter<W>
{ {
let max_width = get_max_length(reductions.into_iter().map(|p| &p.1.nonterminal)); let max_width = get_max_length(reductions.into_iter().map(|p| &p.1.nonterminal));
@ -210,9 +242,14 @@ impl <'report, W> ReportGenerator<'report, W>
Ok(()) Ok(())
} }
fn write_production<'grammar>(&mut self, production : &'grammar Production, max_width : usize) -> io::Result<()> fn write_production<'grammar>(&mut self,
{ production: &'grammar Production,
try!(write!(self.out, "{:width$} ->", production.nonterminal, width = max_width)); max_width: usize)
-> io::Result<()> {
try!(write!(self.out,
"{:width$} ->",
production.nonterminal,
width = max_width));
for symbol in production.symbols.iter() { for symbol in production.symbols.iter() {
try!(write!(self.out, " {}", symbol)); try!(write!(self.out, " {}", symbol));
} }
@ -220,7 +257,10 @@ impl <'report, W> ReportGenerator<'report, W>
Ok(()) Ok(())
} }
fn write_reduction<'grammar, L>(&mut self, reduction: &(L, &'grammar Production), max_width: usize) -> io::Result<()> fn write_reduction<'grammar, L>(&mut self,
reduction: &(L, &'grammar Production),
max_width: usize)
-> io::Result<()>
where L: Lookahead + LookaheadPrinter<W> where L: Lookahead + LookaheadPrinter<W>
{ {
let ref production = reduction.1; let ref production = reduction.1;
@ -241,25 +281,26 @@ impl <'report, W> ReportGenerator<'report, W>
Ok(()) Ok(())
} }
fn write_gotos(&mut self, gotos: &Map<NonterminalString, StateIndex>, max_width : usize) -> io::Result<()> fn write_gotos(&mut self,
{ gotos: &Map<NonterminalString, StateIndex>,
max_width: usize)
-> io::Result<()> {
for entry in gotos { for entry in gotos {
try!(write!(self.out, "{}", INDENT_STRING)); try!(write!(self.out, "{}", INDENT_STRING));
let s = format!("{}", entry.0); // stringize it first to allow handle :width by Display for string // stringize it first to allow handle :width by Display for string
let s = format!("{}", entry.0);
try!(writeln!(self.out, "{:width$} goto {}", s, entry.1, width = max_width)); try!(writeln!(self.out, "{:width$} goto {}", s, entry.1, width = max_width));
} }
Ok(()) Ok(())
} }
fn write_section_header(&mut self, title: &str) -> io::Result<()> fn write_section_header(&mut self, title: &str) -> io::Result<()> {
{
try!(writeln!(self.out, "\n{}", title)); try!(writeln!(self.out, "\n{}", title));
try!(writeln!(self.out, "----------------------------------------")); try!(writeln!(self.out, "----------------------------------------"));
Ok(()) Ok(())
} }
fn write_header(&mut self) -> io::Result<()> fn write_header(&mut self) -> io::Result<()> {
{
try!(writeln!(self.out, "Lalrpop Report File")); try!(writeln!(self.out, "Lalrpop Report File"));
try!(writeln!(self.out, "========================================")); try!(writeln!(self.out, "========================================"));
Ok(()) Ok(())
@ -303,8 +344,7 @@ impl<W> LookaheadPrinter<W> for TokenSet
} }
} }
trait HasDisplayLen trait HasDisplayLen {
{
fn display_len(&self) -> usize; fn display_len(&self) -> usize;
} }
@ -321,11 +361,10 @@ impl<'a> HasDisplayLen for &'a NonterminalString {
} }
fn get_max_length<I>(m: I) -> usize fn get_max_length<I>(m: I) -> usize
where I : Iterator, I::Item : HasDisplayLen where I: Iterator,
I::Item: HasDisplayLen
{ {
m m.map(|k| k.display_len()).fold(0, |acc, x| max(acc, x))
.map(|k| k.display_len())
.fold(0, |acc, x| max(acc,x))
} }
fn get_width_for_gotos<'grammar, L>(state: &State<'grammar, L>) -> usize fn get_width_for_gotos<'grammar, L>(state: &State<'grammar, L>) -> usize