stchang / parsack

A basic Parsec-like monadic parser combinator library implementation in Racket.
MIT License
50 stars 10 forks source link

`lookAhead` return a result #15

Closed greghendershott closed 10 years ago

greghendershott commented 10 years ago

I had a situation where I wanted lookAhead to check for something in general, and if found, parse based on specifically what it found. But lookAhead doesn't return its result.

In my app I made a lookAhead*, which does return its result:

;; parse with p and -- unlike Parsack's lookAhead, return result --
;; but never consume input
(define (lookAhead* p)
  (match-lambda
    [(and input (State inp pos))
     (match (p input)
       [(Consumed! (Ok result _ (Msg _ str strs)))
        (Empty (Ok result input (Msg pos inp strs)))]
       [emp emp])]))

I wonder if I should change Parsack's lookAhead to work like this? I believe it would be 100% backward-compatible, and, not have any performance impact -- do you think that's correct?

p.s. I'm still new to using Parsack. If you think I'm doing it wrong, using the lookAhead result, please do let me know. In that case either we should leave lookAhead as-is, or, make the change but have the docs say something discouraging.

p.p.s. If you like the change, I'm happy to make it. Also happy if you'd prefer to make it.

stchang commented 10 years ago

Yes, I see how your lookAhead* is strictly more useful than lookAhead so go ahead and change lookAhead.

stchang commented 10 years ago

And in fact, Haskell's Parsec looks like it behaves like your lookAhead* so if you changed lookAhead we would be consistent.

http://code.haskell.org/parsec3/Text/Parsec/Prim.hs

greghendershott commented 10 years ago

Thanks. Just made a PR for that: https://github.com/stchang/parsack/pull/17

stchang commented 10 years ago

Thanks, merged. 22e39876f6c89c736962cbd8aa97d96295e1852e