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(details, parent=None, name=None)¶
Common base class.
- Parameters
details – the parse tree
parent (
None
orNode
instance) –None
to indicate that the node is the root of the parse tree, otherwise it is the immediate parent of the new nodename (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
ast.Node
produces aNode
instance, alist
ortuple
produces aList
instance, everything else aScalar
instance.
- class pglast.node.Comment(location, text, at_start_of_line, continue_previous)¶
A structure to carry information about a single SQL comment.
- property at_start_of_line¶
Alias for field number 2
- property continue_previous¶
Alias for field number 3
- property location¶
Alias for field number 0
- property text¶
Alias for field number 1
- class pglast.node.List(details, parent=None, name=None)¶
Represent a sequence of
Node
instances.- Parameters
items (list) – a list of items, usually
Node
instancesparent (
None
orNode
instance) –None
to indicate that the node is the root of the parse tree, otherwise it is the immediate parent of the new nodename (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(details, parent=None, name=None)¶
Represent a single entry in a parse tree.
- Parameters
details (
ast.Node
) – the parse tree of the nodeparent (
None
orNode
instance) –None
to indicate that the node is the root of the parse tree, otherwise it is the immediate parent of the new nodename (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
- property 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(details, parent=None, name=None)¶
Represent a single scalar value.