X-Git-Url: https://git.distorted.org.uk/~mdw/sod/blobdiff_plain/e0808c472145fc81e52898bc9ac289e10c4f4f41..fddbedf7b1b4b19add30eeb62281748cc77e6955:/src/parser/parser-proto.lisp diff --git a/src/parser/parser-proto.lisp b/src/parser/parser-proto.lisp index b97cd4b..f7b5993 100644 --- a/src/parser/parser-proto.lisp +++ b/src/parser/parser-proto.lisp @@ -87,6 +87,21 @@ failures :initial-value nil)) +(export 'parse-empty) +(defun parse-empty (&optional value) + "Return a parser which parses nothing, successfully. + + The parser returns VALUE and consumes nothing." + (lambda () (values value t nil))) + +(export 'parse-fail) +(defun parse-fail (indicator &optional consumedp) + "Return a parser which fails. + + The parser reports the INDICATOR and (falsely) claims to have consumed + input if CONSUMEDP is true." + (lambda () (values indicator nil consumedp))) + ;;;-------------------------------------------------------------------------- ;;; Basic protocol. @@ -591,7 +606,7 @@ The return value may later be used with `parser-restore-place'. Be careful: all of this is happening at macro-expansion time.") (:method (context) - (error "Parser context ~S doesn't support rewinding." context))) + (error "Parser context ~S doesn't support rewinding" context))) (export 'parser-restore-place) (defgeneric parser-restore-place (context place) @@ -646,7 +661,8 @@ `(let ((,,place ,(parser-capture-place ,context))) ,(if (parser-places-must-be-released-p ,context) `(unwind-protect ,(,bodyfunc) - ,(parser-release-place ,context ,place)) + (when ,,place + ,(parser-release-place ,context ,place))) (,bodyfunc)))))))) (export 'peek) @@ -654,12 +670,30 @@ "Attempt to run PARSER, but rewind the underlying source if it fails." (with-gensyms (value win consumedp) (with-parser-place (place context) - `(multiple-value-bind (,value ,win ,consumedp) (parse ,parser) - (cond (,win - (values ,value ,win ,consumedp)) - (t - ,(parser-restore-place context place) - (values ,value ,win nil))))))) + `(macrolet ((commit-peeked-place () + `(progn + ,',(parser-release-place context place) + (setf ,',place nil)))) + (multiple-value-bind (,value ,win ,consumedp) (parse ,parser) + (cond ((or ,win (null ,place)) + (values ,value ,win ,consumedp)) + (t + ,(parser-restore-place context place) + (values ,value ,win nil)))))))) + +(defun commit-peeked-place () + "Called by `commit' not lexically within `peek'." + (error "`commit' is not within `peek'")) + +(export 'commit) +(defparse commit () + "Commit to the current parse. + + This releases the place captured by the innermost lexically enclosing + `peek'." + '(progn + (commit-peeked-place) + (values nil t nil))) ;;;-------------------------------------------------------------------------- ;;; Character parser context protocol. @@ -845,9 +879,10 @@ "Return the parser's current token's semantic value.")) (export 'token) -(defparse token (:context (context token-parser-context) - type &optional (value nil valuep) &key peekp) - "Match tokens of a particular type. +(locally (declare #+sbcl (sb-ext:muffle-conditions style-warning)) + (defparse token (:context (context token-parser-context) + type &optional (value nil valuep) &key peekp) + "Match tokens of a particular type. A token matches under the following conditions: @@ -869,35 +904,35 @@ If the match fails then the failure indicator is either TYPE or (TYPE VALUE), depending on whether a VALUE was specified." - (once-only (type value peekp) - (with-gensyms (tokty tokval) - `(let ((,tokty ,(parser-token-type context)) - (,tokval ,(parser-token-value context))) - (if ,(if (eq type t) - `(not (eq ,tokty :eof)) - (flet ((check-value (cond) - (if valuep - `(and ,cond (equal ,tokval ,value)) - cond))) - (if (constantp type) - (check-value `(eql ,tokty ,type)) - `(if (eq ,type t) - (not (eq ,tokty :eof)) - ,(check-value `(eql ,tokty ,type)))))) - ,(let* ((result `(values ,tokval t ,(if (constantp peekp) - (not peekp) - `(not ,peekp)))) - (step (parser-step context))) - (cond ((not (constantp peekp)) - `(multiple-value-prog1 ,result - (unless ,peekp ,step))) - (peekp - result) - (t - `(multiple-value-prog1 ,result - ,step)))) - (values (list ,(if valuep `(list ,type ,value) type)) - nil nil)))))) + (once-only (type value peekp) + (with-gensyms (tokty tokval) + `(let ((,tokty ,(parser-token-type context)) + (,tokval ,(parser-token-value context))) + (if ,(if (eq type t) + `(not (eq ,tokty :eof)) + (flet ((check-value (cond) + (if valuep + `(and ,cond (equal ,tokval ,value)) + cond))) + (if (constantp type) + (check-value `(eql ,tokty ,type)) + `(if (eq ,type t) + (not (eq ,tokty :eof)) + ,(check-value `(eql ,tokty ,type)))))) + ,(let* ((result `(values ,tokval t ,(if (constantp peekp) + (not peekp) + `(not ,peekp)))) + (step (parser-step context))) + (cond ((not (constantp peekp)) + `(multiple-value-prog1 ,result + (unless ,peekp ,step))) + (peekp + result) + (t + `(multiple-value-prog1 ,result + ,step)))) + (values (list ,(if valuep `(list ,type ,value) type)) + nil nil))))))) (defmethod expand-parser-spec ((context token-parser-context) spec) (if (atom spec)