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

Last change on this file since 7056 was 7056, checked in by bastiK, 11 years ago

mapcss: optimization by converting very hot double loop into single loop (gain ~ 20%)

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