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

Last change on this file since 6513 was 6513, checked in by simon04, 13 years ago

see #9414 - convert some tagchecker tests to MapCSS, extend MapCSS by test for false values

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