-- |-- Module : Data.Attoparsec.ByteString.Lazy-- Copyright : Bryan O'Sullivan 2010, 2011-- License : BSD3---- Maintainer : bos@serpentine.com-- Stability : experimental-- Portability : unknown---- Simple, efficient combinator parsing that can consume lazy-- 'ByteString' strings, loosely based on the Parsec library.---- This is essentially the same code as in the 'Data.Attoparsec'-- module, only with a 'parse' function that can consume a lazy-- 'ByteString' incrementally, and a 'Result' type that does not allow-- more input to be fed in. Think of this as suitable for use with a-- lazily read file, e.g. via 'L.readFile' or 'L.hGetContents'.---- /Note:/ The various parser functions and combinators such as-- 'string' still expect /strict/ 'B.ByteString' parameters, and-- return strict 'B.ByteString' results. Behind the scenes, strict-- 'B.ByteString' values are still used internally to store parser-- input and manipulate it efficiently.moduleData.Attoparsec.ByteString.Lazy(Result(..),moduleData.Attoparsec.ByteString-- * Running parsers,parse,parseTest-- ** Result conversion,maybeResult,eitherResult)whereimportControl.DeepSeq(NFData(rnf))importData.ByteString.Lazy.Internal(ByteString(..),chunk)importqualifiedData.ByteStringasBimportqualifiedData.Attoparsec.ByteStringasAimportqualifiedData.Attoparsec.Internal.TypesasTimportData.Attoparsec.ByteStringhiding(IResult(..),Result,eitherResult,maybeResult,parse,parseWith,parseTest)-- | The result of a parse.dataResultr=FailByteString[String]String-- ^ The parse failed. The 'ByteString' is the input-- that had not yet been consumed when the failure-- occurred. The @[@'String'@]@ is a list of contexts-- in which the error occurred. The 'String' is the-- message describing the error, if any.|DoneByteStringr-- ^ The parse succeeded. The 'ByteString' is the-- input that had not yet been consumed (if any) when-- the parse succeeded.instanceNFDatar=>NFData(Resultr)wherernf(Failbsctxsmsg)=rnfBSbs`seq`rnfctxs`seq`rnfmsgrnf(Donebsr)=rnfBSbs`seq`rnfr{-# INLINE rnf #-}rnfBS::ByteString->()rnfBS(Chunk_xs)=rnfBSxsrnfBSEmpty=(){-# INLINE rnfBS #-}instanceShowr=>Show(Resultr)whereshow(Failbsstkmsg)="Fail "++showbs++" "++showstk++" "++showmsgshow(Donebsr)="Done "++showbs++" "++showrfmapR::(a->b)->Resulta->ResultbfmapR_(Failststkmsg)=FailststkmsgfmapRf(Donebsr)=Donebs(fr)instanceFunctorResultwherefmap=fmapR-- | Run a parser and return its result.parse::A.Parsera->ByteString->Resultaparseps=casesofChunkxxs->go(A.parsepx)xsempty->go(A.parsepB.empty)emptywherego(T.Failxstkmsg)ys=Fail(chunkxys)stkmsggo(T.Donexr)ys=Done(chunkxys)rgo(T.Partialk)(Chunkyys)=go(ky)ysgo(T.Partialk)empty=go(kB.empty)empty-- | Run a parser and print its result to standard output.parseTest::(Showa)=>A.Parsera->ByteString->IO()parseTestps=print(parseps)-- | Convert a 'Result' value to a 'Maybe' value.maybeResult::Resultr->MaybermaybeResult(Done_r)=JustrmaybeResult_=Nothing-- | Convert a 'Result' value to an 'Either' value.eitherResult::Resultr->EitherStringreitherResult(Done_r)=RightreitherResult(Fail__msg)=Leftmsg