Ok, with kelp of kqr I finally realized: I should use sepBy
combinator. My final code to parse table with fields declarations:
tableParser :: P.Parser Table
tableParser = do
P.manyTill P.anyChar $ P.stringCI "create table"
P.skipWhile fieldGarbage
n <- P.takeWhile (not . fieldGarbage)
P.skipWhile fieldGarbage
P.char '('
fs <- fieldParser `P.sepBy` P.char ','
P.manyTill P.anyChar (P.char ';')
return $ Table (C.unpack n) fs
parseStr = do
P.skipWhile fieldGarbage
P.takeWhile (not . fieldGarbage)
fieldParser :: P.Parser Field
fieldParser = do
n <- parseStr
t <- parseStr
P.takeTill (\c -> c == ',' || c == ';')
return $ Field (C.unpack n) (C.unpack t)
Thank you all for the help.