mirror of
https://github.com/fluencelabs/lalrpop
synced 2025-03-28 06:01:02 +00:00
refactor: Remove some duplication from the reduce action
This commit is contained in:
parent
4f0a7a6a49
commit
63183f67c6
@ -761,9 +761,8 @@ impl<'ascent, 'grammar, W: Write> CodeGenerator<'ascent, 'grammar, W, TableDrive
|
|||||||
rust!(self.out, "{{");
|
rust!(self.out, "{{");
|
||||||
|
|
||||||
rust!(self.out,
|
rust!(self.out,
|
||||||
"let {}nonterminal = match -{}action {{",
|
"let ({p}pop_states, {p}symbol, {p}nonterminal) = match -{}action {{",
|
||||||
self.prefix,
|
p = self.prefix);
|
||||||
self.prefix);
|
|
||||||
for (production, index) in self.grammar
|
for (production, index) in self.grammar
|
||||||
.nonterminals
|
.nonterminals
|
||||||
.values()
|
.values()
|
||||||
@ -778,6 +777,20 @@ impl<'ascent, 'grammar, W: Write> CodeGenerator<'ascent, 'grammar, W, TableDrive
|
|||||||
"_ => panic!(\"invalid action code {{}}\", {}action)",
|
"_ => panic!(\"invalid action code {{}}\", {}action)",
|
||||||
self.prefix);
|
self.prefix);
|
||||||
rust!(self.out, "}};");
|
rust!(self.out, "}};");
|
||||||
|
|
||||||
|
|
||||||
|
// pop the consumed states from the stack
|
||||||
|
rust!(self.out,
|
||||||
|
"let {p}states_len = {p}states.len();",
|
||||||
|
p = self.prefix);
|
||||||
|
rust!(self.out,
|
||||||
|
"{p}states.truncate({p}states_len - {p}pop_states);",
|
||||||
|
p = self.prefix);
|
||||||
|
|
||||||
|
rust!(self.out,
|
||||||
|
"{p}symbols.push({p}symbol);",
|
||||||
|
p = self.prefix);
|
||||||
|
|
||||||
rust!(self.out,
|
rust!(self.out,
|
||||||
"let {}state = *{}states.last().unwrap() as usize;",
|
"let {}state = *{}states.last().unwrap() as usize;",
|
||||||
self.prefix,
|
self.prefix,
|
||||||
@ -900,19 +913,10 @@ impl<'ascent, 'grammar, W: Write> CodeGenerator<'ascent, 'grammar, W, TableDrive
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
// pop the consumed states from the stack
|
|
||||||
rust!(self.out,
|
|
||||||
"let {p}states_len = {p}states.len();",
|
|
||||||
p = self.prefix);
|
|
||||||
rust!(self.out,
|
|
||||||
"{p}states.truncate({p}states_len - {len});",
|
|
||||||
p = self.prefix,
|
|
||||||
len = production.symbols.len());
|
|
||||||
|
|
||||||
// push the produced value on the stack
|
// push the produced value on the stack
|
||||||
let name = self.variant_name_for_symbol(&Symbol::Nonterminal(production.nonterminal.clone()));
|
let name = self.variant_name_for_symbol(&Symbol::Nonterminal(production.nonterminal.clone()));
|
||||||
rust!(self.out,
|
rust!(self.out,
|
||||||
"{}symbols.push(({}start, {}Symbol::{}({}nt), {}end));",
|
"let {}symbol = ({}start, {}Symbol::{}({}nt), {}end);",
|
||||||
self.prefix,
|
self.prefix,
|
||||||
self.prefix,
|
self.prefix,
|
||||||
self.prefix,
|
self.prefix,
|
||||||
@ -927,7 +931,10 @@ impl<'ascent, 'grammar, W: Write> CodeGenerator<'ascent, 'grammar, W, TableDrive
|
|||||||
.iter()
|
.iter()
|
||||||
.position(|x| *x == production.nonterminal)
|
.position(|x| *x == production.nonterminal)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
rust!(self.out, "{}", index);
|
rust!(self.out, "({len}, {p}symbol, {index})",
|
||||||
|
p = self.prefix,
|
||||||
|
index = index,
|
||||||
|
len = production.symbols.len());
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user