peg: cleanups
authorAndy Wingo <wingo@pobox.com>
Thu, 17 Feb 2011 12:41:55 +0000 (13:41 +0100)
committerAndy Wingo <wingo@pobox.com>
Wed, 16 Jan 2013 09:11:35 +0000 (10:11 +0100)
* module/ice-9/peg.scm (until): Rename from until-works, and be
  functional (and faster).
  (peg-match): Adapt.

module/ice-9/peg.scm

index fa7cb3e..9419dd5 100644 (file)
 
 ;; Perform ACTION. If it succeeded, return its return value.  If it failed, run
 ;; IF_FAILS and try again
-(define-syntax until-works
-  (lambda (x)
-    (syntax-case x ()
-      ((_ action if-fails)
-       #'(let ((retval action))
-           (while (not retval)
-                  if-fails
-                  (set! retval action))
-           retval)))))
+(define-syntax until
+  (syntax-rules ()
+    ((_ test stmt stmt* ...)
+     (let lp ()
+       (or action
+           (begin stmt stmt* (lp)))))))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;;;; GENERIC LIST-PROCESSING MACROS
            #`(let ((string (string-copy string-uncopied))
                    (strlen (string-length string-uncopied))
                    (at 0))
-               (let ((ret ((@@ (ice-9 peg) until-works)
-                           (or (>= at strlen)
-                               (#,peg-sexp-compile
-                                string strlen at))
-                           (set! at (+ at 1)))))
+               (let ((ret (until (or (>= at strlen)
+                                     (#,peg-sexp-compile
+                                      string strlen at))
+                                 (set! at (+ at 1)))))
                  (if (eq? ret #t) ;; (>= at strlen) succeeded
                      #f
                      (let ((end (car ret))