--- /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 43011431FAF\r
+ for <notmuch@notmuchmail.org>; Thu, 5 Jul 2012 11:36:25 -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 xQXdBg+Hsd-E for <notmuch@notmuchmail.org>;\r
+ Thu, 5 Jul 2012 11:36:24 -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 AC51C431FAE\r
+ for <notmuch@notmuchmail.org>; Thu, 5 Jul 2012 11:36:23 -0700 (PDT)\r
+X-AuditID: 12074424-b7f2a6d0000008bf-73-4ff5dea6533d\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 4B.55.02239.6AED5FF4; Thu, 5 Jul 2012 14:36:22 -0400 (EDT)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+ by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id q65IaLka008157; \r
+ Thu, 5 Jul 2012 14:36:21 -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.6/8.12.4) with ESMTP id q65IaJKa023407\r
+ (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+ Thu, 5 Jul 2012 14:36:20 -0400 (EDT)\r
+Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.77)\r
+ (envelope-from <amdragon@mit.edu>)\r
+ id 1SmquZ-00045N-36; Thu, 05 Jul 2012 14:36:19 -0400\r
+Date: Thu, 5 Jul 2012 14:36:19 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: Mark Walters <markwalters1009@gmail.com>\r
+Subject: Re: [PATCH 7/8] emacs: Implement an incremental JSON parser\r
+Message-ID: <20120705183619.GI21653@mit.edu>\r
+References: <1341354059-29396-1-git-send-email-amdragon@mit.edu>\r
+ <1341354059-29396-8-git-send-email-amdragon@mit.edu>\r
+ <873956fw4u.fsf@qmul.ac.uk>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+Content-Disposition: inline\r
+In-Reply-To: <873956fw4u.fsf@qmul.ac.uk>\r
+User-Agent: Mutt/1.5.21 (2010-09-15)\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFuplleLIzCtJLcpLzFFi42IRYrdT0V1276u/wc/9ihar5/JYXL85k9ni\r
+ zcp5rA7MHjtn3WX3OPx1IYvHs1W3mAOYo7hsUlJzMstSi/TtErgyGtbNYi/oDqw4MeUnUwNj\r
+ i10XIyeHhICJxOH5l5ggbDGJC/fWs3UxcnEICexjlNj76QojhLOeUaL/wS1WCOcEk8Txo5uZ\r
+ IZwljBKrb7SxgvSzCKhIbFm8CWwWm4CGxLb9yxlBbBEBHYnbhxawg9jMAvoSK0/OZAaxhQVc\r
+ JM5+Og9WwwtU83HVcaihUxkl2u5+YIVICEqcnPmEBaJZS+LGv5dACziAbGmJ5f84QExOoF0d\r
+ c9RAKkSBTphychvbBEahWUiaZyFpnoXQvICReRWjbEpulW5uYmZOcWqybnFyYl5eapGuuV5u\r
+ ZoleakrpJkZwqLuo7GBsPqR0iFGAg1GJh9cw94u/EGtiWXFl7iFGSQ4mJVHe11e/+gvxJeWn\r
+ VGYkFmfEF5XmpBYfYpTgYFYS4e3NAMrxpiRWVqUW5cOkpDlYlMR5r6fc9BcSSE8sSc1OTS1I\r
+ LYLJynBwKEnwGgFjWkiwKDU9tSItM6cEIc3EwQkynAdouBlIDW9xQWJucWY6RP4Uo6KUOO+9\r
+ u0AJAZBERmkeXC8sFb1iFAd6RZiXF6SdB5jG4LpfAQ1mAhqct/gTyOCSRISUVAPjrOrcqax/\r
+ 7b805tu0hstyrpl14cfy6KiJYtER6o1dfDNfZH/flJZv9GZjVUTV/UI5zT7FtA3JZ2Ykm7Hw\r
+ q3k+2Ld+iuosa9HrOw5V8c7KepS1LJdh92/NUxa8Nu3/erL2v3h30v67SlnAB8Np9kai0bvb\r
+ rk/oyamsOnXUJdUnKXPlvCt2ETlKLMUZiYZazEXFiQDUe/HSIAMAAA==\r
+Cc: tomi.ollila@iki.fi, 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: Thu, 05 Jul 2012 18:36:26 -0000\r
+\r
+Quoth Mark Walters on Jul 05 at 9:30 am:\r
+> On Tue, 03 Jul 2012, Austin Clements <amdragon@MIT.EDU> wrote:\r
+> > This parser is designed to read streaming JSON whose structure is\r
+> > known to the caller. Like a typical JSON parsing interface, it\r
+> > provides a function to read a complete JSON value from the input.\r
+> > However, it extends this with an additional function that\r
+> > requires the next value in the input to be a compound value and\r
+> > descends into it, allowing its elements to be read one at a time\r
+> > or further descended into. Both functions can return 'retry to\r
+> > indicate that not enough input is available.\r
+> >\r
+> > The parser supports efficient partial parsing, so there's no need to\r
+> > frame the input for correctness or performance.\r
+> >\r
+> > Currently only descending into JSON lists is supported because that's\r
+> > all we need, but support for descending into JSON objects can be added\r
+> > in the future.\r
+> > ---\r
+> > emacs/notmuch-lib.el | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++\r
+> > 1 file changed, 183 insertions(+)\r
+> >\r
+> > diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el\r
+> > index c829df3..f7cda33 100644\r
+> > --- a/emacs/notmuch-lib.el\r
+> > +++ b/emacs/notmuch-lib.el\r
+> > @@ -23,6 +23,7 @@\r
+> > \r
+> > (require 'mm-view)\r
+> > (require 'mm-decode)\r
+> > +(require 'json)\r
+> > (eval-when-compile (require 'cl))\r
+> > \r
+> > (defvar notmuch-command "notmuch"\r
+> > @@ -296,6 +297,188 @@ was called."\r
+> > (defvar notmuch-show-process-crypto nil)\r
+> > (make-variable-buffer-local 'notmuch-show-process-crypto)\r
+> > \r
+> > +;; Incremental JSON parsing\r
+> > +\r
+> > +(defun notmuch-json-create-parser (buffer)\r
+> > + "Return a streaming JSON parser that consumes input from BUFFER.\r
+> > +\r
+> > +This parser is designed to read streaming JSON whose structure is\r
+> > +known to the caller. Like a typical JSON parsing interface, it\r
+> > +provides a function to read a complete JSON value from the input.\r
+> > +However, it extends this with an additional function that\r
+> > +requires the next value in the input to be a compound value and\r
+> > +descends into it, allowing its elements to be read one at a time\r
+> > +or further descended into. Both functions can return 'retry to\r
+> > +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 and data before point and may\r
+> > +resynchronize after an error by moving point."\r
+> > +\r
+> > + (list buffer\r
+> > + ;; Terminator stack: a stack of characters that indicate the\r
+> > + ;; end of the compound values enclosing point\r
+> > + '()\r
+> > + ;; Next: One of\r
+> > + ;; * 'expect-value if the next token must be a value, but a\r
+> > + ;; value has not yet been reached\r
+> > + ;; * 'value if point is at the beginning of a value\r
+> > + ;; * 'expect-comma if the next token must be a comma\r
+> > + 'expect-value\r
+> > + ;; Allow terminator: non-nil if the next token may be a\r
+> > + ;; terminator\r
+> > + nil\r
+> > + ;; Partial parse position: If state is 'value, a marker for\r
+> > + ;; the position of the partial parser or nil if no partial\r
+> > + ;; parsing has happened yet\r
+> > + nil\r
+> > + ;; Partial parse state: If state is 'value, the current\r
+> > + ;; `parse-partial-sexp' state\r
+> > + nil))\r
+> > +\r
+> > +(defmacro notmuch-json-buffer (jp) `(first ,jp))\r
+> > +(defmacro notmuch-json-term-stack (jp) `(second ,jp))\r
+> > +(defmacro notmuch-json-next (jp) `(third ,jp))\r
+> > +(defmacro notmuch-json-allow-term (jp) `(fourth ,jp))\r
+> > +(defmacro notmuch-json-partial-pos (jp) `(fifth ,jp))\r
+> > +(defmacro notmuch-json-partial-state (jp) `(sixth ,jp))\r
+> > +\r
+> > +(defvar notmuch-json-syntax-table\r
+> > + (let ((table (make-syntax-table)))\r
+> > + ;; The standard syntax table is what we need except that "." needs\r
+> > + ;; to have word syntax instead of punctuation syntax.\r
+> > + (modify-syntax-entry ?. "w" table)\r
+> > + table)\r
+> > + "Syntax table used for incremental JSON parsing.")\r
+> > +\r
+> > +(defun notmuch-json-scan-to-value (jp)\r
+> > + ;; Helper function that consumes separators, terminators, and\r
+> > + ;; whitespace from point. Returns nil if it successfully reached\r
+> > + ;; the beginning of a value, 'end if it consumed a terminator, or\r
+> > + ;; 'retry if not enough input was available to reach a value. Upon\r
+> > + ;; nil return, (notmuch-json-next jp) is always 'value.\r
+> > +\r
+> > + (if (eq (notmuch-json-next jp) 'value)\r
+> > + ;; We're already at a value\r
+> > + nil\r
+> > + ;; Drive the state toward 'expect-value\r
+> > + (skip-chars-forward " \t\r\n")\r
+> > + (or (when (eobp) 'retry)\r
+> > + ;; Test for the terminator for the current compound\r
+> > + (when (and (notmuch-json-allow-term jp)\r
+> > + (eq (char-after) (car (notmuch-json-term-stack jp))))\r
+> > + ;; Consume it and expect a comma or terminator next\r
+> > + (forward-char)\r
+> > + (setf (notmuch-json-term-stack jp) (cdr (notmuch-json-term-stack jp))\r
+> > + (notmuch-json-next jp) 'expect-comma\r
+> > + (notmuch-json-allow-term jp) t)\r
+> > + 'end)\r
+> > + ;; Test for a separator\r
+> > + (when (eq (notmuch-json-next jp) 'expect-comma)\r
+> > + (when (/= (char-after) ?,)\r
+> > + (signal 'json-readtable-error (list "expected ','")))\r
+> > + ;; Consume it, switch to 'expect-value, and disallow a\r
+> > + ;; terminator\r
+> > + (forward-char)\r
+> > + (skip-chars-forward " \t\r\n")\r
+> > + (setf (notmuch-json-next jp) 'expect-value\r
+> > + (notmuch-json-allow-term jp) nil)\r
+> > + ;; We moved point, so test for eobp again and fall through\r
+> > + ;; to the next test if there's more input\r
+> > + (when (eobp) 'retry))\r
+> > + ;; Next must be 'expect-value and we know this isn't\r
+> > + ;; whitespace, EOB, or a terminator, so point must be on a\r
+> > + ;; value\r
+> > + (progn\r
+> > + (assert (eq (notmuch-json-next jp) 'expect-value))\r
+> > + (setf (notmuch-json-next jp) 'value)\r
+> > + nil))))\r
+> > +\r
+> > +(defun notmuch-json-begin-compound (jp)\r
+> > + "Parse the beginning of a compound value and traverse inside it.\r
+> > +\r
+> > +Returns 'retry if there is insufficient input to parse the\r
+> > +beginning of the compound. If this is able to parse the\r
+> > +beginning of a compound, it returns t and later calls to\r
+> > +`notmuch-json-read' will return the compound's elements.\r
+> > +\r
+> > +Entering JSON objects is current unimplemented."\r
+> > +\r
+> > + (with-current-buffer (notmuch-json-buffer jp)\r
+> > + ;; Disallow terminators\r
+> > + (setf (notmuch-json-allow-term jp) nil)\r
+> > + (or (notmuch-json-scan-to-value jp)\r
+> > + (if (/= (char-after) ?\[)\r
+> > + (signal 'json-readtable-error (list "expected '['"))\r
+> > + (forward-char)\r
+> > + (push ?\] (notmuch-json-term-stack jp))\r
+> > + ;; Expect a value or terminator next\r
+> > + (setf (notmuch-json-next jp) 'expect-value\r
+> > + (notmuch-json-allow-term jp) t)\r
+> > + t))))\r
+> > +\r
+> > +(defun notmuch-json-read (jp)\r
+> > + "Parse the value at point in JP's buffer.\r
+> > +\r
+> > +Returns 'retry if there is insufficient input to parse a complete\r
+> > +JSON value. If the parser is currently inside a compound value\r
+> > +and the next token ends the list or object, returns 'end.\r
+> > +Otherwise, returns the value."\r
+> \r
+> \r
+> This looks excellent! My only comment is that I think it would be\r
+> helpful it the above comment and the one for notmuch-json-begin-compound\r
+> said what happens to point when they get called.\r
+\r
+Good idea.\r
+\r
+> Also, I think in practice a lot of the parsing is done by json.el (that\r
+> you use this to enter one level of the hierarchy and then use this to\r
+> ensure that json.el only gets complete objects to parse) so I assume\r
+> this means that your much faster replacement for json.el could be\r
+> slotted in? It might be worth mentioning that in the commit message.\r
+\r
+Yes, definitely. My hope is that we'll skip over the optimized JSON\r
+parser and move straight to S-expressions, which perform ~3X better\r
+than my optimized json.el (and ~8X better than Emacs' json.el).\r
+\r
+> Best wishes\r
+> \r
+> Mark\r
+> \r
+> > +\r
+> > + (with-current-buffer (notmuch-json-buffer jp)\r
+> > + (or\r
+> > + ;; Get to a value state\r
+> > + (notmuch-json-scan-to-value jp)\r
+> > +\r
+> > + ;; Can we parse a complete value?\r
+> > + (let ((complete\r
+> > + (if (looking-at "[-+0-9tfn]")\r
+> > + ;; This is a number or a keyword, so the partial\r
+> > + ;; parser isn't going to help us because a truncated\r
+> > + ;; number or keyword looks like a complete symbol to\r
+> > + ;; it. Look for something that clearly ends it.\r
+> > + (save-excursion\r
+> > + (skip-chars-forward "^]},: \t\r\n")\r
+> > + (not (eobp)))\r
+> > +\r
+> > + ;; We're looking at a string, object, or array, which we\r
+> > + ;; can partial parse. If we just reached the value, set\r
+> > + ;; up the partial parser.\r
+> > + (when (null (notmuch-json-partial-state jp))\r
+> > + (setf (notmuch-json-partial-pos jp) (point-marker)))\r
+> > +\r
+> > + ;; Extend the partial parse until we either reach EOB or\r
+> > + ;; get the whole value\r
+> > + (save-excursion\r
+> > + (let ((pstate\r
+> > + (with-syntax-table notmuch-json-syntax-table\r
+> > + (parse-partial-sexp\r
+> > + (notmuch-json-partial-pos jp) (point-max) 0 nil\r
+> > + (notmuch-json-partial-state jp)))))\r
+> > + ;; A complete value is available if we've reached\r
+> > + ;; depth 0 or less and encountered a complete\r
+> > + ;; subexpression.\r
+> > + (if (and (<= (first pstate) 0) (third pstate))\r
+> > + t\r
+> > + ;; Not complete. Update the partial parser state\r
+> > + (setf (notmuch-json-partial-pos jp) (point-marker)\r
+> > + (notmuch-json-partial-state jp) pstate)\r
+> > + nil))))))\r
+> > +\r
+> > + (if (not complete)\r
+> > + 'retry\r
+> > + ;; We have a value. Reset the partial parse state and expect\r
+> > + ;; a comma or terminator after the value.\r
+> > + (setf (notmuch-json-next jp) 'expect-comma\r
+> > + (notmuch-json-allow-term jp) t\r
+> > + (notmuch-json-partial-pos jp) nil\r
+> > + (notmuch-json-partial-state jp) nil)\r
+> > + ;; Parse the value\r
+> > + (let* ((json-object-type 'plist)\r
+> > + (json-array-type 'list)\r
+> > + (json-false nil))\r
+> > + (json-read)))))))\r
+> > +\r
+> > (provide 'notmuch-lib)\r
+> > \r
+> > ;; Local Variables:\r