From fc47efcfc2ac497b4e013e68ea0984232b5c3e71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Mon, 31 Oct 2022 06:57:17 -0300 Subject: Change "any" to "either" and add "nothing" --- parser.lisp | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'parser.lisp') diff --git a/parser.lisp b/parser.lisp index 16911ec..37abb75 100644 --- a/parser.lisp +++ b/parser.lisp @@ -29,16 +29,16 @@ (lambda (input) (make-failure :place input :message message))) -(defun any (first-parser &rest other-parsers) +(defun either (first-parser &rest other-parsers) (lambda (input) - (labels ((any-rec (body) - (if (cdr body) - (let ((r (funcall (car body) input))) - (if (parsing-p r) - r - (any-rec (cdr body)))) - (funcall (car body) input)))) - (any-rec (cons first-parser other-parsers))))) + (labels ((either-rec (body) + (if (cdr body) + (let ((r (funcall (car body) input))) + (if (parsing-p r) + r + (either-rec (cdr body)))) + (funcall (car body) input)))) + (either-rec (cons first-parser other-parsers))))) (defun unit (&optional (predicate #'characterp)) (lambda (input) @@ -56,14 +56,17 @@ (p (second (car bindings)))) `(bind ,p (lambda (,v) (comp ,(cdr bindings) ,@body)))))) +(defparameter nothing + (new nil)) + (defun zero-or-one (p) - (any p (new nil))) + (either p nothing)) (defun zero-or-more (p) - (any (comp ((x p) - (xs (zero-or-more p))) - (cons x xs)) - (new nil))) + (either (comp ((x p) + (xs (zero-or-more p))) + (cons x xs)) + nothing)) (defun one-or-more (p) (comp ((x p) -- cgit v1.2.3