1
0
forked from VimPlug/jedi

Fast parser simplifications and bug fixes.

This commit is contained in:
Dave Halter
2015-01-21 18:34:22 +01:00
parent 91ab1d0ecd
commit 8569651bf4
+24 -19
View File
@@ -8,7 +8,6 @@ from itertools import chain
from jedi._compatibility import use_metaclass, unicode from jedi._compatibility import use_metaclass, unicode
from jedi import settings from jedi import settings
from jedi import common
from jedi.parser import Parser from jedi.parser import Parser
from jedi.parser import tree as pr from jedi.parser import tree as pr
from jedi.parser import tokenize from jedi.parser import tokenize
@@ -24,6 +23,7 @@ class FastModule(pr.Module, pr.Simple):
super(FastModule, self).__init__([]) super(FastModule, self).__init__([])
self.modules = [] self.modules = []
self.reset_caches() self.reset_caches()
self.names_dict = {}
def reset_caches(self): def reset_caches(self):
self.modules = [] self.modules = []
@@ -64,7 +64,7 @@ class MergedNamesDict(object):
def values(self): def values(self):
lst = [] lst = []
for dct in self.dicts: for dct in self.dicts:
lst.append(dct.values()) lst += dct.values()
return lst return lst
@@ -113,6 +113,9 @@ class ParserNode(object):
self._content_scope = self._fast_module self._content_scope = self._fast_module
self._names_dict_scope = parser.module self._names_dict_scope = parser.module
# We need to be able to reset the original children of a parser.
self._old_children = list(self._content_scope.children)
""" """
scope = self._content_scope scope = self._content_scope
self._contents = {} self._contents = {}
@@ -128,6 +131,7 @@ class ParserNode(object):
Removes changes that were applied in this class. Removes changes that were applied in this class.
""" """
nd_scope = self._names_dict_scope nd_scope = self._names_dict_scope
self._content_scope.children = list(self._old_children)
try: try:
# This works if it's a MergedNamesDict. # This works if it's a MergedNamesDict.
# We are correcting it, because the MergedNamesDicts are artificial # We are correcting it, because the MergedNamesDicts are artificial
@@ -223,18 +227,17 @@ class ParserNode(object):
self.node_children.append(node) self.node_children.append(node)
# Insert parser objects into current structure. We only need to set the # Insert parser objects into current structure. We only need to set the
# parents in a good way. # parents and children in a good way.
if True or node.parent != self: # TODO remove true scope = self._content_scope
scope = self._content_scope for child in m.children:
for child in m.children: child.parent = scope
child.parent = scope scope.children.append(child)
scope.children.append(child) #print('\t\t', scope, child)
#print('\t\t', scope, child) """
""" if isinstance(i, (pr.Function, pr.Class)):
if isinstance(i, (pr.Function, pr.Class)): for d in i.decorators:
for d in i.decorators: d.parent = scope
d.parent = scope """
"""
""" """
scope = self.content_scope scope = self.content_scope
@@ -253,13 +256,13 @@ class ParserNode(object):
print('add parser') print('add parser')
return self.add_node(ParserNode(self._fast_module, parser, code, self), True) return self.add_node(ParserNode(self._fast_module, parser, code, self), True)
def all_nodes(self): def all_sub_nodes(self):
""" """
Returns all nodes including nested ones. Returns all nodes including nested ones.
""" """
yield self
for n in self.node_children: for n in self.node_children:
for y in n.all_nodes(): yield n
for y in n.all_sub_nodes():
yield y yield y
@@ -277,9 +280,11 @@ class FastParser(use_metaclass(CachedFastParser)):
def _reset_caches(self): def _reset_caches(self):
self.module = FastModule() self.module = FastModule()
self.current_node = ParserNode(self.module) self.current_node = ParserNode(self.module)
self.current_node.set_parser(self, '')
def update(self, code): def update(self, code):
self.module.reset_caches() self.module.reset_caches()
self.current_node.reset_node()
try: try:
self._parse(code) self._parse(code)
except: except:
@@ -355,7 +360,7 @@ class FastParser(use_metaclass(CachedFastParser)):
line_offset = 0 line_offset = 0
start = 0 start = 0
is_first = True is_first = True
nodes = list(self.current_node.all_nodes()) nodes = list(self.current_node.all_sub_nodes())
for code_part in self._split_parts(code): for code_part in self._split_parts(code):
if is_first or line_offset + 1 == self.current_node.parser.module.end_pos[0]: if is_first or line_offset + 1 == self.current_node.parser.module.end_pos[0]:
@@ -370,7 +375,7 @@ class FastParser(use_metaclass(CachedFastParser)):
line_offset, nodes, not is_first) line_offset, nodes, not is_first)
print('HmmmmA', self.current_node.parser.module.names_dict) print('HmmmmA', self.current_node.parser.module.names_dict)
if is_first and self.current_node.parser.module.subscopes: if False and is_first and self.current_node.parser.module.subscopes:
print('NOXXXX') print('NOXXXX')
raise NotImplementedError raise NotImplementedError
# Special case, we cannot use a function subscope as a # Special case, we cannot use a function subscope as a