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

Last change on this file since 7277 was 7277, checked in by bastiK, 10 years ago

fixed #10210 - Bug combining multiple media queries

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