diff options
-rw-r--r-- | package.lisp | 3 | ||||
-rw-r--r-- | parser.lisp | 31 |
2 files changed, 19 insertions, 15 deletions
diff --git a/package.lisp b/package.lisp index fdb1ab6..8656c7c 100644 --- a/package.lisp +++ b/package.lisp @@ -9,9 +9,10 @@ #:new #:bind #:fail - #:any + #:either #:unit #:comp + #:nothing #:zero-or-one #:zero-or-more #:one-or-more)) 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) |