git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #400 from garyb/prettyprinter-escaping

Fix escaping for reserved name props in externs

commit2d68e037a5047d09edf894ac701db917879aad8a authorPhil Freeman <paf31@cantab.net> dateThu 1 May 2014, 14:14 (13 years ago) parent9d7e05b2c8abc3
4 files changed+20−5
src/Language/PureScript/Parser/Common.hs+8−2 src/Language/PureScript/Pretty/Common.hs+9 src/Language/PureScript/Pretty/Types.hs+1−1 src/Language/PureScript/Pretty/Values.hs+2−2
src/Language/PureScript/Parser/Common.hs+8−2
@@ -53,6 +53,12 @@ reservedPsNames = [ "data"
5353 , "where"
5454 ]
5555
56+-- |
57+-- The characters allowed for use in operators
58+--
59+opChars :: [Char]
60+opChars = ":!#$%&*+./<=>?@\\^|-~"
61+
5662-- |
5763-- A list of reserved identifiers for types
5864--
@@ -82,13 +88,13 @@ identLetter = P.alphaNum <|> P.oneOf "_'"
8288-- Valid first characters for an operator
8389--
8490opStart :: P.Parsec String u Char
85−opStart = P.oneOf ":!#$%&*+./<=>?@\\^|-~"
91+opStart = P.oneOf opChars
8692
8793-- |
8894-- Valid operators characters
8995--
9096opLetter :: P.Parsec String u Char
91−opLetter = P.oneOf ":!#$%&*+./<=>?@\\^|-~"
97+opLetter = P.oneOf opChars
9298
9399-- |
94100-- The PureScript language definition
src/Language/PureScript/Pretty/Common.hs+9
@@ -17,6 +17,7 @@ module Language.PureScript.Pretty.Common where
1717
1818import Control.Monad.State
1919import Data.List (intercalate)
20+import Language.PureScript.Parser.Common (reservedPsNames, opChars)
2021
2122-- |
2223-- Wrap a string in parentheses
@@ -58,3 +59,11 @@ prettyPrintMany f xs = do
5859 ss <- mapM f xs
5960 indentString <- currentIndent
6061 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
5151prettyPrintRow = (\(tys, rest) -> intercalate ", " (map (uncurry nameAndTypeToPs) tys) ++ tailToPs rest) . toList []
5252 where
5353 nameAndTypeToPs :: String -> Type -> String
54− nameAndTypeToPs name ty = name ++ " :: " ++ prettyPrintType ty
54+ nameAndTypeToPs name ty = prettyPrintObjectKey name ++ " :: " ++ prettyPrintType ty
5555 tailToPs :: Type -> String
5656 tailToPs REmpty = ""
5757 tailToPs other = " | " ++ prettyPrintType other
src/Language/PureScript/Pretty/Values.hs+2−2
@@ -215,12 +215,12 @@ matchConsBinder = mkPattern match'
215215
216216prettyPrintObjectPropertyBinder :: (String, Binder) -> StateT PrinterState Maybe String
217217prettyPrintObjectPropertyBinder (key, binder) = fmap concat $ sequence
218− [ return $ key ++ ": "
218+ [ return $ prettyPrintObjectKey key ++ ": "
219219 , prettyPrintBinder' binder
220220 ]
221221
222222prettyPrintObjectProperty :: (String, Value) -> StateT PrinterState Maybe String
223223prettyPrintObjectProperty (key, value) = fmap concat $ sequence
224− [ return $ key ++ ": "
224+ [ return $ prettyPrintObjectKey key ++ ": "
225225 , prettyPrintValue' value
226226 ]