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