git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Disallow reserved names as value names #136
f6aacecPhil Freeman
examples/failing/Reserved.pursnew file+4
@@ -0,0 +1,4 @@
1+module Reserved where
2+
3+(<) :: Number -> Number -> Number
4+(<) a b = !(a >= b)
src/Language/PureScript/Parser/Common.hs+16−1
@@ -78,7 +78,9 @@ reservedNames = [ "case"
78 , "let"
78 , "let"
79 , "class"
79 , "class"
80 , "instance"
80 , "instance"
81− , "where" ]
81+ , "where"
82+ , "null"
83+ , "undefined" ]
82
84
83-- |
85-- |
84-- A list of built-in operator names
86-- A list of built-in operator names
@@ -266,6 +268,19 @@ operatorOrBuiltIn = P.try operator <|> P.choice (map (\s -> P.try (reservedOp s)
266parseIdent :: P.Parsec String ParseState Ident
268parseIdent :: P.Parsec String ParseState Ident
267parseIdent = (Ident <$> identifier) <|> (Op <$> parens operatorOrBuiltIn)
269parseIdent = (Ident <$> identifier) <|> (Op <$> parens operatorOrBuiltIn)
268
270
271+
272+-- |
273+-- Parse an identifier or parenthesized operator that is not a reserved keyword or operator
274+--
275+parseNonReservedIdent :: P.Parsec String ParseState Ident
276+parseNonReservedIdent = do
277+ ident <- parseIdent
278+ when (isReserved ident) $ P.unexpected $ "reserved identifier " ++ show ident
279+ return ident
280+ where
281+ isReserved (Ident ident) = ident `elem` reservedNames
282+ isReserved (Op op) = op `elem` reservedOpNames
283+
269-- |
284-- |
270-- Parse a token inside square brackets
285-- Parse a token inside square brackets
271--
286--
src/Language/PureScript/Parser/Declarations.hs+4−3
@@ -20,6 +20,7 @@ module Language.PureScript.Parser.Declarations (
20) where
20) where
21
21
22import Data.Maybe (isJust, fromMaybe)
22import Data.Maybe (isJust, fromMaybe)
23+import Control.Monad (when)
23import Control.Applicative
24import Control.Applicative
24import qualified Text.Parsec as P
25import qualified Text.Parsec as P
25
26
@@ -45,7 +46,7 @@ parseDataDeclaration = do
45
46
46parseTypeDeclaration :: P.Parsec String ParseState Declaration
47parseTypeDeclaration :: P.Parsec String ParseState Declaration
47parseTypeDeclaration =
48parseTypeDeclaration =
48− TypeDeclaration <$> P.try (parseIdent <* lexeme (indented *> P.string "::"))
49+ TypeDeclaration <$> P.try (parseNonReservedIdent <* lexeme (indented *> P.string "::"))
49 <*> parsePolyType
50 <*> parsePolyType
50
51
51parseTypeSynonymDeclaration :: P.Parsec String ParseState Declaration
52parseTypeSynonymDeclaration :: P.Parsec String ParseState Declaration
@@ -56,7 +57,7 @@ parseTypeSynonymDeclaration =
56
57
57parseValueDeclaration :: P.Parsec String ParseState Declaration
58parseValueDeclaration :: P.Parsec String ParseState Declaration
58parseValueDeclaration =
59parseValueDeclaration =
59− ValueDeclaration <$> parseIdent
60+ ValueDeclaration <$> parseNonReservedIdent
60 <*> P.many parseTopLevelBinder
61 <*> P.many parseTopLevelBinder
61 <*> P.optionMaybe parseGuard
62 <*> P.optionMaybe parseGuard
62 <*> ((lexeme (indented *> P.char '=')) *> parseValue)
63 <*> ((lexeme (indented *> P.char '=')) *> parseValue)
@@ -68,7 +69,7 @@ parseExternDeclaration :: P.Parsec String ParseState Declaration
68parseExternDeclaration = P.try (reserved "foreign") *> indented *> (reserved "import") *> indented *>
69parseExternDeclaration = P.try (reserved "foreign") *> indented *> (reserved "import") *> indented *>
69 (ExternDataDeclaration <$> (P.try (reserved "data") *> indented *> properName)
70 (ExternDataDeclaration <$> (P.try (reserved "data") *> indented *> properName)
70 <*> (lexeme (indented *> P.string "::") *> parseKind)
71 <*> (lexeme (indented *> P.string "::") *> parseKind)
71− <|> do ident <- parseIdent
72+ <|> do ident <- parseNonReservedIdent
72 js <- P.optionMaybe (parseJSLiteral <$> stringLiteral)
73 js <- P.optionMaybe (parseJSLiteral <$> stringLiteral)
73 ty <- (lexeme (indented *> P.string "::") *> parsePolyType)
74 ty <- (lexeme (indented *> P.string "::") *> parsePolyType)
74 return $ ExternDeclaration (if isJust js then InlineJavascript else ForeignImport) ident js ty)
75 return $ ExternDeclaration (if isJust js then InlineJavascript else ForeignImport) ident js ty)