parser working for get_set_vars

This commit is contained in:
David Halter
2012-03-04 02:27:06 +01:00
parent 933e908fe2
commit dd50a20d12
3 changed files with 38 additions and 21 deletions

View File

@@ -232,21 +232,21 @@ def handle_names(names):
print 'star!', n.from_ns
print 'global names:'
names = cmpl.parser.top.get_names()
names = cmpl.parser.top.get_set_vars()
handle_names(names)
print
print 'func names:'
names = cmpl.parser.top.subscopes[0].get_names()
names = cmpl.parser.top.subscopes[7].get_set_vars()
handle_names(names)
print
print 'class names:'
names = cmpl.parser.top.subscopes[2].get_names()
names = cmpl.parser.top.subscopes[2].get_set_vars()
handle_names(names)
for s in cmpl.parser.top.subscopes[2].subscopes:
print 'method names:'
names = s.get_names()
names = s.get_set_vars()
handle_names(names)

View File

@@ -33,7 +33,6 @@ TODO take special care for future imports
TODO check meta classes
"""
import sys
import tokenize
import cStringIO
import token
@@ -147,7 +146,7 @@ class Scope(object):
string = indent_block(string, indention=indention)
return string
def get_names(self):
def get_set_vars(self):
"""
Get all the names, that are active and accessible in the current
scope.
@@ -157,7 +156,7 @@ class Scope(object):
"""
n = []
for stmt in self.statements:
n += stmt.get_names()
n += stmt.get_set_vars()
# function and class names
n += [s.name for s in self.subscopes]
@@ -210,6 +209,24 @@ class Class(Scope):
str += "pass\n"
return str
def get_set_vars(self):
n = []
for s in self.subscopes:
try:
# get the self name, if there's one
self_name = s.params[0].used_vars[0].names[0]
except:
pass
else:
for n2 in s.get_set_vars():
# Only names with the selfname are being added.
# It is also important, that they have a len() of 2,
# because otherwise, they are just something else
if n2.names[0] == self_name and len(n2.names) == 2:
n.append(n2)
n += super(Class, self).get_set_vars()
return n
class Function(Scope):
"""
@@ -242,12 +259,11 @@ class Function(Scope):
str += "pass\n"
return str
def get_names(self):
#n = self.params
def get_set_vars(self):
n = []
for p in self.params:
for i, p in enumerate(self.params):
n += p.set_vars or p.used_vars
n += super(Function, self).get_names()
n += super(Function, self).get_set_vars()
return n
@@ -307,15 +323,15 @@ class Flow(Scope):
str += self.next.get_code()
return str
def get_names(self):
def get_set_vars(self):
"""
Get the names for the flow. This includes also a call to the super
class.
"""
n = self.set_vars
if self.next:
n += self.next.get_names()
n += super(Flow, self).get_names()
n += self.next.get_set_vars()
n += super(Flow, self).get_set_vars()
return n
def set_next(self, next):
@@ -410,7 +426,7 @@ class Statement(object):
else:
return self.code
def get_names(self):
def get_set_vars(self):
""" Get the names for the statement. """
return self.set_vars
@@ -847,9 +863,9 @@ class PyFuzzyParser(object):
#print "_not_implemented_", tok, self.parserline
except StopIteration: # thrown on EOF
pass
except StopIteration: # TODO catch the right error
dbg("parse error: %s, %s @ %s" %
(sys.exc_info()[0], sys.exc_info()[1], self.parserline))
#except StopIteration:
# dbg("parse error: %s, %s @ %s" %
# (sys.exc_info()[0], sys.exc_info()[1], self.parserline))
return self.top

View File

@@ -26,7 +26,7 @@ class Supi(A, datetime.datetime):
class 2
static_var = 0
def __init__():
def __init__(self):
self.b = A()
def test(self):
import time
@@ -36,6 +36,7 @@ class Supi(A, datetime.datetime):
2,[3,2
])
self.c = {1:3,
a = self.a
4:2,5:9}
return A()
@@ -69,7 +70,7 @@ def ass_test(a):
"""docstring for assignment test"""
a -= 1
# (comment without indent)
b, c, d = (1,2,3)
(b, c, d) = (1,2,3)
del b
# test strange statements
[a,c] ; {1: a}; (1,); `a`
@@ -107,7 +108,7 @@ def flow_test(a):
pass
finally:
pass
return Matrix[0,m]
return matrix[0,m]
if True or a:
print a