package pyre-ast

  1. Overview
  2. Docs
Full-fidelity Python parser in OCaml

Install

Dune Dependency

Authors

Maintainers

Sources

pyre-ast-0.1.2.tbz
sha256=f4d334053d6909a9ecc8c8df4817c49058ab1e40ac62ce104c7c8e63d29d99a2
sha512=77ec951bb9f7dd280f95afd5e2773910e09c092366eeb55af33a39fd68060c7d79650d555285f6adf686e1dc984a88a230c83c6d4d89293f9e761060d7fa5409

doc/pyre-ast/PyreAst/Parser/index.html

Module PyreAst.ParserSource

This module contains all parsing APIs, i.e. functions that transfrom plain strings into syntactical constructs. These APIs will always hand back a Result.t where if the parsing fails, a Parser.Error.t gets returned.

Under the hood, this library actually compiles and calls into the actual CPython parser code (with PyCF_TYPE_COMMENTS flag enabled), and then it walk through the CPython AST and translate them into OCaml structures via C bindings. This is how 100% fidelity with the official CPython implementation is achieved -- we are actually relying on exactly the same parser implementation that CPython uses. This approach has some notable implications:

  • The parsing APIs are stateful as one need to intialize/finalize CPython runtime before invoking its parser. The low-level details are abstracted away with the Parser.Context module, but the fact that no parsing can be done prior to obtaining a Parser.Context.t still holds.
  • As of the latest release, I have yet to find an easy way for the parser to handle Unicode identifier names. Unicode characters in string literals are fine, but Unicode identifier name is something that a barely-initialized CPython runtime cannot handle even with utf-8 mode enabled.
Sourcemodule Context : sig ... end

This module contains a type that abstracts away the details of global states required to set up the parser.

module Error : sig ... end

This module contains a type that represents parsing errors.

Sourceval with_context : ?on_init_failure:(unit -> 'a) -> (Context.t -> 'a) -> 'a

with_context ?on_init_failure f first creates a value c of type Context.t and then invoke f on c. It is guaranteed that the created context c will be destroyed in the end regardless of whether f raises an exception or not.

If the creation of c fails, on_init_failure () will be invoked, and f will not be called. By default, if not explicitly overriden then on_init_failure would simply raise a Failure.

Sourcemodule TaglessFinal : sig ... end

This module provides parsing APIs for downstream clients that are written in tagless-final style. See PyreAst.TaglessFinal for more explanation about this style.

Sourcemodule Concrete : sig ... end

This module provides parsing APIs for downstream clients that are written in the traditional "initial" style which expects a concrete ADT representation for abstract syntax trees.

OCaml

Innovation. Community. Security.