From 79ceb7962fd326e412946d2b68b478bcd6b11ab5 Mon Sep 17 00:00:00 2001 From: mrkkrp Date: Thu, 15 Oct 2015 15:12:28 +0600 Subject: [PATCH] eliminate indentation in error messages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Indented text returned by ‘showMessages’ may be undesirable, but we cannot add indentation outside of the function (edge case: strings including newline are displayed in the messages). --- Text/Megaparsec/Char.hs | 4 ++-- Text/Megaparsec/Error.hs | 4 ++-- Text/Megaparsec/Prim.hs | 16 ++++++++-------- old-tests/Bugs/Bug6.hs | 2 +- old-tests/Bugs/Bug9.hs | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Text/Megaparsec/Char.hs b/Text/Megaparsec/Char.hs index 5db9bec..b8b324a 100644 --- a/Text/Megaparsec/Char.hs +++ b/Text/Megaparsec/Char.hs @@ -257,8 +257,8 @@ char c = satisfy (== c) showToken c -- 'E' -- >>> parseTest (char' 'e') "G" -- 1:1: --- unexpected 'G' --- expecting 'E' or 'e' +-- unexpected 'G' +-- expecting 'E' or 'e' char' :: MonadParsec s m Char => Char -> m Char char' = choice . fmap char . extendi . pure diff --git a/Text/Megaparsec/Error.hs b/Text/Megaparsec/Error.hs index 068e931..9ebf81e 100644 --- a/Text/Megaparsec/Error.hs +++ b/Text/Megaparsec/Error.hs @@ -169,12 +169,12 @@ mergeError e1@(ParseError pos1 _) e2@(ParseError pos2 ms2) = -- their textual representation. showMessages :: [Message] -> String -showMessages [] = " unknown parse error" +showMessages [] = "unknown parse error" showMessages ms = tail $ foldMap (fromMaybe "") (zipWith f ns rs) where (unexpected, ms') = span ((== 0) . fromEnum) ms (expected, messages) = span ((== 1) . fromEnum) ms' f prefix m = (prefix ++) <$> m - ns = ["\n unexpected ","\n expecting ","\n "] + ns = ["\nunexpected ","\nexpecting ","\n"] rs = renderMsgs <$> [unexpected, expected, messages] -- | Render collection of messages. If the collection is empty, return diff --git a/Text/Megaparsec/Prim.hs b/Text/Megaparsec/Prim.hs index abc1fa9..18ae5c3 100644 --- a/Text/Megaparsec/Prim.hs +++ b/Text/Megaparsec/Prim.hs @@ -140,15 +140,15 @@ data Reply s a = Ok a !(State s) | Error ParseError -- -- >>> parseTest (many (char 'r') <* eof) "ra" -- 1:2: --- unexpected 'a' --- expecting end of input +-- unexpected 'a' +-- expecting end of input -- -- We're getting better error messages with help of hints: -- -- >>> parseTest (many (char 'r') <* eof) "ra" -- 1:2: --- unexpected 'a' --- expecting 'r' or end of input +-- unexpected 'a' +-- expecting 'r' or end of input newtype Hints = Hints [[String]] deriving Monoid @@ -385,8 +385,8 @@ class (A.Alternative m, Monad m, Stream s t) -- -- >>> parseTest (string "let" <|> string "lexical") "lexical" -- 1:1: - -- unexpected "lex" - -- expecting "let" + -- unexpected "lex" + -- expecting "let" -- -- What happens here? First parser consumes “le” and fails (because it -- doesn't see a “t”). The second parser, however, isn't tried, since the @@ -401,8 +401,8 @@ class (A.Alternative m, Monad m, Stream s t) -- -- >>> parseTest (try (string "let") <|> string "lexical") "le" -- 1:1: - -- unexpected "le" - -- expecting "let" or "lexical" + -- unexpected "le" + -- expecting "let" or "lexical" try :: m a -> m a diff --git a/old-tests/Bugs/Bug6.hs b/old-tests/Bugs/Bug6.hs index 8a6b947..fc8f736 100644 --- a/old-tests/Bugs/Bug6.hs +++ b/old-tests/Bugs/Bug6.hs @@ -13,7 +13,7 @@ import Util main :: Test main = testCase "Look-ahead preserving error location (#6)" $ - parseErrors variable "return" @?= [" 'return' is a reserved keyword"] + parseErrors variable "return" @?= ["'return' is a reserved keyword"] variable :: Parser String variable = do diff --git a/old-tests/Bugs/Bug9.hs b/old-tests/Bugs/Bug9.hs index 7282e83..6f2b607 100644 --- a/old-tests/Bugs/Bug9.hs +++ b/old-tests/Bugs/Bug9.hs @@ -24,7 +24,7 @@ data Expr = Const Integer | Op Expr Expr deriving Show main :: Test main = testCase "Tracing of current position in error message (#9)" - $ result @?= [" unexpected '>'", " expecting end of input or operator"] + $ result @?= ["unexpected '>'", "expecting end of input or operator"] where result :: [String] result = parseErrors parseTopLevel "4 >> 5"