From 9c9d2802fd191ce4d6684e4b1242b6f5b3f511c2 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sun, 18 Oct 2015 20:53:19 -0400 Subject: parsers: Add parse-filter. * syntax-highlight/parsers.scm (parse-filter): New procedure. --- syntax-highlight/parsers.scm | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'syntax-highlight/parsers.scm') diff --git a/syntax-highlight/parsers.scm b/syntax-highlight/parsers.scm index 5349296..bb967db 100644 --- a/syntax-highlight/parsers.scm +++ b/syntax-highlight/parsers.scm @@ -33,6 +33,7 @@ parse-lift parse-never parse-map + parse-filter parse-either parse-both parse-any @@ -77,6 +78,15 @@ "Return a new parser that applies PROC to result of PARSER." (parse-bind (parse-lift proc) parser)) +(define (parse-filter predicate parser) + "Create a new parser that succeeds when PARSER is successful and +PREDICATE is satisfied with the result." + (lambda (stream) + (let-values (((result remaining) (parser stream))) + (if (and result (predicate result)) + (values result remaining) + (parse-fail stream))))) + (define (parse-either first second) "Create a parser that tries to parse with FIRST or, if that fails, parses SECOND." -- cgit v1.2.3