aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2022-09-06 12:50:21 +0100
committerJuan J. Martinez <jjm@usebox.net>2022-09-06 12:50:21 +0100
commitf8dfbfc1604aea626a6502b9f6d06ea1299a0cdb (patch)
treead45f7f7e057e5ff4c91b5d417e46aa2f52a0a85
parent67967f029305c663fb3fbb4e0f1a6e375f5d572c (diff)
downloadmicro-lang-hs-f8dfbfc1604aea626a6502b9f6d06ea1299a0cdb.tar.gz
micro-lang-hs-f8dfbfc1604aea626a6502b9f6d06ea1299a0cdb.zip
Split statement parsers to get better error reporting
Also prevents duplicated parsers for var.
-rw-r--r--src/Parser.hs25
1 files 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]