1
votes

This is one of my task. Parser getList suppose to work next way

GHCi> parseTest getList "1;234;56"
["1","234","56"]
GHCi> parseTest getList "1;234;56;"
parse error at (line 1, column 10):
unexpected end of input
expecting digit
GHCi> parseTest getList "1;;234;56"
parse error at (line 1, column 3):
unexpected ";"
expecting digit

My solution getList = many digit `sepBy1` char ';' is working like this

*Main> test1
["1","234","56"]
*Main> test2
["1","234","56",""]
*Main> test3
["1","","234","56"]

It's not correct, I can't figure out how to deal with double-quoted cases.

3
Just filter empty strings out? - arrowd
It supposed to be parser solution only and should works exactly as in first box. - StalkAlex
many digit `sepBy` (many1 $ char ';') then? - arrowd
Nope, it's not giving errors in second and third cases. - StalkAlex
I don't get it. You need to parse double ; or you need to error out on them? - arrowd

3 Answers

2
votes

Your problem what many digit it's parser which accepts zero or more digits. You should use, for example, many1 digit. So all together:

getList = many1 digit `sepBy` char ';'
1
votes

Using megaparsec (which I recommend over parsec):

getList = some digitChar `sepBy1` char ';'

This means "at least one digit, at least one time, separated by semicolons". Note that I'm using some where your attempt used many.

ghci> parse getList "" "123;456"
Right ["123", "456"]
ghci> parse getList "" "123;;456"
Left (ParseError {errorPos = SourcePos {sourceName = "", sourceLine = Pos 1, sourceColumn = Pos 5} :| [], errorUnexpected = fromList [Tokens (';' :| "")], errorExpected = fromList [Label ('d' :| "igit")], errorCustom = fromList []})
0
votes

Try this:

getList = do
    many1 digit
    many $ do
       char ';'
       many1 digit