{-|
This module exports the underlying Attoparsec row parser. This is helpful if
you want to do some ad-hoc CSV string parsing.
-}moduleData.CSV.Enumerator.Parser(parseCSV,parseRow,row,csv)whereimportControl.ApplicativeimportControl.Monad(mzero,mplus,foldM,when,liftM)importControl.Monad.IO.Class(liftIO,MonadIO)importqualifiedData.ByteStringasBimportqualifiedData.ByteString.Char8asB8importData.ByteString.Char8(ByteString)importData.ByteString.Internal(c2w)importqualifiedData.MapasMimportData.AttoparsecasPhiding(take)importqualifiedData.Attoparsec.Char8asC8importData.Word(Word8)importData.CSV.Enumerator.Types-------------------------------------------------------------------------------- | Try to parse given string as CSVparseCSV::CSVSettings->ByteString->EitherString[Row]parseCSVs=parseOnly$csvs-------------------------------------------------------------------------------- | Try to parse given string as 'Row'parseRow::CSVSettings->ByteString->EitherString(MaybeRow)parseRows=parseOnly$rows-------------------------------------------------------------------------------- | Parse CSVcsv::CSVSettings->Parser[Row]csvs=dor<-rowsend<-atEndcaseendofTrue->caserofJustx->return[x]Nothing->return[]False->dorest<-csvsreturn$caserofJustx->x:restNothing->rest-------------------------------------------------------------------------------- | Parse a CSV rowrow::CSVSettings->Parser(MaybeRow)rowcsvs=csvrowcsvs<|>badrowbadrow::Parser(MaybeRow)badrow=P.takeWhile(not.C8.isEndOfLine)*>(C8.endOfLine<|>C8.endOfInput)*>returnNothingcsvrow::CSVSettings->Parser(MaybeRow)csvrowc=letrowbody=(quotedField'<|>(fieldc))`sepBy`C8.char(csvSepc)properrow=rowbody<*(C8.endOfLine<|>P.endOfInput)quotedField'=casecsvQuoteCharcofNothing->mzeroJustq'->try(quotedFieldq')indores<-properrowreturn$Justresfield::CSVSettings->ParserFieldfields=P.takeWhile(isFieldChars)isFieldChars=notInClassxs'wherexs=csvSeps:"\n\r"xs'=casecsvQuoteCharsofNothing->xsJustx->x:xsquotedField::Char->ParserFieldquotedFieldc=letquoted=stringdbl*>returncdbl=B8.pack[c,c]indoC8.charcf<-many(C8.notCharc<|>quoted)C8.charcreturn$B8.packf