2.6 and 2.7 while1 grammar rule

Fixes issue #40
This commit is contained in:
rocky
2016-07-27 13:19:42 -04:00
parent 87dc5ad80c
commit 5ffd9b2be7
10 changed files with 29 additions and 6 deletions

Binary file not shown.

View File

@@ -1,9 +1,12 @@
if args == ['-']: if __file__ == ['-']:
while True: while True:
try: try:
compile(filename, doraise=True) compile(__file__, doraise=True)
except RuntimeError: except RuntimeError:
rv = 1 rv = 1
else: else:
rv = 1 rv = 1
print(rv) print(rv)
while 1:pass

View File

@@ -72,7 +72,7 @@ class PythonParser(GenericASTBuilder):
print("Instruction context:") print("Instruction context:")
for i in range(start, finish): for i in range(start, finish):
indent = ' ' if i != index else '-> ' indent = ' ' if i != index else '-> '
print("%s%s" % (indent, instructions[i].format())) print("%s%s" % (indent, instructions[i]))
raise ParserError(err_token, err_token.offset) raise ParserError(err_token, err_token.offset)
def typestring(self, token): def typestring(self, token):

View File

@@ -40,6 +40,11 @@ class Python2Parser(PythonParser):
print_nl ::= PRINT_NEWLINE print_nl ::= PRINT_NEWLINE
''' '''
def p_stmt2(self, args):
"""
while1stmt ::= SETUP_LOOP l_stmts_opt JUMP_BACK POP_BLOCK COME_FROM
"""
def p_print_to(self, args): def p_print_to(self, args):
''' '''
stmt ::= print_to stmt ::= print_to

View File

@@ -157,6 +157,8 @@ class Python26Parser(Python2Parser):
iflaststmtl ::= testexpr c_stmts_opt JUMP_BACK come_from_pop iflaststmtl ::= testexpr c_stmts_opt JUMP_BACK come_from_pop
iflaststmt ::= testexpr c_stmts_opt JUMP_ABSOLUTE come_from_pop iflaststmt ::= testexpr c_stmts_opt JUMP_ABSOLUTE come_from_pop
while1stmt ::= SETUP_LOOP l_stmts_opt JUMP_BACK COME_FROM
# Common with 2.7 # Common with 2.7
while1stmt ::= SETUP_LOOP return_stmts bp_come_from while1stmt ::= SETUP_LOOP return_stmts bp_come_from
while1stmt ::= SETUP_LOOP return_stmts COME_FROM while1stmt ::= SETUP_LOOP return_stmts COME_FROM

View File

@@ -61,6 +61,8 @@ class Python27Parser(Python2Parser):
POP_BLOCK LOAD_CONST COME_FROM POP_BLOCK LOAD_CONST COME_FROM
WITH_CLEANUP END_FINALLY WITH_CLEANUP END_FINALLY
while1stmt ::= SETUP_LOOP l_stmts_opt JUMP_BACK POP_BLOCK COME_FROM
# Common with 2.6 # Common with 2.6
while1stmt ::= SETUP_LOOP return_stmts bp_come_from while1stmt ::= SETUP_LOOP return_stmts bp_come_from
while1stmt ::= SETUP_LOOP return_stmts COME_FROM while1stmt ::= SETUP_LOOP return_stmts COME_FROM

View File

@@ -218,8 +218,19 @@ class Scanner2(scan.Scanner):
# in arbitrary value 0. # in arbitrary value 0.
customize[opname] = 0 customize[opname] = 0
elif op == self.opc.JUMP_ABSOLUTE: elif op == self.opc.JUMP_ABSOLUTE:
# Further classify JUMP_ABSOLUTE into backward jumps
# which are used in loops, and "CONTINUE" jumps which
# may appear in a "continue" statement. The loop-type
# and continue-type jumps will help us classify loop
# boundaries The continue-type jumps help us get
# "continue" statements with would otherwise be turned
# into a "pass" statement because JUMPs are sometimes
# ignored in rules as just boundary overhead. In
# comprehensions we might sometimes classify JUMP_BACK
# as CONTINUE, but that's okay since we add a grammar
# rule for that.
target = self.get_target(offset) target = self.get_target(offset)
if target < offset: if target <= offset:
if (offset in self.stmts if (offset in self.stmts
and self.code[offset+3] not in (self.opc.END_FINALLY, and self.code[offset+3] not in (self.opc.END_FINALLY,
self.opc.POP_BLOCK) self.opc.POP_BLOCK)

View File

@@ -259,7 +259,7 @@ class Scanner3(scan.Scanner):
argval = (before_args, after_args) argval = (before_args, after_args)
opname = '%s_%d+%d' % (opname, before_args, after_args) opname = '%s_%d+%d' % (opname, before_args, after_args)
elif op == self.opc.JUMP_ABSOLUTE: elif op == self.opc.JUMP_ABSOLUTE:
# Further classifhy JUMP_ABSOLUTE into backward jumps # Further classify JUMP_ABSOLUTE into backward jumps
# which are used in loops, and "CONTINUE" jumps which # which are used in loops, and "CONTINUE" jumps which
# may appear in a "continue" statement. The loop-type # may appear in a "continue" statement. The loop-type
# and continue-type jumps will help us classify loop # and continue-type jumps will help us classify loop

View File

@@ -431,7 +431,7 @@ class ParserError(python_parser.ParserError):
def __str__(self): def __str__(self):
lines = ['--- This code section failed: ---'] lines = ['--- This code section failed: ---']
lines.extend([i.format() for i in self.tokens]) lines.extend([str(i) for i in self.tokens])
lines.extend( ['', str(self.error)] ) lines.extend( ['', str(self.error)] )
return '\n'.join(lines) return '\n'.join(lines)