Improve error handling in general parser

This commit is contained in:
2021-11-04 11:36:54 +01:00
parent 324336a897
commit a10e857ffe

View File

@@ -4,6 +4,7 @@ module Assembler (
) where ) where
import qualified Data.Char as Char import qualified Data.Char as Char
import qualified Data.List as List
import qualified Data.Monoid as Monoid import qualified Data.Monoid as Monoid
import qualified VirtualMachine as VM (Op(..), Instruction, Command, instructionByOp) import qualified VirtualMachine as VM (Op(..), Instruction, Command, instructionByOp)
import qualified Util as U import qualified Util as U
@@ -284,9 +285,11 @@ assertConsumed parser tokens = do
else Nothing else Nothing
parse :: [Token] -> Either String AST parse :: [Token] -> Either String AST
parse tokens = case sequenceA results of parse tokens = do
(Just r) -> Right $ ProgramNode $ map (\(ParseResult ast _) -> ast) r let lines = U.explode (==NewLine) tokens
Nothing -> Left "Unexpected token" let results = map (assertConsumed parseLine) lines
where let errors = filter ((==Nothing) . snd) (zipWith (,) [1..] $ results)
lines = U.explode (==NewLine) tokens let errorMsg = "Parse error in line(s): " ++ (List.intercalate ", " $ map (show. fst) errors)
results = map (assertConsumed parseLine) lines case sequenceA results of
(Just r) -> return $ ProgramNode $ map (\(ParseResult ast _) -> ast) r
Nothing -> Left errorMsg