aboutsummaryrefslogtreecommitdiffstats
path: root/src/Parser.hs
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2022-09-27 15:45:38 +0300
committerJan Tuomi <jans.tuomi@gmail.com>2022-12-05 14:21:53 +0200
commit3b1144988c49c1221b1c5f6b8f7544f1dea7cb44 (patch)
tree1dbba923dbe11ada9e7138ad64e7272fe35553ab /src/Parser.hs
parent0a56af62ffa95360587728b82c947a2936743a4c (diff)
Show position info in error messages
Diffstat (limited to 'src/Parser.hs')
-rw-r--r--src/Parser.hs19
1 files changed, 11 insertions, 8 deletions
diff --git a/src/Parser.hs b/src/Parser.hs
index b60fdc9..e46d80c 100644
--- a/src/Parser.hs
+++ b/src/Parser.hs
@@ -11,15 +11,17 @@ import Utils
validateBalance :: [String] -> [AST] -> LContext [AST]
validateBalance allowed asts = do
- when (ASTSymbol "(" `elem` astNodes && "(" `notElem` allowed)
- $ throwL "unbalanced function call"
- when (ASTSymbol "[" `elem` astNodes && "[" `notElem` allowed)
- $ throwL "unbalanced vector"
- when (ASTSymbol "{" `elem` astNodes && "{" `notElem` allowed)
- $ throwL "unbalanced hash map"
+ when (MB.isJust parenM && "(" `notElem` allowed)
+ $ throwL (astPos $ MB.fromJust parenM) "unbalanced function call"
+ when (MB.isJust bracketM && "[" `notElem` allowed)
+ $ throwL (astPos $ MB.fromJust bracketM) "unbalanced vector"
+ when (MB.isJust curlyM && "{" `notElem` allowed)
+ $ throwL (astPos $ MB.fromJust curlyM) "unbalanced hash map"
return asts
where
- astNodes = map astNode asts
+ parenM = L.find ((== ASTSymbol "(") . astNode) asts
+ bracketM = L.find ((== ASTSymbol "[") . astNode) asts
+ curlyM = L.find ((== ASTSymbol "{") . astNode) asts
parseToken :: Token -> AST
parseToken (Token token tr tc tf)
@@ -62,8 +64,9 @@ _parse acc (Token { tokenContent = "]" }:rest) = do
_parse acc (Token { tokenContent = "}" }:rest) = do
let children' = takeWhile (astNode .> (/= ASTSymbol "{")) acc
children <- validateBalance ["{"] children'
- pairs <- asPairsM $ reverse children
let openCurly = MB.fromJust $ L.find (astNode .> (== ASTSymbol "{")) acc
+ pairs <- asPairsM (reverse children) `catchError`
+ \(LException _ e) -> throwL (astPos openCurly) e
let hmap = openCurly { astNode = ASTHashMap (M.fromList pairs) }
let newAcc = hmap : drop (length children + 1) acc
_parse newAcc rest