dictcomp_func -> dict_comp_func...

to match AST better. Also adds a correction in last commit,
including set_comp -> set_comp_expr where apprpriate

Note: can't use dict_comp as that was already used.
But dict_comp_func is matches AST better than dictcomp_func
This commit is contained in:
rocky
2018-03-05 11:10:09 -05:00
parent 2bdfd76635
commit 9f66694056
6 changed files with 30 additions and 19 deletions

View File

@@ -308,9 +308,9 @@ class Python2Parser(PythonParser):
], customize)
if self.version >= 2.7:
self.add_unique_rule(
'dictcomp_func ::= BUILD_MAP_n LOAD_FAST FOR_ITER store '
'dict_comp_func ::= BUILD_MAP_n LOAD_FAST FOR_ITER store '
'comp_iter JUMP_BACK RETURN_VALUE RETURN_LAST',
'dictcomp_func', 0, customize)
'dict_comp_func', 0, customize)
else:
kvlist_n = "kvlist_%s" % token.attr

View File

@@ -26,9 +26,9 @@ class Python27Parser(Python2Parser):
expr ::= dict_comp
dict_comp ::= LOAD_DICTCOMP MAKE_FUNCTION_0 expr GET_ITER CALL_FUNCTION_1
stmt ::= dictcomp_func
dictcomp_func ::= BUILD_MAP_0 LOAD_FAST FOR_ITER store
comp_iter JUMP_BACK RETURN_VALUE RETURN_LAST
stmt ::= dict_comp_func
dict_comp_func ::= BUILD_MAP_0 LOAD_FAST FOR_ITER store
comp_iter JUMP_BACK RETURN_VALUE RETURN_LAST
set_comp ::= BUILD_SET_0 LOAD_FAST FOR_ITER store comp_iter
JUMP_BACK RETURN_VALUE RETURN_LAST

View File

@@ -3,7 +3,18 @@
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
# Copyright (c) 1999 John Aycock
#
# See LICENSE for license
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
A spark grammar for Python 3.x.
@@ -66,14 +77,14 @@ class Python3Parser(PythonParser):
# See also common Python p_list_comprehension
"""
def p_dictcomp3(self, args):
def p_dict_comp3(self, args):
""""
expr ::= dict_comp
stmt ::= dictcomp_func
dictcomp_func ::= BUILD_MAP_0 LOAD_FAST FOR_ITER store
comp_iter JUMP_BACK RETURN_VALUE RETURN_LAST
dict_comp ::= LOAD_DICTCOMP LOAD_CONST MAKE_FUNCTION_0 expr
GET_ITER CALL_FUNCTION_1
stmt ::= dict_comp_func
dict_comp_func ::= BUILD_MAP_0 LOAD_FAST FOR_ITER store
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
@@ -621,9 +632,9 @@ class Python3Parser(PythonParser):
kvlist_n = "kvlist_%s" % token.attr
if opname == 'BUILD_MAP_n':
# PyPy sometimes has no count. Sigh.
rule = ('dictcomp_func ::= BUILD_MAP_n LOAD_FAST FOR_ITER store '
rule = ('dict_comp_func ::= BUILD_MAP_n LOAD_FAST FOR_ITER store '
'comp_iter JUMP_BACK RETURN_VALUE RETURN_LAST')
self.add_unique_rule(rule, 'dictomp_func', 1, customize)
self.add_unique_rule(rule, 'dict_comp_func', 1, customize)
kvlist_n = 'kvlist_n'
rule = 'kvlist_n ::= kvlist_n kv3'

View File

@@ -323,6 +323,7 @@ PRECEDENCE = {
'unary_convert': 0,
'dict_comp': 0,
'set_comp': 0,
'set_comp_expr': 0,
'list_comp': 0,
'generator_exp': 0,

View File

@@ -849,7 +849,7 @@ class FragmentsWalker(pysource.SourceWalker, object):
self.set_pos_info(node, start, len(self.f.getvalue()))
self.prune()
def n_set_comp(self, node):
def n_set_comp_expr(self, node):
start = len(self.f.getvalue())
self.write('{')
if node[0] in ['LOAD_SETCOMP', 'LOAD_DICTCOMP']:

View File

@@ -1262,7 +1262,7 @@ class SourceWalker(GenericASTTraversal, object):
self.write(')')
self.prune()
def n_set_comp(self, node):
def n_set_comp_expr(self, node):
self.write('{')
if node[0] in ['LOAD_SETCOMP', 'LOAD_DICTCOMP']:
self.comprehension_walk3(node, 1, 0)
@@ -1272,6 +1272,7 @@ class SourceWalker(GenericASTTraversal, object):
self.comprehension_walk(node, iter_index=4)
self.write('}')
self.prune()
n_dict_comp = n_set_comp_expr
def comprehension_walk3(self, node, iter_index, code_index=-5):
"""Non-closure-based comprehensions the way they are done in Python3.
@@ -1296,7 +1297,7 @@ class SourceWalker(GenericASTTraversal, object):
ast = ast[0]
store = None
if ast in ['set_comp', 'dictcomp_func']:
if ast in ['set_comp', 'dict_comp_func']:
for k in ast:
if k == 'comp_iter':
n = k
@@ -1470,8 +1471,6 @@ class SourceWalker(GenericASTTraversal, object):
self.write(']')
self.prune()
n_dict_comp = n_set_comp
def setcomprehension_walk3(self, node, collection_index):
"""Set comprehensions the way they are done in Python3.
They're more other comprehensions, e.g. set comprehensions