@@ -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" ]
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"
21expecting = flip (<?>)
36expecting = flip (<?>)
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 '\'' <?> ""
52−deprime :: String -> String
67+makeSafe :: String -> String
68+makeSafe = dereserve . deprime
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
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)
60reserved :: String -> IParser String
80reserved :: String -> IParser String
61reserved word =
81reserved word =