我想解析这样的文件:
66:3 3:4 329:2 101:3 495:4 55:5 268:5 267:2 242:4 262:1 861:1
我的代码如下:
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
但是在运行时,它会抛出异常:
ts <- getTestData "data"
*** Exception: "test data" (line 11, column 1):
unexpected end of input
expecting natural or "\n"
我不明白,为什么它说第 11 行,而我的 data.test 文件只有 10 行。所以我在几次尝试后都没有解决这个问题。