summaryrefslogtreecommitdiff
path: root/kicad/sexpr.c
blob: 9541ab884eba1a7d5a5dcd321ab3c7fe8866cf97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 * kicad/sexpr.c - S-expression parser
 *
 * Written 2016 by Werner Almesberger
 * Copyright 2016 by Werner Almesberger
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "misc/util.h"
#include "misc/diag.h"
#include "kicad/sexpr.h"


struct stack {
	struct expr **next;
	struct stack *prev;
};

struct sexpr_ctx {
	enum sexpr_state {
		idle,
		token,
		string,
		escape,
		failed
	} state;
	struct expr *e;		/* expression */
	const char *p;		/* beginning of string */
	struct stack *sp;
	struct stack stack;
};


/* ----- Parser ------------------------------------------------------------ */


static bool pop(struct sexpr_ctx *ctx)
{
	struct stack *prev;

	if (!ctx->sp->prev) {
		error("too many )");
		return 0;
	}
	prev = ctx->sp->prev;
	free(ctx->sp);
	ctx->sp = prev;
	return 1;
}


static void new_expr(struct sexpr_ctx *ctx)
{
	struct stack *st = alloc_type(struct stack);
	struct expr *e = alloc_type(struct expr);

	e->s = NULL;
	e->e = NULL;
	e->next = NULL;

	*ctx->sp->next = e;
	ctx->sp->next = &e->next;

	st->prev = ctx->sp;
	st->next = &e->e;
	ctx->sp = st;
}


static void add_string(struct sexpr_ctx *ctx, const char *end)
{
	struct expr *e;
	unsigned old;
	unsigned new = end - ctx->p;

	e = *ctx->sp->next;
	if (e) {
		old = strlen(e->s);
	} else {
		e = alloc_type(struct expr);
		e->s = NULL;
		e->e = NULL;
		e->next = NULL;

		*ctx->sp->next = e;
		old = 0;
	}

	if (!new)
		return;

	e->s = realloc_size(e->s, old + new + 1);
	memcpy(e->s + old, ctx->p, new);
	e->s[old + new] = 0;
}


static void end_string(struct sexpr_ctx *ctx, const char *end)
{
	struct expr *e;
	char *s, *t;

	add_string(ctx, end);
	e = *ctx->sp->next;
	ctx->sp->next = &e->next;

	for (s = t = e->s; *s; s++) {
		if (*s == '\\')
			switch (*++s) {
			case 'n':
				*t++ = '\n';
				continue;
			case 't':
				*t++ = '\t';
				continue;
			case 0:
				BUG("escaped NUL");
			default:
				break;
			}
		*t++ = *s;
	}
	*t = 0;
}


bool sexpr_parse(struct sexpr_ctx *ctx, const char *s)
{
	ctx->p = s;
	while (*s) {
		switch (ctx->state) {
		case idle:
			switch (*s) {
			case ' ':
			case '\t':
			case '\r':
			case '\n':
				break;
			case '(':
				new_expr(ctx);
				break;
			case ')':
				if (!pop(ctx))
					ctx->state = failed;
				break;
			case '"':
				ctx->state = string;
				ctx->p = s + 1;
				break;
			default:
				ctx->p = s;
				ctx->state = token;
				break;
			}
			break;
		case token:
			switch (*s) {
			case ' ':
			case '\t':
			case '\r':
			case '\n':
				ctx->state = idle;
				end_string(ctx, s);
				break;
			case '(':
			case ')':
			case '"':
				ctx->state = idle;
				end_string(ctx, s);
				continue;
			default:
				break;
			}
			break;
		case string:
			switch (*s) {
			case '\r':
			case '\n':
				ctx->state = failed;
				error("newline in string");
				break;
			case '"':
				ctx->state = idle;
				end_string(ctx, s);
				break;
			case '\\':
				ctx->state = escape;
				break;
			default:
				break;
			}
			break;
		case escape:
			switch (*s) {
			case '\r':
			case '\n':
				ctx->state = failed;
				error("newline in string");
				break;
			default:
				ctx->state = string;
				break;
			}
			break;
		case failed:
			return 0;
		default:
			BUG("invalid state %d", ctx->state);
		}
		s++;
	}

	switch (ctx->state) {
	case token:
	case string:
	case escape:
		add_string(ctx, s);
		break;
	default:
		break;
	}

	return 1;
}


/* ----- Dumping ----------------------------------------------------------- */


static void do_dump_expr(const struct expr *e, unsigned depth)
{
	bool need_nl = 0;

	if (depth)
		printf("%*s(", (depth - 1) * 2, "");
	while (e) {
		if (e->s) {
			if (need_nl) {
				printf("\n%*s", depth * 2, "");
				need_nl = 0;
			}
			printf("\"%s\" ", e->s); /* @@@ escaping */
		}
		if (e->e) {
			putchar('\n');
			do_dump_expr(e->e, depth + 1);
			need_nl = 1;
		}
		e = e->next;
	}
	if (depth)
		putchar(')');
}


void dump_expr(const struct expr *e)
{
	do_dump_expr(e, 0);
	putchar('\n');
}


/* ----- Cleanup ----------------------------------------------------------- */


static void free_stack(struct sexpr_ctx *ctx)
{
	struct stack *prev;

	while (ctx->sp != &ctx->stack) {
		prev = ctx->sp->prev;
		free(ctx->sp);
		ctx->sp = prev;
	}
}


void free_expr(struct expr *e)
{
	struct expr *next;

	while (e) {
		next = e->next;
		if (e->s)
			free(e->s);
		else if (e->e)
			free_expr(e->e);
		free(e);
		e = next;
	}
}


/* ----- Parser creation --------------------------------------------------- */


struct sexpr_ctx *sexpr_new(void)
{
	struct sexpr_ctx *ctx;

	ctx = alloc_type(struct sexpr_ctx);
	ctx->state = idle;
	ctx->e = NULL;
	ctx->p = NULL;
	ctx->sp = &ctx->stack;
	ctx->stack.next = &ctx->e;
	ctx->stack.prev = NULL;
	return ctx;
}


/* ----- Termination ------------------------------------------------------- */


void sexpr_abort(struct sexpr_ctx *ctx)
{
	free_stack(ctx);
	free_expr(ctx->e);
	free(ctx);
}


bool sexpr_finish(struct sexpr_ctx *ctx, struct expr **res)
{
	if (ctx->sp != &ctx->stack) {
		error("not enough )");
		ctx->state = failed;
	}
	if (ctx->state != idle && ctx->state != failed)
		error("invalid end state %d", ctx->state);
	if (ctx->state != idle) {
		sexpr_abort(ctx);
		return 0;
	}
	if (res)
		*res = ctx->e;
	else
		free_expr(ctx->e);
	free(ctx);
	return 1;
}