Haskell Parsec "many" combinator is applied to a parser that accepts an empty string

import Text.ParserCombinators.Parsec

delimiter :: Parser ()
delimiter = do char '|'
               return ()
          <?> "delimiter"


eol :: Parser ()
eol = do oneOf "\n\r"
         return ()
    <?> "end of line"

item :: Parser String
item = do entry <- manyTill anyChar (try eol <|> try delimiter <|> eof)
          return entry

items :: Parser [String]
items = do result <- many item
           return result

When I run parseTest items "a|b|c"with the above code, I get the following error:

*** Exception: Text.ParserCombinators.Parsec.Prim.many: 
combinator 'many' is applied to a parser that accepts an empty string.

I believe that this has something to do with eofand many itemif I delete eof, then I can get it to work until the line ends on eof, which makes it kind of useless.

I understand that I can just use it sepBy, but I'm interested in why this code does not work and how to make it work.

+4
source share
2 answers

, many, , , : ? ...

, many item . A item manyTill. (: Btw, manyTill

item :: Parser String
item = manyTill anyChar (eol <|> delimiter <|> eof)

do return, try, .) manyTill , eol, a delimiter, eof. eol delimiter , , eof . eof , . ,

ghci> parseTest (do { eof; eof }) ""
()

item ( ) .

, - sepBy, item ( eof ) item ( eof ).

+6

, many emptyString

0

All Articles