mirror of
https://github.com/mrkkrp/megaparsec.git
synced 2024-12-24 16:51:38 +03:00
prefer longest match in ‘mergeError’, fixes #23
Since ‘mergeError’ is only used to merge errors from alternative branches of parsing, longest match should be preferred.
This commit is contained in:
parent
b81962e44a
commit
45f449ddb2
@ -138,14 +138,17 @@ setErrorPos :: SourcePos -> ParseError -> ParseError
|
||||
setErrorPos pos (ParseError _ ms) = ParseError pos ms
|
||||
|
||||
-- | Merge two error data structures into one joining their collections of
|
||||
-- messages and preferring shortest match.
|
||||
-- messages and preferring longest match. In other words earlier error
|
||||
-- message is discarded. This may seem counter-intuitive, but @mergeError@
|
||||
-- is only used to merge error messages of alternative branches of parsing
|
||||
-- and in this case longest match should be preferred.
|
||||
|
||||
mergeError :: ParseError -> ParseError -> ParseError
|
||||
mergeError e1@(ParseError pos1 _) e2@(ParseError pos2 ms2) =
|
||||
case pos1 `compare` pos2 of
|
||||
LT -> e1
|
||||
LT -> e2
|
||||
EQ -> foldr addErrorMessage e1 ms2
|
||||
GT -> e2
|
||||
GT -> e1
|
||||
|
||||
-- | @showMessages ms@ transforms list of error messages @ms@ into
|
||||
-- their textual representation.
|
||||
|
@ -107,7 +107,7 @@ prop_setErrorMessage msg err =
|
||||
unique = length (filter (== fromEnum msg) (fromEnum <$> msgs)) == 1
|
||||
|
||||
prop_mergeErrorPos :: ParseError -> ParseError -> Bool
|
||||
prop_mergeErrorPos e1 e2 = errorPos (mergeError e1 e2) == min pos1 pos2
|
||||
prop_mergeErrorPos e1 e2 = errorPos (mergeError e1 e2) == max pos1 pos2
|
||||
where pos1 = errorPos e1
|
||||
pos2 = errorPos e2
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user