From f9dae95bd0f1434aa56fd8de259325b9742e28e2 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Mon, 19 Oct 2015 09:10:31 -0400 Subject: parsers: Add parse-maybe. * syntax-highlight/parsers.scm (parse-maybe): New procedure. --- syntax-highlight/parsers.scm | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'syntax-highlight/parsers.scm') 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 -- cgit v1.2.3