From aabcaf712f67ac88ec8bd32b5477f17c1a182080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Mon, 10 Jun 2024 03:28:51 -0300 Subject: Add repeat parser --- parser.lisp | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'parser.lisp') diff --git a/parser.lisp b/parser.lisp index 5470523..82f619e 100644 --- a/parser.lisp +++ b/parser.lisp @@ -26,8 +26,8 @@ (make-parsing :tree tree :left input))) (defun bind-with-input (p f) - (declare (optimize (speed 3))) - (declare (type function p f)) + (declare (optimize (speed 3)) + (function p f)) (lambda (input) (let ((r (funcall p input))) (if (parsing-p r) @@ -36,8 +36,8 @@ r)))) (defun bind (p f) - (declare (optimize (speed 3))) - (declare (type function p f)) + (declare (optimize (speed 3)) + (function p f)) (lambda (input) (let ((r (funcall p input))) (if (parsing-p r) @@ -46,8 +46,8 @@ r)))) (defun discarding-bind (p f) - (declare (optimize (speed 3))) - (declare (type function p f)) + (declare (optimize (speed 3)) + (function p f)) (lambda (input) (let ((r (funcall p input))) (if (parsing-p r) @@ -130,6 +130,17 @@ (xs (optional (many p)))) (cons x xs))) +(defun repeat (p min &optional (max 0)) + (if (> min 0) + (comp ((x p) + (xs (repeat p (1- min) (1- max)))) + (cons x xs)) + (if (> max 0) + (comp ((x (optional p)) + (xs (repeat p 0 (if x (1- max) 0)))) + (if x (cons x xs) x)) + nothing))) + (defparameter whitespace (comp ((_ (optional (many (unit char:whitespace?))))) nil)) -- cgit v1.2.3