1
0
forked from VimPlug/jedi

KeywordStatements are working except for some of the old ones (global, assert, return, yield)

This commit is contained in:
Dave Halter
2014-03-04 17:20:29 +01:00
parent 53fd1f925a
commit f54344fd9e
2 changed files with 35 additions and 21 deletions

View File

@@ -573,6 +573,11 @@ class Parser(object):
# add the global to the top, because there it is
# important.
self.module.add_global(t)
elif tok_str == 'assert':
stmt, tok = self._parse_statement()
if stmt is not None:
stmt.parent = use_as_parent_scope
self._scope.asserts.append(stmt)
elif tok_str in STATEMENT_KEYWORDS:
stmt, _ = self._parse_statement()
k = pr.KeywordStatement(tok_str, tok.start_pos,
@@ -585,11 +590,6 @@ class Parser(object):
self._decorators.append(stmt)
elif tok_str == 'pass':
continue
elif tok_str == 'assert':
stmt, tok = self._parse_statement()
if stmt is not None:
stmt.parent = use_as_parent_scope
self._scope.asserts.append(stmt)
# default
elif token_type in [tokenize.NAME, tokenize.STRING,
tokenize.NUMBER] \

View File

@@ -101,6 +101,23 @@ class Base(object):
#TODO: we need tab detection
return " "
@Python3Method
def get_parent_until(self, classes=(), reverse=False,
include_current=True):
"""
Searches the parent "chain" until the object is an instance of
classes. If classes is empty return the last parent in the chain
(is without a parent).
"""
if type(classes) not in (tuple, list):
classes = (classes,)
scope = self if include_current else self.parent
while scope.parent is not None:
if classes and reverse != scope.isinstance(*classes):
break
scope = scope.parent
return scope
def space(self, from_pos, to_pos):
"""Return the space between two tokens"""
linecount = to_pos[0] - from_pos[0]
@@ -160,19 +177,6 @@ class Simple(Base):
def end_pos(self, value):
self._end_pos = value
@Python3Method
def get_parent_until(self, classes=(), reverse=False,
include_current=True):
""" Takes always the parent, until one class (not a Class) """
if type(classes) not in (tuple, list):
classes = (classes,)
scope = self if include_current else self.parent
while scope.parent is not None:
if classes and reverse != scope.isinstance(*classes):
break
scope = scope.parent
return scope
def __repr__(self):
code = self.get_code().replace('\n', ' ')
if not is_py3:
@@ -801,7 +805,7 @@ class Import(Simple):
return n
class KeywordStatement(object):
class KeywordStatement(Base):
"""
For the following statements: `assert`, `del`, `global`, `nonlocal`,
`raise`, `return`, `yield`, `pass`, `continue`, `break`, `return`, `yield`.
@@ -819,9 +823,19 @@ class KeywordStatement(object):
def get_code(self):
if self._stmt is None:
return self.name
return "%s\n" % self.name
else:
return '%s %s' % (self.name, self._stmt)
return '%s %s\n' % (self.name, self._stmt)
def get_set_vars(self):
return []
@property
def end_pos(self):
try:
return self._stmt.end_pos
except AttributeError:
return self.start_pos[0], self.start_pos[1] + len(self.name)
class Statement(Simple, DocstringMixin):