Skip to main content

Compiling a lib2to3 CST to a Python AST

Project description

🍞 lib2toast

This library converts a lib2to3 concrete syntax tree (CST) to a standard Python AST.

Potential use cases include:

  • Parsing Python code with less dependence on the Python version
  • Extending or modifying the Python grammar in order to experiment with new features or to create a Python-like dialect

Usage

This library is still at an early stage and the API may change.

  • lib2toast.api.compile(code, *, grammar=..., compiler=...): Compile a string of code to an AST. This AST can then be compiled to a Python code object or executed with the built-in compile() and exec() functions. By default, this uses a grammar that covers all syntax that is accepted by the latest version of Python, plus some additional syntax. Pass a custom grammar to use different syntax. You can use lib2toast.api.load_grammar to load a grammar object from a file. If you do this, you'll usually also want to pass a custom compiler object by subclassing lib2toast.compile.Compiler.
  • lib2toast.api.run(code, *, filename=..., grammar=..., compiler=...): Compiles code and then immediately executes it.
  • lib2toast.api.load_grammar(path, *, async_keywords=True): Load a grammar file from a path. If async_keywords is True, treats async as a keyword as in Python 3.7+.

There is also a command-line interface: python -m lib2toast -c code runs code after parsing it using lib2toast.

Showcase

The command-line interface shows that lib2toast supports parsing some new syntax in older Python versions:

$ python3.9 -m lib2toast -c 'print(f"{"x"}")'
x

This is new syntax introduced in Python 3.12 by PEP 701.

It also supports some (not all) Python 2 syntax that was removed in Python 3:

$ python3.9 -m lib2toast -c 'print(1 <> 2)'
True

The test suite shows some examples of syntactic variants of Python parsed with lib2toast. For example:

dataclass(frozen=True) C:
    x: int
    y: int = 0

Implementation

lib2toast is implemented on top of blib2to3, the fork of lib2to3 maintained by the Black project in order to parse and format Python code. It originates from lib2to3, a tool shipped with earlier Python 3 versions to support converting between Python 2 and 3 code.

The core part of the implementation is a tool that converts Python code to an AST. This makes it easy to test for correctness: just run Python's built-in ast.parse and assert that it produces the same tree, including line and column numbers. So far I have tested the compiler on lib2toast's own code as well as some of Black's code (the Black test cases were especially helpful), but there are probably more bugs.

Python version support

This library supports Python versions 3.9 and up.

Python 3.8 is unsupported because it is about to reach the end of its support period, the AST structure is quite different between 3.8 and 3.9, and I don't have a use case for 3.8.

In the future I plan to support all supported upstream versions of Python.

Contributing

Contributions to this project are welcome, including ideas for new ways to use the core functionality of the library.

Check the "Issues" tab for potential areas to contribute.

Release notes

Version 0.1.0 (August 6, 2024)

  • Fix miscompilation of Unicode identifiers; they are now NFKC-normalized
  • Fix crash on augmented assignment with an unparenthesized tuple on the right-hand side
  • Fix crash on nested async comprehensions
  • Fix incorrect line ranges in suites containing statements ending in a semicolon
  • Fix crash on statement ending in a semicolon
  • Fix crash on certain triple-quoted f-strings
  • Fix miscompilation of calls with arguments on the left-hand side of an assignment
  • Make it easier to create a dialect with more kinds of trailers
  • Adjust some type annotations to make subclassing easier

Version 0.0.1 (July 1, 2024)

Initial release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lib2toast-0.1.0.tar.gz (31.2 kB view details)

Uploaded Source

Built Distribution

lib2toast-0.1.0-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

Details for the file lib2toast-0.1.0.tar.gz.

File metadata

  • Download URL: lib2toast-0.1.0.tar.gz
  • Upload date:
  • Size: 31.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for lib2toast-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b55cf438d7be8c5ce189fe85e5c49698162815c59f2d8f5ec5afa54c6813f64e
MD5 ee3625460cacd8a9caa2e27f7f5b5d8c
BLAKE2b-256 32da383891b06ae4a364511b2d3a0e15eb01a2ff769edb8531aa9893d7e9688d

See more details on using hashes here.

File details

Details for the file lib2toast-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: lib2toast-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for lib2toast-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7ebd3a5ccdcde3f46033511e0ab271da84a2073b48bcfea65011a130ad097494
MD5 8c3af17edde470e6f74327a947b8f90b
BLAKE2b-256 3a13d6820c459e6988fb35079ff5d07f660bfef1b956a5cc29d78519a5bbbced

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page