with statement - multiple arguments

This commit is contained in:
David Halter
2012-04-22 14:10:10 +02:00
parent 909a6bfa2b
commit 190e02b61f
4 changed files with 59 additions and 40 deletions

View File

@@ -29,7 +29,7 @@ class Parser(object):
'int': '0', 'int': '0',
'dictionary': '{}', 'dictionary': '{}',
'list': '[]', 'list': '[]',
'object': '{}', 'file object': 'file("")',
# TODO things like dbg: ('not working', 'tuple of integers') # TODO things like dbg: ('not working', 'tuple of integers')
} }
cache = {} cache = {}

View File

@@ -6,13 +6,7 @@ follow_statement -> follow_call -> follow_paths -> follow_path
TODO include super classes TODO include super classes
""" """
from _compatibility import next
# python2.5 compatibility
try:
next
except NameError:
def next(obj):
return obj.next()
import itertools import itertools
import copy import copy
@@ -270,7 +264,10 @@ def get_scopes_for_name(scope, name, search_global=False):
if isinstance(par, parsing.Flow): if isinstance(par, parsing.Flow):
# TODO get Flow data, which is defined by the loop # TODO get Flow data, which is defined by the loop
# (or with) # (or with)
pass if par.command == 'for':
print 'for', par, par.inits
else:
debug.warning('Why are you here? %s' % par.command)
elif isinstance(par, parsing.Param) \ elif isinstance(par, parsing.Param) \
and isinstance(par.parent.parent, parsing.Class) \ and isinstance(par.parent.parent, parsing.Class) \
and par.position == 0: and par.position == 0:

View File

@@ -142,9 +142,9 @@ c = u"asdf".join([1,2])
matrix_test = [[1,2], [1,3]] matrix_test = [[1,2], [1,3]]
c = c1().c3().sleep() c = c1().c3().sleep()
asdf = c1; asdf2 = asdf b= asdf2 asdf = c1; asdf2 = asdf b= asdf2
with open('') as fi: pass
c = b().c3() c = b().c3()
1.0.fromhex(); import flask ; flsk = flask.Flask + flask.Request; 1.0.fromhex(); import flask ; flsk = flask.Flask + flask.Request;
abc = [1,2+3]; abc[0]. abc = [1,2+3]; abc[0].
import pylab; def add(a1,b1): nana = 1; return a1+b1 import pylab; def add(a1,b1): nana = 1; return a1+b1
abc = datetime; return [abc][0]. ;pylab.; add(1+2,2).real abc = datetime; return [abc][0]. ;pylab.; add(1+2,2).; for fi in [1,2]: fi.

View File

@@ -31,6 +31,7 @@ Ignored statements:
TODO take special care for future imports TODO take special care for future imports
TODO check meta classes TODO check meta classes
""" """
from _compatibility import next
import tokenize import tokenize
import cStringIO import cStringIO
@@ -355,8 +356,8 @@ class Flow(Scope):
:param command: The flow command, if, while, else, etc. :param command: The flow command, if, while, else, etc.
:type command: str :type command: str
:param statement: The statement after the flow comand -> while 'statement'. :param inits: The initializations of a flow -> while 'statement'.
:type statement: Statement :type inits: list(Statement)
:param indent: The indent level of the flow statement. :param indent: The indent level of the flow statement.
:type indent: int :type indent: int
:param line_nr: Line number of the flow statement. :param line_nr: Line number of the flow statement.
@@ -364,12 +365,13 @@ class Flow(Scope):
:param set_vars: Local variables used in the for loop (only there). :param set_vars: Local variables used in the for loop (only there).
:type set_vars: list :type set_vars: list
""" """
def __init__(self, command, statement, indent, line_nr, set_vars=None): def __init__(self, command, inits, indent, line_nr, set_vars=None):
super(Flow, self).__init__(indent, line_nr, '') super(Flow, self).__init__(indent, line_nr, '')
self.command = command self.command = command
self.statement = statement # These have to be statements, because of with, which takes multiple.
if statement: self.inits = inits
statement.parent = self for s in inits:
s.parent = self
if set_vars == None: if set_vars == None:
self.set_vars = [] self.set_vars = []
else: else:
@@ -385,10 +387,10 @@ class Flow(Scope):
else: else:
vars = '' vars = ''
if self.statement: stmts = []
stmt = self.statement.get_code(new_line=False) for s in self.inits:
else: stmts.append(s.get_code(new_line=False))
stmt = '' stmt = ', '.join(stmts)
str = "%s %s%s:\n" % (self.command, vars, stmt) str = "%s %s%s:\n" % (self.command, vars, stmt)
str += super(Flow, self).get_code(True, indention) str += super(Flow, self).get_code(True, indention)
if self.next: if self.next:
@@ -405,8 +407,8 @@ class Flow(Scope):
""" """
if is_internal_call: if is_internal_call:
n = list(self.set_vars) n = list(self.set_vars)
if self.statement: for s in self.inits:
n += self.statement.set_vars n += s.set_vars
if self.next: if self.next:
n += self.next.get_set_vars(is_internal_call) n += self.next.get_set_vars(is_internal_call)
n += super(Flow, self).get_set_vars() n += super(Flow, self).get_set_vars()
@@ -549,7 +551,8 @@ class Statement(Simple):
close_brackets = False close_brackets = False
debug.dbg('tok_list', self.token_list) debug.dbg('tok_list', self.token_list)
for i, tok_temp in enumerate(self.token_list): tok_iter = enumerate(self.token_list)
for i, tok_temp in tok_iter:
#print 'tok', tok_temp, result #print 'tok', tok_temp, result
try: try:
token_type, tok, indent = tok_temp token_type, tok, indent = tok_temp
@@ -562,6 +565,12 @@ class Statement(Simple):
result = Array(Array.EMPTY, self) result = Array(Array.EMPTY, self)
top = result top = result
continue continue
elif tok == 'as':
# TODO change with parser to allow multiple statements
# This is the name and can be ignored, because set_vars is
# already caring for this.
next(tok_iter)
continue
except TypeError: except TypeError:
# the token is a Name, which has already been parsed # the token is a Name, which has already been parsed
tok = tok_temp tok = tok_temp
@@ -1292,36 +1301,49 @@ class PyFuzzyParser(object):
if tok == 'in': if tok == 'in':
statement, tok = self._parse_statement() statement, tok = self._parse_statement()
if tok == ':': if tok == ':':
f = Flow('for', statement, indent, self.line_nr, \ f = Flow('for', [statement], indent,
value_list) self.line_nr, value_list)
debug.dbg("new scope: flow for@%s" % (f.line_nr)) debug.dbg("new scope: flow for@%s" % (f.line_nr))
self.scope = self.scope.add_statement(f) self.scope = self.scope.add_statement(f)
elif tok in ['if', 'while', 'try', 'with'] + extended_flow: elif tok in ['if', 'while', 'try', 'with'] + extended_flow:
added_breaks = [] added_breaks = []
command = tok command = tok
if command == 'except': if command in ['except', 'with']:
added_breaks += (',') added_breaks.append(',')
statement, tok = \ # multiple statements because of with
self._parse_statement(added_breaks=added_breaks) inits = []
if tok in added_breaks: first = True
# the except statement defines a var while first or command == 'with' and tok != ':':
# this is only true for python 2 statement, tok = \
path, token_type, tok, start_indent, start_line2 = \ self._parse_statement(added_breaks=added_breaks)
self._parsedotname() if command == 'except' and tok in added_breaks:
n = Name(path, start_indent, start_line2, self.line_nr) # the except statement defines a var
statement.set_vars.append(n) # this is only true for python 2
statement.code += ',' + n.get_code() path, token_type, tok, start_indent, start_line2 = \
self._parsedotname()
n = Name(path, start_indent, start_line2,
self.line_nr)
statement.set_vars.append(n)
statement.code += ',' + n.get_code()
if statement:
inits.append(statement)
first = False
if tok == ':': if tok == ':':
f = Flow(command, statement, indent, self.line_nr) f = Flow(command, inits, indent, self.line_nr)
debug.dbg("new scope: flow %s@%s" debug.dbg("new scope: flow %s@%s"
% (command, self.line_nr)) % (command, self.line_nr))
if command in extended_flow: if command in extended_flow:
# the last statement has to be another part of # the last statement has to be another part of
# the flow statement # the flow statement, because a dedent releases the
# main scope, so just take the last statement.
self.scope = self.scope.statements[-1].set_next(f) self.scope = self.scope.statements[-1].set_next(f)
else: else:
self.scope = self.scope.add_statement(f) self.scope = self.scope.add_statement(f)
else:
debug.warning('syntax err, flow started @%s',
self.line_nr)
# globals # globals
elif tok == 'global': elif tok == 'global':
stmt, tok = self._parse_statement(self.current) stmt, tok = self._parse_statement(self.current)