Function is starting to work.

This commit is contained in:
Dave Halter
2014-10-08 17:58:02 +02:00
parent 585e92ac9f
commit 6bb88ddd85
3 changed files with 33 additions and 26 deletions

View File

@@ -46,9 +46,9 @@ del python_grammar_no_print_statement.keywords["print"]
from jedi.parser.representation import ExprStmt, Class, Function from jedi.parser.representation import ExprStmt, Class, Function
_ast_mapping = { _ast_mapping = {
'simple_stmt': ExprStmt, #'simple_stmt': ExprStmt,
'classdef': Class, 'classdef': Class,
'funcdef': Function 'funcdef': Function,
} }
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())
@@ -79,7 +79,6 @@ class Base(object):
# Default values for instance variables # Default values for instance variables
type = None # int: token number (< 256) or symbol number (>= 256) type = None # int: token number (< 256) or symbol number (>= 256)
parent = None # Parent node pointer, or None parent = None # Parent node pointer, or None
children = () # Tuple of subnodes
def leaves(self): def leaves(self):
for child in self.children: for child in self.children:
@@ -134,6 +133,14 @@ class Node(Base):
return "" return ""
return self.children[0].prefix 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 @prefix.setter
def prefix(self, prefix): def prefix(self, prefix):
if self.children: if self.children:
@@ -172,6 +179,10 @@ class Leaf(Base):
self.type = type self.type = type
self.value = value self.value = value
@property
def end_pos(self):
return self.start_pos[0], self.start_pos[1] + len(self.value)
def __repr__(self): def __repr__(self):
"""Return a canonical string representation.""" """Return a canonical string representation."""
return "%s(%r, %r)" % (self.__class__.__name__, return "%s(%r, %r)" % (self.__class__.__name__,

View File

@@ -173,7 +173,7 @@ class Simple(Base):
The super class for Scope, Import, Name and Statement. Every object in The super class for Scope, Import, Name and Statement. Every object in
the parser tree inherits from this class. the parser tree inherits from this class.
""" """
__slots__ = ('children',) __slots__ = ('children', 'parent')
def __init__(self, children): def __init__(self, children):
""" """
@@ -183,6 +183,7 @@ class Simple(Base):
:param children: The module in which this Python object locates. :param children: The module in which this Python object locates.
""" """
self.children = children self.children = children
self.parent = None
@property @property
def start_pos(self): def start_pos(self):
@@ -238,8 +239,8 @@ class Scope(Simple, DocstringMixin):
__slots__ = ('subscopes', 'imports', 'statements', '_doc_token', 'asserts', __slots__ = ('subscopes', 'imports', 'statements', '_doc_token', 'asserts',
'returns', 'is_generator', '_names_dict') 'returns', 'is_generator', '_names_dict')
def __init__(self, module, start_pos): def __init__(self, children):
super(Scope, self).__init__(module, start_pos) super(Scope, self).__init__(children)
self.subscopes = [] self.subscopes = []
self.imports = [] self.imports = []
self.statements = [] self.statements = []
@@ -549,30 +550,25 @@ class Function(Scope):
:param start_pos: The start position (line, column) the Function. :param start_pos: The start position (line, column) the Function.
:type start_pos: tuple(int, int) :type start_pos: tuple(int, int)
""" """
__slots__ = ('name', 'params', 'decorators', 'listeners', 'annotation') __slots__ = ('decorators', 'listeners')
def __init__(self, module, name, params, start_pos, annotation): def __init__(self, children):
super(Function, self).__init__(module, start_pos) super(Function, self).__init__(children)
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
self.decorators = [] self.decorators = []
self.listeners = set() # not used here, but in evaluation. self.listeners = set() # not used here, but in evaluation.
if annotation is not None: @property
annotation.parent = self.use_as_parent def name(self):
self.annotation = annotation return self.children[1] # First token after `def`
def get_code(self, first_indent=False, indention=' '): def params(self):
string = "\n".join('@' + stmt.get_code() for stmt in self.decorators) return self.children[3].children # After def foo(
params = ', '.join([stmt.get_code(False) for stmt in self.params])
string += "def %s(%s):\n" % (self.name, params) def annotation(self):
string += super(Function, self).get_code(True, indention) try:
return string return self.children[6] # 6th element: def foo(...) -> bar
except IndexError:
return None
def get_defined_names(self): def get_defined_names(self):
n = super(Function, self).get_defined_names() n = super(Function, self).get_defined_names()

View File

@@ -19,7 +19,7 @@ def test_basic():
print(repr(tree)) print(repr(tree))
print(tree) print(tree)
#import pdb; pdb.set_trace() #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() print()
x = d.parse_string('\na #pass\n') x = d.parse_string('\na #pass\n')
print(repr(x)) print(repr(x))