diff options
author | Juan Manuel Tomás <jtomas1815@gmail.com> | 2022-10-31 06:57:17 -0300 |
---|---|---|
committer | Juan Manuel Tomás <jtomas1815@gmail.com> | 2022-10-31 06:57:17 -0300 |
commit | fc47efcfc2ac497b4e013e68ea0984232b5c3e71 (patch) | |
tree | d6580ccacc1faaa3dc5b65238d8a8e6b9c4dce46 | |
parent | 6a90bc2c3a2fb77a66c19e7400ea0e2fec410e26 (diff) | |
download | monparser-fc47efcfc2ac497b4e013e68ea0984232b5c3e71.tar.gz monparser-fc47efcfc2ac497b4e013e68ea0984232b5c3e71.zip |
Change "any" to "either" and add "nothing"
-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) |