-
Notifications
You must be signed in to change notification settings - Fork 0
/
53revisionParsers.hs
45 lines (36 loc) · 1.3 KB
/
53revisionParsers.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import Text.ParserCombinators.Parsec
-- :set -package parsec
-- GenParser context, it reads individual Chars and returns out lists of Strings.
csvFile :: GenParser Char st [[String]]
csvFile = do
result <- many line
eof
return result
-- Each line contains 1 or more cells, separated by a comma
line :: GenParser Char st [String]
line = do
result <- cells
eol
return result
-- Build up a list of cells. Try to parse the first cell, then figure out
-- what ends the cell.
-- this gets all the cells in a line
cells :: GenParser Char st [String]
cells = do
first <- cellContent
next <- remainingCells
return (first : next)
-- The cell either ends with a comma, indicating that 1 or more cells follow,
-- or it doesn't, indicating that we're at the end of the cells for this line
remainingCells :: GenParser Char st [String]
remainingCells =
(char ',' >> cells) -- Found comma? More cells coming
<|> (return []) -- No comma? Return [], no more ce
-- Each cell contains 0 or more characters, which must not be a comma or
-- EOL
cellContent :: GenParser Char st String
cellContent =
many (noneOf ",\n")
-- The end of line character is \n
eol :: GenParser Char st Char
eol = char '\n'