From f8dfbfc1604aea626a6502b9f6d06ea1299a0cdb Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Tue, 6 Sep 2022 12:50:21 +0100 Subject: Split statement parsers to get better error reporting Also prevents duplicated parsers for var. --- src/Parser.hs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Parser.hs b/src/Parser.hs index 2817208..5ee6de9 100644 --- a/src/Parser.hs +++ b/src/Parser.hs @@ -86,9 +86,10 @@ fdef ident priv anon pos = do body <- braces $ many $ - try (grVar False) <|> do - x <- statement + do + x <- fStatement pure $ [x] + <|> grVar True return $ Func ident args rtyp (concat $ body) priv anon pos function :: Bool -> Parser Expr @@ -102,7 +103,6 @@ function priv = do varWithValue :: Bool -> Parser Expr varWithValue priv = do (ident, typ, _, pos) <- arg - -- FIXME: this error hint is not being used reservedOp "=" "assignation" value <- expr return $ Var ident typ value priv pos @@ -175,11 +175,15 @@ exprStmt = do reservedOp ";" return $ e +-- statements that appear in functions +fStatement :: Parser Expr +fStatement = try exprStmt <|> var True <|> return' + +-- top level statement statement :: Parser Expr -statement = do - try exprStmt - <|> var False - <|> return' +statement = + try exprStmt <|> try (privateDf var) + <|> return' -- this will raise an error module' :: Parser Expr module' = do @@ -193,11 +197,10 @@ program = do m <- module' n <- many $ - try (privateDfn grVar) <|> do - x <- - try (privateDf function) <|> try (privateDf var) - <|> statement + do + x <- try (privateDf function) <|> statement pure $ [x] + <|> privateDfn grVar return $ [m] ++ (concat $ n) parse :: Parser [Expr] -- cgit v1.2.3