Add Python 2.3 rule for "if 1: ..."

Fully fixes #97 for Python 2.3. Python 2.4 was fixed in a previous commit.
This commit is contained in:
rocky
2017-04-13 01:14:49 -04:00
parent be9194c223
commit b4ded92822
3 changed files with 34 additions and 1 deletions

Binary file not shown.

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2016 Rocky Bernstein
# Copyright (c) 2016-2017 Rocky Bernstein
# Copyright (c) 2000-2002 by hartmut Goebel <hartmut@goebel.noris.de>
# Copyright (c) 1999 John Aycock
@@ -14,6 +14,17 @@ class Python23Parser(Python24Parser):
def p_misc23(self, args):
'''
# Python 2.4 only adds something like the below for if 1:
# However we will just treat it as a noop (which of course messes up
# simple verify of bytecode.
# See also below in reduce_is_invalid where we check that the JUMP_FORWARD
# target matches the COME_FROM target
stmt ::= if1_stmt
if1_stmt ::= JUMP_FORWARD JUMP_IF_FALSE THEN POP_TOP COME_FROM
stmts
JUMP_FORWARD COME_FROM POP_TOP COME_FROM
# Used to keep semantic positions the same across later versions
# of Python
_while1test ::= SETUP_LOOP JUMP_FORWARD JUMP_IF_FALSE POP_TOP COME_FROM
@@ -33,6 +44,23 @@ class Python23Parser(Python24Parser):
lc_body ::= LOAD_FAST expr LIST_APPEND
'''
def add_custom_rules(self, tokens, customize):
super(Python23Parser, self).add_custom_rules(tokens, customize)
def reduce_is_invalid(self, rule, ast, tokens, first, last):
invalid = super(Python24Parser,
self).reduce_is_invalid(rule, ast,
tokens, first, last)
if invalid:
return invalid
# FiXME: this code never gets called...
lhs = rule[0]
if lhs == 'nop_stmt':
return not int(tokens[first].pattr) == tokens[last].offset
return False
class Python23ParserSingle(Python23Parser, PythonParserSingle):
pass

View File

@@ -218,6 +218,11 @@ class SourceWalker(GenericASTTraversal, object):
'importlist2': ( '%C', (0, maxint, ', ') ),
})
if version <= 2.4:
if version == 2.3:
TABLE_DIRECT.update({
'if1_stmt': ( '%|if 1\n%+%c%-', 5 )
})
global NAME_MODULE
NAME_MODULE = AST('stmt',
[ AST('assign',