localize 2 and 3 argument BUILD_SLICE...

Nontermninal name matches AST anme now. Add test.
This commit is contained in:
rocky
2017-11-25 21:07:43 -05:00
parent 3d7b160e30
commit b7003914c9
10 changed files with 53 additions and 17 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,3 +1,8 @@
ary = [1,2,3]
# Tests of Python slice operators
ary = [1,2,3,4,5]
# Forces BUILD_SLICE 2 on 3.x
ary[:2]
ary[2:]
# Forces BUILD_SLICE 3
ary[1:4:2]

View File

@@ -101,7 +101,10 @@ class PythonParser(GenericASTBuilder):
def fix(c):
s = str(c)
last_token_pos = s.find('_')
return s if last_token_pos == -1 else s[:last_token_pos]
if last_token_pos == -1:
return s
else:
return s[:last_token_pos]
prefix = ''
if parent and tokens:
@@ -134,7 +137,10 @@ class PythonParser(GenericASTBuilder):
err_token = instructions[index]
print("Instruction context:")
for i in range(start, finish):
indent = ' ' if i != index else '-> '
if i != index:
indent = ' '
else:
indent = '-> '
print("%s%s" % (indent, instructions[i]))
raise ParserError(err_token, err_token.offset)
else:
@@ -448,8 +454,6 @@ class PythonParser(GenericASTBuilder):
expr ::= binary_subscr
expr ::= binary_subscr2
expr ::= get_iter
expr ::= buildslice2
expr ::= buildslice3
expr ::= yield
binary_expr ::= expr expr binary_op
@@ -477,8 +481,6 @@ class PythonParser(GenericASTBuilder):
load_attr ::= expr LOAD_ATTR
get_iter ::= expr GET_ITER
buildslice3 ::= expr expr expr BUILD_SLICE_3
buildslice2 ::= expr expr BUILD_SLICE_2
yield ::= expr YIELD_VALUE

View File

@@ -313,6 +313,21 @@ class Python2Parser(PythonParser):
"mapexpr ::= %s %s" % (opname, kvlist_n)
], customize)
continue
elif opname_base == 'BUILD_SLICE':
slice_num = token.attr
if slice_num == 2:
self.add_unique_rules([
'expr ::= slice2',
'slice2 ::= expr expr BUILD_SLICE_2'
], customize)
else:
assert slice_num == 3, ("BUILD_SLICE value must be 2 or 3; is %s" %
slice_num)
self.add_unique_rules([
'expr ::= slice3',
'slice3 ::= expr expr expr BUILD_SLICE_3',
], customize)
continue
elif opname_base in ('CALL_FUNCTION', 'CALL_FUNCTION_VAR',
'CALL_FUNCTION_VAR_KW', 'CALL_FUNCTION_KW'):
args_pos = (v & 0xff) # positional parameters

View File

@@ -738,6 +738,18 @@ class Python3Parser(PythonParser):
self.custom_classfunc_rule(opname, token, customize,
seen_LOAD_BUILD_CLASS,
seen_GET_AWAITABLE_YIELD_FROM)
elif opname_base == 'BUILD_SLICE':
if token.attr == 2:
self.add_unique_rules([
'expr ::= slice2',
'slice2 ::= expr expr BUILD_SLICE_2'
], customize)
else:
assert token.attr == 3, "BUILD_SLICE value must be 2 or 3; is %s" % v
self.add_unique_rules([
'expr ::= slice3',
'slice3 ::= expr expr expr BUILD_SLICE_3',
], customize)
elif opname_base == 'CALL_METHOD':
# PyPy only - DRY with parse2

View File

@@ -85,7 +85,9 @@ class Scanner2(Scanner):
cause specific rules for the specific number of arguments they take.
"""
show_asm = self.show_asm if not show_asm else show_asm
if not show_asm:
show_asm = self.show_asm
# show_asm = 'after'
if show_asm in ('both', 'before'):
from xdis.bytecode import Bytecode
@@ -247,8 +249,7 @@ class Scanner2(Scanner):
op_name = 'BUILD_MAP_n'
else:
op_name = '%s_%d' % (op_name, oparg)
if op != self.opc.BUILD_SLICE:
customize[op_name] = oparg
customize[op_name] = oparg
elif self.is_pypy and op_name in ('LOOKUP_METHOD',
'JUMP_IF_NOT_DEBUG',
'SETUP_EXCEPT',
@@ -1038,8 +1039,10 @@ class Scanner2(Scanner):
# FIXME: rocky: I think we need something like this...
if offset not in set(self.ignore_if) or self.version == 2.7:
source = (self.setup_loops[label]
if label in self.setup_loops else offset)
if label in self.setup_loops:
source = self.setup_loops[label]
else:
source = offset
targets[label] = targets.get(label, []) + [source]
pass

View File

@@ -235,8 +235,7 @@ class Scanner26(scan.Scanner2):
continue
else:
op_name = '%s_%d' % (op_name, oparg)
if op != self.opc.BUILD_SLICE:
customize[op_name] = oparg
customize[op_name] = oparg
elif op == self.opc.JUMP_ABSOLUTE:
# Further classify JUMP_ABSOLUTE into backward jumps
# which are used in loops, and "CONTINUE" jumps which

View File

@@ -714,7 +714,7 @@ class SourceWalker(GenericASTTraversal, object):
assert False, "dunno about this python version"
self.prune() # stop recursing
def n_buildslice3(self, node):
def n_slice3(self, node):
p = self.prec
self.prec = 100
if not node[0].isNone():
@@ -728,7 +728,7 @@ class SourceWalker(GenericASTTraversal, object):
self.prec = p
self.prune() # stop recursing
def n_buildslice2(self, node):
def n_slice2(self, node):
p = self.prec
self.prec = 100
if not node[0].isNone():
@@ -1744,7 +1744,7 @@ class SourceWalker(GenericASTTraversal, object):
# if a tuple item is some sort of slice.
no_parens = False
for n in node:
if n == 'expr' and n[0].kind.startswith('buildslice'):
if n == 'expr' and n[0].kind.startswith('slice'):
no_parens = True
break
pass