diff --git a/jedi/parser/pytree.py b/jedi/parser/pytree.py index 2d91fc2d..3c968bfe 100644 --- a/jedi/parser/pytree.py +++ b/jedi/parser/pytree.py @@ -46,9 +46,9 @@ del python_grammar_no_print_statement.keywords["print"] from jedi.parser.representation import ExprStmt, Class, Function _ast_mapping = { - 'simple_stmt': ExprStmt, + #'simple_stmt': ExprStmt, 'classdef': Class, - 'funcdef': Function + 'funcdef': Function, } ast_mapping = dict((getattr(python_symbols, k), v) for k, v in _ast_mapping.items()) @@ -79,7 +79,6 @@ class Base(object): # Default values for instance variables type = None # int: token number (< 256) or symbol number (>= 256) parent = None # Parent node pointer, or None - children = () # Tuple of subnodes def leaves(self): for child in self.children: @@ -134,6 +133,14 @@ class Node(Base): 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: @@ -172,6 +179,10 @@ class Leaf(Base): self.type = type self.value = value + @property + def end_pos(self): + return self.start_pos[0], self.start_pos[1] + len(self.value) + def __repr__(self): """Return a canonical string representation.""" return "%s(%r, %r)" % (self.__class__.__name__, diff --git a/jedi/parser/representation.py b/jedi/parser/representation.py index e9575fef..1ff9d7d9 100644 --- a/jedi/parser/representation.py +++ b/jedi/parser/representation.py @@ -173,7 +173,7 @@ class Simple(Base): The super class for Scope, Import, Name and Statement. Every object in the parser tree inherits from this class. """ - __slots__ = ('children',) + __slots__ = ('children', 'parent') def __init__(self, children): """ @@ -183,6 +183,7 @@ class Simple(Base): :param children: The module in which this Python object locates. """ self.children = children + self.parent = None @property def start_pos(self): @@ -238,8 +239,8 @@ class Scope(Simple, DocstringMixin): __slots__ = ('subscopes', 'imports', 'statements', '_doc_token', 'asserts', 'returns', 'is_generator', '_names_dict') - def __init__(self, module, start_pos): - super(Scope, self).__init__(module, start_pos) + def __init__(self, children): + super(Scope, self).__init__(children) self.subscopes = [] self.imports = [] self.statements = [] @@ -549,30 +550,25 @@ class Function(Scope): :param start_pos: The start position (line, column) the Function. :type start_pos: tuple(int, int) """ - __slots__ = ('name', 'params', 'decorators', 'listeners', 'annotation') + __slots__ = ('decorators', 'listeners') - def __init__(self, module, name, params, start_pos, annotation): - super(Function, self).__init__(module, start_pos) - self.name = name - if name is not None: - name.parent = self.use_as_parent - self.params = params - for p in params: - p.parent = self.use_as_parent - p.parent_function = self.use_as_parent + def __init__(self, children): + super(Function, self).__init__(children) self.decorators = [] self.listeners = set() # not used here, but in evaluation. - if annotation is not None: - annotation.parent = self.use_as_parent - self.annotation = annotation + @property + def name(self): + return self.children[1] # First token after `def` - def get_code(self, first_indent=False, indention=' '): - string = "\n".join('@' + stmt.get_code() for stmt in self.decorators) - params = ', '.join([stmt.get_code(False) for stmt in self.params]) - string += "def %s(%s):\n" % (self.name, params) - string += super(Function, self).get_code(True, indention) - return string + def params(self): + return self.children[3].children # After def foo( + + def annotation(self): + try: + return self.children[6] # 6th element: def foo(...) -> bar + except IndexError: + return None def get_defined_names(self): n = super(Function, self).get_defined_names() diff --git a/test/test_new_parser.py b/test/test_new_parser.py index 8910c0be..8d289d9f 100644 --- a/test/test_new_parser.py +++ b/test/test_new_parser.py @@ -19,7 +19,7 @@ def test_basic(): print(repr(tree)) print(tree) #import pdb; pdb.set_trace() - print(repr(d.parse_string('def x(): pass\n'))) + print(repr(d.parse_string('def x(a, b:3): pass\n'))) print() x = d.parse_string('\na #pass\n') print(repr(x))