forked from VimPlug/jedi
Operator refactoring.
This commit is contained in:
@@ -44,11 +44,12 @@ python_grammar_no_print_statement = python_grammar.copy()
|
|||||||
del python_grammar_no_print_statement.keywords["print"]
|
del python_grammar_no_print_statement.keywords["print"]
|
||||||
|
|
||||||
|
|
||||||
from jedi.parser.representation import ExprStmt, Class, Function
|
from jedi.parser import representation as er
|
||||||
_ast_mapping = {
|
_ast_mapping = {
|
||||||
#'simple_stmt': ExprStmt,
|
#'simple_stmt': ExprStmt,
|
||||||
'classdef': Class,
|
'classdef': er.Class,
|
||||||
'funcdef': Function,
|
'funcdef': er.Function,
|
||||||
|
'file_input': er.SubModule,
|
||||||
}
|
}
|
||||||
|
|
||||||
ast_mapping = dict((getattr(python_symbols, k), v) for k, v in _ast_mapping.items())
|
ast_mapping = dict((getattr(python_symbols, k), v) for k, v in _ast_mapping.items())
|
||||||
@@ -220,6 +221,7 @@ def convert(grammar, raw_node):
|
|||||||
if len(children) == 1:
|
if len(children) == 1:
|
||||||
return children[0]
|
return children[0]
|
||||||
print(raw_node, type_repr(type))
|
print(raw_node, type_repr(type))
|
||||||
|
#import pdb; pdb.set_trace()
|
||||||
try:
|
try:
|
||||||
return ast_mapping[type](children)
|
return ast_mapping[type](children)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
|||||||
@@ -421,7 +421,7 @@ class SubModule(Scope, Module):
|
|||||||
__slots__ = ('path', 'global_vars', 'used_names', 'temp_used_names',
|
__slots__ = ('path', 'global_vars', 'used_names', 'temp_used_names',
|
||||||
'line_offset', 'use_as_parent')
|
'line_offset', 'use_as_parent')
|
||||||
|
|
||||||
def __init__(self, path, start_pos=(1, 0), top_module=None):
|
def __init__(self, children):
|
||||||
"""
|
"""
|
||||||
Initialize :class:`SubModule`.
|
Initialize :class:`SubModule`.
|
||||||
|
|
||||||
@@ -430,14 +430,15 @@ class SubModule(Scope, Module):
|
|||||||
|
|
||||||
.. todo:: Document `top_module`.
|
.. todo:: Document `top_module`.
|
||||||
"""
|
"""
|
||||||
super(SubModule, self).__init__(self, start_pos)
|
super(SubModule, self).__init__(children)
|
||||||
self.path = path
|
self.path = None # Set later.
|
||||||
self.global_vars = []
|
self.global_vars = []
|
||||||
self.used_names = {}
|
self.used_names = {}
|
||||||
self.temp_used_names = []
|
self.temp_used_names = []
|
||||||
# this may be changed depending on fast_parser
|
# this may be changed depending on fast_parser
|
||||||
self.line_offset = 0
|
self.line_offset = 0
|
||||||
|
|
||||||
|
if 0:
|
||||||
self.use_as_parent = top_module or self
|
self.use_as_parent = top_module or self
|
||||||
|
|
||||||
def add_global(self, name):
|
def add_global(self, name):
|
||||||
@@ -1582,20 +1583,21 @@ class ListComprehension(ForFlow):
|
|||||||
return "%s for %s in %s" % tuple(code)
|
return "%s for %s in %s" % tuple(code)
|
||||||
|
|
||||||
|
|
||||||
class Operator(Simple):
|
class _Leaf(Base):
|
||||||
__slots__ = ('string',)
|
__slots__ = ('value', 'parent', 'start_pos', 'prefix')
|
||||||
|
def __init__(self, value, start_pos, prefix):
|
||||||
def __init__(self, module, string, parent, start_pos):
|
self.value = value
|
||||||
end_pos = start_pos[0], start_pos[1] + len(string)
|
self.start_pos = start_pos
|
||||||
super(Operator, self).__init__(module, start_pos, end_pos, parent)
|
self.prefix = prefix
|
||||||
self.string = string
|
|
||||||
|
|
||||||
def get_code(self):
|
def get_code(self):
|
||||||
return self.string
|
return self.prefix + self.value
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<%s: `%s`>" % (type(self).__name__, self.string)
|
return "<%s: `%s`>" % (type(self).__name__, self.value)
|
||||||
|
|
||||||
|
|
||||||
|
class Operator(_Leaf):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.get_code()
|
return self.get_code()
|
||||||
|
|
||||||
@@ -1607,11 +1609,11 @@ class Operator(Simple):
|
|||||||
if isinstance(other, Operator):
|
if isinstance(other, Operator):
|
||||||
return self is other
|
return self is other
|
||||||
else:
|
else:
|
||||||
return self.string == other
|
return self.value == other
|
||||||
|
|
||||||
def __ne__(self, other):
|
def __ne__(self, other):
|
||||||
"""Python 2 compatibility."""
|
"""Python 2 compatibility."""
|
||||||
return self.string != other
|
return self.value != other
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return hash(self.string)
|
return hash(self.value)
|
||||||
|
|||||||
Reference in New Issue
Block a user