[PATCH v2 7/9] emacs: Implement an incremental JSON parser
authorAustin Clements <amdragon@MIT.EDU>
Thu, 5 Jul 2012 20:52:25 +0000 (16:52 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:48:02 +0000 (09:48 -0800)
ac/669200d495b82f56b245d728a89b81fbbfd85e [new file with mode: 0644]

diff --git a/ac/669200d495b82f56b245d728a89b81fbbfd85e b/ac/669200d495b82f56b245d728a89b81fbbfd85e
new file mode 100644 (file)
index 0000000..5daad9d
--- /dev/null
@@ -0,0 +1,314 @@
+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 AFE68431FC3\r
+       for <notmuch@notmuchmail.org>; Thu,  5 Jul 2012 13:52:48 -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 LPWVmsQDGSvx for <notmuch@notmuchmail.org>;\r
+       Thu,  5 Jul 2012 13:52:46 -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 5A0B6431FBF\r
+       for <notmuch@notmuchmail.org>; Thu,  5 Jul 2012 13:52:43 -0700 (PDT)\r
+X-AuditID: 12074424-b7f2a6d0000008bf-2c-4ff5fe9a66df\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+       by dmz-mailsec-scanner-7.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id 76.5B.02239.A9EF5FF4; Thu,  5 Jul 2012 16:52:42 -0400 (EDT)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+       by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id q65Kqgse019983; \r
+       Thu, 5 Jul 2012 16:52:42 -0400\r
+Received: from drake.dyndns.org (26-4-182.dynamic.csail.mit.edu [18.26.4.182])\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 q65KqbrC027240\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Thu, 5 Jul 2012 16:52:40 -0400 (EDT)\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1Smt2T-0004Xn-Lh; Thu, 05 Jul 2012 16:52:37 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 7/9] emacs: Implement an incremental JSON parser\r
+Date: Thu,  5 Jul 2012 16:52:25 -0400\r
+Message-Id: <1341521547-15502-8-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.7.10\r
+In-Reply-To: <1341521547-15502-1-git-send-email-amdragon@mit.edu>\r
+References: <1341354059-29396-1-git-send-email-amdragon@mit.edu>\r
+       <1341521547-15502-1-git-send-email-amdragon@mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFjrOIsWRmVeSWpSXmKPExsUixG6nrjvr31d/g9N/1SxWz+WxuH5zJrPF\r
+       m5XzWB2YPXbOusvucfjrQhaPZ6tuMQcwR3HZpKTmZJalFunbJXBlLO78ylqwyaPi584HjA2M\r
+       vZZdjJwcEgImElv2LmCBsMUkLtxbz9bFyMUhJLCPUeLb7afsEM56RolVy4+yQDgnmSRerj4G\r
+       lZnLKLHr73OwfjYBDYlt+5czgtgiAtISO+/OZgWxmQXiJLZM+Q8WFxZwlji76QBYnEVAVWL9\r
+       mRYmEJtXwEGibcF8dog75CWe3u9jA7E5BRwlLkxcDNYrJFAu8WfJP5YJjPwLGBlWMcqm5Fbp\r
+       5iZm5hSnJusWJyfm5aUW6Zrr5WaW6KWmlG5iBIeXi8oOxuZDSocYBTgYlXh4DXO/+AuxJpYV\r
+       V+YeYpTkYFIS5W38/dVfiC8pP6UyI7E4I76oNCe1+BCjBAezkghvbwZQjjclsbIqtSgfJiXN\r
+       waIkzns95aa/kEB6YklqdmpqQWoRTFaGg0NJgvfpX6BGwaLU9NSKtMycEoQ0EwcnyHAeoOEv\r
+       QGp4iwsSc4sz0yHypxgVpYBGgyQEQBIZpXlwvbD4f8UoDvSKMO8nkCoeYOqA634FNJgJaHDe\r
+       4k8gg0sSEVJSDYyRU7r1XbUXCLGu3Hd67TozlbSNEkLWeTt6Y/Z+Up7z9NC7PX9uaH3YfFgj\r
+       9OHXN7vatu7Z9WxCjnd/6Uy955d73kx4+/DKB53ZYe8LPSwjN9gc/NjasG6F3ffdoUtFzH7r\r
+       z+Bb993ObvahzRK/dHZyZzhdTFz8df5f5ZBt9heDjnhzbao0/ajIrMRSnJFoqMVcVJwIAPyt\r
+       WdjaAgAA\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 20:52:49 -0000\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 supports efficient partial parsing, so there's no need to\r
+frame the input for correctness or performance.\r
+\r
+The bulk of the parsing is still done by Emacs' json.el, so any\r
+improvements or optimizations to that will benefit the incremental\r
+parser as well.\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 |  196 ++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ 1 file changed, 196 insertions(+)\r
+\r
+diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el\r
+index c829df3..9e04d97 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,201 @@ 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 moves point past the token that opens\r
++the compound and returns t.  Later calls to `notmuch-json-read'\r
++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, moves point to just past the end of the value and\r
++returns the value."\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
++(defun notmuch-json-eof (jp)\r
++  "Signal a json-error if there is more input in JP's buffer.\r
++\r
++Moves point to the beginning of any trailing garbage or to the\r
++end of the buffer if there is no trailing garbage."\r
++\r
++  (with-current-buffer (notmuch-json-buffer jp)\r
++    (skip-chars-forward " \t\r\n")\r
++    (unless (eobp)\r
++      (signal 'json-error (list "Trailing garbage following JSON data")))))\r
++\r
+ (provide 'notmuch-lib)\r
\r
+ ;; Local Variables:\r
+-- \r
+1.7.10\r
+\r