我使用以下方法编写了以下解析代码attoparsec
:
data Test = Test {
a :: Int,
b :: Int
} deriving (Show)
testParser :: Parser Test
testParser = do
a <- decimal
tab
b <- decimal
return $ Test a b
tParser :: Parser [Test]
tParser = many' $ testParser <* endOfLine
这适用于小型文件,我执行它是这样的:
main :: IO ()
main = do
text <- TL.readFile "./testFile"
let (Right a) = parseOnly (manyTill anyChar endOfLine *> tParser) text
print a
但是当文件的大小大于 70MB 时,它会消耗大量的内存。作为解决方案,我想我会使用attoparsec-conduit
. 在浏览了他们的API之后,我不确定如何让它们一起工作。我的解析器具有类型Parser Test
,但它sinkParser
实际上接受 type 的解析器Parser a b
。我对如何在常量内存中执行这个解析器感兴趣?(基于管道的解决方案也是可以接受的,但我不习惯 Pipes API。)