module Language.PureScript.CodeGen.Common where
import Data.Char
import Data.List (intercalate)
import Language.PureScript.Names
identToJs :: Ident -> String
identToJs (Ident name) | nameIsJsReserved name = "$$" ++ name
identToJs (Ident name) = concatMap identCharToString name
identToJs (Op op) = concatMap identCharToString op
identToJs (Escaped name) = name
identCharToString :: Char -> String
identCharToString c | isAlphaNum c = [c]
identCharToString '_' = "_"
identCharToString '.' = "$dot"
identCharToString '$' = "$dollar"
identCharToString '~' = "$tilde"
identCharToString '=' = "$eq"
identCharToString '<' = "$less"
identCharToString '>' = "$greater"
identCharToString '!' = "$bang"
identCharToString '#' = "$hash"
identCharToString '%' = "$percent"
identCharToString '^' = "$up"
identCharToString '&' = "$amp"
identCharToString '|' = "$bar"
identCharToString '*' = "$times"
identCharToString '/' = "$div"
identCharToString '+' = "$plus"
identCharToString '-' = "$minus"
identCharToString ':' = "$colon"
identCharToString '\\' = "$bslash"
identCharToString '?' = "$qmark"
identCharToString '@' = "$at"
identCharToString '\'' = "$prime"
identCharToString c = '$' : show (ord c)
nameIsJsReserved :: String -> Bool
nameIsJsReserved name =
name `elem` [ "abstract"
, "arguments"
, "boolean"
, "break"
, "byte"
, "case"
, "catch"
, "char"
, "class"
, "const"
, "continue"
, "debugger"
, "default"
, "delete"
, "do"
, "double"
, "else"
, "enum"
, "eval"
, "export"
, "extends"
, "final"
, "finally"
, "float"
, "for"
, "function"
, "goto"
, "if"
, "implements"
, "import"
, "in"
, "instanceof"
, "int"
, "interface"
, "let"
, "long"
, "native"
, "new"
, "package"
, "private"
, "protected"
, "public"
, "return"
, "short"
, "static"
, "super"
, "switch"
, "synchronized"
, "this"
, "throw"
, "throws"
, "transient"
, "try"
, "typeof"
, "var"
, "void"
, "volatile"
, "while"
, "with"
, "yield" ]
moduleNameToJs :: ModuleName -> String
moduleNameToJs (ModuleName pns) = intercalate "_" (runProperName `map` pns)