diff options
-rw-r--r-- | parser-combinators.scm | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/parser-combinators.scm b/parser-combinators.scm index 4a3a3f8..e0522dd 100644 --- a/parser-combinators.scm +++ b/parser-combinators.scm @@ -46,6 +46,7 @@ parse-each parse-zero-or-more parse-one-or-more + parse-up-to parse-maybe parse)) @@ -158,6 +159,18 @@ parsers succeed." (($ <parse-result> value stream) (loop stream (cons value result))))))) +(define (parse-up-to n parser) + "Parse using PARSER at most N times." + (lambda (stream) + (let loop ((stream stream) + (m 0)) + (if (= m n) + '() + (match (parser stream) + ((? parse-failure?) '()) + (($ <parse-result> value stream) + (cons value (loop stream (1+ m))))))))) + (define* (parse-maybe parser #:optional (default #f)) (lambda (stream) (match (parser stream) |