From 53fd1f925a3fd64c61e649718d38b9bed707d607 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Tue, 4 Mar 2014 15:35:24 +0100 Subject: [PATCH] create KeywordStatement to fit assert, del, global, etc into a more generalized schema, which can also improve the get_code method --- jedi/parser/__init__.py | 5 +++++ jedi/parser/representation.py | 25 ++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/jedi/parser/__init__.py b/jedi/parser/__init__.py index 1edb35df..b8531e79 100644 --- a/jedi/parser/__init__.py +++ b/jedi/parser/__init__.py @@ -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 in STATEMENT_KEYWORDS: + stmt, _ = self._parse_statement() + k = pr.KeywordStatement(tok_str, tok.start_pos, + use_as_parent_scope, stmt) + self._scope.add_statement(k) # decorator elif tok_str == '@': stmt, tok = self._parse_statement() diff --git a/jedi/parser/representation.py b/jedi/parser/representation.py index aa41dacd..7146dc9a 100644 --- a/jedi/parser/representation.py +++ b/jedi/parser/representation.py @@ -46,7 +46,7 @@ from jedi import cache from jedi.parser import tokenize -SCOPE_CONTENTS = ['asserts', 'subscopes', 'imports', 'statements', 'returns'] +SCOPE_CONTENTS = 'asserts', 'subscopes', 'imports', 'statements', 'returns' class GetCodeState(object): @@ -801,6 +801,29 @@ class Import(Simple): return n +class KeywordStatement(object): + """ + For the following statements: `assert`, `del`, `global`, `nonlocal`, + `raise`, `return`, `yield`, `pass`, `continue`, `break`, `return`, `yield`. + """ + __slots__ = ('name', 'start_pos', '_stmt', 'parent') + + def __init__(self, name, start_pos, parent, stmt=None): + self.name = name + self.start_pos = start_pos + self._stmt = stmt + self.parent = parent + + if stmt is not None: + stmt.parent = self + + def get_code(self): + if self._stmt is None: + return self.name + else: + return '%s %s' % (self.name, self._stmt) + + class Statement(Simple, DocstringMixin): """ This is the class for all the possible statements. Which means, this class