[Haskell-cafe] Re: Parsec question (new user): unexpected end of input

Christian Maeder Christian.Maeder at dfki.de
Wed Sep 29 07:39:48 EDT 2010


Am 29.09.2010 11:55, schrieb Christian Maeder:
> Am 29.09.2010 09:54, schrieb Christian Maeder:
>> Am 29.09.2010 05:35, schrieb Peter Schmitz:
>> [...]
>>> Error parsing file: "...\sampleTaggedContent.txt" (line 4, column 1):
>>> unexpected end of input
>>> expecting "<"
>>>
>>> The input was:
>> [...]
>>>
>>>> -- Parsers:
>>>> taggedContent = do
>>>>    optionalWhiteSpace
>>>>    aTag
>>>>    many tagOrContent
>>>>    aTag
>>
>> "many tagOrContent" will consume all tags, so that no tag for the
>> following "aTag" will be left.
> 
> if you want to match a final tag, you could try:
> 
>   manyTill tagOrContent (try (aTag >> eof))

better yet, avoiding backtracking, return different things for aTag and
someContents and check if the last entry is a tag.

  tagOrContent = fmap Left aTag <|> fmap Right someContent

  taggedContent = do
   spaces
   aTag
   l <- many tagOrContent
   eof
   case reverse l of
     Left _ : _ -> return ()
     _ -> fail "expected final tag before EOF"

C.

>>
>> Cheers Christian
>>
>>>>    eof
>>>>    return "Parse complete."
>>>>
>>>> tagOrContent = aTag <|> someContent <?> "tagOrContent"
>>>>
>>>> aTag = do
>>>>    tagBegin
>>>>    xs <- many (noneOf [tagEndChar])
> 
> this also looks like "manyTill anyChar tagEnd"
> 
> C.
> 
>>>>    tagEnd
>>>>    optionalWhiteSpace
>>>>    return ()
>>>>
>>>> someContent = do
>>>>    manyTill anyChar tagBegin
>>>>    return ()
>>>>
>>>> optionalWhiteSpace = spaces   -- i.e., any of " \v\f\t\r\n"
>>>> tagBegin = char tagBeginChar
>>>> tagEnd = char tagEndChar
>>>>
>>>> -- Etc:
>>>> tagBeginChar = '<'
>>>> tagEndChar = '>'
>>>
>>> --------


More information about the Haskell-Cafe mailing list