Less ambigouus 2.x grammar rule for BUILD_MAP

This commit is contained in:
rocky 2018-03-25 12:09:42 -04:00
parent 2ae9cd7d08
commit 7fb94176b1
3 changed files with 15 additions and 12 deletions

View File

@ -330,11 +330,9 @@ class Python2Parser(PythonParser):
'dict_comp_func', 0, customize)
else:
kvlist_n = "kvlist_%s" % token.attr
self.add_unique_rules([
(kvlist_n + " ::=" + ' kv3' * token.attr),
"dict ::= %s %s" % (opname, kvlist_n)
], customize)
kvlist_n = ' kv3' * token.attr
rule = "dict ::= %s%s" % (opname, kvlist_n)
self.addRule(rule, nop_func)
continue
elif opname_base == 'BUILD_SLICE':
slice_num = token.attr

View File

@ -646,6 +646,8 @@ class Python3Parser(PythonParser):
elif self.version >= 3.5:
if opname != 'BUILD_MAP_WITH_CALL':
if opname == 'BUILD_MAP_UNPACK':
# FIXME: start here
# rule = "%s ::= %s %s" % (kvlist_n, 'expr ' * (token.attr*2), opname)
rule = kvlist_n + ' ::= ' + 'expr ' * (token.attr*2)
self.add_unique_rule(rule, opname, token.attr, customize)
rule = 'dict_entry ::= ' + 'expr ' * (token.attr*2)

View File

@ -1705,10 +1705,6 @@ class SourceWalker(GenericASTTraversal, object):
kv_node = node[0]
l = list(kv_node)
length = len(l)
# FIXME: Parser-speed improved grammars will have BUILD_MAP
# at the end. So in the future when everything is
# complete, we can do an "assert" instead of "if".
if kv_node[-1].kind.startswith("BUILD_MAP"):
length -= 1
i = 0
@ -1784,13 +1780,20 @@ class SourceWalker(GenericASTTraversal, object):
pass
pass
else:
# Python 2 style kvlist
assert node[-1].kind.startswith('kvlist')
kv_node = node[-1] # goto kvlist
# Python 2 style kvlist. Find beginning of kvlist.
if node[0].kind.startswith("BUILD_MAP"):
if len(node) > 1 and node[1].kind in ('kvlist', 'kvlist_n'):
kv_node = node[1]
else:
kv_node = node[1:]
else:
assert node[-1].kind.startswith('kvlist')
kv_node = node[-1]
first_time = True
for kv in kv_node:
assert kv in ('kv', 'kv2', 'kv3')
# kv ::= DUP_TOP expr ROT_TWO expr STORE_SUBSCR
# kv2 ::= DUP_TOP expr expr ROT_THREE STORE_SUBSCR
# kv3 ::= expr expr STORE_MAP