[Haskell-cafe] Greedy Lazy Possessive

Chris Kuklewicz haskell at list.mightyreason.com
Tue Aug 29 11:40:45 EDT 2006

How to use parsec to match optional things.

Lets consider regex style matching of greedy a?b,
lazy a??b, and possessive a?+b.

And consider greedy a*b, lazy a*?b, and possessive a*+b.

Then I think these examples (cribbed from my module) will work:

-- Building blocks
greedyOpt     p contFail contSuccess = try (p >> contSuccess) <|> contFail
lazyOpt       p contFail contSuccess = try contFail <|> (p >> contSuccess)
possessiveOpt p contFail contSuccess = ((try p) >> contSuccess) <|> contFail

-- Match p*cont p*?cont p*+cont
greedyStar     p cont = fix (greedyOpt     p cont)
lazyStar       p cont = fix (lazyOpt       p cont)
possessiveStar p cont = fix (possessiveOpt p cont)

-- Match p?cont p??cont p?+cont
greedyQuest     p cont = greedyOpt     p cont cont
lazyQuest       p cont = lazyOpt       p cont cont
possessiveQuest p cont = possessiveOpt p cont cont

Altering p to return a useful value is left to the user.

More information about the Haskell-Cafe mailing list