1
0
forked from VimPlug/jedi

Start implementing an algorithm for actually evaluating the parser tree.

This commit is contained in:
Dave Halter
2014-10-10 11:29:03 +02:00
parent 66840a742c
commit 3bf1fec568
4 changed files with 27 additions and 9 deletions

View File

@@ -287,9 +287,14 @@ class Script(object):
if user_stmt is None: if user_stmt is None:
# Set the start_pos to a pseudo position, that doesn't exist but works # Set the start_pos to a pseudo position, that doesn't exist but works
# perfectly well (for both completions in docstrings and statements). # perfectly well (for both completions in docstrings and statements).
stmt.start_pos = self._pos pos = self._pos
else: else:
stmt.start_pos = user_stmt.start_pos pos = user_stmt.start_pos
child = stmt
while hasattr(child, 'children'):
child = child.children[0]
child.start_pos = pos
stmt.parent = self._parser.user_scope() stmt.parent = self._parser.user_scope()
return stmt return stmt

View File

@@ -130,7 +130,7 @@ class Evaluator(object):
if isinstance(stmt, FakeStatement): if isinstance(stmt, FakeStatement):
return expression_list # Already contains the results. return expression_list # Already contains the results.
result = self.eval_expression_list(expression_list) result = self.eval_element(stmt.children[0])
ass_details = stmt.assignment_details ass_details = stmt.assignment_details
if ass_details and ass_details[0][1] != '=' and not isinstance(stmt, er.InstanceElement): # TODO don't check for this. if ass_details and ass_details[0][1] != '=' and not isinstance(stmt, er.InstanceElement): # TODO don't check for this.
@@ -161,6 +161,14 @@ class Evaluator(object):
result = new_result result = new_result
return result return result
def eval_element(self, element):
if isinstance(element, pr.Name):
stmt = element.get_parent_until(pr.ExprStmt)
return self.find_types(stmt.parent, element, stmt.start_pos,
search_global=True)
else:
raise NotImplementedError
def eval_expression_list(self, expression_list): def eval_expression_list(self, expression_list):
""" """
`expression_list` can be either `pr.Array` or `list of list`. `expression_list` can be either `pr.Array` or `list of list`.

View File

@@ -57,7 +57,8 @@ class Parser(object):
# and only if the refactor method's write parameter was True. # and only if the refactor method's write parameter was True.
logger = logging.getLogger("RefactoringTool") logger = logging.getLogger("RefactoringTool")
d = Driver(pytree.python_grammar, convert=pytree.convert, logger=logger) d = Driver(pytree.python_grammar, convert=pytree.convert, logger=logger)
self.module = d.parse_string(source) print(repr(source))
self.module = d.parse_string(source).get_parent_until()
def __init__old__(self, source, module_path=None, no_docstr=False, def __init__old__(self, source, module_path=None, no_docstr=False,
tokenizer=None, top_module=None): tokenizer=None, top_module=None):

View File

@@ -175,6 +175,7 @@ class _Leaf(Base):
self.value = value self.value = value
self.start_pos = start_pos self.start_pos = start_pos
self.prefix = prefix self.prefix = prefix
self.parent = None
@property @property
def end_pos(self): def end_pos(self):
@@ -262,9 +263,10 @@ class Simple(Base):
""" """
Initialize :class:`Simple`. Initialize :class:`Simple`.
:type children: :class:`SubModule`
:param children: The module in which this Python object locates. :param children: The module in which this Python object locates.
""" """
for c in children:
c.parent = self
self.children = children self.children = children
self.parent = None self.parent = None
@@ -319,14 +321,13 @@ class Scope(Simple, DocstringMixin):
:param start_pos: The position (line and column) of the scope. :param start_pos: The position (line and column) of the scope.
:type start_pos: tuple(int, int) :type start_pos: tuple(int, int)
""" """
__slots__ = ('subscopes', 'imports', 'statements', '_doc_token', 'asserts', __slots__ = ('subscopes', 'imports', '_doc_token', 'asserts',
'returns', 'is_generator', '_names_dict') 'returns', 'is_generator', '_names_dict')
def __init__(self, children): def __init__(self, children):
super(Scope, self).__init__(children) super(Scope, self).__init__(children)
self.subscopes = [] self.subscopes = []
self.imports = [] self.imports = []
self.statements = []
self._doc_token = None self._doc_token = None
self.asserts = [] self.asserts = []
# Needed here for fast_parser, because the fast_parser splits and # Needed here for fast_parser, because the fast_parser splits and
@@ -335,6 +336,10 @@ class Scope(Simple, DocstringMixin):
self._names_dict = defaultdict(_return_empty_list) self._names_dict = defaultdict(_return_empty_list)
self.is_generator = False self.is_generator = False
@property
def statements(self):
return [c for c in self.children if isinstance(c, ExprStmt)]
def is_scope(self): def is_scope(self):
return True return True
@@ -927,6 +932,7 @@ class Statement(Simple, DocstringMixin):
self.expression_list() self.expression_list()
def get_defined_names(self): def get_defined_names(self):
return []
"""Get the names for the statement.""" """Get the names for the statement."""
if self._set_vars is None: if self._set_vars is None:
@@ -997,8 +1003,6 @@ class Statement(Simple, DocstringMixin):
would result in ``[(Name(x), '='), (Array([Name(y), Name(z)]), '=')]``. would result in ``[(Name(x), '='), (Array([Name(y), Name(z)]), '=')]``.
""" """
# parse statement which creates the assignment details.
self.expression_list()
return self._assignment_details return self._assignment_details
@cache.underscore_memoization @cache.underscore_memoization