@@ -19,7 +19,7 @@ class ParseError(Exception):
19
19
def __init__ (self , input , position , message , trail = None ):
20
20
Exception .__init__ (self , position , message )
21
21
self .position = position
22
- self .error = message
22
+ self .error = message or []
23
23
self .input = input
24
24
self .trail = trail or []
25
25
@@ -30,7 +30,7 @@ def __eq__(self, other):
30
30
31
31
32
32
def formatReason (self ):
33
- if self .error is None :
33
+ if not self .error :
34
34
return "Syntax error"
35
35
if len (self .error ) == 1 :
36
36
if self .error [0 ][0 ] == 'message' :
@@ -118,29 +118,30 @@ def eof():
118
118
return [("message" , "end of input" )]
119
119
120
120
121
- def joinErrors (errors , presorted = False ):
121
+ def joinErrors (errors ):
122
122
"""
123
123
Return the error from the branch that matched the most of the input.
124
124
"""
125
125
if len (errors ) == 1 :
126
126
return errors [0 ]
127
127
128
- if not presorted :
129
- errors .sort (reverse = True , key = operator .itemgetter (0 ))
130
-
128
+ highestPos = - 1
131
129
results = set ()
132
- pos = errors [0 ].position
133
130
trail = None
134
- for err in errors :
135
- if pos != err .position :
136
- break
137
131
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 )
142
143
143
- return ParseError (errors [0 ].input , pos , list (results ) or None , trail )
144
+ return ParseError (errors [0 ].input , highestPos , list (results ), trail )
144
145
145
146
146
147
class character (str ):
@@ -393,7 +394,13 @@ def considerError(self, error, typ=None):
393
394
if newPos > curPos :
394
395
self .currentError = error
395
396
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 )
397
404
398
405
399
406
def _trace (self , src , span , inputPos ):
0 commit comments