Using the Parsec library

Jan Kort kort@science.uva.nl
Tue, 10 Sep 2002 14:56:57 +0200


Jose Romildo Malaquias wrote:
> Any sugestion on how to implement the 'not starting with
> "A:" or "B:"' problem?
> 

Hi,

I'm not familiar with Parsec, but in ParseLib it's
fairly straightforward, first you add a new combinator:

ifnot :: Parser a -> Parser b -> Parser b
ifnot (P p1) (P p2) =
 P ( \s -> if parseOk (p1 s) then []
                             else p2 s
   )
  where
    parseOk :: [(a,String)] -> Bool
    parseOk [x] = True
    parseOk _   = False

then you can say:

  ifnot parse_a_or_b parse_foo

You will need the sources of ParseLib to try it out
though, because "P" isn't exported.


  Jan