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

Last change on this file since 8087 was 8087, checked in by bastiK, 9 years ago

MapCSS: new @supports rule

Implementation should be mostly compatible with
the CSS Conditional 3 draft [2].

This is similar to @media, which was previously misused
for this purpose and is now deprecated.

refs:
[1] https://wiki.openstreetmap.org/wiki/MapCSS/0.2/Proposal_media_query
[2] http://dev.w3.org/csswg/css-conditional/

File size: 25.9 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.io.InputStream;
11import java.util.ArrayList;
12import java.util.List;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.gui.mappaint.Keyword;
16import org.openstreetmap.josm.gui.mappaint.mapcss.Condition;
17import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context;
18import org.openstreetmap.josm.gui.mappaint.mapcss.Expression;
19import org.openstreetmap.josm.gui.mappaint.mapcss.ExpressionFactory;
20import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction;
21import org.openstreetmap.josm.gui.mappaint.mapcss.LiteralExpression;
22import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSException;
23import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule;
24import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule.Declaration;
25import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
26import org.openstreetmap.josm.gui.mappaint.mapcss.Selector;
27import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector;
28import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector;
29import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.LinkSelector;
30import org.openstreetmap.josm.gui.mappaint.mapcss.Subpart;
31import org.openstreetmap.josm.tools.ColorHelper;
32import org.openstreetmap.josm.tools.Pair;
33
34/**
35 * MapCSS parser.
36 *
37 * Contains two independent grammars:
38 * (a) the preprocessor and (b) the main mapcss parser.
39 *
40 * The preprocessor handles @supports and @media syntax (@media is deprecated).
41 * Basically this allows to write one style for different versions of JOSM (or different editors).
42 * When the @supports condition is not fulfilled, it should simply skip over
43 * the whole section and not attempt to parse the possibly unknown
44 * grammar. It preserves whitespace and comments, in order to keep the
45 * line and column numbers in the error messages correct for the second pass.
46 *
47 */
48
49public class MapCSSParser {
50 MapCSSStyleSource sheet;
51 StringBuilder sb;
52 int declarationCounter;
53
54 /**
55 * Nicer way to refer to a lexical state.
56 */
57 public static enum LexicalState {
58 PREPROCESSOR(0), /* the preprocessor */
59 DEFAULT(2); /* the main parser */
60
61 int idx; // the integer, which javacc assigns to this state
62
63 LexicalState(int idx) {
64 if (!this.name().equals(MapCSSParserTokenManager.lexStateNames[idx])) {
65 throw new RuntimeException();
66 }
67 this.idx = idx;
68 }
69 };
70
71 /**
72 * Constructor which initializes the parser with a certain lexical state.
73 */
74 public MapCSSParser(InputStream in, String encoding, LexicalState initState) {
75 this(createTokenManager(in, encoding, initState));
76 declarationCounter = 0;
77 }
78
79 protected static MapCSSParserTokenManager createTokenManager(InputStream in, String encoding, LexicalState initState) {
80 SimpleCharStream scs;
81 try {
82 scs = new SimpleCharStream(in, encoding, 1, 1);
83 } catch(java.io.UnsupportedEncodingException e) {
84 throw new RuntimeException(e);
85 }
86 return new MapCSSParserTokenManager(scs, initState.idx);
87 }
88}
89PARSER_END(MapCSSParser)
90
91/*************
92 * Token definitions
93 *
94 * Lexical states for the preprocessor: <PREPROCESSOR>, <PP_COMMENT>
95 * Lexical states for the main parser: <DEFAULT>, <COMMENT>
96 */
97
98<PREPROCESSOR>
99TOKEN:
100{
101 < PP_AND: "and" >
102| < PP_OR: "or" >
103| < PP_NOT: "not" >
104| < PP_SUPPORTS: "@supports" >
105| < PP_MEDIA: "@media" >
106| < PP_NEWLINECHAR: "\n" | "\r" | "\f" >
107| < PP_WHITESPACE: " " | "\t" >
108| < PP_COMMENT_START: "/*" > : PP_COMMENT
109}
110
111<PP_COMMENT>
112TOKEN:
113{
114 < PP_COMMENT_END: "*/" > : PREPROCESSOR
115}
116
117<PP_COMMENT>
118MORE:
119{
120 < ~[] >
121}
122
123<DEFAULT>
124TOKEN [IGNORE_CASE]:
125{
126 /* Special keyword in some contexts, ordinary identifier in other contexts.
127 Use the parsing rule <code>ident()</code> to refer to a general
128 identifier, including "set". */
129 < SET: "set" >
130}
131
132<DEFAULT,PREPROCESSOR>
133TOKEN:
134{
135 < IDENT: ["a"-"z","A"-"Z","_"] ( ["a"-"z","A"-"Z","_","-","0"-"9"] )* >
136| < UINT: ["1"-"9"] ( ["0"-"9"] )* >
137| < STRING: "\"" ( [" ","!","#"-"[","]"-"~","\u0080"-"\uFFFF"] | "\\\"" | "\\\\" )* "\"" >
138| < #PREDEFINED: "\\" ["d","D","s","S","w","W","b","B","A","G","Z","z"] >
139| < #REGEX_CHAR_WITHOUT_STAR: [" "-")","+"-".","0"-"[","]"-"~","\u0080"-"\uFFFF"] | "\\/" | "\\\\" | "\\[" | "\\]" | "\\+" | "\\." | "\\'" | "\\\"" | "\\(" | "\\)" |<PREDEFINED> >
140| < REGEX: "/" <REGEX_CHAR_WITHOUT_STAR> ( <REGEX_CHAR_WITHOUT_STAR> | "*" )* "/" >
141| < LBRACE: "{" >
142| < RBRACE: "}" >
143| < LPAR: "(" >
144| < RPAR: ")" >
145| < COMMA: "," >
146| < COLON: ":" >
147}
148
149<PREPROCESSOR>
150TOKEN:
151{
152 < PP_SOMETHING_ELSE : ~[] >
153}
154
155<DEFAULT>
156TOKEN:
157{
158 < UFLOAT: ( ["0"-"9"] )+ ( "." ( ["0"-"9"] )+ )? >
159| < #H: ["0"-"9","a"-"f","A"-"F"] >
160| < HEXCOLOR: "#" ( <H><H><H><H><H><H><H><H> | <H><H><H><H><H><H> | <H><H><H> ) >
161| < S: ( " " | "\t" | "\n" | "\r" | "\f" )+ >
162| < STAR: "*" >
163| < SLASH: "/" >
164| < LSQUARE: "[" >
165| < RSQUARE: "]" >
166| < GREATER_EQUAL: ">=" >
167| < LESS_EQUAL: "<=" >
168| < GREATER: ">" >
169| < LESS: "<" >
170| < EQUAL: "=" >
171| < EXCLAMATION: "!" >
172| < TILDE: "~" >
173| < DCOLON: "::" >
174| < SEMICOLON: ";" >
175| < PIPE: "|" >
176| < PIPE_Z: "|z" >
177| < PLUS: "+" >
178| < MINUS: "-" >
179| < AMPERSAND: "&" >
180| < QUESTION: "?" >
181| < DOLLAR: "$" >
182| < CARET: "^" >
183| < FULLSTOP: "." >
184| < ELEMENT_OF: "∈" >
185| < CROSSING: "⧉" >
186| < COMMENT_START: "/*" > : COMMENT
187| < UNEXPECTED_CHAR : ~[] > // avoid TokenMgrErrors because they are hard to recover from
188}
189
190<COMMENT>
191TOKEN:
192{
193 < COMMENT_END: "*/" > : DEFAULT
194}
195
196<COMMENT>
197SKIP:
198{
199 < ~[] >
200}
201
202
203/*************
204 *
205 * Preprocessor parser definitions:
206 *
207 * <pre>
208 *
209 * {@literal @media} { ... } queries are supported, following http://www.w3.org/TR/css3-mediaqueries/#syntax
210 *
211 * media_query
212 * ___________________________|_______________________________
213 * | |
214 * {@literal @media} all and (min-josm-version: 7789) and (max-josm-version: 7790), all and (user-agent: xyz) { ... }
215 * |______________________|
216 * |
217 * media_expression
218 * </pre>
219 */
220
221
222/**
223 * root method for the preprocessor.
224 */
225String pp_root(MapCSSStyleSource sheet):
226{
227}
228{
229 { sb = new StringBuilder(); this.sheet = sheet; }
230 pp_black_box(true) <EOF>
231 { return sb.toString(); }
232}
233
234/**
235 * Parse any unknown grammar (black box).
236 *
237 * Only stop when "@media" is encountered and keep track of correct number of
238 * opening and closing curly brackets.
239 *
240 * @param write false if this content should be skipped (@pp_media condition is not fulfilled), true otherwise
241 */
242void pp_black_box(boolean write):
243{
244 Token t;
245}
246{
247 (
248 (t=<PP_AND> | t=<PP_OR> | t=<PP_NOT> | t=<UINT> | t=<STRING> | t=<REGEX> | t=<LPAR> | t=<RPAR> | t=<COMMA> | t=<COLON> | t=<IDENT> | t=<PP_SOMETHING_ELSE>) { if (write) sb.append(t.image); }
249 |
250 pp_w1()
251 |
252 pp_supports(!write)
253 |
254 pp_media(!write)
255 |
256 t=<LBRACE> { if (write) sb.append(t.image); } pp_black_box(write) t=<RBRACE> { if (write) sb.append(t.image); }
257 )*
258}
259
260/**
261 * Parses an @supports rule.
262 *
263 * @param ignore if the content of this rule should be ignored
264 * (because we are already inside a @supports block that didn't pass)
265 */
266void pp_supports(boolean ignore):
267{
268 boolean pass;
269}
270{
271 <PP_SUPPORTS> pp_w()
272 pass=pp_supports_condition()
273 <LBRACE>
274 pp_black_box(pass && !ignore)
275 <RBRACE>
276}
277
278/**
279 * Parses the condition of the @supports rule.
280 *
281 * Unlike other parsing rules, grabs trailing whitespace.
282 * @return true, if the condition is fulfilled
283 */
284boolean pp_supports_condition():
285{
286 boolean pass;
287 boolean q;
288}
289{
290 (
291 <PP_NOT> pp_w() q=pp_supports_condition_in_parens() { pass = !q; } pp_w()
292 |
293 LOOKAHEAD(pp_supports_condition_in_parens() pp_w() <PP_AND>)
294 pass=pp_supports_condition_in_parens() pp_w()
295 ( <PP_AND> pp_w() q=pp_supports_condition_in_parens() { pass = pass && q; } pp_w() )+
296 |
297 LOOKAHEAD(pp_supports_condition_in_parens() pp_w() <PP_OR>)
298 pass=pp_supports_condition_in_parens() pp_w()
299 ( <PP_OR> pp_w() q=pp_supports_condition_in_parens() { pass = pass || q; } pp_w() )+
300 |
301 pass=pp_supports_condition_in_parens() pp_w()
302 )
303 { return pass; }
304}
305
306/**
307 * Parses something in parenthesis inside the condition of the @supports rule.
308 *
309 * @return true, if the condition is fulfilled
310 */
311boolean pp_supports_condition_in_parens():
312{
313 boolean pass;
314}
315{
316 (
317 LOOKAHEAD(pp_supports_declaration_condition())
318 pass=pp_supports_declaration_condition()
319 |
320 <LPAR> pp_w() pass=pp_supports_condition() <RPAR>
321 )
322 { return pass; }
323}
324
325/**
326 * Parse an @supports declaration condition, e.&nbsp;g. a single (key:value) or (key) statement.
327 *
328 * The parsing rule {@link #literal()} from the main mapcss parser is reused here.
329 *
330 * @return true if the condition is fulfilled
331 */
332boolean pp_supports_declaration_condition():
333{
334 Token t;
335 String feature;
336 Object val = null;
337}
338{
339 <LPAR> pp_w() t=<IDENT> { feature = t.image; } pp_w() ( <COLON> pp_w() val=literal() )? <RPAR>
340 { return this.sheet.evalSupportsDeclCondition(feature, val); }
341}
342
343// deprecated
344void pp_media(boolean ignore):
345{
346 boolean pass = false;
347 boolean q;
348 boolean empty = true;
349}
350{
351 <PP_MEDIA> pp_w()
352 ( q=pp_media_query() { pass = pass || q; empty = false; }
353 ( <COMMA> pp_w() q=pp_media_query() { pass = pass || q; } )*
354 )?
355 <LBRACE>
356 pp_black_box((empty || pass) && !ignore)
357 <RBRACE>
358}
359
360// deprecated
361boolean pp_media_query():
362{
363 Token t;
364 String mediatype = "all";
365 boolean pass = true;
366 boolean invert = false;
367 boolean e;
368}
369{
370 ( <PP_NOT> { invert = true; } pp_w() )?
371 (
372 t=<IDENT> { mediatype = t.image.toLowerCase(); } pp_w()
373 ( <PP_AND> pp_w() e=pp_media_expression() { pass = pass && e; } pp_w() )*
374 |
375 e=pp_media_expression() { pass = pass && e; } pp_w()
376 ( <PP_AND> pp_w() e=pp_media_expression() { pass = pass && e; } pp_w() )*
377 )
378 {
379 if (!"all".equals(mediatype)) {
380 pass = false;
381 }
382 return invert ? (!pass) : pass;
383 }
384}
385
386/**
387 * Parse an @media expression.
388 *
389 * The parsing rule {@link #literal()} from the main mapcss parser is reused here.
390 *
391 * @return true if the condition is fulfilled
392 */
393// deprecated
394boolean pp_media_expression():
395{
396 Token t;
397 String feature;
398 Object val = null;
399}
400{
401 <LPAR> pp_w() t=<IDENT> { feature = t.image; } pp_w() ( <COLON> pp_w() val=literal() )? <RPAR>
402 { return this.sheet.evalSupportsDeclCondition(feature, val); }
403}
404
405void pp_w1():
406{
407 Token t;
408}
409{
410 t=<PP_NEWLINECHAR> { sb.append(t.image); }
411 |
412 t=<PP_WHITESPACE> { sb.append(t.image); }
413 |
414 t=<PP_COMMENT_START> { sb.append(t.image); } t=<PP_COMMENT_END> { sb.append(t.image); }
415}
416
417void pp_w():
418{
419}
420{
421 ( pp_w1() )*
422}
423
424/*************
425 *
426 * Parser definition for the main MapCSS parser:
427 *
428 * <pre>
429 *
430 * rule
431 * _______________________|______________________________
432 * | |
433 * selector declaration
434 * _________|___________________ _________|____________
435 * | | | |
436 *
437 * way|z11-12[highway=residential] { color: red; width: 3 }
438 *
439 * |_____||___________________| |_________|
440 * | | |
441 * zoom condition instruction
442 *
443 * more general:
444 *
445 * way|z13-[a=b][c=d]::subpart, way|z-3[u=v]:closed::subpart2 { p1 : val; p2 : val; }
446 *
447 * 'val' can be a literal, or an expression like "prop(width, default) + 0.8".
448 *
449 * </pre>
450 */
451
452int uint() :
453{
454 Token i;
455}
456{
457 i=<UINT> { return Integer.parseInt(i.image); }
458}
459
460int int_() :
461{
462 int i;
463}
464{
465 <MINUS> i=uint() { return -i; } | i=uint() { return i; }
466}
467
468float ufloat() :
469{
470 Token f;
471}
472{
473 ( f=<UFLOAT> | f=<UINT> )
474 { return Float.parseFloat(f.image); }
475}
476
477float float_() :
478{
479 float f;
480}
481{
482 <MINUS> f=ufloat() { return -f; } | f=ufloat() { return f; }
483}
484
485String string() :
486{
487 Token t;
488}
489{
490 t=<STRING>
491 { return t.image.substring(1, t.image.length() - 1).replace("\\\"", "\"").replace("\\\\", "\\"); }
492}
493
494String ident():
495{
496 Token t;
497 String s;
498}
499{
500 ( t=<IDENT> | t=<SET> ) { return t.image; }
501}
502
503String string_or_ident() :
504{
505 Token t;
506 String s;
507}
508{
509 ( s=ident() | s=string() ) { return s; }
510}
511
512String regex() :
513{
514 Token t;
515}
516{
517 t=<REGEX>
518 { return t.image.substring(1, t.image.length() - 1); }
519}
520
521/**
522 * white-space
523 */
524void s() :
525{
526}
527{
528 ( <S> )?
529}
530
531/**
532 * mix of white-space and comments
533 */
534void w() :
535{
536}
537{
538 ( <S> | <COMMENT_START> <COMMENT_END> )*
539}
540
541/**
542 * comma delimited list of floats (at least 2, all &gt;= 0)
543 */
544List<Float> float_array() :
545{
546 float f;
547 List<Float> fs = new ArrayList<Float>();
548}
549{
550 f=ufloat() { fs.add(f); }
551 (
552 <COMMA> s()
553 f=ufloat() { fs.add(f); }
554 )+
555 {
556 return fs;
557 }
558}
559
560/**
561 * entry point for the main parser
562 */
563void sheet(MapCSSStyleSource sheet):
564{
565}
566{
567 { this.sheet = sheet; }
568 w()
569 (
570 try {
571 rule() w()
572 } catch (MapCSSException mex) {
573 error_skipto(RBRACE, mex);
574 w();
575 } catch (ParseException ex) {
576 error_skipto(RBRACE, null);
577 w();
578 }
579 )*
580 <EOF>
581}
582
583void rule():
584{
585 List<Selector> selectors = new ArrayList<Selector>();
586 Selector sel;
587 Declaration decl;
588}
589{
590 sel=child_selector() { selectors.add(sel); }
591 (
592 <COMMA> w()
593 sel=child_selector() { selectors.add(sel); }
594 )*
595 decl=declaration()
596 {
597 for (Selector s : selectors) {
598 sheet.rules.add(new MapCSSRule(s, decl));
599 }
600 }
601}
602
603Selector child_selector() :
604{
605 Selector.ChildOrParentSelectorType type = null;
606 Condition c;
607 List<Condition> conditions = new ArrayList<Condition>();
608 Selector selLeft;
609 LinkSelector selLink = null;
610 Selector selRight = null;
611}
612{
613 selLeft=selector() w()
614 (
615 (
616 (
617 (
618 <GREATER> { type = Selector.ChildOrParentSelectorType.CHILD; }
619 |
620 <LESS> { type = Selector.ChildOrParentSelectorType.PARENT; }
621 |
622 <PLUS> { type = Selector.ChildOrParentSelectorType.SIBLING; }
623 )
624 ( ( c=condition(Context.LINK) | c=class_or_pseudoclass(Context.LINK) ) { conditions.add(c); } )*
625 |
626 <ELEMENT_OF> { type = Selector.ChildOrParentSelectorType.ELEMENT_OF; }
627 |
628 <CROSSING> { type = Selector.ChildOrParentSelectorType.CROSSING; }
629 )
630 w()
631 |
632 { /* <GREATER> is optional for child selector */ type = Selector.ChildOrParentSelectorType.CHILD; }
633 )
634 { selLink = new LinkSelector(conditions); }
635 selRight=selector() w()
636 )?
637 { return selRight != null ? new ChildOrParentSelector(selLeft, selLink, selRight, type) : selLeft; }
638}
639
640Selector selector() :
641{
642 Token base;
643 Condition c;
644 Pair<Integer, Integer> r = null;
645 List<Condition> conditions = new ArrayList<Condition>();
646 Subpart sub = null;
647}
648{
649 ( base=<IDENT> | base=<STAR> )
650 ( r=zoom() )?
651 ( ( c=condition(Context.PRIMITIVE) | c=class_or_pseudoclass(Context.PRIMITIVE) ) { conditions.add(c); } )*
652 ( sub=subpart() )?
653 { return new GeneralSelector(base.image, r, conditions, sub); }
654}
655
656Pair<Integer, Integer> zoom() :
657{
658 Integer min = 0;
659 Integer max = Integer.MAX_VALUE;
660}
661{
662 <PIPE_Z>
663 (
664 <MINUS> max=uint()
665 |
666 LOOKAHEAD(2)
667 min=uint() <MINUS> ( max=uint() )?
668 |
669 min=uint() { max = min; }
670 )
671 { return new Pair<Integer, Integer>(min, max); }
672}
673
674Condition condition(Context context) :
675{
676 Condition c;
677 Expression e;
678}
679{
680 <LSQUARE> s()
681 (
682 LOOKAHEAD( simple_key_condition(context) s() <RSQUARE> )
683 c=simple_key_condition(context) s() <RSQUARE> { return c; }
684 |
685 LOOKAHEAD( simple_key_value_condition(context) s() <RSQUARE> )
686 c=simple_key_value_condition(context) s() <RSQUARE> { return c; }
687 |
688 e=expression() <RSQUARE> { return Condition.createExpressionCondition(e, context); }
689 )
690}
691
692String tag_key() :
693{
694 String s, s2;
695 Token t;
696}
697{
698 s=string() { return s; }
699 |
700 s=ident() ( <COLON> s2=ident() { s += ':' + s2; } )* { return s; }
701}
702
703Condition simple_key_condition(Context context) :
704{
705 boolean not = false;
706 Condition.KeyMatchType matchType = null;;
707 String key;
708}
709{
710 ( <EXCLAMATION> { not = true; } )?
711 (
712 { matchType = Condition.KeyMatchType.REGEX; } key = regex()
713 |
714 key = tag_key()
715 )
716 ( LOOKAHEAD(2) <QUESTION> <EXCLAMATION> { matchType = Condition.KeyMatchType.FALSE; } )?
717 ( <QUESTION> { matchType = Condition.KeyMatchType.TRUE; } )?
718 { return Condition.createKeyCondition(key, not, matchType, context); }
719}
720
721Condition simple_key_value_condition(Context context) :
722{
723 String key;
724 String val;
725 float f;
726 int i;
727 Condition.Op op;
728 boolean considerValAsKey = false;
729}
730{
731 key=tag_key() s()
732 (
733 LOOKAHEAD(3)
734 (
735 <EQUAL> <TILDE> { op=Condition.Op.REGEX; }
736 |
737 <EXCLAMATION> <TILDE> { op=Condition.Op.NREGEX; }
738 )
739 s()
740 ( <STAR> { considerValAsKey=true; } )?
741 val=regex()
742 |
743 (
744 <EXCLAMATION> <EQUAL> { op=Condition.Op.NEQ; }
745 |
746 <EQUAL> { op=Condition.Op.EQ; }
747 |
748 <TILDE> <EQUAL> { op=Condition.Op.ONE_OF; }
749 |
750 <CARET> <EQUAL> { op=Condition.Op.BEGINS_WITH; }
751 |
752 <DOLLAR> <EQUAL> { op=Condition.Op.ENDS_WITH; }
753 |
754 <STAR> <EQUAL> { op=Condition.Op.CONTAINS; }
755 )
756 s()
757 ( <STAR> { considerValAsKey=true; } )?
758 (
759 LOOKAHEAD(2)
760 i=int_() { val=Integer.toString(i); }
761 |
762 f=float_() { val=Float.toString(f); }
763 |
764 val=string_or_ident()
765 )
766 |
767 (
768 <GREATER_EQUAL> { op=Condition.Op.GREATER_OR_EQUAL; }
769 |
770 <GREATER> { op=Condition.Op.GREATER; }
771 |
772 <LESS_EQUAL> { op=Condition.Op.LESS_OR_EQUAL; }
773 |
774 <LESS> { op=Condition.Op.LESS; }
775 )
776 s()
777 f=float_() { val=Float.toString(f); }
778 )
779 { return Condition.createKeyValueCondition(key, val, op, context, considerValAsKey); }
780}
781
782Condition class_or_pseudoclass(Context context) :
783{
784 String s;
785 boolean not = false;
786 boolean pseudo;
787}
788{
789 ( <EXCLAMATION> { not = true; } )?
790 (
791 <FULLSTOP> { pseudo = false; }
792 |
793 <COLON> { pseudo = true; }
794 )
795 s=ident()
796 { return pseudo
797 ? Condition.createPseudoClassCondition(s, not, context)
798 : Condition.createClassCondition(s, not, context); }
799}
800
801Subpart subpart() :
802{
803 String s;
804 Expression e;
805}
806{
807 <DCOLON>
808 (
809 s=ident() { return new Subpart.StringSubpart(s); }
810 |
811 <STAR> { return new Subpart.StringSubpart("*"); }
812 |
813 <LPAR> e=expression() <RPAR> { return new Subpart.ExpressionSubpart(e); }
814 )
815}
816
817Declaration declaration() :
818{
819 List<Instruction> ins = new ArrayList<Instruction>();
820 Instruction i;
821 Token key;
822 Object val = null;
823}
824{
825 <LBRACE> w()
826 (
827 (
828 <SET> w()
829 (<FULLSTOP>)? // specification allows "set .class" to set "class". we also support "set class"
830 key=<IDENT> w()
831 ( <EQUAL> val=expression() )?
832 { ins.add(new Instruction.AssignmentInstruction(key.image, val == null ? true : val, true)); }
833 ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
834 )
835 |
836 key=<IDENT> w() <COLON> w()
837 (
838 LOOKAHEAD( float_array() w() ( <SEMICOLON> | <RBRACE> ) )
839 val=float_array()
840 { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
841 w()
842 ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
843 |
844 LOOKAHEAD( expression() ( <SEMICOLON> | <RBRACE> ) )
845 val=expression()
846 { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
847 ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
848 |
849 val=readRaw() w() { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
850 )
851 )*
852 <RBRACE>
853 { return new Declaration(ins, declarationCounter++); }
854}
855
856Expression expression():
857{
858 List<Expression> args = new ArrayList<Expression>();
859 Expression e;
860 String op = null;
861}
862{
863 (
864 <EXCLAMATION> { op = "not"; } w() e=primary() { args.add(e); } w()
865 |
866 <MINUS> { op = "minus"; } w() e=primary() { args.add(e); } w()
867 |
868
869 (
870 e=primary() { args.add(e); } w()
871 (
872 ( <PLUS> { op = "plus"; } w() e=primary() { args.add(e); } w() )+
873 |
874 ( <STAR> { op = "times"; } w() e=primary() { args.add(e); } w() )+
875 |
876 ( <MINUS> { op = "minus"; } w() e=primary() { args.add(e); } w() )+
877 |
878 ( <SLASH> { op = "divided_by"; } w() e=primary() { args.add(e); } w() )+
879 |
880 <GREATER_EQUAL> { op = "greater_equal"; } w() e=primary() { args.add(e); } w()
881 |
882 <LESS_EQUAL> { op = "less_equal"; } w() e=primary() { args.add(e); } w()
883 |
884 <GREATER> { op = "greater"; } w() e=primary() { args.add(e); } w()
885 |
886 <EQUAL> ( <EQUAL> )? { op = "equal"; } w() e=primary() { args.add(e); } w()
887 |
888 <LESS> { op = "less"; } w() e=primary() { args.add(e); } w()
889 |
890 <AMPERSAND> <AMPERSAND> { op = "and"; } w() e=primary() { args.add(e); } w()
891 |
892 <PIPE> <PIPE> { op = "or"; } w() e=primary() { args.add(e); } w()
893 |
894 <QUESTION> { op = "cond"; } w() e=primary() { args.add(e); } w() <COLON> w() e=primary() { args.add(e); } w()
895 )?
896 )
897 )
898 {
899 if (op == null)
900 return args.get(0);
901 return ExpressionFactory.createFunctionExpression(op, args);
902 }
903}
904
905Expression primary() :
906{
907 Expression nested;
908 Expression fn;
909 Object lit;
910}
911{
912 LOOKAHEAD(3) // both function and identifier start with an identifier (+ optional whitespace)
913 fn=function() { return fn; }
914 |
915 lit=literal() { return new LiteralExpression(lit); }
916 |
917 <LPAR> w() nested=expression() <RPAR> { return nested; }
918}
919
920Expression function() :
921{
922 Expression arg;
923 String name;
924 List<Expression> args = new ArrayList<Expression>();
925}
926{
927 name=ident() w()
928 <LPAR> w()
929 (
930 arg=expression() { args.add(arg); }
931 ( <COMMA> w() arg=expression() { args.add(arg); } )*
932 )?
933 <RPAR>
934 { return ExpressionFactory.createFunctionExpression(name, args); }
935}
936
937Object literal() :
938{
939 String val, pref;
940 Token t;
941 float f;
942}
943{
944 LOOKAHEAD(2)
945 pref=ident() t=<HEXCOLOR>
946 { return Main.pref.getColor("mappaint." + (sheet == null ? "MapCSS" : sheet.title) + "." + pref, ColorHelper.html2color(t.image)); }
947 |
948 t=<IDENT> { return new Keyword(t.image); }
949 |
950 val=string() { return val; }
951 |
952 <PLUS> f=ufloat() { return new Instruction.RelativeFloat(f); }
953 |
954 f=ufloat() { return f; }
955 |
956 t=<HEXCOLOR> { return ColorHelper.html2color(t.image); }
957}
958
959JAVACODE
960void error_skipto(int kind, MapCSSException me) {
961 if (token.kind == EOF)
962 throw new ParseException("Reached end of file while parsing");
963
964 Exception e = null;
965 ParseException pe = generateParseException();
966
967 if (me != null) {
968 me.setLine(pe.currentToken.next.beginLine);
969 me.setColumn(pe.currentToken.next.beginColumn);
970 e = me;
971 } else {
972 e = new ParseException(pe.getMessage()); // prevent memory leak
973 }
974
975 Main.error("Skipping to the next rule, because of an error:");
976 Main.error(e);
977 if (sheet != null) {
978 sheet.logError(e);
979 }
980 Token t;
981 do {
982 t = getNextToken();
983 } while (t.kind != kind && t.kind != EOF);
984 if (t.kind == EOF)
985 throw new ParseException("Reached end of file while parsing");
986}
987
988JAVACODE
989/**
990 * read everything to the next semicolon
991 */
992String readRaw() {
993 Token t;
994 StringBuilder s = new StringBuilder();
995 while (true) {
996 t = getNextToken();
997 if ((t.kind == S || t.kind == STRING || t.kind == UNEXPECTED_CHAR) &&
998 t.image.contains("\n")) {
999 ParseException e = new ParseException(String.format("Warning: end of line while reading an unquoted string at line %s column %s.", t.beginLine, t.beginColumn));
1000 Main.error(e);
1001 if (sheet != null) {
1002 sheet.logError(e);
1003 }
1004 }
1005 if (t.kind == SEMICOLON || t.kind == EOF)
1006 break;
1007 s.append(t.image);
1008 }
1009 if (t.kind == EOF)
1010 throw new ParseException("Reached end of file while parsing");
1011 return s.toString();
1012}
1013
Note: See TracBrowser for help on using the repository browser.