git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Allow string literal object accessors
commitad5f482b5997722277bb0d89703ba53cefb2a288 authorPhil Freeman <paf31@cantab.net> dateSat 5 Apr 2014, 15:00 (13 years ago) parent2fe0a83
3 files changed+12−2
examples/passing/Objects.purs+4 src/Language/PureScript/CodeGen/JS.hs+7−1 src/Language/PureScript/Parser/Declarations.hs+1−1
examples/passing/Objects.purs+4
@@ -14,5 +14,9 @@ module Main where
1414
1515 typed :: { foo :: Number }
1616 typed = { foo: 0 }
17+
18+ test2 = \x -> x."!@#"
19+
20+ test3 = typed."foo"
1721
1822 main = Debug.Trace.trace "Done"
src/Language/PureScript/CodeGen/JS.hs+7−1
@@ -22,6 +22,7 @@ module Language.PureScript.CodeGen.JS (
2222 wrapExportsContainer
2323) where
2424
25+import Data.Char (isAlpha, isAlphaNum)
2526import Data.Maybe (fromMaybe, mapMaybe)
2627import Data.Function (on)
2728
@@ -132,7 +133,8 @@ valueToJs opts m e (ObjectUpdate o ps) = extendObj (valueToJs opts m e o) (map (
132133valueToJs _ m _ (Constructor name) = qualifiedToJS m (Ident . runProperName) name
133134valueToJs opts m e (Case values binders) = bindersToJs opts m e binders (map (valueToJs opts m e) values)
134135valueToJs opts m e (IfThenElse cond th el) = JSConditional (valueToJs opts m e cond) (valueToJs opts m e th) (valueToJs opts m e el)
135−valueToJs opts m e (Accessor prop val) = JSAccessor prop (valueToJs opts m e val)
136+valueToJs opts m e (Accessor prop val) | isIdent prop = JSAccessor prop (valueToJs opts m e val)
137+ | otherwise = JSIndexer (JSStringLiteral prop) (valueToJs opts m e val)
136138valueToJs opts m e (App val arg) = JSApp (valueToJs opts m e val) [valueToJs opts m e arg]
137139valueToJs opts m e (Let ds val) = JSApp (JSFunction Nothing [] (JSBlock (concat (mapMaybe (flip (declToJs opts m) e) ds) ++ [JSReturn $ valueToJs opts m e val]))) []
138140valueToJs opts m e (Abs (Left arg) val) = JSFunction Nothing [identToJs arg] (JSBlock [JSReturn (valueToJs opts m (bindName m arg e) val)])
@@ -143,6 +145,10 @@ valueToJs opts m e (PositionedValue _ val) = valueToJs opts m e val
143145valueToJs _ _ _ (TypeClassDictionary _ _) = error "Type class dictionary was not replaced"
144146valueToJs _ _ _ _ = error "Invalid argument to valueToJs"
145147
148+isIdent :: String -> Bool
149+isIdent (first : rest) | isAlpha first && all isAlphaNum rest = True
150+isIdent _ = False
151+
146152-- |
147153-- Shallow copy an object.
148154--
src/Language/PureScript/Parser/Declarations.hs+1−1
@@ -297,7 +297,7 @@ parsePropertyUpdate = do
297297
298298parseAccessor :: Value -> P.Parsec String ParseState Value
299299parseAccessor (Constructor _) = P.unexpected "constructor"
300−parseAccessor obj = P.try $ Accessor <$> (C.indented *> C.dot *> P.notFollowedBy C.opLetter *> C.indented *> C.identifier) <*> pure obj
300+parseAccessor obj = P.try $ Accessor <$> (C.indented *> C.dot *> P.notFollowedBy C.opLetter *> C.indented *> (C.identifier <|> C.stringLiteral)) <*> pure obj
301301
302302parseDo :: P.Parsec String ParseState Value
303303parseDo = do