mirror of
https://github.com/mrkkrp/megaparsec.git
synced 2024-11-23 19:38:05 +03:00
eliminate indentation in error messages
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).
This commit is contained in:
parent
69eabcca37
commit
79ceb7962f
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user