From 485b8ae3dac1d855eef7b8e44fa0bc99dc502ae4 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Mon, 13 Oct 2014 13:47:32 +0200 Subject: [PATCH] Move Node away from pytree into the parser representation. --- jedi/parser/pytree.py | 72 +---------------------------------- jedi/parser/representation.py | 24 +++++++++++- 2 files changed, 24 insertions(+), 72 deletions(-) diff --git a/jedi/parser/pytree.py b/jedi/parser/pytree.py index e5b09367..edc7e05a 100644 --- a/jedi/parser/pytree.py +++ b/jedi/parser/pytree.py @@ -56,76 +56,6 @@ def type_repr(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): """ Convert raw node information to a Node or Leaf instance. @@ -160,7 +90,7 @@ def convert(grammar, raw_node): try: return ast_mapping[type](children) except KeyError: - return Node(type, children) + return pr.Node(type, children) else: print('leaf', raw_node, type_repr(type)) prefix, start_pos = context diff --git a/jedi/parser/representation.py b/jedi/parser/representation.py index 0f81239b..6c25d744 100644 --- a/jedi/parser/representation.py +++ b/jedi/parser/representation.py @@ -45,7 +45,7 @@ from jedi import common from jedi import debug from jedi import cache 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' @@ -326,6 +326,28 @@ class Simple(Base): (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): def __instancecheck__(self, other): return other.is_scope()