책 읽다가 생소한 게 나와서 적어 본다.
패키지 attoparsec
에 있는 함수 parseOnly
는 청크 한 개만 다룰 수 있고 나머지 청크는 버린다. 이러면 스트림 데이터를 읽을 수가 없다. 이때 모듈 Data.Attoparsec.ByteString.Run
에 있는 다음 도구를 사용하면 된다.
parseAndRestore :: Monad m =>
RestorableInput m ByteString -> Parser a -> m (Either ParseError a)
data RestorbleInput m i = RestorableInput
(m i) -- ^ Get the next chunk of input, or an empty string if there is no more
(i -> m ()) -- ^ Push a nonempty chunk of input back to the input stream
newRestorableIO
:: IO i -- ^ Get the next chunk of input, or an empty string if there is no more
-> IO (RestorableInput IO i)
RestorableInput IO ByteString
은 ByteString
청크 스트림을 표현한다. 이게 일종의 가변 상태를 가지고 있다. 이 가변 상태가 있기 때문에 앞에서 읽었지만 소모되지 않은 청크를 버리지 않고 입력에 다시 넣는 방식을 쓰는 것 같다.
이런 방식을 Incremental parsing이라고 하는 것 같다.