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 95D72431FAF
\r
6 for <notmuch@notmuchmail.org>; Mon, 27 May 2013 13:58:40 -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 92L5TPlETU+D for <notmuch@notmuchmail.org>;
\r
17 Mon, 27 May 2013 13:58:34 -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 8E277431FAE
\r
22 for <notmuch@notmuchmail.org>; Mon, 27 May 2013 13:58:33 -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 1Uh4Uv-00042P-GJ; Mon, 27 May 2013 21:58:30 +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 1Uh4Uu-0000Wd-Sp; Mon, 27 May 2013 21:58:29 +0100
\r
31 From: Mark Walters <markwalters1009@gmail.com>
\r
32 To: Austin Clements <amdragon@MIT.EDU>
\r
33 Subject: Re: [PATCH 4/5] emacs: Streaming S-expression parser
\r
34 In-Reply-To: <20130527190436.GS5999@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 <87fvxg2wc4.fsf@qmul.ac.uk>
\r
38 <87ip2b3moz.fsf@awakening.csail.mit.edu>
\r
39 <8761y7ph8v.fsf@qmul.ac.uk> <20130527190436.GS5999@mit.edu>
\r
40 User-Agent: Notmuch/0.14+255~gff3cc55 (http://notmuchmail.org) Emacs/23.4.1
\r
42 Date: Mon, 27 May 2013 21:58:27 +0100
\r
43 Message-ID: <8761y4jg24.fsf@qmul.ac.uk>
\r
45 Content-Type: text/plain; charset=utf-8
\r
46 Content-Transfer-Encoding: quoted-printable
\r
47 X-Sender-Host-Address: 93.97.24.31
\r
48 X-QM-SPAM-Info: Sender has good ham record. :)
\r
49 X-QM-Body-MD5: bd9b0ce573f2b98574890d6b736b23f0 (of first 20000 bytes)
\r
50 X-SpamAssassin-Score: -0.1
\r
51 X-SpamAssassin-SpamBar: /
\r
52 X-SpamAssassin-Report: The QM spam filters have analysed this message to
\r
54 spam. We require at least 5.0 points to mark a message as spam.
\r
55 This message scored -0.1 points.
\r
56 Summary of the scoring:
\r
57 * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail
\r
58 provider * (markwalters1009[at]gmail.com)
\r
59 * -0.1 AWL AWL: From: address is in the auto white-list
\r
60 X-QM-Scan-Virus: ClamAV says the message is clean
\r
61 Cc: notmuch@notmuchmail.org
\r
62 X-BeenThere: notmuch@notmuchmail.org
\r
63 X-Mailman-Version: 2.1.13
\r
65 List-Id: "Use and development of the notmuch mail system."
\r
66 <notmuch.notmuchmail.org>
\r
67 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
68 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
69 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
70 List-Post: <mailto:notmuch@notmuchmail.org>
\r
71 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
72 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
73 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
74 X-List-Received-Date: Mon, 27 May 2013 20:58:40 -0000
\r
77 Austin Clements <amdragon@MIT.EDU> writes:
\r
79 > Quoth Mark Walters on May 25 at 9:59 am:
\r
83 >> On Wed, 22 May 2013, Austin Clements <amdragon@MIT.EDU> wrote:
\r
84 >> > On Tue, 21 May 2013, Mark Walters <markwalters1009@gmail.com> wrote:
\r
87 >> >> This patch looks good to me. Some minor comments below.
\r
89 >> > Some minor replies below.
\r
91 >> > In building some other code on top of this, I found an interesting (but
\r
92 >> > easy to fix) interface bug. Currently, the interface is designed as if
\r
93 >> > it doesn't matter what buffer these functions are called from, however,
\r
94 >> > because they move point and expect this point motion to persist, it's
\r
95 >> > actually not safe to call this interface unless the caller is in the
\r
96 >> > right buffer anyway. For example, if the buffer is selected in a
\r
97 >> > window, the with-current-buffer in the parser functions will actually
\r
98 >> > move a *temporary* point, meaning that the only way the caller can
\r
99 >> > discover the new point is to first select the buffer for itself. I can
\r
100 >> > think of two solutions: 1) maintain our own mark for the parser's
\r
101 >> > current position or 2) tweak the doc strings and code so that it reads
\r
102 >> > from the current buffer. 1 keeps the interface the way it's currently
\r
103 >> > documented, but complicates the parser implementation and interface and
\r
104 >> > doesn't simplify the caller. 2 simplifies the parser and it turns out
\r
105 >> > all callers already satisfy the requirement.
\r
107 >> I am confused by this: the docs strings for json/sexp-parse-partial-list
\r
108 >> both say something like "Parse a partial JSON list from current buffer"?
\r
109 >> Or do you mean the with-current-buffer in notmuch-search-process-filter?
\r
111 > I was referring to the lower level parser, which effectively has the
\r
112 > same requirement but isn't documented to and has code that pointlessly
\r
113 > tries to track the parsing buffer (I consider
\r
114 > json/sexp-parse-partial-list to be a helper). In fact, one reason the
\r
115 > lower level parser didn't choke is because right now we only use it
\r
116 > through json/sexp-parse-partial-list, which requires that it be called
\r
117 > from the right buffer.
\r
119 Right I think I see. I am definitely happy with insisting on being
\r
120 called from the correct buffer in the low level code.
\r
122 >> >> On Sat, 18 May 2013, Austin Clements <amdragon@MIT.EDU> wrote:
\r
123 >> >>> This provides the same interface as the streaming JSON parser, but
\r
124 >> >>> reads S-expressions incrementally. The only difference is that the
\r
125 >> >>> `notmuch-sexp-parse-partial-list' helper does not handle interleaved
\r
126 >> >>> error messages (since we now have the ability to separate these out =
\r
128 >> >>> the invocation level), so it no longer takes an error function and
\r
129 >> >>> does not need to do the horrible resynchronization that the JSON
\r
130 >> >>> parser had to.
\r
132 >> >>> Some implementation improvements have been made over the JSON parser.
\r
133 >> >>> This uses a vector instead of a list for the parser data structure,
\r
134 >> >>> since this allows faster access to elements (and modern versions of
\r
135 >> >>> Emacs handle storage of small vectors efficiently). Private functio=
\r
137 >> >>> follow the "prefix--name" convention. And the implementation is much
\r
138 >> >>> simpler overall because S-expressions are much easier to parse.
\r
140 >> >>> emacs/Makefile.local | 1 +
\r
141 >> >>> emacs/notmuch-parser.el | 212 ++++++++++++++++++++++++++++++++++++=
\r
143 >> >>> 2 files changed, 213 insertions(+)
\r
144 >> >>> create mode 100644 emacs/notmuch-parser.el
\r
146 >> >>> diff --git a/emacs/Makefile.local b/emacs/Makefile.local
\r
147 >> >>> index 456700a..a910aff 100644
\r
148 >> >>> --- a/emacs/Makefile.local
\r
149 >> >>> +++ b/emacs/Makefile.local
\r
150 >> >>> @@ -3,6 +3,7 @@
\r
151 >> >>> dir :=3D emacs
\r
152 >> >>> emacs_sources :=3D \
\r
153 >> >>> $(dir)/notmuch-lib.el \
\r
154 >> >>> + $(dir)/notmuch-parser.el \
\r
155 >> >>> $(dir)/notmuch.el \
\r
156 >> >>> $(dir)/notmuch-query.el \
\r
157 >> >>> $(dir)/notmuch-show.el \
\r
158 >> >>> diff --git a/emacs/notmuch-parser.el b/emacs/notmuch-parser.el
\r
159 >> >>> new file mode 100644
\r
160 >> >>> index 0000000..1b7cf64
\r
161 >> >>> --- /dev/null
\r
162 >> >>> +++ b/emacs/notmuch-parser.el
\r
163 >> >>> @@ -0,0 +1,212 @@
\r
164 >> >>> +;; notmuch-parser.el --- streaming S-expression parser
\r
166 >> >>> +;; Copyright =C2=A9 Austin Clements
\r
168 >> >>> +;; This file is part of Notmuch.
\r
170 >> >>> +;; Notmuch is free software: you can redistribute it and/or modify =
\r
172 >> >>> +;; under the terms of the GNU General Public License as published by
\r
173 >> >>> +;; the Free Software Foundation, either version 3 of the License, or
\r
174 >> >>> +;; (at your option) any later version.
\r
176 >> >>> +;; Notmuch is distributed in the hope that it will be useful, but
\r
177 >> >>> +;; WITHOUT ANY WARRANTY; without even the implied warranty of
\r
178 >> >>> +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
179 >> >>> +;; General Public License for more details.
\r
181 >> >>> +;; You should have received a copy of the GNU General Public License
\r
182 >> >>> +;; along with Notmuch. If not, see <http://www.gnu.org/licenses/>.
\r
184 >> >>> +;; Authors: Austin Clements <aclements@csail.mit.edu>
\r
186 >> >>> +(require 'cl)
\r
188 >> >>> +(defun notmuch-sexp-create-parser (buffer)
\r
189 >> >>> + "Return a streaming S-expression parser that reads from BUFFER.
\r
191 >> >>> +This parser is designed to incrementally read an S-expression
\r
192 >> >>> +whose structure is known to the caller. Like a typical
\r
193 >> >>> +S-expression parsing interface, it provides a function to read a
\r
194 >> >>> +complete S-expression from the input. However, it extends this
\r
195 >> >>> +with an additional function that requires the next value in the
\r
196 >> >>> +input to be a list and descends into it, allowing its elements to
\r
197 >> >>> +be read one at a time or further descended into. Both functions
\r
198 >> >>> +can return 'retry to indicate that not enough input is available.
\r
200 >> >>> +The parser always consumes input from BUFFER's point. Hence, the
\r
201 >> >>> +caller is allowed to delete any data before point and may
\r
202 >> >>> +resynchronize after an error by moving point."
\r
204 >> >>> + (vector 'notmuch-sexp-parser
\r
206 >> >>> + ;; List depth
\r
208 >> >>> + ;; Partial parse position marker
\r
210 >> >>> + ;; Partial parse state
\r
213 >> >>> +(defmacro notmuch-sexp--buffer (sp) `(aref ,sp 1))
\r
214 >> >>> +(defmacro notmuch-sexp--depth (sp) `(aref ,sp 2))
\r
215 >> >>> +(defmacro notmuch-sexp--partial-pos (sp) `(aref ,sp 3))
\r
216 >> >>> +(defmacro notmuch-sexp--partial-state (sp) `(aref ,sp 4))
\r
218 >> >> Why the double hyphen --? Is it a name-space or some convention?
\r
220 >> > More specifically, this seems to be the most common Elisp convention f=
\r
222 >> > indicating private symbols.
\r
224 >> Ok. If we are keeping the json parser it might be worth making it follow
\r
225 >> the same convention but as it is purely internal it's probably not worth
\r
228 > Yeah, it is purely internal. Also, I was planning to remove the JSON
\r
229 > parser (or maybe move it somewhere else? I feel bad deleting that
\r
230 > much perfectly functional code, though of course git will keep it in
\r
233 Yes it does seem a shame to lose it. It would be nice to find a better
\r
234 home for it then hiding in a git tree.
\r
241 >> >>> +(defun notmuch-sexp-read (sp)
\r
242 >> >>> + "Consume and return the value at point in SP's buffer.
\r
244 >> >>> +Returns 'retry if there is insufficient input to parse a complete
\r
245 >> >>> +value (though it may still move point over whitespace). If the
\r
246 >> >>> +parser is currently inside a list and the next token ends the
\r
247 >> >>> +list, this moves point just past the terminator and returns 'end.
\r
248 >> >>> +Otherwise, this moves point to just past the end of the value and
\r
249 >> >>> +returns the value."
\r
251 >> >>> + (with-current-buffer (notmuch-sexp--buffer sp)
\r
252 >> >>> + (skip-chars-forward " \n\r\t")
\r
253 >> >>> + (cond ((eobp) 'retry)
\r
254 >> >>> + ((=3D (char-after) ?\))
\r
255 >> >>> + ;; We've reached the end of a list
\r
256 >> >>> + (if (=3D (notmuch-sexp--depth sp) 0)
\r
257 >> >>> + ;; .. but we weren't in a list. Let read signal the
\r
259 >> >>> + (read (current-buffer))
\r
261 >> >> Why is good for read to signal the error rather than us doing it?
\r
263 >> > This ensures the syntax error handling and signal behavior of
\r
264 >> > notmuch-sexp-read is identical in every way to a regular read call.
\r
265 >> > Maybe the comment should read "Let read signal the error like we do in
\r
266 >> > all other code paths."?
\r
268 >> Yes that would be good: or perhaps "like it does in all other code
\r
278 >> >>> + ;; Go up a level and return an end token
\r
279 >> >>> + (decf (notmuch-sexp--depth sp))
\r
280 >> >>> + (forward-char)
\r
282 >> >>> + ((=3D (char-after) ?\()
\r
283 >> >>> + ;; We're at the beginning of a list. If we haven't started
\r
284 >> >>> + ;; a partial parse yet, attempt to read the list in its
\r
285 >> >>> + ;; entirety. If this fails, or we've started a partial
\r
286 >> >>> + ;; parse, extend the partial parse to figure out when we
\r
287 >> >>> + ;; have a complete list.
\r
288 >> >>> + (catch 'return
\r
289 >> >>> + (when (null (notmuch-sexp--partial-state sp))
\r
290 >> >>> + (let ((start (point)))
\r
291 >> >>> + (condition-case nil
\r
292 >> >>> + (throw 'return (read (current-buffer)))
\r
293 >> >>> + (end-of-file (goto-char start)))))
\r
294 >> >>> + ;; Extend the partial parse
\r
295 >> >>> + (let (is-complete)
\r
296 >> >>> + (save-excursion
\r
297 >> >>> + (let* ((new-state (parse-partial-sexp
\r
298 >> >>> + (or (notmuch-sexp--partial-pos sp) (point))
\r
299 >> >>> + (point-max) 0 nil
\r
300 >> >>> + (notmuch-sexp--partial-state sp)))
\r
301 >> >>> + ;; A complete value is available if we've
\r
302 >> >>> + ;; reached depth 0.
\r
303 >> >>> + (depth (first new-state)))
\r
304 >> >>> + (assert (>=3D depth 0))
\r
305 >> >>> + (if (=3D depth 0)
\r
306 >> >>> + ;; Reset partial parse state
\r
307 >> >>> + (setf (notmuch-sexp--partial-state sp) nil
\r
308 >> >>> + (notmuch-sexp--partial-pos sp) nil
\r
309 >> >>> + is-complete t)
\r
310 >> >>> + ;; Update partial parse state
\r
311 >> >>> + (setf (notmuch-sexp--partial-state sp) new-state
\r
312 >> >>> + (notmuch-sexp--partial-pos sp) (point-marker)))))
\r
313 >> >>> + (if is-complete
\r
314 >> >>> + (read (current-buffer))
\r
315 >> >>> + 'retry))))
\r
317 >> >>> + ;; Attempt to read a non-compound value
\r
318 >> >>> + (let ((start (point)))
\r
319 >> >>> + (condition-case nil
\r
320 >> >>> + (let ((val (read (current-buffer))))
\r
321 >> >>> + ;; We got what looks like a complete read, but if
\r
322 >> >>> + ;; we reached the end of the buffer in the process,
\r
323 >> >>> + ;; we may not actually have all of the input we
\r
324 >> >>> + ;; need (unless it's a string, which is delimited).
\r
325 >> >>> + (if (or (stringp val) (not (eobp)))
\r
327 >> >>> + ;; We can't be sure the input was complete
\r
328 >> >>> + (goto-char start)
\r
330 >> >>> + (end-of-file
\r
331 >> >>> + (goto-char start)
\r
332 >> >>> + 'retry)))))))
\r
334 >> >>> +(defun notmuch-sexp-begin-list (sp)
\r
335 >> >>> + "Parse the beginning of a list value and enter the list.
\r
337 >> >>> +Returns 'retry if there is insufficient input to parse the
\r
338 >> >>> +beginning of the list. If this is able to parse the beginning of
\r
339 >> >>> +a list, it moves point past the token that opens the list and
\r
340 >> >>> +returns t. Later calls to `notmuch-sexp-read' will return the
\r
341 >> >>> +elements inside the list. If the input in buffer is not the
\r
342 >> >>> +beginning of a list, throw invalid-read-syntax."
\r
344 >> >>> + (with-current-buffer (notmuch-sexp--buffer sp)
\r
345 >> >>> + (skip-chars-forward " \n\r\t")
\r
346 >> >>> + (cond ((eobp) 'retry)
\r
347 >> >>> + ((=3D (char-after) ?\()
\r
348 >> >>> + (forward-char)
\r
349 >> >>> + (incf (notmuch-sexp--depth sp))
\r
352 >> >>> + ;; Skip over the bad character like `read' does
\r
353 >> >>> + (forward-char)
\r
354 >> >>> + (signal 'invalid-read-syntax (list (string (char-before))))))))
\r
356 >> >>> +(defun notmuch-sexp-eof (sp)
\r
357 >> >>> + "Signal an error if there is more data in SP's buffer.
\r
359 >> >>> +Moves point to the beginning of any trailing data or to the end
\r
360 >> >>> +of the buffer if there is only trailing whitespace."
\r
362 >> >>> + (with-current-buffer (notmuch-sexp--buffer sp)
\r
363 >> >>> + (skip-chars-forward " \n\r\t")
\r
364 >> >>> + (unless (eobp)
\r
365 >> >>> + (error "Trailing garbage following expression"))))
\r
367 >> >>> +(defvar notmuch-sexp--parser nil
\r
368 >> >>> + "The buffer-local notmuch-sexp-parser instance.
\r
370 >> >>> +Used by `notmuch-sexp-parse-partial-list'.")
\r
372 >> >>> +(defvar notmuch-sexp--state nil
\r
373 >> >>> + "The buffer-local `notmuch-sexp-parse-partial-list' state.")
\r
375 >> >>> +(defun notmuch-sexp-parse-partial-list (result-function result-buff=
\r
377 >> >>> + "Incrementally parse an S-expression list from the current buffer.
\r
379 >> >>> +This function consume an S-expression list from the current
\r
385 >> >>> +buffer, applying RESULT-FUNCTION in RESULT-BUFFER to each
\r
386 >> >>> +complete value in the list. It operates incrementally and should
\r
387 >> >>> +be called whenever the input buffer has been extended with
\r
388 >> >>> +additional data. The caller just needs to ensure it does not
\r
389 >> >>> +move point in the input buffer."
\r
391 >> >>> + ;; Set up the initial state
\r
392 >> >>> + (unless (local-variable-p 'notmuch-sexp--parser)
\r
393 >> >>> + (set (make-local-variable 'notmuch-sexp--parser)
\r
394 >> >>> + (notmuch-sexp-create-parser (current-buffer)))
\r
395 >> >>> + (set (make-local-variable 'notmuch-sexp--state) 'begin))
\r
396 >> >>> + (let (done)
\r
397 >> >>> + (while (not done)
\r
398 >> >>> + (case notmuch-sexp--state
\r
400 >> >>> + ;; Enter the list
\r
401 >> >>> + (if (eq (notmuch-sexp-begin-list notmuch-sexp--parser) 'retry)
\r
402 >> >>> + (setq done t)
\r
403 >> >>> + (setq notmuch-sexp--state 'result)))
\r
405 >> >>> + ;; Parse a result
\r
406 >> >>> + (let ((result (notmuch-sexp-read notmuch-sexp--parser)))
\r
407 >> >>> + (case result
\r
408 >> >>> + (retry (setq done t))
\r
409 >> >>> + (end (setq notmuch-sexp--state 'end))
\r
410 >> >>> + (t (with-current-buffer result-buffer
\r
411 >> >>> + (funcall result-function result))))))
\r
413 >> >>> + ;; Any trailing data is unexpected
\r
414 >> >>> + (notmuch-sexp-eof notmuch-sexp--parser)
\r
415 >> >>> + (setq done t)))))
\r
416 >> >>> + ;; Clear out what we've parsed
\r
417 >> >>> + (delete-region (point-min) (point)))
\r
419 >> >>> +(provide 'notmuch-parser)
\r
421 >> >>> +;; Local Variables:
\r
422 >> >>> +;; byte-compile-warnings: (not cl-functions)
\r