mirror of
https://github.com/mrkkrp/megaparsec.git
synced 2024-11-27 15:32:14 +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'
|
-- 'E'
|
||||||
-- >>> parseTest (char' 'e') "G"
|
-- >>> parseTest (char' 'e') "G"
|
||||||
-- 1:1:
|
-- 1:1:
|
||||||
-- unexpected 'G'
|
-- unexpected 'G'
|
||||||
-- expecting 'E' or 'e'
|
-- expecting 'E' or 'e'
|
||||||
|
|
||||||
char' :: MonadParsec s m Char => Char -> m Char
|
char' :: MonadParsec s m Char => Char -> m Char
|
||||||
char' = choice . fmap char . extendi . pure
|
char' = choice . fmap char . extendi . pure
|
||||||
|
@ -169,12 +169,12 @@ mergeError e1@(ParseError pos1 _) e2@(ParseError pos2 ms2) =
|
|||||||
-- their textual representation.
|
-- their textual representation.
|
||||||
|
|
||||||
showMessages :: [Message] -> String
|
showMessages :: [Message] -> String
|
||||||
showMessages [] = " unknown parse error"
|
showMessages [] = "unknown parse error"
|
||||||
showMessages ms = tail $ foldMap (fromMaybe "") (zipWith f ns rs)
|
showMessages ms = tail $ foldMap (fromMaybe "") (zipWith f ns rs)
|
||||||
where (unexpected, ms') = span ((== 0) . fromEnum) ms
|
where (unexpected, ms') = span ((== 0) . fromEnum) ms
|
||||||
(expected, messages) = span ((== 1) . fromEnum) ms'
|
(expected, messages) = span ((== 1) . fromEnum) ms'
|
||||||
f prefix m = (prefix ++) <$> m
|
f prefix m = (prefix ++) <$> m
|
||||||
ns = ["\n unexpected ","\n expecting ","\n "]
|
ns = ["\nunexpected ","\nexpecting ","\n"]
|
||||||
rs = renderMsgs <$> [unexpected, expected, messages]
|
rs = renderMsgs <$> [unexpected, expected, messages]
|
||||||
|
|
||||||
-- | Render collection of messages. If the collection is empty, return
|
-- | 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"
|
-- >>> parseTest (many (char 'r') <* eof) "ra"
|
||||||
-- 1:2:
|
-- 1:2:
|
||||||
-- unexpected 'a'
|
-- unexpected 'a'
|
||||||
-- expecting end of input
|
-- expecting end of input
|
||||||
--
|
--
|
||||||
-- We're getting better error messages with help of hints:
|
-- We're getting better error messages with help of hints:
|
||||||
--
|
--
|
||||||
-- >>> parseTest (many (char 'r') <* eof) "ra"
|
-- >>> parseTest (many (char 'r') <* eof) "ra"
|
||||||
-- 1:2:
|
-- 1:2:
|
||||||
-- unexpected 'a'
|
-- unexpected 'a'
|
||||||
-- expecting 'r' or end of input
|
-- expecting 'r' or end of input
|
||||||
|
|
||||||
newtype Hints = Hints [[String]] deriving Monoid
|
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"
|
-- >>> parseTest (string "let" <|> string "lexical") "lexical"
|
||||||
-- 1:1:
|
-- 1:1:
|
||||||
-- unexpected "lex"
|
-- unexpected "lex"
|
||||||
-- expecting "let"
|
-- expecting "let"
|
||||||
--
|
--
|
||||||
-- What happens here? First parser consumes “le” and fails (because it
|
-- 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
|
-- 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"
|
-- >>> parseTest (try (string "let") <|> string "lexical") "le"
|
||||||
-- 1:1:
|
-- 1:1:
|
||||||
-- unexpected "le"
|
-- unexpected "le"
|
||||||
-- expecting "let" or "lexical"
|
-- expecting "let" or "lexical"
|
||||||
|
|
||||||
try :: m a -> m a
|
try :: m a -> m a
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ import Util
|
|||||||
main :: Test
|
main :: Test
|
||||||
main =
|
main =
|
||||||
testCase "Look-ahead preserving error location (#6)" $
|
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 :: Parser String
|
||||||
variable = do
|
variable = do
|
||||||
|
@ -24,7 +24,7 @@ data Expr = Const Integer | Op Expr Expr deriving Show
|
|||||||
main :: Test
|
main :: Test
|
||||||
main =
|
main =
|
||||||
testCase "Tracing of current position in error message (#9)"
|
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
|
where
|
||||||
result :: [String]
|
result :: [String]
|
||||||
result = parseErrors parseTopLevel "4 >> 5"
|
result = parseErrors parseTopLevel "4 >> 5"
|
||||||
|
Loading…
Reference in New Issue
Block a user