From 45f449ddb2190f5cec1c22f06a535abda2047491 Mon Sep 17 00:00:00 2001 From: mrkkrp Date: Mon, 24 Aug 2015 01:45:12 +0600 Subject: [PATCH] =?UTF-8?q?prefer=20longest=20match=20in=20=E2=80=98mergeE?= =?UTF-8?q?rror=E2=80=99,=20fixes=20#23?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since ‘mergeError’ is only used to merge errors from alternative branches of parsing, longest match should be preferred. --- Text/Megaparsec/Error.hs | 9 ++++++--- tests/Error.hs | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Text/Megaparsec/Error.hs b/Text/Megaparsec/Error.hs index 4484fea..5c0a969 100644 --- a/Text/Megaparsec/Error.hs +++ b/Text/Megaparsec/Error.hs @@ -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. diff --git a/tests/Error.hs b/tests/Error.hs index ac49b6f..267ace2 100644 --- a/tests/Error.hs +++ b/tests/Error.hs @@ -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