pglast.node — The higher level interface to the parse tree

This module implements a set of classes that make it easier to deal with the pglast.ast nodes.

The pglast.node.Node wraps a single pglast.ast.Node adding a reference to the parent node; the class:pglast.node.List wraps a sequence of them and pglast.node.Scalar represents plain values such a strings, integers, booleans or none.

Every node is identified by a tag, a string label that characterizes its content, exposed as a set of attributes as well as with a dictionary-like interface (technically pglast.node.Node implements both a __getattr__ method and a __getitem__ method, while underlying pglast.ast.Node only the former). When asked for an attribute, the node returns an instance of the base classes, i.e. another Node, or a List or a Scalar, depending on the data type of that item. When the node does not contain the requested attribute it returns a singleton pglast.node.Missing marker instance.

A List wraps a plain Python list and may contains a sequence of Node instances, or in some cases other sub-lists, that can be accessed with the usual syntax, or iterated.

Finally, a Scalar carries a single value of some scalar type, accessible through its value attribute.

class pglast.node.Base

Common base class.

Parameters:
  • details – the parse tree
  • parent (None or Node instance) – None to indicate that the node is the root of the parse tree, otherwise it is the immediate parent of the new node
  • name (str or tuple) – the name of the attribute in the parent node that points to this one; it may be a tuple (name, position) when parent[name] is actually a list of nodes

Its main purpose is to create the right kind of instance, depending on the type of the details argument passed to the constructor: a dict produces a Node instance, a list produces a List instance, everything else a Scalar instance.

parent_attribute

The attribute in the parent Node referencing this element.

parent_node

The parent Node of this element.

class pglast.node.Comment(location, text, at_start_of_line, continue_previous)

A structure to carry information about a single SQL comment.

at_start_of_line

Alias for field number 2

continue_previous

Alias for field number 3

location

Alias for field number 0

text

Alias for field number 1

class pglast.node.List

Represent a sequence of Node instances.

Parameters:
  • items (list) – a list of items, usually Node instances
  • parent (None or Node instance) – None to indicate that the node is the root of the parse tree, otherwise it is the immediate parent of the new node
  • name (str or tuple) – the name of the attribute in the parent node that points to this one; it may be a tuple (name, position) when parent[name] is actually a list of nodes
traverse()

A generator that recursively traverse all the items in the list.

pglast.node.Missing = MISSING

Singleton returned when trying to get a non-existing attribute out of a Node.

class pglast.node.Node

Represent a single entry in a parse tree.

Parameters:
  • details (ast.Node) – the parse tree of the node
  • parent (None or Node instance) – None to indicate that the node is the root of the parse tree, otherwise it is the immediate parent of the new node
  • name (str or tuple) – the name of the attribute in the parent node that points to this one; it may be a tuple (name, position) when parent[name] is actually a list of nodes
attribute_names

The names of the attributes present in the parse tree of the node.

traverse()

A generator that recursively traverse all attributes of the node.

class pglast.node.Scalar

Represent a single scalar value.