From ad00b4534582c5ce94243f7dda3b76d7c0bba021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 5 Apr 2026 10:31:55 +0200 Subject: Parse data constructors --- src/Parser/Expr.hs | 18 +++++++++++++++++- src/Parser/Statement.hs | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Parser/Expr.hs b/src/Parser/Expr.hs index b9b5f01..585f727 100644 --- a/src/Parser/Expr.hs +++ b/src/Parser/Expr.hs @@ -5,11 +5,13 @@ module Parser.Expr ( varName, newVarName, addVarName, + constrName, someExpr, typedExpr, literal, variable, + constructor, stringExpansion, @@ -83,6 +85,13 @@ addVarName off (TypedVarName name) = do Nothing -> return () modify $ \s -> s { testVars = ( name, ( LocalVarName name, ExprTypePrim @a Proxy )) : testVars s } +constrName :: TestParser VarName +constrName = label "contructor name" $ do + lexeme $ try $ do + lead <- upperChar + rest <- takeWhileP Nothing (\x -> isAlphaNum x || x == '_') + return $ VarName $ TL.toStrict $ TL.fromChunks $ T.singleton lead : TL.toChunks rest + someExpansion :: TestParser SomeExpr someExpansion = do void $ char '$' @@ -370,10 +379,17 @@ variable = label "variable" $ do e <- lookupVarExpr off sline name recordSelector e <|> return e +constructor :: TestParser SomeExpr +constructor = label "constructor" $ do + off <- stateOffset <$> getParserState + sline <- getSourceLine + name <- constrName + lookupVarExpr off sline name + functionCall :: TestParser SomeExpr functionCall = do sline <- getSourceLine - variable >>= \case + (variable <|> constructor) >>= \case SomeExpr e'@(FunVariable argTypes _ _) -> do let check = checkFunctionArguments argTypes args <- functionArguments check someExpr literal (\poff -> lookupVarExpr poff sline . VarName) diff --git a/src/Parser/Statement.hs b/src/Parser/Statement.hs index 0e6314b..7876542 100644 --- a/src/Parser/Statement.hs +++ b/src/Parser/Statement.hs @@ -309,7 +309,7 @@ instance ExprType a => ParamType (ExprParam a) where type ParamRep (ExprParam a) = Expr a parseParam _ = do off <- stateOffset <$> getParserState - SomeExpr e <- literal <|> variable <|> between (symbol "(") (symbol ")") someExpr + SomeExpr e <- literal <|> variable <|> constructor <|> between (symbol "(") (symbol ")") someExpr unifyExpr off Proxy e showParamType _ = "<" ++ T.unpack (textExprType @a Proxy) ++ ">" paramExpr = fmap ExprParam -- cgit v1.2.3