| 1 |
/* |
| 2 |
* CSS Grammar |
| 3 |
* =========== |
| 4 |
* |
| 5 |
* Based on grammar from CSS 2.1 specification [1] (including the errata [2]). |
| 6 |
* Generated parser builds a syntax tree composed of nested JavaScript objects, |
| 7 |
* vaguely inspired by CSS DOM [3]. The CSS DOM itself wasn't used as it is not |
| 8 |
* expressive enough (e.g. selectors are reflected as text, not structured |
| 9 |
* objects) and somewhat cumbersome. |
| 10 |
* |
| 11 |
* Limitations: |
| 12 |
* |
| 13 |
* * Many errors which should be recovered from according to the specification |
| 14 |
* (e.g. malformed declarations or unexpected end of stylesheet) are fatal. |
| 15 |
* This is a result of straightforward rewrite of the CSS grammar to PEG.js. |
| 16 |
* |
| 17 |
* [1] http://www.w3.org/TR/2011/REC-CSS2-20110607 |
| 18 |
* [2] http://www.w3.org/Style/css2-updates/REC-CSS2-20110607-errata.html |
| 19 |
* [3] http://www.w3.org/TR/DOM-Level-2-Style/css.html |
| 20 |
*/ |
| 21 |
|
| 22 |
{ |
| 23 |
function extractOptional(optional, index) { |
| 24 |
return optional ? optional[index] : null; |
| 25 |
} |
| 26 |
|
| 27 |
function extractList(list, index) { |
| 28 |
var result = [], i; |
| 29 |
|
| 30 |
for (i = 0; i < list.length; i++) { |
| 31 |
if (list[i][index] !== null) { |
| 32 |
result.push(list[i][index]); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
return result; |
| 37 |
} |
| 38 |
|
| 39 |
function buildList(head, tail, index) { |
| 40 |
return (head !== null ? [head] : []).concat(extractList(tail, index)); |
| 41 |
} |
| 42 |
|
| 43 |
function buildExpression(head, tail) { |
| 44 |
var result = head, i; |
| 45 |
|
| 46 |
for (i = 0; i < tail.length; i++) { |
| 47 |
result = { |
| 48 |
type: "Expression", |
| 49 |
operator: tail[i][0], |
| 50 |
left: result, |
| 51 |
right: tail[i][1] |
| 52 |
}; |
| 53 |
} |
| 54 |
|
| 55 |
return result; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
start |
| 60 |
= stylesheet:stylesheet comment* { return stylesheet; } |
| 61 |
|
| 62 |
/* ----- G.1 Grammar ----- */ |
| 63 |
|
| 64 |
stylesheet |
| 65 |
= charset:(CHARSET_SYM STRING ";")? (S / CDO / CDC)* |
| 66 |
imports:(import (CDO S* / CDC S*)*)* |
| 67 |
rules:((ruleset / media / page) (CDO S* / CDC S*)*)* |
| 68 |
{ |
| 69 |
return { |
| 70 |
type: "StyleSheet", |
| 71 |
charset: extractOptional(charset, 1), |
| 72 |
imports: extractList(imports, 0), |
| 73 |
rules: extractList(rules, 0) |
| 74 |
}; |
| 75 |
} |
| 76 |
|
| 77 |
import |
| 78 |
= IMPORT_SYM S* href:(STRING / URI) S* media:media_list? ";" S* { |
| 79 |
return { |
| 80 |
type: "ImportRule", |
| 81 |
href: href, |
| 82 |
media: media !== null ? media : [] |
| 83 |
}; |
| 84 |
} |
| 85 |
|
| 86 |
media |
| 87 |
= MEDIA_SYM S* media:media_list "{" S* rules:ruleset* "}" S* { |
| 88 |
return { |
| 89 |
type: "MediaRule", |
| 90 |
media: media, |
| 91 |
rules: rules |
| 92 |
}; |
| 93 |
} |
| 94 |
|
| 95 |
media_list |
| 96 |
= head:medium tail:("," S* medium)* { return buildList(head, tail, 2); } |
| 97 |
|
| 98 |
medium |
| 99 |
= name:IDENT S* { return name; } |
| 100 |
|
| 101 |
page |
| 102 |
= PAGE_SYM S* selector:pseudo_page? |
| 103 |
"{" S* |
| 104 |
declarationsFirst:declaration? |
| 105 |
declarationsRest:(";" S* declaration?)* |
| 106 |
"}" S* |
| 107 |
{ |
| 108 |
return { |
| 109 |
type: "PageRule", |
| 110 |
selector: selector, |
| 111 |
declarations: buildList(declarationsFirst, declarationsRest, 2) |
| 112 |
}; |
| 113 |
} |
| 114 |
|
| 115 |
pseudo_page |
| 116 |
= ":" value:IDENT S* { return { type: "PseudoSelector", value: value }; } |
| 117 |
|
| 118 |
operator |
| 119 |
= "/" S* { return "/"; } |
| 120 |
/ "," S* { return ","; } |
| 121 |
|
| 122 |
combinator |
| 123 |
= "+" S* { return "+"; } |
| 124 |
/ ">" S* { return ">"; } |
| 125 |
|
| 126 |
property |
| 127 |
= name:IDENT S* { return name; } |
| 128 |
|
| 129 |
ruleset |
| 130 |
= selectorsFirst:selector |
| 131 |
selectorsRest:("," S* selector)* |
| 132 |
"{" S* |
| 133 |
declarationsFirst:declaration? |
| 134 |
declarationsRest:(";" S* declaration?)* |
| 135 |
"}" S* |
| 136 |
{ |
| 137 |
return { |
| 138 |
type: "RuleSet", |
| 139 |
selectors: buildList(selectorsFirst, selectorsRest, 2), |
| 140 |
declarations: buildList(declarationsFirst, declarationsRest, 2) |
| 141 |
}; |
| 142 |
} |
| 143 |
|
| 144 |
selector |
| 145 |
= left:simple_selector S* combinator:combinator right:selector { |
| 146 |
return { |
| 147 |
type: "Selector", |
| 148 |
combinator: combinator, |
| 149 |
left: left, |
| 150 |
right: right |
| 151 |
}; |
| 152 |
} |
| 153 |
/ left:simple_selector S+ right:selector { |
| 154 |
return { |
| 155 |
type: "Selector", |
| 156 |
combinator: " ", |
| 157 |
left: left, |
| 158 |
right: right |
| 159 |
}; |
| 160 |
} |
| 161 |
/ selector:simple_selector S* { return selector; } |
| 162 |
|
| 163 |
simple_selector |
| 164 |
= element:element_name qualifiers:(id / class / attrib / pseudo)* { |
| 165 |
return { |
| 166 |
type: "SimpleSelector", |
| 167 |
element: element, |
| 168 |
qualifiers: qualifiers |
| 169 |
}; |
| 170 |
} |
| 171 |
/ qualifiers:(id / class / attrib / pseudo)+ { |
| 172 |
return { |
| 173 |
type: "SimpleSelector", |
| 174 |
element: "*", |
| 175 |
qualifiers: qualifiers |
| 176 |
}; |
| 177 |
} |
| 178 |
|
| 179 |
id |
| 180 |
= id:HASH { return { type: "IDSelector", id: id }; } |
| 181 |
|
| 182 |
class |
| 183 |
= "." class_:IDENT { return { type: "ClassSelector", "class": class_ }; } |
| 184 |
|
| 185 |
element_name |
| 186 |
= IDENT |
| 187 |
/ "*" |
| 188 |
|
| 189 |
attrib |
| 190 |
= "[" S* |
| 191 |
attribute:IDENT S* |
| 192 |
operatorAndValue:(("=" / INCLUDES / DASHMATCH) S* (IDENT / STRING) S*)? |
| 193 |
"]" |
| 194 |
{ |
| 195 |
return { |
| 196 |
type: "AttributeSelector", |
| 197 |
attribute: attribute, |
| 198 |
operator: extractOptional(operatorAndValue, 0), |
| 199 |
value: extractOptional(operatorAndValue, 2) |
| 200 |
}; |
| 201 |
} |
| 202 |
|
| 203 |
pseudo |
| 204 |
= ":" |
| 205 |
value:( |
| 206 |
name:FUNCTION S* params:(IDENT S*)? ")" { |
| 207 |
return { |
| 208 |
type: "Function", |
| 209 |
name: name, |
| 210 |
params: params !== null ? [params[0]] : [] |
| 211 |
}; |
| 212 |
} |
| 213 |
/ IDENT |
| 214 |
) |
| 215 |
{ return { type: "PseudoSelector", value: value }; } |
| 216 |
|
| 217 |
declaration |
| 218 |
= name:property ':' S* value:expr prio:prio? { |
| 219 |
return { |
| 220 |
type: "Declaration", |
| 221 |
name: name, |
| 222 |
value: value, |
| 223 |
important: prio !== null |
| 224 |
}; |
| 225 |
} |
| 226 |
|
| 227 |
prio |
| 228 |
= IMPORTANT_SYM S* |
| 229 |
|
| 230 |
expr |
| 231 |
= head:term tail:(operator? term)* { return buildExpression(head, tail); } |
| 232 |
|
| 233 |
term |
| 234 |
= quantity:(PERCENTAGE / LENGTH / EMS / EXS / ANGLE / TIME / FREQ / NUMBER) |
| 235 |
S* |
| 236 |
{ |
| 237 |
return { |
| 238 |
type: "Quantity", |
| 239 |
value: quantity.value, |
| 240 |
unit: quantity.unit |
| 241 |
}; |
| 242 |
} |
| 243 |
/ value:STRING S* { return { type: "String", value: value }; } |
| 244 |
/ value:URI S* { return { type: "URI", value: value }; } |
| 245 |
/ function |
| 246 |
/ hexcolor |
| 247 |
/ value:IDENT S* { return { type: "Ident", value: value }; } |
| 248 |
|
| 249 |
function |
| 250 |
= name:FUNCTION S* params:expr ")" S* { |
| 251 |
return { type: "Function", name: name, params: params }; |
| 252 |
} |
| 253 |
|
| 254 |
hexcolor |
| 255 |
= value:HASH S* { return { type: "Hexcolor", value: value }; } |
| 256 |
|
| 257 |
/* ----- G.2 Lexical scanner ----- */ |
| 258 |
|
| 259 |
/* Macros */ |
| 260 |
|
| 261 |
h |
| 262 |
= [0-9a-f]i |
| 263 |
|
| 264 |
nonascii |
| 265 |
= [\x80-\uFFFF] |
| 266 |
|
| 267 |
unicode |
| 268 |
= "\\" digits:$(h h? h? h? h? h?) ("\r\n" / [ \t\r\n\f])? { |
| 269 |
return String.fromCharCode(parseInt(digits, 16)); |
| 270 |
} |
| 271 |
|
| 272 |
escape |
| 273 |
= unicode |
| 274 |
/ "\\" ch:[^\r\n\f0-9a-f]i { return ch; } |
| 275 |
|
| 276 |
nmstart |
| 277 |
= [_a-z]i |
| 278 |
/ nonascii |
| 279 |
/ escape |
| 280 |
|
| 281 |
nmchar |
| 282 |
= [_a-z0-9-]i |
| 283 |
/ nonascii |
| 284 |
/ escape |
| 285 |
|
| 286 |
string1 |
| 287 |
= '"' chars:([^\n\r\f\\"] / "\\" nl:nl { return ""; } / escape)* '"' { |
| 288 |
return chars.join(""); |
| 289 |
} |
| 290 |
|
| 291 |
string2 |
| 292 |
= "'" chars:([^\n\r\f\\'] / "\\" nl:nl { return ""; } / escape)* "'" { |
| 293 |
return chars.join(""); |
| 294 |
} |
| 295 |
|
| 296 |
comment |
| 297 |
= "/*" [^*]* "*"+ ([^/*] [^*]* "*"+)* "/" |
| 298 |
|
| 299 |
ident |
| 300 |
= prefix:$"-"? start:nmstart chars:nmchar* { |
| 301 |
return prefix + start + chars.join(""); |
| 302 |
} |
| 303 |
|
| 304 |
name |
| 305 |
= chars:nmchar+ { return chars.join(""); } |
| 306 |
|
| 307 |
num |
| 308 |
= [+-]? ([0-9]+ / [0-9]* "." [0-9]+) ("e" [+-]? [0-9]+)? { |
| 309 |
return parseFloat(text()); |
| 310 |
} |
| 311 |
|
| 312 |
string |
| 313 |
= string1 |
| 314 |
/ string2 |
| 315 |
|
| 316 |
url |
| 317 |
= chars:([!#$%&*-\[\]-~] / nonascii / escape)* { return chars.join(""); } |
| 318 |
|
| 319 |
s |
| 320 |
= [ \t\r\n\f]+ |
| 321 |
|
| 322 |
w |
| 323 |
= s? |
| 324 |
|
| 325 |
nl |
| 326 |
= "\n" |
| 327 |
/ "\r\n" |
| 328 |
/ "\r" |
| 329 |
/ "\f" |
| 330 |
|
| 331 |
A = "a"i / "\\" "0"? "0"? "0"? "0"? [\x41\x61] ("\r\n" / [ \t\r\n\f])? { return "a"; } |
| 332 |
C = "c"i / "\\" "0"? "0"? "0"? "0"? [\x43\x63] ("\r\n" / [ \t\r\n\f])? { return "c"; } |
| 333 |
D = "d"i / "\\" "0"? "0"? "0"? "0"? [\x44\x64] ("\r\n" / [ \t\r\n\f])? { return "d"; } |
| 334 |
E = "e"i / "\\" "0"? "0"? "0"? "0"? [\x45\x65] ("\r\n" / [ \t\r\n\f])? { return "e"; } |
| 335 |
G = "g"i / "\\" "0"? "0"? "0"? "0"? [\x47\x67] ("\r\n" / [ \t\r\n\f])? / "\\g"i { return "g"; } |
| 336 |
H = "h"i / "\\" "0"? "0"? "0"? "0"? [\x48\x68] ("\r\n" / [ \t\r\n\f])? / "\\h"i { return "h"; } |
| 337 |
I = "i"i / "\\" "0"? "0"? "0"? "0"? [\x49\x69] ("\r\n" / [ \t\r\n\f])? / "\\i"i { return "i"; } |
| 338 |
K = "k"i / "\\" "0"? "0"? "0"? "0"? [\x4b\x6b] ("\r\n" / [ \t\r\n\f])? / "\\k"i { return "k"; } |
| 339 |
L = "l"i / "\\" "0"? "0"? "0"? "0"? [\x4c\x6c] ("\r\n" / [ \t\r\n\f])? / "\\l"i { return "l"; } |
| 340 |
M = "m"i / "\\" "0"? "0"? "0"? "0"? [\x4d\x6d] ("\r\n" / [ \t\r\n\f])? / "\\m"i { return "m"; } |
| 341 |
N = "n"i / "\\" "0"? "0"? "0"? "0"? [\x4e\x6e] ("\r\n" / [ \t\r\n\f])? / "\\n"i { return "n"; } |
| 342 |
O = "o"i / "\\" "0"? "0"? "0"? "0"? [\x4f\x6f] ("\r\n" / [ \t\r\n\f])? / "\\o"i { return "o"; } |
| 343 |
P = "p"i / "\\" "0"? "0"? "0"? "0"? [\x50\x70] ("\r\n" / [ \t\r\n\f])? / "\\p"i { return "p"; } |
| 344 |
R = "r"i / "\\" "0"? "0"? "0"? "0"? [\x52\x72] ("\r\n" / [ \t\r\n\f])? / "\\r"i { return "r"; } |
| 345 |
S_ = "s"i / "\\" "0"? "0"? "0"? "0"? [\x53\x73] ("\r\n" / [ \t\r\n\f])? / "\\s"i { return "s"; } |
| 346 |
T = "t"i / "\\" "0"? "0"? "0"? "0"? [\x54\x74] ("\r\n" / [ \t\r\n\f])? / "\\t"i { return "t"; } |
| 347 |
U = "u"i / "\\" "0"? "0"? "0"? "0"? [\x55\x75] ("\r\n" / [ \t\r\n\f])? / "\\u"i { return "u"; } |
| 348 |
X = "x"i / "\\" "0"? "0"? "0"? "0"? [\x58\x78] ("\r\n" / [ \t\r\n\f])? / "\\x"i { return "x"; } |
| 349 |
Z = "z"i / "\\" "0"? "0"? "0"? "0"? [\x5a\x7a] ("\r\n" / [ \t\r\n\f])? / "\\z"i { return "z"; } |
| 350 |
|
| 351 |
/* Tokens */ |
| 352 |
|
| 353 |
S "whitespace" |
| 354 |
= comment* s |
| 355 |
|
| 356 |
CDO "<!--" |
| 357 |
= comment* "<!--" |
| 358 |
|
| 359 |
CDC "-->" |
| 360 |
= comment* "-->" |
| 361 |
|
| 362 |
INCLUDES "~=" |
| 363 |
= comment* "~=" |
| 364 |
|
| 365 |
DASHMATCH "|=" |
| 366 |
= comment* "|=" |
| 367 |
|
| 368 |
STRING "string" |
| 369 |
= comment* string:string { return string; } |
| 370 |
|
| 371 |
IDENT "identifier" |
| 372 |
= comment* ident:ident { return ident; } |
| 373 |
|
| 374 |
HASH "hash" |
| 375 |
= comment* "#" name:name { return "#" + name; } |
| 376 |
|
| 377 |
IMPORT_SYM "@import" |
| 378 |
= comment* "@" I M P O R T |
| 379 |
|
| 380 |
PAGE_SYM "@page" |
| 381 |
= comment* "@" P A G E |
| 382 |
|
| 383 |
MEDIA_SYM "@media" |
| 384 |
= comment* "@" M E D I A |
| 385 |
|
| 386 |
CHARSET_SYM "@charset" |
| 387 |
= comment* "@charset " |
| 388 |
|
| 389 |
/* We use |s| instead of |w| here to avoid infinite recursion. */ |
| 390 |
IMPORTANT_SYM "!important" |
| 391 |
= comment* "!" (s / comment)* I M P O R T A N T |
| 392 |
|
| 393 |
EMS "length" |
| 394 |
= comment* value:num E M { return { value: value, unit: "em" }; } |
| 395 |
|
| 396 |
EXS "length" |
| 397 |
= comment* value:num E X { return { value: value, unit: "ex" }; } |
| 398 |
|
| 399 |
LENGTH "length" |
| 400 |
= comment* value:num P X { return { value: value, unit: "px" }; } |
| 401 |
/ comment* value:num C M { return { value: value, unit: "cm" }; } |
| 402 |
/ comment* value:num M M { return { value: value, unit: "mm" }; } |
| 403 |
/ comment* value:num I N { return { value: value, unit: "in" }; } |
| 404 |
/ comment* value:num P T { return { value: value, unit: "pt" }; } |
| 405 |
/ comment* value:num P C { return { value: value, unit: "pc" }; } |
| 406 |
|
| 407 |
ANGLE "angle" |
| 408 |
= comment* value:num D E G { return { value: value, unit: "deg" }; } |
| 409 |
/ comment* value:num R A D { return { value: value, unit: "rad" }; } |
| 410 |
/ comment* value:num G R A D { return { value: value, unit: "grad" }; } |
| 411 |
|
| 412 |
TIME "time" |
| 413 |
= comment* value:num M S_ { return { value: value, unit: "ms" }; } |
| 414 |
/ comment* value:num S_ { return { value: value, unit: "s" }; } |
| 415 |
|
| 416 |
FREQ "frequency" |
| 417 |
= comment* value:num H Z { return { value: value, unit: "hz" }; } |
| 418 |
/ comment* value:num K H Z { return { value: value, unit: "kh" }; } |
| 419 |
|
| 420 |
PERCENTAGE "percentage" |
| 421 |
= comment* value:num "%" { return { value: value, unit: "%" }; } |
| 422 |
|
| 423 |
NUMBER "number" |
| 424 |
= comment* value:num { return { value: value, unit: null }; } |
| 425 |
|
| 426 |
URI "uri" |
| 427 |
= comment* U R L "("i w url:string w ")" { return url; } |
| 428 |
/ comment* U R L "("i w url:url w ")" { return url; } |
| 429 |
|
| 430 |
FUNCTION "function" |
| 431 |
= comment* name:ident "(" { return name; } |