summaryrefslogtreecommitdiff
path: root/src/parse/mq.c
blob: f7b8b6ef6a72bf723e5548fed86b1148eb6ebeeb (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
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
/*
 * This file is part of LibCSS.
 * Licensed under the MIT License,
 *		  http://www.opensource.org/licenses/mit-license.php
 * Copyright 2016 John-Mark Bell <jmb@netsurf-browser.org>
 */

/* https://drafts.csswg.org/mediaqueries/ */

#include <string.h>

#include <libcss/fpmath.h>

#include "stylesheet.h"
#include "bytecode/bytecode.h"
#include "parse/mq.h"
#include "parse/properties/utils.h"
#include "utils/utils.h"

static void css_mq_feature_destroy(css_mq_feature *feature)
{
	if (feature != NULL) {
		lwc_string_unref(feature->name);
		free(feature);
	}
}

static void css__mq_cond_or_feature_destroy(
		css_mq_cond_or_feature *cond_or_feature);

static void css__mq_cond_parts_destroy(css_mq_cond_parts *cond_parts)
{
	if (cond_parts != NULL) {
		for (uint32_t i = 0; i < cond_parts->nparts; i++) {
			css__mq_cond_or_feature_destroy(cond_parts->parts[i]);
		}
		free(cond_parts);
	}
}

static void css__mq_cond_destroy(css_mq_cond *cond)
{
	if (cond != NULL) {
		css__mq_cond_parts_destroy(cond->parts);
		free(cond);
	}
}

static void css__mq_cond_or_feature_destroy(
		css_mq_cond_or_feature *cond_or_feature)
{
	if (cond_or_feature != NULL) {
		switch (cond_or_feature->type) {
		case CSS_MQ_FEATURE:
			css_mq_feature_destroy(cond_or_feature->data.feat);
			break;
		case CSS_MQ_COND:
			css__mq_cond_destroy(cond_or_feature->data.cond);
			break;
		}
		free(cond_or_feature);
	}
}

void css__mq_query_destroy(css_mq_query *media)
{
	while (media != NULL) {
		css_mq_query *next = media->next;

		css__mq_cond_destroy(media->cond);
		free(media);

		media = next;
	}
}

static css_error mq_parse_condition(css_language *c,
		const parserutils_vector *vector, int *ctx,
		bool permit_or, css_mq_cond **cond);

static css_error mq_parse_ratio(
		const parserutils_vector *vector, int *ctx,
		const css_token *numerator, css_fixed *ratio)
{
	const css_token *token;
	css_fixed num, den;
	size_t num_len, den_len;

	/* NUMBER ws* '/' ws* NUMBER */

	/* numerator, ws* already consumed */

	token = parserutils_vector_iterate(vector, ctx);
	if (token == NULL || tokenIsChar(token, '/') == false) {
		return CSS_INVALID;
	}

	consumeWhitespace(vector, ctx);

	token = parserutils_vector_iterate(vector, ctx);
	if (token == NULL || token->type != CSS_TOKEN_NUMBER) {
		return CSS_INVALID;
	}

	num = css__number_from_lwc_string(numerator->idata, true, &num_len);
	den = css__number_from_lwc_string(token->idata, true, &den_len);

	*ratio = css_divide_fixed(num, den);

	return CSS_OK;
}

static css_error mq_create_feature(
		lwc_string *name,
		css_mq_feature **feature)
{
	css_mq_feature *f;

	f = malloc(sizeof(*f));
	if (f == NULL) {
		return CSS_NOMEM;
	}

	memset(f, 0, sizeof(*f));

	f->name = lwc_string_ref(name);

	*feature = f;

	return CSS_OK;
}

static css_error mq_populate_value(css_mq_value *value,
		const css_token *token)
{
	if (token->type == CSS_TOKEN_NUMBER) {
		size_t num_len;
		value->type = CSS_MQ_VALUE_TYPE_NUM;
		value->data.num_or_ratio = css__number_from_lwc_string(
				token->idata, false, &num_len);
	} else if (token->type == CSS_TOKEN_DIMENSION) {
		size_t len = lwc_string_length(token->idata);
		const char *data = lwc_string_data(token->idata);
		uint32_t unit = UNIT_PX;
		size_t consumed;
		css_error error;

		value->type = CSS_MQ_VALUE_TYPE_DIM;
		value->data.dim.len = css__number_from_lwc_string(
				token->idata, false, &consumed);
		error = css__parse_unit_keyword(data + consumed, len - consumed,
				&unit);
		if (error != CSS_OK) {
			return error;
		}
		value->data.dim.unit = unit;
	} else if (token->type == CSS_TOKEN_IDENT) {
		value->type = CSS_MQ_VALUE_TYPE_IDENT;
		value->data.ident = lwc_string_ref(token->idata);
	}

	return CSS_OK;
}

static css_error mq_parse_op(const css_token *token,
		css_mq_feature_op *op)
{
	size_t len;
	const char *data;

	if (token == NULL || token->type != CSS_TOKEN_CHAR)
		return CSS_INVALID;

	len = lwc_string_length(token->idata);
	data = lwc_string_data(token->idata);

	if (len == 2) {
		if (strncasecmp(data, "<=", 2) == 0)
			*op = CSS_MQ_FEATURE_OP_LTE;
		else if (strncasecmp(data, ">=", 2) == 0)
			*op = CSS_MQ_FEATURE_OP_GTE;
		else
			return CSS_INVALID;
	} else if (len == 1) {
		if (*data == '<')
			*op = CSS_MQ_FEATURE_OP_LT;
		else if (*data == '=')
			*op = CSS_MQ_FEATURE_OP_EQ;
		else if (*data == '>')
			*op = CSS_MQ_FEATURE_OP_GT;
		else
			return CSS_INVALID;
	} else {
		return CSS_INVALID;
	}

	return CSS_OK;
}

static css_error mq_parse_range(css_language *c,
		const parserutils_vector *vector, int *ctx,
		const css_token *name_or_value,
		css_mq_feature **feature)
{
	const css_token *token, *value_or_name, *name = NULL, *value2 = NULL;
	css_mq_feature *result;
	css_mq_feature_op op, op2;
	css_fixed ratio, ratio2;
	bool name_first = false, value_is_ratio = false, value2_is_ratio = false, match;
	css_error error;

	/* <mf-range> = <mf-name> [ '<' | '>' ]? '='? <mf-value>
	 *            | <mf-value> [ '<' | '>' ]? '='? <mf-name>
	 *            | <mf-value> '<' '='? <mf-name> '<' '='? <mf-value>
	 *            | <mf-value> '>' '='? <mf-name> '>' '='? <mf-value>
	 */

	if (name_or_value == NULL || (name_or_value->type != CSS_TOKEN_NUMBER &&
			name_or_value->type != CSS_TOKEN_DIMENSION &&
			name_or_value->type != CSS_TOKEN_IDENT)) {
		return CSS_INVALID;
	}

	consumeWhitespace(vector, ctx);

	/* Name-or-value */
	if (name_or_value->type == CSS_TOKEN_NUMBER &&
			tokenIsChar(parserutils_vector_peek(vector, *ctx), '/')) {
		/* ratio */
		error = mq_parse_ratio(vector, ctx, name_or_value, &ratio);
		if (error != CSS_OK) {
			return error;
		}

		consumeWhitespace(vector, ctx);

		value_is_ratio = true;
	} else if (name_or_value->type == CSS_TOKEN_IDENT &&
			lwc_string_caseless_isequal(name_or_value->idata,
				c->strings[INFINITE], &match) == lwc_error_ok && 
			match == false) {
		/* The only ident permitted for mf-value is 'infinite', thus must have name */
		name = name_or_value;
		name_first = true;
	}

	/* Op */
	token = parserutils_vector_iterate(vector, ctx);
	error = mq_parse_op(token, &op);
	if (error != CSS_OK) {
		return error;
	}

	consumeWhitespace(vector, ctx);

	/* Value-or-name */
	value_or_name = parserutils_vector_iterate(vector, ctx);
	if (value_or_name == NULL || (value_or_name->type != CSS_TOKEN_NUMBER &&
			value_or_name->type != CSS_TOKEN_DIMENSION &&
			value_or_name->type != CSS_TOKEN_IDENT)) {
		return CSS_INVALID;
	}

	if (name == NULL) {
		if (value_or_name->type != CSS_TOKEN_IDENT) {
			return CSS_INVALID;
		} else {
			name = value_or_name;
		}
	}

	consumeWhitespace(vector, ctx);

	if (value_or_name->type == CSS_TOKEN_NUMBER &&
			tokenIsChar(parserutils_vector_peek(vector, *ctx), '/')) {
		/* ratio */
		error = mq_parse_ratio(vector, ctx, token, &ratio);
		if (error != CSS_OK) {
			return error;
		}

		consumeWhitespace(vector, ctx);

		value_is_ratio = true;
	}

	token = parserutils_vector_peek(vector, *ctx);
	if (name_first == false && token != NULL && tokenIsChar(token, ')') == false) {
		/* Op2 */
		token = parserutils_vector_iterate(vector, ctx);
		error = mq_parse_op(token, &op2);
		if (error != CSS_OK) {
			return error;
		}

		consumeWhitespace(vector, ctx);

		/* Validate operators: must both be LT(E) or GT(E) */
		if (op == CSS_MQ_FEATURE_OP_LT || op == CSS_MQ_FEATURE_OP_LTE) {
			if (op2 != CSS_MQ_FEATURE_OP_LT && op2 != CSS_MQ_FEATURE_OP_LTE) {
				return CSS_INVALID;
			}
		} else if (op == CSS_MQ_FEATURE_OP_GT || op == CSS_MQ_FEATURE_OP_GTE) {
		       if (op2 != CSS_MQ_FEATURE_OP_GT && op2 != CSS_MQ_FEATURE_OP_GTE) {
				return CSS_INVALID;
			}
		} else {
			return CSS_INVALID;
		}

		/* Value2 */
		value2 = parserutils_vector_iterate(vector, ctx);
		if (value2 == NULL || (value2->type != CSS_TOKEN_NUMBER &&
				value2->type != CSS_TOKEN_DIMENSION &&
				value2->type != CSS_TOKEN_IDENT)) {
			return CSS_INVALID;
		}

		consumeWhitespace(vector, ctx);

		if (value_or_name->type == CSS_TOKEN_NUMBER &&
				tokenIsChar(parserutils_vector_peek(vector, *ctx), '/')) {
			/* ratio */
			error = mq_parse_ratio(vector, ctx, token, &ratio2);
			if (error != CSS_OK) {
				return error;
			}

			consumeWhitespace(vector, ctx);

			value2_is_ratio = true;
		}
	}

	error = mq_create_feature(name->idata, &result);
	if (error != CSS_OK) {
		return error;
	}
	if (name_first) {
		/* Invert operator */
		if (op == CSS_MQ_FEATURE_OP_LT) {
			op = CSS_MQ_FEATURE_OP_GTE;
		} else if (op == CSS_MQ_FEATURE_OP_LTE) {
			op = CSS_MQ_FEATURE_OP_GT;
		} else if (op == CSS_MQ_FEATURE_OP_GT) {
			op = CSS_MQ_FEATURE_OP_LTE;
		} else if (op == CSS_MQ_FEATURE_OP_GTE) {
			op = CSS_MQ_FEATURE_OP_LT;
		}
	}
	result->op = op;
	if (value_is_ratio) {
		result->value.type = CSS_MQ_VALUE_TYPE_RATIO;
		result->value.data.num_or_ratio = ratio;
	} else {
		/* num/dim/ident */
		error = mq_populate_value(&result->value, token);
		if (error != CSS_OK) {
			free(result);
			return error;
		}
	}
	if (value2 != NULL) {
		result->op2 = op2;
		if (value2_is_ratio) {
			result->value2.type = CSS_MQ_VALUE_TYPE_RATIO;
			result->value2.data.num_or_ratio = ratio;
		} else {
			/* num/dim/ident */
			error = mq_populate_value(&result->value2, token);
			if (error != CSS_OK) {
				css_mq_feature_destroy(result);
				return error;
			}
		}
	}

	*feature = result;

	return CSS_OK;
}

static css_error mq_parse_media_feature(css_language *c,
		const parserutils_vector *vector, int *ctx,
		css_mq_feature **feature)
{
	const css_token *name_or_value, *token;
	css_mq_feature *result;
	css_error error;

	/* <media-feature> = ( [ <mf-plain> | <mf-boolean> | <mf-range> ] )
	 * <mf-plain> = <mf-name> : <mf-value>
	 * <mf-boolean> = <mf-name>
	 * <mf-name> = <ident>
	 * <mf-value> = <number> | <dimension> | <ident> | <ratio>
	 */

	/* ( already consumed */

	consumeWhitespace(vector, ctx);

	name_or_value = parserutils_vector_iterate(vector, ctx);
	if (name_or_value == NULL)
		return CSS_INVALID;

	if (name_or_value->type == CSS_TOKEN_IDENT) {
		consumeWhitespace(vector, ctx);

		token = parserutils_vector_peek(vector, *ctx);
		if (tokenIsChar(token, ')')) {
			/* mf-boolean */
			error = mq_create_feature(name_or_value->idata, &result);
			if (error != CSS_OK) {
				return error;
			}

			result->op = CSS_MQ_FEATURE_OP_BOOL;
		} else if (tokenIsChar(token, ':')) {
			/* mf-plain */
			parserutils_vector_iterate(vector, ctx);

			consumeWhitespace(vector, ctx);

			token = parserutils_vector_iterate(vector, ctx);
			if (token == NULL || (token->type != CSS_TOKEN_NUMBER &&
					token->type != CSS_TOKEN_DIMENSION &&
					token->type != CSS_TOKEN_IDENT)) {
				return CSS_INVALID;
			}

			consumeWhitespace(vector, ctx);

			error = mq_create_feature(name_or_value->idata, &result);
			if (error != CSS_OK) {
				return error;
			}
			result->op = CSS_MQ_FEATURE_OP_EQ;

			if (token->type == CSS_TOKEN_NUMBER &&
					tokenIsChar(parserutils_vector_peek(vector, *ctx), '/')) {
				/* ratio */
				css_fixed ratio;

				error = mq_parse_ratio(vector, ctx, token, &ratio);
				if (error != CSS_OK) {
					free(result);
					return error;
				}

				result->value.type = CSS_MQ_VALUE_TYPE_RATIO;
				result->value.data.num_or_ratio = ratio;
			} else {
				/* num/dim/ident */
				error = mq_populate_value(&result->value, token);
				if (error != CSS_OK) {
					free(result);
					return error;
				}
			}

			consumeWhitespace(vector, ctx);
		} else {
			/* mf-range */
			error = mq_parse_range(c, vector, ctx, name_or_value, &result);
			if (error != CSS_OK) {
				return error;
			}

			consumeWhitespace(vector, ctx);
		}
	} else {
		/* mf-range */
		error = mq_parse_range(c, vector, ctx, name_or_value, &result);
		if (error != CSS_OK) {
			return error;
		}

		consumeWhitespace(vector, ctx);
	}

	token = parserutils_vector_iterate(vector, ctx);
	if (tokenIsChar(token, ')') == false) {
		css_mq_feature_destroy(result);
		return CSS_INVALID;
	}

	*feature = result;

	return CSS_OK;
}

/*
 * Consume any value
 *
 * CSS Syntax Module Level 3: 8.2
 */
static css_error mq_parse_consume_any_value(css_language *c,
		const parserutils_vector *vector, int *ctx,
		bool until, const char until_char)
{
	const css_token *token;
	css_error error;

	while (true) {
		consumeWhitespace(vector, ctx);

		token = parserutils_vector_iterate(vector, ctx);
		if (token == NULL) {
			return CSS_INVALID;
		}

		switch (token->type) {
		case CSS_TOKEN_INVALID_STRING:
			return CSS_INVALID;

		case CSS_TOKEN_CHAR:
			if (until && tokenIsChar(token, until_char)) {
				/* Found matching close bracket */
				return CSS_OK;

			} else if (tokenIsChar(token, ')') ||
			           tokenIsChar(token, ']') ||
			           tokenIsChar(token, '}')) {
				/* Non-matching close bracket */
				return CSS_INVALID;
			}
			if (tokenIsChar(token, '(')) {
				/* Need to consume until matching bracket. */
				error = mq_parse_consume_any_value(
						c, vector, ctx, true, ')');
				if (error != CSS_OK) {
					return error;
				}
			} else if (tokenIsChar(token, '[')) {
				/* Need to consume until matching bracket. */
				error = mq_parse_consume_any_value(
						c, vector, ctx, true, ']');
				if (error != CSS_OK) {
					return error;
				}
			} else if (tokenIsChar(token, '{')) {
				/* Need to consume until matching bracket. */
				error = mq_parse_consume_any_value(
						c, vector, ctx, true, '}');
				if (error != CSS_OK) {
					return error;
				}
			}
			break;

		default:
			break;
		}
	}

	return CSS_OK;
}

static css_error mq_parse_general_enclosed(css_language *c,
		const parserutils_vector *vector, int *ctx)
{
	const css_token *token;
	css_error error;

	/* <general-enclosed> = [ <function-token> <any-value> ) ]
	 *                    | ( <ident> <any-value> )
	 */

	token = parserutils_vector_iterate(vector, ctx);
	if (token == NULL) {
		return CSS_INVALID;
	}

	switch (token->type) {
	case CSS_TOKEN_FUNCTION:
		error = mq_parse_consume_any_value(c, vector, ctx, true, ')');
		if (error != CSS_OK) {
			return error;
		}

		token = parserutils_vector_peek(vector, *ctx);
		if (!tokenIsChar(token, ')')) {
			return CSS_INVALID;
		}
		break;

	case CSS_TOKEN_IDENT:
		error = mq_parse_consume_any_value(c, vector, ctx, false, '\0');
		if (error != CSS_OK) {
			return error;
		}
		break;

	default:
		return CSS_INVALID;
	}

	return CSS_OK;
}

static css_error mq_parse_media_in_parens(css_language *c,
		const parserutils_vector *vector, int *ctx,
		css_mq_cond_or_feature **cond_or_feature)
{
	const css_token *token;
	bool match;
	int old_ctx;
	css_mq_cond_or_feature *result = NULL;
	css_error error = CSS_OK;

	/* <media-in-parens> = ( <media-condition> ) | <media-feature> | <general-enclosed>
	 */

	//LPAREN -> condition-or-feature
	//	  "not" or LPAREN -> condition
	//	  IDENT | NUMBER | DIMENSION | RATIO -> feature

	token = parserutils_vector_iterate(vector, ctx);
	if (token == NULL || tokenIsChar(token, '(') == false) {
		return CSS_INVALID;
	}

	consumeWhitespace(vector, ctx);

	token = parserutils_vector_peek(vector, *ctx);
	if (token == NULL) {
		return CSS_INVALID;
	}

	old_ctx = *ctx;

	if (tokenIsChar(token, '(') || (token->type == CSS_TOKEN_IDENT &&
			lwc_string_caseless_isequal(token->idata,
				c->strings[NOT], &match) == lwc_error_ok &&
			match)) {
		css_mq_cond *cond;
		error = mq_parse_condition(c, vector, ctx, true, &cond);
		if (error == CSS_OK) {
			token = parserutils_vector_iterate(vector, ctx);
			if (tokenIsChar(token, ')') == false) {
				return CSS_INVALID;
			}

			result = malloc(sizeof(*result));
			if (result == NULL) {
				css__mq_cond_destroy(cond);
				return CSS_NOMEM;
			}
			memset(result, 0, sizeof(*result));
			result->type = CSS_MQ_COND;
			result->data.cond = cond;
			*cond_or_feature = result;
			return CSS_OK;
		}
	} else if (token->type == CSS_TOKEN_IDENT ||
			token->type == CSS_TOKEN_NUMBER ||
			token->type == CSS_TOKEN_DIMENSION) {
		css_mq_feature *feature;
		error = mq_parse_media_feature(c, vector, ctx, &feature);
		if (error == CSS_OK) {
			result = malloc(sizeof(*result));
			if (result == NULL) {
				css_mq_feature_destroy(feature);
				return CSS_NOMEM;
			}
			memset(result, 0, sizeof(*result));
			result->type = CSS_MQ_FEATURE;
			result->data.feat = feature;
			*cond_or_feature = result;
			return CSS_OK;
		}
	}

	*ctx = old_ctx;
	error = mq_parse_general_enclosed(c, vector, ctx);

	return error;
}

static css_error mq_parse_condition(css_language *c,
		const parserutils_vector *vector, int *ctx,
		bool permit_or, css_mq_cond **cond)
{
	const css_token *token;
	bool match;
	int op = 0; /* Will be AND | OR once we've had one */
	css_mq_cond_or_feature *cond_or_feature, **parts;
	css_mq_cond *result;
	css_error error;

	/* <media-condition> = <media-not> | <media-in-parens> [ <media-and>* | <media-or>* ]
	 * <media-condition-without-or> = <media-not> | <media-in-parens> <media-and>*
	 * <media-not> = not <media-in-parens>
	 * <media-and> = and <media-in-parens>
	 * <media-or> = or <media-in-parens>
	 */

	token = parserutils_vector_peek(vector, *ctx);
	if (token == NULL || tokenIsChar(token, '(') == false ||
			token->type != CSS_TOKEN_IDENT ||
			lwc_string_caseless_isequal(token->idata,
				c->strings[NOT], &match) != lwc_error_ok ||
			match == false) {
		return CSS_INVALID;
	}

	result = malloc(sizeof(*result));
	if (result == NULL) {
		return CSS_NOMEM;
	}
	memset(result, 0, sizeof(*result));
	result->parts = malloc(sizeof(*result->parts));
	if (result->parts == NULL) {
		free(result);
		return CSS_NOMEM;
	}
	memset(result->parts, 0, sizeof(*result->parts));

	if (tokenIsChar(token, '(') == false) {
		/* Must be "not" */
		parserutils_vector_iterate(vector, ctx);
		consumeWhitespace(vector, ctx);

		error = mq_parse_media_in_parens(c, vector, ctx, &cond_or_feature);
		if (error != CSS_OK) {
			css__mq_cond_destroy(result);
			return CSS_INVALID;
		}

		result->negate = 1;
		result->parts->nparts = 1;
		result->parts->parts = malloc(sizeof(*result->parts->parts));
		if (result->parts->parts == NULL) {
			css__mq_cond_or_feature_destroy(cond_or_feature);
			css__mq_cond_destroy(result);
			return CSS_NOMEM;
		}
		result->parts->parts[0] = cond_or_feature;

		*cond = result;

		return CSS_OK;
	}

	/* FOLLOW(media-condition) := RPAREN | COMMA | EOF */
	while (token != NULL && tokenIsChar(token, ')') == false &&
			tokenIsChar(token, ',') == false) {
		error = mq_parse_media_in_parens(c, vector, ctx, &cond_or_feature);
		if (error != CSS_OK) {
			css__mq_cond_destroy(result);
			return CSS_INVALID;
		}

		parts = realloc(result->parts->parts,
				(result->parts->nparts+1)*sizeof(*result->parts->parts));
		if (parts == NULL) {
			css__mq_cond_or_feature_destroy(cond_or_feature);
			css__mq_cond_destroy(result);
			return CSS_NOMEM;
		}
		parts[result->parts->nparts] = cond_or_feature;
		result->parts->parts = parts;
		result->parts->nparts++;

		consumeWhitespace(vector, ctx);

		token = parserutils_vector_peek(vector, *ctx);
		if (token != NULL && tokenIsChar(token, ')') == false &&
				tokenIsChar(token, ',') == false) {
			if (token->type != CSS_TOKEN_IDENT) {
				css__mq_cond_destroy(result);
				return CSS_INVALID;
			} else if (lwc_string_caseless_isequal(token->idata,
					c->strings[AND], &match) == lwc_error_ok &&
					match) {
				if (op != 0 && op != AND) {
					css__mq_cond_destroy(result);
					return CSS_INVALID;
				}
				op = AND;
			} else if (lwc_string_caseless_isequal(token->idata,
						c->strings[OR], &match) == lwc_error_ok &&
					match) {
				if (permit_or == false || (op != 0 && op != OR)) {
					css__mq_cond_destroy(result);
					return CSS_INVALID;
				}
				op = OR;
			} else {
				/* Neither AND nor OR */
				css__mq_cond_destroy(result);
				return CSS_INVALID;
			}

			parserutils_vector_iterate(vector, ctx);
			consumeWhitespace(vector, ctx);
		}
	}

	if (op == OR) {
		result->op = 1;
	}

	*cond = result;

	return CSS_OK;
}

/**
 * Parse a media query type.
 */
static uint64_t mq_parse_type(css_language *c, lwc_string *type)
{
	bool match;

	if (type == NULL) {
		return CSS_MEDIA_ALL;
	} else if (lwc_string_caseless_isequal(
			type, c->strings[AURAL],
			&match) == lwc_error_ok && match) {
		return CSS_MEDIA_AURAL;
	} else if (lwc_string_caseless_isequal(
			type, c->strings[BRAILLE],
			&match) == lwc_error_ok && match) {
		return CSS_MEDIA_BRAILLE;
	} else if (lwc_string_caseless_isequal(
			type, c->strings[EMBOSSED],
			&match) == lwc_error_ok && match) {
		return CSS_MEDIA_EMBOSSED;
	} else if (lwc_string_caseless_isequal(
			type, c->strings[HANDHELD],
			&match) == lwc_error_ok && match) {
		return CSS_MEDIA_HANDHELD;
	} else if (lwc_string_caseless_isequal(
			type, c->strings[PRINT],
			&match) == lwc_error_ok && match) {
		return CSS_MEDIA_PRINT;
	} else if (lwc_string_caseless_isequal(
			type, c->strings[PROJECTION],
			&match) == lwc_error_ok && match) {
		return CSS_MEDIA_PROJECTION;
	} else if (lwc_string_caseless_isequal(
			type, c->strings[SCREEN],
			&match) == lwc_error_ok && match) {
		return CSS_MEDIA_SCREEN;
	} else if (lwc_string_caseless_isequal(
			type, c->strings[SPEECH],
			&match) == lwc_error_ok && match) {
		return CSS_MEDIA_SPEECH;
	} else if (lwc_string_caseless_isequal(
			type, c->strings[TTY],
			&match) == lwc_error_ok && match) {
		return CSS_MEDIA_TTY;
	} else if (lwc_string_caseless_isequal(
			type, c->strings[TV],
			&match) == lwc_error_ok && match) {
		return CSS_MEDIA_TV;
	} else if (lwc_string_caseless_isequal(
			type, c->strings[ALL],
			&match) == lwc_error_ok && match) {
		return CSS_MEDIA_ALL;
	}

	return 0;
}

static css_error mq_parse_media_query(css_language *c,
		const parserutils_vector *vector, int *ctx,
		css_mq_query **query)
{
	const css_token *token;
	bool match, is_condition = false;
	css_mq_query *result;
	css_error error;

	/* <media-query> = <media-condition>
	 *               | [ not | only ]? <media-type> [ and <media-condition-without-or> ]?
	 * <media-type> = <ident> (except "not", "and", "or", "only")
	 */

	// LPAREN -> media-condition
	//    not LPAREN -> media-condition

	consumeWhitespace(vector, ctx);

	token = parserutils_vector_peek(vector, *ctx);
	if (tokenIsChar(token, '(')) {
		is_condition = true;
	} else if (token->type == CSS_TOKEN_IDENT &&
			lwc_string_caseless_isequal(token->idata,
				c->strings[NOT], &match) == lwc_error_ok &&
				match) {
		int old_ctx = *ctx;

		parserutils_vector_iterate(vector, ctx);
		consumeWhitespace(vector, ctx);

		token = parserutils_vector_peek(vector, *ctx);
		if (tokenIsChar(token, '(')) {
			is_condition = true;
		}

		*ctx = old_ctx;
	}

	result = malloc(sizeof(*result));
	if (result == NULL) {
		return CSS_NOMEM;
	}
	memset(result, 0, sizeof(*result));

	if (is_condition) {
		/* media-condition */
		error = mq_parse_condition(c, vector, ctx, true, &result->cond);
		if (error != CSS_OK) {
			free(result);
			return error;
		}

		*query = result;
		return CSS_OK;
	}

	token = parserutils_vector_iterate(vector, ctx);
	if (token == NULL || token->type != CSS_TOKEN_IDENT) {
		free(result);
		return CSS_INVALID;
	}

	if (lwc_string_caseless_isequal(token->idata,
			c->strings[NOT], &match) == lwc_error_ok &&
			match) {
		result->negate_type = 1;
		consumeWhitespace(vector, ctx);
		token = parserutils_vector_iterate(vector, ctx);
	} else if (lwc_string_caseless_isequal(token->idata,
			c->strings[ONLY], &match) == lwc_error_ok &&
			match) {
		consumeWhitespace(vector, ctx);
		token = parserutils_vector_iterate(vector, ctx);
	}

	if (token == NULL || token->type != CSS_TOKEN_IDENT) {
		free(result);
		return CSS_INVALID;
	}

	result->type = mq_parse_type(c, token->idata);

	consumeWhitespace(vector, ctx);

	token = parserutils_vector_iterate(vector, ctx);
	if (token != NULL) {
		if (token->type != CSS_TOKEN_IDENT ||
				lwc_string_caseless_isequal(token->idata,
					c->strings[AND], &match) != lwc_error_ok ||
				match == false) {
			free(result);
			return CSS_INVALID;
		}

		consumeWhitespace(vector, ctx);

		error = mq_parse_condition(c, vector, ctx, false, &result->cond);
		if (error != CSS_OK) {
			free(result);
			return error;
		}
	}

	*query = result;
	return CSS_OK;
}

css_error css__mq_parse_media_list(css_language *c,
		const parserutils_vector *vector, int *ctx,
		css_mq_query **media)
{
	css_mq_query *result = NULL, *last = NULL;
	const css_token *token;
	css_error error;

	/* <media-query-list> = <media-query> [ COMMA <media-query> ]* */

	/* if {[(, push }]) to stack
	 * if func, push ) to stack
	 * on error, scan forward until stack is empty (or EOF), popping matching tokens off stack
	 * if stack is empty, the next input token must be comma or EOF
	 * if comma, consume, and start again from the next input token
	 */

	token = parserutils_vector_peek(vector, *ctx);
	while (token != NULL) {
		css_mq_query *query;

		error = mq_parse_media_query(c, vector, ctx, &query);
		if (error != CSS_OK) {
			/* TODO: error recovery (see above) */
			css__mq_query_destroy(result);
			return error;
		} else {
			if (result == NULL) {
				result = last = query;
			} else {
				assert(last != NULL);
				last->next = query;
				last = query;
			}
		}

		consumeWhitespace(vector, ctx);

		token = parserutils_vector_iterate(vector, ctx);
		if (token != NULL && tokenIsChar(token, ',') == false) {
			/* Give up */
			break;
		}
	}

	*media = result;

	return CSS_OK;
}