[PATCH 7/8] emacs: Implement an incremental JSON parser
authorAustin Clements <amdragon@MIT.EDU>
Tue, 3 Jul 2012 22:20:58 +0000 (18:20 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:47:58 +0000 (09:47 -0800)
c9/5652fe803443039a41213fae32158c419ec386 [new file with mode: 0644]

diff --git a/c9/5652fe803443039a41213fae32158c419ec386 b/c9/5652fe803443039a41213fae32158c419ec386
new file mode 100644 (file)
index 0000000..659505d
--- /dev/null
@@ -0,0 +1,296 @@
+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 910EF431FAE\r
+       for <notmuch@notmuchmail.org>; Tue,  3 Jul 2012 15:21:19 -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 rHKshUWe0KMz for <notmuch@notmuchmail.org>;\r
+       Tue,  3 Jul 2012 15:21:15 -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 AC1CB431FCB\r
+       for <notmuch@notmuchmail.org>; Tue,  3 Jul 2012 15:21:06 -0700 (PDT)\r
+X-AuditID: 12074424-b7f2a6d0000008bf-52-4ff370522e41\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 9B.F6.02239.25073FF4; Tue,  3 Jul 2012 18:21:06 -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 q63ML5NO010810; \r
+       Tue, 3 Jul 2012 18:21:05 -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 q63ML15l023225\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Tue, 3 Jul 2012 18:21:02 -0400 (EDT)\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1SmBSv-0007fj-2l; Tue, 03 Jul 2012 18:21:01 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 7/8] emacs: Implement an incremental JSON parser\r
+Date: Tue,  3 Jul 2012 18:20:58 -0400\r
+Message-Id: <1341354059-29396-8-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.7.10\r
+In-Reply-To: <1341354059-29396-1-git-send-email-amdragon@mit.edu>\r
+References: <1341354059-29396-1-git-send-email-amdragon@mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFjrLIsWRmVeSWpSXmKPExsUixG6nrhtU8Nnf4MEMNosje2axWyw985/Z\r
+       4vrNmcwWb1bOY3Vg8dg56y67x+GvC1k8nq26xezR9GMxawBLFJdNSmpOZllqkb5dAlfG2+YF\r
+       zAXdzhUzttU3MP426WLk4JAQMJGY3O/WxcgJZIpJXLi3ng3EFhLYxyjRtikQwl7PKPFruReE\r
+       fZJJ4m0LfxcjF5A9l1Hi7oRmJpAEm4CGxLb9yxlBbBEBaYmdd2ezgtjMArkSZ1sPg8WFBRwk\r
+       Dn37DRZnEVCVeDPhN1gvL1B89cPVjBBHyEs8vd8HdgSngKPEiptnWSAWO0hs+9DBOoGRfwEj\r
+       wypG2ZTcKt3cxMyc4tRk3eLkxLy81CJdc73czBK91JTSTYzgUHNR2cHYfEjpEKMAB6MSD2+i\r
+       wmd/IdbEsuLK3EOMkhxMSqK8V/KAQnxJ+SmVGYnFGfFFpTmpxYcYJTiYlUR4VycB5XhTEiur\r
+       UovyYVLSHCxK4rzXU276CwmkJ5akZqemFqQWwWRlODiUJHgZgDElJFiUmp5akZaZU4KQZuLg\r
+       BBnOAzR8WT7I8OKCxNzizHSI/ClGRSlx3p0gCQGQREZpHlwvLBW8YhQHekWYlxFkBQ8wjcB1\r
+       vwIazAQ0OG/xJ5DBJYkIKakGRqk2SYPy7be+ZCRZO2swGmY5ll66+L1m32Wv6IcTLmwQ9u1X\r
+       PbrhyGW5GKZjodP/LLs+reOhw9ejtW1pP1KOGqUbaTEdOL5uns2l5uU5Rxiru454virbsFT+\r
+       0WO3Lccumx6Vs/+6a2HiUa2rq0/U9+6YO01RIt362GbLK6ef+C24J7hj4dHu7d5KLMUZiYZa\r
+       zEXFiQBVISnA4AIAAA==\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: Tue, 03 Jul 2012 22:21:20 -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
+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
++  (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