summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2015-10-18 20:53:19 -0400
committerDavid Thompson <dthompson2@worcester.edu>2015-10-18 20:53:19 -0400
commit9c9d2802fd191ce4d6684e4b1242b6f5b3f511c2 (patch)
tree13302ad75f6912ad14a15ff0e74cc3d56bc046e1
parentfa6f3ffee42be46a8e1e14762a73fe44504a9796 (diff)
parsers: Add parse-filter.
* syntax-highlight/parsers.scm (parse-filter): New procedure.
-rw-r--r--syntax-highlight/parsers.scm10
1 files changed, 10 insertions, 0 deletions
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."