summaryrefslogtreecommitdiff
path: root/css/parser.y
blob: 9fc1d398d2cfd437b5172588d5629cc1a846c342 (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
/*
 * This file is part of NetSurf, http://netsurf.sourceforge.net/
 * Licensed under the GNU General Public License,
 *                http://www.opensource.org/licenses/gpl-license
 * Copyright 2003 James Bursa <bursa@users.sourceforge.net>
 */

/*

CSS parser using the lemon parser generator

see CSS2 Specification, chapter 4
http://www.w3.org/TR/REC-CSS2/syndata.html,
and errata
http://www.w3.org/Style/css2-updates/REC-CSS2-19980512-errata

stylesheet  : [ CDO | CDC | S | statement ]*;
statement   : ruleset | at-rule;
at-rule     : ATKEYWORD S* any* [ block | ';' S* ];
block       : '{' S* [ any | block | ATKEYWORD S* | ';' ]* '}' S*;
ruleset     : selector? '{' S* declaration? [ ';' S* declaration? ]* '}' S*;
selector    : any+;
declaration : property ':' S* value;
property    : IDENT S*;
value       : [ any | block | ATKEYWORD S* ]+;
any         : [ IDENT | NUMBER | PERCENTAGE | DIMENSION | STRING
              | DELIM | URI | HASH | UNICODE-RANGE | INCLUDES
              | FUNCTION | DASHMATCH | '(' any* ')' | '[' any* ']' ] S*;

Note: S, CDO, CDC will be stripped out by the scanner
*/

stylesheet ::= statement_list.

statement_list ::= .
statement_list ::= statement_list statement.

statement ::= ruleset.
statement ::= at_rule.

at_rule ::= ATKEYWORD any_list block.
at_rule ::= ATKEYWORD(A) any_list(B) SEMI.
		{ if (strcasecmp(A, "@import") == 0)
			css_atimport(param->stylesheet, B);
		free(A); css_free_node(B); }

block ::= LBRACE block_body RBRACE.
block_body ::= .
block_body ::= block_body any.
block_body ::= block_body block.
block_body ::= block_body ATKEYWORD.
block_body ::= block_body SEMI.

ruleset ::= selector_list(A) LBRACE declaration_list(B) RBRACE.
		{ css_add_ruleset(param->stylesheet, A, B);
		css_free_node(B); }
ruleset ::= LBRACE declaration_list(A) RBRACE.
		/* this form of ruleset not used in CSS2
		   used to parse style attributes (ruleset_only = 1) */
		{ if (param->ruleset_only) param->declaration = A;
		  else css_free_node(A); }
ruleset ::= any_list_1(A) LBRACE declaration_list(B) RBRACE.
		{ css_free_node(A); css_free_node(B); } /* not CSS2 */

selector_list(A) ::= selector(B).
		{ A = B; }
selector_list(A) ::= selector_list(B) COMMA selector(C).
		{ C->next = B; A = C; }

selector(A) ::= simple_selector(B).
		{ A = B; }
selector(A) ::= selector(B) css_combinator(C) simple_selector(D).
		{ D->right = B; D->comb = C; A = D;
		A->specificity += B->specificity; }

css_combinator(A) ::= .
		{ A = CSS_COMB_ANCESTOR; }
css_combinator(A) ::= PLUS.
		{ A = CSS_COMB_PRECEDED; }
css_combinator(A) ::= GT.
		{ A = CSS_COMB_PARENT; }

simple_selector(A) ::= element_name(B) detail_list(C).
		{ A = css_new_node(CSS_NODE_SELECTOR, B, C, 0);
		A->specificity = (B ? 1 : 0) + (C ? C->specificity : 0); }

element_name(A) ::= . [EMPTYIDENT]
		{ A = 0; }
element_name(A) ::= IDENT(B).
		{ A = B; }

detail_list(A) ::= .
		{ A = 0; }
detail_list(A) ::= HASH(B) detail_list(C).
		{ A = css_new_node(CSS_NODE_ID, B, 0, 0);
		A->specificity = 0x10000 + (C ? C->specificity : 0); A->next = C; }
detail_list(A) ::= DOT IDENT(B) detail_list(C).
		{ A = css_new_node(CSS_NODE_CLASS, B, 0, 0);
		A->specificity = 0x100 + (C ? C->specificity : 0); A->next = C; }
detail_list(A) ::= LBRAC IDENT(B) RBRAC detail_list(C).
		{ A = css_new_node(CSS_NODE_ATTRIB, B, 0, 0);
		A->specificity = 0x100 + (C ? C->specificity : 0); A->next = C; }
detail_list(A) ::= LBRAC IDENT(B) EQUALS IDENT(C) RBRAC detail_list(D).
		{ A = css_new_node(CSS_NODE_ATTRIB_EQ, B, 0, 0); A->data2 = C;
		A->specificity = 0x100 + (D ? D->specificity : 0); A->next = D; }
detail_list(A) ::= LBRAC IDENT(B) EQUALS STRING(C) RBRAC detail_list(D).
		{ A = css_new_node(CSS_NODE_ATTRIB_EQ, B, 0, 0); A->data2 = css_unquote(C);
		A->specificity = 0x100 + (D ? D->specificity : 0); A->next = D; }
detail_list(A) ::= LBRAC IDENT(B) INCLUDES IDENT(C) RBRAC detail_list(D).
		{ A = css_new_node(CSS_NODE_ATTRIB_INC, B, 0, 0); A->data2 = C;
		A->specificity = 0x100 + (D ? D->specificity : 0); A->next = D; }
detail_list(A) ::= LBRAC IDENT(B) INCLUDES STRING(C) RBRAC detail_list(D).
		{ A = css_new_node(CSS_NODE_ATTRIB_INC, B, 0, 0); A->data2 = css_unquote(C);
		A->specificity = 0x100 + (D ? D->specificity : 0); A->next = D; }
detail_list(A) ::= LBRAC IDENT(B) DASHMATCH IDENT(C) RBRAC detail_list(D).
		{ A = css_new_node(CSS_NODE_ATTRIB_DM, B, 0, 0); A->data2 = C;
		A->specificity = 0x100 + (D ? D->specificity : 0); A->next = D; }
detail_list(A) ::= LBRAC IDENT(B) DASHMATCH STRING(C) RBRAC detail_list(D).
		{ A = css_new_node(CSS_NODE_ATTRIB_DM, B, 0, 0); A->data2 = css_unquote(C);
		A->specificity = 0x100 + (D ? D->specificity : 0); A->next = D; }
detail_list(A) ::= COLON IDENT(B) detail_list(C).
		{ if (strcasecmp(B, "link") == 0) {
			A = css_new_node(CSS_NODE_ATTRIB, xstrdup("href"), 0, 0);
			A->specificity = 0x100 + (C ? C->specificity : 0); A->next = C;
			free(B);
		} else {
			A = css_new_node(CSS_NODE_PSEUDO, B, 0, 0);
			A->specificity = 0x100 + (C ? C->specificity : 0); A->next = C;
		} }
detail_list(A) ::= COLON FUNCTION(B) IDENT RPAREN detail_list(C).
		{ A = css_new_node(CSS_NODE_PSEUDO, B, 0, 0);
		A->specificity = 0x100 + (C ? C->specificity : 0); A->next = C; }

declaration_list(A) ::= .
		{ A = 0; }
declaration_list(A) ::= declaration(B).
		{ A = B; }
declaration_list(A) ::= declaration_list(B) SEMI.
		{ A = B; }
declaration_list(A) ::= declaration(B) SEMI declaration_list(C).
		{ B->next = C; A = B; }

declaration(A) ::= property(B) COLON value(C).
		{ A = css_new_node(CSS_NODE_DECLARATION, B, C, 0); }

property(A) ::= IDENT(B).
		{ A = B; }

value(A) ::= any(B).
		{ A = B; }
value(A) ::= any(B) value(C).
		{ B->next = C; A = B; }
value(A) ::= value(B) block.
		{ A = B; }
value(A) ::= value(B) ATKEYWORD.
		{ A = B; }

any_list(A) ::= .
		{ A = 0; }
any_list(A) ::= any(B) any_list(C).
		{ B->next = C; A = B; }
any_list_1(A) ::= any(B) any_list(C).
		{ B->next = C; A = B; }
any(A) ::= IDENT(B).
		{ A = css_new_node(CSS_NODE_IDENT, B, 0, 0); }
any(A) ::= NUMBER(B).
		{ A = css_new_node(CSS_NODE_NUMBER, B, 0, 0); }
any(A) ::= PERCENTAGE(B).
		{ A = css_new_node(CSS_NODE_PERCENTAGE, B, 0, 0); }
any(A) ::= DIMENSION(B).
		{ A = css_new_node(CSS_NODE_DIMENSION, B, 0, 0); }
any(A) ::= STRING(B).
		{ A = css_new_node(CSS_NODE_STRING, css_unquote(B), 0, 0); }
any(A) ::= DELIM(B).
		{ A = css_new_node(CSS_NODE_DELIM, B, 0, 0); }
any(A) ::= URI(B).
		{ A = css_new_node(CSS_NODE_URI, B, 0, 0); }
any(A) ::= HASH(B).
		{ A = css_new_node(CSS_NODE_HASH, B, 0, 0); }
any(A) ::= UNICODE_RANGE(B).
		{ A = css_new_node(CSS_NODE_UNICODE_RANGE, B, 0, 0); }
any(A) ::= INCLUDES.
		{ A = css_new_node(CSS_NODE_INCLUDES, 0, 0, 0); }
any(A) ::= FUNCTION(B).
		{ A = css_new_node(CSS_NODE_FUNCTION, B, 0, 0); }
any(A) ::= DASHMATCH.
		{ A = css_new_node(CSS_NODE_DASHMATCH, 0, 0, 0); }
any(A) ::= COLON.
		{ A = css_new_node(CSS_NODE_COLON, 0, 0, 0); }
any(A) ::= COMMA.
		{ A = css_new_node(CSS_NODE_COMMA, 0, 0, 0); }
any(A) ::= DOT.
		{ A = css_new_node(CSS_NODE_DOT, 0, 0, 0); }
any(A) ::= PLUS.
		{ A = css_new_node(CSS_NODE_PLUS, 0, 0, 0); }
any(A) ::= GT.
		{ A = css_new_node(CSS_NODE_GT, 0, 0, 0); }
any(A) ::= LPAREN any_list(B) RPAREN.
		{ A = css_new_node(CSS_NODE_PAREN, 0, B, 0); }
any(A) ::= LBRAC any_list(B) RBRAC.
		{ A = css_new_node(CSS_NODE_BRAC, 0, B, 0); }


/* lemon directives */

%extra_argument { struct parse_params *param }
%include {
#define CSS_INTERNALS
#include "netsurf/css/scanner.h"
#include "netsurf/css/css.h"
#include "netsurf/utils/utils.h" }
%name css_parser_

%token_type { char* }
%token_destructor { xfree($$); }

%type selector_list { struct css_node * }
%type selector { struct css_node * }
%type css_combinator { css_combinator }
%type simple_selector { struct css_node * }
%type detail_list { struct css_node * }
%type declaration_list { struct css_node * }
%type declaration { struct css_node * }
%type value { struct css_node * }
%type any_list { struct css_node * }
%type any_list_1 { struct css_node * }
%type any { struct css_node * }

%destructor selector_list { css_free_node($$); }
%destructor selector { css_free_node($$); }
%destructor simple_selector { css_free_node($$); }
%destructor detail_list { css_free_node($$); }
%destructor declaration_list { css_free_node($$); }
%destructor declaration { css_free_node($$); }
%destructor value { css_free_node($$); }
%destructor any_list { css_free_node($$); }
%destructor any_list_1 { css_free_node($$); }
%destructor any { css_free_node($$); }

%left COLON COMMA GT HASH LBRAC PLUS.
%left DOT.
%left EMPTYIDENT.
%left IDENT.
%left LBRACE.

%syntax_error { param->syntax_error = true; }