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:
mrkkrp 2015-08-24 01:45:12 +06:00
parent b81962e44a
commit 45f449ddb2
2 changed files with 7 additions and 4 deletions

View File

@ -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.

View File

@ -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