Parsec, comma sperated list with special last element

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Parsec, comma sperated list with special last element

Christian Maeder-2
Hi,

you should not distinguish last or other cells.
Do you really want to discard the last cell in your result?

You could use:

   cell = many1 $ noneOf "\n,"

   endBy cell (oneOf "\n,")

This would give you all cells of a file.

To get all cells of a single line use:

   line = sepBy cell (char ',')

and skip the final newline. To get all lines of a file
use:

   endBy line (char `\n')

HTH Christian


you wrote: 2012-12-18 08:58:45 GMT

I tried

file = do
   res <- endBy (try cell) (char ',')
   l   <- lastCell
   eof
   return res

cell = many1 (noneOf ",")
lastCell = many1 (noneOf "\n")

But that does not work because cell succeeds on the last cell.
I can replace the endBy by

many (try $ do {a <- cell; string ","; return a})

Nathan