--- /dev/null
+Return-Path: <m.walters@qmul.ac.uk>\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 47ACD431FAF\r
+ for <notmuch@notmuchmail.org>; Sat, 25 May 2013 01:59:51 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -1.098\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5\r
+ tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001,\r
+ NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3] 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 P3PKygnSq2pT for <notmuch@notmuchmail.org>;\r
+ Sat, 25 May 2013 01:59:42 -0700 (PDT)\r
+Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6])\r
+ (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id E9E0C431FAE\r
+ for <notmuch@notmuchmail.org>; Sat, 25 May 2013 01:59:41 -0700 (PDT)\r
+Received: from smtp.qmul.ac.uk ([138.37.6.40])\r
+ by mail2.qmul.ac.uk with esmtp (Exim 4.71)\r
+ (envelope-from <m.walters@qmul.ac.uk>)\r
+ id 1UgAK2-0000s6-KK; Sat, 25 May 2013 09:59:36 +0100\r
+Received: from 93-97-24-31.zone5.bethere.co.uk ([93.97.24.31] helo=localhost)\r
+ by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.71)\r
+ (envelope-from <m.walters@qmul.ac.uk>)\r
+ id 1UgAK1-0003EC-UM; Sat, 25 May 2013 09:59:30 +0100\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: Austin Clements <amdragon@MIT.EDU>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH 4/5] emacs: Streaming S-expression parser\r
+In-Reply-To: <87ip2b3moz.fsf@awakening.csail.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
+User-Agent: Notmuch/0.14+255~gff3cc55 (http://notmuchmail.org) Emacs/23.4.1\r
+ (x86_64-pc-linux-gnu)\r
+Date: Sat, 25 May 2013 09:59:28 +0100\r
+Message-ID: <8761y7ph8v.fsf@qmul.ac.uk>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=utf-8\r
+Content-Transfer-Encoding: quoted-printable\r
+X-Sender-Host-Address: 93.97.24.31\r
+X-QM-SPAM-Info: Sender has good ham record. :)\r
+X-QM-Body-MD5: beeb5d5746d0b2c707bc2474617b0c6f (of first 20000 bytes)\r
+X-SpamAssassin-Score: -0.1\r
+X-SpamAssassin-SpamBar: /\r
+X-SpamAssassin-Report: The QM spam filters have analysed this message to\r
+ determine if it is\r
+ spam. We require at least 5.0 points to mark a message as spam.\r
+ This message scored -0.1 points.\r
+ Summary of the scoring: \r
+ * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail\r
+ provider * (markwalters1009[at]gmail.com)\r
+ * -0.1 AWL AWL: From: address is in the auto white-list\r
+X-QM-Scan-Virus: ClamAV says the message is clean\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: Sat, 25 May 2013 08:59:51 -0000\r
+\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
+>> 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
+++++++++\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 :=3D emacs\r
+>>> emacs_sources :=3D \\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 =C2=A9 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
+>\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
+>>> + ((=3D (char-after) ?\))\r
+>>> + ;; We've reached the end of a list\r
+>>> + (if (=3D (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
+Best wishes=20\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
+>>> + ((=3D (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 (>=3D depth 0))\r
+>>> + (if (=3D 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
+>>> + ((=3D (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
+>>> --=20\r
+>>> 1.7.10.4\r