summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2024-12-08 11:06:49 +0100
committerRoman Smrž <roman.smrz@seznam.cz>2024-12-08 15:19:44 +0100
commitfb35e1c3273927425e6cca340c6375666fb87be7 (patch)
treebcb94abf1490b8bf96145e752e6fa404af1b775f /src
parent20b24c190e70cf57819c9b67aae1338d20248784 (diff)
Reserved words not accepted as identifiers
Diffstat (limited to 'src')
-rw-r--r--src/Parser/Expr.hs16
1 files changed, 14 insertions, 2 deletions
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