parsec中意外的输入结束

Jam*_*mes 5 haskell parsec

我想解析这样的文件:

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
Run Code Online (Sandbox Code Playgroud)

但是在运行时会引发异常:

ts <- getTestData "data" 
*** Exception: "test data" (line 11, column 1):
unexpected end of input
expecting natural or "\n"
Run Code Online (Sandbox Code Playgroud)

我不明白,为什么它说第11行,当我的data.test文件只有10行.所以几次尝试后我都没能解决这个问题.

luq*_*qui 5

我最好的猜测是,whiteSpaceline被消费的换行.所以整个文件都由一个line解析器解析,eol解析器永远不会有机会获得它"\n".尝试替换whiteSpace,many (char ' ')看看是否有帮助.