Skip to content

Commit

Permalink
Add Text.Megaparsec.Error.errorBundlePrettyForGhcPreProcessors
Browse files Browse the repository at this point in the history
  • Loading branch information
sol authored and mrkkrp committed Nov 19, 2024
1 parent 8d1f5cc commit c0b7856
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* `many` and `some` of the `Alternative` instance of `ParsecT` are now more
efficient, since they use the monadic implementations under the hood.
[Issue 567](https://github.com/mrkkrp/megaparsec/issues/567).
* Add `Text.Megaparsec.Error.errorBundlePrettyForGhcPreProcessors`. [PR
573](https://github.com/mrkkrp/megaparsec/pull/573).

## Megaparsec 9.6.1

Expand Down
86 changes: 79 additions & 7 deletions Text/Megaparsec/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ module Text.Megaparsec.Error
-- * Pretty-printing
ShowErrorComponent (..),
errorBundlePretty,
errorBundlePrettyForGhcPreProcessors,
errorBundlePrettyWith,
parseErrorPretty,
parseErrorTextPretty,
showErrorItem,
)
where

import Control.Arrow ((>>>))
import Control.DeepSeq
import Control.Exception
import Control.Monad.State.Strict
Expand Down Expand Up @@ -349,24 +352,25 @@ instance ShowErrorComponent Void where
showErrorComponent = absurd

-- | Pretty-print a 'ParseErrorBundle'. All 'ParseError's in the bundle will
-- be pretty-printed in order together with the corresponding offending
-- lines by doing a single pass over the input stream. The rendered 'String'
-- always ends with a newline.
-- be pretty-printed in order, by applying a provided format function, with
-- a single pass over the input stream.
--
-- @since 7.0.0
errorBundlePretty ::
-- @since 9.7.0
errorBundlePrettyWith ::
forall s e.
( VisualStream s,

Check failure on line 361 in Text/Megaparsec/Error.hs

View workflow job for this annotation

GitHub Actions / build (3.10, 9.8.2)

Redundant constraint: ShowErrorComponent e

Check failure on line 361 in Text/Megaparsec/Error.hs

View workflow job for this annotation

GitHub Actions / build (3.10, 9.10.1)

Redundant constraint: ShowErrorComponent e
TraversableStream s,
ShowErrorComponent e
) =>
-- | Format function for a single 'ParseError'
(Maybe String -> SourcePos -> ParseError s e -> String) ->
-- | Parse error bundle to display
ParseErrorBundle s e ->
-- | Textual rendition of the bundle
String
errorBundlePretty ParseErrorBundle {..} =
errorBundlePrettyWith format ParseErrorBundle {..} =
let (r, _) = foldl f (id, bundlePosState) bundleErrors
in drop 1 (r "")
in r ""
where
f ::
(ShowS, PosState s) ->
Expand All @@ -376,6 +380,36 @@ errorBundlePretty ParseErrorBundle {..} =
where
(msline, pst') = reachOffset (errorOffset e) pst
epos = pstateSourcePos pst'
outChunk = format msline epos e

-- | Pretty-print a 'ParseErrorBundle'. All 'ParseError's in the bundle will
-- be pretty-printed in order together with the corresponding offending
-- lines by doing a single pass over the input stream. The rendered 'String'
-- always ends with a newline.
--
-- @since 7.0.0
errorBundlePretty ::
forall s e.
( VisualStream s,
TraversableStream s,
ShowErrorComponent e
) =>
-- | Parse error bundle to display
ParseErrorBundle s e ->
-- | Textual rendition of the bundle
String
errorBundlePretty = drop 1 . errorBundlePrettyWith format
where
format ::
( VisualStream s,

Check failure on line 404 in Text/Megaparsec/Error.hs

View workflow job for this annotation

GitHub Actions / build (3.10, 9.8.2)

• Redundant constraints: (VisualStream s, ShowErrorComponent e)

Check failure on line 404 in Text/Megaparsec/Error.hs

View workflow job for this annotation

GitHub Actions / build (3.10, 9.10.1)

• Redundant constraints: (VisualStream s, ShowErrorComponent e)
ShowErrorComponent e
) =>
Maybe String ->
SourcePos ->
ParseError s e ->
String
format msline epos e = outChunk
where
outChunk =
"\n"
<> sourcePosPretty epos
Expand Down Expand Up @@ -418,6 +452,44 @@ errorBundlePretty ParseErrorBundle {..} =
FancyError _ xs ->
E.foldl' (\a b -> max a (errorFancyLength b)) 1 xs

-- | Pretty-print a 'ParseErrorBundle'. All 'ParseError's in the bundle will
-- be pretty-printed in order by doing a single pass over the input stream.
--
-- The rendered format is suitable for custom GHC pre-processors (as can be
-- specified with -F -pgmF).
--
-- @since 9.7.0
errorBundlePrettyForGhcPreProcessors ::
forall s e.
( VisualStream s,
TraversableStream s,
ShowErrorComponent e
) =>
-- | Parse error bundle to display
ParseErrorBundle s e ->
-- | Textual rendition of the bundle
String
errorBundlePrettyForGhcPreProcessors = errorBundlePrettyWith format
where
format ::
( VisualStream s,

Check failure on line 475 in Text/Megaparsec/Error.hs

View workflow job for this annotation

GitHub Actions / build (3.10, 9.8.2)

• Redundant constraints: (VisualStream s, ShowErrorComponent e)

Check failure on line 475 in Text/Megaparsec/Error.hs

View workflow job for this annotation

GitHub Actions / build (3.10, 9.10.1)

• Redundant constraints: (VisualStream s, ShowErrorComponent e)
ShowErrorComponent e
) =>
Maybe String ->
SourcePos ->
ParseError s e ->
String
format _msline epos e =
sourcePosPretty epos
<> ":"
<> indent (parseErrorTextPretty e)

indent :: String -> String
indent =
lines >>> \case
[err] -> err
err -> intercalate "\n" $ map (" " <>) err

-- | Pretty-print a 'ParseError'. The rendered 'String' always ends with a
-- newline.
--
Expand Down

0 comments on commit c0b7856

Please sign in to comment.