Skip to content

Commit

Permalink
add: InvalidDefer error
Browse files Browse the repository at this point in the history
  • Loading branch information
oriollinan committed Jan 13, 2025
1 parent c02b791 commit bfc12bf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
8 changes: 6 additions & 2 deletions lib/Ast/Parser/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ parseTerm =
parseContinue,
parseBlock id,
parseCast,
parseDefer *> parseExpr,
parseDefer,
M.try parseFunction,
M.try parseForeignFunction,
M.try parseDeclaration,
Expand Down Expand Up @@ -258,10 +258,14 @@ parseCast = do
expr <- M.between (PU.symbol "(") (PU.symbol ")") parseExpr
return $ AT.Cast srcLoc type' expr

parseDefer :: PU.Parser ()
parseDefer :: PU.Parser AT.Expr
parseDefer = do
defered <- PU.symbol "defer" *> parseExpr
S.modify $ PS.pushDefered defered
next <- M.optional parseExpr
case next of
Nothing -> M.customFailure $ PU.InvalidDefer defered
(Just e) -> return e

parseParenExpr :: PU.Parser AT.Expr
parseParenExpr = M.between (PU.symbol "(") (PU.symbol ")") parseExpr
9 changes: 3 additions & 6 deletions lib/Ast/Parser/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,17 @@ type Parser = M.ParsecT ParseErrorCustom String (S.State PS.ParserState)

data ParseErrorCustom
= UnknownType String
| UndefinedVar String
| UndefinedFunction String
| InvalidFunctionType String AT.Type
| InvalidDefer AT.Expr
deriving (Show, Ord, Eq)

instance M.ShowErrorComponent ParseErrorCustom where
showErrorComponent (UnknownType n) =
"Unknown type: type \"" ++ n ++ "\" does not exist"
showErrorComponent (UndefinedVar n) =
"Undefined Variable: variable \"" ++ n ++ "\" is not defined"
showErrorComponent (UndefinedFunction n) =
"Undefined Function: function \"" ++ n ++ "\" is not defined"
showErrorComponent (InvalidFunctionType n t) =
"Invalid Function Type: function \"" ++ n ++ "\" with type \"" ++ show t ++ "\" is not valid"
showErrorComponent (InvalidDefer e) =
"Invalid Defer: defer \"" ++ show e ++ "\" is not valid "

-- | Skips whitespace and comments (starting with `%`). Ensures proper handling of spacing in parsers.
sc :: Parser ()
Expand Down

0 comments on commit bfc12bf

Please # to comment.