css_property_parser_gen.c 18.4 KB
Newer Older
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
/*
 * This file generates parts of LibCSS.
 * Licensed under the MIT License,
 *		  http://www.opensource.org/licenses/mit-license.php
 * Copyright 2010 Vincent Sanders <vince@netsurf-browser.org>
 * Copyright (C) 2021 Beijing FMSoft Technologies Co., Ltd.
 */

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

/* Descriptors are space separated key:value pairs brackets () are
 * used to quote in values.
 *
 * Examples:
 * list_style_image:CSS_PROP_LIST_STYLE_IMAGE IDENT:( INHERIT: NONE:0,LIST_STYLE_IMAGE_NONE IDENT:) URI:LIST_STYLE_IMAGE_URI
 *
 * list_style_position:CSS_PROP_LIST_STYLE_POSITION IDENT:( INHERIT: INSIDE:0,LIST_STYLE_POSITION_INSIDE OUTSIDE:0,LIST_STYLE_POSITION_OUTSIDE IDENT:)
*/

struct keyval {
	char *key;
	char *val;
};

struct keyval_list {
	struct keyval *item[100];
	int count;
};

struct keyval *get_keyval(char **pos)
{
	char *endpos;
	struct keyval *nkeyval;
	int kvlen;

	endpos = strchr(*pos, ' '); /* single space separated pairs */
	if (endpos == NULL) {
		/* no space, but might be the end of the input */
		kvlen = strlen(*pos);
		if (kvlen == 0)
			return NULL;
	} else {
		kvlen = (endpos - *pos);
	}
	nkeyval = calloc(1, sizeof(struct keyval) + kvlen + 1);

	memcpy(nkeyval + 1, *pos, kvlen);

	nkeyval->key = (char *)nkeyval + sizeof(struct keyval);

	endpos = strchr(nkeyval->key, ':'); /* split key and value on : */
	if (endpos != NULL) {
		endpos[0] = 0; /* change : to null terminator */
		nkeyval->val = endpos + 1; /* skip : */
	}

	*pos += kvlen; /* update position */

	/* skip spaces */
	while ((*pos[0] != 0) &&
	       (*pos[0] == ' ')) {
		(*pos)++;
	}

	return nkeyval;
}

void output_header(FILE *outputf, const char *descriptor, struct keyval *parser_id, bool is_generic)
{
	fprintf(outputf,
XueShuming's avatar
XueShuming committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
        "/////////////////////////////////////////////////////////////////////////////// //\n"
        "//                          IMPORTANT NOTICE\n"
        "//\n"
        "// The following open source license statement does not apply to any\n"
        "// entity in the Exception List published by FMSoft.\n"
        "//\n"
        "// For more information, please visit:\n"
        "//\n"
        "// https://www.fmsoft.cn/exception-list\n"
        "//\n"
        "//////////////////////////////////////////////////////////////////////////////\n"
        "/**\n"
        " \\verbatim\n"
        "\n"
        "    This file is part of HiDOMLayout. hiDOMLayout is a library to\n"
        "    maintain a DOM tree, lay out and stylize the DOM nodes by\n"
        "    using CSS (Cascaded Style Sheets).\n"
        "\n"
        "    Copyright (C) 2021 Beijing FMSoft Technologies Co., Ltd.\n"
        "\n"
        "    This program is free software: you can redistribute it and/or modify\n"
XueShuming's avatar
XueShuming committed
95
        "    it under the terms of the GNU Lesser General License as published by\n"
XueShuming's avatar
XueShuming committed
96
97
98
99
100
101
        "    the Free Software Foundation, either version 3 of the License, or\n"
        "    (at your option) any later version.\n"
        "\n"
        "    This program is distributed in the hope that it will be useful,\n"
        "    but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
        "    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
XueShuming's avatar
XueShuming committed
102
        "    GNU Lesser General License for more details.\n"
XueShuming's avatar
XueShuming committed
103
        "\n"
XueShuming's avatar
XueShuming committed
104
        "    You should have received a copy of the GNU Lesser General License\n"
XueShuming's avatar
XueShuming committed
105
106
107
108
109
        "    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n"
        "\n"
        "    Or,\n"
        "\n"
        "    As this program is a library, any link to this program must follow\n"
XueShuming's avatar
XueShuming committed
110
111
        "    GNU Lesser General License version 3 (LLGPLv3). If you cannot accept\n"
        "    LLGPLv3, you need to be licensed from FMSoft.\n"
XueShuming's avatar
XueShuming committed
112
113
114
115
116
117
118
119
120
121
        "\n"
        "    If you have got a commercial license of this program, please use it\n"
        "    under the terms and conditions of the commercial license.\n"
        "\n"
        "    For more information about the commercial license, please refer to\n"
        "    <http://www.minigui.com/blog/minigui-licensing-policy/>.\n"
        "\n"
        " \\endverbatim\n"
        " */\n"
        "\n"
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
		"/*\n"
		" * This file was generated by LibCSS gen_parser \n"
		" * \n"
		" * Generated from:\n"
		" *\n"
		" * %s\n"
		" * \n"
		" * Copyright (C) 2021 Beijing FMSoft Technologies Co., Ltd.\n"
		" */\n"
		"\n"
		"#include <assert.h>\n"
		"#include <string.h>\n"
		"\n"
		"#include \"bytecode/bytecode.h\"\n"
		"#include \"bytecode/opcodes.h\"\n"
		"#include \"parse/properties/properties.h\"\n"
		"#include \"parse/properties/utils.h\"\n"
		"\n"
		"/**\n"
		" * Parse %s\n"
		" *\n"
		" * \\param c	  Parsing context\n"
		" * \\param vector  Vector of tokens to process\n"
		" * \\param ctx	  Pointer to vector iteration context\n"
		" * \\param result  resulting style\n"
		"%s"
		" * \\return CSS_OK on success,\n"
		" *	   CSS_NOMEM on memory exhaustion,\n"
		" *	   CSS_INVALID if the input is not valid\n"
		" *\n"
		" * Post condition: \\a *ctx is updated with the next token to process\n"
		" *		   If the input is invalid, then \\a *ctx remains unchanged.\n"
		" */\n"
		"css_error css__parse_%s(css_language *c,\n"
		"		const parserutils_vector *vector, int *ctx,\n"
		"		css_style *result%s)\n"
		"{\n",
		descriptor,
		parser_id->key,
		is_generic ? " * \\param op	 Bytecode OpCode for CSS property to encode\n" : "",
		parser_id->key,
		is_generic ? ", enum css_properties_e op" : "");
}


void output_token_type_check(FILE *outputf, bool do_token_check, struct keyval_list *IDENT, struct keyval_list *URI, struct keyval_list *NUMBER)
{
	fprintf(outputf,
		"	int orig_ctx = *ctx;\n"
		"	css_error error;\n"
		"	const css_token *token;\n"
		"	bool match;\n\n"
		"	token = parserutils_vector_iterate(vector, ctx);\n"
		"	if (%stoken == NULL%s",
		do_token_check ? "(" : "",
		do_token_check ? ")" : "");

	if (do_token_check) {
		bool prev = false; /* there was a previous check - add && */
		fprintf(outputf," || (");

		if (IDENT->count > 0) {
			fprintf(outputf,"(token->type != CSS_TOKEN_IDENT)");
			prev = true;
		}
		if (URI->count > 0) {
			if (prev) fprintf(outputf," && ");
			fprintf(outputf,"(token->type != CSS_TOKEN_URI)");
			prev = true;
		}
		if (NUMBER->count > 0) {
			if (prev) fprintf(outputf," && ");
			fprintf(outputf,"(token->type != CSS_TOKEN_NUMBER)");
			prev = true;
		}

		fprintf(outputf,")");
	}

	fprintf(outputf,
		") {\n"
		"\t\t*ctx = orig_ctx;\n"
		"\t\treturn CSS_INVALID;\n"
		"\t}\n\n\t");
}

void output_ident(FILE *outputf, bool only_ident, struct keyval *parseid, struct keyval_list *IDENT)
{
	int ident_count;

	for (ident_count = 0 ; ident_count < IDENT->count; ident_count++) {
		struct keyval *ckv = IDENT->item[ident_count];

		fprintf(outputf,
			"if (");
		if (!only_ident) {
			fprintf(outputf,
			"(token->type == CSS_TOKEN_IDENT) && ");
		}
		fprintf(outputf,
			"(lwc_string_caseless_isequal(token->idata, c->strings[%s], &match) == lwc_error_ok && match)) {\n",
			ckv->key);
		if (strcmp(ckv->key,"INHERIT") == 0) {
		fprintf(outputf,
			"\t\t\terror = css_stylesheet_style_inherit(result, %s);\n",
			parseid->val);
		} else {
		fprintf(outputf,
			"\t\t\terror = css__stylesheet_style_appendOPV(result, %s, %s);\n",
			parseid->val,
			ckv->val);
		}
		fprintf(outputf,
			"\t} else ");
	}
}

void output_uri(FILE *outputf, struct keyval *parseid, struct keyval_list *kvlist)
{
	struct keyval *ckv = kvlist->item[0];

	fprintf(outputf,
		"if (token->type == CSS_TOKEN_URI) {\n"
		"		lwc_string *uri = NULL;\n"
		"		uint32_t uri_snumber;\n"
		"\n"
		"		error = c->sheet->resolve(c->sheet->resolve_pw,\n"
		"				c->sheet->url,\n"
		"				token->idata, &uri);\n"
		"		if (error != CSS_OK) {\n"
		"			*ctx = orig_ctx;\n"
		"			return error;\n"
		"		}\n"
		"\n"
		"		error = css__stylesheet_string_add(c->sheet, uri, &uri_snumber);\n"
		"		if (error != CSS_OK) {\n"
		"			*ctx = orig_ctx;\n"
		"			return error;\n"
		"		}\n"
		"\n"
		"		error = css__stylesheet_style_appendOPV(result, %s, 0, %s);\n"
		"		if (error != CSS_OK) {\n"
		"			*ctx = orig_ctx;\n"
		"			return error;\n"
		"		}\n"
		"\n"
		"		error = css__stylesheet_style_append(result, uri_snumber);\n"
		"	} else ",
		parseid->val,
		ckv->val);
}

void output_number(FILE *outputf, struct keyval *parseid, struct keyval_list *kvlist)
{
	struct keyval *ckv = kvlist->item[0];
	int ident_count;

	fprintf(outputf,
		"if (token->type == CSS_TOKEN_NUMBER) {\n"
		"\t\tcss_fixed num = 0;\n"
		"\t\tsize_t consumed = 0;\n\n"
		"\t\tnum = css__number_from_lwc_string(token->idata, %s, &consumed);\n"
		"\t\t/* Invalid if there are trailing characters */\n"
		"\t\tif (consumed != lwc_string_length(token->idata)) {\n"
		"\t\t\t*ctx = orig_ctx;\n"
		"\t\t\treturn CSS_INVALID;\n"
		"\t\t}\n",
		ckv->key);

	for (ident_count = 1 ; ident_count < kvlist->count; ident_count++) {
		struct keyval *ulkv = kvlist->item[ident_count];

		if (strcmp(ulkv->key, "RANGE") == 0) {
			fprintf(outputf,
				"\t\tif (%s) {\n"
				"\t\t\t*ctx = orig_ctx;\n"
				"\t\t\treturn CSS_INVALID;\n"
				"\t\t}\n\n",
				ulkv->val);
		}

	}

	fprintf(outputf,
		"\t\terror = css__stylesheet_style_appendOPV(result, %s, 0, %s);\n"
		"\t\tif (error != CSS_OK) {\n"
		"\t\t\t*ctx = orig_ctx;\n"
		"\t\t\treturn error;\n"
		"\t\t}\n\n"
		"\t\terror = css__stylesheet_style_append(result, num);\n"
		"\t} else ",
		parseid->val,
		ckv->val);
}

void output_color(FILE *outputf, struct keyval *parseid, struct keyval_list *kvlist)
{
	fprintf(outputf,
		"{\n"
		"\t\tuint16_t value = 0;\n"
		"\t\tuint32_t color = 0;\n"
		"\t\t*ctx = orig_ctx;\n\n"
		"\t\terror = css__parse_colour_specifier(c, vector, ctx, &value, &color);\n"
		"\t\tif (error != CSS_OK) {\n"
		"\t\t\t*ctx = orig_ctx;\n"
		"\t\t\treturn error;\n"
		"\t\t}\n\n"
		"\t\terror = css__stylesheet_style_appendOPV(result, %s, 0, value);\n"
		"\t\tif (error != CSS_OK) {\n"
		"\t\t\t*ctx = orig_ctx;\n"
		"\t\t\treturn error;\n"
		"\t\t}\n"
		"\n"
		"\t\tif (value == COLOR_SET)\n"
		"\t\t\terror = css__stylesheet_style_append(result, color);\n"
		"\t}\n\n",
		parseid->val);
}

void output_length_unit(FILE *outputf, struct keyval *parseid, struct keyval_list *kvlist)
{
	struct keyval *ckv = kvlist->item[0];
	int ident_count;


	fprintf(outputf,
		"{\n"
		"\t\tcss_fixed length = 0;\n"
		"\t\tuint32_t unit = 0;\n"
		"\t\t*ctx = orig_ctx;\n\n"
		"\t\terror = css__parse_unit_specifier(c, vector, ctx, %s, &length, &unit);\n"
		"\t\tif (error != CSS_OK) {\n"
		"\t\t\t*ctx = orig_ctx;\n"
		"\t\t\treturn error;\n"
		"\t\t}\n\n",
		ckv->key);

	for (ident_count = 1 ; ident_count < kvlist->count; ident_count++) {
		struct keyval *ulkv = kvlist->item[ident_count];

		if (strcmp(ulkv->key, "ALLOW") == 0) {
			fprintf(outputf,
				"\t\tif ((%s) == false) {\n"
				"\t\t\t*ctx = orig_ctx;\n"
				"\t\t\treturn CSS_INVALID;\n"
				"\t\t}\n\n",
				ulkv->val);
		} else if (strcmp(ulkv->key, "DISALLOW") == 0) {
			fprintf(outputf,
				"\t\tif (%s) {\n"
				"\t\t\t*ctx = orig_ctx;\n"
				"\t\t\treturn CSS_INVALID;\n"
				"\t\t}\n\n",
				ulkv->val);
		} else if (strcmp(ulkv->key, "RANGE") == 0) {
			fprintf(outputf,
				"\t\tif (length %s) {\n"
				"\t\t\t*ctx = orig_ctx;\n"
				"\t\t\treturn CSS_INVALID;\n"
				"\t\t}\n\n",
				ulkv->val);
		}

	}

	fprintf(outputf,
		"\t\terror = css__stylesheet_style_appendOPV(result, %s, 0, %s);\n"
		"\t\tif (error != CSS_OK) {\n"
		"\t\t\t*ctx = orig_ctx;\n"
		"\t\t\treturn error;\n"
		"\t\t}\n"
		"\n"
		"\t\terror = css__stylesheet_style_vappend(result, 2, length, unit);\n"
		"\t}\n\n",
		parseid->val,
		ckv->val);
}

void
output_ident_list(FILE *outputf,
		  struct keyval *parseid,
		  struct keyval_list *kvlist)
{
	struct keyval *ckv = kvlist->item[0]; /* list type : opv value */
	struct keyval *ikv;

	if (strcmp(ckv->key, "STRING_OPTNUM") != 0) {
		fprintf(stderr, "unknown IDENT list type %s\n", ckv->key);
		exit(4);
	}

	if (kvlist->count < 2) {
		fprintf(stderr, "Not enough parameters to IDENT list type %s\n", ckv->key);
		exit(4);
	}

	/* list of IDENT and optional numbers */
	ikv = kvlist->item[1]; /* numeric default : end condition */

	fprintf(outputf,
		"{\n"
		"\t\terror = css__stylesheet_style_appendOPV(result, %s, 0, %s);\n"
		"\t\tif (error != CSS_OK) {\n"
		"\t\t\t*ctx = orig_ctx;\n"
		"\t\t\treturn error;\n"
		"\t\t}\n\n"
		"\t\twhile ((token != NULL) && (token->type == CSS_TOKEN_IDENT)) {\n"
		"\t\t\tuint32_t snumber;\n"
		"\t\t\tcss_fixed num;\n"
		"\t\t\tint pctx;\n\n"
		"\t\t\terror = css__stylesheet_string_add(c->sheet, lwc_string_ref(token->idata), &snumber);\n"
		"\t\t\tif (error != CSS_OK) {\n"
		"\t\t\t\t*ctx = orig_ctx;\n"
		"\t\t\t\treturn error;\n"
		"\t\t\t}\n\n"
		"\t\t\terror = css__stylesheet_style_append(result, snumber);\n"
		"\t\t\tif (error != CSS_OK) {\n"
		"\t\t\t\t*ctx = orig_ctx;\n"
		"\t\t\t\treturn error;\n"
		"\t\t\t}\n\n"
		"\t\t\tconsumeWhitespace(vector, ctx);\n\n"
		"\t\t\tpctx = *ctx;\n"
		"\t\t\ttoken = parserutils_vector_iterate(vector, ctx);\n"
		"\t\t\tif ((token != NULL) && (token->type == CSS_TOKEN_NUMBER)) {\n"
		"\t\t\t\tsize_t consumed = 0;\n\n"
		"\t\t\t\tnum = css__number_from_lwc_string(token->idata, true, &consumed);\n"
		"\t\t\t\tif (consumed != lwc_string_length(token->idata)) {\n"
		"\t\t\t\t\t*ctx = orig_ctx;\n"
		"\t\t\t\t\treturn CSS_INVALID;\n"
		"\t\t\t\t}\n"
		"\t\t\t\tconsumeWhitespace(vector, ctx);\n\n"
		"\t\t\t\tpctx = *ctx;\n"
		"\t\t\t\ttoken = parserutils_vector_iterate(vector, ctx);\n"
		"\t\t\t} else {\n"
		"\t\t\t\tnum = INTTOFIX(%s);\n"
		"\t\t\t}\n\n"
		"\t\t\terror = css__stylesheet_style_append(result, num);\n"
		"\t\t\tif (error != CSS_OK) {\n"
		"\t\t\t\t*ctx = orig_ctx;\n"
		"\t\t\t\treturn error;\n"
		"\t\t\t}\n\n"
		"\t\t\tif (token == NULL)\n"
		"\t\t\t\tbreak;\n\n"
		"\t\t\tif (token->type == CSS_TOKEN_IDENT) {\n"
		"\t\t\t\terror = css__stylesheet_style_append(result, %s);\n"
		"\t\t\t\tif (error != CSS_OK) {\n"
		"\t\t\t\t\t*ctx = orig_ctx;\n"
		"\t\t\t\t\treturn error;\n"
		"\t\t\t\t}\n"
		"\t\t\t} else {\n"
		"\t\t\t\t*ctx = pctx; /* rewind one token back */\n"
		"\t\t\t}\n"
		"\t\t}\n\n"
		"\t\terror = css__stylesheet_style_append(result, %s);\n"
		"\t}\n\n",
		parseid->val,
		ckv->val,
		ikv->key,
		ckv->val,
		ikv->val);
}

void output_invalidcss(FILE *outputf)
{
	fprintf(outputf, "{\n\t\terror = CSS_INVALID;\n\t}\n\n");
}

void output_footer(FILE *outputf)
{
	fprintf(outputf,
		"	if (error != CSS_OK)\n"
		"		*ctx = orig_ctx;\n"
		"	\n"
		"	return error;\n"
		"}\n\n");
}

void output_wrap(FILE *outputf, struct keyval *parseid, struct keyval_list *WRAP)
{
	struct keyval *ckv = WRAP->item[0];
	fprintf(outputf,
		"	return %s(c, vector, ctx, result, %s);\n}\n",
		ckv->val,
		parseid->val);
}

char str_INHERIT[] = "INHERIT";

struct keyval ident_inherit = {
	.key = str_INHERIT,
};

int main(int argc, char **argv)
{
	char *descriptor;
	char *curpos; /* current position in input string */
	struct keyval *parser_id; /* the parser we are creating output for */
	FILE *outputf;
	struct keyval *rkv; /* current read key:val */
	struct keyval_list *curlist;
	bool do_token_check = true; /* if the check for valid tokens is done */
	bool only_ident = true; /* if the only token type is ident */
	bool is_generic = false;

	struct keyval_list base;
	struct keyval_list IDENT;
	struct keyval_list IDENT_LIST;
	struct keyval_list LENGTH_UNIT;
	struct keyval_list URI;
	struct keyval_list WRAP;
	struct keyval_list NUMBER;
	struct keyval_list COLOR;


	if (argc < 2) {
		fprintf(stderr,"Usage: %s [-o <filename>] <descriptor>\n", argv[0]);
		return 1;
	}

	if ((argv[1][0] == '-') && (argv[1][1] == 'o')) {
		if (argc != 4) {
			fprintf(stderr,"Usage: %s [-o <filename>] <descriptor>\n", argv[0]);
			return 1;
		}
		outputf = fopen(argv[2], "w");
		if (outputf == NULL) {
			perror("unable to open file");
			return 2; /* exit on output file output error */
		}
		descriptor = strdup(argv[3]);
	} else {
		outputf = stdout;
		descriptor = strdup(argv[1]);
	}
	curpos = descriptor;

	base.count = 0;
	IDENT.count = 0;
	URI.count = 0;
	WRAP.count = 0;
	NUMBER.count = 0;
	COLOR.count = 0;
	LENGTH_UNIT.count = 0;
	IDENT_LIST.count = 0;

	curlist = &base;

	while (*curpos != 0) {
		rkv = get_keyval(&curpos);
		if (rkv == NULL) {
			fprintf(stderr,"Token error at offset %ld\n",
					(long)(curpos - descriptor));
			fclose(outputf);
			return 2;
		}

		if (strcmp(rkv->key, "WRAP") == 0) {
			WRAP.item[WRAP.count++] = rkv;
			only_ident = false;
		} else if (strcmp(rkv->key, "NUMBER") == 0) {
			if (rkv->val[0] == '(') {
				curlist = &NUMBER;
			} else if (rkv->val[0] == ')') {
				curlist = &base;
			} else {
				NUMBER.item[NUMBER.count++] = rkv;
			}
			only_ident = false;
		} else if (strcmp(rkv->key, "IDENT") == 0) {
			if (rkv->val[0] == '(') {
				curlist = &IDENT;
			} else if (rkv->val[0] == ')') {
				curlist = &base;
			} else if (strcmp(rkv->val, str_INHERIT) == 0) {
				IDENT.item[IDENT.count++] = &ident_inherit;
			}
		} else if (strcmp(rkv->key, "IDENT_LIST") == 0) {
			if (rkv->val[0] == '(') {
				curlist = &IDENT_LIST;
			} else if (rkv->val[0] == ')') {
				curlist = &base;
			}
		} else if (strcmp(rkv->key, "LENGTH_UNIT") == 0) {
			if (rkv->val[0] == '(') {
				curlist = &LENGTH_UNIT;
			} else if (rkv->val[0] == ')') {
				curlist = &base;
			}
			only_ident = false;
			do_token_check = false;
		} else if (strcmp(rkv->key, "COLOR") == 0) {
			COLOR.item[COLOR.count++] = rkv;
			do_token_check = false;
			only_ident = false;
		} else if (strcmp(rkv->key, "URI") == 0) {
			URI.item[URI.count++] = rkv;
			only_ident = false;
		} else if (strcmp(rkv->key, "GENERIC") == 0) {
			is_generic = true;
		} else {
			/* just append to current list */
			curlist->item[curlist->count++] = rkv;
		}
	}

	if (base.count != 1) {
		fprintf(stderr,"Incorrect base element count (got %d expected 1)\n", base.count);
		fclose(outputf);
		return 3;
	}


	/* header */
output_header(outputf, descriptor, base.item[0], is_generic);

	if (WRAP.count > 0) {
		output_wrap(outputf, base.item[0], &WRAP);
	} else {
		/* check token type is correct */
		output_token_type_check(outputf, do_token_check,  &IDENT, &URI, &NUMBER);

		if (IDENT.count > 0)
			output_ident(outputf, only_ident, base.item[0], &IDENT);

		if (URI.count > 0)
			output_uri(outputf, base.item[0], &URI);

		if (NUMBER.count > 0)
			output_number(outputf, base.item[0], &NUMBER);

		/* terminal blocks, these end the ladder ie no trailing else */
		if (COLOR.count > 0) {
			output_color(outputf, base.item[0], &COLOR);
		} else if (LENGTH_UNIT.count > 0) {
			output_length_unit(outputf, base.item[0], &LENGTH_UNIT);
		} else if (IDENT_LIST.count > 0) {
			output_ident_list(outputf, base.item[0], &IDENT_LIST);
		} else {
			output_invalidcss(outputf);
		}

		output_footer(outputf);

	}

	fclose(outputf);

	return 0;
}