Skip to content

Commit 4de6a86

Browse files
committed
Skip joinErrors from considerError. Avoid sorting in joinErrors.
1 parent 4223b2e commit 4de6a86

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

ometa/runtime.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ParseError(Exception):
1919
def __init__(self, input, position, message, trail=None):
2020
Exception.__init__(self, position, message)
2121
self.position = position
22-
self.error = message
22+
self.error = message or []
2323
self.input = input
2424
self.trail = trail or []
2525

@@ -30,7 +30,7 @@ def __eq__(self, other):
3030

3131

3232
def formatReason(self):
33-
if self.error is None:
33+
if not self.error:
3434
return "Syntax error"
3535
if len(self.error) == 1:
3636
if self.error[0][0] == 'message':
@@ -118,29 +118,30 @@ def eof():
118118
return [("message", "end of input")]
119119

120120

121-
def joinErrors(errors, presorted=False):
121+
def joinErrors(errors):
122122
"""
123123
Return the error from the branch that matched the most of the input.
124124
"""
125125
if len(errors) == 1:
126126
return errors[0]
127127

128-
if not presorted:
129-
errors.sort(reverse=True, key=operator.itemgetter(0))
130-
128+
highestPos = -1
131129
results = set()
132-
pos = errors[0].position
133130
trail = None
134-
for err in errors:
135-
if pos != err.position:
136-
break
137131

138-
trail = err.trail or trail
139-
e = err.error
140-
if e:
141-
results.update(e)
132+
for err in errors:
133+
pos = err.position
134+
if pos < highestPos:
135+
continue
136+
elif pos > highestPos:
137+
highestPos = pos
138+
trail = err.trail or None
139+
results = set(err.error)
140+
else:
141+
trail = err.trail or trail
142+
results.update(err.error)
142143

143-
return ParseError(errors[0].input, pos, list(results) or None, trail)
144+
return ParseError(errors[0].input, highestPos, list(results), trail)
144145

145146

146147
class character(str):
@@ -393,7 +394,13 @@ def considerError(self, error, typ=None):
393394
if newPos > curPos:
394395
self.currentError = error
395396
elif newPos == curPos:
396-
self.currentError = joinErrors([error, self.currentError], presorted=True)
397+
# Same logic as joinErrors, inlined and special-cased for two
398+
# errors at the same position
399+
self.currentError = ParseError(
400+
self.currentError.input,
401+
curPos,
402+
list(set(self.currentError.error + error.error)),
403+
error.trail or self.currentError.trail or None)
397404

398405

399406
def _trace(self, src, span, inputPos):

0 commit comments

Comments
 (0)