git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix and clean up string literal rendering
6461a24Andy Arvanitis
src/CodeGen/Cpp/Printer.hs+20−11
@@ -29,7 +29,7 @@ import Language.PureScript.Comments
29import Language.PureScript.Crash
29import Language.PureScript.Crash
30import Language.PureScript.Names (Ident)
30import Language.PureScript.Names (Ident)
31import Language.PureScript.Pretty.Common
31import Language.PureScript.Pretty.Common
32−import Language.PureScript.PSString (PSString, decodeString, mkString, prettyPrintString)
32+import Language.PureScript.PSString (PSString, decodeString, mkString)
33import CodeGen.Cpp.Common
33import CodeGen.Cpp.Common
34
34
35import qualified Language.PureScript.Constants as C
35import qualified Language.PureScript.Constants as C
@@ -45,13 +45,9 @@ literals = mkPattern' match'
45 match :: (Emit gen) => AST -> StateT PrinterState Maybe gen
45 match :: (Emit gen) => AST -> StateT PrinterState Maybe gen
46 match (NumericLiteral _ n) = return $ emit $ T.pack $ either show show n
46 match (NumericLiteral _ n) = return $ emit $ T.pack $ either show show n
47 match (StringLiteral _ s)
47 match (StringLiteral _ s)
48− | Just s' <- decodeString s = return $ emit $
48+ | Just s' <- decodeString s
49− if T.all isAscii s'
50− then stringLiteral s'
51− else "u8" <> stringLiteral s'
49+ , T.all isAscii s' = return $ emit $ stringLiteral s
52− match (StringLiteral _ s)
50+ match (StringLiteral _ s) = return $ emit $ "u8" <> stringLiteral s
53− | Just s' <- decodeString s = return $ emit $ "u8" <> stringLiteral s'
54− match (StringLiteral _ s) = error $ "Bad character in string literal " ++ show s
55 match (BooleanLiteral _ True) = return $ emit "true"
51 match (BooleanLiteral _ True) = return $ emit "true"
56 match (BooleanLiteral _ False) = return $ emit "false"
52 match (BooleanLiteral _ False) = return $ emit "false"
57 -- match (ArrayLiteral _ []) = return $ emit "std::initializer_list<boxed>{}"
53 -- match (ArrayLiteral _ []) = return $ emit "std::initializer_list<boxed>{}"
@@ -75,7 +71,7 @@ literals = mkPattern' match'
75 ]
71 ]
76 where
72 where
77 objectPropertyToString :: (Emit gen) => PSString -> gen
73 objectPropertyToString :: (Emit gen) => PSString -> gen
78− objectPropertyToString s = emit $ prettyPrintString s <> ", "
74+ objectPropertyToString s = emit $ stringLiteral s <> ", "
79 match (Block _ sts) = mconcat <$> sequence
75 match (Block _ sts) = mconcat <$> sequence
80 [ return $ emit "{\n"
76 [ return $ emit "{\n"
81 , withIndent $ prettyStatements sts
77 , withIndent $ prettyStatements sts
@@ -381,8 +377,21 @@ prettyPrintCpp' = A.runKleisli $ runPattern matchValue
381prettyPrintCpp1 :: AST -> Text
377prettyPrintCpp1 :: AST -> Text
382prettyPrintCpp1 = maybe (internalError "Incomplete pattern") runPlainString . flip evalStateT (PrinterState 0) . prettyPrintCpp'
378prettyPrintCpp1 = maybe (internalError "Incomplete pattern") runPlainString . flip evalStateT (PrinterState 0) . prettyPrintCpp'
383
379
384−stringLiteral :: Text -> Text
380+stringLiteral :: PSString -> Text
381+stringLiteral pss | Just s <- decodeString pss = stringLiteral' s
382+ where
383+ stringLiteral' :: Text -> Text
385−stringLiteral s = "\"" <> (T.replace "\\" "\\\\" s) <> "\""
384+ stringLiteral' s = "\"" <> T.concatMap encodeChar s <> "\""
385+ encodeChar :: Char -> Text
386+ encodeChar '\b' = "\\b"
387+ encodeChar '\t' = "\\t"
388+ encodeChar '\n' = "\\n"
389+ encodeChar '\v' = "\\v"
390+ encodeChar '\f' = "\\f"
391+ encodeChar '\r' = "\\r"
392+ encodeChar '"' = "\\\""
393+ encodeChar '\\' = "\\\\"
394+ encodeChar c = T.singleton $ c
386
395
387unbox :: Text -> AST -> Text
396unbox :: Text -> AST -> Text
388unbox _ v@(NumericLiteral{}) = prettyPrintCpp1 v
397unbox _ v@(NumericLiteral{}) = prettyPrintCpp1 v