git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Use indexer for JS reserved idents where possible
commit3a2e274f505dc3009f61cef75884a5c292d4b36d authorGary Burgess <garyb@users.noreply.github.com> dateSat 8 Feb 2014, 11:02 (13 years ago) parent65ecaeb
7 files changed+118−95
docs/source/prelude.rst+3−3 examples/passing/JSReserved.purs+20−7 examples/passing/Operators.purs+3 libraries/prelude/README.md+3−3 src/Language/PureScript/CodeGen/Common.hs+74−2 src/Language/PureScript/CodeGen/JS.hs+14−11 src/Language/PureScript/Parser/Common.hs+1−69
docs/source/prelude.rst+3−3
@@ -52,7 +52,7 @@ Type Classes
5252 (<$>) :: forall b. forall a. (a -> b) -> f a -> f b
5353
5454 class Monad m where
55− $return :: forall a. a -> m a
55+ return :: forall a. a -> m a
5656 (>>=) :: forall b. forall a. m a -> (a -> m b) -> m b
5757
5858 class Num a where
@@ -119,8 +119,6 @@ Values
119119
120120::
121121
122− $const :: forall b. forall a. a -> b -> a
123−
124122 (!!) :: forall a. [a] -> Prim.Number -> a
125123
126124 (#) :: forall b. forall a. a -> (a -> b) -> b
@@ -135,6 +133,8 @@ Values
135133
136134 boolOr :: Prim.Boolean -> Prim.Boolean -> Prim.Boolean
137135
136+ const :: forall b. forall a. a -> b -> a
137+
138138 flip :: forall c. forall b. forall a. (a -> b -> c) -> b -> a -> c
139139
140140 numAdd :: Prim.Number -> Prim.Number -> Prim.Number
examples/passing/JSReserved.purs+20−7
@@ -1,11 +1,11 @@
1−module Main where
1+module JSReserved where
22
33 import Prelude
44
5− yield = "test"
6− member = "test"
5+ yield = 0
6+ member = 1
77
8− fortest = {
8+ this = {
99 var function = 0;
1010 for (i <- 0 until 10) {
1111 function = function + i;
@@ -13,7 +13,7 @@ module Main where
1313 return function;
1414 }
1515
16− whiletest = {
16+ catch = {
1717 var for = 0;
1818 while (for > 0) {
1919 for = for - 1;
@@ -21,10 +21,23 @@ module Main where
2121 return for;
2222 }
2323
24− vartest = {
24+ instanceof = {
2525 var var = 1;
2626 var = var + 1;
2727 return var;
2828 }
29+
30+ public = \return -> return
31+
32+module Main where
33+
34+ import Prelude
35+ import Arrays
36+ import JSReserved
2937
30− main = Trace.trace "Done"
38+ main = Trace.print [ yield
39+ , member
40+ , this
41+ , catch
42+ , instanceof
43+ , public 1 ]
examples/passing/Operators.purs+3
@@ -50,6 +50,8 @@ module Operators2 where
5050 test11 = [1, 2, 3] ... [4, 5, 6]
5151
5252 test12 (<%>) a b = a <%> b
53+
54+ test13 = \(<%>) a b -> a <%> b
5355
5456module Main where
5557
@@ -75,4 +77,5 @@ main =
7577 , show test10
7678 , show test11
7779 , show (test12 k 1 2)
80+ , show (test13 k 1 2)
7881 ]
libraries/prelude/README.md+3−3
@@ -44,7 +44,7 @@
4444 (<$>) :: forall b. forall a. (a -> b) -> f a -> f b
4545
4646 class Monad m where
47− $return :: forall a. a -> m a
47+ return :: forall a. a -> m a
4848 (>>=) :: forall b. forall a. m a -> (a -> m b) -> m b
4949
5050 class Num a where
@@ -107,8 +107,6 @@
107107
108108### Values
109109
110− $const :: forall b. forall a. a -> b -> a
111−
112110 (!!) :: forall a. [a] -> Prim.Number -> a
113111
114112 (#) :: forall b. forall a. a -> (a -> b) -> b
@@ -123,6 +121,8 @@
123121
124122 boolOr :: Prim.Boolean -> Prim.Boolean -> Prim.Boolean
125123
124+ const :: forall b. forall a. a -> b -> a
125+
126126 flip :: forall c. forall b. forall a. (a -> b -> c) -> b -> a -> c
127127
128128 numAdd :: Prim.Number -> Prim.Number -> Prim.Number
src/Language/PureScript/CodeGen/Common.hs+74−2
@@ -21,15 +21,22 @@ import Language.PureScript.Names
2121-- |
2222-- Convert an Ident into a valid Javascript identifier:
2323--
24−-- * Alphanumeric characters are kept unmodified
24+-- * Alphanumeric characters are kept unmodified.
2525--
26−-- * Symbols are encoded as a dollar symbol ($) followed by their ordinal value
26+-- * Reserved javascript identifiers are prefixed with '$$'.
27+--
28+-- * Symbols are prefixed with '$' followed by a symbol name or their ordinal value.
2729--
2830identToJs :: Ident -> String
31+identToJs (Ident name) | nameIsJsReserved name = "$$" ++ name
2932identToJs (Ident name) = concatMap identCharToString name
3033identToJs (Op op) = concatMap identCharToString op
3134identToJs (Escaped name) = name
3235
36+-- |
37+-- Attempts to find a human-readable name for a symbol, if none has been specified returns the
38+-- ordinal value.
39+--
3340identCharToString :: Char -> String
3441identCharToString c | isAlphaNum c = [c]
3542identCharToString '_' = "_"
@@ -54,3 +61,68 @@ identCharToString '\\' = "$bslash"
5461identCharToString '?' = "$qmark"
5562identCharToString '@' = "$at"
5663identCharToString c = '$' : show (ord c)
64+
65+-- |
66+-- Checks whether an identifier name is reserved in Javascript.
67+--
68+nameIsJsReserved :: String -> Bool
69+nameIsJsReserved name =
70+ elem name [ "abstract"
71+ , "boolean"
72+ , "break"
73+ , "byte"
74+ , "case"
75+ , "catch"
76+ , "char"
77+ , "class"
78+ , "const"
79+ , "continue"
80+ , "debugger"
81+ , "default"
82+ , "delete"
83+ , "do"
84+ , "double"
85+ , "else"
86+ , "enum"
87+ , "export"
88+ , "extends"
89+ , "final"
90+ , "finally"
91+ , "float"
92+ , "for"
93+ , "function"
94+ , "goto"
95+ , "if"
96+ , "implements"
97+ , "import"
98+ , "in"
99+ , "instanceof"
100+ , "int"
101+ , "interface"
102+ , "let"
103+ , "long"
104+ , "native"
105+ , "new"
106+ , "package"
107+ , "private"
108+ , "protected"
109+ , "public"
110+ , "return"
111+ , "short"
112+ , "static"
113+ , "super"
114+ , "switch"
115+ , "synchronized"
116+ , "this"
117+ , "throw"
118+ , "throws"
119+ , "transient"
120+ , "try"
121+ , "typeof"
122+ , "var"
123+ , "void"
124+ , "volatile"
125+ , "while"
126+ , "with"
127+ , "yield" ]
128+
src/Language/PureScript/CodeGen/JS.hs+14−11
@@ -41,7 +41,7 @@ import Language.PureScript.Options
4141import Language.PureScript.CodeGen.JS.AST as AST
4242import Language.PureScript.Types
4343import Language.PureScript.CodeGen.Optimize
44−import Language.PureScript.CodeGen.Common (identToJs)
44+import Language.PureScript.CodeGen.Common
4545import Language.PureScript.TypeChecker.Monad (canonicalizeDataConstructor)
4646
4747-- |
@@ -87,14 +87,18 @@ declToJs _ _ _ _ = Nothing
8787
8888setProperty :: Ident -> JS -> ModuleName -> [JS]
8989setProperty ident@(Op op) val (ModuleName (ProperName moduleName)) =
90− [ JSAssignment (JSAccessor (identToJs ident) (JSVar moduleName)) val
91− , JSAssignment (JSIndexer (JSStringLiteral op) (JSVar moduleName)) (JSAccessor (identToJs ident) (JSVar moduleName)) ]
90+ [ JSAssignment (accessor ident (JSVar moduleName)) val
91+ , JSAssignment (JSIndexer (JSStringLiteral op) (JSVar moduleName)) (accessor ident (JSVar moduleName)) ]
9292setProperty ident val (ModuleName (ProperName moduleName)) =
93− [ JSAssignment (JSAccessor (identToJs ident) (JSVar moduleName)) val ]
93+ [ JSAssignment (accessor ident (JSVar moduleName)) val ]
9494
9595var :: Ident -> JS
9696var = JSVar . identToJs
9797
98+accessor :: Ident -> JS -> JS
99+accessor (Ident name) | nameIsJsReserved name = JSIndexer (JSStringLiteral name)
100+accessor ident = JSAccessor (identToJs ident)
101+
98102valueToJs :: Options -> ModuleName -> Environment -> Value -> JS
99103valueToJs _ _ _ (NumericLiteral n) = JSNumericLiteral n
100104valueToJs _ _ _ (StringLiteral s) = JSStringLiteral s
@@ -104,9 +108,9 @@ valueToJs opts m e (ObjectLiteral ps) = JSObjectLiteral (map (second (valueToJs
104108valueToJs opts m e (ObjectUpdate o ps) = JSApp (JSAccessor "extend" (JSVar "Object")) [ valueToJs opts m e o, JSObjectLiteral (map (second (valueToJs opts m e)) ps)]
105109valueToJs _ m e (Constructor (Qualified Nothing name)) =
106110 case M.lookup (m, name) (dataConstructors e) of
107− Just (_, Alias aliasModule aliasIdent) -> qualifiedToJS identToJs (Qualified (Just aliasModule) aliasIdent)
111+ Just (_, Alias aliasModule aliasIdent) -> qualifiedToJS id (Qualified (Just aliasModule) aliasIdent)
108112 _ -> JSVar . runProperName $ name
109−valueToJs _ _ _ (Constructor name) = qualifiedToJS runProperName name
113+valueToJs _ _ _ (Constructor name) = qualifiedToJS (Ident . runProperName) name
110114valueToJs opts m e (Block sts) = JSApp (JSFunction Nothing [] (JSBlock (map (statementToJs opts m e) sts))) []
111115valueToJs opts m e (Case values binders) = bindersToJs opts m e binders (map (valueToJs opts m e) values)
112116valueToJs opts m e (IfThenElse cond th el) = JSConditional (valueToJs opts m e cond) (valueToJs opts m e th) (valueToJs opts m e el)
@@ -156,17 +160,16 @@ varToJs m e qual@(Qualified _ ident) = go qual
156160 Just (_, Alias aliasModule aliasIdent) -> go (Qualified (Just aliasModule) aliasIdent)
157161 _ -> case qual of
158162 Qualified Nothing _ -> var ident
159− Qualified (Just (ModuleName (ProperName mn))) (Op op) -> JSIndexer (JSStringLiteral op) (JSVar mn)
160− _ -> qualifiedToJS identToJs qual
163+ _ -> qualifiedToJS id qual
161164 isExtern (Extern ForeignImport) = True
162165 isExtern (Alias m' ident') = case M.lookup (m', ident') (names e) of
163166 Just (_, ty') -> isExtern ty'
164167 Nothing -> error "Undefined alias in varToJs"
165168 isExtern _ = False
166169
167−qualifiedToJS :: (a -> String) -> Qualified a -> JS
168−qualifiedToJS f (Qualified (Just (ModuleName (ProperName m))) a) = JSAccessor (f a) (JSVar m)
169−qualifiedToJS f (Qualified Nothing a) = JSVar (f a)
170+qualifiedToJS :: (a -> Ident) -> Qualified a -> JS
171+qualifiedToJS f (Qualified (Just (ModuleName (ProperName m))) a) = accessor (f a) (JSVar m)
172+qualifiedToJS f (Qualified Nothing a) = JSVar $ identToJs (f a)
170173
171174bindersToJs :: Options -> ModuleName -> Environment -> [([Binder], Maybe Guard, Value)] -> [JS] -> JS
172175bindersToJs opts m e binders vals = runGen (map identToJs (unusedNames (binders, vals))) $ do
src/Language/PureScript/Parser/Common.hs+1−69
@@ -52,69 +52,6 @@ reservedPsNames = [ "data"
5252
5353 ]
5454
55−-- |
56−-- A list of javascript reserved identifiers
57−--
58−reservedJsNames :: [String]
59−reservedJsNames = [ "abstract"
60− , "boolean"
61− , "break"
62− , "byte"
63− , "case"
64− , "catch"
65− , "char"
66− , "class"
67− , "const"
68− , "continue"
69− , "debugger"
70− , "default"
71− , "delete"
72− , "do"
73− , "double"
74− , "else"
75− , "enum"
76− , "export"
77− , "extends"
78− , "final"
79− , "finally"
80− , "float"
81− , "for"
82− , "function"
83− , "goto"
84− , "if"
85− , "implements"
86− , "import"
87− , "in"
88− , "instanceof"
89− , "int"
90− , "interface"
91− , "let"
92− , "long"
93− , "native"
94− , "new"
95− , "package"
96− , "private"
97− , "protected"
98− , "public"
99− , "return"
100− , "short"
101− , "static"
102− , "super"
103− , "switch"
104− , "synchronized"
105− , "this"
106− , "throw"
107− , "throws"
108− , "transient"
109− , "try"
110− , "typeof"
111− , "var"
112− , "void"
113− , "volatile"
114− , "while"
115− , "with"
116− , "yield" ]
117−
11855-- |
11956-- A list of reserved identifiers for types
12057--
@@ -287,12 +224,7 @@ integerOrFloat = (Left <$> P.try (PT.natural tokenParser) <|>
287224-- Parse an identifier or parenthesized operator
288225--
289226parseIdent :: P.Parsec String ParseState Ident
290−parseIdent = parseIdent' <|> (Op <$> parens operator)
291− where
292− parseIdent' :: P.Parsec String ParseState Ident
293− parseIdent' = do
294− ident <- identifier
295− return $ if (ident `elem` reservedJsNames) then (Escaped $ "$" ++ ident) else (Ident ident)
227+parseIdent = (Ident <$> identifier) <|> (Op <$> parens operator)
296228
297229-- |
298230-- Parse a token inside square brackets