[PATCH 4/5] emacs: Streaming S-expression parser
authorAustin Clements <amdragon@MIT.EDU>
Sat, 18 May 2013 04:31:11 +0000 (00:31 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:54:49 +0000 (09:54 -0800)
eb/96729bb8891068f1ac8a191bac4671e1bff81b [new file with mode: 0644]

diff --git a/eb/96729bb8891068f1ac8a191bac4671e1bff81b b/eb/96729bb8891068f1ac8a191bac4671e1bff81b
new file mode 100644 (file)
index 0000000..0eaded6
--- /dev/null
@@ -0,0 +1,329 @@
+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 1B19E431FC2\r
+       for <notmuch@notmuchmail.org>; Fri, 17 May 2013 21:32:22 -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 cRhIAVnnV607 for <notmuch@notmuchmail.org>;\r
+       Fri, 17 May 2013 21:32:14 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-4.mit.edu (DMZ-MAILSEC-SCANNER-4.MIT.EDU\r
+       [18.9.25.15])\r
+       by olra.theworths.org (Postfix) with ESMTP id 039E8431FC7\r
+       for <notmuch@notmuchmail.org>; Fri, 17 May 2013 21:32:13 -0700 (PDT)\r
+X-AuditID: 1209190f-b7f256d000005616-b6-5197044d4c48\r
+Received: from mailhub-auth-3.mit.edu ( [18.9.21.43])\r
+       by dmz-mailsec-scanner-4.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id 5F.3E.22038.D4407915; Sat, 18 May 2013 00:32:13 -0400 (EDT)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH-1.MIT.EDU [18.9.28.11])\r
+       by mailhub-auth-3.mit.edu (8.13.8/8.9.2) with ESMTP id r4I4W9NA022588; \r
+       Sat, 18 May 2013 00:32:09 -0400\r
+Received: from drake.dyndns.org\r
+       (216-15-114-40.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com\r
+       [216.15.114.40]) (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 r4I4W6xL018816\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Sat, 18 May 2013 00:32:08 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1UdYo1-0001Z0-L0; Sat, 18 May 2013 00:31:41 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 4/5] emacs: Streaming S-expression parser\r
+Date: Sat, 18 May 2013 00:31:11 -0400\r
+Message-Id: <1368851472-5382-5-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <1368851472-5382-1-git-send-email-amdragon@mit.edu>\r
+References: <1368851472-5382-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+NgFupjleLIzCtJLcpLzFFi42IR4hTV1vVlmR5osGadosXquTwW12/OZHZg\r
+       8tg56y67x7NVt5gDmKK4bFJSczLLUov07RK4MhY+ucBY0OVd0fThBGMD43erLkZODgkBE4lV\r
+       O44zQ9hiEhfurWfrYuTiEBLYxyhx8sFuFghnI6PE7wP7oDJ3mCTWfdoF1iIkMJdR4spRIRCb\r
+       TUBDYtv+5YwgtoiAtMTOu7NZQWxmAUeJz/sXsYHYwgKhEjsv/QCLswioSpyZdgxsDq+AvcSq\r
+       1umsEGcoSnQ/mwBWzyngIHF4/jE2iF32El0XbrJB1AtKnJz5BOg6DqD56hLr5wlBrJKXaN46\r
+       m3kCo9AsJFWzEKpmIalawMi8ilE2JbdKNzcxM6c4NVm3ODkxLy+1SNdELzezRC81pXQTIzio\r
+       Jfl3MH47qHSIUYCDUYmH96PrtEAh1sSy4srcQ4ySHExKorwm/4BCfEn5KZUZicUZ8UWlOanF\r
+       hxglOJiVRHifFwPleFMSK6tSi/JhUtIcLErivFdTbvoLCaQnlqRmp6YWpBbBZGU4OJQkeHWZ\r
+       pwcKCRalpqdWpGXmlCCkmTg4QYbzAA23AqnhLS5IzC3OTIfIn2LU5Wj7OvkdoxBLXn5eqpQ4\r
+       rzNIkQBIUUZpHtwcWDJ6xSgO9JYwrzRIFQ8wkcFNegW0hAloCeu1qSBLShIRUlINjEEKX/fa\r
+       f859G3jGVqqVve82Y5Sa/8qIZ1Nf8izSiq0X0Zlqc77ku/D8ifO/M79aa/zwAUvWcmnusLin\r
+       Lq4HHH4mpbLPXCUs1/tinXnBJ4OsY2wKJlanvO+K2Lkb/9lkPb2b82rTv9tmj0LvszbtcNR8\r
+       5vMqpGTpN7mCq/IzS2xEpW+82DX3jRJLcUaioRZzUXEiAEhDwYYhAwAA\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, 18 May 2013 04:32:22 -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 |  212 +++++++++++++++++++++++++++++++++++++++++++++++\r
+ 2 files changed, 213 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..1b7cf64\r
+--- /dev/null\r
++++ b/emacs/notmuch-parser.el\r
+@@ -0,0 +1,212 @@\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 (buffer)\r
++  "Return a streaming S-expression parser that reads from BUFFER.\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 BUFFER's point.  Hence, the\r
++caller is allowed to delete any data before point and may\r
++resynchronize after an error by moving point."\r
++\r
++  (vector 'notmuch-sexp-parser\r
++        buffer\r
++        ;; List depth\r
++        0\r
++        ;; Partial parse position marker\r
++        nil\r
++        ;; Partial parse state\r
++        nil))\r
++\r
++(defmacro notmuch-sexp--buffer (sp)        `(aref ,sp 1))\r
++(defmacro notmuch-sexp--depth (sp)         `(aref ,sp 2))\r
++(defmacro notmuch-sexp--partial-pos (sp)   `(aref ,sp 3))\r
++(defmacro notmuch-sexp--partial-state (sp) `(aref ,sp 4))\r
++\r
++(defun notmuch-sexp-read (sp)\r
++  "Consume and return the value at point in SP's 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
++  (with-current-buffer (notmuch-sexp--buffer sp)\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.\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
++  (with-current-buffer (notmuch-sexp--buffer sp)\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
++  (with-current-buffer (notmuch-sexp--buffer sp)\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 consume 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 (current-buffer)))\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