mirror of
https://github.com/fluencelabs/lalrpop
synced 2025-03-28 06:01:02 +00:00
run rustfmt
This commit is contained in:
parent
382491d596
commit
df0292aa64
@ -6,16 +6,14 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
static INDENT_STRING : &'static str = " ";
|
static INDENT_STRING: &'static str = " ";
|
||||||
|
|
||||||
struct ReportGenerator<'report, W>
|
struct ReportGenerator<'report, W>
|
||||||
where W: Write + 'report
|
where W: Write + 'report
|
||||||
@ -23,18 +21,13 @@ struct ReportGenerator<'report, W>
|
|||||||
pub out: &'report mut W,
|
pub out: &'report mut W,
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConflictStateMap<'report, 'grammar, L> = Map<StateIndex, Vec<&'report Conflict<'grammar, L>>>;
|
type ConflictStateMap<'report, 'grammar, L> = Map<StateIndex, Vec<&'report Conflict<'grammar, L>>>;
|
||||||
|
|
||||||
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,25 +71,32 @@ 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,
|
||||||
-> (usize, usize, ConflictStateMap<'report, 'grammar, L>)
|
conflicts: &'report Vec<Conflict<'grammar, L>>)
|
||||||
where L : Lookahead
|
-> (usize, usize, ConflictStateMap<'report, 'grammar, L>)
|
||||||
|
where L: Lookahead
|
||||||
{
|
{
|
||||||
let mut sr : usize = 0;
|
let mut sr: usize = 0;
|
||||||
let mut rr : usize = 0;
|
let mut rr: usize = 0;
|
||||||
let mut conflict_map = Map::new();
|
let mut conflict_map = Map::new();
|
||||||
for conflict in conflicts.iter() {
|
for conflict in conflicts.iter() {
|
||||||
match conflict.action {
|
match conflict.action {
|
||||||
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,
|
||||||
where L : Lookahead + LookaheadPrinter<W>
|
states: &Vec<State<'grammar, L>>,
|
||||||
|
conflict_map: &ConflictStateMap<'report, 'grammar, L>)
|
||||||
|
-> io::Result<()>
|
||||||
|
where L: Lookahead + LookaheadPrinter<W>
|
||||||
{
|
{
|
||||||
try!(self.write_section_header("State Table"));
|
try!(self.write_section_header("State Table"));
|
||||||
for ref state in states {
|
for ref state in states {
|
||||||
@ -101,8 +106,11 @@ 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,
|
||||||
where L : Lookahead + LookaheadPrinter<W>
|
state: &State<'grammar, L>,
|
||||||
|
conflicts_opt: Option<&Vec<&'report Conflict<'grammar, L>>>)
|
||||||
|
-> io::Result<()>
|
||||||
|
where L: Lookahead + LookaheadPrinter<W>
|
||||||
{
|
{
|
||||||
try!(writeln!(self.out, "State {} {{", state.index));
|
try!(writeln!(self.out, "State {} {{", state.index));
|
||||||
try!(self.write_items(&state.items));
|
try!(self.write_items(&state.items));
|
||||||
@ -133,21 +141,29 @@ impl <'report, W> ReportGenerator<'report, W>
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_conflict<'grammar, L>(&mut self, conflict: &Conflict<'grammar, L>) -> io::Result<()>
|
fn write_conflict<'grammar, L>(&mut self, conflict: &Conflict<'grammar, L>) -> io::Result<()>
|
||||||
where L : Lookahead + LookaheadPrinter<W>
|
where L: Lookahead + LookaheadPrinter<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));
|
||||||
@ -160,9 +176,12 @@ 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,12 +190,16 @@ 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,
|
||||||
where L : Lookahead + LookaheadPrinter<W>
|
item: &Item<'grammar, L>,
|
||||||
|
max_width: usize)
|
||||||
|
-> io::Result<()>
|
||||||
|
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
|
||||||
try!(write!(self.out, "{:width$} ->", s, width=max_width));
|
let s = format!("{}", item.production.nonterminal);
|
||||||
|
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,18 +212,27 @@ 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,
|
||||||
where L : Lookahead + LookaheadPrinter<W>
|
reductions: &Vec<(L, &'grammar Production)>)
|
||||||
|
-> io::Result<()>
|
||||||
|
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));
|
||||||
for reduction in reductions.iter() {
|
for reduction in reductions.iter() {
|
||||||
@ -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,8 +257,11 @@ 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,
|
||||||
where L : Lookahead + LookaheadPrinter<W>
|
reduction: &(L, &'grammar Production),
|
||||||
|
max_width: usize)
|
||||||
|
-> io::Result<()>
|
||||||
|
where L: Lookahead + LookaheadPrinter<W>
|
||||||
{
|
{
|
||||||
let ref production = reduction.1;
|
let ref production = reduction.1;
|
||||||
try!(write!(self.out, "{}reduction ", INDENT_STRING));
|
try!(write!(self.out, "{}reduction ", INDENT_STRING));
|
||||||
@ -230,8 +270,8 @@ impl <'report, W> ReportGenerator<'report, W>
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_lookahead<L>(&mut self, lookahead : &L) -> io::Result<()>
|
fn write_lookahead<L>(&mut self, lookahead: &L) -> io::Result<()>
|
||||||
where L : Lookahead + LookaheadPrinter<W>
|
where L: Lookahead + LookaheadPrinter<W>
|
||||||
{
|
{
|
||||||
if (lookahead.has_anything_to_print()) {
|
if (lookahead.has_anything_to_print()) {
|
||||||
try!(write!(self.out, "{}{}lookahead", INDENT_STRING, INDENT_STRING));
|
try!(write!(self.out, "{}{}lookahead", INDENT_STRING, INDENT_STRING));
|
||||||
@ -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(())
|
||||||
@ -271,19 +312,19 @@ impl <'report, W> ReportGenerator<'report, W>
|
|||||||
trait LookaheadPrinter<W>
|
trait LookaheadPrinter<W>
|
||||||
where W: Write
|
where W: Write
|
||||||
{
|
{
|
||||||
fn print<'report, 'grammar>(self : &Self, out: &'report mut W) -> io::Result<()> ;
|
fn print<'report, 'grammar>(self: &Self, out: &'report mut W) -> io::Result<()>;
|
||||||
|
|
||||||
fn has_anything_to_print<'report>(self : &Self) -> bool;
|
fn has_anything_to_print<'report>(self: &Self) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W> LookaheadPrinter<W> for Nil
|
impl<W> LookaheadPrinter<W> for Nil
|
||||||
where W: Write
|
where W: Write
|
||||||
{
|
{
|
||||||
fn print<'report, 'grammar>(self : &Self, _: &'report mut W) -> io::Result<()> {
|
fn print<'report, 'grammar>(self: &Self, _: &'report mut W) -> io::Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn has_anything_to_print<'report>(self : &Self) -> bool {
|
fn has_anything_to_print<'report>(self: &Self) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -291,20 +332,19 @@ impl<W> LookaheadPrinter<W> for Nil
|
|||||||
impl<W> LookaheadPrinter<W> for TokenSet
|
impl<W> LookaheadPrinter<W> for TokenSet
|
||||||
where W: Write
|
where W: Write
|
||||||
{
|
{
|
||||||
fn print<'report, 'grammar>(self : &Self, out: &'report mut W) -> io::Result<()> {
|
fn print<'report, 'grammar>(self: &Self, out: &'report mut W) -> io::Result<()> {
|
||||||
for i in self.iter() {
|
for i in self.iter() {
|
||||||
try!(write!(out, " {}", i))
|
try!(write!(out, " {}", i))
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn has_anything_to_print<'report>(self : &Self) -> bool {
|
fn has_anything_to_print<'report>(self: &Self) -> bool {
|
||||||
self.len() > 0
|
self.len() > 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
trait HasDisplayLen
|
trait HasDisplayLen {
|
||||||
{
|
|
||||||
fn display_len(&self) -> usize;
|
fn display_len(&self) -> usize;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -320,18 +360,17 @@ 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
|
||||||
where L : Lookahead
|
where L: Lookahead
|
||||||
{
|
{
|
||||||
let shifts_max_width = get_max_length(state.shifts.keys());
|
let shifts_max_width = get_max_length(state.shifts.keys());
|
||||||
let gotos_max_width = get_max_length(state.gotos.keys());
|
let gotos_max_width = get_max_length(state.gotos.keys());
|
||||||
max(shifts_max_width, gotos_max_width)
|
max(shifts_max_width, gotos_max_width)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user