I want to parse the file as follows:
66: 3 3: 4
329: 2
101: 3
495: 4
55: 5
268: 5
267: 2
242: 4
262: 1
861: 1
My code is as follows:
getTestData :: String -> IO [[(Int, Int)]]
getTestData name = do
--res <- parseFromFile testData (name ++ ".test")
fc <- readFile (name ++ ".test")
let res = parse testData "test data" fc
case res of
Left e -> error $ show e-- "test data parse eror."
Right ts -> return ts
eol = char '\n'
testData = endBy line eol
--testData = many line
testTuple = do
i <- natural
colon
r <- natural
return (fromIntegral i:: Int, fromIntegral r:: Int)
line = sepBy testTuple whiteSpace
But at startup, it throws an exception:
ts <- getTestData "data"
*** Exception: "test data" (line 11, column 1):
unexpected end of input
expecting natural or "\n"
I don't understand why he said line 11 when my data.test file has only 10 lines. Therefore, I could not solve this problem after several attempts.
source
share