mirror of
https://github.com/python/cpython.git
synced 2025-07-29 06:05:00 +00:00
Refactor scripts in Tools/peg_generator/scripts (GH-20401)
This commit is contained in:
parent
2e6593db00
commit
ba6fd87e41
7 changed files with 146 additions and 147 deletions
|
@ -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("}")
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue