summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2015-10-19 09:10:31 -0400
committerDavid Thompson <dthompson2@worcester.edu>2015-10-19 09:10:31 -0400
commitf9dae95bd0f1434aa56fd8de259325b9742e28e2 (patch)
tree56abf1d128f18e3f0a60f58d6022abf7ec1a9fcb
parented72c28cd11726dd2e3f547aa29157b69fe5eaff (diff)
parsers: Add parse-maybe.
* syntax-highlight/parsers.scm (parse-maybe): New procedure.
-rw-r--r--syntax-highlight/parsers.scm11
1 files changed, 11 insertions, 0 deletions
diff --git a/syntax-highlight/parsers.scm b/syntax-highlight/parsers.scm
index 31c17ae..5d08e45 100644
--- a/syntax-highlight/parsers.scm
+++ b/syntax-highlight/parsers.scm
@@ -38,6 +38,7 @@
parse-both
parse-any
parse-each
+ parse-maybe
parse-many
parse-string
parse-char-set
@@ -117,6 +118,16 @@ parser fails without consuming any input if any parser in PARSERS
fails."
(fold-right parse-both (parse-return '()) parsers))
+(define (parse-maybe parser)
+ "Create a parser that always succeeds, but tries to use PARSER. If
+PARSER succeeds, its result is returned, otherwise the empty string is
+returned without consuming any input."
+ (lambda (stream)
+ (let-values (((result remaining) (parser stream)))
+ (if result
+ (values result remaining)
+ (values "" stream)))))
+
(define (parse-many parser)
"Create a parser that uses PARSER as many times as possible until it
fails and return the results of each successful parse in a list. This