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

Last change on this file since 6554 was 6554, checked in by simon04, 12 years ago

see #9414 fix #9409 - extend MapCSS condition syntax to allow the comparison of two key values

The syntax is [key1 = *key2] where * is inspired by the C de-reference operator, and = stands for any of =/!=/~=/^=/$=/*=/=~/!~.

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