1
0
forked from VimPlug/jedi

Remove used_names from the parser step. It's a separate iteration, now.

This commit is contained in:
Dave Halter
2017-03-16 21:28:42 +01:00
parent fd50146f92
commit 8156a6b8a2
3 changed files with 25 additions and 10 deletions

View File

@@ -357,7 +357,7 @@ class DiffParser(object):
for name in names: for name in names:
if name.line in copied_line_numbers: if name.line in copied_line_numbers:
new_used_names.setdefault(key, []).append(name) new_used_names.setdefault(key, []).append(name)
self._module.used_names = new_used_names self._module._used_names = new_used_names
def _diff_tokenize(self, lines, until_line, line_offset=0): def _diff_tokenize(self, lines, until_line, line_offset=0):
is_first_token = True is_first_token = True

View File

@@ -43,8 +43,6 @@ class Parser(object):
tokens=None, start_parsing=True): tokens=None, start_parsing=True):
# Todo Remove start_parsing (with False) # Todo Remove start_parsing (with False)
self._used_names = {}
self.source = source self.source = source
self._added_newline = False self._added_newline = False
# The Python grammar needs a newline at the end of each statement. # The Python grammar needs a newline at the end of each statement.
@@ -124,11 +122,7 @@ class Parser(object):
if value in grammar.keywords: if value in grammar.keywords:
return tree.Keyword(value, start_pos, prefix) return tree.Keyword(value, start_pos, prefix)
else: else:
name = tree.Name(value, start_pos, prefix) return tree.Name(value, start_pos, prefix)
# Keep a listing of all used names
arr = self._used_names.setdefault(name.value, [])
arr.append(name)
return name
elif type == STRING: elif type == STRING:
return tree.String(value, start_pos, prefix) return tree.String(value, start_pos, prefix)
elif type == NUMBER: elif type == NUMBER:
@@ -203,7 +197,6 @@ class ParserWithRecovery(Parser):
def parse(self, tokenizer): def parse(self, tokenizer):
root_node = super(ParserWithRecovery, self).parse(self._tokenize(tokenizer)) root_node = super(ParserWithRecovery, self).parse(self._tokenize(tokenizer))
self.module = root_node self.module = root_node
self.module.used_names = self._used_names
self.module.path = self._module_path self.module.path = self._module_path
return root_node return root_node

View File

@@ -442,7 +442,7 @@ class Module(Scope):
Depending on the underlying parser this may be a full module or just a part Depending on the underlying parser this may be a full module or just a part
of a module. of a module.
""" """
__slots__ = ('path', 'used_names', '_name') __slots__ = ('path', '_used_names', '_name')
type = 'file_input' type = 'file_input'
def __init__(self, children): def __init__(self, children):
@@ -456,6 +456,7 @@ class Module(Scope):
""" """
super(Module, self).__init__(children) super(Module, self).__init__(children)
self.path = None # Set later. self.path = None # Set later.
self._used_names = None
@property @property
def name(self): def name(self):
@@ -497,6 +498,27 @@ class Module(Scope):
result += child.nodes_to_execute() result += child.nodes_to_execute()
return result return result
@property
def used_names(self):
if self._used_names is None:
# Don't directly use self._used_names to eliminate a lookup.
dct = {}
def recurse(node):
try:
children = node.children
except AttributeError:
if node.type == 'name':
arr = dct.setdefault(node.value, [])
arr.append(node)
else:
for child in children:
recurse(child)
recurse(self)
self._used_names = dct
return self._used_names
class Decorator(PythonBaseNode): class Decorator(PythonBaseNode):
type = 'decorator' type = 'decorator'