Commit 0d7af347 authored by Stefan Behnel's avatar Stefan Behnel

move JoinedStrNode cleanup into ConstantFolding transform as that's essentially what it does

parent 6f43f6d7
...@@ -3,6 +3,7 @@ from __future__ import absolute_import ...@@ -3,6 +3,7 @@ from __future__ import absolute_import
import sys import sys
import copy import copy
import codecs import codecs
import itertools
from . import TypeSlots from . import TypeSlots
from .ExprNodes import not_a_constant from .ExprNodes import not_a_constant
...@@ -3951,6 +3952,35 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations): ...@@ -3951,6 +3952,35 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
sequence_node.mult_factor = factor sequence_node.mult_factor = factor
return sequence_node return sequence_node
def visit_JoinedStrNode(self, node):
"""
Clean up after the parser by discarding empty Unicode strings and merging
substring sequences. Empty or single-value join lists are not uncommon
because f-string format specs are always parsed into JoinedStrNodes.
"""
self.visitchildren(node)
unicode_node = ExprNodes.UnicodeNode
values = []
for is_unode_group, substrings in itertools.groupby(node.values, lambda v: isinstance(v, unicode_node)):
if is_unode_group:
substrings = list(substrings)
unode = substrings[0]
if len(substrings) > 1:
unode.value = EncodedString(u''.join(node.value for node in substrings))
# ignore empty Unicode strings
if unode.value:
values.append(unode)
else:
values.extend(substrings)
if not values:
node = ExprNodes.UnicodeNode(node.pos, value=EncodedString(''))
elif len(values) == 1:
node = values[0]
else:
node.values = values
return node
def visit_MergedDictNode(self, node): def visit_MergedDictNode(self, node):
"""Unpack **args in place if we can.""" """Unpack **args in place if we can."""
self.visitchildren(node) self.visitchildren(node)
......
...@@ -7,7 +7,6 @@ cython.declare(PyrexTypes=object, Naming=object, ExprNodes=object, Nodes=object, ...@@ -7,7 +7,6 @@ cython.declare(PyrexTypes=object, Naming=object, ExprNodes=object, Nodes=object,
error=object, warning=object, copy=object, _unicode=object) error=object, warning=object, copy=object, _unicode=object)
import copy import copy
import itertools
from . import PyrexTypes from . import PyrexTypes
from . import Naming from . import Naming
...@@ -349,35 +348,6 @@ class PostParse(ScopeTrackingTransform): ...@@ -349,35 +348,6 @@ class PostParse(ScopeTrackingTransform):
self.visitchildren(node) self.visitchildren(node)
return node return node
def visit_JoinedStrNode(self, node):
"""
Clean up after the parser by discarding empty Unicode strings and merging
substring sequences. Empty or single-value join lists are not uncommon
because f-string format specs are always parsed into JoinedStrNodes.
"""
self.visitchildren(node)
unicode_node = ExprNodes.UnicodeNode
values = []
for is_unode_group, substrings in itertools.groupby(node.values, lambda v: isinstance(v, unicode_node)):
if is_unode_group:
substrings = list(substrings)
unode = substrings[0]
if len(substrings) > 1:
unode.value = EncodedString(u''.join(node.value for node in substrings))
# ignore empty Unicode strings
if unode.value:
values.append(unode)
else:
values.extend(substrings)
if not values:
node = ExprNodes.UnicodeNode(node.pos, value=EncodedString(''))
elif len(values) == 1:
node = values[0]
else:
node.values = values
return node
def eliminate_rhs_duplicates(expr_list_list, ref_node_sequence): def eliminate_rhs_duplicates(expr_list_list, ref_node_sequence):
"""Replace rhs items by LetRefNodes if they appear more than once. """Replace rhs items by LetRefNodes if they appear more than once.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment