diff --git a/src/Text/Pretty/Simple/Internal/Expr.hs b/src/Text/Pretty/Simple/Internal/Expr.hs index 5fb3b6f..acc828e 100644 --- a/src/Text/Pretty/Simple/Internal/Expr.hs +++ b/src/Text/Pretty/Simple/Internal/Expr.hs @@ -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 diff --git a/src/Text/Pretty/Simple/Internal/ExprParser.hs b/src/Text/Pretty/Simple/Internal/ExprParser.hs index af7e5e0..71faaaf 100644 --- a/src/Text/Pretty/Simple/Internal/ExprParser.hs +++ b/src/Text/Pretty/Simple/Internal/ExprParser.hs @@ -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 @@ -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:_) @@ -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 diff --git a/src/Text/Pretty/Simple/Internal/ExprToOutput.hs b/src/Text/Pretty/Simple/Internal/ExprToOutput.hs index 859df9c..194708b 100644 --- a/src/Text/Pretty/Simple/Internal/ExprToOutput.hs +++ b/src/Text/Pretty/Simple/Internal/ExprToOutput.hs @@ -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 diff --git a/src/Text/Pretty/Simple/Internal/Output.hs b/src/Text/Pretty/Simple/Internal/Output.hs index f38ade8..20cea3a 100644 --- a/src/Text/Pretty/Simple/Internal/Output.hs +++ b/src/Text/Pretty/Simple/Internal/Output.hs @@ -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) diff --git a/src/Text/Pretty/Simple/Internal/OutputPrinter.hs b/src/Text/Pretty/Simple/Internal/OutputPrinter.hs index 36137b1..73b72d1 100644 --- a/src/Text/Pretty/Simple/Internal/OutputPrinter.hs +++ b/src/Text/Pretty/Simple/Internal/OutputPrinter.hs @@ -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. --