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

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

see #9485 - MapCSS: add support for set .class_name instruction (with optional . before class), define Range.ZERO_TO_INFINITY

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