mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 05:31:20 +00:00 
			
		
		
		
	Refactor scripts in Tools/peg_generator/scripts (GH-20401)
(cherry picked from commit ba6fd87e41)
Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
			
			
This commit is contained in:
		
							parent
							
								
									d5e7348e41
								
							
						
					
					
						commit
						18f1226884
					
				
					 7 changed files with 146 additions and 147 deletions
				
			
		|  | @ -42,6 +42,13 @@ | |||
| ) | ||||
| 
 | ||||
| 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
	
	 Miss Islington (bot)
						Miss Islington (bot)