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

Last change on this file since 4069 was 4069, checked in by bastiK, 14 years ago

mapcss: add role based selection (collaborative work by Gubaer and me)

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