Moved the convert function away from pytree

This commit is contained in:
Dave Halter
2014-11-04 19:09:58 +01:00
parent f3e4bf9ed1
commit 3518123afa
3 changed files with 48 additions and 63 deletions
-61
View File
@@ -57,64 +57,3 @@ def type_repr(type_num):
return _type_reprs.setdefault(type_num, type_num)
def gen_ast_mapping():
from jedi.parser import representation as pr
_ast_mapping = {
'expr_stmt': pr.ExprStmt,
'classdef': pr.Class,
'funcdef': pr.Function,
'file_input': pr.SubModule,
'import_name': pr.Import,
'import_from': pr.Import,
'break_stmt': pr.KeywordStatement,
'continue_stmt': pr.KeywordStatement,
'return_stmt': pr.ReturnStmt,
'raise_stmt': pr.KeywordStatement,
'yield_expr': pr.YieldExpr,
'del_stmt': pr.KeywordStatement,
'pass_stmt': pr.KeywordStatement,
'global_stmt': pr.GlobalStmt,
'nonlocal_stmt': pr.KeywordStatement,
'assert_stmt': pr.KeywordStatement,
'if_stmt': pr.IfStmt,
'with_stmt': pr.WithStmt,
'for_stmt': pr.ForStmt,
'while_stmt': pr.WhileStmt,
'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:
# If there's exactly one child, return that child instead of
# creating a new node.
# We still create expr_stmt and file_input though, because a lot of
# Jedi depends on its logic.
if len(children) == 1 and type not in (python_symbols.expr_stmt,
python_symbols.file_input):
return children[0]
#print(raw_node, type_repr(type))
#import pdb; pdb.set_trace()
try:
return ast_mapping[type](children)
except KeyError:
return pr.Node(type, children)
else:
raise NotImplementedError