forked from VimPlug/jedi
work in progress refactoring to make ListComprehension a sublass of ForFlow
This commit is contained in:
@@ -192,10 +192,10 @@ class Evaluator(object):
|
|||||||
next(call_path, None) # the first one has been used already
|
next(call_path, None) # the first one has been used already
|
||||||
return self.follow_path(call_path, r, element.parent)
|
return self.follow_path(call_path, r, element.parent)
|
||||||
elif isinstance(element, pr.ListComprehension):
|
elif isinstance(element, pr.ListComprehension):
|
||||||
loop = _evaluate_list_comprehension(element)
|
#lc = element.get_most_inner_lc()
|
||||||
# Caveat: parents are being changed, but this doesn't matter,
|
# Caveat: parents are being changed, but this doesn't matter,
|
||||||
# because nothing else uses it.
|
# because nothing else uses it.
|
||||||
element.stmt.parent = loop
|
#element.stmt.parent = loop
|
||||||
return self.eval_statement(element.stmt)
|
return self.eval_statement(element.stmt)
|
||||||
elif isinstance(element, pr.Lambda):
|
elif isinstance(element, pr.Lambda):
|
||||||
return [er.Function(self, element)]
|
return [er.Function(self, element)]
|
||||||
@@ -377,17 +377,3 @@ def filter_private_variable(scope, call_scope, var_name):
|
|||||||
if s != scope.base.base:
|
if s != scope.base.base:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def _evaluate_list_comprehension(lc, parent=None):
|
|
||||||
# create a for loop, which does the same as list comprehensions
|
|
||||||
input = lc.input
|
|
||||||
nested_lc = input.expression_list()[0]
|
|
||||||
if isinstance(nested_lc, pr.ListComprehension):
|
|
||||||
# is nested LC
|
|
||||||
input = nested_lc.stmt
|
|
||||||
loop = iterable.ListComprehensionFlow(lc, input, parent)
|
|
||||||
|
|
||||||
if isinstance(nested_lc, pr.ListComprehension):
|
|
||||||
loop = _evaluate_list_comprehension(nested_lc, loop)
|
|
||||||
return loop
|
|
||||||
|
|||||||
@@ -513,13 +513,3 @@ def create_indexes_or_slices(evaluator, index_array):
|
|||||||
return (Slice(evaluator, start, stop, step),)
|
return (Slice(evaluator, start, stop, step),)
|
||||||
else:
|
else:
|
||||||
return tuple(evaluator.process_precedence_element(prec))
|
return tuple(evaluator.process_precedence_element(prec))
|
||||||
|
|
||||||
|
|
||||||
class ListComprehensionFlow(pr.ForFlow):
|
|
||||||
"""Fake implementation to pretend being a ForFlow."""
|
|
||||||
def __init__(self, list_comprehension, input, parent):
|
|
||||||
lc = list_comprehension
|
|
||||||
sup = super(ListComprehensionFlow, self)
|
|
||||||
sup.__init__(helpers.FakeSubModule, [input], lc.parent.start_pos, lc.middle)
|
|
||||||
self.parent = parent or lc.get_parent_until(pr.IsScope)
|
|
||||||
self.list_comprehension = list_comprehension
|
|
||||||
|
|||||||
@@ -1094,7 +1094,7 @@ isinstance(c, (tokenize.Token, Operator)) else unicode(c)
|
|||||||
debug.warning('list comprehension in @%s', start_pos)
|
debug.warning('list comprehension in @%s', start_pos)
|
||||||
return None, tok
|
return None, tok
|
||||||
|
|
||||||
return ListComprehension(st, middle, in_clause, self), tok
|
return ListComprehension(self._sub_module, st, middle, in_clause, self), tok
|
||||||
|
|
||||||
# initializations
|
# initializations
|
||||||
result = []
|
result = []
|
||||||
@@ -1480,22 +1480,29 @@ class Name(Simple):
|
|||||||
return len(self.names)
|
return len(self.names)
|
||||||
|
|
||||||
|
|
||||||
class ListComprehension(Base):
|
class ListComprehension(ForFlow):
|
||||||
""" Helper class for list comprehensions """
|
""" Helper class for list comprehensions """
|
||||||
def __init__(self, stmt, middle, input, parent):
|
def __init__(self, module, stmt, middle, input, parent):
|
||||||
self.stmt = stmt
|
self.stmt = stmt
|
||||||
self.middle = middle
|
self.middle = middle
|
||||||
self.input = input
|
self.input = input
|
||||||
for s in stmt, middle, input:
|
for s in middle, input:
|
||||||
s.parent = self
|
s.parent = self
|
||||||
|
stmt.parent = self._get_most_inner_lc()
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
|
|
||||||
def get_parent_until(self, *args, **kwargs):
|
nested_lc = input.expression_list()[0]
|
||||||
return Simple.get_parent_until(self, *args, **kwargs)
|
if isinstance(nested_lc, ListComprehension):
|
||||||
|
# is nested LC
|
||||||
|
input = nested_lc.stmt
|
||||||
|
super(ListComprehension, self).__init__(module, [input],
|
||||||
|
stmt.start_pos, middle)
|
||||||
|
|
||||||
@property
|
def _get_most_inner_lc(self):
|
||||||
def start_pos(self):
|
nested_lc = self.input.expression_list()[0]
|
||||||
return self.stmt.start_pos
|
if isinstance(nested_lc, ListComprehension):
|
||||||
|
return nested_lc._get_most_inner_lc()
|
||||||
|
return self
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def end_pos(self):
|
def end_pos(self):
|
||||||
@@ -1510,6 +1517,31 @@ class ListComprehension(Base):
|
|||||||
return "%s for %s in %s" % tuple(code)
|
return "%s for %s in %s" % tuple(code)
|
||||||
|
|
||||||
|
|
||||||
|
def _evaluate_list_comprehension(lc, parent=None):
|
||||||
|
# create a for loop, which does the same as list comprehensions
|
||||||
|
input = lc.input
|
||||||
|
nested_lc = input.expression_list()[0]
|
||||||
|
if isinstance(nested_lc, ListComprehension):
|
||||||
|
# is nested LC
|
||||||
|
input = nested_lc.stmt
|
||||||
|
loop = ListComprehensionFlow(lc, input, parent)
|
||||||
|
|
||||||
|
if isinstance(nested_lc, ListComprehension):
|
||||||
|
loop = _evaluate_list_comprehension(nested_lc, loop)
|
||||||
|
return loop
|
||||||
|
|
||||||
|
|
||||||
|
class ListComprehensionFlow(ForFlow):
|
||||||
|
"""Fake implementation to pretend being a ForFlow."""
|
||||||
|
def __init__(self, list_comprehension, input, parent):
|
||||||
|
lc = list_comprehension
|
||||||
|
sup = super(ListComprehensionFlow, self)
|
||||||
|
module = list_comprehension.get_parent_until()
|
||||||
|
sup.__init__(module, [input], lc.parent.start_pos, lc.middle)
|
||||||
|
self.parent = parent or lc.get_parent_until(IsScope)
|
||||||
|
self.list_comprehension = list_comprehension
|
||||||
|
|
||||||
|
|
||||||
class Operator(Base):
|
class Operator(Base):
|
||||||
__slots__ = ('string', '_line', '_column')
|
__slots__ = ('string', '_line', '_column')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user