1
0
forked from VimPlug/jedi

Small optimizations to make parsing faster.

This commit is contained in:
Dave Halter
2014-11-04 17:23:16 +01:00
parent c6c2768dda
commit d483d50284
2 changed files with 27 additions and 16 deletions

View File

@@ -178,8 +178,13 @@ class Parser(object):
def pop(self):
"""Pop a nonterminal. (Internal)"""
popdfa, popstate, popnode = self.stack.pop()
children = popnode[3]
if len(children) != 1 or popnode[0] in (self.grammar.symbol2number['expr_stmt'],
self.grammar.symbol2number['file_input']):
newnode = self.convert(self.grammar, popnode)
if newnode is not None:
else:
newnode = children[0]
if self.stack:
dfa, state, node = self.stack[-1]
node[-1].append(newnode)

View File

@@ -57,15 +57,7 @@ def type_repr(type_num):
return _type_reprs.setdefault(type_num, type_num)
def convert(grammar, raw_node):
"""
Convert raw node information to a Node or Leaf instance.
This is passed to the parser driver which calls it whenever a reduction of a
grammar rule produces a new complete node, so that the tree is build
strictly bottom-up.
"""
def gen_ast_mapping():
from jedi.parser import representation as pr
_ast_mapping = {
'expr_stmt': pr.ExprStmt,
@@ -91,9 +83,23 @@ def convert(grammar, raw_node):
'try_stmt': pr.TryStmt,
}
global ast_mapping, pr
ast_mapping = dict((getattr(python_symbols, k), v) for k, v in _ast_mapping.items())
def convert(grammar, raw_node):
"""
Convert raw node information to a Node or Leaf instance.
This is passed to the parser driver which calls it whenever a reduction of a
grammar rule produces a new complete node, so that the tree is build
strictly bottom-up.
"""
try:
ast_mapping
except NameError:
gen_ast_mapping()
#import pdb; pdb.set_trace()
type, value, context, children = raw_node
if type in grammar.number2symbol: