Anonymous committed 6f3497f

Revert to actually working implementation of sequence

  • Participants
  • Parent commits 3fc510a

Comments (0)

Files changed (2)

File comparse.scm

 (define ((sequence parser . parsers) input)
   (let loop ((parsers (cons parser parsers))
+             (parts '())
              (input input))
-    (if (null? (cdr parsers))
-        (car parsers)
+    (if (null? parsers)
+        (cons (reverse parts) input)
         (and-let* ((value ((car parsers) input)))
-          (loop (cdr parsers) (cdr value))))))
+          (loop (cdr parsers)
+                (cons (car value) parts)
+                (cdr value))))))
 (define ((char-seq str) input)
   (let ((len (string-length str)))

File tests/run.scm

     ((_ expect parser input)
      (test-parse expect (as-string parser) input))))
-(test-parse* "aaba" (repeatedly (satisfies (char-set #\a #\b))) "aabac")
-(test-parse* "   " (repeatedly (is #\space) max: 3) "      ")
-(test-parse* "" (repeatedly (is #\f)) "x")
-(test-parse* #f (repeatedly (is #\a) min: 1) "b")
+(test-parse* "aaba" (repeated (satisfies (char-set #\a #\b))) "aabac")
+(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* "b52" (zero-or-more (any-of (satisfies char-set:digit) (is #\b))) "b52s")
   (test "ok\\" (parse singly-quoted-bang-string "'ok\\'"))
   (test-assert (not (parse singly-quoted-bang-string "\"check\""))))
-(let ((lol (last-of (followed-by item (char-seq "ol")) item)))
-  (test #\o (parse lol "lol"))
-  (test #f  (parse lol "lxl")))
+(test-group "followed-by"
+  (let ((lol (last-of (followed-by item (char-seq "ol")) 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-group "sequence"
   (test-parse* "ab" (sequence (is #\a) (is #\b)) "abc"))
-;; (test-group "repeatedly"
-;;   (test-parse* "hohoho" (repeatedly (char-seq "ho")) "hohoho")
-;;   (test-parse* "ho    ho ho"
-;;                (repeatedly (sequence (char-seq "ho") (zero-or-more (is #\space))))
-;;                ""))
+(test-group "repeated"
+  (test-parse* "hohoho" (repeated (char-seq "ho")) "hohoho")
+  (test-parse* "ho    ho ho "
+               (repeated (sequence (char-seq "ho")
+                                     (zero-or-more (is #\space)))
+                           min: 2)
+               "ho    ho ho rofl"))