--- /dev/null
+Return-Path: <amdragon@mit.edu>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 07049431FAF\r
+ for <notmuch@notmuchmail.org>; Mon, 27 May 2013 12:04:55 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id 4uuybgHLJgkT for <notmuch@notmuchmail.org>;\r
+ Mon, 27 May 2013 12:04:45 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-4.mit.edu (DMZ-MAILSEC-SCANNER-4.MIT.EDU\r
+ [18.9.25.15])\r
+ by olra.theworths.org (Postfix) with ESMTP id 4565E431FAE\r
+ for <notmuch@notmuchmail.org>; Mon, 27 May 2013 12:04:45 -0700 (PDT)\r
+X-AuditID: 1209190f-b7f256d000005616-05-51a3ae491c90\r
+Received: from mailhub-auth-3.mit.edu ( [18.9.21.43])\r
+ by dmz-mailsec-scanner-4.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id 4C.05.22038.94EA3A15; Mon, 27 May 2013 15:04:41 -0400 (EDT)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH-1.MIT.EDU [18.9.28.11])\r
+ by mailhub-auth-3.mit.edu (8.13.8/8.9.2) with ESMTP id r4RJ4ewa024942; \r
+ Mon, 27 May 2013 15:04:41 -0400\r
+Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91])\r
+ (authenticated bits=0)\r
+ (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+ by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id r4RJ4bg4029612\r
+ (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NOT);\r
+ Mon, 27 May 2013 15:04:38 -0400\r
+Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.80)\r
+ (envelope-from <amdragon@mit.edu>)\r
+ id 1Uh2ii-0008EC-PM; Mon, 27 May 2013 15:04:36 -0400\r
+Date: Mon, 27 May 2013 15:04:36 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: Mark Walters <markwalters1009@gmail.com>\r
+Subject: Re: [PATCH 4/5] emacs: Streaming S-expression parser\r
+Message-ID: <20130527190436.GS5999@mit.edu>\r
+References: <1368851472-5382-1-git-send-email-amdragon@mit.edu>\r
+ <1368851472-5382-5-git-send-email-amdragon@mit.edu>\r
+ <87fvxg2wc4.fsf@qmul.ac.uk>\r
+ <87ip2b3moz.fsf@awakening.csail.mit.edu>\r
+ <8761y7ph8v.fsf@qmul.ac.uk>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=iso-8859-1\r
+Content-Disposition: inline\r
+Content-Transfer-Encoding: 8bit\r
+In-Reply-To: <8761y7ph8v.fsf@qmul.ac.uk>\r
+User-Agent: Mutt/1.5.21 (2010-09-15)\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFlrLKsWRmVeSWpSXmKPExsUixCmqreu5bnGgwcstYhar5/JYXL85k9mB\r
+ yWPnrLvsHs9W3WIOYIrisklJzcksSy3St0vgyth1bTljwbf8iu6bk1kaGI+FdTFyckgImEg0\r
+ H5nPBmGLSVy4tx7I5uIQEtjHKLH0ynVmCGcjo8TKb9OgnNNMEo3dC9khnCWMEtPWLGIC6WcR\r
+ UJVo7/nBCGKzCWhIbNu/HMwWEdCRuH1oATuIzSwgLfHtdzNQPQeHsICtxKL9YKt5BbQl9i35\r
+ wwox8w6jxJe+lUwQCUGJkzOfsED06kjs3HqHDaQXZM7yfxwQYXmJ5q2zmUFsTqC1E161g80U\r
+ FVCRmHJyG9sERuFZSCbNQjJpFsKkWUgmLWBkWcUom5JbpZubmJlTnJqsW5ycmJeXWqRropeb\r
+ WaKXmlK6iREcB5L8Oxi/HVQ6xCjAwajEwzshe3GgEGtiWXFl7iFGSQ4mJVHe0yuAQnxJ+SmV\r
+ GYnFGfFFpTmpxYcYJTiYlUR4t4PkeFMSK6tSi/JhUtIcLErivFdTbvoLCaQnlqRmp6YWpBbB\r
+ ZGU4OJQkeP3XAjUKFqWmp1akZeaUIKSZODhBhvMADdcAqeEtLkjMLc5Mh8ifYjTm2Hx+8jtG\r
+ jravQFKIJS8/L1VKnNcJpFQApDSjNA9uGiyVvWIUB3pOmNcepIoHmAbh5r0CWsUEtEqcGWxV\r
+ SSJCSqqBUeNA6of0Pk3vGeqBq//naWTxVC67FPLsQOUdycY10e1B9ds7/lccCbbb9r+h/9nB\r
+ 6wtqLTvVfZlMbn44Hrl/X9fe+Q5vXk5bbpD2hTnqfemuHZvnMS7o1g20PGJWffnr4etdxbfN\r
+ WBmTDt3ycZy9dpZcqhnnKRVvsczuT+5LFf8/UFB737oyS4mlOCPRUIu5qDgRAMQVG6RAAwAA\r
+Cc: notmuch@notmuchmail.org\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Mon, 27 May 2013 19:04:55 -0000\r
+\r
+Quoth Mark Walters on May 25 at 9:59 am:\r
+> \r
+> Hi\r
+> \r
+> On Wed, 22 May 2013, Austin Clements <amdragon@MIT.EDU> wrote:\r
+> > On Tue, 21 May 2013, Mark Walters <markwalters1009@gmail.com> wrote:\r
+> >> Hi\r
+> >>\r
+> >> This patch looks good to me. Some minor comments below.\r
+> >\r
+> > Some minor replies below.\r
+> >\r
+> > In building some other code on top of this, I found an interesting (but\r
+> > easy to fix) interface bug. Currently, the interface is designed as if\r
+> > it doesn't matter what buffer these functions are called from, however,\r
+> > because they move point and expect this point motion to persist, it's\r
+> > actually not safe to call this interface unless the caller is in the\r
+> > right buffer anyway. For example, if the buffer is selected in a\r
+> > window, the with-current-buffer in the parser functions will actually\r
+> > move a *temporary* point, meaning that the only way the caller can\r
+> > discover the new point is to first select the buffer for itself. I can\r
+> > think of two solutions: 1) maintain our own mark for the parser's\r
+> > current position or 2) tweak the doc strings and code so that it reads\r
+> > from the current buffer. 1 keeps the interface the way it's currently\r
+> > documented, but complicates the parser implementation and interface and\r
+> > doesn't simplify the caller. 2 simplifies the parser and it turns out\r
+> > all callers already satisfy the requirement.\r
+> \r
+> I am confused by this: the docs strings for json/sexp-parse-partial-list\r
+> both say something like "Parse a partial JSON list from current buffer"?\r
+> Or do you mean the with-current-buffer in notmuch-search-process-filter?\r
+\r
+I was referring to the lower level parser, which effectively has the\r
+same requirement but isn't documented to and has code that pointlessly\r
+tries to track the parsing buffer (I consider\r
+json/sexp-parse-partial-list to be a helper). In fact, one reason the\r
+lower level parser didn't choke is because right now we only use it\r
+through json/sexp-parse-partial-list, which requires that it be called\r
+from the right buffer.\r
+\r
+> >> On Sat, 18 May 2013, Austin Clements <amdragon@MIT.EDU> wrote:\r
+> >>> This provides the same interface as the streaming JSON parser, but\r
+> >>> reads S-expressions incrementally. The only difference is that the\r
+> >>> `notmuch-sexp-parse-partial-list' helper does not handle interleaved\r
+> >>> error messages (since we now have the ability to separate these out at\r
+> >>> the invocation level), so it no longer takes an error function and\r
+> >>> does not need to do the horrible resynchronization that the JSON\r
+> >>> parser had to.\r
+> >>>\r
+> >>> Some implementation improvements have been made over the JSON parser.\r
+> >>> This uses a vector instead of a list for the parser data structure,\r
+> >>> since this allows faster access to elements (and modern versions of\r
+> >>> Emacs handle storage of small vectors efficiently). Private functions\r
+> >>> follow the "prefix--name" convention. And the implementation is much\r
+> >>> simpler overall because S-expressions are much easier to parse.\r
+> >>> ---\r
+> >>> emacs/Makefile.local | 1 +\r
+> >>> emacs/notmuch-parser.el | 212 +++++++++++++++++++++++++++++++++++++++++++++++\r
+> >>> 2 files changed, 213 insertions(+)\r
+> >>> create mode 100644 emacs/notmuch-parser.el\r
+> >>>\r
+> >>> diff --git a/emacs/Makefile.local b/emacs/Makefile.local\r
+> >>> index 456700a..a910aff 100644\r
+> >>> --- a/emacs/Makefile.local\r
+> >>> +++ b/emacs/Makefile.local\r
+> >>> @@ -3,6 +3,7 @@\r
+> >>> dir := emacs\r
+> >>> emacs_sources := \\r
+> >>> $(dir)/notmuch-lib.el \\r
+> >>> + $(dir)/notmuch-parser.el \\r
+> >>> $(dir)/notmuch.el \\r
+> >>> $(dir)/notmuch-query.el \\r
+> >>> $(dir)/notmuch-show.el \\r
+> >>> diff --git a/emacs/notmuch-parser.el b/emacs/notmuch-parser.el\r
+> >>> new file mode 100644\r
+> >>> index 0000000..1b7cf64\r
+> >>> --- /dev/null\r
+> >>> +++ b/emacs/notmuch-parser.el\r
+> >>> @@ -0,0 +1,212 @@\r
+> >>> +;; notmuch-parser.el --- streaming S-expression parser\r
+> >>> +;;\r
+> >>> +;; Copyright © Austin Clements\r
+> >>> +;;\r
+> >>> +;; This file is part of Notmuch.\r
+> >>> +;;\r
+> >>> +;; Notmuch is free software: you can redistribute it and/or modify it\r
+> >>> +;; under the terms of the GNU General Public License as published by\r
+> >>> +;; the Free Software Foundation, either version 3 of the License, or\r
+> >>> +;; (at your option) any later version.\r
+> >>> +;;\r
+> >>> +;; Notmuch is distributed in the hope that it will be useful, but\r
+> >>> +;; WITHOUT ANY WARRANTY; without even the implied warranty of\r
+> >>> +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
+> >>> +;; General Public License for more details.\r
+> >>> +;;\r
+> >>> +;; You should have received a copy of the GNU General Public License\r
+> >>> +;; along with Notmuch. If not, see <http://www.gnu.org/licenses/>.\r
+> >>> +;;\r
+> >>> +;; Authors: Austin Clements <aclements@csail.mit.edu>\r
+> >>> +\r
+> >>> +(require 'cl)\r
+> >>> +\r
+> >>> +(defun notmuch-sexp-create-parser (buffer)\r
+> >>> + "Return a streaming S-expression parser that reads from BUFFER.\r
+> >>> +\r
+> >>> +This parser is designed to incrementally read an S-expression\r
+> >>> +whose structure is known to the caller. Like a typical\r
+> >>> +S-expression parsing interface, it provides a function to read a\r
+> >>> +complete S-expression from the input. However, it extends this\r
+> >>> +with an additional function that requires the next value in the\r
+> >>> +input to be a list and descends into it, allowing its elements to\r
+> >>> +be read one at a time or further descended into. Both functions\r
+> >>> +can return 'retry to indicate that not enough input is available.\r
+> >>> +\r
+> >>> +The parser always consumes input from BUFFER's point. Hence, the\r
+> >>> +caller is allowed to delete any data before point and may\r
+> >>> +resynchronize after an error by moving point."\r
+> >>> +\r
+> >>> + (vector 'notmuch-sexp-parser\r
+> >>> + buffer\r
+> >>> + ;; List depth\r
+> >>> + 0\r
+> >>> + ;; Partial parse position marker\r
+> >>> + nil\r
+> >>> + ;; Partial parse state\r
+> >>> + nil))\r
+> >>> +\r
+> >>> +(defmacro notmuch-sexp--buffer (sp) `(aref ,sp 1))\r
+> >>> +(defmacro notmuch-sexp--depth (sp) `(aref ,sp 2))\r
+> >>> +(defmacro notmuch-sexp--partial-pos (sp) `(aref ,sp 3))\r
+> >>> +(defmacro notmuch-sexp--partial-state (sp) `(aref ,sp 4))\r
+> >>\r
+> >> Why the double hyphen --? Is it a name-space or some convention?\r
+> >\r
+> > More specifically, this seems to be the most common Elisp convention for\r
+> > indicating private symbols.\r
+> \r
+> Ok. If we are keeping the json parser it might be worth making it follow\r
+> the same convention but as it is purely internal it's probably not worth\r
+> bothering.\r
+\r
+Yeah, it is purely internal. Also, I was planning to remove the JSON\r
+parser (or maybe move it somewhere else? I feel bad deleting that\r
+much perfectly functional code, though of course git will keep it in\r
+perpetuity).\r
+\r
+> >>> +(defun notmuch-sexp-read (sp)\r
+> >>> + "Consume and return the value at point in SP's buffer.\r
+> >>> +\r
+> >>> +Returns 'retry if there is insufficient input to parse a complete\r
+> >>> +value (though it may still move point over whitespace). If the\r
+> >>> +parser is currently inside a list and the next token ends the\r
+> >>> +list, this moves point just past the terminator and returns 'end.\r
+> >>> +Otherwise, this moves point to just past the end of the value and\r
+> >>> +returns the value."\r
+> >>> +\r
+> >>> + (with-current-buffer (notmuch-sexp--buffer sp)\r
+> >>> + (skip-chars-forward " \n\r\t")\r
+> >>> + (cond ((eobp) 'retry)\r
+> >>> + ((= (char-after) ?\))\r
+> >>> + ;; We've reached the end of a list\r
+> >>> + (if (= (notmuch-sexp--depth sp) 0)\r
+> >>> + ;; .. but we weren't in a list. Let read signal the\r
+> >>> + ;; error.\r
+> >>> + (read (current-buffer))\r
+> >>\r
+> >> Why is good for read to signal the error rather than us doing it?\r
+> >\r
+> > This ensures the syntax error handling and signal behavior of\r
+> > notmuch-sexp-read is identical in every way to a regular read call.\r
+> > Maybe the comment should read "Let read signal the error like we do in\r
+> > all other code paths."?\r
+> \r
+> Yes that would be good: or perhaps "like it does in all other code\r
+> paths".\r
+\r
+Sure.\r
+\r
+> Best wishes \r
+> \r
+> Mark\r
+> \r
+> >\r
+> >>> + ;; Go up a level and return an end token\r
+> >>> + (decf (notmuch-sexp--depth sp))\r
+> >>> + (forward-char)\r
+> >>> + 'end))\r
+> >>> + ((= (char-after) ?\()\r
+> >>> + ;; We're at the beginning of a list. If we haven't started\r
+> >>> + ;; a partial parse yet, attempt to read the list in its\r
+> >>> + ;; entirety. If this fails, or we've started a partial\r
+> >>> + ;; parse, extend the partial parse to figure out when we\r
+> >>> + ;; have a complete list.\r
+> >>> + (catch 'return\r
+> >>> + (when (null (notmuch-sexp--partial-state sp))\r
+> >>> + (let ((start (point)))\r
+> >>> + (condition-case nil\r
+> >>> + (throw 'return (read (current-buffer)))\r
+> >>> + (end-of-file (goto-char start)))))\r
+> >>> + ;; Extend the partial parse\r
+> >>> + (let (is-complete)\r
+> >>> + (save-excursion\r
+> >>> + (let* ((new-state (parse-partial-sexp\r
+> >>> + (or (notmuch-sexp--partial-pos sp) (point))\r
+> >>> + (point-max) 0 nil\r
+> >>> + (notmuch-sexp--partial-state sp)))\r
+> >>> + ;; A complete value is available if we've\r
+> >>> + ;; reached depth 0.\r
+> >>> + (depth (first new-state)))\r
+> >>> + (assert (>= depth 0))\r
+> >>> + (if (= depth 0)\r
+> >>> + ;; Reset partial parse state\r
+> >>> + (setf (notmuch-sexp--partial-state sp) nil\r
+> >>> + (notmuch-sexp--partial-pos sp) nil\r
+> >>> + is-complete t)\r
+> >>> + ;; Update partial parse state\r
+> >>> + (setf (notmuch-sexp--partial-state sp) new-state\r
+> >>> + (notmuch-sexp--partial-pos sp) (point-marker)))))\r
+> >>> + (if is-complete\r
+> >>> + (read (current-buffer))\r
+> >>> + 'retry))))\r
+> >>> + (t\r
+> >>> + ;; Attempt to read a non-compound value\r
+> >>> + (let ((start (point)))\r
+> >>> + (condition-case nil\r
+> >>> + (let ((val (read (current-buffer))))\r
+> >>> + ;; We got what looks like a complete read, but if\r
+> >>> + ;; we reached the end of the buffer in the process,\r
+> >>> + ;; we may not actually have all of the input we\r
+> >>> + ;; need (unless it's a string, which is delimited).\r
+> >>> + (if (or (stringp val) (not (eobp)))\r
+> >>> + val\r
+> >>> + ;; We can't be sure the input was complete\r
+> >>> + (goto-char start)\r
+> >>> + 'retry))\r
+> >>> + (end-of-file\r
+> >>> + (goto-char start)\r
+> >>> + 'retry)))))))\r
+> >>> +\r
+> >>> +(defun notmuch-sexp-begin-list (sp)\r
+> >>> + "Parse the beginning of a list value and enter the list.\r
+> >>> +\r
+> >>> +Returns 'retry if there is insufficient input to parse the\r
+> >>> +beginning of the list. If this is able to parse the beginning of\r
+> >>> +a list, it moves point past the token that opens the list and\r
+> >>> +returns t. Later calls to `notmuch-sexp-read' will return the\r
+> >>> +elements inside the list. If the input in buffer is not the\r
+> >>> +beginning of a list, throw invalid-read-syntax."\r
+> >>> +\r
+> >>> + (with-current-buffer (notmuch-sexp--buffer sp)\r
+> >>> + (skip-chars-forward " \n\r\t")\r
+> >>> + (cond ((eobp) 'retry)\r
+> >>> + ((= (char-after) ?\()\r
+> >>> + (forward-char)\r
+> >>> + (incf (notmuch-sexp--depth sp))\r
+> >>> + t)\r
+> >>> + (t\r
+> >>> + ;; Skip over the bad character like `read' does\r
+> >>> + (forward-char)\r
+> >>> + (signal 'invalid-read-syntax (list (string (char-before))))))))\r
+> >>> +\r
+> >>> +(defun notmuch-sexp-eof (sp)\r
+> >>> + "Signal an error if there is more data in SP's buffer.\r
+> >>> +\r
+> >>> +Moves point to the beginning of any trailing data or to the end\r
+> >>> +of the buffer if there is only trailing whitespace."\r
+> >>> +\r
+> >>> + (with-current-buffer (notmuch-sexp--buffer sp)\r
+> >>> + (skip-chars-forward " \n\r\t")\r
+> >>> + (unless (eobp)\r
+> >>> + (error "Trailing garbage following expression"))))\r
+> >>> +\r
+> >>> +(defvar notmuch-sexp--parser nil\r
+> >>> + "The buffer-local notmuch-sexp-parser instance.\r
+> >>> +\r
+> >>> +Used by `notmuch-sexp-parse-partial-list'.")\r
+> >>> +\r
+> >>> +(defvar notmuch-sexp--state nil\r
+> >>> + "The buffer-local `notmuch-sexp-parse-partial-list' state.")\r
+> >>> +\r
+> >>> +(defun notmuch-sexp-parse-partial-list (result-function result-buffer)\r
+> >>> + "Incrementally parse an S-expression list from the current buffer.\r
+> >>> +\r
+> >>> +This function consume an S-expression list from the current\r
+> >>\r
+> >> consumes\r
+> >\r
+> > Oops, yes.\r
+> >\r
+> >>> +buffer, applying RESULT-FUNCTION in RESULT-BUFFER to each\r
+> >>> +complete value in the list. It operates incrementally and should\r
+> >>> +be called whenever the input buffer has been extended with\r
+> >>> +additional data. The caller just needs to ensure it does not\r
+> >>> +move point in the input buffer."\r
+> >>> +\r
+> >>> + ;; Set up the initial state\r
+> >>> + (unless (local-variable-p 'notmuch-sexp--parser)\r
+> >>> + (set (make-local-variable 'notmuch-sexp--parser)\r
+> >>> + (notmuch-sexp-create-parser (current-buffer)))\r
+> >>> + (set (make-local-variable 'notmuch-sexp--state) 'begin))\r
+> >>> + (let (done)\r
+> >>> + (while (not done)\r
+> >>> + (case notmuch-sexp--state\r
+> >>> + (begin\r
+> >>> + ;; Enter the list\r
+> >>> + (if (eq (notmuch-sexp-begin-list notmuch-sexp--parser) 'retry)\r
+> >>> + (setq done t)\r
+> >>> + (setq notmuch-sexp--state 'result)))\r
+> >>> + (result\r
+> >>> + ;; Parse a result\r
+> >>> + (let ((result (notmuch-sexp-read notmuch-sexp--parser)))\r
+> >>> + (case result\r
+> >>> + (retry (setq done t))\r
+> >>> + (end (setq notmuch-sexp--state 'end))\r
+> >>> + (t (with-current-buffer result-buffer\r
+> >>> + (funcall result-function result))))))\r
+> >>> + (end\r
+> >>> + ;; Any trailing data is unexpected\r
+> >>> + (notmuch-sexp-eof notmuch-sexp--parser)\r
+> >>> + (setq done t)))))\r
+> >>> + ;; Clear out what we've parsed\r
+> >>> + (delete-region (point-min) (point)))\r
+> >>> +\r
+> >>> +(provide 'notmuch-parser)\r
+> >>> +\r
+> >>> +;; Local Variables:\r
+> >>> +;; byte-compile-warnings: (not cl-functions)\r
+> >>> +;; End:\r