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, 10 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
RevLine 
[3848]1// License: GPL. For details, see LICENSE file.
2options {
3 STATIC = false;
[6521]4 OUTPUT_DIRECTORY = "parsergen";
[3848]5}
6
7PARSER_BEGIN(MapCSSParser)
[4252]8package org.openstreetmap.josm.gui.mappaint.mapcss.parsergen;
[3848]9
10import java.awt.Color;
11import java.util.ArrayList;
12import java.util.List;
13
[3967]14import org.openstreetmap.josm.gui.mappaint.Keyword;
[3848]15import org.openstreetmap.josm.gui.mappaint.mapcss.Condition;
[4069]16import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context;
[3848]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;
[5705]22import org.openstreetmap.josm.gui.mappaint.mapcss.ExpressionFactory;
23import org.openstreetmap.josm.gui.mappaint.mapcss.LiteralExpression;
[4069]24import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSException;
[4011]25import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector;
[3902]26import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector;
[4069]27import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.LinkSelector;
[3848]28import org.openstreetmap.josm.tools.Pair;
[5577]29import org.openstreetmap.josm.tools.Utils;
[6248]30import org.openstreetmap.josm.Main;
[3848]31
32public class MapCSSParser {
[3863]33 MapCSSStyleSource sheet;
[3848]34}
35PARSER_END(MapCSSParser)
36
37/*************
38 * Token definitions
39 */
40
41<DEFAULT>
42TOKEN:
43{
[6560]44 < SET: ("set" | "SET") >
45| < IDENT: ["a"-"z","A"-"Z","_"] ( ["a"-"z","A"-"Z","_","-","0"-"9"] )* >
[3848]46| < UINT: ["1"-"9"] ( ["0"-"9"] )* >
47| < UFLOAT: ( ["0"-"9"] )+ ( "." ( ["0"-"9"] )+ )? >
[3876]48| < STRING: "\"" ( [" ","!","#"-"[","]"-"~","\u0080"-"\uFFFF"] | "\\\"" | "\\\\" )* "\"" >
[6532]49| < #REGEX_CHAR_WITHOUT_STAR: [" "-")","+"-".","0"-"[","]"-"~","\u0080"-"\uFFFF"] | "\\/" | "\\\\" | "\\[" | "\\]" | "\\+" | "\\." | "\\'" | "\\\"" >
[3876]50| < REGEX: "/" <REGEX_CHAR_WITHOUT_STAR> ( <REGEX_CHAR_WITHOUT_STAR> | "*" )* "/" >
[3848]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: ")" >
[3867]62| < GREATER_EQUAL: ">=" >
63| < LESS_EQUAL: "<=" >
64| < GREATER: ">" >
65| < LESS: "<" >
[3848]66| < EQUAL: "=" >
67| < EXCLAMATION: "!" >
[3876]68| < TILDE: "~" >
[3848]69| < COLON: ":" >
70| < DCOLON: "::" >
71| < SEMICOLON: ";" >
72| < COMMA: "," >
[3856]73| < PIPE: "|" >
[3848]74| < PIPE_Z: "|z" >
75| < PLUS: "+" >
76| < MINUS: "-" >
[3856]77| < AMPERSAND: "&" >
78| < QUESTION: "?" >
[3876]79| < DOLLAR: "$" >
80| < CARET: "^" >
[6560]81| < FULLSTOP: "." >
[6609]82| < ELEMENT_OF: "∈" >
[6613]83| < CROSSING: "⧉" >
[3848]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
[3911]132int int_() :
133{
134 int i;
135}
136{
137 <MINUS> i=uint() { return -i; } | i=uint() { return i; }
138}
139
[3848]140float ufloat() :
141{
142 Token f;
143}
144{
145 ( f=<UFLOAT> | f=<UINT> )
146 { return Float.parseFloat(f.image); }
147}
148
[3876]149float float_() :
150{
151 float f;
152}
153{
154 <MINUS> f=ufloat() { return -f; } | f=ufloat() { return f; }
155}
156
[3848]157String string() :
158{
159 Token t;
160}
161{
162 t=<STRING>
[3876]163 { return t.image.substring(1, t.image.length() - 1).replace("\\\"", "\"").replace("\\\\", "\\"); }
[3848]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
[3876]175String regex() :
176{
177 Token t;
178}
179{
180 t=<REGEX>
181 { return t.image.substring(1, t.image.length() - 1); }
182}
183
[3848]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{
[3876]201 ( <S> | <COMMENT_START> <COMMENT_END> )*
[3848]202}
203
204/**
205 * comma delimited list of floats (at least 2, all >= 0)
206 */
[3856]207List<Float> float_array() :
[3848]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 {
[3856]219 return fs;
[3848]220 }
221}
222
223/**
224 * root
225 */
226void sheet(MapCSSStyleSource sheet):
227{
228 MapCSSRule r;
229 Token com = null;
230}
231{
[3863]232 { this.sheet = sheet; }
[3848]233 w()
[3876]234 (
235 try {
[4069]236 r=rule() { if (r != null) { sheet.rules.add(r); } } w()
237 } catch (MapCSSException mex) {
238 error_skipto(RBRACE, mex);
239 w();
[3876]240 } catch (ParseException ex) {
[4069]241 error_skipto(RBRACE, null);
[3876]242 w();
243 }
244 )*
[3848]245 <EOF>
246}
247
248MapCSSRule rule():
249{
250 List<Selector> selectors = new ArrayList<Selector>();
251 Selector sel;
252 List<Instruction> decl;
253}
254{
[3902]255 sel=child_selector() { selectors.add(sel); }
[3876]256 (
257 <COMMA> w()
[3902]258 sel=child_selector() { selectors.add(sel); }
[3876]259 )*
[3848]260 decl=declaration()
261 { return new MapCSSRule(selectors, decl); }
262}
263
[3902]264Selector child_selector() :
265{
[6607]266 Selector.ChildOrParentSelectorType type = null;
[4069]267 Condition c;
268 List<Condition> conditions = new ArrayList<Condition>();
269 Selector selLeft;
270 LinkSelector selLink = null;
271 Selector selRight = null;
[3902]272}
273{
[4069]274 selLeft=selector() w()
[3902]275 (
[6607]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 |
[6609]280 <ELEMENT_OF> { type = Selector.ChildOrParentSelectorType.ELEMENT_OF; }
[6613]281 |
282 <CROSSING> { type = Selector.ChildOrParentSelectorType.CROSSING; }
[6607]283 )
[4069]284 { selLink = new LinkSelector(conditions); }
285 w()
286 selRight=selector() w()
[3902]287 )?
[6607]288 { return selRight != null ? new ChildOrParentSelector(selLeft, selLink, selRight, type) : selLeft; }
[3902]289}
290
[3848]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() )?
[6560]302 ( ( c=condition(Context.PRIMITIVE) | c=class_or_pseudoclass(Context.PRIMITIVE) ) { conditions.add(c); } )*
[3848]303 ( sub=subpart() )?
[3902]304 { return new GeneralSelector(base.image, r, conditions, sub); }
[3848]305}
306
307Pair<Integer, Integer> zoom() :
308{
309 Integer min = 0;
310 Integer max = Integer.MAX_VALUE;
311}
312{
313 <PIPE_Z>
314 (
[3876]315 <MINUS> max=uint()
316 |
[4280]317 LOOKAHEAD(2)
318 min=uint() <MINUS> ( max=uint() )?
319 |
320 min=uint() { max = min; }
[3848]321 )
322 { return new Pair<Integer, Integer>(min, max); }
323}
324
[4069]325Condition condition(Context context) :
[3848]326{
[3856]327 Condition c;
328 Expression e;
329}
330{
[3911]331 <LSQUARE> s()
[3876]332 (
[4069]333 LOOKAHEAD( simple_key_condition(context) s() <RSQUARE> )
334 c=simple_key_condition(context) s() <RSQUARE> { return c; }
[3876]335 |
[4069]336 LOOKAHEAD( simple_key_value_condition(context) s() <RSQUARE> )
337 c=simple_key_value_condition(context) s() <RSQUARE> { return c; }
[3876]338 |
[6560]339 e=expression() <RSQUARE> { return Condition.createExpressionCondition(e, context); }
[3856]340 )
341}
342
[3902]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
[4069]354Condition simple_key_condition(Context context) :
[3856]355{
[3848]356 boolean not = false;
[6547]357 Condition.KeyMatchType matchType = null;;
[3848]358 String key;
359}
360{
361 ( <EXCLAMATION> { not = true; } )?
[6547]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; } )?
[6560]369 { return Condition.createKeyCondition(key, not, matchType, context); }
[3848]370}
371
[4069]372Condition simple_key_value_condition(Context context) :
[3856]373{
374 String key;
375 String val;
[3876]376 float f;
[3911]377 int i;
[3876]378 Condition.Op op;
[6554]379 boolean considerValAsKey = false;
[3856]380}
381{
[3911]382 key=tag_key() s()
[3876]383 (
[6554]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()
[3911]393 |
[3876]394 (
[3911]395 <EXCLAMATION> <EQUAL> { op=Condition.Op.NEQ; }
[3876]396 |
397 <EQUAL> { op=Condition.Op.EQ; }
398 |
[3912]399 <TILDE> <EQUAL> { op=Condition.Op.ONE_OF; }
[3876]400 |
[3912]401 <CARET> <EQUAL> { op=Condition.Op.BEGINS_WITH; }
[3876]402 |
[3912]403 <DOLLAR> <EQUAL> { op=Condition.Op.ENDS_WITH; }
[3876]404 |
[3912]405 <STAR> <EQUAL> { op=Condition.Op.CONTAINS; }
[3876]406 )
[3911]407 s()
[6554]408 ( <STAR> { considerValAsKey=true; } )?
[3911]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 )
[3876]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 )
[3911]427 s()
428 f=float_() { val=Float.toString(f); }
[3876]429 )
[6560]430 { return Condition.createKeyValueCondition(key, val, op, context, considerValAsKey); }
[3856]431}
432
[6560]433Condition class_or_pseudoclass(Context context) :
[3848]434{
435 Token t;
436 boolean not = false;
[6560]437 boolean pseudo;
[3848]438}
439{
[3888]440 ( <EXCLAMATION> { not = true; } )?
[6560]441 (
442 <FULLSTOP> { pseudo = false; }
443 |
444 <COLON> { pseudo = true; }
445 )
[3888]446 t=<IDENT>
[6560]447 { return pseudo
448 ? Condition.createPseudoClassCondition(t.image, not, context)
449 : Condition.createClassCondition(t.image, not, context); }
[3848]450}
451
452String subpart() :
453{
454 Token t;
455}
456{
[3876]457 <DCOLON>
[3848]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;
[6560]467 Object val = null;
[3848]468}
469{
[3876]470 <LBRACE> w()
[3848]471 (
[6560]472 (
[6561]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() )?
[6611]477 { ins.add(new Instruction.AssignmentInstruction(key.image, val == null ? true : val, true)); }
[6561]478 ( <RBRACE> { return ins; } | <SEMICOLON> w() )
[6560]479 )
[6561]480 |
[3876]481 key=<IDENT> w() <COLON> w()
482 (
483 LOOKAHEAD( float_array() w() ( <SEMICOLON> | <RBRACE> ) )
484 val=float_array()
[6611]485 { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
[3876]486 w()
487 ( <RBRACE> { return ins; } | <SEMICOLON> w() )
488 |
489 LOOKAHEAD( expression() ( <SEMICOLON> | <RBRACE> ) )
490 val=expression()
[6611]491 { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
[3876]492 ( <RBRACE> { return ins; } | <SEMICOLON> w() )
493 |
[6611]494 val=readRaw() w() { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
[3876]495 )
496 )*
497 <RBRACE>
498 { return ins; }
[3848]499}
500
501Expression expression():
502{
503 List<Expression> args = new ArrayList<Expression>();
504 Expression e;
505 String op = null;
506}
507{
508 (
[3876]509 <EXCLAMATION> { op = "not"; } w() e=primary() { args.add(e); } w()
[3867]510 |
[3876]511 <MINUS> { op = "minus"; } w() e=primary() { args.add(e); } w()
[3867]512 |
[3876]513
[3867]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 |
[3876]525 <GREATER_EQUAL> { op = "greater_equal"; } w() e=primary() { args.add(e); } w()
[3867]526 |
[3876]527 <LESS_EQUAL> { op = "less_equal"; } w() e=primary() { args.add(e); } w()
[3867]528 |
[3876]529 <GREATER> { op = "greater"; } w() e=primary() { args.add(e); } w()
[3867]530 |
[3876]531 <EQUAL> ( <EQUAL> )? { op = "equal"; } w() e=primary() { args.add(e); } w()
[3867]532 |
[3876]533 <LESS> { op = "less"; } w() e=primary() { args.add(e); } w()
[3867]534 |
[3876]535 <AMPERSAND> <AMPERSAND> { op = "and"; } w() e=primary() { args.add(e); } w()
[3867]536 |
[3876]537 <PIPE> <PIPE> { op = "or"; } w() e=primary() { args.add(e); } w()
[3867]538 |
[3876]539 <QUESTION> { op = "cond"; } w() e=primary() { args.add(e); } w() <COLON> w() e=primary() { args.add(e); } w()
[3867]540 )?
541 )
542 )
[3848]543 {
[3856]544 if (op == null)
[3848]545 return args.get(0);
[5705]546 return ExpressionFactory.createFunctionExpression(op, args);
[3848]547 }
548}
549
550Expression primary() :
551{
552 Expression nested;
[5705]553 Expression fn;
[3848]554 Object lit;
555}
556{
[3971]557 LOOKAHEAD(3) // both function and identifier start with an identifier (+ optional whitespace)
[3848]558 fn=function() { return fn; }
559 |
560 lit=literal() { return new LiteralExpression(lit); }
561 |
[3876]562 <LPAR> w() nested=expression() <RPAR> { return nested; }
[3848]563}
564
[5705]565Expression function() :
[3848]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()
[3856]575 (
576 arg=expression() { args.add(arg); }
577 ( <COMMA> w() arg=expression() { args.add(arg); } )*
578 )?
[3848]579 <RPAR>
[5705]580 { return ExpressionFactory.createFunctionExpression(name, args); }
[3848]581}
582
583Object literal() :
584{
[3967]585 String val;
[3848]586 Token t;
587 float f;
588}
589{
[3967]590 t=<IDENT> { return new Keyword(t.image); }
[3848]591 |
[3967]592 val=string() { return val; }
593 |
[3876]594 <PLUS> f=ufloat() { return new Instruction.RelativeFloat(f); }
[3848]595 |
596 f=ufloat() { return f; }
597 |
[5577]598 t=<HEXCOLOR> { return Utils.hexToColor(t.image); }
[3848]599}
600
601JAVACODE
[4069]602void error_skipto(int kind, MapCSSException me) {
[3893]603 if (token.kind == EOF)
604 throw new ParseException("Reached end of file while parsing");
[4069]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
[6248]617 Main.error("Skipping to the next rule, because of an error:");
618 Main.error(e);
[3863]619 if (sheet != null) {
[4069]620 sheet.logError(e);
[3863]621 }
622 Token t;
623 do {
624 t = getNextToken();
[3867]625 } while (t.kind != kind && t.kind != EOF);
626 if (t.kind == EOF)
627 throw new ParseException("Reached end of file while parsing");
[3848]628}
629
[3876]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();
[3893]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));
[6248]642 Main.error(e);
[3893]643 if (sheet != null) {
644 sheet.logError(e);
645 }
646 }
[3876]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.