Skip to content

Commit

Permalink
Add an "ancestors" slot to all AST nodes
Browse files Browse the repository at this point in the history
This is the first step to implement issue #80, making it easier to get
rid of the old Node wrappers, whose main functionality in v3 was to keep
track of the "parent_node"/"parent_member" information.
  • Loading branch information
lelit committed May 28, 2022
1 parent f752ca9 commit 901c75e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pglast/ast.py
Expand Up @@ -58,7 +58,7 @@ def __repr__(self):
class Node:
"Base class for all AST nodes."

__slots__ = ()
__slots__ = ('ancestors',)

def __init__(self, data):
if not isinstance(data, dict): # pragma: no cover
Expand Down Expand Up @@ -141,7 +141,7 @@ def __setattr__(self, name, value):
attribute, raising opportune exception when that is not possible.
'''

if value is not None:
if value is not None and name in self.__slots__:
ctype, ptype, adaptor = self.__slots__[name]
if not isinstance(ptype, tuple):
ptype = (ptype,)
Expand Down
8 changes: 7 additions & 1 deletion pglast/stream.py
Expand Up @@ -11,8 +11,8 @@
from re import match
from sys import stderr

from . import parse_plpgsql, parse_sql
from .node import List, Missing, Node, Scalar
from . import ast, parse_plpgsql, parse_sql, visitors
from .keywords import RESERVED_KEYWORDS, TYPE_FUNC_NAME_KEYWORDS
from .printers import get_printer_for_node_tag, get_special_function

Expand Down Expand Up @@ -176,6 +176,12 @@ def __call__(self, sql, plpgsql=False):
" a node.Node instance, a node.List, an ast.Node or tuple of"
" them, got %r" % type(sql))

class UpdateAncestors(visitors.Visitor):
def visit(self, ancestors, node):
node.ancestors = ancestors

UpdateAncestors()(sql)

first = True
for statement in sql:
if first:
Expand Down
4 changes: 2 additions & 2 deletions tools/extract_ast.py
Expand Up @@ -78,7 +78,7 @@ def __repr__(self):
class Node:
"Base class for all AST nodes."
__slots__ = ()
__slots__ = ('ancestors',)
def __init__(self, data):
if not isinstance(data, dict): # pragma: no cover
Expand Down Expand Up @@ -161,7 +161,7 @@ def __setattr__(self, name, value):
attribute, raising opportune exception when that is not possible.
'''
if value is not None:
if value is not None and name in self.__slots__:
ctype, ptype, adaptor = self.__slots__[name]
if not isinstance(ptype, tuple):
ptype = (ptype,)
Expand Down

0 comments on commit 901c75e

Please sign in to comment.