[Haskell-cafe] Parsec question (new user): unexpected end of input
aslatter at gmail.com
Wed Sep 29 00:23:01 EDT 2010
On Tue, Sep 28, 2010 at 10:35 PM, Peter Schmitz <ps.haskell at gmail.com> wrote:
> I am a new Parsec user, and having some trouble with a relatively
> simple parser.
> The grammar I want to parse contains tags (not html) marked by
> angle brackets (e.g., "<some tag>"), with arbitrary text (no angle
> brackets allowed) optionally in between tags.
> Tags may not nest, but the input must begin and end with a tag.
> Whitespace may occur anywhere (beginning/end of input,
> inside/between tags, etc.), and is optional.
> I think my problem may be a lack of using "try", but I'm not sure
> At runtime I get:
> Error parsing file: "...\sampleTaggedContent.txt" (line 4, column 1):
> unexpected end of input
> expecting "<"
> The input was:
> <tag1>stuff<tag 2>
> more stuff < tag 3 > even more
> The code is below. (I'm using Parsec-126.96.36.199.) I don't really want
> to return anything meaningful yet; just parse okay.
> Any advice about the error (or how to simplify or improve the code)
> would be appreciated.
> Thanks much,
> -- Peter
>> -- Parsers:
>> taggedContent = do
>> many tagOrContent
>> return "Parse complete."
>> tagOrContent = aTag <|> someContent <?> "tagOrContent"
>> aTag = do
>> xs <- many (noneOf [tagEndChar])
>> 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 = '>'
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
Here's something I put together:
It doesn't have the whitespace handling you want.
The big difference in what I did was that when parsing content, it
needs to stop on EOF as well as the signal char. Otherwise it won't
allow the document to end :-)
More information about the Haskell-Cafe