Look I'm just thinking hard about converting to a parser generator because I want to derive the pretty-printer from the parser without having to repeat myself all over the place. This parser.py is derived from my old LRParsers project, and should go back there eventually, but for now I'm driving the work from here.
40 lines
802 B
Python
40 lines
802 B
Python
import io
|
|
|
|
import parser
|
|
|
|
|
|
def generate_rust_parser(output: io.TextIOBase, table: list[dict[str, parser.Action]]):
|
|
lines = []
|
|
|
|
tree_kinds = list(
|
|
sorted(
|
|
{
|
|
action[1]
|
|
for state in table
|
|
for action in state.values()
|
|
if action[0] == "reduce" and action[1][0] != "_"
|
|
}
|
|
)
|
|
)
|
|
|
|
# First, generate the treekind enumeration
|
|
lines.extend(
|
|
[
|
|
"#[derive(Debug, Eq, PartialEq)]",
|
|
"pub enum TreeKind {",
|
|
" Error,",
|
|
"",
|
|
]
|
|
)
|
|
lines.extend(f" {kind}," for kind in tree_kinds)
|
|
lines.extend(
|
|
[
|
|
"}",
|
|
"",
|
|
]
|
|
)
|
|
|
|
# Next generate the parse table
|
|
lines.extend([])
|
|
|
|
pass
|