MCPcopy Index your code
hub / github.com/python/cpython / parse_clinic_block

Method parse_clinic_block

Tools/clinic/libclinic/block_parser.py:172–256  ·  view source on GitHub ↗
(self, dsl_name: str)

Source from the content-addressed store, hash-verified

170 return Block("".join(lines))
171
172 def parse_clinic_block(self, dsl_name: str) -> Block:
173 in_lines = []
174 self.block_start_line_number = self.line_number + 1
175 stop_line = self.language.stop_line.format(dsl_name=dsl_name)
176 body_prefix = self.language.body_prefix.format(dsl_name=dsl_name)
177
178 def is_stop_line(line: str) -> bool:
179 # make sure to recognize stop line even if it
180 # doesn't end with EOL (it could be the very end of the file)
181 if line.startswith(stop_line):
182 remainder = line.removeprefix(stop_line)
183 if remainder and not remainder.isspace():
184 fail(f"Garbage after stop line: {remainder!r}")
185 return True
186 else:
187 # gh-92256: don't allow incorrectly formatted stop lines
188 if line.lstrip().startswith(stop_line):
189 fail(f"Whitespace is not allowed before the stop line: {line!r}")
190 return False
191
192 # consume body of program
193 while self.input:
194 line = self._line()
195 if is_stop_line(line) or self.is_start_line(line):
196 break
197 if body_prefix:
198 line = line.lstrip()
199 assert line.startswith(body_prefix)
200 line = line.removeprefix(body_prefix)
201 in_lines.append(line)
202
203 # consume output and checksum line, if present.
204 if self.last_dsl_name == dsl_name:
205 checksum_re = self.last_checksum_re
206 else:
207 before, _, after = self.language.checksum_line.format(dsl_name=dsl_name, arguments='{arguments}').partition('{arguments}')
208 assert _ == '{arguments}'
209 checksum_re = libclinic.create_regex(before, after, word=False)
210 self.last_dsl_name = dsl_name
211 self.last_checksum_re = checksum_re
212 assert checksum_re is not None
213
214 # scan forward for checksum line
215 out_lines = []
216 arguments = None
217 while self.input:
218 line = self._line(lookahead=True)
219 match = checksum_re.match(line.lstrip())
220 arguments = match.group(1) if match else None
221 if arguments:
222 break
223 out_lines.append(line)
224 if self.is_start_line(line):
225 break
226
227 output: str | None
228 output = "".join(out_lines)
229 if arguments:

Callers 1

__next__Method · 0.95

Calls 15

_lineMethod · 0.95
is_start_lineMethod · 0.95
failFunction · 0.90
BlockClass · 0.70
formatMethod · 0.45
lstripMethod · 0.45
startswithMethod · 0.45
removeprefixMethod · 0.45
appendMethod · 0.45
partitionMethod · 0.45
matchMethod · 0.45
groupMethod · 0.45

Tested by

no test coverage detected