haskellparsec

Parsec not parsing newline character


I have the following piece of code:

import Text.ParserCombinators.Parsec
import Control.Applicative hiding ((<|>))
import Control.Monad

data Test = Test Integer Integer deriving Show

integer :: Parser Integer
integer = rd <$> many1 digit
    where rd = read :: String -> Integer

testParser :: Parser Test
testParser = do
  a <- integer
  char ','
  b <- integer
  eol
  return $ Test a b

eol :: Parser Char
eol = char '\n'

main = forever $ do putStrLn "Enter the value you need to parse: "
                    input <- getLine
                    parseTest testParser input

But when I actually try to parse my value in ghci, it doesn't work.

ghci > main
Enter the value you need to parse: 
34,343\n
parse error at (line 1, column 7):
unexpected "\\"
expecting digit or "\n"

Any ideas on what I'm missing here ?


Solution

  • The problem seems to be that you're expecting a newline, but your text doesn't contain one. Change eol to

    import Control.Monad (void)
    
    eol :: Parser ()
    eol = void (char '\n') <|> eof
    

    and it'll work.