forked from Ramarren/cl-parser-combinators
-
Notifications
You must be signed in to change notification settings - Fork 1
/
recurse.lisp
21 lines (20 loc) · 1006 Bytes
/
recurse.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
(in-package :parser-combinators)
(defun curtail? (parser &optional (label (gensym)))
"Add recursion curtailing to promise."
(unless (gethash label *curtail-table*)
(setf (gethash label *curtail-table*) (make-hash-table)))
(let ((curtail-table (gethash label *curtail-table*)))
(labels ((curtailed (inp)
(multiple-value-bind (counter counter-p) (gethash (position-of inp) curtail-table)
(cond (counter-p
(destructuring-bind (c . l) counter
(cond ((>= c (1+ l))
(funcall (zero) inp))
(t
(incf (car counter))
(funcall parser inp)))))
(t
(setf (gethash (position-of inp) curtail-table)
(cons 1 (- (length-of inp) (position-of inp))))
(funcall parser inp))))))
#'curtailed)))