parsack icon indicating copy to clipboard operation
parsack copied to clipboard

"between X and Y occurences of p"

Open Arteneko opened this issue 4 years ago • 1 comments

I was wondering if there was a clean way to write a parser that would do "between x and y occurrences of p".

Basically, to allow, for example, "between 1 and 5 spaces".

Arteneko avatar May 18 '21 07:05 Arteneko

If you still need it, I'm using the following:

(define (ntimes p from (to from))
  (cond [(> from 0) (>>= p
                         (λ (x)
                             (>>= (ntimes p (- from 1) (- to 1))
                                  (λ (y)
                                    (return (cons x y))))))]
        [(> to 0) (<any> (>>= p
                              (λ (x)
                                (>>= (ntimes p 0 (- to 1))
                                     (λ (y) (return (cons x y))))))
                         (return '()))]
        [else (return '())]))

Unfortunately, I don't have the time right now to make a proper pull request (with docs and tests and a more generic interface), but I might make one when I do.

JwanMan avatar Jun 06 '23 00:06 JwanMan