--- /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 974E4431FB6\r
+ for <notmuch@notmuchmail.org>; Thu, 5 Jul 2012 01:30:22 -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 wlj16INpiuJE for <notmuch@notmuchmail.org>;\r
+ Thu, 5 Jul 2012 01:30:21 -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 20E47431FAE\r
+ for <notmuch@notmuchmail.org>; Thu, 5 Jul 2012 01:30:21 -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 1SmhS1-0007XL-NO; Thu, 05 Jul 2012 09:30:16 +0100\r
+Received: from 94-192-233-223.zone6.bethere.co.uk ([94.192.233.223]\r
+ helo=localhost)\r
+ by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.69)\r
+ (envelope-from <m.walters@qmul.ac.uk>)\r
+ id 1SmhS1-0002uJ-6j; Thu, 05 Jul 2012 09:30:13 +0100\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: Austin Clements <amdragon@MIT.EDU>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH 7/8] emacs: Implement an incremental JSON parser\r
+In-Reply-To: <1341354059-29396-8-git-send-email-amdragon@mit.edu>\r
+References: <1341354059-29396-1-git-send-email-amdragon@mit.edu>\r
+ <1341354059-29396-8-git-send-email-amdragon@mit.edu>\r
+User-Agent: Notmuch/0.13.2+70~gb6a56e7 (http://notmuchmail.org) Emacs/23.4.1\r
+ (x86_64-pc-linux-gnu)\r
+Date: Thu, 05 Jul 2012 09:30:09 +0100\r
+Message-ID: <873956fw4u.fsf@qmul.ac.uk>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+X-Sender-Host-Address: 94.192.233.223\r
+X-QM-SPAM-Info: Sender has good ham record. :)\r
+X-QM-Body-MD5: 961ee8e9e28306f3ed16f58e7506782e (of first 20000 bytes)\r
+X-SpamAssassin-Score: -1.8\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 -1.8 points.\r
+ Summary of the scoring: \r
+ * -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/,\r
+ * medium trust\r
+ * [138.37.6.40 listed in list.dnswl.org]\r
+ * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail\r
+ provider * (markwalters1009[at]gmail.com)\r
+ * -0.0 T_RP_MATCHES_RCVD Envelope sender domain matches handover relay\r
+ * domain\r
+ * 0.5 AWL AWL: From: address is in the auto white-list\r
+X-QM-Scan-Virus: ClamAV says the message is clean\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: Thu, 05 Jul 2012 08:30:22 -0000\r
+\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
+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
+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
+> -- \r
+> 1.7.10\r
+>\r
+> _______________________________________________\r
+> notmuch mailing list\r
+> notmuch@notmuchmail.org\r
+> http://notmuchmail.org/mailman/listinfo/notmuch\r