new scope function: get_simple_for_line

This commit is contained in:
David Halter
2012-03-07 12:32:58 +01:00
parent 3be36968ce
commit 3fb2d50e93
4 changed files with 45 additions and 18 deletions

View File

@@ -13,5 +13,11 @@ def complete(source, row, colum, file_callback=None):
:return: list :return: list
:rtype: list :rtype: list
""" """
row = 38
p = parsing.PyFuzzyParser(source, row) p = parsing.PyFuzzyParser(source, row)
return []
print
print
print p.user_scope
print p.user_scope.get_simple_for_line(row)
return p.user_scope.get_set_vars()

View File

@@ -49,7 +49,16 @@ def indent_block(text, indention=" "):
return '\n'.join(map(lambda s: indention + s, lines)) + temp return '\n'.join(map(lambda s: indention + s, lines)) + temp
class Scope(object): class Simple(object):
""" The super class for Scope, Import and Statement. """
def __init__(self, indent, line_nr, line_end=None):
self.indent = indent
self.line_nr = line_nr
self.line_end = line_end
self.parent = None
class Scope(Simple):
""" """
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
text file. text file.
@@ -65,15 +74,12 @@ class Scope(object):
:type docstr: str :type docstr: str
""" """
def __init__(self, indent, line_nr, docstr=''): def __init__(self, indent, line_nr, docstr=''):
super(Scope, self).__init__(indent, line_nr)
self.subscopes = [] self.subscopes = []
self.imports = [] self.imports = []
self.statements = [] self.statements = []
self.global_vars = [] self.global_vars = []
self.docstr = docstr self.docstr = docstr
self.parent = None
self.indent = indent
self.line_nr = line_nr
self.line_end = None
def add_scope(self, sub, decorators): def add_scope(self, sub, decorators):
# print 'push scope: [%s@%s]' % (sub.line_nr, sub.indent) # print 'push scope: [%s@%s]' % (sub.line_nr, sub.indent)
@@ -185,11 +191,22 @@ class Scope(object):
""" """
return not (self.imports or self.subscopes or self.statements) return not (self.imports or self.subscopes or self.statements)
def get_simple_for_line(self, line):
""" Get the Simple objects, which are on the line. """
simple = []
for s in self.statements:
if s.line_nr <= line <= s.line_end:
simple.append(s)
return simple
def __repr__(self): def __repr__(self):
try: try:
name = self.name name = self.name
except: except:
try:
name = self.command name = self.command
except:
name = 'global'
return "<%s: %s@%s-%s>" % \ return "<%s: %s@%s-%s>" % \
(self.__class__.__name__, name, self.line_nr, self.line_end) (self.__class__.__name__, name, self.line_nr, self.line_end)
@@ -404,7 +421,7 @@ class Import(object):
return [self.alias] if self.alias else [self.namespace] return [self.alias] if self.alias else [self.namespace]
class Statement(object): class Statement(Simple):
""" """
This is the class for all the possible statements. Which means, this class This is the class for all the possible statements. Which means, this class
stores pretty much all the Python code, except functions, classes, imports, stores pretty much all the Python code, except functions, classes, imports,
@@ -424,16 +441,14 @@ class Statement(object):
:param line_nr: Line number of the flow statement. :param line_nr: Line number of the flow statement.
:type line_nr: int :type line_nr: int
""" """
def __init__(self, code, set_vars, used_funcs, used_vars, indent, line_nr): def __init__(self, code, set_vars, used_funcs, used_vars, indent, line_nr,
line_end):
super(Statement, self).__init__(indent, line_nr, line_end)
self.code = code self.code = code
self.set_vars = set_vars self.set_vars = set_vars
self.used_funcs = used_funcs self.used_funcs = used_funcs
self.used_vars = used_vars self.used_vars = used_vars
self.indent = indent
self.line_nr = line_nr
self.parent = None
def get_code(self, new_line=True): def get_code(self, new_line=True):
if new_line: if new_line:
return self.code + '\n' return self.code + '\n'
@@ -477,6 +492,10 @@ class Name(object):
def __hash__(self): def __hash__(self):
return hash(self.names) + hash(self.indent) + hash(self.line_nr) return hash(self.names) + hash(self.indent) + hash(self.line_nr)
def __repr__(self):
return "<%s: %s@%s>" % \
(self.__class__.__name__, self.get_code(), self.line_nr)
class PyFuzzyParser(object): class PyFuzzyParser(object):
""" """
@@ -711,6 +730,8 @@ class PyFuzzyParser(object):
else: else:
token_type, tok, indent = self.next() token_type, tok, indent = self.next()
line_start = self.line_nr
# the difference between "break" and "always break" is that the latter # the difference between "break" and "always break" is that the latter
# will even break in parentheses. This is true for typical flow # will even break in parentheses. This is true for typical flow
# commands like def and class and the imports, which will never be used # commands like def and class and the imports, which will never be used
@@ -776,7 +797,7 @@ class PyFuzzyParser(object):
return None, tok return None, tok
#print 'new_stat', string, set_vars, used_funcs, used_vars #print 'new_stat', string, set_vars, used_funcs, used_vars
stmt = Statement(string, set_vars, used_funcs, used_vars,\ stmt = Statement(string, set_vars, used_funcs, used_vars,\
self.line_nr, indent) indent, line_start, self.line_nr)
return stmt, tok return stmt, tok
def next(self): def next(self):
@@ -785,7 +806,7 @@ class PyFuzzyParser(object):
(self.line_nr, indent) = position (self.line_nr, indent) = position
if self.line_nr == self.user_line: if self.line_nr == self.user_line:
print 'user scope found [%s] =%s' % \ print 'user scope found [%s] =%s' % \
(self.parserline.replace('\n', ''), self.scope.get_name()) (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 = (type, tok, indent) self.current = (type, tok, indent)

View File

@@ -1,7 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# python >= 2.4 working with readmodule_ex # python >= 2.4 working with readmodule_ex
import pyclbr import pyclbr
import cStringIO
import sys import sys
import types import types

View File

@@ -9,6 +9,7 @@ from token import OP as OP_TEST, INDENT as INDENT_TEST
aaa = 6; bbb = 13 aaa = 6; bbb = 13
ccc = bbb; d = open("test.py"); ccc = bbb; d = open("test.py");
a
def func(a,b,c=3): def func(a,b,c=3):
#def test: #def test:
@@ -31,10 +32,10 @@ class Supi(A, datetime.datetime):
def test(self): def test(self):
import time import time
print 1 print 1
self.a = 1
self.b = (1, self.b = (1,
2,[3,2 2,[3,2
]) ]);self.a = 1
self.c = {1:3, self.c = {1:3,
a = self.a a = self.a
4:2,5:9} 4:2,5:9}