aboutsummaryrefslogtreecommitdiff
path: root/src/parse.c
blob: cd64e1922441136c8d05b6f9f4837e1fd4a99f12 (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
#include "parse.h"

#include <errno.h>
#include <string.h>
#include <assert.h>
#include <libguile.h>
#include "guile_interface.h"

#include "macro.h"
#include "vcal.h"

#include "err.h"

/*
 * name *(";" param) ":" value CRLF
 */
int parse_file(char* filename, FILE* f, vcomponent* root) {
	part_context p_ctx = p_key;

	SNEW(parse_ctx, ctx, f, filename);
	SCM_PUSH_X(ctx.comp_stack, scm_from_vcomponent(root));

	/*
	 * Create a content_line which we use as storage while we are
	 * parsing. This object is constantly broken down and rebuilt.
	 *
	 * {cline,param}_key is also temporary register used during
	 * parsing.
	 */
	// SNEW(content_line, cline);
	SCM content_pair = scm_cons (SCM_BOOL_F, scm_make_hash_table (scm_from_ulong(32)));
	scm_gc_protect_object (content_pair);

	SNEW(strbuf, cline_key);
	SNEW(strbuf, cline_val);

	SNEW(strbuf, param_key);

	char c;
	while ( (c = fgetc(f)) != EOF) {

		/* We have a linebreak */
		if (c == '\r' || c == '\n') {

			if (fold(&ctx, c) > 0) {
				 TRANSFER(&cline_val, &ctx.str);

				/* Actuall end of line, handle value */
				handle_kv(&cline_key, &cline_val, &content_pair, &ctx);
				p_ctx = p_key;
			} /* Else continue on current line */

		/* We have an escaped character */
		} else if (c == '\\') {
			handle_escape (&ctx);

		/* Border between param {key, value} */
		} else if (p_ctx == p_param_name && c == '=') {

			/* Save the current parameter key */
			TRANSFER (&param_key, &ctx.str);
			p_ctx = p_param_value;

		/*
		 * One of four cases:
		 * 1) end of key  , start of value
		 * 2)   ,,   key  ,    ,,    param
		 * 3)   ,,   param,    ,,    param
		 * 4)   ,,   param,    ,,    value
		 */
		} else if ((p_ctx == p_key || p_ctx == p_param_value) && (c == ':' || c == ';')) {

			/* We got a parameter value, push the current string to
			 * the current parameter set. */
			if (p_ctx == p_param_value) {
				/* save current parameter value. */

				// TODO resolve
				scm_hashq_set_x (
						scm_cdr (content_pair),
						scm_string_to_symbol (scm_from_utf8_stringn (param_key.mem, param_key.len)),
						scm_from_utf8_stringn (ctx.str.mem, ctx.str.len));

				strbuf_soft_reset (&ctx.str);
			}

			/*
			 * Top level key.
			 * Copy the key into the current cline, and create a
			 * content_set for the upcomming value and (possible)
			 * parameters.
			 */
			if (p_ctx == p_key) {
				TRANSFER(&cline_key, &ctx.str);
			}

			if      (c == ':') p_ctx = p_value;
			else if (c == ';') p_ctx = p_param_name;

		/*
		 * Nothing interesting happened, append the read character to
		 * the current string.
		 */
		} else {
			strbuf_append(&ctx.str, c);

			++ctx.column;
			++ctx.pcolumn;
		}
	}

	if (! feof(f)) {
		ERR("Error parsing");
	}
	/* Check to see if empty line */
	else if (ctx.str.ptr != 0) {
		/*
		 * The standard (3.4, l. 2675) says that each icalobject must
		 * end with CRLF. My files however does not, so we also parse
		 * the end here.
		 */

		// TODO
		handle_kv(&cline_key, &cline_val, &content_pair, &ctx);

	}

	/*
	FREE(content_line)(&cline);
	FREE(strbuf)(&cline_key);
	FREE(strbuf)(&param_key);

	assert(POP(LLIST(vcomponent))(&ctx.comp_stack) == root);
	assert(EMPTY(LLIST(strbuf))(&ctx.key_stack));
	assert(EMPTY(LLIST(vcomponent))(&ctx.comp_stack));
	*/

	FREE(parse_ctx)(&ctx);

	return 0;
}

/*
 * We have a complete key value pair.
 */
int handle_kv (
	strbuf* key,
	strbuf* val,
	SCM* content_pair,
	parse_ctx* ctx
	) {

	strbuf_cap (key);
	strbuf_cap (val);

	/*
	 * The key being BEGIN means that we decend into a new component.
	 */
	if (strbuf_c(key, "BEGIN")) {
		/* key \in { VCALENDAR, VEVENT, VALARM, VTODO, VTIMEZONE, ...  } */

		NEW(vcomponent, e,
				ctx->str.mem,
				ctx->filename);

		SCM_PUSH_X(ctx->key_stack, scm_string_to_symbol(scm_from_utf8_stringn(ctx->str.mem, ctx->str.len)));

		/* Clear the value list in the parse content_line */
		// RESET(LLIST(content_set))(cline);

		/*
		 * Create the new curent component, link it with the current
		 * component in a parent/child relationship.
		 * Finally push the new component on to the top of the
		 * component stack.
		 */

		// vcomponent* parent = PEEK(LLIST(vcomponent))(&ctx->comp_stack);
		vcomponent* parent = scm_to_vcomponent(scm_car(ctx->comp_stack));
		PUSH(vcomponent)(parent, e);

		// PUSH(LLIST(vcomponent))(&ctx->comp_stack, e);
		SCM_PUSH_X(ctx->comp_stack, scm_from_vcomponent (e));

	/*
	 * The end of a component, go back along the stack to the previous
	 * component.
	 */
	} else if (strbuf_c(key, "END")) {
		// strbuf* expected_key = POP(LLIST(strbuf))(&ctx->key_stack);
		SCM expected_key = scm_car(ctx->key_stack);

		if (! scm_is_eq (expected_key,
					scm_string_to_symbol (scm_from_utf8_stringn (ctx->str.mem, ctx->str.len)))) {

			// ERR_P(ctx, "Expected END:%s, got END:%s.\n%s line",
			// 		expected_key->mem,
			// 		CLINE_CUR_VAL(cline)->mem,
			// 		vcomponent_get_val(
			// 			PEEK(LLIST(vcomponent))(&ctx->comp_stack),
			// 			"X-HNH-FILENAME"));
			// PUSH(LLIST(strbuf))(&ctx->key_stack, expected_key);

			return -1;

		} else {
			// FFREE(strbuf, expected_key);
			// POP(LLIST(vcomponent))(&ctx->comp_stack);
			SCM_POP_X (ctx->key_stack);
			SCM_POP_X (ctx->comp_stack);
		}

	/*
	 * A regular key, value pair. Push it into to the current
	 * component.
	 */
	} else {

		SNEW(strbuf, sbuf);
		TRANSFER (&sbuf, val);

		SCM str = scm_from_utf8_stringn (sbuf.mem, sbuf.len);
		FREE(strbuf) (&sbuf);
		scm_set_car_x (*content_pair, str);

		vcomponent* e = scm_to_vcomponent (scm_car (ctx->comp_stack));
		vcomponent_push_val (e, key, *content_pair);

		*content_pair = scm_cons (SCM_BOOL_F, scm_make_hash_table (scm_from_ulong(32)));
		scm_gc_protect_object (*content_pair);
	}

	strbuf_reset(key);
	strbuf_reset(&ctx->str);

	return 0;
}

int fold(parse_ctx* ctx, char c) {
	int retval;

	char buf[2] = {
		(c == '\n' ? '\n' : (char) fgetc(ctx->f)),
		(char) fgetc(ctx->f)
	};

	ctx->pcolumn = 1;

	if (buf[0] != '\n') {
		ERR_P(ctx, "expected new_line after CR");
		retval = -1;

	} else if (buf[1] == ' ' || buf[1] == '\t') {
		retval = 0;
		ctx->pcolumn++;

	} else if (ungetc(buf[1], ctx->f) != buf[1]) {
		ERR_P(ctx, "Failed to put character back on FILE");
		retval = -2;

	} else {
		retval = 1;
		++ctx->line;
		ctx->column = 0;
	}

	++ctx->pline;

	return retval;
}


INIT_F(parse_ctx, FILE* f, char* filename) {
	self->key_stack = SCM_EOL;
	self->comp_stack = SCM_EOL;

	self->filename = (char*) calloc(sizeof(*filename), strlen(filename) + 1);
	strcpy(self->filename, filename);
	self->f = f;

	self->line    = 0;
	self->column  = 0;

	self->pline   = 1;
	self->pcolumn = 1;

	INIT(strbuf, &self->str);

	return 0;
}

FREE_F(parse_ctx) {

	// FREE(LLIST(strbuf))(&self->key_stack);
	// FREE(LLIST(vcomponent))(&self->comp_stack);
	self->key_stack = SCM_UNDEFINED;
	self->comp_stack = SCM_UNDEFINED;
	free(self->filename);

	self->line = 0;
	self->column = 0;
	FREE(strbuf)(&self->str);

	return 0;
}

int handle_escape (parse_ctx* ctx) {
	char esc = fgetc(ctx->f);

	/*
	 * Escape character '\' and escaped token sepparated by a newline
	 * (since the standard for some reason allows that (!!!))
	 * We are at least guaranteed that it's a folded line, so just
	 * unfold it and continue trying to find a token to escape.
	 */
	if (esc == '\r' || esc == '\n') {
		int ret;
		if ( (ret = fold(ctx, esc)) != 0) {
			if (ret == 1) ERR_P(ctx, "ESC before not folded line");
			else          ERR_P(ctx, "other error: val = %i", ret);
			exit (2);
		} else {
			esc = fgetc(ctx->f);
		}
	}

	/* Escaped new_line */
	if (esc == 'n' || esc == 'N') {
		esc = '\n';

	/* "Standard" escaped character */
	} else if (esc == ';' || esc == ',' || esc == '\\') {
		/* esc already contains character, do nothing */

	/* Invalid escaped character */
	} else {
		ERR_P(ctx, "Non escapable character '%c' (%i)", esc, esc);
	}

	/* save escapade character as a normal character */
	strbuf_append(&ctx->str, esc);

	++ctx->column;
	++ctx->pcolumn;

	return 0;
}