on master help sign in
Make sure that generated JS does not use JS's reserved words as variables
40ea6dfEvan Czaplicki
compiler/Parse/Declaration.hs+2−15
@@ -3,6 +3,7 @@ module Parse.Declaration where
3
3
4import Control.Applicative ((<$>), (<*>))
4import Control.Applicative ((<$>), (<*>))
5import qualified Data.List as List
5import qualified Data.List as List
6+import qualified Data.Set as Set
6import Text.Parsec hiding (newline,spaces)
7import Text.Parsec hiding (newline,spaces)
7import Text.Parsec.Indent
8import Text.Parsec.Indent
8import qualified Text.Pandoc as Pan
9import qualified Text.Pandoc as Pan
@@ -94,24 +95,10 @@ importEvent = do
94jsVar :: IParser String
95jsVar :: IParser String
95jsVar = betwixt '"' '"' $ do
96jsVar = betwixt '"' '"' $ do
96 v <- (:) <$> (letter <|> char '_') <*> many (alphaNum <|> char '_')
97 v <- (:) <$> (letter <|> char '_') <*> many (alphaNum <|> char '_')
97− if v `notElem` jsReserveds then return v else
98+ if Set.notMember v jsReserveds then return v else
98 error $ "'" ++ v ++
99 error $ "'" ++ v ++
99 "' is not a good name for a importing or exporting JS values."
100 "' is not a good name for a importing or exporting JS values."
100
101
101−jsReserveds :: [String]
102−jsReserveds =
103− [ "null", "undefined", "Nan", "Infinity", "true", "false", "eval"
104− , "arguments", "int", "byte", "char", "goto", "long", "final", "float"
105− , "short", "double", "native", "throws", "boolean", "abstract", "volatile"
106− , "transient", "synchronized", "function", "break", "case", "catch"
107− , "continue", "debugger", "default", "delete", "do", "else", "finally"
108− , "for", "function", "if", "in", "instanceof", "new", "return", "switch"
109− , "this", "throw", "try", "typeof", "var", "void", "while", "with", "class"
110− , "const", "enum", "export", "extends", "import", "super", "implements"
111− , "interface", "let", "package", "private", "protected", "public"
112− , "static", "yield"
113− ]
114−
115isExportable tipe =
102isExportable tipe =
116 case tipe of
103 case tipe of
117 T.Lambda _ _ ->
104 T.Lambda _ _ ->
compiler/Parse/Helpers.hs+23−3
@@ -4,6 +4,7 @@ import Control.Applicative ((<$>),(<*>))
4import Control.Monad
4import Control.Monad
5import Control.Monad.State
5import Control.Monad.State
6import Data.Char (isUpper)
6import Data.Char (isUpper)
7+import qualified Data.Set as Set
7import SourceSyntax.Helpers as Help
8import SourceSyntax.Helpers as Help
8import SourceSyntax.Location as Location
9import SourceSyntax.Location as Location
9import SourceSyntax.Expression
10import SourceSyntax.Expression
@@ -18,6 +19,20 @@ reserveds = [ "if", "then", "else"
18 , "import", "as", "hiding", "open"
19 , "import", "as", "hiding", "open"
19 , "export", "foreign" ]
20 , "export", "foreign" ]
20
21
22+jsReserveds :: Set.Set String
23+jsReserveds = Set.fromList
24+ [ "null", "undefined", "Nan", "Infinity", "true", "false", "eval"
25+ , "arguments", "int", "byte", "char", "goto", "long", "final", "float"
26+ , "short", "double", "native", "throws", "boolean", "abstract", "volatile"
27+ , "transient", "synchronized", "function", "break", "case", "catch"
28+ , "continue", "debugger", "default", "delete", "do", "else", "finally"
29+ , "for", "function", "if", "in", "instanceof", "new", "return", "switch"
30+ , "this", "throw", "try", "typeof", "var", "void", "while", "with", "class"
31+ , "const", "enum", "export", "extends", "import", "super", "implements"
32+ , "interface", "let", "package", "private", "protected", "public"
33+ , "static", "yield"
34+ ]
35+
21expecting = flip (<?>)
36expecting = flip (<?>)
22
37
23type IParser a = ParsecT String () (State SourcePos) a
38type IParser a = ParsecT String () (State SourcePos) a
@@ -49,13 +64,18 @@ rLabel = lowVar
49innerVarChar :: IParser Char
64innerVarChar :: IParser Char
50innerVarChar = alphaNum <|> char '_' <|> char '\'' <?> ""
65innerVarChar = alphaNum <|> char '_' <|> char '\'' <?> ""
51
66
52−deprime :: String -> String
67+makeSafe :: String -> String
68+makeSafe = dereserve . deprime
69+ where
53−deprime = map (\c -> if c == '\'' then '$' else c)
70+ deprime = map (\c -> if c == '\'' then '$' else c)
71+ dereserve x = case Set.member x jsReserveds of
72+ False -> x
73+ True -> "$" ++ x
54
74
55makeVar :: IParser Char -> IParser String
75makeVar :: IParser Char -> IParser String
56makeVar p = do v <- (:) <$> p <*> many innerVarChar
76makeVar p = do v <- (:) <$> p <*> many innerVarChar
57 guard (v `notElem` reserveds)
77 guard (v `notElem` reserveds)
58− return (deprime v)
78+ return (makeSafe v)
59
79
60reserved :: String -> IParser String
80reserved :: String -> IParser String
61reserved word =
81reserved word =