Python 2.5 "with"; Isolate 2.5-2.6 grammar better

This commit is contained in:
rocky
2017-11-16 09:26:23 -05:00
parent 953cf312db
commit 53beae8ee6
4 changed files with 21 additions and 1 deletions

Binary file not shown.

View File

@@ -1,3 +1,5 @@
from __future__ import with_statement
with (sys) as f:
with open(__file__, 'r') as f:
print(f)
with f:
pass

View File

@@ -21,8 +21,13 @@ class Python25Parser(Python26Parser):
# Python 2.6 uses ROT_TWO instead of the STORE_xxx
# withas is allowed as a "from future" in 2.5
# 2.6 and 2.7 do something slightly different
setupwithas ::= DUP_TOP LOAD_ATTR store LOAD_ATTR CALL_FUNCTION_0
setup_finally
# opcode SETUP_WITH
setupwith ::= DUP_TOP LOAD_ATTR STORE_NAME LOAD_ATTR CALL_FUNCTION_0 POP_TOP
withstmt ::= expr setupwith SETUP_FINALLY suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM with_cleanup
store ::= STORE_FAST
store ::= STORE_NAME
@@ -41,6 +46,14 @@ class Python25Parser(Python26Parser):
"""
def add_custom_rules(self, tokens, customize):
# grammar rules inherited from Python 2.6
self.remove_rules("""
setupwith ::= DUP_TOP LOAD_ATTR ROT_TWO LOAD_ATTR CALL_FUNCTION_0 POP_TOP
withstmt ::= expr setupwith SETUP_FINALLY suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM WITH_CLEANUP END_FINALLY
withasstmt ::= expr setupwithas designator suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM WITH_CLEANUP END_FINALLY
""")
super(Python25Parser, self).add_custom_rules(tokens, customize)
if self.version == 2.5:
self.check_reduce['tryelsestmt'] = 'tokens'

View File

@@ -251,6 +251,11 @@ class Python26Parser(Python2Parser):
"""
def add_custom_rules(self, tokens, customize):
self.remove_rules("""
withasstmt ::= expr SETUP_WITH designator suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_WITH
WITH_CLEANUP END_FINALLY
""")
super(Python26Parser, self).add_custom_rules(tokens, customize)
self.check_reduce['and'] = 'AST'