Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions arpeggio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
DEFAULT_WS = '\t\n\r '
NOMATCH_MARKER = 0


class ShortestChoiceList(list):
pass

class ArpeggioError(Exception):
"""
Base class for arpeggio errors.
Expand Down Expand Up @@ -403,7 +405,37 @@ def _parse(self, parser):
parser._nm_raise(self, c_pos, parser)

return result

class ShortestChoice(Sequence):
def _parse(self, parser):
match = False
c_pos = parser.position
shortest_result = None
shortest_result_str = None
shortest_result_cpos = None
for e in self.nodes:
try:
result = e.parse(parser)
if result is not None:
result = [result]
result_str = "".join([x.flat_str() for x in flatten(result)])
if (not shortest_result) or (len(shortest_result_str) > len(result_str)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be done more optimal. Flattening and converting to string of all resulting subtrees is slow.
It is enough to use position and track in which branch you have smaller advance.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will make a patch for this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make a unit test also if you can. I am trying to keep test coverage as high as possible. Thanks.

shortest_result = result
shortest_result_str = result_str
shortest_result_cpos = parser.position
match = True
parser.position = c_pos
except NoMatch as m:
parser.position = c_pos # Backtracking


if not match:
parser._nm_raise(self, c_pos, parser)

parser.position = shortest_result_cpos
return shortest_result

pass

class Repetition(ParsingExpression):
"""
Expand Down Expand Up @@ -1701,8 +1733,10 @@ def inner_from_python(expression):
if any((isinstance(x, CrossRef) for x in retval.nodes)):
__for_resolving.append(retval)

elif type(expression) in [list, tuple]:
if type(expression) is list:
elif type(expression) in [list, tuple, ShortestChoiceList]:
if type(expression) is ShortestChoiceList:
retval = ShortestChoice(expression)
elif type(expression) is list:
retval = OrderedChoice(expression)
else:
retval = Sequence(expression)
Expand Down