Advise-me-0.1: src/Main/ParserCSV.hs
-----------------------------------------------------------------------------
-- Copyright 2019, Advise-Me project team. This file is distributed under
-- the terms of the Apache License 2.0. For more information, see the files
-- "LICENSE.txt" and "NOTICE.txt", which are included in the distribution.
-----------------------------------------------------------------------------
-- |
-- Maintainer : bastiaan.heeren@ou.nl
-- Stability : provisional
-- Portability : portable (depends on ghc)
--
-----------------------------------------------------------------------------
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 --v THIS PART WAS COMMENTED v
pQuotedCell = quoted (many (try (char '"' *> char '"') <|> noneOf "\""))
quoted :: Parser a -> Parser a
quoted = between (char '"') (char '"')