git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Allow definition of objects with unusual property names, as well as row types.
commit6691b8af81902b762d5faca10ada6c05d4a01560 authorPhil Freeman <paf31@cantab.net> dateSat 5 Apr 2014, 15:14 (13 years ago) parentad5f482
6 files changed+23−10
examples/passing/Objects.purs+5 src/Language/PureScript/CodeGen/Common.hs+7 src/Language/PureScript/CodeGen/JS.hs+2−6 src/Language/PureScript/Parser/Declarations.hs+2−2 src/Language/PureScript/Parser/Types.hs+1−1 src/Language/PureScript/Pretty/JS.hs+6−1
examples/passing/Objects.purs+5
@@ -18,5 +18,10 @@ module Main where
1818 test2 = \x -> x."!@#"
1919
2020 test3 = typed."foo"
21+
22+ test4 = test2 weirdObj
23+ where
24+ weirdObj :: { "!@#" :: Number }
25+ weirdObj = { "!@#": 1 }
2126
2227 main = Debug.Trace.trace "Done"
src/Language/PureScript/CodeGen/Common.hs+7
@@ -130,5 +130,12 @@ nameIsJsReserved name =
130130 , "with"
131131 , "yield" ]
132132
133+-- |
134+-- Test if a string is a valid JS identifier (may return false negatives)
135+--
136+isIdent :: String -> Bool
137+isIdent s@(first : rest) | not (nameIsJsReserved s) && isAlpha first && all isAlphaNum rest = True
138+isIdent _ = False
139+
133140moduleNameToJs :: ModuleName -> String
134141moduleNameToJs (ModuleName pns) = intercalate "_" (runProperName `map` pns)
src/Language/PureScript/CodeGen/JS.hs+2−6
@@ -19,10 +19,10 @@ module Language.PureScript.CodeGen.JS (
1919 module AST,
2020 declToJs,
2121 moduleToJs,
22− wrapExportsContainer
22+ wrapExportsContainer,
23+ isIdent
2324) where
2425
25−import Data.Char (isAlpha, isAlphaNum)
2626import Data.Maybe (fromMaybe, mapMaybe)
2727import Data.Function (on)
2828
@@ -145,10 +145,6 @@ valueToJs opts m e (PositionedValue _ val) = valueToJs opts m e val
145145valueToJs _ _ _ (TypeClassDictionary _ _) = error "Type class dictionary was not replaced"
146146valueToJs _ _ _ _ = error "Invalid argument to valueToJs"
147147
148−isIdent :: String -> Bool
149−isIdent (first : rest) | isAlpha first && all isAlphaNum rest = True
150−isIdent _ = False
151−
152148-- |
153149-- Shallow copy an object.
154150--
src/Language/PureScript/Parser/Declarations.hs+2−2
@@ -227,7 +227,7 @@ parseObjectLiteral :: P.Parsec String ParseState Value
227227parseObjectLiteral = ObjectLiteral <$> C.braces (C.commaSep parseIdentifierAndValue)
228228
229229parseIdentifierAndValue :: P.Parsec String ParseState (String, Value)
230−parseIdentifierAndValue = (,) <$> (C.indented *> C.identifier <* C.indented <* C.colon)
230+parseIdentifierAndValue = (,) <$> (C.indented *> (C.identifier <|> C.stringLiteral) <* C.indented <* C.colon)
231231 <*> (C.indented *> parseValue)
232232
233233parseAbs :: P.Parsec String ParseState Value
@@ -290,7 +290,7 @@ parseValueAtom = P.choice
290290
291291parsePropertyUpdate :: P.Parsec String ParseState (String, Value)
292292parsePropertyUpdate = do
293− name <- C.lexeme C.identifier
293+ name <- C.lexeme (C.identifier <|> C.stringLiteral)
294294 _ <- C.lexeme $ C.indented *> P.char '='
295295 value <- C.indented *> parseValue
296296 return (name, value)
src/Language/PureScript/Parser/Types.hs+1−1
@@ -118,7 +118,7 @@ parsePolyType :: P.Parsec String ParseState Type
118118parsePolyType = parseAnyType
119119
120120parseNameAndType :: P.Parsec String ParseState t -> P.Parsec String ParseState (String, t)
121−parseNameAndType p = (,) <$> (indented *> identifier <* indented <* lexeme (P.string "::")) <*> p
121+parseNameAndType p = (,) <$> (indented *> (identifier <|> stringLiteral) <* indented <* lexeme (P.string "::")) <*> p
122122
123123parseRowEnding :: P.Parsec String ParseState Type
124124parseRowEnding = P.option REmpty (TypeVar <$> (lexeme (indented *> P.char '|') *> indented *> identifier))
src/Language/PureScript/Pretty/JS.hs+6−1
@@ -18,6 +18,7 @@ module Language.PureScript.Pretty.JS (
1818) where
1919
2020import Language.PureScript.Pretty.Common
21+import Language.PureScript.CodeGen.JS (isIdent)
2122import Language.PureScript.CodeGen.JS.AST
2223
2324import Data.List
@@ -46,13 +47,17 @@ literals = mkPattern' match
4647 match (JSObjectLiteral ps) = fmap concat $ sequence
4748 [ return "{\n"
4849 , withIndent $ do
49− jss <- forM ps $ \(key, value) -> fmap ((key ++ ": ") ++) . prettyPrintJS' $ value
50+ jss <- forM ps $ \(key, value) -> fmap ((objectPropertyToString key ++ ": ") ++) . prettyPrintJS' $ value
5051 indentString <- currentIndent
5152 return $ intercalate ", \n" $ map (indentString ++) jss
5253 , return "\n"
5354 , currentIndent
5455 , return "}"
5556 ]
57+ where
58+ objectPropertyToString :: String -> String
59+ objectPropertyToString s | isIdent s = s
60+ | otherwise = show s
5661 match (JSBlock sts) = fmap concat $ sequence
5762 [ return "{\n"
5863 , withIndent $ prettyStatements sts