Commits

Moritz Heidkamp  committed 3b65c28

Add optional default arg to maybe parser

  • Participants
  • Parent commits 8623b86

Comments (0)

Files changed (2)

File comparse.scm

         (else
          (repeated* parser (car args) (car args)))))
 
-(define (maybe parser)
-  (any-of parser (result #f)))
+(define (maybe parser #!optional default)
+  (any-of parser (result default)))
 
 (define (as-string parser)
   (sequence* ((parts parser))

File tests/run.scm

 (test-group "maybe"
   (let ((foo (preceded-by (maybe (is #\x)) (char-seq "foo"))))
     (test-parse* "foo" foo "foo")
-    (test-parse* "foo" foo "xfoo")))
+    (test-parse* "foo" foo "xfoo")
+    (test-parse* #f foo "bar")
+    (test-parse* "ok" (maybe (is #\x) "ok") "y")))
 
 (test-group "repeated"
   (test-parse* "hohoho" (repeated (char-seq "ho")) "hohoho")