[Python-Dev] Exposing the abstract syntax

martin at v.loewis.de martin at v.loewis.de
Sun Feb 26 20:59:09 CET 2006


At PyCon, there was general reluctance for incorporating
the ast-objects branch, primarily because people where
concerned what the reference counting would do to
maintainability, and what (potentially troublesome)
options direct exposure of AST objects would do.

OTOH, the approach of creating a shadow tree did not
find opposition, so I implemented that.

Currently, you can use compile() to create an AST
out of source code, by passing PyCF_ONLY_AST (0x400)
to compile. The mapping of AST to Python objects
is as follows:

- there is a Python type for every sum, product,
  and constructor.
- The constructor types inherit from their sum
  types (e.g. ClassDef inherits from stmt)
- Each constructor and product type has an
  _fields member, giving the names of the fields
  of the product.
- Each node in the AST has members with the names
  given in _fields
- If the field is optional, it might be None
- if the field is zero-or-more, it is represented
  as a list.

It might be reasonable to expose this through
a separate module, in particular to provide
access to the type objects.

Regards,
Martin






More information about the Python-Dev mailing list