package pyre-ast

  1. Overview
  2. Docs

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

An implementation note: if the client provides a tagless-final specification that contains side-effects, then the order in which various syntax constructs gets recursed into by these APIs starts to matter. This library generally adopts the following order:

  • Traversals are post-order: child constructs will be visited before the parent constructs.
  • If a parent construct contains more than one child constructs, these child constructs will be visited in their order of appearance (e.g. for PyreAst.TaglessFinal.Expression.name, the location field will be visited first, then name, and finally ctx).
  • For list-like constructs, however, the visiting order will be the reverse of the appearance order (e.g. when parsing a module x = 1; y = 2, the second statement y = 2 will be visited before the first statement x = 1).
val parse_module : context:Context.t -> spec: (_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 'module_, _, _, _, _, _, _) TaglessFinal.t -> ?enable_type_comment:bool -> string -> ('module_, Error.t) Result.t

parse_module ~context ~spec input takes the string input and parse it as Python module using tagless-final specification spec. See documentation of Context.t for the meaning of the context argument.

Optionally an enable_type_comment argument can be specified. If it is true, the parser will attempt to populate the type_comment section of each AST node that has it. Otherwise, contents in comments will all get ignored and type_comment will always be unset.

val parse_expression : context:Context.t -> spec: (_, _, _, _, _, _, _, _, 'expression, _, _, _, _, _, _, _, _, _, _, _, _, _, _) TaglessFinal.t -> string -> ('expression, Error.t) Result.t

parse_expression ~context ~spec input takes the string input and parse it as Python expression using tagless-final specification spec. See documentation of Context.t for the meaning of the context argument.

Optionally an enable_type_comment argument can be specified. If it is true, the parser will attempt to populate the type_comment section of each AST node that has it. Otherwise, contents in comments will all get ignored and type_comment will always be unset.

val parse_function_type : context:Context.t -> spec: (_, _, _, _, _, _, _, _, _, _, 'function_type, _, _, _, _, _, _, _, _, _, _, _, _) TaglessFinal.t -> string -> ('function_type, Error.t) Result.t

parse_expression ~context ~spec input takes the string input and parse it as Python function type signature using tagless-final specification spec. See documentation of Context.t for the meaning of the context argument.

Function type signature is not a reified construct in the core Python langugage. They only appears in Python2-style typing comments for functions and are superceded by the inline annotation syntax added in Python3. This API is provided here for completeness, in case downstream clients want to support the old comment-style annotation.

OCaml

Innovation. Community. Security.