1 | // License: GPL. For details, see LICENSE file. |
---|
2 | options { |
---|
3 | STATIC = false; |
---|
4 | OUTPUT_DIRECTORY = "parsergen"; |
---|
5 | } |
---|
6 | |
---|
7 | PARSER_BEGIN(MapCSSParser) |
---|
8 | package org.openstreetmap.josm.gui.mappaint.mapcss.parsergen; |
---|
9 | |
---|
10 | import java.awt.Color; |
---|
11 | import java.util.ArrayList; |
---|
12 | import java.util.List; |
---|
13 | |
---|
14 | import org.openstreetmap.josm.gui.mappaint.Keyword; |
---|
15 | import org.openstreetmap.josm.gui.mappaint.mapcss.Condition; |
---|
16 | import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context; |
---|
17 | import org.openstreetmap.josm.gui.mappaint.mapcss.Expression; |
---|
18 | import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction; |
---|
19 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule; |
---|
20 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource; |
---|
21 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector; |
---|
22 | import org.openstreetmap.josm.gui.mappaint.mapcss.ExpressionFactory; |
---|
23 | import org.openstreetmap.josm.gui.mappaint.mapcss.LiteralExpression; |
---|
24 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSException; |
---|
25 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector; |
---|
26 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector; |
---|
27 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.LinkSelector; |
---|
28 | import org.openstreetmap.josm.tools.ColorHelper; |
---|
29 | import org.openstreetmap.josm.tools.Pair; |
---|
30 | import org.openstreetmap.josm.tools.Utils; |
---|
31 | import org.openstreetmap.josm.Main; |
---|
32 | |
---|
33 | /************* |
---|
34 | * <pre> |
---|
35 | * Parser definitions |
---|
36 | * |
---|
37 | * rule |
---|
38 | * _______________________|______________________________ |
---|
39 | * | | |
---|
40 | * selector declaration |
---|
41 | * _________|___________________ _________|____________ |
---|
42 | * | | | | |
---|
43 | * |
---|
44 | * way|z11-12[highway=residential] { color: red; width: 3 } |
---|
45 | * |
---|
46 | * |_____||___________________| |_________| |
---|
47 | * | | | |
---|
48 | * zoom condition instruction |
---|
49 | * |
---|
50 | * more general: |
---|
51 | * |
---|
52 | * way|z13-[a=b][c=d]::subpart, way|z-3[u=v]:closed::subpart2 { p1 : val; p2 : val; } |
---|
53 | * |
---|
54 | * 'val' can be a literal, or an expression like "prop(width, default) + 0.8". |
---|
55 | * |
---|
56 | * {@literal @media} { ... } queries are supported, following http://www.w3.org/TR/css3-mediaqueries/#syntax |
---|
57 | * |
---|
58 | * media_query |
---|
59 | * ___________________________|_______________________________ |
---|
60 | * | | |
---|
61 | * {@literal @media} all and (min-josm-version: 7789) and (max-josm-version: 7790), all and (user-agent: xyz) { ... } |
---|
62 | * |______________________| |
---|
63 | * | |
---|
64 | * media_expression |
---|
65 | * </pre> |
---|
66 | */ |
---|
67 | |
---|
68 | public class MapCSSParser { |
---|
69 | MapCSSStyleSource sheet; |
---|
70 | } |
---|
71 | PARSER_END(MapCSSParser) |
---|
72 | |
---|
73 | /************* |
---|
74 | * Token definitions |
---|
75 | */ |
---|
76 | |
---|
77 | <DEFAULT> |
---|
78 | TOKEN [IGNORE_CASE]: |
---|
79 | { |
---|
80 | /* Special keywords in some contexts, ordinary identifiers in other contexts. |
---|
81 | Don't use the javacc token contexts (except DEFAULT and COMMENT) because |
---|
82 | they complicate error handling. Instead simply add a parsing rule <code>ident()</code> |
---|
83 | that parses the IDENT token and in addition all the keyword tokens. */ |
---|
84 | < AND: "and" > |
---|
85 | | < SET: "set" > |
---|
86 | | < NOT: "not" > |
---|
87 | | < MEDIA: "@media" > |
---|
88 | } |
---|
89 | |
---|
90 | <DEFAULT> |
---|
91 | TOKEN: |
---|
92 | { |
---|
93 | < IDENT: ["a"-"z","A"-"Z","_"] ( ["a"-"z","A"-"Z","_","-","0"-"9"] )* > |
---|
94 | | < UINT: ["1"-"9"] ( ["0"-"9"] )* > |
---|
95 | | < UFLOAT: ( ["0"-"9"] )+ ( "." ( ["0"-"9"] )+ )? > |
---|
96 | | < STRING: "\"" ( [" ","!","#"-"[","]"-"~","\u0080"-"\uFFFF"] | "\\\"" | "\\\\" )* "\"" > |
---|
97 | | < #PREDEFINED: "\\" ["d","D","s","S","w","W"] > |
---|
98 | | < #REGEX_CHAR_WITHOUT_STAR: [" "-")","+"-".","0"-"[","]"-"~","\u0080"-"\uFFFF"] | "\\/" | "\\\\" | "\\[" | "\\]" | "\\+" | "\\." | "\\'" | "\\\"" | <PREDEFINED> > |
---|
99 | | < REGEX: "/" <REGEX_CHAR_WITHOUT_STAR> ( <REGEX_CHAR_WITHOUT_STAR> | "*" )* "/" > |
---|
100 | | < #H: ["0"-"9","a"-"f","A"-"F"] > |
---|
101 | | < HEXCOLOR: "#" ( <H><H><H><H><H><H><H><H> | <H><H><H><H><H><H> | <H><H><H> ) > |
---|
102 | | < S: ( " " | "\t" | "\n" | "\r" | "\f" )+ > |
---|
103 | | < STAR: "*" > |
---|
104 | | < SLASH: "/" > |
---|
105 | | < LBRACE: "{" > |
---|
106 | | < RBRACE: "}" > |
---|
107 | | < LSQUARE: "[" > |
---|
108 | | < RSQUARE: "]" > |
---|
109 | | < LPAR: "(" > |
---|
110 | | < RPAR: ")" > |
---|
111 | | < GREATER_EQUAL: ">=" > |
---|
112 | | < LESS_EQUAL: "<=" > |
---|
113 | | < GREATER: ">" > |
---|
114 | | < LESS: "<" > |
---|
115 | | < EQUAL: "=" > |
---|
116 | | < EXCLAMATION: "!" > |
---|
117 | | < TILDE: "~" > |
---|
118 | | < COLON: ":" > |
---|
119 | | < DCOLON: "::" > |
---|
120 | | < SEMICOLON: ";" > |
---|
121 | | < COMMA: "," > |
---|
122 | | < PIPE: "|" > |
---|
123 | | < PIPE_Z: "|z" > |
---|
124 | | < PLUS: "+" > |
---|
125 | | < MINUS: "-" > |
---|
126 | | < AMPERSAND: "&" > |
---|
127 | | < QUESTION: "?" > |
---|
128 | | < DOLLAR: "$" > |
---|
129 | | < CARET: "^" > |
---|
130 | | < FULLSTOP: "." > |
---|
131 | | < ELEMENT_OF: "∈" > |
---|
132 | | < CROSSING: "⧉" > |
---|
133 | | < COMMENT_START: "/*" > : COMMENT |
---|
134 | | < UNEXPECTED_CHAR : ~[] > // avoid TokenMgrErrors because they are hard to recover from |
---|
135 | } |
---|
136 | |
---|
137 | <COMMENT> |
---|
138 | TOKEN: |
---|
139 | { |
---|
140 | < COMMENT_END: "*/" > : DEFAULT |
---|
141 | } |
---|
142 | |
---|
143 | <COMMENT> |
---|
144 | SKIP: |
---|
145 | { |
---|
146 | < ~[] > |
---|
147 | } |
---|
148 | |
---|
149 | int uint() : |
---|
150 | { |
---|
151 | Token i; |
---|
152 | } |
---|
153 | { |
---|
154 | i=<UINT> { return Integer.parseInt(i.image); } |
---|
155 | } |
---|
156 | |
---|
157 | int int_() : |
---|
158 | { |
---|
159 | int i; |
---|
160 | } |
---|
161 | { |
---|
162 | <MINUS> i=uint() { return -i; } | i=uint() { return i; } |
---|
163 | } |
---|
164 | |
---|
165 | float ufloat() : |
---|
166 | { |
---|
167 | Token f; |
---|
168 | } |
---|
169 | { |
---|
170 | ( f=<UFLOAT> | f=<UINT> ) |
---|
171 | { return Float.parseFloat(f.image); } |
---|
172 | } |
---|
173 | |
---|
174 | float float_() : |
---|
175 | { |
---|
176 | float f; |
---|
177 | } |
---|
178 | { |
---|
179 | <MINUS> f=ufloat() { return -f; } | f=ufloat() { return f; } |
---|
180 | } |
---|
181 | |
---|
182 | String string() : |
---|
183 | { |
---|
184 | Token t; |
---|
185 | } |
---|
186 | { |
---|
187 | t=<STRING> |
---|
188 | { return t.image.substring(1, t.image.length() - 1).replace("\\\"", "\"").replace("\\\\", "\\"); } |
---|
189 | } |
---|
190 | |
---|
191 | String ident(): |
---|
192 | { |
---|
193 | Token t; |
---|
194 | String s; |
---|
195 | } |
---|
196 | { |
---|
197 | ( t=<IDENT> | t=<SET> | t=<NOT> | t=<AND> ) { return t.image; } |
---|
198 | } |
---|
199 | |
---|
200 | String string_or_ident() : |
---|
201 | { |
---|
202 | Token t; |
---|
203 | String s; |
---|
204 | } |
---|
205 | { |
---|
206 | ( s=ident() | s=string() ) { return s; } |
---|
207 | } |
---|
208 | |
---|
209 | String regex() : |
---|
210 | { |
---|
211 | Token t; |
---|
212 | } |
---|
213 | { |
---|
214 | t=<REGEX> |
---|
215 | { return t.image.substring(1, t.image.length() - 1); } |
---|
216 | } |
---|
217 | |
---|
218 | /** |
---|
219 | * white-space |
---|
220 | */ |
---|
221 | void s() : |
---|
222 | { |
---|
223 | } |
---|
224 | { |
---|
225 | ( <S> )? |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | * mix of white-space and comments |
---|
230 | */ |
---|
231 | void w() : |
---|
232 | { |
---|
233 | } |
---|
234 | { |
---|
235 | ( <S> | <COMMENT_START> <COMMENT_END> )* |
---|
236 | } |
---|
237 | |
---|
238 | /** |
---|
239 | * comma delimited list of floats (at least 2, all >= 0) |
---|
240 | */ |
---|
241 | List<Float> float_array() : |
---|
242 | { |
---|
243 | float f; |
---|
244 | List<Float> fs = new ArrayList<Float>(); |
---|
245 | } |
---|
246 | { |
---|
247 | f=ufloat() { fs.add(f); } |
---|
248 | ( |
---|
249 | <COMMA> s() |
---|
250 | f=ufloat() { fs.add(f); } |
---|
251 | )+ |
---|
252 | { |
---|
253 | return fs; |
---|
254 | } |
---|
255 | } |
---|
256 | |
---|
257 | /** |
---|
258 | * root |
---|
259 | */ |
---|
260 | void sheet(MapCSSStyleSource sheet): |
---|
261 | { |
---|
262 | MapCSSRule r; |
---|
263 | } |
---|
264 | { |
---|
265 | { this.sheet = sheet; } |
---|
266 | w() |
---|
267 | rules(true) ( media() w() rules(true) )* |
---|
268 | <EOF> |
---|
269 | } |
---|
270 | |
---|
271 | void media(): |
---|
272 | { |
---|
273 | boolean pass = false; |
---|
274 | boolean q; |
---|
275 | boolean empty = true; |
---|
276 | } |
---|
277 | { |
---|
278 | <MEDIA> w() |
---|
279 | ( q=media_query() { pass = pass || q; empty = false; } |
---|
280 | ( <COMMA> w() q=media_query() { pass = pass || q; } )* |
---|
281 | )? |
---|
282 | <LBRACE> w() |
---|
283 | rules(empty || pass) |
---|
284 | <RBRACE> |
---|
285 | } |
---|
286 | |
---|
287 | boolean media_query(): |
---|
288 | { |
---|
289 | Token t; |
---|
290 | String mediatype = "all"; |
---|
291 | boolean pass = true; |
---|
292 | boolean invert = false; |
---|
293 | boolean e; |
---|
294 | } |
---|
295 | { |
---|
296 | ( <NOT> { invert = true; } w() )? |
---|
297 | ( |
---|
298 | t=<IDENT> { mediatype = t.image.toLowerCase(); } w() |
---|
299 | ( <AND> w() e=media_expression() { pass = pass && e; } w() )* |
---|
300 | | |
---|
301 | e=media_expression() { pass = pass && e; } w() |
---|
302 | ( <AND> w() e=media_expression() { pass = pass && e; } w() )* |
---|
303 | ) |
---|
304 | { |
---|
305 | if (!"all".equals(mediatype)) { |
---|
306 | pass = false; |
---|
307 | } |
---|
308 | return invert ? (!pass) : pass; |
---|
309 | } |
---|
310 | } |
---|
311 | |
---|
312 | boolean media_expression(): |
---|
313 | { |
---|
314 | Token t; |
---|
315 | String feature; |
---|
316 | Object val = null; |
---|
317 | } |
---|
318 | { |
---|
319 | <LPAR> w() t=<IDENT> { feature = t.image; } w() ( <COLON> w() val=literal() )? <RPAR> |
---|
320 | { return this.sheet.evalMediaExpression(feature, val); } |
---|
321 | } |
---|
322 | |
---|
323 | void rules(boolean add): |
---|
324 | { |
---|
325 | MapCSSRule r; |
---|
326 | } |
---|
327 | { |
---|
328 | ( |
---|
329 | try { |
---|
330 | r=rule() { if (add && r != null) { sheet.rules.add(r); } } w() |
---|
331 | } catch (MapCSSException mex) { |
---|
332 | error_skipto(RBRACE, mex); |
---|
333 | w(); |
---|
334 | } catch (ParseException ex) { |
---|
335 | error_skipto(RBRACE, null); |
---|
336 | w(); |
---|
337 | } |
---|
338 | )* |
---|
339 | } |
---|
340 | |
---|
341 | MapCSSRule rule(): |
---|
342 | { |
---|
343 | List<Selector> selectors = new ArrayList<Selector>(); |
---|
344 | Selector sel; |
---|
345 | List<Instruction> decl; |
---|
346 | } |
---|
347 | { |
---|
348 | sel=child_selector() { selectors.add(sel); } |
---|
349 | ( |
---|
350 | <COMMA> w() |
---|
351 | sel=child_selector() { selectors.add(sel); } |
---|
352 | )* |
---|
353 | decl=declaration() |
---|
354 | { return new MapCSSRule(selectors, decl); } |
---|
355 | } |
---|
356 | |
---|
357 | Selector child_selector() : |
---|
358 | { |
---|
359 | Selector.ChildOrParentSelectorType type = null; |
---|
360 | Condition c; |
---|
361 | List<Condition> conditions = new ArrayList<Condition>(); |
---|
362 | Selector selLeft; |
---|
363 | LinkSelector selLink = null; |
---|
364 | Selector selRight = null; |
---|
365 | } |
---|
366 | { |
---|
367 | selLeft=selector() w() |
---|
368 | ( |
---|
369 | ( |
---|
370 | ( |
---|
371 | <GREATER> { type = Selector.ChildOrParentSelectorType.CHILD; } |
---|
372 | | |
---|
373 | <LESS> { type = Selector.ChildOrParentSelectorType.PARENT; } |
---|
374 | | |
---|
375 | <PLUS> { type = Selector.ChildOrParentSelectorType.SIBLING; } |
---|
376 | ) |
---|
377 | ( ( c=condition(Context.LINK) | c=class_or_pseudoclass(Context.LINK) ) { conditions.add(c); } )* |
---|
378 | | |
---|
379 | <ELEMENT_OF> { type = Selector.ChildOrParentSelectorType.ELEMENT_OF; } |
---|
380 | | |
---|
381 | <CROSSING> { type = Selector.ChildOrParentSelectorType.CROSSING; } |
---|
382 | ) |
---|
383 | { selLink = new LinkSelector(conditions); } |
---|
384 | w() |
---|
385 | selRight=selector() w() |
---|
386 | )? |
---|
387 | { return selRight != null ? new ChildOrParentSelector(selLeft, selLink, selRight, type) : selLeft; } |
---|
388 | } |
---|
389 | |
---|
390 | Selector selector() : |
---|
391 | { |
---|
392 | Token base; |
---|
393 | Condition c; |
---|
394 | Pair<Integer, Integer> r = null; |
---|
395 | List<Condition> conditions = new ArrayList<Condition>(); |
---|
396 | String sub = null; |
---|
397 | } |
---|
398 | { |
---|
399 | ( base=<IDENT> | base=<STAR> ) |
---|
400 | ( r=zoom() )? |
---|
401 | ( ( c=condition(Context.PRIMITIVE) | c=class_or_pseudoclass(Context.PRIMITIVE) ) { conditions.add(c); } )* |
---|
402 | ( sub=subpart() )? |
---|
403 | { return new GeneralSelector(base.image, r, conditions, sub); } |
---|
404 | } |
---|
405 | |
---|
406 | Pair<Integer, Integer> zoom() : |
---|
407 | { |
---|
408 | Integer min = 0; |
---|
409 | Integer max = Integer.MAX_VALUE; |
---|
410 | } |
---|
411 | { |
---|
412 | <PIPE_Z> |
---|
413 | ( |
---|
414 | <MINUS> max=uint() |
---|
415 | | |
---|
416 | LOOKAHEAD(2) |
---|
417 | min=uint() <MINUS> ( max=uint() )? |
---|
418 | | |
---|
419 | min=uint() { max = min; } |
---|
420 | ) |
---|
421 | { return new Pair<Integer, Integer>(min, max); } |
---|
422 | } |
---|
423 | |
---|
424 | Condition condition(Context context) : |
---|
425 | { |
---|
426 | Condition c; |
---|
427 | Expression e; |
---|
428 | } |
---|
429 | { |
---|
430 | <LSQUARE> s() |
---|
431 | ( |
---|
432 | LOOKAHEAD( simple_key_condition(context) s() <RSQUARE> ) |
---|
433 | c=simple_key_condition(context) s() <RSQUARE> { return c; } |
---|
434 | | |
---|
435 | LOOKAHEAD( simple_key_value_condition(context) s() <RSQUARE> ) |
---|
436 | c=simple_key_value_condition(context) s() <RSQUARE> { return c; } |
---|
437 | | |
---|
438 | e=expression() <RSQUARE> { return Condition.createExpressionCondition(e, context); } |
---|
439 | ) |
---|
440 | } |
---|
441 | |
---|
442 | String tag_key() : |
---|
443 | { |
---|
444 | String s, s2; |
---|
445 | Token t; |
---|
446 | } |
---|
447 | { |
---|
448 | s=string() { return s; } |
---|
449 | | |
---|
450 | s=ident() ( <COLON> s2=ident() { s += ':' + s2; } )* { return s; } |
---|
451 | } |
---|
452 | |
---|
453 | Condition simple_key_condition(Context context) : |
---|
454 | { |
---|
455 | boolean not = false; |
---|
456 | Condition.KeyMatchType matchType = null;; |
---|
457 | String key; |
---|
458 | } |
---|
459 | { |
---|
460 | ( <EXCLAMATION> { not = true; } )? |
---|
461 | ( |
---|
462 | { matchType = Condition.KeyMatchType.REGEX; } key = regex() |
---|
463 | | |
---|
464 | key = tag_key() |
---|
465 | ) |
---|
466 | ( LOOKAHEAD(2) <QUESTION> <EXCLAMATION> { matchType = Condition.KeyMatchType.FALSE; } )? |
---|
467 | ( <QUESTION> { matchType = Condition.KeyMatchType.TRUE; } )? |
---|
468 | { return Condition.createKeyCondition(key, not, matchType, context); } |
---|
469 | } |
---|
470 | |
---|
471 | Condition simple_key_value_condition(Context context) : |
---|
472 | { |
---|
473 | String key; |
---|
474 | String val; |
---|
475 | float f; |
---|
476 | int i; |
---|
477 | Condition.Op op; |
---|
478 | boolean considerValAsKey = false; |
---|
479 | } |
---|
480 | { |
---|
481 | key=tag_key() s() |
---|
482 | ( |
---|
483 | LOOKAHEAD(3) |
---|
484 | ( |
---|
485 | <EQUAL> <TILDE> { op=Condition.Op.REGEX; } |
---|
486 | | |
---|
487 | <EXCLAMATION> <TILDE> { op=Condition.Op.NREGEX; } |
---|
488 | ) |
---|
489 | s() |
---|
490 | ( <STAR> { considerValAsKey=true; } )? |
---|
491 | val=regex() |
---|
492 | | |
---|
493 | ( |
---|
494 | <EXCLAMATION> <EQUAL> { op=Condition.Op.NEQ; } |
---|
495 | | |
---|
496 | <EQUAL> { op=Condition.Op.EQ; } |
---|
497 | | |
---|
498 | <TILDE> <EQUAL> { op=Condition.Op.ONE_OF; } |
---|
499 | | |
---|
500 | <CARET> <EQUAL> { op=Condition.Op.BEGINS_WITH; } |
---|
501 | | |
---|
502 | <DOLLAR> <EQUAL> { op=Condition.Op.ENDS_WITH; } |
---|
503 | | |
---|
504 | <STAR> <EQUAL> { op=Condition.Op.CONTAINS; } |
---|
505 | ) |
---|
506 | s() |
---|
507 | ( <STAR> { considerValAsKey=true; } )? |
---|
508 | ( |
---|
509 | LOOKAHEAD(2) |
---|
510 | i=int_() { val=Integer.toString(i); } |
---|
511 | | |
---|
512 | f=float_() { val=Float.toString(f); } |
---|
513 | | |
---|
514 | val=string_or_ident() |
---|
515 | ) |
---|
516 | | |
---|
517 | ( |
---|
518 | <GREATER_EQUAL> { op=Condition.Op.GREATER_OR_EQUAL; } |
---|
519 | | |
---|
520 | <GREATER> { op=Condition.Op.GREATER; } |
---|
521 | | |
---|
522 | <LESS_EQUAL> { op=Condition.Op.LESS_OR_EQUAL; } |
---|
523 | | |
---|
524 | <LESS> { op=Condition.Op.LESS; } |
---|
525 | ) |
---|
526 | s() |
---|
527 | f=float_() { val=Float.toString(f); } |
---|
528 | ) |
---|
529 | { return Condition.createKeyValueCondition(key, val, op, context, considerValAsKey); } |
---|
530 | } |
---|
531 | |
---|
532 | Condition class_or_pseudoclass(Context context) : |
---|
533 | { |
---|
534 | String s; |
---|
535 | boolean not = false; |
---|
536 | boolean pseudo; |
---|
537 | } |
---|
538 | { |
---|
539 | ( <EXCLAMATION> { not = true; } )? |
---|
540 | ( |
---|
541 | <FULLSTOP> { pseudo = false; } |
---|
542 | | |
---|
543 | <COLON> { pseudo = true; } |
---|
544 | ) |
---|
545 | s=ident() |
---|
546 | { return pseudo |
---|
547 | ? Condition.createPseudoClassCondition(s, not, context) |
---|
548 | : Condition.createClassCondition(s, not, context); } |
---|
549 | } |
---|
550 | |
---|
551 | String subpart() : |
---|
552 | { |
---|
553 | String s; |
---|
554 | } |
---|
555 | { |
---|
556 | <DCOLON> |
---|
557 | ( s=ident() { return s; } | <STAR> { return "*"; } ) |
---|
558 | } |
---|
559 | |
---|
560 | List<Instruction> declaration() : |
---|
561 | { |
---|
562 | List<Instruction> ins = new ArrayList<Instruction>(); |
---|
563 | Instruction i; |
---|
564 | Token key; |
---|
565 | Object val = null; |
---|
566 | } |
---|
567 | { |
---|
568 | <LBRACE> w() |
---|
569 | ( |
---|
570 | ( |
---|
571 | <SET> w() |
---|
572 | (<FULLSTOP>)? // specification allows "set .class" to set "class". we also support "set class" |
---|
573 | key=<IDENT> w() |
---|
574 | ( <EQUAL> val=expression() )? |
---|
575 | { ins.add(new Instruction.AssignmentInstruction(key.image, val == null ? true : val, true)); } |
---|
576 | ( <RBRACE> { return ins; } | <SEMICOLON> w() ) |
---|
577 | ) |
---|
578 | | |
---|
579 | key=<IDENT> w() <COLON> w() |
---|
580 | ( |
---|
581 | LOOKAHEAD( float_array() w() ( <SEMICOLON> | <RBRACE> ) ) |
---|
582 | val=float_array() |
---|
583 | { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); } |
---|
584 | w() |
---|
585 | ( <RBRACE> { return ins; } | <SEMICOLON> w() ) |
---|
586 | | |
---|
587 | LOOKAHEAD( expression() ( <SEMICOLON> | <RBRACE> ) ) |
---|
588 | val=expression() |
---|
589 | { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); } |
---|
590 | ( <RBRACE> { return ins; } | <SEMICOLON> w() ) |
---|
591 | | |
---|
592 | val=readRaw() w() { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); } |
---|
593 | ) |
---|
594 | )* |
---|
595 | <RBRACE> |
---|
596 | { return ins; } |
---|
597 | } |
---|
598 | |
---|
599 | Expression expression(): |
---|
600 | { |
---|
601 | List<Expression> args = new ArrayList<Expression>(); |
---|
602 | Expression e; |
---|
603 | String op = null; |
---|
604 | } |
---|
605 | { |
---|
606 | ( |
---|
607 | <EXCLAMATION> { op = "not"; } w() e=primary() { args.add(e); } w() |
---|
608 | | |
---|
609 | <MINUS> { op = "minus"; } w() e=primary() { args.add(e); } w() |
---|
610 | | |
---|
611 | |
---|
612 | ( |
---|
613 | e=primary() { args.add(e); } w() |
---|
614 | ( |
---|
615 | ( <PLUS> { op = "plus"; } w() e=primary() { args.add(e); } w() )+ |
---|
616 | | |
---|
617 | ( <STAR> { op = "times"; } w() e=primary() { args.add(e); } w() )+ |
---|
618 | | |
---|
619 | ( <MINUS> { op = "minus"; } w() e=primary() { args.add(e); } w() )+ |
---|
620 | | |
---|
621 | ( <SLASH> { op = "divided_by"; } w() e=primary() { args.add(e); } w() )+ |
---|
622 | | |
---|
623 | <GREATER_EQUAL> { op = "greater_equal"; } w() e=primary() { args.add(e); } w() |
---|
624 | | |
---|
625 | <LESS_EQUAL> { op = "less_equal"; } w() e=primary() { args.add(e); } w() |
---|
626 | | |
---|
627 | <GREATER> { op = "greater"; } w() e=primary() { args.add(e); } w() |
---|
628 | | |
---|
629 | <EQUAL> ( <EQUAL> )? { op = "equal"; } w() e=primary() { args.add(e); } w() |
---|
630 | | |
---|
631 | <LESS> { op = "less"; } w() e=primary() { args.add(e); } w() |
---|
632 | | |
---|
633 | <AMPERSAND> <AMPERSAND> { op = "and"; } w() e=primary() { args.add(e); } w() |
---|
634 | | |
---|
635 | <PIPE> <PIPE> { op = "or"; } w() e=primary() { args.add(e); } w() |
---|
636 | | |
---|
637 | <QUESTION> { op = "cond"; } w() e=primary() { args.add(e); } w() <COLON> w() e=primary() { args.add(e); } w() |
---|
638 | )? |
---|
639 | ) |
---|
640 | ) |
---|
641 | { |
---|
642 | if (op == null) |
---|
643 | return args.get(0); |
---|
644 | return ExpressionFactory.createFunctionExpression(op, args); |
---|
645 | } |
---|
646 | } |
---|
647 | |
---|
648 | Expression primary() : |
---|
649 | { |
---|
650 | Expression nested; |
---|
651 | Expression fn; |
---|
652 | Object lit; |
---|
653 | } |
---|
654 | { |
---|
655 | LOOKAHEAD(3) // both function and identifier start with an identifier (+ optional whitespace) |
---|
656 | fn=function() { return fn; } |
---|
657 | | |
---|
658 | lit=literal() { return new LiteralExpression(lit); } |
---|
659 | | |
---|
660 | <LPAR> w() nested=expression() <RPAR> { return nested; } |
---|
661 | } |
---|
662 | |
---|
663 | Expression function() : |
---|
664 | { |
---|
665 | Token tmp; |
---|
666 | Expression arg; |
---|
667 | String name; |
---|
668 | List<Expression> args = new ArrayList<Expression>(); |
---|
669 | } |
---|
670 | { |
---|
671 | name=ident() w() |
---|
672 | <LPAR> w() |
---|
673 | ( |
---|
674 | arg=expression() { args.add(arg); } |
---|
675 | ( <COMMA> w() arg=expression() { args.add(arg); } )* |
---|
676 | )? |
---|
677 | <RPAR> |
---|
678 | { return ExpressionFactory.createFunctionExpression(name, args); } |
---|
679 | } |
---|
680 | |
---|
681 | Object literal() : |
---|
682 | { |
---|
683 | String val; |
---|
684 | Token t, t2; |
---|
685 | float f; |
---|
686 | } |
---|
687 | { |
---|
688 | LOOKAHEAD(2) |
---|
689 | t2=<IDENT> t=<HEXCOLOR> |
---|
690 | { return Main.pref.getColor("mappaint." + (sheet == null ? "MapCSS" : sheet.title) + "." + t2.image, ColorHelper.html2color(t.image)); } |
---|
691 | | |
---|
692 | t=<IDENT> { return new Keyword(t.image); } |
---|
693 | | |
---|
694 | val=string() { return val; } |
---|
695 | | |
---|
696 | <PLUS> f=ufloat() { return new Instruction.RelativeFloat(f); } |
---|
697 | | |
---|
698 | f=ufloat() { return f; } |
---|
699 | | |
---|
700 | t=<HEXCOLOR> { return ColorHelper.html2color(t.image); } |
---|
701 | } |
---|
702 | |
---|
703 | JAVACODE |
---|
704 | void error_skipto(int kind, MapCSSException me) { |
---|
705 | if (token.kind == EOF) |
---|
706 | throw new ParseException("Reached end of file while parsing"); |
---|
707 | |
---|
708 | Exception e = null; |
---|
709 | ParseException pe = generateParseException(); |
---|
710 | |
---|
711 | if (me != null) { |
---|
712 | me.setLine(pe.currentToken.next.beginLine); |
---|
713 | me.setColumn(pe.currentToken.next.beginColumn); |
---|
714 | e = me; |
---|
715 | } else { |
---|
716 | e = new ParseException(pe.getMessage()); // prevent memory leak |
---|
717 | } |
---|
718 | |
---|
719 | Main.error("Skipping to the next rule, because of an error:"); |
---|
720 | Main.error(e); |
---|
721 | if (sheet != null) { |
---|
722 | sheet.logError(e); |
---|
723 | } |
---|
724 | Token t; |
---|
725 | do { |
---|
726 | t = getNextToken(); |
---|
727 | } while (t.kind != kind && t.kind != EOF); |
---|
728 | if (t.kind == EOF) |
---|
729 | throw new ParseException("Reached end of file while parsing"); |
---|
730 | } |
---|
731 | |
---|
732 | JAVACODE |
---|
733 | /** |
---|
734 | * read everything to the next semicolon |
---|
735 | */ |
---|
736 | String readRaw() { |
---|
737 | Token t; |
---|
738 | StringBuilder s = new StringBuilder(); |
---|
739 | while (true) { |
---|
740 | t = getNextToken(); |
---|
741 | if ((t.kind == S || t.kind == STRING || t.kind == UNEXPECTED_CHAR) && |
---|
742 | t.image.contains("\n")) { |
---|
743 | ParseException e = new ParseException(String.format("Warning: end of line while reading an unquoted string at line %s column %s.", t.beginLine, t.beginColumn)); |
---|
744 | Main.error(e); |
---|
745 | if (sheet != null) { |
---|
746 | sheet.logError(e); |
---|
747 | } |
---|
748 | } |
---|
749 | if (t.kind == SEMICOLON || t.kind == EOF) |
---|
750 | break; |
---|
751 | s.append(t.image); |
---|
752 | } |
---|
753 | if (t.kind == EOF) |
---|
754 | throw new ParseException("Reached end of file while parsing"); |
---|
755 | return s.toString(); |
---|
756 | } |
---|
757 | |
---|