Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
tokens: Iterator,
opening: str = OPENING_BRACES,
closing: str = CLOSING_BRACES,
delimiter: Union[str, None] = None,
) -> list:
g = [] # type: List[Any]
if delimiter:
g.append(delimiter)
g.append(next(tokens))
while True:
try:
token = next(tokens)
if token == closing and not delimiter:
if len(g):
break
raise EmptyGroupError
elif token == opening:
try:
g.append(group(tokens))
except EmptyGroupError:
g += [[]]
elif token == LEFT:
g.append(group(tokens, delimiter=token))
elif token == RIGHT:
g.append(token)
g.append(next(tokens))
break
else:
g.append(token)
except StopIteration:
break
if delimiter:
aggregated += [previous, token]
return
try:
next_token = next_item_or_group(tokens)
if len(aggregated) >= 2:
if aggregated[-2] == '_' and token == '^':
aggregated[-2] = '_^'
aggregated += [previous, next_token]
elif aggregated[-2] == '^' and token == '_':
aggregated[-2] = '_^'
aggregated += [next_token, previous]
else:
aggregated += [token, previous, next_token]
else:
aggregated += [token, previous, next_token]
except EmptyGroupError:
aggregated += [previous, token, '{', '}']
except StopIteration:
return
except IndexError:
aggregated.append(token)