module Main.ParserCSV (readFileCSV, CSV, Row, Cell) where
import Control.Monad
import Ideas.Utils.Parsing
type CSV = [Row]
type Row = [Cell]
type Cell = String
readFileCSV :: FilePath -> IO CSV
readFileCSV = readFile >=> parseCSV
parseCSV :: Monad m => String -> m CSV
parseCSV = either fail return . parseSimple pCSV
pCSV :: Parser CSV
pCSV = many pRow
pRow :: Parser Row
pRow = pCell `sepBy` (char ';' <|> char ',') <* optionMaybe (char '\r') <* char '\n'
pCell :: Parser Cell
pCell = pQuotedCell
<|> many (noneOf ",;\r\n\"")
pQuotedCell :: Parser Cell
pQuotedCell = quoted (many (try (char '"' *> char '"') <|> noneOf "\""))
quoted :: Parser a -> Parser a
quoted = between (char '"') (char '"')