cl-fringe.lisp: Gratuitously allow :START and :END keyword arguments.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 30 Dec 2010 12:29:51 +0000 (12:29 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Thu, 30 Dec 2010 12:29:51 +0000 (12:29 +0000)
Just to illustrate the way Lisp sequence functions tend to work.

cl-fringe.lisp

index 0a41838..febdada 100644 (file)
@@ -64,7 +64,7 @@
                   (t (values nil nil)))))
     (lambda () (recur node nil))))
 
-(defun parse-tree (string)
+(defun parse-tree (string &key (start 0) (end (length string)))
   "Parse STRING, and return the tree described.
 
    The syntax is simple:
    The ambiguity is resolved by always treating `(' as a tree when a tree is
    expected."
 
-  (let ((len (length string)))
-    (labels ((parse (i)
-              (cond ((and (< i len) (char= (char string i) #\())
-                     (multiple-value-bind (left i) (parse (1+ i))
-                       (unless (< i len) (error "no data"))
-                       (let ((data (char string i)))
-                         (multiple-value-bind (right i) (parse (1+ i))
-                           (unless (and (< i len)
-                                        (char= (char string i) #\)))
-                             (error "missing )"))
-                           (values
-                            (make-node :left left :data data :right right)
-                            (1+ i))))))
-                    (t (values nil i)))))
-      (multiple-value-bind (tree i) (parse 0)
-       (unless (= i len) (error "trailing junk"))
-       tree))))
+  (labels ((parse (i)
+            (cond ((and (< i end) (char= (char string i) #\())
+                   (multiple-value-bind (left i) (parse (1+ i))
+                     (unless (< i end) (error "no data"))
+                     (let ((data (char string i)))
+                       (multiple-value-bind (right i) (parse (1+ i))
+                         (unless (and (< i end)
+                                      (char= (char string i) #\)))
+                           (error "missing )"))
+                         (values
+                          (make-node :left left :data data :right right)
+                          (1+ i))))))
+                  (t (values nil i)))))
+    (multiple-value-bind (tree i) (parse start)
+      (unless (= i end) (error "trailing junk"))
+      tree)))
 
 ;;;--------------------------------------------------------------------------
 ;;; Main program.