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

Class ASDLParser

Parser/asdl.py:267–399  ·  view source on GitHub ↗

Parser for ASDL files. Create, then call the parse method on a buffer containing ASDL. This is a simple recursive descent parser that uses tokenize_asdl for the lexing.

Source from the content-addressed store, hash-verified

265 yield Token(op_kind, c, lineno)
266
267class ASDLParser:
268 """Parser for ASDL files.
269
270 Create, then call the parse method on a buffer containing ASDL.
271 This is a simple recursive descent parser that uses tokenize_asdl for the
272 lexing.
273 """
274 def __init__(self):
275 self._tokenizer = None
276 self.cur_token = None
277
278 def parse(self, buf):
279 """Parse the ASDL in the buffer and return an AST with a Module root.
280 """
281 self._tokenizer = tokenize_asdl(buf)
282 self._advance()
283 return self._parse_module()
284
285 def _parse_module(self):
286 if self._at_keyword('module'):
287 self._advance()
288 else:
289 raise ASDLSyntaxError(
290 'Expected "module" (found {})'.format(self.cur_token.value),
291 self.cur_token.lineno)
292 name = self._match(self._id_kinds)
293 self._match(TokenKind.LBrace)
294 defs = self._parse_definitions()
295 self._match(TokenKind.RBrace)
296 return Module(name, defs)
297
298 def _parse_definitions(self):
299 defs = []
300 while self.cur_token.kind == TokenKind.TypeId:
301 typename = self._advance()
302 self._match(TokenKind.Equals)
303 type = self._parse_type()
304 defs.append(Type(typename, type))
305 return defs
306
307 def _parse_type(self):
308 if self.cur_token.kind == TokenKind.LParen:
309 # If we see a (, it's a product
310 return self._parse_product()
311 else:
312 # Otherwise it's a sum. Look for ConstructorId
313 sumlist = [Constructor(self._match(TokenKind.ConstructorId),
314 self._parse_optional_fields())]
315 while self.cur_token.kind == TokenKind.Pipe:
316 # More constructors
317 self._advance()
318 sumlist.append(Constructor(
319 self._match(TokenKind.ConstructorId),
320 self._parse_optional_fields()))
321 return Sum(sumlist, self._parse_optional_attributes())
322
323 def _parse_product(self):
324 return Product(self._parse_fields(), self._parse_optional_attributes())

Callers 1

parseFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…