--- /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 25DDF429E52\r
+ for <notmuch@notmuchmail.org>; Fri, 31 May 2013 17:40:50 -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 eIHNYimVX5A7 for <notmuch@notmuchmail.org>;\r
+ Fri, 31 May 2013 17:40:41 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-7.mit.edu (dmz-mailsec-scanner-7.mit.edu\r
+ [18.7.68.36])\r
+ by olra.theworths.org (Postfix) with ESMTP id 6FAE4429E39\r
+ for <notmuch@notmuchmail.org>; Fri, 31 May 2013 17:40:26 -0700 (PDT)\r
+X-AuditID: 12074424-b7f8c6d0000028c4-9e-51a942facb02\r
+Received: from mailhub-auth-2.mit.edu ( [18.7.62.36])\r
+ by dmz-mailsec-scanner-7.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id 1F.C2.10436.AF249A15; Fri, 31 May 2013 20:40:26 -0400 (EDT)\r
+Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
+ by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id r510eDF5024661; \r
+ Fri, 31 May 2013 20:40:14 -0400\r
+Received: from drake.dyndns.org (c-76-21-105-205.hsd1.ca.comcast.net\r
+ [76.21.105.205]) (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 r510eAcv020945\r
+ (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+ Fri, 31 May 2013 20:40:12 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
+ (envelope-from <amdragon@mit.edu>)\r
+ id 1UiZrd-0003QV-IX; Fri, 31 May 2013 20:40:09 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v3 4/6] emacs: Streaming S-expression parser\r
+Date: Fri, 31 May 2013 20:40:06 -0400\r
+Message-Id: <1370047208-12785-5-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <1370047208-12785-1-git-send-email-amdragon@mit.edu>\r
+References: <1370047208-12785-1-git-send-email-amdragon@mit.edu>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=UTF-8\r
+Content-Transfer-Encoding: 8bit\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFprKKsWRmVeSWpSXmKPExsUixG6novvLaWWgwa+dzBar5/JYXL85k9ni\r
+ zcp5rA7MHjtn3WX3OPx1IYvHs1W3mAOYo7hsUlJzMstSi/TtErgy5vWwFDz2qJh9YhVjA+Mx\r
+ yy5GTg4JAROJw38bGSFsMYkL99azdTFycQgJ7GOUuHHkKRNIQkhgI6PEhaeyEInTTBLN5zYy\r
+ QjhzGSVmzH3ADlLFJqAhsW3/crBRIgLSEjvvzmYFsZkF4iS2TPkPFOfgEBaIknh1RhUkzCKg\r
+ KtG+5gZYmFfAQeLDsniIIxQlup9NYAOxOQUcJc5f/ccMcYODxOMTJ8FsXgFBiZMzn7CAtDIL\r
+ qEusnycEsUheonnrbOYJjEKzkFTNQqiahaRqASPzKkbZlNwq3dzEzJzi1GTd4uTEvLzUIl1z\r
+ vdzMEr3UlNJNjOAQd1HZwdh8SOkQowAHoxIPr8GKFYFCrIllxZW5hxglOZiURHlnO64MFOJL\r
+ yk+pzEgszogvKs1JLT7EKMHBrCTCmz8HqJw3JbGyKrUoHyYlzcGiJM57PeWmv5BAemJJanZq\r
+ akFqEUxWhoNDSYJXDRjLQoJFqempFWmZOSUIaSYOTpDhPEDDOUFqeIsLEnOLM9Mh8qcYdTna\r
+ vk5+xyjEkpeflyolzssPUiQAUpRRmgc3B5aaXjGKA70lzMsNUsUDTGtwk14BLWECWvLEejnI\r
+ kpJEhJRUA+PkjMV5Zw0ZBM+dW/BU9kLpi+AXkVvd89kOfGl6eXmVjvK+WiY1znezPOPTVI2m\r
+ HD07OdPoxoo07+n//CwTrj7UNa5rcd59ZlPb48MyrZ+5Ve/e+7PAWpkvIjU1acmWWWzrfph7\r
+ piX7z6kru9ekd9uhdef3GcrndHgW7ObSkmllk5kkV2MywUuJpTgj0VCLuag4EQDKoU21KAMA AA==\r
+Cc: tomi.ollila@iki.fi\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, 01 Jun 2013 00:40:50 -0000\r
+\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 | 207 +++++++++++++++++++++++++++++++++++++++++++++++\r
+ 2 files changed, 208 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..d59c0e1\r
+--- /dev/null\r
++++ b/emacs/notmuch-parser.el\r
+@@ -0,0 +1,207 @@\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 ()\r
++ "Return a new streaming S-expression parser.\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 point in the current\r
++buffer. Hence, the caller is allowed to delete any data before\r
++point and may resynchronize after an error by moving point."\r
++\r
++ (vector 'notmuch-sexp-parser\r
++ ;; List depth\r
++ 0\r
++ ;; Partial parse position marker\r
++ nil\r
++ ;; Partial parse state\r
++ nil))\r
++\r
++(defmacro notmuch-sexp--depth (sp) `(aref ,sp 1))\r
++(defmacro notmuch-sexp--partial-pos (sp) `(aref ,sp 2))\r
++(defmacro notmuch-sexp--partial-state (sp) `(aref ,sp 3))\r
++\r
++(defun notmuch-sexp-read (sp)\r
++ "Consume and return the value at point in the current 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
++ (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 to be consistent with all other code paths.\r
++ (read (current-buffer))\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
++ (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
++ (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 consumes an S-expression list from the current\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))\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
+-- \r
+1.7.10.4\r
+\r