Commits

Moritz Heidkamp  committed 35da6da

Allow a list to be passed to vararg functions alternatively

  • Participants
  • Parent commits 5b03032

Comments (0)

Files changed (2)

File comparse.scm

            (lambda (binding)
              (sequence* (more-bindings ...) body ...))))))
 
+(define (parser-list parser more-parsers)
+  (if (and (list? parser) (null? more-parsers))
+      parser
+      (cons parser more-parsers)))
+
 (define ((sequence parser . parsers) input)
-  (let loop ((parsers (cons parser parsers))
+  (let loop ((parsers (parser-list parser parsers))
              (parts '())
              (input input))
     (if (null? parsers)
                (eq? (lazy-head input) (string-ref str pos))
                (loop (+ pos 1) (lazy-tail input)))))))
 
+;; Allow (any-of (list parser ...)) alternatively?
 (define ((any-of parser . parsers) input)
-  (let loop ((parsers (cons parser parsers)))
+  (let loop ((parsers (parser-list parser parsers)))
     (and (not (null? parsers))
          (or ((car parsers) input)
              (loop (cdr parsers))))))
 
 (define ((all-of parser . parsers) input)
-  (let loop ((parsers (cons parser parsers)))
+  (let loop ((parsers (parser-list parser parsers)))
     (and-let* ((value ((car parsers) input)))
       (if (null? (cdr parsers))
           value
           (and value (loop (cdr parsers)))))))
 
 (define ((none-of parser . parsers) input)
-  (let loop ((parsers (cons parser parsers)))
+  (let loop ((parsers (parser-list parser parsers)))
     (if (null? parsers)
         (cons #t input)
         (and (not ((car parsers) input))
              (loop (cdr parsers))))))
 
 (define (preceded-by parser . parsers)
-  (let loop ((parsers (cons parser parsers)))
+  (let loop ((parsers (parser-list parser parsers)))
     (bind (car parsers)
           (lambda (value)
             (if (null? (cdr parsers))

File tests/run.scm

   (test-parse 3 (preceded-by (is 1) (is 2) (is 3)) '(1 2 3)))
 
 (test-group "sequence"
-  (test-parse* "ab" (sequence (is #\a) (is #\b)) "abc"))
+  (test-parse* "ab" (sequence (is #\a) (is #\b)) "abc")
+  (test-parse* "ab" (sequence (list (is #\a) (is #\b))) "abc"))
 
 (test-group "maybe"
   (let ((foo (preceded-by (maybe (is #\x)) (char-seq "foo"))))