Skip to content

Commit

Permalink
Add tests and fix up one thing.
Browse files Browse the repository at this point in the history
  • Loading branch information
cdepillabout committed Dec 21, 2019
1 parent 44156ec commit 0b95aec
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
22 changes: 11 additions & 11 deletions src/Text/Pretty/Simple.hs
Expand Up @@ -542,10 +542,10 @@ pStringOpt outputOptions =
--
-- __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__
--
Expand All @@ -557,19 +557,19 @@ pStringOpt outputOptions =
--
-- __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__
Expand All @@ -582,16 +582,16 @@ pStringOpt outputOptions =
-- } 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 =
-- [ 10
-- , 11
-- ]
-- , barList =
-- [ Foo 1.1 ""
-- , Foo 2.2 "hello"
-- [ Foo 1.1 "" 'a'
-- , Foo 2.2 "hello" 'b'
-- ]
-- }
--
Expand Down
25 changes: 23 additions & 2 deletions src/Text/Pretty/Simple/Internal/ExprParser.hs
Expand Up @@ -43,7 +43,10 @@ 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
--
Expand Down Expand Up @@ -71,6 +74,15 @@ parseCSep end s@(c:cs)
(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)
Expand All @@ -79,6 +91,15 @@ parseStringLit ('\\':c:cs) = ('\\':c:cs', rest)
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)
Expand Down Expand Up @@ -141,7 +162,7 @@ parseOther = go False
-> (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')
Expand Down

0 comments on commit 0b95aec

Please sign in to comment.