X-Git-Url: https://git.notmuchmail.org/git?a=blobdiff_plain;f=emacs%2Fnotmuch-parser.el;h=3aa5bd8ff1cdc45f6265d9f2133e9e8b9e070c57;hb=HEAD;hp=4a437016dc6b47d6e6e6fa33d76c31fa6ac1e5df;hpb=b4ee80dcbdd6702a693110321ad69c380967846d;p=notmuch diff --git a/emacs/notmuch-parser.el b/emacs/notmuch-parser.el index 4a437016..710c60e1 100644 --- a/emacs/notmuch-parser.el +++ b/emacs/notmuch-parser.el @@ -1,4 +1,4 @@ -;;; notmuch-parser.el --- streaming S-expression parser +;;; notmuch-parser.el --- streaming S-expression parser -*- lexical-binding: t -*- ;; ;; Copyright © Austin Clements ;; @@ -21,7 +21,9 @@ ;;; Code: -(eval-when-compile (require 'cl-lib)) +(require 'cl-lib) +(require 'pcase) +(require 'subr-x) (defun notmuch-sexp-create-parser () "Return a new streaming S-expression parser. @@ -33,7 +35,7 @@ complete S-expression from the input. However, it extends this with an additional function that requires the next value in the input to be a list and descends into it, allowing its elements to be read one at a time or further descended into. Both functions -can return 'retry to indicate that not enough input is available. +can return \\='retry to indicate that not enough input is available. The parser always consumes input from point in the current buffer. Hence, the caller is allowed to delete any data before @@ -50,10 +52,10 @@ point and may resynchronize after an error by moving point." (defun notmuch-sexp-read (sp) "Consume and return the value at point in the current buffer. -Returns 'retry if there is insufficient input to parse a complete +Returns \\='retry if there is insufficient input to parse a complete value (though it may still move point over whitespace). If the parser is currently inside a list and the next token ends the -list, this moves point just past the terminator and returns 'end. +list, this moves point just past the terminator and returns \\='end. Otherwise, this moves point to just past the end of the value and returns the value." (skip-chars-forward " \n\r\t") @@ -123,7 +125,7 @@ returns the value." (defun notmuch-sexp-begin-list (sp) "Parse the beginning of a list value and enter the list. -Returns 'retry if there is insufficient input to parse the +Returns \\='retry if there is insufficient input to parse the beginning of the list. If this is able to parse the beginning of a list, it moves point past the token that opens the list and returns t. Later calls to `notmuch-sexp-read' will return the @@ -140,15 +142,6 @@ beginning of a list, throw invalid-read-syntax." (forward-char) (signal 'invalid-read-syntax (list (string (char-before))))))) -(defun notmuch-sexp-eof (sp) - "Signal an error if there is more data in SP's buffer. - -Moves point to the beginning of any trailing data or to the end -of the buffer if there is only trailing whitespace." - (skip-chars-forward " \n\r\t") - (unless (eobp) - (error "Trailing garbage following expression"))) - (defvar notmuch-sexp--parser nil "The buffer-local notmuch-sexp-parser instance. @@ -187,8 +180,11 @@ move point in the input buffer." (t (with-current-buffer result-buffer (funcall result-function result)))))) (end - ;; Any trailing data is unexpected - (notmuch-sexp-eof notmuch-sexp--parser) + ;; Skip over trailing whitespace. + (skip-chars-forward " \n\r\t") + ;; Any trailing data is unexpected. + (unless (eobp) + (error "Trailing garbage following expression")) (setq done t))))) ;; Clear out what we've parsed (delete-region (point-min) (point)))