Clean up the parser tree.

This commit is contained in:
Dave Halter
2014-12-18 03:38:24 +01:00
parent ab9571bccd
commit 98eb4a71a1

View File

@@ -20,18 +20,13 @@ The easiest way to play with this module is to use :class:`parsing.Parser`.
>>> submodule >>> submodule
<SubModule: example.py@1-2> <SubModule: example.py@1-2>
Any subclasses of :class:`Scope`, including :class:`SubModule` has Any subclasses of :class:`Scope`, including :class:`SubModule` has an attribute
attribute :attr:`imports <Scope.imports>`. This attribute has import :attr:`imports <Scope.imports>`:
statements in this scope. Check this out:
>>> submodule.imports >>> submodule.imports
[<ImportName: import os@1,0>] [<ImportName: import os@1,0>]
See also :attr:`Scope.subscopes` and :attr:`Scope.statements`. See also :attr:`Scope.subscopes` and :attr:`Scope.statements`.
# TODO New docstring
""" """
import os import os
import re import re
@@ -414,10 +409,6 @@ class Simple(Base):
def start_pos(self): def start_pos(self):
return self.children[0].start_pos return self.children[0].start_pos
@property
def _sub_module(self):
return self.get_parent_until()
@property @property
def end_pos(self): def end_pos(self):
return self.children[-1].end_pos return self.children[-1].end_pos
@@ -479,14 +470,6 @@ class IsScope(use_metaclass(IsScopeMeta)):
pass pass
def _return_empty_list():
"""
Necessary for pickling. It needs to be reachable for pickle, cannot
be a lambda or a closure.
"""
return []
class Scope(Simple, DocstringMixin): class Scope(Simple, DocstringMixin):
""" """
Super class for the parser tree, which represents the state of a python Super class for the parser tree, which represents the state of a python
@@ -547,16 +530,6 @@ class Scope(Simple, DocstringMixin):
def is_scope(self): def is_scope(self):
return True return True
def get_imports(self):
""" Gets also the imports within flow statements """
raise NotImplementedError
return []
i = [] + self.imports
for s in self.statements:
if isinstance(s, Scope):
i += s.get_imports()
return i
@Python3Method @Python3Method
def get_defined_names(self): def get_defined_names(self):
""" """
@@ -598,14 +571,7 @@ class Scope(Simple, DocstringMixin):
checks += [r for r in self.returns if r is not None] checks += [r for r in self.returns if r is not None]
for s in checks: for s in checks:
if isinstance(s, Flow): if s.start_pos <= pos <= s.end_pos:
p = s.get_statement_for_position(pos, include_imports)
while s.next and not p:
s = s.next
p = s.get_statement_for_position(pos, include_imports)
if p:
return p
elif s.start_pos <= pos <= s.end_pos:
return s return s
for s in self.subscopes: for s in self.subscopes:
@@ -686,10 +652,6 @@ class SubModule(Scope, Module):
""" """
self.global_names = names self.global_names = names
def add_global(self, name):
# set no parent here, because globals are not defined in this scope.
self.global_vars.append(name)
def get_defined_names(self): def get_defined_names(self):
n = super(SubModule, self).get_defined_names() n = super(SubModule, self).get_defined_names()
# TODO uncomment # TODO uncomment
@@ -1277,10 +1239,6 @@ class Param(Base):
df = '' if self.default is None else '=' + self.default.get_code() df = '' if self.default is None else '=' + self.default.get_code()
return self.tfpdef.get_code() + df return self.tfpdef.get_code() + df
def add_annotation(self, annotation_stmt):
annotation_stmt.parent = self.use_as_parent
self.annotation_stmt = annotation_stmt
def __repr__(self): def __repr__(self):
default = '' if self.default is None else '=%s' % self.default default = '' if self.default is None else '=%s' % self.default
return '<%s: %s>' % (type(self).__name__, str(self.tfpdef) + default) return '<%s: %s>' % (type(self).__name__, str(self.tfpdef) + default)
@@ -1310,9 +1268,6 @@ class CompFor(Simple):
arr.append(name) arr.append(name)
return dct return dct
def get_rhs(self):
return self.children[3]
def get_defined_names(self): def get_defined_names(self):
return _defined_names(self.children[1]) return _defined_names(self.children[1])