From 718b0f358a3e5430766ab34169744b76bb1376d2 Mon Sep 17 00:00:00 2001 From: mrkkrp Date: Sat, 2 Jan 2016 20:19:40 +0600 Subject: [PATCH] Don't use hints with custom error messages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Close #75. Now accumulated hints are not used with ‘ParseError’ records that have only custom messages in them (created with ‘Message’ constructor, as opposed to ‘Unexpected’ or ‘Expected’). This strips “expected” line from custom error messages where it's unlikely to be relevant anyway. --- Text/Megaparsec/Prim.hs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Text/Megaparsec/Prim.hs b/Text/Megaparsec/Prim.hs index 7c8968a..37170a7 100644 --- a/Text/Megaparsec/Prim.hs +++ b/Text/Megaparsec/Prim.hs @@ -136,9 +136,18 @@ toHints err = Hints hints msgs = filter ((== 1) . fromEnum) $ errorMessages err -- | @withHints hs c@ makes “error” continuation @c@ use given hints @hs@. +-- +-- Note that if resulting continuation gets 'ParseError' where all messages +-- are created with 'Message' constructor, hints are ignored. withHints :: Hints -> (ParseError -> m b) -> ParseError -> m b -withHints (Hints xs) c = c . addErrorMessages (Expected <$> concat xs) +withHints (Hints xs) c e = + let isMessage (Message _) = True + isMessage _ = False + in (if all isMessage (errorMessages e) + then c + else c . addErrorMessages (Expected <$> concat xs)) + e -- | @accHints hs c@ results in “OK” continuation that will add given hints -- @hs@ to third argument of original continuation @c@.