forked from VimPlug/jedi
parsing.Parser.scope -> parsing.Parser._scope, fixes #224
This commit is contained in:
@@ -56,7 +56,7 @@ class Parser(object):
|
|||||||
self.start_pos = self.end_pos = 1 + offset[0], offset[1]
|
self.start_pos = self.end_pos = 1 + offset[0], offset[1]
|
||||||
# initialize global Scope
|
# initialize global Scope
|
||||||
self.module = pr.SubModule(module_path, self.start_pos, top_module)
|
self.module = pr.SubModule(module_path, self.start_pos, top_module)
|
||||||
self.scope = self.module
|
self._scope = self.module
|
||||||
self.current = (None, None)
|
self.current = (None, None)
|
||||||
|
|
||||||
source = source + '\n' # end with \n, because the parser needs it
|
source = source + '\n' # end with \n, because the parser needs it
|
||||||
@@ -390,7 +390,7 @@ class Parser(object):
|
|||||||
#print 'new_stat', set_vars, used_vars
|
#print 'new_stat', set_vars, used_vars
|
||||||
if self.freshscope and not self.no_docstr and len(tok_list) == 1 \
|
if self.freshscope and not self.no_docstr and len(tok_list) == 1 \
|
||||||
and self.last_token[0] == tokenize.STRING:
|
and self.last_token[0] == tokenize.STRING:
|
||||||
self.scope.add_docstr(self.last_token[1])
|
self._scope.add_docstr(self.last_token[1])
|
||||||
return None, tok
|
return None, tok
|
||||||
else:
|
else:
|
||||||
stmt = stmt_class(self.module, set_vars, used_vars, tok_list,
|
stmt = stmt_class(self.module, set_vars, used_vars, tok_list,
|
||||||
@@ -408,7 +408,7 @@ class Parser(object):
|
|||||||
and len(stmt.token_list) == 1
|
and len(stmt.token_list) == 1
|
||||||
and first_tok[0] == tokenize.STRING):
|
and first_tok[0] == tokenize.STRING):
|
||||||
# ... then set it as a docstring
|
# ... then set it as a docstring
|
||||||
self.scope.statements[-1].add_docstr(first_tok[1])
|
self._scope.statements[-1].add_docstr(first_tok[1])
|
||||||
|
|
||||||
if tok in always_break + not_first_break:
|
if tok in always_break + not_first_break:
|
||||||
self._gen.push_last_back()
|
self._gen.push_last_back()
|
||||||
@@ -429,7 +429,7 @@ class Parser(object):
|
|||||||
self.start_pos, self.end_pos = start_pos, end_pos
|
self.start_pos, self.end_pos = start_pos, end_pos
|
||||||
except (StopIteration, common.MultiLevelStopIteration):
|
except (StopIteration, common.MultiLevelStopIteration):
|
||||||
# on finish, set end_pos correctly
|
# on finish, set end_pos correctly
|
||||||
s = self.scope
|
s = self._scope
|
||||||
while s is not None:
|
while s is not None:
|
||||||
if isinstance(s, pr.Module) \
|
if isinstance(s, pr.Module) \
|
||||||
and not isinstance(s, pr.SubModule):
|
and not isinstance(s, pr.SubModule):
|
||||||
@@ -443,8 +443,8 @@ class Parser(object):
|
|||||||
or self.user_scope is None
|
or self.user_scope is None
|
||||||
and self.start_pos[0] >= self.user_position[0]):
|
and self.start_pos[0] >= self.user_position[0]):
|
||||||
debug.dbg('user scope found [%s] = %s' %
|
debug.dbg('user scope found [%s] = %s' %
|
||||||
(self.parserline.replace('\n', ''), repr(self.scope)))
|
(self.parserline.replace('\n', ''), repr(self._scope)))
|
||||||
self.user_scope = self.scope
|
self.user_scope = self._scope
|
||||||
self.last_token = self.current
|
self.last_token = self.current
|
||||||
self.current = (typ, tok)
|
self.current = (typ, tok)
|
||||||
return self.current
|
return self.current
|
||||||
@@ -472,29 +472,29 @@ class Parser(object):
|
|||||||
#debug.dbg('main: tok=[%s] type=[%s] indent=[%s]'\
|
#debug.dbg('main: tok=[%s] type=[%s] indent=[%s]'\
|
||||||
# % (tok, tokenize.tok_name[token_type], start_position[0]))
|
# % (tok, tokenize.tok_name[token_type], start_position[0]))
|
||||||
|
|
||||||
while token_type == tokenize.DEDENT and self.scope != self.module:
|
while token_type == tokenize.DEDENT and self._scope != self.module:
|
||||||
token_type, tok = self.next()
|
token_type, tok = self.next()
|
||||||
if self.start_pos[1] <= self.scope.start_pos[1]:
|
if self.start_pos[1] <= self._scope.start_pos[1]:
|
||||||
self.scope.end_pos = self.start_pos
|
self._scope.end_pos = self.start_pos
|
||||||
self.scope = self.scope.parent
|
self._scope = self._scope.parent
|
||||||
if isinstance(self.scope, pr.Module) \
|
if isinstance(self._scope, pr.Module) \
|
||||||
and not isinstance(self.scope, pr.SubModule):
|
and not isinstance(self._scope, pr.SubModule):
|
||||||
self.scope = self.module
|
self._scope = self.module
|
||||||
|
|
||||||
# check again for unindented stuff. this is true for syntax
|
# check again for unindented stuff. this is true for syntax
|
||||||
# errors. only check for names, because thats relevant here. If
|
# errors. only check for names, because thats relevant here. If
|
||||||
# some docstrings are not indented, I don't care.
|
# some docstrings are not indented, I don't care.
|
||||||
while self.start_pos[1] <= self.scope.start_pos[1] \
|
while self.start_pos[1] <= self._scope.start_pos[1] \
|
||||||
and (token_type == tokenize.NAME or tok in ['(', '['])\
|
and (token_type == tokenize.NAME or tok in ['(', '['])\
|
||||||
and self.scope != self.module:
|
and self._scope != self.module:
|
||||||
self.scope.end_pos = self.start_pos
|
self._scope.end_pos = self.start_pos
|
||||||
self.scope = self.scope.parent
|
self._scope = self._scope.parent
|
||||||
if isinstance(self.scope, pr.Module) \
|
if isinstance(self._scope, pr.Module) \
|
||||||
and not isinstance(self.scope, pr.SubModule):
|
and not isinstance(self._scope, pr.SubModule):
|
||||||
self.scope = self.module
|
self._scope = self.module
|
||||||
|
|
||||||
use_as_parent_scope = self.top_module if isinstance(self.scope,
|
use_as_parent_scope = self.top_module if isinstance(self._scope,
|
||||||
pr.SubModule) else self.scope
|
pr.SubModule) else self._scope
|
||||||
first_pos = self.start_pos
|
first_pos = self.start_pos
|
||||||
if tok == 'def':
|
if tok == 'def':
|
||||||
func = self._parse_function()
|
func = self._parse_function()
|
||||||
@@ -503,7 +503,7 @@ class Parser(object):
|
|||||||
self.start_pos[0])
|
self.start_pos[0])
|
||||||
continue
|
continue
|
||||||
self.freshscope = True
|
self.freshscope = True
|
||||||
self.scope = self.scope.add_scope(func, self._decorators)
|
self._scope = self._scope.add_scope(func, self._decorators)
|
||||||
self._decorators = []
|
self._decorators = []
|
||||||
elif tok == 'class':
|
elif tok == 'class':
|
||||||
cls = self._parse_class()
|
cls = self._parse_class()
|
||||||
@@ -511,7 +511,7 @@ class Parser(object):
|
|||||||
debug.warning("class: syntax error@%s" % self.start_pos[0])
|
debug.warning("class: syntax error@%s" % self.start_pos[0])
|
||||||
continue
|
continue
|
||||||
self.freshscope = True
|
self.freshscope = True
|
||||||
self.scope = self.scope.add_scope(cls, self._decorators)
|
self._scope = self._scope.add_scope(cls, self._decorators)
|
||||||
self._decorators = []
|
self._decorators = []
|
||||||
# import stuff
|
# import stuff
|
||||||
elif tok == 'import':
|
elif tok == 'import':
|
||||||
@@ -522,7 +522,7 @@ class Parser(object):
|
|||||||
i = pr.Import(self.module, first_pos, end_pos, m,
|
i = pr.Import(self.module, first_pos, end_pos, m,
|
||||||
alias, defunct=defunct)
|
alias, defunct=defunct)
|
||||||
self._check_user_stmt(i)
|
self._check_user_stmt(i)
|
||||||
self.scope.add_import(i)
|
self._scope.add_import(i)
|
||||||
if not imports:
|
if not imports:
|
||||||
i = pr.Import(self.module, first_pos, self.end_pos, None,
|
i = pr.Import(self.module, first_pos, self.end_pos, None,
|
||||||
defunct=True)
|
defunct=True)
|
||||||
@@ -559,7 +559,7 @@ class Parser(object):
|
|||||||
alias, mod, star, relative_count,
|
alias, mod, star, relative_count,
|
||||||
defunct=defunct or defunct2)
|
defunct=defunct or defunct2)
|
||||||
self._check_user_stmt(i)
|
self._check_user_stmt(i)
|
||||||
self.scope.add_import(i)
|
self._scope.add_import(i)
|
||||||
self.freshscope = False
|
self.freshscope = False
|
||||||
#loops
|
#loops
|
||||||
elif tok == 'for':
|
elif tok == 'for':
|
||||||
@@ -569,7 +569,7 @@ class Parser(object):
|
|||||||
if tok == ':':
|
if tok == ':':
|
||||||
s = [] if statement is None else [statement]
|
s = [] if statement is None else [statement]
|
||||||
f = pr.ForFlow(self.module, s, first_pos, set_stmt)
|
f = pr.ForFlow(self.module, s, first_pos, set_stmt)
|
||||||
self.scope = self.scope.add_statement(f)
|
self._scope = self._scope.add_statement(f)
|
||||||
else:
|
else:
|
||||||
debug.warning('syntax err, for flow started @%s',
|
debug.warning('syntax err, for flow started @%s',
|
||||||
self.start_pos[0])
|
self.start_pos[0])
|
||||||
@@ -612,13 +612,13 @@ class Parser(object):
|
|||||||
# the flow statement, because a dedent releases the
|
# the flow statement, because a dedent releases the
|
||||||
# main scope, so just take the last statement.
|
# main scope, so just take the last statement.
|
||||||
try:
|
try:
|
||||||
s = self.scope.statements[-1].set_next(f)
|
s = self._scope.statements[-1].set_next(f)
|
||||||
except (AttributeError, IndexError):
|
except (AttributeError, IndexError):
|
||||||
# If set_next doesn't exist, just add it.
|
# If set_next doesn't exist, just add it.
|
||||||
s = self.scope.add_statement(f)
|
s = self._scope.add_statement(f)
|
||||||
else:
|
else:
|
||||||
s = self.scope.add_statement(f)
|
s = self._scope.add_statement(f)
|
||||||
self.scope = s
|
self._scope = s
|
||||||
else:
|
else:
|
||||||
for i in inputs:
|
for i in inputs:
|
||||||
i.parent = use_as_parent_scope
|
i.parent = use_as_parent_scope
|
||||||
@@ -629,7 +629,7 @@ class Parser(object):
|
|||||||
s = self.start_pos
|
s = self.start_pos
|
||||||
self.freshscope = False
|
self.freshscope = False
|
||||||
# add returns to the scope
|
# add returns to the scope
|
||||||
func = self.scope.get_parent_until(pr.Function)
|
func = self._scope.get_parent_until(pr.Function)
|
||||||
if tok == 'yield':
|
if tok == 'yield':
|
||||||
func.is_generator = True
|
func.is_generator = True
|
||||||
|
|
||||||
@@ -646,7 +646,7 @@ class Parser(object):
|
|||||||
elif tok == 'global':
|
elif tok == 'global':
|
||||||
stmt, tok = self._parse_statement(self.current)
|
stmt, tok = self._parse_statement(self.current)
|
||||||
if stmt:
|
if stmt:
|
||||||
self.scope.add_statement(stmt)
|
self._scope.add_statement(stmt)
|
||||||
for name in stmt.used_vars:
|
for name in stmt.used_vars:
|
||||||
# add the global to the top, because there it is
|
# add the global to the top, because there it is
|
||||||
# important.
|
# important.
|
||||||
@@ -660,7 +660,7 @@ class Parser(object):
|
|||||||
elif tok == 'assert':
|
elif tok == 'assert':
|
||||||
stmt, tok = self._parse_statement()
|
stmt, tok = self._parse_statement()
|
||||||
stmt.parent = use_as_parent_scope
|
stmt.parent = use_as_parent_scope
|
||||||
self.scope.asserts.append(stmt)
|
self._scope.asserts.append(stmt)
|
||||||
# default
|
# default
|
||||||
elif token_type in [tokenize.NAME, tokenize.STRING,
|
elif token_type in [tokenize.NAME, tokenize.STRING,
|
||||||
tokenize.NUMBER] \
|
tokenize.NUMBER] \
|
||||||
@@ -670,7 +670,7 @@ class Parser(object):
|
|||||||
# by the statement parser.
|
# by the statement parser.
|
||||||
stmt, tok = self._parse_statement(self.current)
|
stmt, tok = self._parse_statement(self.current)
|
||||||
if stmt:
|
if stmt:
|
||||||
self.scope.add_statement(stmt)
|
self._scope.add_statement(stmt)
|
||||||
self.freshscope = False
|
self.freshscope = False
|
||||||
else:
|
else:
|
||||||
if token_type not in [tokenize.COMMENT, tokenize.INDENT,
|
if token_type not in [tokenize.COMMENT, tokenize.INDENT,
|
||||||
|
|||||||
@@ -16,11 +16,11 @@ is the easiest way to write a parser. The same behaviour applies to ``Param``,
|
|||||||
which is being used in a function definition.
|
which is being used in a function definition.
|
||||||
|
|
||||||
The easiest way to play with this module is to use :class:`parsing.Parser`.
|
The easiest way to play with this module is to use :class:`parsing.Parser`.
|
||||||
:attr:`parsing.Parser.scope` holds an instance of :class:`SubModule`:
|
:attr:`parsing.Parser.module` holds an instance of :class:`SubModule`:
|
||||||
|
|
||||||
>>> from jedi.parsing import Parser
|
>>> from jedi.parsing import Parser
|
||||||
>>> parser = Parser('import os', 'example.py')
|
>>> parser = Parser('import os', 'example.py')
|
||||||
>>> submodule = parser.scope
|
>>> submodule = parser.module
|
||||||
>>> submodule
|
>>> submodule
|
||||||
<SubModule: example.py@1-1>
|
<SubModule: example.py@1-1>
|
||||||
|
|
||||||
@@ -247,14 +247,14 @@ class Scope(Simple, IsScope):
|
|||||||
... b = y
|
... b = y
|
||||||
... b.c = z
|
... b.c = z
|
||||||
... ''')
|
... ''')
|
||||||
>>> parser.scope.get_defined_names()
|
>>> parser.module.get_defined_names()
|
||||||
[<Name: a@2,0>, <Name: b@3,0>]
|
[<Name: a@2,0>, <Name: b@3,0>]
|
||||||
|
|
||||||
Note that unlike :meth:`get_set_vars`, assignment to object
|
Note that unlike :meth:`get_set_vars`, assignment to object
|
||||||
attribute does not change the result because it does not change
|
attribute does not change the result because it does not change
|
||||||
the defined names in this scope.
|
the defined names in this scope.
|
||||||
|
|
||||||
>>> parser.scope.get_set_vars()
|
>>> parser.module.get_set_vars()
|
||||||
[<Name: a@2,0>, <Name: b@3,0>, <Name: b.c@4,0>]
|
[<Name: a@2,0>, <Name: b@3,0>, <Name: b.c@4,0>]
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -377,7 +377,7 @@ class TestRegression(TestBase):
|
|||||||
# jedi issue #150
|
# jedi issue #150
|
||||||
s = "x()\nx( )\nx( )\nx ( )"
|
s = "x()\nx( )\nx( )\nx ( )"
|
||||||
parser = parsing.Parser(s)
|
parser = parsing.Parser(s)
|
||||||
for i, s in enumerate(parser.scope.statements, 3):
|
for i, s in enumerate(parser.module.statements, 3):
|
||||||
for c in s.get_commands():
|
for c in s.get_commands():
|
||||||
self.assertEqual(c.execution.end_pos[1], i)
|
self.assertEqual(c.execution.end_pos[1], i)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user