moved PushBackIterator to parsing

This commit is contained in:
David Halter
2012-08-27 15:18:03 +02:00
parent 9fd7b96043
commit 5d6e30c6ab
2 changed files with 41 additions and 30 deletions

View File

@@ -565,7 +565,7 @@ class Execution(Executable):
for key, value in var_arg_iterator:
# Iterate until a key argument is found.
if key:
var_arg_iterator.push_back(key, value)
var_arg_iterator.push_back((key, value))
break
values.append(value)
elif assignment[0] == '**':
@@ -630,28 +630,7 @@ class Execution(Executable):
else:
yield None, var_arg
class PushBackIterator(object):
def __init__(self, iterator):
self.pushes = []
self.iterator = iterator
def push_back(self, key, value):
self.pushes.append((key, value))
def __iter__(self):
return self
def next(self):
""" Python 2 Compatibility """
return self.__next__()
def __next__(self):
try:
return self.pushes.pop()
except IndexError:
return next(self.iterator)
return iter(PushBackIterator(iterate()))
return iter(parsing.PushBackIterator(iterate()))
def get_set_vars(self):
return self.get_defined_names()
@@ -1198,6 +1177,9 @@ def follow_call_list(call_list):
It is used to evaluate a two dimensional object, that has calls, arrays and
operators in it.
"""
def evaluate_list_comprehension(self, call_list):
for a in call_list:
pass
if parsing.Array.is_type(call_list, parsing.Array.TUPLE,
parsing.Array.DICT):
# Tuples can stand just alone without any braces. These would be
@@ -1217,9 +1199,11 @@ def follow_call_list(call_list):
result.append(call)
# The string tokens are just operations (+, -, etc.)
elif not isinstance(call, str):
#if str(call.name) == 'for': <--- list comprehensions
# print '\n\ndini mueter'
if str(call.name) == 'if':
if str(call.name) == 'for':
# list comprehensions
print '\n\ndini mueter', call_list
return []
elif str(call.name) == 'if':
# Ternary operators.
while True:
try:

View File

@@ -54,6 +54,28 @@ def indent_block(text, indention=" "):
return '\n'.join(map(lambda s: indention + s, lines)) + temp
class PushBackIterator(object):
def __init__(self, iterator):
self.pushes = []
self.iterator = iterator
def push_back(self, value):
self.pushes.append(value)
def __iter__(self):
return self
def next(self):
""" Python 2 Compatibility """
return self.__next__()
def __next__(self):
if self.pushes:
return self.pushes.pop()
else:
return next(self.iterator)
class Base(object):
"""
This is just here to have an isinstance check, which is also used on
@@ -1208,7 +1230,7 @@ class PyFuzzyParser(object):
return Class(cname, super, first_pos)
def _parse_statement(self, pre_used_token=None, added_breaks=None,
stmt_class=Statement):
stmt_class=Statement, list_comp=False):
"""
Parses statements like:
@@ -1293,7 +1315,8 @@ class PyFuzzyParser(object):
continue
#token_type, tok = self.next()
b = [')', ']']
in_clause, tok = self._parse_statement(added_breaks=b)
in_clause, tok = self._parse_statement(added_breaks=b,
list_comp=True)
if tok not in b:
debug.warning('list comprehension brackets %s@%s' %
(tok, self.start_pos[0]))
@@ -1374,12 +1397,16 @@ class PyFuzzyParser(object):
except AttributeError:
debug.warning('return in non-function')
if list_comp:
self.gen.push_back(self._current_full)
return stmt, tok
def next(self):
""" Generate the next tokenize pattern. """
self._current_full = next(self.gen)
type, tok, self._tokenize_start_pos, self._tokenize_end_pos, \
self.parserline = next(self.gen)
self.parserline = self._current_full
if self.user_position and self.start_pos[0] == self.user_position[0]:
debug.dbg('user scope found [%s] = %s' % \
(self.parserline.replace('\n', ''), repr(self.scope)))
@@ -1400,7 +1427,7 @@ class PyFuzzyParser(object):
:raises: IndentationError
"""
buf = BytesIO(self.code)
self.gen = tokenize_func(buf.readline)
self.gen = PushBackIterator(tokenize_func(buf.readline))
extended_flow = ['else', 'elif', 'except', 'finally']
statement_toks = ['{', '[', '(', '`']