1
0
forked from VimPlug/jedi

Move Node away from pytree into the parser representation.

This commit is contained in:
Dave Halter
2014-10-13 13:47:32 +02:00
parent 6458047bac
commit 485b8ae3da
2 changed files with 24 additions and 72 deletions

View File

@@ -56,76 +56,6 @@ def type_repr(type_num):
return _type_reprs.setdefault(type_num, type_num) return _type_reprs.setdefault(type_num, type_num)
class Node(object):
"""Concrete implementation for interior nodes."""
def __init__(self, type, children):
"""
Initializer.
Takes a type constant (a symbol number >= 256), a sequence of
child nodes, and an optional context keyword argument.
As a side effect, the parent pointers of the children are updated.
"""
self.type = type
self.children = children
for ch in self.children:
ch.parent = self
def __repr__(self):
"""Return a canonical string representation."""
return "%s(%s, %r)" % (self.__class__.__name__,
type_repr(self.type),
self.children)
def __unicode__(self):
"""
Return a pretty string representation.
This reproduces the input source exactly.
"""
return "".join(map(str, self.children))
if sys.version_info > (3, 0):
__str__ = __unicode__
def get_code(self):
return "".join(c.get_code() for c in self.children)
@property
def prefix(self):
"""
The whitespace and comments preceding this node in the input.
"""
if not self.children:
return ""
return self.children[0].prefix
@property
def start_pos(self):
return self.children[0].start_pos
@property
def end_pos(self):
return self.children[-1].end_pos
@prefix.setter
def prefix(self, prefix):
if self.children:
self.children[0].prefix = prefix
else:
raise NotImplementedError
def append_child(self, child):
"""
Equivalent to 'node.children.append(child)'. This method also sets the
child's parent attribute appropriately.
"""
child.parent = self
self.children.append(child)
def convert(grammar, raw_node): def convert(grammar, raw_node):
""" """
Convert raw node information to a Node or Leaf instance. Convert raw node information to a Node or Leaf instance.
@@ -160,7 +90,7 @@ def convert(grammar, raw_node):
try: try:
return ast_mapping[type](children) return ast_mapping[type](children)
except KeyError: except KeyError:
return Node(type, children) return pr.Node(type, children)
else: else:
print('leaf', raw_node, type_repr(type)) print('leaf', raw_node, type_repr(type))
prefix, start_pos = context prefix, start_pos = context

View File

@@ -45,7 +45,7 @@ from jedi import common
from jedi import debug from jedi import debug
from jedi import cache from jedi import cache
from jedi.parser import tokenize from jedi.parser import tokenize
from jedi.parser.pytree import python_symbols, Node from jedi.parser.pytree import python_symbols, type_repr
SCOPE_CONTENTS = 'asserts', 'subscopes', 'imports', 'statements', 'returns' SCOPE_CONTENTS = 'asserts', 'subscopes', 'imports', 'statements', 'returns'
@@ -326,6 +326,28 @@ class Simple(Base):
(type(self).__name__, code, self.start_pos[0], self.start_pos[1]) (type(self).__name__, code, self.start_pos[0], self.start_pos[1])
class Node(Simple):
"""Concrete implementation for interior nodes."""
def __init__(self, type, children):
"""
Initializer.
Takes a type constant (a symbol number >= 256), a sequence of
child nodes, and an optional context keyword argument.
As a side effect, the parent pointers of the children are updated.
"""
super(Node, self).__init__(children)
self.type = type
def __repr__(self):
"""Return a canonical string representation."""
return "%s(%s, %r)" % (self.__class__.__name__,
type_repr(self.type),
self.children)
class IsScopeMeta(type): class IsScopeMeta(type):
def __instancecheck__(self, other): def __instancecheck__(self, other):
return other.is_scope() return other.is_scope()