Search code examples
haskellparsec

Parse string by semicolon using parsec


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.


Solution

  • 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 ';'