[Haskell-cafe] simple parsec question
Andrey Chudnov
achudnov at gmail.com
Sun Mar 3 17:12:26 CET 2013
Immanuel,
Since a heading always starts with a new line (and ends with a colon
followed by a carriage return or just a colon?), I think it might be
useful to first separate the input into lines and then classify them
depending on whether it's a heading or not and reassemble them into the
value you need. You don't even need parsec for that.
However, if you really want to use parsec, you can write something like
(warning, not tested):
many $ liftM2 Section headline content
where headline = anyChar `manyTill` (char ':' >> spaces >> newline)
content = anyChar `manyTill` (try $ newline >> headline)
/Andrey
On 3/3/2013 10:44 AM, Immanuel Normann wrote:
> I am trying to parse a semi structured text with parsec that basically
> should identify sections. Each section starts with a headline and has
> an unstructured content - that's all. For instance, consider the
> following example text (inside the dashed lines):
>
> ---------------------------
>
> top 1:
>
> some text ... bla
>
> top 2:
>
> more text ... bla bla
>
>
> ---------------------------
>
> This should be parsed into a structure like this:
>
> [Section (Top 1) (Content "some text ... bla"), Section (Top 1)
> (Content "more text ... bla")]
>
> Say, I have a parser "headline", but the content after a headline
> could be anything that is different from what "headline" parses.
> How could the "section" parser making use of "headline" look like?
> My idea would be to use the "manyTill" combinator, but I don"t find an
> easy solution.
More information about the Haskell-Cafe
mailing list