1. Moritz Heidkamp
  2. comparse


Moritz Heidkamp  committed 6a1c84a

Rename last-of to preceded-by and make followed-by variadic

  • Participants
  • Parent commits 0e73065
  • Branches master

Comments (0)

Files changed (2)

File comparse.scm

View file
  • Ignore whitespace
  fail result item bind satisfies is char-seq maybe
  sequence sequence*
  repeated zero-or-more one-or-more
- any-of all-of none-of last-of
+ any-of all-of none-of
  preceded-by followed-by
         (and (not ((car parsers) input))
              (loop (cdr parsers))))))
-(define (last-of parser . parsers)
+(define (preceded-by parser . parsers)
   (let loop ((parsers (cons parser parsers)))
     (bind (car parsers)
           (lambda (value)
                 (result value)
                 (loop (cdr parsers)))))))
-(define (preceded-by parser preceeding . more-preceeding)
-  (let loop ((preceeding (cons preceeding more-preceeding)))
-    (bind (car preceeding)
-          (lambda (value)
-            (if (null? (cdr preceeding))
-                parser
-                (loop (cdr preceeding)))))))
-(define ((followed-by parser following) input)
-  (let ((value (parser input)))
-    (and value (following (cdr value)) value)))
+(define ((followed-by parser . following) input)
+  (and-let* ((value (parser input)))
+    (let loop ((following following) (input (cdr value)))
+      (if (null? following)
+          value
+          (and-let* ((value ((car following) input)))
+            (loop (cdr following) (cdr value)))))))
 (define (->parser object)
   (cond ((procedure? object) object)

File tests/run.scm

View file
  • Ignore whitespace
 (test-parse* "   " (repeated (is #\space) max: 3) "      ")
 (test-parse* "" (repeated (is #\f)) "x")
 (test-parse* #f (repeated (is #\a) min: 1) "b")
-(test-parse #\a (last-of (none-of (is #\b) (is #\c)) item) "a")
+(test-parse #\a (preceded-by (none-of (is #\b) (is #\c)) item) "a")
 (test-parse* "b52" (zero-or-more (any-of (satisfies char-set:digit) (is #\b))) "b52s")
 (test-parse #f (none-of (is #\b) (is #\a)) "a")
 (define (quoted-string #!key
                        (delimiter (satisfies (char-set #\" #\')))
                        (escape (is #\\)))
-  (let ((escaped-char (last-of escape item)))
+  (let ((escaped-char (preceded-by escape item)))
     (sequence* ((_ (zero-or-more (satisfies char-set:whitespace)))
                 (actual-delimiter delimiter)
                 (chars (zero-or-more
                         (any-of escaped-char
-                                (last-of (none-of escape (is actual-delimiter))
-                                         item))))
+                                (preceded-by (none-of escape (is actual-delimiter))
+                                             item))))
                 (_ (is actual-delimiter)))
       (result (list->string chars)))))
   (test-assert (not (parse singly-quoted-bang-string "\"check\""))))
 (test-group "followed-by"
-  (let ((lol (last-of (followed-by item (char-seq "ol")) item)))
+  (let ((lol (preceded-by (followed-by item (is #\o) (is #\l)) item)))
     (test #\o (parse lol "lol"))
     (test #f  (parse lol "lxl"))))
 (test-group "preceded-by"
-  (test-parse 3 (preceded-by (is 3) (is 1) (is 2)) '(1 2 3)))
+  (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-group "all-of"
   (test-parse #\b (all-of (none-of (is #\a)) (is #\b)) "b")
   (test-parse #f (all-of (none-of (is #\a)) (is #\b)) "a"))