我想解析这样的文件:
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 like the following:
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 行。所以我尝试了几次都没能解决这个问题。
最佳答案
我最好的猜测是 line
中的 whiteSpace
正在消耗换行符。因此,您的整个文件由单个 line
解析器进行解析,而 eol
解析器永远没有机会接触到 "\n"
.尝试将 whiteSpace
替换为 many (char ' ')
并查看是否有帮助。
关于haskell - 秒差距输入意外结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6235369/