forked from entropia/tip-toi-reveng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOneLineParser.hs
31 lines (26 loc) · 1.03 KB
/
OneLineParser.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module OneLineParser (parseOneLine) where
import System.Exit
import Text.Parsec hiding (Line, lookAhead, spaces)
import Text.Parsec.String
import Text.Parsec.Error
import qualified Text.Parsec as P
-- Parser utilities
-- | A nicer way to print an error message
printLineParserErrorMessage :: String -> ParseError -> IO a
printLineParserErrorMessage input err = do
putStrLn (lineParserErrorMessage input err)
exitFailure
lineParserErrorMessage :: String -> ParseError -> String
lineParserErrorMessage input err =
"In " ++ sourceName pos ++ " column " ++ show (sourceColumn pos) ++ ":\n" ++
input ++ "\n" ++
replicate (sourceColumn pos - 1) ' ' ++ "↑" ++
showErrorMessages "or" "unknown parse err" "expecting"
"unexpected" "end of input"
(errorMessages err)
where pos = errorPos err
parseOneLine :: Parser a -> String -> String -> IO a
parseOneLine p name input =
case P.parse p name input of
Left e -> printLineParserErrorMessage input e
Right l -> return l