Skip to content
Snippets Groups Projects
Commit 53e9cd50975e authored by Sylvain Thenault's avatar Sylvain Thenault
Browse files

set variable scope to the outer common parent scope

parent d38764ab62ef
No related branches found
No related tags found
No related merge requests found
...@@ -26,7 +26,8 @@ ...@@ -26,7 +26,8 @@
from rql import CoercionError from rql import CoercionError
from rql.base import BaseNode, Node, BinaryNode, LeafNode from rql.base import BaseNode, Node, BinaryNode, LeafNode
from rql.utils import function_description, quote, uquote, build_visitor_stub from rql.utils import (function_description, quote, uquote, build_visitor_stub,
common_parent)
CONSTANT_TYPES = frozenset((None, 'Date', 'Datetime', 'Boolean', 'Float', 'Int', CONSTANT_TYPES = frozenset((None, 'Date', 'Datetime', 'Boolean', 'Float', 'Int',
'String', 'Substitute', 'etype')) 'String', 'Substitute', 'etype'))
...@@ -949,11 +950,8 @@ ...@@ -949,11 +950,8 @@
def set_scope(self, scopenode): def set_scope(self, scopenode):
if scopenode is self.stmt or self.stinfo['scope'] is None: if scopenode is self.stmt or self.stinfo['scope'] is None:
self.stinfo['scope'] = scopenode self.stinfo['scope'] = scopenode
elif self.stinfo['scope'] is not self.stmt and scopenode is not self.stinfo['scope']: elif not (self.stinfo['scope'] is self.stmt or scopenode is self.stinfo['scope']):
# XXX get common parent scope if this assertion fail self.stinfo['scope'] = common_parent(self.stinfo['scope'], scopenode)
assert scopenode.parent.scope is self.stinfo['scope'].parent.scope, \
(scopenode.parent.scope, self.stinfo['scope'].parent.scope)
self.stinfo['scope'] = scopenode.parent.scope
def get_scope(self): def get_scope(self):
return self.stinfo['scope'] return self.stinfo['scope']
...@@ -985,6 +983,7 @@ ...@@ -985,6 +983,7 @@
return rel return rel
return None return None
build_visitor_stub((SubQuery, And, Or, Not, Exists, Relation, build_visitor_stub((SubQuery, And, Or, Not, Exists, Relation,
Comparison, MathExpression, Function, Constant, Comparison, MathExpression, Function, Constant,
VariableRef, SortTerm, ColumnAlias, Variable)) VariableRef, SortTerm, ColumnAlias, Variable))
...@@ -73,6 +73,25 @@ ...@@ -73,6 +73,25 @@
"""Return true if the given word is a RQL keyword.""" """Return true if the given word is a RQL keyword."""
return word.upper() in KEYWORDS return word.upper() in KEYWORDS
def common_parent(node1, node2):
"""return the first common parent between node1 and node2
algorithm :
1) index node1's parents
2) climb among node2's parents until we find a common parent
"""
# index node1's parents
node1_parents = set()
while node1:
node1_parents.add(node1)
node1 = node1.parent
# climb among node2's parents until we find a common parent
while node2:
if node2 in node1_parents:
return node2
node2 = node2.parent
raise Exception('DUH!')
FUNCTIONS = _GenericAdvFuncHelper.FUNCTIONS.copy() FUNCTIONS = _GenericAdvFuncHelper.FUNCTIONS.copy()
def register_function(funcdef): def register_function(funcdef):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment