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

Last change on this file since 6609 was 6609, checked in by simon04, 10 years ago

see #9516 - MapCSS: greatly improve performance of spatial "element of" (was: "contains") expression

The current syntax is inner ∈ outer.

The order plus symbol has been changed since now the left side is matched in the end.

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