source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParser.jj@ 6613

Last change on this file since 6613 was 6613, checked in by simon04, 13 years ago

see #9516 - MapCSS: add support for crossing polygon check (area ⧉ area), replace OverlappingAreas test by a corresponding MapCSS test

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