git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Disallow reserved names as value names #136
commitf6aacec03499f8e28c85560d6e88da690b7e7993 authorPhil Freeman <paf31@cantab.net> dateWed 29 Jan 2014, 20:00 (13 years ago) parent3ab225e
3 files changed+24−4
examples/failing/Reserved.purs+4 src/Language/PureScript/Parser/Common.hs+16−1 src/Language/PureScript/Parser/Declarations.hs+4−3
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"
7878 , "let"
7979 , "class"
8080 , "instance"
81− , "where" ]
81+ , "where"
82+ , "null"
83+ , "undefined" ]
8284
8385-- |
8486-- A list of built-in operator names
@@ -266,6 +268,19 @@ operatorOrBuiltIn = P.try operator <|> P.choice (map (\s -> P.try (reservedOp s)
266268parseIdent :: P.Parsec String ParseState Ident
267269parseIdent = (Ident <$> identifier) <|> (Op <$> parens operatorOrBuiltIn)
268270
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+
269284-- |
270285-- Parse a token inside square brackets
271286--
src/Language/PureScript/Parser/Declarations.hs+4−3
@@ -20,6 +20,7 @@ module Language.PureScript.Parser.Declarations (
2020) where
2121
2222import Data.Maybe (isJust, fromMaybe)
23+import Control.Monad (when)
2324import Control.Applicative
2425import qualified Text.Parsec as P
2526
@@ -45,7 +46,7 @@ parseDataDeclaration = do
4546
4647parseTypeDeclaration :: P.Parsec String ParseState Declaration
4748parseTypeDeclaration =
48− TypeDeclaration <$> P.try (parseIdent <* lexeme (indented *> P.string "::"))
49+ TypeDeclaration <$> P.try (parseNonReservedIdent <* lexeme (indented *> P.string "::"))
4950 <*> parsePolyType
5051
5152parseTypeSynonymDeclaration :: P.Parsec String ParseState Declaration
@@ -56,7 +57,7 @@ parseTypeSynonymDeclaration =
5657
5758parseValueDeclaration :: P.Parsec String ParseState Declaration
5859parseValueDeclaration =
59− ValueDeclaration <$> parseIdent
60+ ValueDeclaration <$> parseNonReservedIdent
6061 <*> P.many parseTopLevelBinder
6162 <*> P.optionMaybe parseGuard
6263 <*> ((lexeme (indented *> P.char '=')) *> parseValue)
@@ -68,7 +69,7 @@ parseExternDeclaration :: P.Parsec String ParseState Declaration
6869parseExternDeclaration = P.try (reserved "foreign") *> indented *> (reserved "import") *> indented *>
6970 (ExternDataDeclaration <$> (P.try (reserved "data") *> indented *> properName)
7071 <*> (lexeme (indented *> P.string "::") *> parseKind)
71− <|> do ident <- parseIdent
72+ <|> do ident <- parseNonReservedIdent
7273 js <- P.optionMaybe (parseJSLiteral <$> stringLiteral)
7374 ty <- (lexeme (indented *> P.string "::") *> parsePolyType)
7475 return $ ExternDeclaration (if isJust js then InlineJavascript else ForeignImport) ident js ty)