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

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

MapCSS: add regular expression support for key conditions

For instance, [/^addr:/] matches any addr:* key.

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