Parsec question (new user): unexpected end of input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import Text.ParserCombinators.Parsec

import Control.Monad
import Control.Applicative hiding (many, (<|>))

-- not needed for Parsec 3
instance Applicative (GenParser a b) where
    pure = return
    (<*>) = ap

data Content = ContentData String | ContentTagged String
 deriving Show

-- custom version of notFollowedBy, which allows the test parser
-- to be a bit more general
notFollowedBy' :: Show a => GenParser tok st a -> GenParser tok st ()   
notFollowedBy' p     = try (do{ c <- p; unexpected (show c) }
                              <|> return ()
                           )

manyTill1 :: Show end => Parser a -> Parser end -> Parser [a]
manyTill1 p end = (:) <$> (notFollowedBy' end *> p) <*> manyTill p end 

p :: Parser Content
p = ContentData <$> manyTill1 anyChar (lookAhead tagOpen <|> eof)
    <|> ContentTagged <$> (tagOpen *> manyTill anyChar (lookAhead tagClose) <* tagClose)

tagOpen = char '<'  *> pure ()
tagClose = char '>' *> pure ()

test str = runParser (many p) () "testing!" str