Remove empty literals when concatenating regexes
This commit is contained in:
parent
8e052ddd97
commit
46aab39ee6
12
regex.py
12
regex.py
|
@ -81,7 +81,11 @@ def concat(*elements):
|
||||||
|
|
||||||
combined = []
|
combined = []
|
||||||
for element in flattened:
|
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
|
# Combine two literals next to each other
|
||||||
# into one literal
|
# into one literal
|
||||||
previous = combined.pop()
|
previous = combined.pop()
|
||||||
|
@ -90,7 +94,11 @@ def concat(*elements):
|
||||||
else:
|
else:
|
||||||
combined.append(element)
|
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
|
element, = combined
|
||||||
return element
|
return element
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue