csspool 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/parser.y ADDED
@@ -0,0 +1,287 @@
1
+ class CSS::SAC::GeneratedParser
2
+
3
+ token FUNCTION INCLUDES DASHMATCH LBRACE HASH PLUS GREATER S STRING IDENT
4
+ token COMMA URI CDO CDC NUMBER PERCENTAGE LENGTH EMS EXS ANGLE TIME FREQ
5
+ token IMPORTANT_SYM IMPORT_SYM MEDIA_SYM PAGE_SYM CHARSET_SYM DIMENSION
6
+
7
+ rule
8
+ stylesheet
9
+ : s_cdo_cdc_0toN CHARSET_SYM STRING ';' import_0toN ruleset_media_page_0toN
10
+ | s_cdo_cdc_0toN import_0toN ruleset_media_page_0toN
11
+ ;
12
+ ruleset_media_page_0toN
13
+ : ruleset s_cdo_cdc_0toN ruleset_media_page_0toN
14
+ | media s_cdo_cdc_0toN ruleset_media_page_0toN
15
+ | page s_cdo_cdc_0toN ruleset_media_page_0toN
16
+ | ruleset s_cdo_cdc_0toN
17
+ | media s_cdo_cdc_0toN
18
+ | page s_cdo_cdc_0toN
19
+ |
20
+ ;
21
+ import
22
+ : IMPORT_SYM s_0toN string_or_uri s_0toN medium_0toN ';' s_0toN {
23
+ self.document_handler.import_style(val[2], val[4] || [])
24
+ }
25
+ ;
26
+ optional_ignorable_at
27
+ : ignorable_at
28
+ |
29
+ ;
30
+ ignorable_at
31
+ : '@' IDENT s_0toN string_or_uri s_0toN medium_0toN ';' s_0toN {
32
+ self.document_handler.ignorable_at_rule(val[1])
33
+ }
34
+ | '@' IDENT s_0toN error S {
35
+ yyerrok
36
+ self.document_handler.ignorable_at_rule(val[1])
37
+ }
38
+ ;
39
+ import_0toN
40
+ : import s_cdo_cdc_0toN import_0toN
41
+ | ignorable_at s_cdo_cdc_0toN import_0toN
42
+ |
43
+ ;
44
+ media
45
+ : MEDIA_SYM s_0toN medium_rollup s_0toN ruleset_0toN '}' s_0toN {
46
+ self.document_handler.end_media(val[2])
47
+ }
48
+ ;
49
+ medium_rollup
50
+ : medium_1toN LBRACE { self.document_handler.start_media(val.first) }
51
+ | medium_1toN error LBRACE {
52
+ yyerrok
53
+ self.document_handler.start_media(val.first)
54
+ error = ParseException.new("Error near: \"#{val[0]}\"")
55
+ self.error_handler.error(error)
56
+ }
57
+ ;
58
+ medium
59
+ : IDENT s_0toN
60
+ ;
61
+ medium_0toN
62
+ : medium_1toN
63
+ |
64
+ ;
65
+ medium_1toN
66
+ : medium COMMA s_0toN medium_1toN { result = [val.first, val.last].flatten }
67
+ | medium { result = [val.first] }
68
+ ;
69
+ page
70
+ : page_start s_0toN LBRACE s_0toN declaration_0toN '}' s_0toN {
71
+ page_stuff = val.first
72
+ self.document_handler.end_page(page_stuff[0], page_stuff[1])
73
+ }
74
+ ;
75
+ page_start
76
+ : PAGE_SYM s_0toN optional_page optional_pseudo_page {
77
+ result = [val[2], val[3]]
78
+ self.document_handler.start_page(val[2], val[3])
79
+ }
80
+ ;
81
+ optional_page
82
+ : IDENT
83
+ |
84
+ ;
85
+ optional_pseudo_page
86
+ : ':' IDENT { result = val[1] }
87
+ |
88
+ ;
89
+ operator
90
+ : '/' s_0toN
91
+ | COMMA s_0toN
92
+ |
93
+ ;
94
+ combinator
95
+ : PLUS s_0toN { result = :SAC_DIRECT_ADJACENT_SELECTOR }
96
+ | GREATER s_0toN { result = :SAC_CHILD_SELECTOR }
97
+ | S { result = :SAC_DESCENDANT_SELECTOR }
98
+ ;
99
+ unary_operator
100
+ : '-' | PLUS
101
+ ;
102
+ property
103
+ : IDENT s_0toN
104
+ ;
105
+ ruleset
106
+ : selector_1toN s_0toN declaration_0toN '}' s_0toN {
107
+ self.document_handler.end_selector([val.first].flatten.compact)
108
+ }
109
+ ;
110
+ ruleset_0toN
111
+ : ruleset ruleset_0toN
112
+ | ruleset
113
+ |
114
+ ;
115
+ selector
116
+ : simple_selector_1toN { result = val.flatten }
117
+ ;
118
+ selector_1toN
119
+ : selector_list s_0toN LBRACE {
120
+ self.document_handler.start_selector([val.first].flatten.compact)
121
+ }
122
+ | selector error LBRACE {
123
+ yyerrok
124
+ self.document_handler.start_selector([val.first].flatten.compact)
125
+ }
126
+ | selector LBRACE {
127
+ self.document_handler.start_selector([val.first].flatten.compact)
128
+ }
129
+ ;
130
+ selector_list
131
+ : selector COMMA s_0toN selector_list { result = [val[0], val[3]] }
132
+ | selector
133
+ ;
134
+ simple_selector
135
+ : element_name hcap_0toN {
136
+ result = if val[1].nil?
137
+ val.first
138
+ else
139
+ ConditionalSelector.new(val.first, val[1])
140
+ end
141
+ }
142
+ | hcap_1toN {
143
+ result = ConditionalSelector.new(nil, val.first)
144
+ }
145
+ ;
146
+ simple_selector_1toN
147
+ : simple_selector combinator simple_selector_1toN {
148
+ result =
149
+ if val[1] == :SAC_DIRECT_ADJACENT_SELECTOR
150
+ SiblingSelector.new(val.first, val[2])
151
+ else
152
+ DescendantSelector.new(val.first, val[2], val[1])
153
+ end
154
+ }
155
+ | simple_selector
156
+ ;
157
+ class
158
+ : '.' IDENT { result = AttributeCondition.class_condition(val[1]) }
159
+ ;
160
+ element_name
161
+ : IDENT { result = ElementSelector.new(val.first) }
162
+ | '*' { result = SimpleSelector.new() }
163
+ ;
164
+ attrib
165
+ : '[' s_0toN IDENT s_0toN attrib_val_0or1 ']' {
166
+ result = AttributeCondition.attribute_condition(val[2], val[4])
167
+ }
168
+ ;
169
+ pseudo
170
+ : ':' FUNCTION s_0toN IDENT s_0toN ')'
171
+ | ':' FUNCTION s_0toN s_0toN ')'
172
+ | ':' IDENT { result = AttributeCondition.pseudo_class_condition(val[1]) }
173
+ ;
174
+ declaration
175
+ : property ':' s_0toN expr prio_0or1 {
176
+ if value = self.property_parser.parse_tokens(
177
+ self.tokenizer.tokenize(val.flatten[0..-2].join(' '))
178
+ )
179
+
180
+ value = [value].flatten
181
+
182
+ self.document_handler.property(val.first, value, !val[4].nil?)
183
+ result = value
184
+ end
185
+ }
186
+ ;
187
+ declaration_0toN
188
+ : declaration ';' s_0toN declaration_0toN
189
+ | declaration error ';' s_0toN declaration_0toN {
190
+ yyerrok
191
+ error = ParseException.new("Unkown property: \"#{val[1]}\"")
192
+ self.error_handler.error(error)
193
+ }
194
+ | declaration
195
+ | s_0toN ';' s_0toN declaration_0toN
196
+ |
197
+ ;
198
+ prio
199
+ : IMPORTANT_SYM s_0toN
200
+ ;
201
+ prio_0or1
202
+ : prio
203
+ |
204
+ ;
205
+ expr
206
+ : term operator expr { result = val }
207
+ | term
208
+ ;
209
+ term
210
+ : unary_operator num_or_length { result = val }
211
+ | num_or_length { result = val }
212
+ | STRING s_0toN
213
+ | IDENT s_0toN
214
+ | URI s_0toN
215
+ | hexcolor
216
+ | function
217
+ ;
218
+ num_or_length
219
+ : NUMBER s_0toN
220
+ | PERCENTAGE s_0toN
221
+ | LENGTH s_0toN
222
+ | EMS s_0toN
223
+ | EXS s_0toN
224
+ | ANGLE s_0toN
225
+ | TIME s_0toN
226
+ | FREQ s_0toN
227
+ ;
228
+ function
229
+ : FUNCTION s_0toN expr ')' s_0toN { result = [val[0], val[2], val[3]] }
230
+ | FUNCTION s_0toN expr error ')' s_0toN { yyerrok; result = [val[0], val[2], val[3]] }
231
+ ;
232
+ hexcolor
233
+ : HASH s_0toN
234
+ ;
235
+ hcap_0toN
236
+ : hcap_1toN
237
+ |
238
+ ;
239
+ hcap_1toN
240
+ : attribute_id hcap_1toN {
241
+ result = CombinatorCondition.new(val[0], val[1])
242
+ }
243
+ | class hcap_1toN {
244
+ result = CombinatorCondition.new(val[0], val[1])
245
+ }
246
+ | attrib hcap_1toN {
247
+ result = CombinatorCondition.new(val[0], val[1])
248
+ }
249
+ | pseudo hcap_1toN {
250
+ result = CombinatorCondition.new(val[0], val[1])
251
+ }
252
+ | attribute_id
253
+ | class
254
+ | attrib
255
+ | pseudo
256
+ ;
257
+ attribute_id
258
+ : HASH { result = AttributeCondition.attribute_id(val.first) }
259
+ ;
260
+ attrib_val_0or1
261
+ : eql_incl_dash s_0toN IDENT s_0toN { result = [val.first, val[2]] }
262
+ | eql_incl_dash s_0toN STRING s_0toN { result = [val.first, val[2]] }
263
+ |
264
+ ;
265
+ eql_incl_dash
266
+ : '='
267
+ | INCLUDES
268
+ | DASHMATCH
269
+ ;
270
+ string_or_uri
271
+ : STRING
272
+ | URI
273
+ ;
274
+ s_cdo_cdc_0toN
275
+ : S s_cdo_cdc_0toN
276
+ | CDO s_cdo_cdc_0toN
277
+ | CDC s_cdo_cdc_0toN
278
+ | S
279
+ | CDO
280
+ | CDC
281
+ |
282
+ ;
283
+ s_0toN
284
+ : S s_0toN
285
+ |
286
+ ;
287
+ end