grammar and semantics for dict comp with "if"

Fixes #162
This commit is contained in:
rocky
2018-03-05 07:52:02 -05:00
parent fac5d31f34
commit 02b1554da3
5 changed files with 14 additions and 3 deletions

Binary file not shown.

View File

@@ -0,0 +1,6 @@
# Issue #162
def x(s):
return {k: v
for (k, v) in s
if not k.startswith('_')
}

View File

@@ -74,6 +74,11 @@ class Python3Parser(PythonParser):
comp_iter JUMP_BACK RETURN_VALUE RETURN_LAST
dict_comp ::= LOAD_DICTCOMP LOAD_CONST MAKE_FUNCTION_0 expr
GET_ITER CALL_FUNCTION_1
comp_iter ::= comp_if
comp_iter ::= comp_if_not
comp_if_not ::= expr jmp_true comp_iter
comp_iter ::= comp_body
"""
def p_grammar(self, args):

View File

@@ -184,7 +184,7 @@ TABLE_DIRECT = {
'comp_iter': ( '%c', 0 ),
'comp_if': ( ' if %c%c', 0, 2 ),
'comp_ifnot': ( ' if not %p%c', (0, 22), 2 ),
'comp_if_not': ( ' if not %p%c', (0, 22), 2 ),
'comp_body': ( '', ), # ignore when recusing
'set_comp_body': ( '%c', 0 ),
'gen_comp_body': ( '%c', 0 ),

View File

@@ -1327,8 +1327,8 @@ class SourceWalker(GenericASTTraversal, object):
if n[2] == 'store':
store = n[2]
n = n[3]
elif n in ('list_if', 'list_if_not', 'comp_if', 'comp_ifnot'):
have_not = n in ('list_if_not', 'comp_ifnot')
elif n in ('list_if', 'list_if_not', 'comp_if', 'comp_if_not'):
have_not = n in ('list_if_not', 'comp_if_not')
if_node = n[0]
if n[1] == 'store':
store = n[1]