1 Return-Path: <m.walters@qmul.ac.uk>
\r
2 X-Original-To: notmuch@notmuchmail.org
\r
3 Delivered-To: notmuch@notmuchmail.org
\r
4 Received: from localhost (localhost [127.0.0.1])
\r
5 by olra.theworths.org (Postfix) with ESMTP id 252E2431FC0
\r
6 for <notmuch@notmuchmail.org>; Tue, 21 May 2013 08:20:59 -0700 (PDT)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5
\r
12 tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001,
\r
13 NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled
\r
14 Received: from olra.theworths.org ([127.0.0.1])
\r
15 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
16 with ESMTP id TmNBrcZJbhd7 for <notmuch@notmuchmail.org>;
\r
17 Tue, 21 May 2013 08:20:49 -0700 (PDT)
\r
18 Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6])
\r
19 (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))
\r
20 (No client certificate requested)
\r
21 by olra.theworths.org (Postfix) with ESMTPS id C3872431FB6
\r
22 for <notmuch@notmuchmail.org>; Tue, 21 May 2013 08:20:48 -0700 (PDT)
\r
23 Received: from smtp.qmul.ac.uk ([138.37.6.40])
\r
24 by mail2.qmul.ac.uk with esmtp (Exim 4.71)
\r
25 (envelope-from <m.walters@qmul.ac.uk>)
\r
26 id 1UeoMo-0006uE-Qb; Tue, 21 May 2013 16:20:47 +0100
\r
27 Received: from 93-97-24-31.zone5.bethere.co.uk ([93.97.24.31] helo=localhost)
\r
28 by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.71)
\r
29 (envelope-from <m.walters@qmul.ac.uk>)
\r
30 id 1UeoMo-000577-6x; Tue, 21 May 2013 16:20:46 +0100
\r
31 From: Mark Walters <markwalters1009@gmail.com>
\r
32 To: Austin Clements <amdragon@MIT.EDU>, notmuch@notmuchmail.org
\r
33 Subject: Re: [PATCH 4/5] emacs: Streaming S-expression parser
\r
34 In-Reply-To: <1368851472-5382-5-git-send-email-amdragon@mit.edu>
\r
35 References: <1368851472-5382-1-git-send-email-amdragon@mit.edu>
\r
36 <1368851472-5382-5-git-send-email-amdragon@mit.edu>
\r
37 User-Agent: Notmuch/0.14+255~gff3cc55 (http://notmuchmail.org) Emacs/23.4.1
\r
38 (x86_64-pc-linux-gnu)
\r
39 Date: Tue, 21 May 2013 16:20:43 +0100
\r
40 Message-ID: <87fvxg2wc4.fsf@qmul.ac.uk>
\r
42 Content-Type: text/plain; charset=utf-8
\r
43 Content-Transfer-Encoding: quoted-printable
\r
44 X-Sender-Host-Address: 93.97.24.31
\r
45 X-QM-SPAM-Info: Sender has good ham record. :)
\r
46 X-QM-Body-MD5: f9436054f15c52f0a216976259eed7a2 (of first 20000 bytes)
\r
47 X-SpamAssassin-Score: -0.2
\r
48 X-SpamAssassin-SpamBar: /
\r
49 X-SpamAssassin-Report: The QM spam filters have analysed this message to
\r
51 spam. We require at least 5.0 points to mark a message as spam.
\r
52 This message scored -0.2 points.
\r
53 Summary of the scoring:
\r
54 * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail
\r
55 provider * (markwalters1009[at]gmail.com)
\r
56 * -0.2 AWL AWL: From: address is in the auto white-list
\r
57 X-QM-Scan-Virus: ClamAV says the message is clean
\r
58 X-BeenThere: notmuch@notmuchmail.org
\r
59 X-Mailman-Version: 2.1.13
\r
61 List-Id: "Use and development of the notmuch mail system."
\r
62 <notmuch.notmuchmail.org>
\r
63 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
64 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
65 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
66 List-Post: <mailto:notmuch@notmuchmail.org>
\r
67 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
68 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
69 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
70 X-List-Received-Date: Tue, 21 May 2013 15:20:59 -0000
\r
75 This patch looks good to me. Some minor comments below.
\r
82 On Sat, 18 May 2013, Austin Clements <amdragon@MIT.EDU> wrote:
\r
83 > This provides the same interface as the streaming JSON parser, but
\r
84 > reads S-expressions incrementally. The only difference is that the
\r
85 > `notmuch-sexp-parse-partial-list' helper does not handle interleaved
\r
86 > error messages (since we now have the ability to separate these out at
\r
87 > the invocation level), so it no longer takes an error function and
\r
88 > does not need to do the horrible resynchronization that the JSON
\r
91 > Some implementation improvements have been made over the JSON parser.
\r
92 > This uses a vector instead of a list for the parser data structure,
\r
93 > since this allows faster access to elements (and modern versions of
\r
94 > Emacs handle storage of small vectors efficiently). Private functions
\r
95 > follow the "prefix--name" convention. And the implementation is much
\r
96 > simpler overall because S-expressions are much easier to parse.
\r
98 > emacs/Makefile.local | 1 +
\r
99 > emacs/notmuch-parser.el | 212 +++++++++++++++++++++++++++++++++++++++++=
\r
101 > 2 files changed, 213 insertions(+)
\r
102 > create mode 100644 emacs/notmuch-parser.el
\r
104 > diff --git a/emacs/Makefile.local b/emacs/Makefile.local
\r
105 > index 456700a..a910aff 100644
\r
106 > --- a/emacs/Makefile.local
\r
107 > +++ b/emacs/Makefile.local
\r
110 > emacs_sources :=3D \
\r
111 > $(dir)/notmuch-lib.el \
\r
112 > + $(dir)/notmuch-parser.el \
\r
113 > $(dir)/notmuch.el \
\r
114 > $(dir)/notmuch-query.el \
\r
115 > $(dir)/notmuch-show.el \
\r
116 > diff --git a/emacs/notmuch-parser.el b/emacs/notmuch-parser.el
\r
117 > new file mode 100644
\r
118 > index 0000000..1b7cf64
\r
120 > +++ b/emacs/notmuch-parser.el
\r
121 > @@ -0,0 +1,212 @@
\r
122 > +;; notmuch-parser.el --- streaming S-expression parser
\r
124 > +;; Copyright =C2=A9 Austin Clements
\r
126 > +;; This file is part of Notmuch.
\r
128 > +;; Notmuch is free software: you can redistribute it and/or modify it
\r
129 > +;; under the terms of the GNU General Public License as published by
\r
130 > +;; the Free Software Foundation, either version 3 of the License, or
\r
131 > +;; (at your option) any later version.
\r
133 > +;; Notmuch is distributed in the hope that it will be useful, but
\r
134 > +;; WITHOUT ANY WARRANTY; without even the implied warranty of
\r
135 > +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
136 > +;; General Public License for more details.
\r
138 > +;; You should have received a copy of the GNU General Public License
\r
139 > +;; along with Notmuch. If not, see <http://www.gnu.org/licenses/>.
\r
141 > +;; Authors: Austin Clements <aclements@csail.mit.edu>
\r
145 > +(defun notmuch-sexp-create-parser (buffer)
\r
146 > + "Return a streaming S-expression parser that reads from BUFFER.
\r
148 > +This parser is designed to incrementally read an S-expression
\r
149 > +whose structure is known to the caller. Like a typical
\r
150 > +S-expression parsing interface, it provides a function to read a
\r
151 > +complete S-expression from the input. However, it extends this
\r
152 > +with an additional function that requires the next value in the
\r
153 > +input to be a list and descends into it, allowing its elements to
\r
154 > +be read one at a time or further descended into. Both functions
\r
155 > +can return 'retry to indicate that not enough input is available.
\r
157 > +The parser always consumes input from BUFFER's point. Hence, the
\r
158 > +caller is allowed to delete any data before point and may
\r
159 > +resynchronize after an error by moving point."
\r
161 > + (vector 'notmuch-sexp-parser
\r
165 > + ;; Partial parse position marker
\r
167 > + ;; Partial parse state
\r
170 > +(defmacro notmuch-sexp--buffer (sp) `(aref ,sp 1))
\r
171 > +(defmacro notmuch-sexp--depth (sp) `(aref ,sp 2))
\r
172 > +(defmacro notmuch-sexp--partial-pos (sp) `(aref ,sp 3))
\r
173 > +(defmacro notmuch-sexp--partial-state (sp) `(aref ,sp 4))
\r
175 Why the double hyphen --? Is it a name-space or some convention?
\r
178 > +(defun notmuch-sexp-read (sp)
\r
179 > + "Consume and return the value at point in SP's buffer.
\r
181 > +Returns 'retry if there is insufficient input to parse a complete
\r
182 > +value (though it may still move point over whitespace). If the
\r
183 > +parser is currently inside a list and the next token ends the
\r
184 > +list, this moves point just past the terminator and returns 'end.
\r
185 > +Otherwise, this moves point to just past the end of the value and
\r
186 > +returns the value."
\r
188 > + (with-current-buffer (notmuch-sexp--buffer sp)
\r
189 > + (skip-chars-forward " \n\r\t")
\r
190 > + (cond ((eobp) 'retry)
\r
191 > + ((=3D (char-after) ?\))
\r
192 > + ;; We've reached the end of a list
\r
193 > + (if (=3D (notmuch-sexp--depth sp) 0)
\r
194 > + ;; .. but we weren't in a list. Let read signal the
\r
196 > + (read (current-buffer))
\r
198 Why is good for read to signal the error rather than us doing it?
\r
200 > + ;; Go up a level and return an end token
\r
201 > + (decf (notmuch-sexp--depth sp))
\r
204 > + ((=3D (char-after) ?\()
\r
205 > + ;; We're at the beginning of a list. If we haven't started
\r
206 > + ;; a partial parse yet, attempt to read the list in its
\r
207 > + ;; entirety. If this fails, or we've started a partial
\r
208 > + ;; parse, extend the partial parse to figure out when we
\r
209 > + ;; have a complete list.
\r
211 > + (when (null (notmuch-sexp--partial-state sp))
\r
212 > + (let ((start (point)))
\r
213 > + (condition-case nil
\r
214 > + (throw 'return (read (current-buffer)))
\r
215 > + (end-of-file (goto-char start)))))
\r
216 > + ;; Extend the partial parse
\r
217 > + (let (is-complete)
\r
218 > + (save-excursion
\r
219 > + (let* ((new-state (parse-partial-sexp
\r
220 > + (or (notmuch-sexp--partial-pos sp) (point))
\r
221 > + (point-max) 0 nil
\r
222 > + (notmuch-sexp--partial-state sp)))
\r
223 > + ;; A complete value is available if we've
\r
224 > + ;; reached depth 0.
\r
225 > + (depth (first new-state)))
\r
226 > + (assert (>=3D depth 0))
\r
227 > + (if (=3D depth 0)
\r
228 > + ;; Reset partial parse state
\r
229 > + (setf (notmuch-sexp--partial-state sp) nil
\r
230 > + (notmuch-sexp--partial-pos sp) nil
\r
232 > + ;; Update partial parse state
\r
233 > + (setf (notmuch-sexp--partial-state sp) new-state
\r
234 > + (notmuch-sexp--partial-pos sp) (point-marker)))))
\r
235 > + (if is-complete
\r
236 > + (read (current-buffer))
\r
239 > + ;; Attempt to read a non-compound value
\r
240 > + (let ((start (point)))
\r
241 > + (condition-case nil
\r
242 > + (let ((val (read (current-buffer))))
\r
243 > + ;; We got what looks like a complete read, but if
\r
244 > + ;; we reached the end of the buffer in the process,
\r
245 > + ;; we may not actually have all of the input we
\r
246 > + ;; need (unless it's a string, which is delimited).
\r
247 > + (if (or (stringp val) (not (eobp)))
\r
249 > + ;; We can't be sure the input was complete
\r
250 > + (goto-char start)
\r
253 > + (goto-char start)
\r
256 > +(defun notmuch-sexp-begin-list (sp)
\r
257 > + "Parse the beginning of a list value and enter the list.
\r
259 > +Returns 'retry if there is insufficient input to parse the
\r
260 > +beginning of the list. If this is able to parse the beginning of
\r
261 > +a list, it moves point past the token that opens the list and
\r
262 > +returns t. Later calls to `notmuch-sexp-read' will return the
\r
263 > +elements inside the list. If the input in buffer is not the
\r
264 > +beginning of a list, throw invalid-read-syntax."
\r
266 > + (with-current-buffer (notmuch-sexp--buffer sp)
\r
267 > + (skip-chars-forward " \n\r\t")
\r
268 > + (cond ((eobp) 'retry)
\r
269 > + ((=3D (char-after) ?\()
\r
271 > + (incf (notmuch-sexp--depth sp))
\r
274 > + ;; Skip over the bad character like `read' does
\r
276 > + (signal 'invalid-read-syntax (list (string (char-before))))))))
\r
278 > +(defun notmuch-sexp-eof (sp)
\r
279 > + "Signal an error if there is more data in SP's buffer.
\r
281 > +Moves point to the beginning of any trailing data or to the end
\r
282 > +of the buffer if there is only trailing whitespace."
\r
284 > + (with-current-buffer (notmuch-sexp--buffer sp)
\r
285 > + (skip-chars-forward " \n\r\t")
\r
287 > + (error "Trailing garbage following expression"))))
\r
289 > +(defvar notmuch-sexp--parser nil
\r
290 > + "The buffer-local notmuch-sexp-parser instance.
\r
292 > +Used by `notmuch-sexp-parse-partial-list'.")
\r
294 > +(defvar notmuch-sexp--state nil
\r
295 > + "The buffer-local `notmuch-sexp-parse-partial-list' state.")
\r
297 > +(defun notmuch-sexp-parse-partial-list (result-function result-buffer)
\r
298 > + "Incrementally parse an S-expression list from the current buffer.
\r
300 > +This function consume an S-expression list from the current
\r
304 > +buffer, applying RESULT-FUNCTION in RESULT-BUFFER to each
\r
305 > +complete value in the list. It operates incrementally and should
\r
306 > +be called whenever the input buffer has been extended with
\r
307 > +additional data. The caller just needs to ensure it does not
\r
308 > +move point in the input buffer."
\r
310 > + ;; Set up the initial state
\r
311 > + (unless (local-variable-p 'notmuch-sexp--parser)
\r
312 > + (set (make-local-variable 'notmuch-sexp--parser)
\r
313 > + (notmuch-sexp-create-parser (current-buffer)))
\r
314 > + (set (make-local-variable 'notmuch-sexp--state) 'begin))
\r
316 > + (while (not done)
\r
317 > + (case notmuch-sexp--state
\r
319 > + ;; Enter the list
\r
320 > + (if (eq (notmuch-sexp-begin-list notmuch-sexp--parser) 'retry)
\r
322 > + (setq notmuch-sexp--state 'result)))
\r
324 > + ;; Parse a result
\r
325 > + (let ((result (notmuch-sexp-read notmuch-sexp--parser)))
\r
327 > + (retry (setq done t))
\r
328 > + (end (setq notmuch-sexp--state 'end))
\r
329 > + (t (with-current-buffer result-buffer
\r
330 > + (funcall result-function result))))))
\r
332 > + ;; Any trailing data is unexpected
\r
333 > + (notmuch-sexp-eof notmuch-sexp--parser)
\r
334 > + (setq done t)))))
\r
335 > + ;; Clear out what we've parsed
\r
336 > + (delete-region (point-min) (point)))
\r
338 > +(provide 'notmuch-parser)
\r
340 > +;; Local Variables:
\r
341 > +;; byte-compile-warnings: (not cl-functions)
\r