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

Last change on this file since 4280 was 4280, checked in by bastiK, 13 years ago

mapcss: fix zoom parsing

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.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.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 LOOKAHEAD(2)
305 min=uint() <MINUS> ( max=uint() )?
306 |
307 min=uint() { max = min; }
308 )
309 { return new Pair<Integer, Integer>(min, max); }
310}
311
312Condition condition(Context context) :
313{
314 Condition c;
315 Expression e;
316}
317{
318 <LSQUARE> s()
319 (
320 LOOKAHEAD( simple_key_condition(context) s() <RSQUARE> )
321 c=simple_key_condition(context) s() <RSQUARE> { return c; }
322 |
323 LOOKAHEAD( simple_key_value_condition(context) s() <RSQUARE> )
324 c=simple_key_value_condition(context) s() <RSQUARE> { return c; }
325 |
326 e=expression() <RSQUARE> { return Condition.create(e, context); }
327 )
328}
329
330String tag_key() :
331{
332 String s;
333 Token t;
334}
335{
336 s=string() { return s; }
337 |
338 t=<IDENT> { s = t.image; } ( <COLON> t=<IDENT> { s += ':' + t.image; } )* { return s; }
339}
340
341Condition simple_key_condition(Context context) :
342{
343 boolean not = false;
344 boolean yes = false;
345 String key;
346}
347{
348 ( <EXCLAMATION> { not = true; } )?
349 key=tag_key()
350 ( <QUESTION> { yes = true; } )?
351 { return Condition.create(key, not, yes, context); }
352}
353
354Condition simple_key_value_condition(Context context) :
355{
356 String key;
357 String val;
358 float f;
359 int i;
360 Condition.Op op;
361}
362{
363 key=tag_key() s()
364 (
365 LOOKAHEAD(2)
366 <EQUAL> <TILDE> { op=Condition.Op.REGEX; } s() val=regex()
367 |
368 (
369 <EXCLAMATION> <EQUAL> { op=Condition.Op.NEQ; }
370 |
371 <EQUAL> { op=Condition.Op.EQ; }
372 |
373 <TILDE> <EQUAL> { op=Condition.Op.ONE_OF; }
374 |
375 <CARET> <EQUAL> { op=Condition.Op.BEGINS_WITH; }
376 |
377 <DOLLAR> <EQUAL> { op=Condition.Op.ENDS_WITH; }
378 |
379 <STAR> <EQUAL> { op=Condition.Op.CONTAINS; }
380 )
381 s()
382 (
383 LOOKAHEAD(2)
384 i=int_() { val=Integer.toString(i); }
385 |
386 f=float_() { val=Float.toString(f); }
387 |
388 val=string_or_ident()
389 )
390 |
391 (
392 <GREATER_EQUAL> { op=Condition.Op.GREATER_OR_EQUAL; }
393 |
394 <GREATER> { op=Condition.Op.GREATER; }
395 |
396 <LESS_EQUAL> { op=Condition.Op.LESS_OR_EQUAL; }
397 |
398 <LESS> { op=Condition.Op.LESS; }
399 )
400 s()
401 f=float_() { val=Float.toString(f); }
402 )
403 { return Condition.create(key, val, op, context); }
404}
405
406Condition pseudoclass(Context context) :
407{
408 Token t;
409 boolean not = false;
410}
411{
412 ( <EXCLAMATION> { not = true; } )?
413 <COLON>
414 t=<IDENT>
415 { return Condition.create(t.image, not, context); }
416}
417
418String subpart() :
419{
420 Token t;
421}
422{
423 <DCOLON>
424 ( t=<IDENT> | t=<STAR> )
425 { return t.image; }
426}
427
428List<Instruction> declaration() :
429{
430 List<Instruction> ins = new ArrayList<Instruction>();
431 Instruction i;
432 Token key;
433 Object val;
434}
435{
436 <LBRACE> w()
437 (
438 key=<IDENT> w() <COLON> w()
439 (
440 LOOKAHEAD( float_array() w() ( <SEMICOLON> | <RBRACE> ) )
441 val=float_array()
442 { ins.add(new Instruction.AssignmentInstruction(key.image, val)); }
443 w()
444 ( <RBRACE> { return ins; } | <SEMICOLON> w() )
445 |
446 LOOKAHEAD( expression() ( <SEMICOLON> | <RBRACE> ) )
447 val=expression()
448 { ins.add(new Instruction.AssignmentInstruction(key.image, val)); }
449 ( <RBRACE> { return ins; } | <SEMICOLON> w() )
450 |
451 val=readRaw() w() { ins.add(new Instruction.AssignmentInstruction(key.image, val)); }
452 )
453 )*
454 <RBRACE>
455 { return ins; }
456}
457
458Expression expression():
459{
460 List<Expression> args = new ArrayList<Expression>();
461 Expression e;
462 String op = null;
463}
464{
465 (
466 <EXCLAMATION> { op = "not"; } w() e=primary() { args.add(e); } w()
467 |
468 <MINUS> { op = "minus"; } w() e=primary() { args.add(e); } w()
469 |
470
471 (
472 e=primary() { args.add(e); } w()
473 (
474 ( <PLUS> { op = "plus"; } w() e=primary() { args.add(e); } w() )+
475 |
476 ( <STAR> { op = "times"; } w() e=primary() { args.add(e); } w() )+
477 |
478 ( <MINUS> { op = "minus"; } w() e=primary() { args.add(e); } w() )+
479 |
480 ( <SLASH> { op = "divided_by"; } w() e=primary() { args.add(e); } w() )+
481 |
482 <GREATER_EQUAL> { op = "greater_equal"; } w() e=primary() { args.add(e); } w()
483 |
484 <LESS_EQUAL> { op = "less_equal"; } w() e=primary() { args.add(e); } w()
485 |
486 <GREATER> { op = "greater"; } w() e=primary() { args.add(e); } w()
487 |
488 <EQUAL> ( <EQUAL> )? { op = "equal"; } w() e=primary() { args.add(e); } w()
489 |
490 <LESS> { op = "less"; } w() e=primary() { args.add(e); } w()
491 |
492 <AMPERSAND> <AMPERSAND> { op = "and"; } w() e=primary() { args.add(e); } w()
493 |
494 <PIPE> <PIPE> { op = "or"; } w() e=primary() { args.add(e); } w()
495 |
496 <QUESTION> { op = "cond"; } w() e=primary() { args.add(e); } w() <COLON> w() e=primary() { args.add(e); } w()
497 )?
498 )
499 )
500 {
501 if (op == null)
502 return args.get(0);
503 return new FunctionExpression(op, args);
504 }
505}
506
507Expression primary() :
508{
509 Expression nested;
510 FunctionExpression fn;
511 Object lit;
512}
513{
514 LOOKAHEAD(3) // both function and identifier start with an identifier (+ optional whitespace)
515 fn=function() { return fn; }
516 |
517 lit=literal() { return new LiteralExpression(lit); }
518 |
519 <LPAR> w() nested=expression() <RPAR> { return nested; }
520}
521
522FunctionExpression function() :
523{
524 Token tmp;
525 Expression arg;
526 String name;
527 List<Expression> args = new ArrayList<Expression>();
528}
529{
530 tmp=<IDENT> { name = tmp.image; } w()
531 <LPAR> w()
532 (
533 arg=expression() { args.add(arg); }
534 ( <COMMA> w() arg=expression() { args.add(arg); } )*
535 )?
536 <RPAR>
537 { return new FunctionExpression(name, args); }
538}
539
540Object literal() :
541{
542 String val;
543 Token t;
544 float f;
545}
546{
547 t=<IDENT> { return new Keyword(t.image); }
548 |
549 val=string() { return val; }
550 |
551 <PLUS> f=ufloat() { return new Instruction.RelativeFloat(f); }
552 |
553 f=ufloat() { return f; }
554 |
555 t=<HEXCOLOR>
556 {
557 String clr = t.image.substring(1);
558 if (clr.length() == 3) {
559 clr = new String(new char[] {clr.charAt(0),clr.charAt(0),clr.charAt(1),clr.charAt(1),clr.charAt(2),clr.charAt(2)});
560 }
561 if (clr.length() != 6)
562 throw new AssertionError();
563 return new Color(Integer.parseInt(clr, 16));
564 }
565}
566
567JAVACODE
568void error_skipto(int kind, MapCSSException me) {
569 if (token.kind == EOF)
570 throw new ParseException("Reached end of file while parsing");
571
572 Exception e = null;
573 ParseException pe = generateParseException();
574
575 if (me != null) {
576 me.setLine(pe.currentToken.next.beginLine);
577 me.setColumn(pe.currentToken.next.beginColumn);
578 e = me;
579 } else {
580 e = new ParseException(pe.getMessage()); // prevent memory leak
581 }
582
583 System.err.println("Skipping to the next rule, because of an error:");
584 System.err.println(e);
585 if (sheet != null) {
586 sheet.logError(e);
587 }
588 Token t;
589 do {
590 t = getNextToken();
591 } while (t.kind != kind && t.kind != EOF);
592 if (t.kind == EOF)
593 throw new ParseException("Reached end of file while parsing");
594}
595
596JAVACODE
597/**
598 * read everything to the next semicolon
599 */
600String readRaw() {
601 Token t;
602 StringBuilder s = new StringBuilder();
603 while (true) {
604 t = getNextToken();
605 if ((t.kind == S || t.kind == STRING || t.kind == UNEXPECTED_CHAR) &&
606 t.image.contains("\n")) {
607 ParseException e = new ParseException(String.format("Warning: end of line while reading an unquoted string at line %s column %s.", t.beginLine, t.beginColumn));
608 System.err.println(e);
609 if (sheet != null) {
610 sheet.logError(e);
611 }
612 }
613 if (t.kind == SEMICOLON || t.kind == EOF)
614 break;
615 s.append(t.image);
616 }
617 if (t.kind == EOF)
618 throw new ParseException("Reached end of file while parsing");
619 return s.toString();
620}
621
Note: See TracBrowser for help on using the repository browser.