git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #329 from garyb/unicode-in-strings
642d0f2Phil Freeman
src/Language/PureScript/Pretty/JS.hs+9−1
@@ -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
30+import Numeric
30
31
31newtype PrinterState = PrinterState { indent :: Int } deriving (Show, Eq, Ord)
32newtype PrinterState = PrinterState { indent :: Int } deriving (Show, Eq, Ord)
32
33
@@ -59,7 +60,7 @@ literals = mkPattern' match
59 where
60 where
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
142
143
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) ""
149+ encodeChar c = [c]
150+
143conditional :: Pattern PrinterState JS ((JS, JS), JS)
151conditional :: Pattern PrinterState JS ((JS, JS), JS)
144conditional = mkPattern match
152conditional = mkPattern match
145 where
153 where