1 Return-Path: <jani@nikula.org>
\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 CFD96431FAF
\r
6 for <notmuch@notmuchmail.org>; Fri, 30 Nov 2012 09:26:16 -0800 (PST)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5
\r
12 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled
\r
13 Received: from olra.theworths.org ([127.0.0.1])
\r
14 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
15 with ESMTP id tFelwKdreYMo for <notmuch@notmuchmail.org>;
\r
16 Fri, 30 Nov 2012 09:26:12 -0800 (PST)
\r
17 Received: from mail-la0-f53.google.com (mail-la0-f53.google.com
\r
18 [209.85.215.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))
\r
19 (No client certificate requested)
\r
20 by olra.theworths.org (Postfix) with ESMTPS id 66E6E431FAE
\r
21 for <notmuch@notmuchmail.org>; Fri, 30 Nov 2012 09:26:12 -0800 (PST)
\r
22 Received: by mail-la0-f53.google.com with SMTP id w12so590012lag.26
\r
23 for <notmuch@notmuchmail.org>; Fri, 30 Nov 2012 09:26:09 -0800 (PST)
\r
24 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
\r
25 d=google.com; s=20120113;
\r
26 h=from:to:subject:in-reply-to:references:user-agent:date:message-id
\r
27 :mime-version:content-type:x-gm-message-state;
\r
28 bh=HPMQqgOfEDcMzhu8+WSPqDPq7KmQnfSQ0fywEx4OWkk=;
\r
29 b=fikZmAVJMXLfJYBB5hGZ/l4LOydrGh+z1a7zTgS1sQkF9+bx6+HYq1Q6Dk/YOL0NHm
\r
30 Df7j3E0qVK8OhN+C6dEvzem9a/14lCpJbBL6ukGbyMo2wIhm7n2rXxf+++Tw1+YPprZR
\r
31 kUKxqadNnmHEY/xg6KOQpTm43qAPIH6yboACAuWZdufKCNLQ3vARfuO6xeeuDVKL1mG3
\r
32 LfMCt6vKtXBdmiuXDzjOloDlLzsxEHiz4CI0k33CYF+MFknboREkZM5pNiXHWmN+DnU9
\r
33 WkPBAkqhtDDDLcR+1d4Yr0/Nlzc+DX7BUwKUAn2g4UqdWcszjDdWt5mundYbo7SQvaTT
\r
35 Received: by 10.112.30.70 with SMTP id q6mr1145589lbh.51.1354296369343;
\r
36 Fri, 30 Nov 2012 09:26:09 -0800 (PST)
\r
37 Received: from localhost (dsl-hkibrasgw4-fe51df00-27.dhcp.inet.fi.
\r
39 by mx.google.com with ESMTPS id q2sm2320546lbd.14.2012.11.30.09.26.05
\r
40 (version=SSLv3 cipher=OTHER); Fri, 30 Nov 2012 09:26:06 -0800 (PST)
\r
41 From: Jani Nikula <jani@nikula.org>
\r
42 To: Peter Feigl <craven@gmx.net>, notmuch@notmuchmail.org
\r
43 Subject: Re: [PATCH 1/3] Adding an S-expression structured output printer.
\r
44 In-Reply-To: <1354264143-30173-1-git-send-email-craven@gmx.net>
\r
45 References: <1354264143-30173-1-git-send-email-craven@gmx.net>
\r
46 User-Agent: Notmuch/0.14+124~g3b17402 (http://notmuchmail.org) Emacs/23.4.1
\r
48 Date: Fri, 30 Nov 2012 19:26:04 +0200
\r
49 Message-ID: <87a9tzj93n.fsf@nikula.org>
\r
51 Content-Type: text/plain; charset=us-ascii
\r
53 ALoCoQk0AEL6uGu+djMyN5G1XsAjOMnlJHHDtQqzZA+tRUY2Wbn1GeSML5dts5nYF3VdnrMGaE1q
\r
54 X-BeenThere: notmuch@notmuchmail.org
\r
55 X-Mailman-Version: 2.1.13
\r
57 List-Id: "Use and development of the notmuch mail system."
\r
58 <notmuch.notmuchmail.org>
\r
59 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
60 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
61 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
62 List-Post: <mailto:notmuch@notmuchmail.org>
\r
63 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
64 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
65 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
66 X-List-Received-Date: Fri, 30 Nov 2012 17:26:16 -0000
\r
69 Hi Peter, looks good save for a few nitpicks (see comments
\r
70 inline). Someone more experienced in lisp should still have a look.
\r
76 On Fri, 30 Nov 2012, Peter Feigl <craven@gmx.net> wrote:
\r
77 > This commit adds an sprinter for Lisp S-Expressions. Later commits will
\r
80 > The structure is the same as json, but:
\r
81 > - arrays are written as lists: ("foo" "bar" "baaz" 1 2 3)
\r
82 > - maps are written as a-lists: ((key "value") (other-key "other-value"))
\r
83 > - true is written as t
\r
84 > - false is written as nil
\r
85 > - null is written as nil
\r
87 > Makefile.local | 1 +
\r
88 > sprinter-sexp.c | 235 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
\r
89 > 2 files changed, 236 insertions(+)
\r
90 > create mode 100644 sprinter-sexp.c
\r
92 > diff --git a/Makefile.local b/Makefile.local
\r
93 > index 2b91946..0db1713 100644
\r
94 > --- a/Makefile.local
\r
95 > +++ b/Makefile.local
\r
96 > @@ -270,6 +270,7 @@ notmuch_client_srcs = \
\r
100 > + sprinter-sexp.c \
\r
101 > sprinter-text.c \
\r
104 > diff --git a/sprinter-sexp.c b/sprinter-sexp.c
\r
105 > new file mode 100644
\r
106 > index 0000000..8401c52
\r
108 > +++ b/sprinter-sexp.c
\r
109 > @@ -0,0 +1,235 @@
\r
111 Copyright comment is missing. (Seems to be missing from sprinter-json.c
\r
112 too, but that's no excuse! ;)
\r
114 > +#include <stdbool.h>
\r
115 > +#include <stdio.h>
\r
116 > +#include <talloc.h>
\r
117 > +#include "sprinter.h"
\r
119 > +struct sprinter_sexp {
\r
120 > + struct sprinter vtable;
\r
122 > + /* Top of the state stack, or NULL if the printer is not currently
\r
123 > + * inside any aggregate types. */
\r
124 > + struct sexp_state *state;
\r
126 > + /* A flag to signify that a separator should be inserted in the
\r
127 > + * output as soon as possible.
\r
129 > + notmuch_bool_t insert_separator;
\r
132 > +struct sexp_state {
\r
133 > + struct sexp_state *parent;
\r
135 > + /* True if nothing has been printed in this aggregate yet.
\r
136 > + * Suppresses the space before a value. */
\r
137 > + notmuch_bool_t first;
\r
139 > + /* True if the state is a map state.
\r
140 > + Used to add a space between key/value pairs. */
\r
142 The multi-line comments should use the same style here and elsewhere.
\r
144 > + notmuch_bool_t in_map;
\r
146 > + /* The character that closes the current aggregate. */
\r
150 > +/* Helper function to set up the stream to print a value. If this
\r
151 > + * value follows another value, prints a space. */
\r
152 > +static struct sprinter_sexp *
\r
153 > +sexp_begin_value (struct sprinter *sp)
\r
155 > + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
157 > + if (sps->state) {
\r
158 > + if (! sps->state->first) {
\r
159 > + if (sps->insert_separator) {
\r
160 > + fputc ('\n', sps->stream);
\r
161 > + sps->insert_separator = FALSE;
\r
163 > + if( ! sps->state->in_map)
\r
165 Spacing should be "if (! " ...
\r
167 > + fputc (' ', sps->stream);
\r
170 > + sps->state->first = FALSE;
\r
176 > +/* Helper function to begin an aggregate type. Prints the open
\r
177 > + * character and pushes a new state frame. */
\r
179 > +sexp_begin_aggregate (struct sprinter *sp, char open, char close)
\r
181 > + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
182 > + struct sexp_state *state = talloc (sps, struct sexp_state);
\r
183 > + fputc (open, sps->stream);
\r
184 > + state->parent = sps->state;
\r
185 > + state->first = TRUE;
\r
186 > + state->in_map = FALSE;
\r
187 > + state->close = close;
\r
188 > + sps->state = state;
\r
192 > +sexp_begin_map (struct sprinter *sp)
\r
194 > + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
195 > + sexp_begin_aggregate (sp, '(', ')');
\r
196 > + sps->state->in_map = TRUE;
\r
200 > +sexp_begin_list (struct sprinter *sp)
\r
202 > + sexp_begin_aggregate (sp, '(', ')');
\r
206 > +sexp_end (struct sprinter *sp)
\r
208 > + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
209 > + struct sexp_state *state = sps->state;
\r
211 > + if (sps->state->in_map)
\r
212 > + fputc (')', sps->stream);
\r
213 > + fputc (sps->state->close, sps->stream);
\r
214 > + sps->state = state->parent;
\r
215 > + talloc_free (state);
\r
216 > + if (sps->state == NULL)
\r
217 > + fputc ('\n', sps->stream);
\r
220 > +/* This implementation supports embedded NULs as allowed by the JSON
\r
221 > + * specification and Unicode. Support for *parsing* embedded NULs
\r
222 > + * varies, but is generally not a problem outside of C-based parsers
\r
223 > + * (Python's json module and Emacs' json.el take embedded NULs in
\r
226 References to JSON seem out of place here.
\r
229 > +sexp_string_len_internal (struct sprinter *sp, const char *val, size_t len, notmuch_bool_t quote)
\r
231 > + static const char *const escapes[] = {
\r
232 > + ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",
\r
233 > + ['\f'] = "\\f", ['\n'] = "\\n", ['\t'] = "\\t"
\r
235 > + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
238 > + fputc ('"', sps->stream);
\r
239 > + for (; len; ++val, --len) {
\r
240 > + unsigned char ch = *val;
\r
241 > + if (ch < ARRAY_SIZE (escapes) && escapes[ch])
\r
242 > + fputs (escapes[ch], sps->stream);
\r
243 > + else if (ch >= 32)
\r
244 > + fputc (ch, sps->stream);
\r
246 > + fprintf (sps->stream, "\\u%04x", ch);
\r
249 > + fputc ('"', sps->stream);
\r
253 > +sexp_string_len (struct sprinter *sp, const char *val, size_t len)
\r
255 > + sexp_string_len_internal (sp, val, len, TRUE); /* print quoted */
\r
259 > +sexp_symbol_len (struct sprinter *sp, const char *val, size_t len)
\r
261 > + sexp_string_len_internal (sp, val, len, FALSE); /* print unquoted */
\r
265 > +sexp_string (struct sprinter *sp, const char *val)
\r
267 > + if (val == NULL)
\r
269 > + sexp_string_len (sp, val, strlen (val));
\r
273 > +sexp_symbol (struct sprinter *sp, const char *val)
\r
275 > + if (val == NULL)
\r
277 > + sexp_symbol_len (sp, val, strlen (val));
\r
281 > +sexp_integer (struct sprinter *sp, int val)
\r
283 > + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
285 > + fprintf (sps->stream, "%d", val);
\r
289 > +sexp_boolean (struct sprinter *sp, notmuch_bool_t val)
\r
291 > + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
293 > + fputs (val ? "t" : "nil", sps->stream);
\r
297 > +sexp_null (struct sprinter *sp)
\r
299 > + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
301 > + fputs ("nil", sps->stream);
\r
305 > +sexp_map_key (struct sprinter *sp, const char *key)
\r
307 > + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
309 > + if( sps->state->in_map && ! sps->state->first)
\r
311 Spacing should be "if (sps" ...
\r
313 > + fputs (") ", sps->stream);
\r
314 > + fputc ('(', sps->stream);
\r
315 > + sexp_symbol (sp, key);
\r
316 > + fputc (' ', sps->stream);
\r
320 > +sexp_set_prefix (unused (struct sprinter *sp), unused (const char *name))
\r
325 > +sexp_separator (struct sprinter *sp)
\r
327 > + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
329 > + sps->insert_separator = TRUE;
\r
332 > +struct sprinter *
\r
333 > +sprinter_sexp_create (const void *ctx, FILE *stream)
\r
335 > + static const struct sprinter_sexp template = {
\r
337 > + .begin_map = sexp_begin_map,
\r
338 > + .begin_list = sexp_begin_list,
\r
339 > + .end = sexp_end,
\r
340 > + .string = sexp_string,
\r
341 > + .string_len = sexp_string_len,
\r
342 > + .integer = sexp_integer,
\r
343 > + .boolean = sexp_boolean,
\r
344 > + .null = sexp_null,
\r
345 > + .map_key = sexp_map_key,
\r
346 > + .separator = sexp_separator,
\r
347 > + .set_prefix = sexp_set_prefix,
\r
348 > + .is_text_printer = FALSE,
\r
351 > + struct sprinter_sexp *res;
\r
353 > + res = talloc (ctx, struct sprinter_sexp);
\r
357 > + *res = template;
\r
358 > + res->stream = stream;
\r
359 > + return &res->vtable;
\r
364 > _______________________________________________
\r
365 > notmuch mailing list
\r
366 > notmuch@notmuchmail.org
\r
367 > http://notmuchmail.org/mailman/listinfo/notmuch
\r