display cleaned, ready to split into multiple files
This commit is contained in:
39
CSVParser.hs
Normal file
39
CSVParser.hs
Normal file
@@ -0,0 +1,39 @@
|
||||
module CSVParser (
|
||||
parseCSV
|
||||
)where
|
||||
|
||||
|
||||
import Text.ParserCombinators.Parsec
|
||||
|
||||
csvFile :: GenParser Char st [[String]]
|
||||
csvFile = do
|
||||
result <- many line
|
||||
eof
|
||||
return result
|
||||
|
||||
line :: GenParser Char st [String]
|
||||
line = do
|
||||
result <- cells
|
||||
_ <- eol
|
||||
return result
|
||||
|
||||
cells :: GenParser Char st [String]
|
||||
cells = do
|
||||
first <- cellContent
|
||||
next <- remainingCells
|
||||
return (first : next)
|
||||
|
||||
remainingCells :: GenParser Char st [String]
|
||||
remainingCells =
|
||||
(char ',' >> cells) -- Found comma? More cells coming
|
||||
<|> return [] -- No comma? Return [], no more cells
|
||||
|
||||
cellContent :: GenParser Char st String
|
||||
cellContent =
|
||||
many (noneOf ",\n")
|
||||
|
||||
eol :: GenParser Char st Char
|
||||
eol = char '\n'
|
||||
|
||||
parseCSV :: String -> Either ParseError [[String]]
|
||||
parseCSV input = parse csvFile "(unknown)" input
|
||||
Reference in New Issue
Block a user