MCPcopy
hub / github.com/psf/black / parse_graminit_h

Method parse_graminit_h

src/blib2to3/pgen2/conv.py:55–83  ·  view source on GitHub ↗

Parse the .h file written by pgen. (Internal) This file is a sequence of #define statements defining the nonterminals of the grammar as numbers. We build two tables mapping the numbers to names and back.

(self, filename)

Source from the content-addressed store, hash-verified

53 self.finish_off()
54
55 def parse_graminit_h(self, filename):
56 """Parse the .h file written by pgen. (Internal)
57
58 This file is a sequence of #define statements defining the
59 nonterminals of the grammar as numbers. We build two tables
60 mapping the numbers to names and back.
61
62 """
63 try:
64 f = open(filename)
65 except OSError as err:
66 print(f"Can't open {filename}: {err}")
67 return False
68 self.symbol2number = {}
69 self.number2symbol = {}
70 lineno = 0
71 for line in f:
72 lineno += 1
73 mo = re.match(r"^#define\s+(\w+)\s+(\d+)$", line)
74 if not mo and line.strip():
75 print(f"{filename}({lineno}): can't parse {line.strip()}")
76 else:
77 symbol, number = mo.groups()
78 number = int(number)
79 assert symbol not in self.symbol2number
80 assert number not in self.number2symbol
81 self.symbol2number[symbol] = number
82 self.number2symbol[number] = symbol
83 return True
84
85 def parse_graminit_c(self, filename):
86 """Parse the .c file written by pgen. (Internal)

Callers 1

runMethod · 0.95

Calls 1

matchMethod · 0.45

Tested by

no test coverage detected