Remove empty literals when concatenating regexes

This commit is contained in:
Juhani Krekelä 2019-05-31 14:15:28 +03:00
parent 8e052ddd97
commit 46aab39ee6
1 changed files with 10 additions and 2 deletions

View File

@ -81,7 +81,11 @@ def concat(*elements):
combined = []
for element in flattened:
if len(combined) > 0 and type(combined[-1]) == Literal and type(element) == Literal:
if type(element) == Literal and element.text == '':
# Drop empty literals
continue
elif len(combined) > 0 and type(combined[-1]) == Literal and type(element) == Literal:
# Combine two literals next to each other
# into one literal
previous = combined.pop()
@ -90,7 +94,11 @@ def concat(*elements):
else:
combined.append(element)
if len(combined) == 1:
if len(combined) == 0:
# Empty regex, represent with empty literal
return lit('')
elif len(combined) == 1:
element, = combined
return element
else: