again fixed some todos

This commit is contained in:
David Halter
2012-03-02 15:08:38 +01:00
parent 0e57dce545
commit 626e6ad83e
3 changed files with 68 additions and 39 deletions

View File

@@ -6,6 +6,7 @@ import sys
import types import types
from pyfuzzyparser import PyFuzzyParser from pyfuzzyparser import PyFuzzyParser
import pyfuzzyparser
def complete(file_name, line, colon): def complete(file_name, line, colon):
@@ -222,13 +223,13 @@ showdbg()
print cmpl.parser.top.get_code() print cmpl.parser.top.get_code()
#print cmpl.parser.top.subscopes[1].subscopes[0].get_code() #print cmpl.parser.top.subscopes[1].subscopes[0].get_code()
print 'global names:'
names = cmpl.parser.top.get_names() names = cmpl.parser.top.get_names()
for n in names: print [n.names for n in names]
try:
print n.names
except:
print n.name
print 'global names:'
names = cmpl.parser.top.subscopes[0].get_names()
print [n.names for n in names]
p = cmpl.parser p = cmpl.parser
s = p.top s = p.top

View File

@@ -30,9 +30,7 @@ Ignored statements:
TODO be tolerant with indents TODO be tolerant with indents
TODO dictionaries not working with statement parser TODO dictionaries not working with statement parser
TODO except has local vars
TODO take special care for future imports TODO take special care for future imports
TODO add global statements
""" """
import sys import sys
@@ -71,6 +69,7 @@ class Scope(object):
self.subscopes = [] self.subscopes = []
self.imports = [] self.imports = []
self.statements = [] self.statements = []
self.global_vars = []
self.docstr = docstr self.docstr = docstr
self.parent = None self.parent = None
self.indent = indent self.indent = indent
@@ -109,6 +108,17 @@ class Scope(object):
def add_import(self, imp): def add_import(self, imp):
self.imports.append(imp) self.imports.append(imp)
def add_global(self, name):
"""
Global means in these context a function (subscope) which has a global
statement.
This is only relevant for the top scope.
:param name: The name of the global.
:type name: Name
"""
self.global_vars.append(name)
def _checkexisting(self, test): def _checkexisting(self, test):
"Convienance function... keep out duplicates" "Convienance function... keep out duplicates"
if test.find('=') > -1: if test.find('=') > -1:
@@ -148,7 +158,10 @@ class Scope(object):
n = [] n = []
for stmt in self.statements: for stmt in self.statements:
n += stmt.get_names() n += stmt.get_names()
n += self.subscopes
# function and class names
n += [s.name for s in self.subscopes]
n += self.global_vars
return n return n
def is_empty(self): def is_empty(self):
@@ -196,7 +209,7 @@ class Function(Scope):
:param name: The Function name. :param name: The Function name.
:type name: string :type name: string
:param params: The parameters of a Function. :param params: The parameters (Name) of a Function.
:type name: list :type name: list
:param indent: The indent level of the flow statement. :param indent: The indent level of the flow statement.
:type indent: int :type indent: int
@@ -218,14 +231,8 @@ class Function(Scope):
return str return str
def get_names(self): def get_names(self):
""" n = self.params
Get the names for the flow. This includes also a call to the super n += super(Function, self).get_names()
class.
"""
n = self.set_args
if self.next:
n += self.next.get_names()
n += super(Flow, self).get_names()
return n return n
@@ -323,8 +330,6 @@ class Import(object):
:type star: bool :type star: bool
:raises: None :raises: None
TODO check star?
""" """
def __init__(self, line_nr, namespace, alias='', from_ns='', star=False): def __init__(self, line_nr, namespace, alias='', from_ns='', star=False):
self.line_nr = line_nr self.line_nr = line_nr
@@ -396,7 +401,7 @@ class Name(object):
""" """
def __init__(self, names, indent, line_nr): def __init__(self, names, indent, line_nr):
super(Name, self).__init__() super(Name, self).__init__()
self.names = names self.names = tuple(names)
self.indent = indent self.indent = indent
self.line_nr = line_nr self.line_nr = line_nr
self.parent = None self.parent = None
@@ -408,6 +413,17 @@ class Name(object):
def __str__(self): def __str__(self):
return self.get_code() return self.get_code()
def __eq__(self, other):
return self.names == other.names \
and self.indent == other.indent \
and self.line_nr == self.line_nr
def __ne__(self, other):
return not self.__eq__(other)
def __hash__(self):
return hash(self.names) + hash(self.indent) + hash(self.line_nr)
class PyFuzzyParser(object): class PyFuzzyParser(object):
""" """
@@ -549,6 +565,8 @@ class PyFuzzyParser(object):
if token_type != tokenize.NAME: if token_type != tokenize.NAME:
return None return None
fname = Name([fname], ind, self.line_nr)
token_type, open, ind = self.next() token_type, open, ind = self.next()
if open != '(': if open != '(':
return None return None
@@ -574,6 +592,8 @@ class PyFuzzyParser(object):
% (self.line_nr, token.tok_name[token_type], cname) % (self.line_nr, token.tok_name[token_type], cname)
return None return None
cname = Name([cname], ind, self.line_nr)
super = [] super = []
token_type, next, ind = self.next() token_type, next, ind = self.next()
if next == '(': if next == '(':
@@ -661,17 +681,14 @@ class PyFuzzyParser(object):
else: else:
path, token_type, tok, start_indent = \ path, token_type, tok, start_indent = \
self._parsedotname(self.current) self._parsedotname(self.current)
print 'path', path
n = Name(path, start_indent, self.line_nr) n = Name(path, start_indent, self.line_nr)
if tok == '(': if tok == '(':
# it must be a function # it must be a function
used_funcs.append(n) used_funcs.append(n)
else: else:
if not n.names[0] in ['global']:
used_vars.append(n) used_vars.append(n)
if string:
print 'str', string[-1]
if string and re.match(r'[\w\d]', string[-1]): if string and re.match(r'[\w\d]', string[-1]):
print 'yay'
string += ' ' string += ' '
#if token_type == tokenize.NAME \ #if token_type == tokenize.NAME \
# and self.last_token[0] == tokenize.NAME: # and self.last_token[0] == tokenize.NAME:
@@ -762,7 +779,10 @@ class PyFuzzyParser(object):
mod = Name(mod, start_indent, self.line_nr) mod = Name(mod, start_indent, self.line_nr)
names = self._parseimportlist() names = self._parseimportlist()
for name, alias in names: for name, alias in names:
i = Import(self.line_nr, name, alias, mod) star = name.names[0] == '*'
if star:
name = None
i = Import(self.line_nr, name, alias, mod, star)
self.scope.add_import(i) self.scope.add_import(i)
freshscope = False freshscope = False
#loops #loops
@@ -778,6 +798,7 @@ class PyFuzzyParser(object):
elif tok in ['if', 'while', 'try', 'with'] + extended_flow: elif tok in ['if', 'while', 'try', 'with'] + extended_flow:
# TODO with statement has local variables # TODO with statement has local variables
# TODO except has local vars
command = tok command = tok
statement, tok = self._parse_statement() statement, tok = self._parse_statement()
if tok == ':': if tok == ':':
@@ -791,9 +812,14 @@ class PyFuzzyParser(object):
self.scope = self.scope.add_statement(f) self.scope = self.scope.add_statement(f)
elif tok == 'global': elif tok == 'global':
self._parse_statement(self.current) stmt, tok = self._parse_statement(self.current)
pass if stmt:
# TODO add suport for global self.scope.add_statement(stmt)
print 'global_vars', stmt.used_vars
for name in stmt.used_vars:
# add the global to the top, because there it is
# important.
self.top.add_global(name)
elif token_type == tokenize.STRING: elif token_type == tokenize.STRING:
if freshscope: if freshscope:
self.scope.add_docstr(tok) self.scope.add_docstr(tok)
@@ -806,9 +832,9 @@ class PyFuzzyParser(object):
#print "_not_implemented_", tok, self.parserline #print "_not_implemented_", tok, self.parserline
except StopIteration: # thrown on EOF except StopIteration: # thrown on EOF
pass pass
#except: except StopIteration: # TODO catch the right error
# dbg("parse error: %s, %s @ %s" % dbg("parse error: %s, %s @ %s" %
# (sys.exc_info()[0], sys.exc_info()[1], self.parserline)) (sys.exc_info()[0], sys.exc_info()[1], self.parserline))
return self.top return self.top

14
test.py
View File

@@ -10,6 +10,13 @@ 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");
def func():
#def test:
# return 2
cdef = A()
return test
class Intro(object): class Intro(object):
def testing(self, string): def testing(self, string):
return string+"," return string+","
@@ -46,12 +53,6 @@ class Empty():
cdef = 5 cdef = 5
cdef cdef cdef cdef
def func():
#def test:
# return 2
cdef = A()
return test
for i in range(3): for i in range(3):
asdf = aaa asdf = aaa
print 'blub' print 'blub'
@@ -67,6 +68,7 @@ def ass_test(a):
# test strange statements # test strange statements
[a,c] ; {1: a}; (1,); `a` [a,c] ; {1: a}; (1,); `a`
result = int((a+b)*2) result = int((a+b)*2)
global global_test
return result return result
matrix = [[1,2,3], [4,5,6], [7,8,9]] matrix = [[1,2,3], [4,5,6], [7,8,9]]