Refactor scripts in Tools/peg_generator/scripts (GH-20401)

This commit is contained in:
Lysandros Nikolaou 2020-06-06 07:21:40 +03:00 committed by GitHub
parent 2e6593db00
commit ba6fd87e41
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 146 additions and 147 deletions

View file

@ -42,6 +42,13 @@ from pegen.grammar import (
)
argparser = argparse.ArgumentParser(prog="graph_grammar", description="Graph a grammar tree",)
argparser.add_argument(
"-s",
"--start",
choices=["exec", "eval", "single"],
default="exec",
help="Choose the grammar's start rule (exec, eval or single)",
)
argparser.add_argument("grammar_file", help="The grammar file to graph")
@ -91,19 +98,15 @@ def main() -> None:
references[name] = set(references_for_item(rule))
# Flatten the start node if has only a single reference
root_node = "start"
if start := references["start"]:
if len(start) == 1:
root_node = list(start)[0]
del references["start"]
root_node = {"exec": "file", "eval": "eval", "single": "interactive"}[args.start]
print("digraph g1 {")
print('\toverlap="scale";') # Force twopi to scale the graph to avoid overlaps
print(f'\troot="{root_node}";')
print(f"\t{root_node} [color=green, shape=circle]")
print(f"\t{root_node} [color=green, shape=circle];")
for name, refs in references.items():
if refs: # Ignore empty sets
print(f"\t{name} -> {','.join(refs)};")
for ref in refs:
print(f"\t{name} -> {ref};")
print("}")