@@ -27,6 +27,7 @@ import Control.Arrow ((<+>))
27import Control.PatternArrows
27import Control.PatternArrows
28import Control.Applicative
28import Control.Applicative
29import Control.Monad.State
29import Control.Monad.State
31newtype PrinterState = PrinterState { indent :: Int } deriving (Show, Eq, Ord)
32newtype PrinterState = PrinterState { indent :: Int } deriving (Show, Eq, Ord)
@@ -59,7 +60,7 @@ literals = mkPattern' match
60 match :: JS -> StateT PrinterState Maybe String
61 match :: JS -> StateT PrinterState Maybe String
61 match (JSNumericLiteral n) = return $ either show show n
62 match (JSNumericLiteral n) = return $ either show show n
62− match (JSStringLiteral s) = return $ show s
63+ match (JSStringLiteral s) = return $ '"' : concatMap encodeChar s ++ "\""
63 match (JSBooleanLiteral True) = return "true"
64 match (JSBooleanLiteral True) = return "true"
64 match (JSBooleanLiteral False) = return "false"
65 match (JSBooleanLiteral False) = return "false"
65 match (JSArrayLiteral xs) = fmap concat $ sequence
66 match (JSArrayLiteral xs) = fmap concat $ sequence
@@ -140,6 +141,13 @@ literals = mkPattern' match
140 match (JSRaw js) = return js
141 match (JSRaw js) = return js
141 match _ = mzero
142 match _ = mzero
144+ encodeChar :: Char -> String
145+ encodeChar '\r' = "\\r"
146+ encodeChar '\n' = "\\n"
147+ encodeChar c | fromEnum c > 0xFFF = "\\u" ++ showHex (fromEnum c) ""
148+ encodeChar c | fromEnum c > 0xFF = "\\u0" ++ showHex (fromEnum c) ""
143conditional :: Pattern PrinterState JS ((JS, JS), JS)
151conditional :: Pattern PrinterState JS ((JS, JS), JS)
144conditional = mkPattern match
152conditional = mkPattern match