nicify (empty) → 0.9
raw patch · 5 files changed
+154/−0 lines, 5 filesdep +basedep +nicifydep +parsecsetup-changed
Dependencies added: base, nicify, parsec, transformers
Files
- LICENSE +18/−0
- Setup.hs +4/−0
- nicify.cabal +31/−0
- src/Text/Nicify.hs +98/−0
- src/nicify.hs +3/−0
+ LICENSE view
@@ -0,0 +1,18 @@+Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the "Software"),+to deal in the Software without restriction, including without limitation+the rights to use, copy, modify, merge, publish, distribute, sublicense,+and/or sell copies of the Software, and to permit persons to whom the+Software is furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER+DEALINGS IN THE SOFTWARE.+
+ Setup.hs view
@@ -0,0 +1,4 @@+import Distribution.Simple++main = defaultMain+
+ nicify.cabal view
@@ -0,0 +1,31 @@+Name: nicify+Version: 0.9+Synopsis: Pretty print the standard output of show for algebraic datatypes+Description: Pretty print the standard output of show for algebraic datatypes+ +License: MIT+License-File: LICENSE+Author: Julian Fleischer <julian.fleischer@fu-berlin.de>+Maintainer: Julian Fleischer <julian.fleischer@fu-berlin.de>+Build-Type: Simple+Cabal-Version: >= 1.8+Category: Data+Stability: stable++Library+ Exposed-Modules: Text.Nicify+ Build-Depends: base >= 4 && < 5+ , parsec >= 3+ , transformers >= 0.3+ Hs-Source-Dirs: src++Executable nicify+ Build-Depends: base >= 4 && < 5+ , nicify+ , parsec >= 3+ , transformers >= 0.3++ Hs-Source-Dirs: src+ Main-Is: nicify.hs+ +
+ src/Text/Nicify.hs view
@@ -0,0 +1,98 @@+{-# LANGUAGE Haskell2010 #-}+{-# OPTIONS+ -Wall+ -fno-warn-unused-do-bind+ -fno-warn-name-shadowing+ #-}++module Text.Nicify (+ nicify,+ X (..),+ parseX,+ printX,+ printX'+ ) where++import Data.Functor.Identity+import Text.Parsec+++data X = XString String+ | XCurly [X]+ | XBrackets [X]+ | XAnything String+ | XSep+ deriving Show+++nicify :: String -> String+nicify = printX . parseX++type Parser a = ParsecT String () Identity a++parseX :: String -> [X]+parseX = either (const []) id . runParser (many rData) () "-"++printX :: [X] -> String+printX = printX' ""++printX' :: String -> [X] -> String+printX' indent = (indent ++) . concatMap (prettify indent)++prettify :: String -> X -> String+prettify indent x = case x of+ XAnything s -> s+ XString s -> '\"' : foldr stringify "\"" s+ XSep -> ",\n" ++ indent+ XCurly [] -> "{}"+ XBrackets [] -> "[]"+ XCurly x -> "{\n" ++ indent' ++ foldr (++) ('\n' : indent ++ "}") (map (prettify indent') x)+ XBrackets x -> "[\n" ++ indent' ++ foldr (++) ('\n' : indent ++ "]") (map (prettify indent') x)+ where+ indent' = indent ++ " "+ stringify c cs = case c of+ '\"' -> "\\\"" ++ cs+ '\\' -> "\\\\" ++ cs+ char -> char : cs++rData, rSep, rAnything, rCurly, rBrackets, rString :: Parser X++rData = rString <|> rCurly <|> rBrackets <|> rSep <|> rAnything++rSep = do+ char ','+ spaces+ return XSep++rAnything = do+ str <- many1 (noneOf "\"{}[],")+ return $ XAnything str++rCurly = do+ char '{'+ str <- many rData+ char '}'+ return $ XCurly str++rBrackets = do+ char '['+ str <- many rData+ char ']'+ return $ XBrackets str++rString = do+ char '\"'+ str <- many (noneOf "\\\"" <|> escape)+ char '\"'+ return $ XString str++escape :: Parser Char+escape = do+ char '\\'+ c <- anyChar+ case c of+ 'n' -> return '\n'+ 'r' -> return '\r'+ 't' -> return '\t'+ any -> return any+
+ src/nicify.hs view
@@ -0,0 +1,3 @@+import Text.Nicify++main = interact nicify