git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #400 from garyb/prettyprinter-escaping
2d68e03Phil Freeman
src/Language/PureScript/Parser/Common.hs+8−2
@@ -53,6 +53,12 @@ reservedPsNames = [ "data"
53 , "where"
53 , "where"
54 ]
54 ]
55
55
56+-- |
57+-- The characters allowed for use in operators
58+--
59+opChars :: [Char]
60+opChars = ":!#$%&*+./<=>?@\\^|-~"
61+
56-- |
62-- |
57-- A list of reserved identifiers for types
63-- A list of reserved identifiers for types
58--
64--
@@ -82,13 +88,13 @@ identLetter = P.alphaNum <|> P.oneOf "_'"
82-- Valid first characters for an operator
88-- Valid first characters for an operator
83--
89--
84opStart :: P.Parsec String u Char
90opStart :: P.Parsec String u Char
85−opStart = P.oneOf ":!#$%&*+./<=>?@\\^|-~"
91+opStart = P.oneOf opChars
86
92
87-- |
93-- |
88-- Valid operators characters
94-- Valid operators characters
89--
95--
90opLetter :: P.Parsec String u Char
96opLetter :: P.Parsec String u Char
91−opLetter = P.oneOf ":!#$%&*+./<=>?@\\^|-~"
97+opLetter = P.oneOf opChars
92
98
93-- |
99-- |
94-- The PureScript language definition
100-- The PureScript language definition
src/Language/PureScript/Pretty/Common.hs+9
@@ -17,6 +17,7 @@ module Language.PureScript.Pretty.Common where
17
17
18import Control.Monad.State
18import Control.Monad.State
19import Data.List (intercalate)
19import Data.List (intercalate)
20+import Language.PureScript.Parser.Common (reservedPsNames, opChars)
20
21
21-- |
22-- |
22-- Wrap a string in parentheses
23-- Wrap a string in parentheses
@@ -58,3 +59,11 @@ prettyPrintMany f xs = do
58 ss <- mapM f xs
59 ss <- mapM f xs
59 indentString <- currentIndent
60 indentString <- currentIndent
60 return $ intercalate "\n" $ map (indentString ++) ss
61 return $ intercalate "\n" $ map (indentString ++) ss
62+
63+-- |
64+-- Prints an object key, escaping reserved names.
65+--
66+prettyPrintObjectKey :: String -> String
67+prettyPrintObjectKey s | s `elem` reservedPsNames = show s
68+ | head s `elem` opChars = show s
69+ | otherwise = s
src/Language/PureScript/Pretty/Types.hs+1−1
@@ -51,7 +51,7 @@ prettyPrintRow :: Type -> String
51prettyPrintRow = (\(tys, rest) -> intercalate ", " (map (uncurry nameAndTypeToPs) tys) ++ tailToPs rest) . toList []
51prettyPrintRow = (\(tys, rest) -> intercalate ", " (map (uncurry nameAndTypeToPs) tys) ++ tailToPs rest) . toList []
52 where
52 where
53 nameAndTypeToPs :: String -> Type -> String
53 nameAndTypeToPs :: String -> Type -> String
54− nameAndTypeToPs name ty = name ++ " :: " ++ prettyPrintType ty
54+ nameAndTypeToPs name ty = prettyPrintObjectKey name ++ " :: " ++ prettyPrintType ty
55 tailToPs :: Type -> String
55 tailToPs :: Type -> String
56 tailToPs REmpty = ""
56 tailToPs REmpty = ""
57 tailToPs other = " | " ++ prettyPrintType other
57 tailToPs other = " | " ++ prettyPrintType other
src/Language/PureScript/Pretty/Values.hs+2−2
@@ -215,12 +215,12 @@ matchConsBinder = mkPattern match'
215
215
216prettyPrintObjectPropertyBinder :: (String, Binder) -> StateT PrinterState Maybe String
216prettyPrintObjectPropertyBinder :: (String, Binder) -> StateT PrinterState Maybe String
217prettyPrintObjectPropertyBinder (key, binder) = fmap concat $ sequence
217prettyPrintObjectPropertyBinder (key, binder) = fmap concat $ sequence
218− [ return $ key ++ ": "
218+ [ return $ prettyPrintObjectKey key ++ ": "
219 , prettyPrintBinder' binder
219 , prettyPrintBinder' binder
220 ]
220 ]
221
221
222prettyPrintObjectProperty :: (String, Value) -> StateT PrinterState Maybe String
222prettyPrintObjectProperty :: (String, Value) -> StateT PrinterState Maybe String
223prettyPrintObjectProperty (key, value) = fmap concat $ sequence
223prettyPrintObjectProperty (key, value) = fmap concat $ sequence
224− [ return $ key ++ ": "
224+ [ return $ prettyPrintObjectKey key ++ ": "
225 , prettyPrintValue' value
225 , prettyPrintValue' value
226 ]
226 ]