summaryrefslogtreecommitdiff
path: root/syntax-highlight
diff options
context:
space:
mode:
Diffstat (limited to 'syntax-highlight')
-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