-
Notifications
You must be signed in to change notification settings - Fork 708
/
Copy pathgrammar.html
364 lines (319 loc) · 11.9 KB
/
grammar.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<HEAD>
<TITLE>Grammar of CSS 2.2</TITLE>
<link rel="stylesheet" href="style/default.css" type="text/css">
<link rel="stylesheet" href="https://www.w3.org/StyleSheets/TR/2016/W3C-ED.css" type="text/css">
<link rel="prev" href="propidx.html">
<link rel="next" href="leftblank.html">
<link rel="contents" href="Overview.html#minitoc">
<link rel="CSS-properties" href="propidx.html" title="properties">
<link rel="index" href="indexlist.html" title="index">
<link rel="first" href="Overview.html">
<script type="text/javascript" src="https://www.w3.org/scripts/TR/2016/fixup.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</HEAD>
<BODY>
<div class="navbar">
<p><a href="propidx.html">previous</a>
<a href="leftblank.html">next</a>
<a href="Overview.html#minitoc">contents</a>
<a href="propidx.html">properties</a>
<a href="indexlist.html">index</a>
</div>
<hr class="navbar">
<H1><a name="q0">Appendix G. Grammar of CSS 2.2</a></H1>
<div id="toc" class="subtoc">
<p><strong>Contents</strong>
<ul class="toc">
<li><a href="grammar.html#grammar" class="tocxref">G.1 Grammar</a>
<li><a href="grammar.html#scanner" class="tocxref">G.2 Lexical scanner</a>
<li><a href="grammar.html#tokenizer-diffs" class="tocxref">G.3 Comparison of tokenization in CSS 2.2 and CSS1</a>
<li><a href="grammar.html#q4" class="tocxref">G.4 Implementation note</a>
</ul>
</div>
<p>This appendix is non-normative.
<P>The grammar below defines the syntax of CSS 2.2. It is in some sense,
however, a superset of CSS 2.2 as this specification imposes additional
semantic constraints not expressed in this grammar. A conforming UA
must also adhere to the <a href="./syndata.html#syntax">
forward-compatible parsing rules</a>, the selectors notation, the <a
href="./about.html#property-defs">property and value notation</a>,
and the unit notation. However, not all syntactically correct CSS can take
effect, since the document language may impose restrictions that are
not in CSS, e.g., HTML imposes restrictions on the possible values of
the "class" attribute.
<h2>G.1 <a name="grammar">Grammar</a></h2>
<P> The grammar below is <a name="x0"><span class="index-inst"
title="LALR(1)">LALR(1)</span></a> (but note that most UA's should not use it
directly, since it does not express the <a
href="syndata.html#parsing-errors">parsing conventions</a>, only the
CSS 2.2 syntax). The format of the productions is optimized for human
consumption and some shorthand notation beyond Yacc (see <a href="refs.html#ref-YACC" rel="biblioentry" class="noxref"><span class="normref">[YACC]</span></a>) is
used:</P>
<ul>
<li><strong>*</strong>: 0 or more
<li><strong>+</strong>: 1 or more
<li><strong>?</strong>: 0 or 1
<li><strong>|</strong>: separates alternatives
<li><strong>[ ]</strong>: grouping
</ul>
<p>The productions are:
<pre>
stylesheet
: [ CHARSET_SYM STRING ';' ]?
[S|CDO|CDC]* [ import [ CDO S* | CDC S* ]* ]*
[ [ ruleset | media | page ] [ CDO S* | CDC S* ]* ]*
;
import
: IMPORT_SYM S*
[STRING|URI] S* media_list? ';' S*
;
media
: MEDIA_SYM S* media_list '{' S* ruleset* '}' S*
;
media_list
: medium [ COMMA S* medium]*
;
medium
: IDENT S*
;
page
: PAGE_SYM S* pseudo_page?
'{' S* declaration? [ ';' S* declaration? ]* '}' S*
;
pseudo_page
: ':' IDENT S*
;
operator
: '/' S* | ',' S*
;
combinator
: '+' S*
| '>' S*
;
property
: IDENT S*
;
ruleset
: selector [ ',' S* selector ]*
'{' S* declaration? [ ';' S* declaration? ]* '}' S*
;
<a name="x1"><span class="index-inst" title="selector">selector</span></a>
: simple_selector [ combinator selector | S+ [ combinator? selector ]? ]?
;
simple_selector
: element_name [ HASH | class | attrib | pseudo ]*
| [ HASH | class | attrib | pseudo ]+
;
class
: '.' IDENT
;
element_name
: IDENT | '*'
;
attrib
: '[' S* IDENT S* [ [ '=' | INCLUDES | DASHMATCH ] S*
[ IDENT | STRING ] S* ]? ']'
;
pseudo
: ':' [ IDENT | FUNCTION S* [IDENT S*]? ')' ]
;
declaration
: property ':' S* expr prio?
;
prio
: IMPORTANT_SYM S*
;
expr
: term [ operator? term ]*
;
term
: [ NUMBER S* | PERCENTAGE S* | LENGTH S* | EMS S* | EXS S* | ANGLE S* |
TIME S* | FREQ S* ]
| STRING S* | IDENT S* | URI S* | hexcolor | function
;
function
: FUNCTION S* expr ')' S*
;
/*
* There is a constraint on the <a name="x2"><span class="index-inst" title="color">color</span></a> that it must
* have either 3 or 6 hex-digits (i.e., [0-9a-fA-F])
* after the "#"; e.g., "#000" is OK, but "#abcd" is not.
*/
hexcolor
: HASH S*
;
</pre>
<h2>G.2 <a name="scanner">Lexical scanner</a></h2>
<p> The following is the <a name="x3"><span class="index-def"
title="tokenizer">tokenizer</span></a>, written in Flex (see <a href="refs.html#ref-FLEX" rel="biblioentry" class="noxref"><span class="normref">[FLEX]</span></a>)
notation. The tokenizer is case-insensitive.
<p>The "\377" represents the highest character
number that current versions of Flex can deal with (decimal 255). It
should be read as "\4177777" (decimal 1114111), which is the highest
possible code point in <a name="x4"><span class="index-inst"
title="unicode">Unicode</span></a>/<a name="x5"><span class="index-inst"
title="iso-10646">ISO-10646</span></a>.
<pre>
%option case-insensitive
h [0-9a-f]
nonascii [\240-\377]
unicode \\{h}{1,6}(\r\n|[ \t\r\n\f])?
escape {unicode}|\\[^\r\n\f0-9a-f]
nmstart [_a-z]|{nonascii}|{escape}
nmchar [_a-z0-9-]|{nonascii}|{escape}
string1 \"([^\n\r\f\\"]|\\{nl}|{escape})*\"
string2 \'([^\n\r\f\\']|\\{nl}|{escape})*\'
badstring1 \"([^\n\r\f\\"]|\\{nl}|{escape})*\\?
badstring2 \'([^\n\r\f\\']|\\{nl}|{escape})*\\?
badcomment1 \/\*[^*]*\*+([^/*][^*]*\*+)*
badcomment2 \/\*[^*]*(\*+[^/*][^*]*)*
baduri1 {U}{R}{L}\({w}([!#$%&*-\[\]-~]|{nonascii}|{escape})*{w}
baduri2 {U}{R}{L}\({w}{string}{w}
baduri3 {U}{R}{L}\({w}{badstring}
comment \/\*[^*]*\*+([^/*][^*]*\*+)*\/
ident -?{nmstart}{nmchar}*
name {nmchar}+
num [-+]?[0-9]+|[-+]?[0-9]*"."[0-9]+
string {string1}|{string2}
badstring {badstring1}|{badstring2}
badcomment {badcomment1}|{badcomment2}
baduri {baduri1}|{baduri2}|{baduri3}
url ([!#$%&*-~]|{nonascii}|{escape})*
s [ \t\r\n\f]+
w {s}?
nl \n|\r\n|\r|\f
A a|\\0{0,4}(41|61)(\r\n|[ \t\r\n\f])?
C c|\\0{0,4}(43|63)(\r\n|[ \t\r\n\f])?
D d|\\0{0,4}(44|64)(\r\n|[ \t\r\n\f])?
E e|\\0{0,4}(45|65)(\r\n|[ \t\r\n\f])?
G g|\\0{0,4}(47|67)(\r\n|[ \t\r\n\f])?|\\g
H h|\\0{0,4}(48|68)(\r\n|[ \t\r\n\f])?|\\h
I i|\\0{0,4}(49|69)(\r\n|[ \t\r\n\f])?|\\i
K k|\\0{0,4}(4b|6b)(\r\n|[ \t\r\n\f])?|\\k
L l|\\0{0,4}(4c|6c)(\r\n|[ \t\r\n\f])?|\\l
M m|\\0{0,4}(4d|6d)(\r\n|[ \t\r\n\f])?|\\m
N n|\\0{0,4}(4e|6e)(\r\n|[ \t\r\n\f])?|\\n
O o|\\0{0,4}(4f|6f)(\r\n|[ \t\r\n\f])?|\\o
P p|\\0{0,4}(50|70)(\r\n|[ \t\r\n\f])?|\\p
R r|\\0{0,4}(52|72)(\r\n|[ \t\r\n\f])?|\\r
S s|\\0{0,4}(53|73)(\r\n|[ \t\r\n\f])?|\\s
T t|\\0{0,4}(54|74)(\r\n|[ \t\r\n\f])?|\\t
U u|\\0{0,4}(55|75)(\r\n|[ \t\r\n\f])?|\\u
X x|\\0{0,4}(58|78)(\r\n|[ \t\r\n\f])?|\\x
Z z|\\0{0,4}(5a|7a)(\r\n|[ \t\r\n\f])?|\\z
%%
{s} {return S;}
\/\*[^*]*\*+([^/*][^*]*\*+)*\/ /* ignore comments */
{badcomment} /* unclosed comment at EOF */
"<!--" {return CDO;}
"-->" {return CDC;}
"~=" {return INCLUDES;}
"|=" {return DASHMATCH;}
{string} {return STRING;}
{badstring} {return BAD_STRING;}
{ident} {return IDENT;}
"#"{name} {return HASH;}
@{I}{M}{P}{O}{R}{T} {return IMPORT_SYM;}
@{P}{A}{G}{E} {return PAGE_SYM;}
@{M}{E}{D}{I}{A} {return MEDIA_SYM;}
"@charset " {return CHARSET_SYM;}
"!"({w}|{comment})*{I}{M}{P}{O}{R}{T}{A}{N}{T} {return IMPORTANT_SYM;}
{num}{E}{M} {return EMS;}
{num}{E}{X} {return EXS;}
{num}{P}{X} {return LENGTH;}
{num}{C}{M} {return LENGTH;}
{num}{M}{M} {return LENGTH;}
{num}{I}{N} {return LENGTH;}
{num}{P}{T} {return LENGTH;}
{num}{P}{C} {return LENGTH;}
{num}{D}{E}{G} {return ANGLE;}
{num}{R}{A}{D} {return ANGLE;}
{num}{G}{R}{A}{D} {return ANGLE;}
{num}{M}{S} {return TIME;}
{num}{S} {return TIME;}
{num}{H}{Z} {return FREQ;}
{num}{K}{H}{Z} {return FREQ;}
{num}{ident} {return DIMENSION;}
{num}% {return PERCENTAGE;}
{num} {return NUMBER;}
{U}{R}{L}"("{w}{string}{w}")" {return URI;}
{U}{R}{L}"("{w}{url}{w}")" {return URI;}
{baduri} {return BAD_URI;}
{ident}"(" {return FUNCTION;}
. {return *yytext;}
</pre>
<h2>G.3 <a name="tokenizer-diffs">Comparison of tokenization in CSS 2.2 and
CSS1</a></h2>
<p>There are some differences in the syntax specified in the CSS1
recommendation (<a href="refs.html#ref-CSS1" rel="biblioentry" class="noxref"><span class="informref">[CSS1]</span></a>), and the one above. Most of these are due
to new tokens in CSS2 that did not exist in CSS1. Others are because
the grammar has been rewritten to be more readable. However, there are
some incompatible changes, that were felt to be errors in the CSS1
syntax. They are explained below.
<ul>
<li>CSS1 style sheets could only be in 1-byte-per-character
encodings, such as ASCII and ISO-8859-1. CSS 2.2 has no such
limitation. In practice, there was little difficulty in extrapolating
the CSS1 tokenizer, and some UAs have accepted 2-byte encodings.
<li>CSS1 only allowed four hex-digits after the backslash (\) to refer
to Unicode characters, CSS2 <a
href="syndata.html#escaped-characters">allows six</a>. Furthermore,
CSS2 allows a white space character to delimit the escape
sequence. E.g., according to CSS1, the string "\abcdef" has 3 letters
(\abcd, e, and f), according to CSS2 it has only one (\abcdef).
<li>The tab character (ASCII 9) was not allowed in strings. However,
since strings in CSS1 were only used for font names and for URLs, the
only way this can lead to incompatibility between CSS1 and CSS2 is if
a style sheet contains a font family that has a tab in its name.
<li>Similarly, newlines (<a href="syndata.html#strings">escaped with a
backslash</a>) were not allowed in strings in CSS1.
<li>CSS2 parses a number immediately followed by an identifier as a
DIMENSION token (i.e., an unknown unit), CSS1 parsed it as a number and an
identifier. That means that in CSS1, the declaration 'font:
10pt/1.2serif' was correct, as was 'font: 10pt/12pt serif'; in CSS2, a
space is required before "serif". (Some UAs accepted the first
example, but not the second.)
<li>In CSS1, a class name could start with a digit (".55ft"), unless
it was a dimension (".55in"). In CSS2, such classes are parsed as
unknown dimensions (to allow for future additions of new units). To
make ".55ft" a valid class, CSS2 requires the first digit to be
escaped (".\35 5ft")
</ul>
<h2><a name="q4">G.4 Implementation note</a></h2>
<p>The lexical scanner for the CSS core syntax in <a
href="syndata.html#tokenization">section 4.1.1</a> can be
implemented as a scanner without back-up. In Lex notation, that
requires the addition of the following patterns (which do not change
the returned tokens, only the efficiency of the scanner):
<pre>
{ident}/\\ return IDENT;
#{name}/\\ return HASH;
@{ident}/\\ return ATKEYWORD;
#/\\ return DELIM;
@/\\ return DELIM;
@/- return DELIM;
@/-\\ return DELIM;
-/\\ return DELIM;
-/- return DELIM;
\</! return DELIM;
\</!- return DELIM;
{num}{ident}/\\ return DIMENSION;
{num}/\\ return NUMBER;
{num}/- return NUMBER;
{num}/-\\ return NUMBER;
[0-9]+/\. return NUMBER;
u/\+ return IDENT;
u\+[0-9a-f?]{1,6}/- return UNICODE_RANGE;
</pre>
<hr class="navbar">
<div class="navbar">
<p><a href="propidx.html">previous</a>
<a href="leftblank.html">next</a>
<a href="Overview.html#minitoc">contents</a>
<a href="propidx.html">properties</a>
<a href="indexlist.html">index</a>
</div>
</body>
</html>