packages feed

show-prettyprint (empty) → 0.1.0.0

raw patch · 5 files changed

+191/−0 lines, 5 filesdep +basedep +doctestdep +trifectasetup-changed

Dependencies added: base, doctest, trifecta, wl-pprint

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2016++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Author name here nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ show-prettyprint.cabal view
@@ -0,0 +1,36 @@+name:                show-prettyprint+version:             0.1.0.0+synopsis:            Robust prettyprinter for output of auto-generated Show+                     instances+description:         See README.md+homepage:            https://github.com/quchen/show-prettyprint#readme+license:             BSD3+license-file:        LICENSE+author:              David Luposchainsky <dluposchainsky (λ) google>+maintainer:          David Luposchainsky <dluposchainsky (λ) google>+copyright:           David Luposchainsky, 2016+category:            User Interfaces, Text+build-type:          Simple+-- extra-source-files:+cabal-version:       >=1.10++library+  hs-source-dirs:      src+  exposed-modules:     Text.Show.Prettyprint+  build-depends:       base >= 4.7 && < 5+                     , trifecta >= 1.6+                     , wl-pprint >= 1.1+  default-language:    Haskell2010++test-suite doctest+  type:                exitcode-stdio-1.0+  hs-source-dirs:      test/Doctest+  main-is:             Main.hs+  build-depends:       base+                     , doctest >= 0.9+  ghc-options:         -Wall -threaded -rtsopts -with-rtsopts=-N+  default-language:    Haskell2010++source-repository head+  type:     git+  location: https://github.com/quchen/show-prettyprint
+ src/Text/Show/Prettyprint.hs view
@@ -0,0 +1,117 @@+{-# LANGUAGE LambdaCase        #-}+{-# LANGUAGE OverloadedStrings #-}++-- | Format a 'show'-generated string to make it nicer to read.+--+-- >>> :{+-- (putStrLn . prettyShow . Data.Map.fromList)+--     [("hello", Just True), ("world", Nothing), ("!", Just False)]+-- :}+-- fromList [("!",Just False)+--          ,("hello",Just True)+--          ,("world",Nothing)]+--+-- See the readme for some more examples.+module Text.Show.Prettyprint (+    prettifyShow,+    prettyShow,++    -- * Diagnostic functions+    prettifyShowErr,+    prettyShowErr,+) where++++import Control.Applicative+import Data.Monoid+import Text.PrettyPrint.Leijen as Ppr hiding ((<>))+import Text.Trifecta           as Tri++++-- | Prettyprint a string produced by 'show'. On parse error, silently fall back+-- to a non-prettyprinted version.+prettifyShow :: String -> String+prettifyShow s = case parseString conP mempty s of+    Success x -> show x+    Failure _ -> s++-- | 'prettifyShow' with the 'show' baked in.+prettyShow :: Show a => a -> String+prettyShow = prettifyShow . show++-- | Attempt to prettify a string produced by 'show'. Report error information+-- on failure.+prettifyShowErr :: String -> String+prettifyShowErr s = case parseString conP mempty s of+    Success x -> show x+    Failure ErrInfo{ _errDoc = e } -> "ERROR " <> show e++-- | 'prettifyShowErr' with the 'show' baked in.+prettyShowErr :: Show a => a -> String+prettyShowErr = prettifyShowErr . show++++conP :: Parser Doc+conP = do+    thing <- (token . choice)+        [ word+        , number+        , fmap (dquotes . Ppr.string) stringLiteral ]+    args <- many argP+    pure (if null args+        then thing+        else thing <+> align (sep args) )++word :: Parser Doc+word = variable <|> constructor++number :: Parser Doc+number = p <?> "number"+  where+    p = integerOrDouble >>= \case+        Left i -> pure (Ppr.integer i)+        Right d -> pure (Ppr.double d)++identifierStartingWith :: CharParsing f => f Char -> f Doc+identifierStartingWith x = liftA2 (\c cs -> Ppr.string (c:cs)) (x <|> Tri.char '_') (many (alphaNum <|> oneOf "'_"))++variable :: Parser Doc+variable = identifierStartingWith lower <?> "variable"++constructor :: Parser Doc+constructor = identifierStartingWith upper <?> "constructor"++argP :: Parser Doc+argP = (token . choice) [unitP, tupleP, listP, recordP, conP]++unitP :: Parser Doc+unitP = p <?> "()"+  where+    p = fmap Ppr.string (Tri.string "()")++tupleP :: Parser Doc+tupleP = p <?> "tuple"+  where+    p = fmap (encloseSep lparen rparen Ppr.comma) (Tri.parens (do+        x <- argP+        xs <- many (Tri.comma *> argP)+        pure (x:xs) ))++listP :: Parser Doc+listP = p <?> "list"+  where+    p = fmap (encloseSep lbracket rbracket Ppr.comma)+             (Tri.brackets (sepBy argP Tri.comma))++recordP :: Parser Doc+recordP = p <?> "{...}"+  where+    p = fmap (encloseSep lbrace rbrace Ppr.comma) (Tri.braces (sepBy recordEntryP Tri.comma))+    recordEntryP = do+        lhs <- token word+        _ <- token (Tri.char '=')+        rhs <- argP+        pure (lhs <+> Ppr.string "=" <+> rhs)
+ test/Doctest/Main.hs view
@@ -0,0 +1,6 @@+module Main (main) where++import Test.DocTest++main :: IO ()+main = doctest ["src"]