pretty-simple 3.1.1.0 → 3.2.0.0
raw patch · 8 files changed
+72/−15 lines, 8 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Text.Pretty.Simple.Internal.Expr: CharLit :: !String -> Expr
+ Text.Pretty.Simple.Internal.ExprParser: parseCharLit :: String -> (String, String)
+ Text.Pretty.Simple.Internal.Output: OutputCharLit :: !String -> OutputType
Files
- CHANGELOG.md +7/−1
- pretty-simple.cabal +1/−1
- src/Text/Pretty/Simple.hs +11/−11
- src/Text/Pretty/Simple/Internal/Expr.hs +1/−0
- src/Text/Pretty/Simple/Internal/ExprParser.hs +34/−2
- src/Text/Pretty/Simple/Internal/ExprToOutput.hs +4/−0
- src/Text/Pretty/Simple/Internal/Output.hs +2/−0
- src/Text/Pretty/Simple/Internal/OutputPrinter.hs +12/−0
CHANGELOG.md view
@@ -1,9 +1,15 @@ +## 3.2.0.0++* Add support for pretty-printing Haskell character literals.+ [#57](https://github.com/cdepillabout/pretty-simple/pull/57)+ Thanks again [sjakobi](https://github.com/sjakobi)!+ ## 3.1.1.0 * Added a `pPrintString` function for pretty-printing a `String` that is the output of `show`. Implemented in- [54](https://github.com/cdepillabout/pretty-simple/pull/54). Thanks+ [#54](https://github.com/cdepillabout/pretty-simple/pull/54). Thanks [sureyeaah](https://github.com/sureyeaah)! * Fix build on GHC-7.10.3. [#55](https://github.com/cdepillabout/pretty-simple/pull/55). Thanks
pretty-simple.cabal view
@@ -1,5 +1,5 @@ name: pretty-simple-version: 3.1.1.0+version: 3.2.0.0 synopsis: pretty printer for data types with a 'Show' instance. description: Please see <https://github.com/cdepillabout/pretty-simple#readme README.md>. homepage: https://github.com/cdepillabout/pretty-simple
src/Text/Pretty/Simple.hs view
@@ -542,10 +542,10 @@ -- -- __Simple Haskell data type__ ----- >>> data Foo a = Foo a String deriving Show+-- >>> data Foo a = Foo a String Char deriving Show ----- >>> pPrint $ Foo 3 "hello"--- Foo 3 "hello"+-- >>> pPrint $ Foo 3 "hello" 'a'+-- Foo 3 "hello" 'a' -- -- __List__ --@@ -557,19 +557,19 @@ -- -- __Slightly more complicated list__ ----- >>> pPrint $ [ Foo [ (), () ] "hello" ]+-- >>> pPrint $ [ Foo [ (), () ] "hello" 'b' ] -- [ Foo -- [ () -- , ()--- ] "hello"+-- ] "hello" 'b' -- ] ----- >>> pPrint $ [ Foo [ "bar", "baz" ] "hello", Foo [] "bye" ]+-- >>> pPrint $ [ Foo [ "bar", "baz" ] "hello" 'a', Foo [] "bye" 'b' ] -- [ Foo -- [ "bar" -- , "baz"--- ] "hello"--- , Foo [] "bye"+-- ] "hello" 'a'+-- , Foo [] "bye" 'b' -- ] -- -- __Record__@@ -582,7 +582,7 @@ -- } deriving Show -- :} ----- >>> pPrint $ Bar 1 [10, 11] [Foo 1.1 "", Foo 2.2 "hello"]+-- >>> pPrint $ Bar 1 [10, 11] [Foo 1.1 "" 'a', Foo 2.2 "hello" 'b'] -- Bar -- { barInt = 1 -- , barA =@@ -590,8 +590,8 @@ -- , 11 -- ] -- , barList =--- [ Foo 1.1 ""--- , Foo 2.2 "hello"+-- [ Foo 1.1 "" 'a'+-- , Foo 2.2 "hello" 'b' -- ] -- } --
src/Text/Pretty/Simple/Internal/Expr.hs view
@@ -36,6 +36,7 @@ | Braces !(CommaSeparated [Expr]) | Parens !(CommaSeparated [Expr]) | StringLit !String+ | CharLit !String | NumberLit !String -- ^ We could store this as a 'Rational', say, instead of a 'String'. -- However, we will never need to use its value for anything. Indeed, the
src/Text/Pretty/Simple/Internal/ExprParser.hs view
@@ -39,15 +39,21 @@ parseExpr ('[':rest) = first (Brackets . CommaSeparated) $ parseCSep ']' rest parseExpr ('{':rest) = first (Braces . CommaSeparated) $ parseCSep '}' rest parseExpr ('"':rest) = first StringLit $ parseStringLit rest+parseExpr ('\'':rest) = first CharLit $ parseCharLit rest parseExpr (c:rest) | isDigit c = first NumberLit $ parseNumberLit c rest parseExpr other = first Other $ parseOther other --- |+-- | Parse multiple expressions. --+-- >>> parseExprs "Just 'a'"+-- ([Other "Just ",CharLit "a"],"")+-- -- Handle escaped characters correctly -- -- >>> parseExprs $ "Foo \"hello \\\"world!\"" -- ([Other "Foo ",StringLit "hello \\\"world!"],"")+-- >>> parseExprs $ "'\\''"+-- ([CharLit "\\'"],"") parseExprs :: String -> ([Expr], String) parseExprs [] = ([], "") parseExprs s@(c:_)@@ -68,6 +74,15 @@ (toParse, rest) = parseCSep end rest' in (parsed : toParse, rest) +-- | Parse string literals until a trailing double quote.+--+-- >>> parseStringLit "foobar\" baz"+-- ("foobar"," baz")+--+-- Keep literal back slashes:+--+-- >>> parseStringLit "foobar\\\" baz\" after"+-- ("foobar\\\" baz"," after") parseStringLit :: String -> (String, String) parseStringLit [] = ("", "") parseStringLit ('"':rest) = ("", rest)@@ -76,6 +91,23 @@ parseStringLit (c:cs) = (c:cs', rest) where (cs', rest) = parseStringLit cs +-- | Parse character literals until a trailing single quote.+--+-- >>> parseCharLit "a' foobar"+-- ("a"," foobar")+--+-- Keep literal back slashes:+--+-- >>> parseCharLit "\\'' hello"+-- ("\\'"," hello")+parseCharLit :: String -> (String, String)+parseCharLit [] = ("", "")+parseCharLit ('\'':rest) = ("", rest)+parseCharLit ('\\':c:cs) = ('\\':c:cs', rest)+ where (cs', rest) = parseCharLit cs+parseCharLit (c:cs) = (c:cs', rest)+ where (cs', rest) = parseCharLit cs+ -- | Parses integers and reals, like @123@ and @45.67@. -- -- To be more precise, any numbers matching the regex @\\d+(\\.\\d+)?@ should@@ -130,7 +162,7 @@ -> (String, String) go _ [] = ("", "") go insideIdent cs@(c:cs')- | c `elem` ("{[()]}\"," :: String) = ("", cs)+ | c `elem` ("{[()]}\"'," :: String) = ("", cs) | isDigit c && not insideIdent = ("", cs) | insideIdent = first (c :) (go (isIdentRest c) cs') | otherwise = first (c :) (go (isIdentBegin c) cs')
src/Text/Pretty/Simple/Internal/ExprToOutput.hs view
@@ -227,6 +227,10 @@ nest <- gets nestLevel when (nest < 0) $ addToNestLevel 1 addOutputs [OutputStringLit string, OutputOther " "]+putExpression (CharLit string) = do+ nest <- gets nestLevel+ when (nest < 0) $ addToNestLevel 1+ addOutputs [OutputCharLit string, OutputOther " "] putExpression (NumberLit integer) = do nest <- gets nestLevel when (nest < 0) $ addToNestLevel 1
src/Text/Pretty/Simple/Internal/Output.hs view
@@ -70,6 +70,8 @@ -- of the other tokens. | OutputStringLit !String -- ^ This represents a string literal. For instance, @\"foobar\"@.+ | OutputCharLit !String+ -- ^ This represents a char literal. For example, @'x'@ or @'\b'@ | OutputNumberLit !String -- ^ This represents a numeric literal. For example, @12345@ or @3.14159@. deriving (Data, Eq, Generic, Read, Show, Typeable)
src/Text/Pretty/Simple/Internal/OutputPrinter.hs view
@@ -184,6 +184,18 @@ readStr :: String -> String readStr s = fromMaybe s . readMaybe $ '"':s ++ "\""+renderOutput (Output _ (OutputCharLit string)) = do+ sequenceFold+ [ useColorQuote+ , pure "'"+ , useColorReset+ , useColorString+ , pure (fromString string)+ , useColorReset+ , useColorQuote+ , pure "'"+ , useColorReset+ ] -- | Replace non-printable characters with hex escape sequences. --