forked from ijprest/keyboard-layout-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cssparser.y
191 lines (162 loc) · 10.4 KB
/
cssparser.y
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
/* Parser for CSS3, as defined by http://www.w3.org/TR/css-syntax-3 */
%lex
/* Lexer Macros */
comment \/\*([^\*](\*[^\/])?)*\*\/
newline \r\n|\n|\r|\f
whitespace [ \t\n\r\f]
hex [0-9a-fA-F]
escape \\([^0-9a-fA-F\n\r\f]|({hex}{1,6}{whitespace}?))
ws {whitespace}*
nonascii [\200-\377]
token_start ({escape}|[a-zA-Z_]|{nonascii})
token_char ({escape}|[a-zA-Z_0-9-]|{nonascii})
identifier \-?{token_start}{token_char}*
string \"({escape}|\\{newline}|[^"\\\r\n\f])*\"|\'({escape}|\\{newline}|[^'\\\r\n\f])*\'
urlu ({escape}|[^'"\(\)\\ \t\n\r\f\000-\010\016-\037\177])+
number [+-]?(\d+\.\d+|\.\d+|\d+)([eE][+-]?\d+)?
range [uU]'+'({hex}{1,6}('-'{hex}{1,6})?|'?'{6}|{hex}'?'{5}|{hex}{2}'?'{4}|{hex}{3}'?'{3}|{hex}{4}'?'{2}|{hex}{5}'?'{1})
%%
/* Lexical Tokens */
{comment} return 'WHITESPACE';
{whitespace}+ return 'WHITESPACE';
{identifier}'(' return 'FUNCTION';
{identifier} return 'IDENTIFIER';
'#'{token_char}+ return 'HASH';
'@'{identifier} return 'AT_KEYWORD';
'!'{ws}'important'{ws} return 'IMPORTANT';
{string} return 'STRING';
'url('{w}({string}|{urlu})?{w}')' return 'URL';
{number}{identifier} return 'DIMENSION';
{number}'%' return 'PERCENTAGE';
{number} return 'NUMBER';
{range} return 'UNICODE_RANGE';
[~|^$\*]?'=' return 'MATCH_TOKEN';
'||' return 'COLUMN_TOKEN';
'<!--' return 'CDO';
'-->' return 'CDC';
<<EOF>> return 'EOF';
'\'' return 'SQUOTE';
. return yytext;
/lex
/* language grammar */
%start stylesheet
%ebnf
%%
stylesheet
: WS EOF { return $$ = []; }
| stylesheet_content WS EOF { return $$ = ($stylesheet_content && $stylesheet_content.length) ? $stylesheet_content : []; }
;
stylesheet_content
: stylesheet_item { $$ = []; if($stylesheet_item !== null) $$.push($stylesheet_item); }
| stylesheet_content stylesheet_item { $$ = $stylesheet_content; if($stylesheet_item !== null) $$.push($stylesheet_item); }
;
stylesheet_item
: WS CDO { $$ = null; }
| WS CDC { $$ = null; }
| qualified_rule { $$ = $qualified_rule; }
| at_rule { $$ = $at_rule; }
;
at_rule
: WS AT_KEYWORD ';' { $$ = { name: $AT_KEYWORD }}
| WS AT_KEYWORD at_rule_selector ';' { $$ = { name: $AT_KEYWORD, selector: $at_rule_selector.trim() }; if(!$$.selector) delete $$.selector; }
| WS AT_KEYWORD '{' '}' { $$ = { name: $AT_KEYWORD }; }
| WS AT_KEYWORD '{' at_rule_content '}' { $$ = { name: $AT_KEYWORD, content: $at_rule_content.trim() }; if(!$$.content) delete $$.content; }
| WS AT_KEYWORD at_rule_selector '{' '}' { $$ = { name: $AT_KEYWORD, selector: $at_rule_selector.trim() }; if(!$$.selector) delete $$.selector; }
| WS AT_KEYWORD at_rule_selector '{' at_rule_content '}' { $$ = { name: $AT_KEYWORD, selector: $at_rule_selector.trim(), content: $at_rule_content.trim() }; if(!$$.selector) delete $$.selector; if(!$$.content) delete $$.content; }
;
at_rule_selector
: any_token { $$ = $any_token; }
| at_rule_selector any_token { $$ = $at_rule_selector + $any_token; }
;
at_rule_content
: any_token { $$ = $any_token; }
| at_rule_content ';' { $$ = $at_rule_content + ';'; }
| at_rule_content '{' at_rule_content '}' { $$ = $at_rule_content1 + '{' + $at_rule_content2 + '}'; }
| at_rule_content any_token { $$ = $at_rule_content + $any_token; }
;
qualified_rule
: qualified_rule_prelude '{' WS '}' { $$ = { selector: $qualified_rule_prelude } }
| qualified_rule_prelude '{' WS declaration_list '}' { $$ = { selector: $qualified_rule_prelude, decls: $declaration_list } }
| qualified_rule_prelude '{' WS declaration_list ';' WS '}' { $$ = { selector: $qualified_rule_prelude, decls: $declaration_list } }
;
declaration_list
: declaration { $$ = [$declaration]; }
| declaration_list ';' WS declaration { $$ = $declaration_list; $$.push($declaration); }
;
declaration
: IDENTIFIER WS ':' declaration_value { $$ = [$IDENTIFIER,$declaration_value.trim()]; }
;
// Anything other than a semi-colon or a closing brace
declaration_value
: any_token { $$ = $any_token; }
| declaration_value any_token { $$ = $declaration_value + $any_token; }
;
// Does not include semi-colon, or open/close braces
any_token
: (WHITESPACE | FUNCTION | IDENTIFIER | HASH | AT_KEYWORD |
IMPORTANT | STRING | URL | DIMENSION | PERCENTAGE | NUMBER |
UNICODE_RANGE | MATCH_TOKEN | COLUMN_TOKEN | CDO | CDC | EOF | SQUOTE |
'!'|'"'|'#'|'$'|'%'|'&'|'('|')'|'*'|'+'|','|'-'|'.'|'/'|
':'|'<'|'='|'>'|'?'|'@'|'['|'\'|']'|'^'|'_'|'`'|'|'|'~') { $$ = $1; }
;
qualified_rule_prelude
: WS selector WS { $$ = [$selector]; }
| qualified_rule_prelude ',' WS selector WS { $$ = $qualified_rule_prelude; $$.push($selector); }
;
selector
: single_selector { $$ = $single_selector; }
| selector WHITESPACE single_selector { $$ = $selector + ' ' + $single_selector; }
| selector WS '>' WS single_selector { $$ = $selector + '>' + $single_selector; }
| selector WS '+' WS single_selector { $$ = $selector + '+' + $single_selector; }
| selector WS '~' WS single_selector { $$ = $selector + '~' + $single_selector; }
| ':' IDENTIFIER { $$ = ':' + $IDENTIFIER; }
| ':' ':' IDENTIFIER { $$ = '::' + $IDENTIFIER; }
| selector ':' IDENTIFIER { $$ = $selector + ':' + $IDENTIFIER; }
| selector ':' ':' IDENTIFIER { $$ = $selector + '::' + $IDENTIFIER; }
| ':' FUNCTION WS selector_function_params { $$ = ':' + $FUNCTION + $selector_function_params + ')'; }
| selector ':' FUNCTION WS selector_function_params { $$ = $selector + ':' + $FUNCTION + $selector_function_params + ')'; }
;
selector_function_params
: IDENTIFIER WS ')' { $$ = $IDENTIFIER; }
| NUMBER WS ')' { $$ = $NUMBER; }
| DIMENSION WS ')' { $$ = $DIMENSION; } // 2n
| IDENTIFIER WS NUMBER WS ')' { $$ = $IDENTIFIER + $NUMBER; } // n+1
| IDENTIFIER WS '+' WS NUMBER WS ')' { $$ = $IDENTIFIER + '+' + $NUMBER; } // n+1
| IDENTIFIER WS '-' WS NUMBER WS ')' { $$ = $IDENTIFIER + '-' + $NUMBER; } // n+1
| DIMENSION WS NUMBER WS ')' { $$ = $DIMENSION + $NUMBER; } // 2n+1
| DIMENSION WS '+' WS NUMBER WS ')' { $$ = $DIMENSION + '+' + $NUMBER; } // 2n+1
| DIMENSION WS '-' WS NUMBER WS ')' { $$ = $DIMENSION + '-' + $NUMBER; } // 2n+1
;
single_selector
: '*' { $$ = $1; }
| '.' IDENTIFIER { $$ = '.' + $IDENTIFIER; }
| HASH { $$ = $HASH; }
| IDENTIFIER { $$ = $IDENTIFIER; }
| single_selector '.' IDENTIFIER { $$ = $single_selector + '.' + $IDENTIFIER; }
| single_selector HASH { $$ = $single_selector + $HASH; }
| single_selector '[' WS attribute_selector ']' { $$ = $single_selector + '[' + $attribute_selector + ']'; }
;
attribute_selector
: attribute_name WS { $$ = $attribute_name; }
| attribute_name WS MATCH_TOKEN WS attribute_value { $$ = $attribute_name + $MATCH_TOKEN + $attribute_value; }
;
attribute_name
: IDENTIFIER { $$ = $IDENTIFIER; }
;
attribute_value
: unquoted_attribute_value { $$ = $unquoted_attribute_value; }
| STRING { $$ = $STRING; }
;
unquoted_attribute_value
: unquoted_attribute_value_char { $$ = $unquoted_attribute_value_char; }
| IDENTIFIER { $$ = $IDENTIFIER; }
| unquoted_attribute_value unquoted_attribute_value_char { $$ = $unquoted_attribute_value + $unquoted_attribute_value_char; }
| unquoted_attribute_value IDENTIFIER { $$ = $unquoted_attribute_value + $IDENTIFIER; }
;
unquoted_attribute_value_char
: ('.'|':'|'-'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'|NUMBER) { $$ = $1; } // technically shouldn't allow full number syntax
;
WS
: /*empty*/ { $$ = ''; }
| WHITESPACE+ { $$ = ''; }
;