From fb35e1c3273927425e6cca340c6375666fb87be7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 8 Dec 2024 11:06:49 +0100 Subject: Reserved words not accepted as identifiers --- src/Parser/Expr.hs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/Parser/Expr.hs') diff --git a/src/Parser/Expr.hs b/src/Parser/Expr.hs index 4ed0215..024ea6b 100644 --- a/src/Parser/Expr.hs +++ b/src/Parser/Expr.hs @@ -33,18 +33,30 @@ import Data.Void import Text.Megaparsec hiding (State) import Text.Megaparsec.Char import Text.Megaparsec.Char.Lexer qualified as L +import Text.Megaparsec.Error.Builder qualified as Err import Text.Regex.TDFA qualified as RE import Text.Regex.TDFA.Text qualified as RE import Parser.Core import Test +reservedWords :: [ Text ] +reservedWords = + [ "test", "def", "let" + , "module", "export", "import" + ] + identifier :: TestParser Text identifier = label "identifier" $ do - lexeme $ do + lexeme $ try $ do + off <- stateOffset <$> getParserState lead <- lowerChar rest <- takeWhileP Nothing (\x -> isAlphaNum x || x == '_') - return $ TL.toStrict $ TL.fromChunks $ (T.singleton lead :) $ TL.toChunks rest + let ident = TL.toStrict $ TL.fromChunks $ (T.singleton lead :) $ TL.toChunks rest + when (ident `elem` reservedWords) $ parseError $ Err.err off $ mconcat + [ Err.utoks $ TL.fromStrict ident + ] + return ident varName :: TestParser VarName varName = label "variable name" $ VarName <$> identifier -- cgit v1.2.3