aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/libfyaml/src/lib/fy-composer.c
blob: 2202497d69551427a8dacfcef23820192359e278 (plain) (blame)
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
351
352
353
354
355
356
/*
 * fy-composer.c - Composer support
 *
 * Copyright (c) 2021 Pantelis Antoniou <pantelis.antoniou@konsulko.com>
 *
 * SPDX-License-Identifier: MIT
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <stdarg.h>

#include <libfyaml.h>

#include "fy-parse.h"
#include "fy-doc.h"

#include "fy-utils.h"

struct fy_composer *
fy_composer_create(struct fy_composer_cfg *cfg)
{
	struct fy_composer *fyc;
	struct fy_path *fypp;

	/* verify configuration and mandatory ops */
	if (!cfg || !cfg->ops ||
	    !cfg->ops->process_event)
		return NULL;

	fyc = malloc(sizeof(*fyc));
	if (!fyc)
		return NULL;
	memset(fyc, 0, sizeof(*fyc));
	fyc->cfg = *cfg;

	fy_path_list_init(&fyc->paths);
	fypp = fy_path_create();
	if (!fypp)
		goto err_no_path;
	fy_path_list_add_tail(&fyc->paths, fypp);

	return fyc;

err_no_path:
	free(fyc);
	return NULL;
}

void fy_composer_destroy(struct fy_composer *fyc)
{
	struct fy_path *fypp;

	if (!fyc)
		return;

	fy_diag_unref(fyc->cfg.diag);
	while ((fypp = fy_path_list_pop(&fyc->paths)) != NULL)
		fy_path_destroy(fypp);
	free(fyc);
}

static enum fy_composer_return
fy_composer_process_event_private(struct fy_composer *fyc, struct fy_event *fye, struct fy_path *fypp)
{
	const struct fy_composer_ops *ops;
	struct fy_eventp *fyep;
	struct fy_path_component *fypc, *fypc_last;
	struct fy_path *fyppt;
	struct fy_document *fyd;
	bool is_collection, is_map, is_start, is_end;
	int rc = 0;
	enum fy_composer_return ret;
	bool stop_req = false;

	assert(fyc);
	assert(fye);
	assert(fypp);

	fyep = fy_container_of(fye, struct fy_eventp, e);

	ops = fyc->cfg.ops;
	assert(ops);

	rc = 0;

	switch (fye->type) {
	case FYET_MAPPING_START:
		is_collection = true;
		is_start = true;
		is_end = false;
		is_map = true;
		break;

	case FYET_MAPPING_END:
		is_collection = true;
		is_start = false;
		is_end = true;
		is_map = true;
		break;

	case FYET_SEQUENCE_START:
		is_collection = true;
		is_start = true;
		is_end = false;
		is_map = false;
		break;

	case FYET_SEQUENCE_END:
		is_collection = true;
		is_start = false;
		is_end = true;
		is_map = false;
		break;

	case FYET_SCALAR:
		is_collection = false;
		is_start = true;
		is_end = true;
		is_map = false;
		break;

	case FYET_ALIAS:
		is_collection = false;
		is_start = true;
		is_end = true;
		is_map = false;
		break;

	case FYET_STREAM_START:
	case FYET_STREAM_END:
	case FYET_DOCUMENT_START:
	case FYET_DOCUMENT_END:
		return ops->process_event(fyc, fypp, fye);

	default:
		return FYCR_OK_CONTINUE;
	}

	fypc_last = fy_path_component_list_tail(&fypp->components);

	if (fy_path_component_is_mapping(fypc_last) && fypc_last->map.accumulating_complex_key) {

		/* get the next one */
		fyppt = fy_path_next(&fyc->paths, fypp);
		assert(fyppt);
		assert(fyppt != fypp);
		assert(fyppt->parent == fypp);

		/* and pass along */
		ret = fy_composer_process_event_private(fyc, fye, fyppt);
		if (!fy_composer_return_is_ok(ret)) {
			/* XXX TODO handle skip */
			return ret;
		}
		if (!stop_req)
			stop_req = ret == FYCR_OK_STOP;

		rc = fy_document_builder_process_event(fypp->fydb, fyep);
		if (rc == 0)
			return FYCR_OK_CONTINUE;
		fyc_error_check(fyc, rc > 0, err_out,
				"fy_document_builder_process_event() failed\n");

		/* get the document */
		fyd = fy_document_builder_take_document(fypp->fydb);
		fyc_error_check(fyc, fyd, err_out,
				"fy_document_builder_take_document() failed\n");

		fypc_last->map.is_complex_key = true;
		fypc_last->map.accumulating_complex_key = false;
		fypc_last->map.complex_key = fyd;
		fypc_last->map.has_key = true;
		fypc_last->map.await_key = false;
		fypc_last->map.complex_key_complete = true;
		fypc_last->map.root = false;

		fyppt = fy_path_list_pop_tail(&fyc->paths);
		assert(fyppt);

		fy_path_destroy(fyppt);

		fyc_error_check(fyc, rc >= 0, err_out,
				"fy_path_component_build_text() failed\n");

		return !stop_req ? FYCR_OK_CONTINUE : FYCR_OK_STOP;
	}

	/* start of something on a mapping */
	if (is_start && fy_path_component_is_mapping(fypc_last) && fypc_last->map.await_key && is_collection) {

		/* the configuration must support a document builder for complex keys */
		FYC_TOKEN_ERROR_CHECK(fyc, fy_event_get_token(fye), FYEM_DOC,
				ops->create_document_builder, err_out,
				"composer configuration does not support complex keys");

		/* call out for creating the document builder */
		fypp->fydb = ops->create_document_builder(fyc);
		fyc_error_check(fyc, fypp->fydb, err_out,
				"ops->create_document_builder() failed\n");

		/* and pass the current event; must return 0 since we know it's a collection start */
		rc = fy_document_builder_process_event(fypp->fydb, fyep);
		fyc_error_check(fyc, !rc, err_out,
				"fy_document_builder_process_event() failed\n");

		fypc_last->map.is_complex_key = true;
		fypc_last->map.accumulating_complex_key = true;
		fypc_last->map.complex_key = NULL;
		fypc_last->map.complex_key_complete = false;

		/* create new path */
		fyppt = fy_path_create();
		fyc_error_check(fyc, fyppt, err_out,
				"fy_path_create() failed\n");

		/* append it to the end */
		fyppt->parent = fypp;
		fy_path_list_add_tail(&fyc->paths, fyppt);

		/* and pass along */
		ret = fy_composer_process_event_private(fyc, fye, fyppt);
		if (!fy_composer_return_is_ok(ret)) {
			/* XXX TODO handle skip */
			return ret;
		}
		if (!stop_req)
			stop_req = ret == FYCR_OK_STOP;

		return !stop_req ? FYCR_OK_CONTINUE : FYCR_OK_STOP;
	}

	if (is_start && fy_path_component_is_sequence(fypc_last)) {	/* start in a sequence */

		if (fypc_last->seq.idx < 0)
			fypc_last->seq.idx = 0;
		else
			fypc_last->seq.idx++;
	}

	if (is_collection && is_start) {

		/* collection start */
		if (is_map) {
			fypc = fy_path_component_create_mapping(fypp);
			fyc_error_check(fyc, fypc, err_out,
					"fy_path_component_create_mapping() failed\n");
		} else {
			fypc = fy_path_component_create_sequence(fypp);
			fyc_error_check(fyc, fypc, err_out,
					"fy_path_component_create_sequence() failed\n");
		}

		/* append to the tail */
		fy_path_component_list_add_tail(&fypp->components, fypc);

	} else if (is_collection && is_end) {

		/* collection end */
		assert(fypc_last);
		fy_path_component_clear_state(fypc_last);

	} else if (!is_collection && fy_path_component_is_mapping(fypc_last) && fypc_last->map.await_key) {

		fypc_last->map.is_complex_key = false;
		fypc_last->map.scalar.tag = fy_token_ref(fy_event_get_tag_token(fye));
		fypc_last->map.scalar.key = fy_token_ref(fy_event_get_token(fye));
		fypc_last->map.has_key = true;
		fypc_last->map.root = false;

	}

	/* process the event */
	ret = ops->process_event(fyc, fypp, fye);
	if (!fy_composer_return_is_ok(ret)) {
		/* XXX TODO handle skip */
		return ret;
	}
	if (!stop_req)
		stop_req = ret == FYCR_OK_STOP;

	if (is_collection && is_end) {
		/* for the end of a collection, pop the last component */
		fypc = fy_path_component_list_pop_tail(&fypp->components);
		assert(fypc);

		assert(fypc == fypc_last);

		fy_path_component_recycle(fypp, fypc);

		/* and get the new last */
		fypc_last = fy_path_component_list_tail(&fypp->components);
	}

	/* at the end of something */
	if (is_end && fy_path_component_is_mapping(fypc_last)) {
		if (!fypc_last->map.await_key) {
			fy_path_component_clear_state(fypc_last);
			fypc_last->map.await_key = true;
		} else
			fypc_last->map.await_key = false;
	}

	return !stop_req ? FYCR_OK_CONTINUE : FYCR_OK_STOP;

err_out:
	return FYCR_ERROR;
}

enum fy_composer_return
fy_composer_process_event(struct fy_composer *fyc, struct fy_event *fye)
{
	struct fy_path *fypp;
	int rc;

	if (!fyc || !fye)
		return -1;

	/* start at the head */
	fypp = fy_path_list_head(&fyc->paths);

	/* no top? something's very out of order */
	if (!fypp)
		return -1;

	rc = fy_composer_process_event_private(fyc, fye, fypp);

	return rc;
}

struct fy_composer_cfg *fy_composer_get_cfg(struct fy_composer *fyc)
{
	if (!fyc)
		return NULL;
	return &fyc->cfg;
}

void *fy_composer_get_cfg_userdata(struct fy_composer *fyc)
{
	if (!fyc)
		return NULL;
	return fyc->cfg.userdata;
}

struct fy_diag *fy_composer_get_diag(struct fy_composer *fyc)
{
	if (!fyc)
		return NULL;
	return fyc->cfg.diag;
}