1
0
forked from VimPlug/jedi

reduce regex searches and compile pattern

This commit is contained in:
Akinori Hattori
2014-07-06 11:11:23 +09:00
parent 0610ef16ae
commit aab4891c4e

View File

@@ -183,6 +183,9 @@ class ParserNode(object):
class FastParser(use_metaclass(CachedFastParser)): class FastParser(use_metaclass(CachedFastParser)):
_keyword_re = re.compile('^[ \t]*(def|class|@|%s)' % '|'.join(tokenize.FLOWS))
def __init__(self, code, module_path=None): def __init__(self, code, module_path=None):
# set values like `pr.Module`. # set values like `pr.Module`.
self.module_path = module_path self.module_path = module_path
@@ -224,8 +227,6 @@ class FastParser(use_metaclass(CachedFastParser)):
parts.append(txt) parts.append(txt)
del current_lines[:] del current_lines[:]
r_keyword = '^[ \t]*(def|class|@|%s)' % '|'.join(tokenize.FLOWS)
# Split only new lines. Distinction between \r\n is the tokenizer's # Split only new lines. Distinction between \r\n is the tokenizer's
# job. # job.
self._lines = code.split('\n') self._lines = code.split('\n')
@@ -240,9 +241,9 @@ class FastParser(use_metaclass(CachedFastParser)):
# All things within flows are simply being ignored. # All things within flows are simply being ignored.
for i, l in enumerate(self._lines): for i, l in enumerate(self._lines):
# check for dedents # check for dedents
m = re.match('^([\t ]*)(.?)', l) s = l.lstrip('\t ')
indent = len(m.group(1)) indent = len(l) - len(s)
if m.group(2) in ['', '#']: if not s or s[0] == '#':
current_lines.append(l) # just ignore comments and blank lines current_lines.append(l) # just ignore comments and blank lines
continue continue
@@ -259,7 +260,7 @@ class FastParser(use_metaclass(CachedFastParser)):
# Check lines for functions/classes and split the code there. # Check lines for functions/classes and split the code there.
if not in_flow: if not in_flow:
m = re.match(r_keyword, l) m = self._keyword_re.match(l)
if m: if m:
in_flow = m.group(1) in tokenize.FLOWS in_flow = m.group(1) in tokenize.FLOWS
if not is_decorator and not in_flow: if not is_decorator and not in_flow:
@@ -296,7 +297,7 @@ class FastParser(use_metaclass(CachedFastParser)):
for code_part in parts: for code_part in parts:
lines = code_part.count('\n') + 1 lines = code_part.count('\n') + 1
if is_first or line_offset >= p.module.end_pos[0]: if is_first or line_offset >= p.module.end_pos[0]:
indent = len(re.match(r'[ \t]*', code_part).group(0)) indent = len(code_part) - len(code_part.lstrip('\t '))
if is_first and self.current_node is not None: if is_first and self.current_node is not None:
nodes = [self.current_node] nodes = [self.current_node]
else: else: