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

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

fix #9191 - MapCSS: Add option to include colour preferences of external styles

The syntax is the same as for the XML styles: name#123456

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