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

Method parse_graminit_c

src/blib2to3/pgen2/conv.py:85–246  ·  view source on GitHub ↗

Parse the .c file written by pgen. (Internal) The file looks as follows. The first two lines are always this: #include "pgenheaders.h" #include "grammar.h" After that come four blocks: 1) one or more state definitions 2) a table defining dfas

(self, filename)

Source from the content-addressed store, hash-verified

83 return True
84
85 def parse_graminit_c(self, filename):
86 """Parse the .c file written by pgen. (Internal)
87
88 The file looks as follows. The first two lines are always this:
89
90 #include "pgenheaders.h"
91 #include "grammar.h"
92
93 After that come four blocks:
94
95 1) one or more state definitions
96 2) a table defining dfas
97 3) a table defining labels
98 4) a struct defining the grammar
99
100 A state definition has the following form:
101 - one or more arc arrays, each of the form:
102 static arc arcs_<n>_<m>[<k>] = {
103 {<i>, <j>},
104 ...
105 };
106 - followed by a state array, of the form:
107 static state states_<s>[<t>] = {
108 {<k>, arcs_<n>_<m>},
109 ...
110 };
111
112 """
113 try:
114 f = open(filename)
115 except OSError as err:
116 print(f"Can't open {filename}: {err}")
117 return False
118 # The code below essentially uses f's iterator-ness!
119 lineno = 0
120
121 # Expect the two #include lines
122 lineno, line = lineno + 1, next(f)
123 assert line == '#include "pgenheaders.h"\n', (lineno, line)
124 lineno, line = lineno + 1, next(f)
125 assert line == '#include "grammar.h"\n', (lineno, line)
126
127 # Parse the state definitions
128 lineno, line = lineno + 1, next(f)
129 allarcs = {}
130 states = []
131 while line.startswith("static arc "):
132 while line.startswith("static arc "):
133 mo = re.match(r"static arc arcs_(\d+)_(\d+)\[(\d+)\] = {$", line)
134 assert mo, (lineno, line)
135 n, m, k = list(map(int, mo.groups()))
136 arcs = []
137 for _ in range(k):
138 lineno, line = lineno + 1, next(f)
139 mo = re.match(r"\s+{(\d+), (\d+)},$", line)
140 assert mo, (lineno, line)
141 i, j = list(map(int, mo.groups()))
142 arcs.append((i, j))

Callers 1

runMethod · 0.95

Calls 2

matchMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected