Skip to content

Commit

Permalink
Fix handling of Char literals
Browse files Browse the repository at this point in the history
  • Loading branch information
sjakobi committed Dec 12, 2019
1 parent 8930d26 commit 44156ec
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Text/Pretty/Simple/Internal/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ data Expr
| 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
Expand Down
11 changes: 11 additions & 0 deletions src/Text/Pretty/Simple/Internal/ExprParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ parseExpr ('(':rest) = first (Parens . CommaSeparated) $ parseCSep ')' rest
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

Expand All @@ -48,6 +49,8 @@ parseExpr other = first Other $ parseOther other
--
-- >>> parseExprs $ "Foo \"hello \\\"world!\""
-- ([Other "Foo ",StringLit "hello \\\"world!"],"")
-- >>> parseExprs $ "'\\''"
-- ([CharLit "\\'"],"")
parseExprs :: String -> ([Expr], String)
parseExprs [] = ([], "")
parseExprs s@(c:_)
Expand Down Expand Up @@ -76,6 +79,14 @@ parseStringLit ('\\':c:cs) = ('\\':c:cs', rest)
parseStringLit (c:cs) = (c:cs', rest)
where (cs', rest) = parseStringLit cs

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
Expand Down
4 changes: 4 additions & 0 deletions src/Text/Pretty/Simple/Internal/ExprToOutput.hs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ putExpression (StringLit string) = do
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
Expand Down
2 changes: 2 additions & 0 deletions src/Text/Pretty/Simple/Internal/Output.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ data OutputType
-- 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)
Expand Down
12 changes: 12 additions & 0 deletions src/Text/Pretty/Simple/Internal/OutputPrinter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ renderOutput (Output _ (OutputStringLit string)) = do

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.
--
Expand Down

0 comments on commit 44156ec

Please sign in to comment.