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

Last change on this file since 7761 was 7761, checked in by Don-vip, 9 years ago

fix #10806 - support Java Pattern boundary matchers in MapCSS regex - http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#bounds

File size: 23.4 KB
RevLine 
[3848]1// License: GPL. For details, see LICENSE file.
2options {
3 STATIC = false;
[6521]4 OUTPUT_DIRECTORY = "parsergen";
[3848]5}
6
7PARSER_BEGIN(MapCSSParser)
[4252]8package org.openstreetmap.josm.gui.mappaint.mapcss.parsergen;
[3848]9
[6970]10import java.io.InputStream;
[3848]11import java.util.ArrayList;
12import java.util.List;
13
[3967]14import org.openstreetmap.josm.gui.mappaint.Keyword;
[3848]15import org.openstreetmap.josm.gui.mappaint.mapcss.Condition;
[4069]16import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context;
[3848]17import org.openstreetmap.josm.gui.mappaint.mapcss.Expression;
18import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction;
19import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule;
[7064]20import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule.Declaration;
[3848]21import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
22import org.openstreetmap.josm.gui.mappaint.mapcss.Selector;
[5705]23import org.openstreetmap.josm.gui.mappaint.mapcss.ExpressionFactory;
24import org.openstreetmap.josm.gui.mappaint.mapcss.LiteralExpression;
[4069]25import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSException;
[4011]26import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector;
[3902]27import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector;
[4069]28import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.LinkSelector;
[6740]29import org.openstreetmap.josm.tools.ColorHelper;
[3848]30import org.openstreetmap.josm.tools.Pair;
[6248]31import org.openstreetmap.josm.Main;
[3848]32
[6970]33/**
34 * MapCSS parser.
[6897]35 *
[6970]36 * Contains two independent grammars:
37 * (a) the preprocessor and (b) the main mapcss parser.
[6897]38 *
[6970]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.
[6897]45 *
46 */
47
[3848]48public class MapCSSParser {
[3863]49 MapCSSStyleSource sheet;
[6970]50 StringBuilder sb;
[7064]51 int declarationCounter;
[6970]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));
[7064]75 declarationCounter = 0;
[6970]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 }
[3848]87}
88PARSER_END(MapCSSParser)
89
90/*************
91 * Token definitions
[6970]92 *
93 * Lexical states for the preprocessor: <PREPROCESSOR>, <PP_COMMENT>
94 * Lexical states for the main parser: <DEFAULT>, <COMMENT>
[3848]95 */
[6970]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}
[3848]107
[6970]108<PP_COMMENT>
109TOKEN:
110{
111 < PP_COMMENT_END: "*/" > : PREPROCESSOR
112}
113
114<PP_COMMENT>
115MORE:
116{
117 < ~[] >
118}
119
[3848]120<DEFAULT>
[6896]121TOKEN [IGNORE_CASE]:
122{
[6970]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" >
[6896]127}
128
[6970]129<DEFAULT,PREPROCESSOR>
[3848]130TOKEN:
131{
[6896]132 < IDENT: ["a"-"z","A"-"Z","_"] ( ["a"-"z","A"-"Z","_","-","0"-"9"] )* >
[3848]133| < UINT: ["1"-"9"] ( ["0"-"9"] )* >
[3876]134| < STRING: "\"" ( [" ","!","#"-"[","]"-"~","\u0080"-"\uFFFF"] | "\\\"" | "\\\\" )* "\"" >
[7761]135| < #PREDEFINED: "\\" ["d","D","s","S","w","W","b","B","A","G","Z","z"] >
[7115]136| < #REGEX_CHAR_WITHOUT_STAR: [" "-")","+"-".","0"-"[","]"-"~","\u0080"-"\uFFFF"] | "\\/" | "\\\\" | "\\[" | "\\]" | "\\+" | "\\." | "\\'" | "\\\"" | "\\(" | "\\)" |<PREDEFINED> >
[3876]137| < REGEX: "/" <REGEX_CHAR_WITHOUT_STAR> ( <REGEX_CHAR_WITHOUT_STAR> | "*" )* "/" >
[6970]138| < LBRACE: "{" >
139| < RBRACE: "}" >
140| < LPAR: "(" >
141| < RPAR: ")" >
[7277]142| < COMMA: "," >
[6970]143| < COLON: ":" >
144}
145
146<PREPROCESSOR>
147TOKEN:
148{
149 < PP_SOMETHING_ELSE : ~[] >
150}
151
152<DEFAULT>
153TOKEN:
154{
155 < UFLOAT: ( ["0"-"9"] )+ ( "." ( ["0"-"9"] )+ )? >
[3848]156| < #H: ["0"-"9","a"-"f","A"-"F"] >
[6774]157| < HEXCOLOR: "#" ( <H><H><H><H><H><H><H><H> | <H><H><H><H><H><H> | <H><H><H> ) >
[3848]158| < S: ( " " | "\t" | "\n" | "\r" | "\f" )+ >
159| < STAR: "*" >
160| < SLASH: "/" >
161| < LSQUARE: "[" >
162| < RSQUARE: "]" >
[3867]163| < GREATER_EQUAL: ">=" >
164| < LESS_EQUAL: "<=" >
165| < GREATER: ">" >
166| < LESS: "<" >
[3848]167| < EQUAL: "=" >
168| < EXCLAMATION: "!" >
[3876]169| < TILDE: "~" >
[3848]170| < DCOLON: "::" >
171| < SEMICOLON: ";" >
[3856]172| < PIPE: "|" >
[3848]173| < PIPE_Z: "|z" >
174| < PLUS: "+" >
175| < MINUS: "-" >
[3856]176| < AMPERSAND: "&" >
177| < QUESTION: "?" >
[3876]178| < DOLLAR: "$" >
179| < CARET: "^" >
[6560]180| < FULLSTOP: "." >
[6609]181| < ELEMENT_OF: "∈" >
[6613]182| < CROSSING: "⧉" >
[3848]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
[6970]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 (
[7277]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); }
[6970]246 |
247 pp_w1()
248 |
[7277]249 pp_media(!write)
[6970]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
[7277]255void pp_media(boolean ignore):
[6970]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>
[7277]267 pp_black_box((empty || pass) && !ignore)
[6970]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
[3848]361int uint() :
362{
363 Token i;
364}
365{
366 i=<UINT> { return Integer.parseInt(i.image); }
367}
368
[3911]369int int_() :
370{
371 int i;
372}
373{
374 <MINUS> i=uint() { return -i; } | i=uint() { return i; }
375}
376
[3848]377float ufloat() :
378{
379 Token f;
380}
381{
382 ( f=<UFLOAT> | f=<UINT> )
383 { return Float.parseFloat(f.image); }
384}
385
[3876]386float float_() :
387{
388 float f;
389}
390{
391 <MINUS> f=ufloat() { return -f; } | f=ufloat() { return f; }
392}
393
[3848]394String string() :
395{
396 Token t;
397}
398{
399 t=<STRING>
[3876]400 { return t.image.substring(1, t.image.length() - 1).replace("\\\"", "\"").replace("\\\\", "\\"); }
[3848]401}
402
[6896]403String ident():
404{
405 Token t;
406 String s;
407}
408{
[6970]409 ( t=<IDENT> | t=<SET> ) { return t.image; }
[6896]410}
411
[3848]412String string_or_ident() :
413{
414 Token t;
415 String s;
416}
417{
[6896]418 ( s=ident() | s=string() ) { return s; }
[3848]419}
420
[3876]421String regex() :
422{
423 Token t;
424}
425{
426 t=<REGEX>
427 { return t.image.substring(1, t.image.length() - 1); }
428}
429
[3848]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{
[3876]447 ( <S> | <COMMENT_START> <COMMENT_END> )*
[3848]448}
449
450/**
[6830]451 * comma delimited list of floats (at least 2, all &gt;= 0)
[3848]452 */
[3856]453List<Float> float_array() :
[3848]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 {
[3856]465 return fs;
[3848]466 }
467}
468
469/**
[6970]470 * entry point for the main parser
[3848]471 */
472void sheet(MapCSSStyleSource sheet):
473{
474}
475{
[3863]476 { this.sheet = sheet; }
[3848]477 w()
[3876]478 (
479 try {
[7064]480 rule() w()
[4069]481 } catch (MapCSSException mex) {
482 error_skipto(RBRACE, mex);
483 w();
[3876]484 } catch (ParseException ex) {
[4069]485 error_skipto(RBRACE, null);
[3876]486 w();
487 }
488 )*
[6970]489 <EOF>
[3848]490}
491
[7064]492void rule():
[3848]493{
494 List<Selector> selectors = new ArrayList<Selector>();
495 Selector sel;
[7064]496 Declaration decl;
[3848]497}
498{
[3902]499 sel=child_selector() { selectors.add(sel); }
[3876]500 (
501 <COMMA> w()
[3902]502 sel=child_selector() { selectors.add(sel); }
[3876]503 )*
[3848]504 decl=declaration()
[7064]505 {
506 for (Selector s : selectors) {
507 sheet.rules.add(new MapCSSRule(s, decl));
508 }
509 }
[3848]510}
511
[3902]512Selector child_selector() :
513{
[6607]514 Selector.ChildOrParentSelectorType type = null;
[4069]515 Condition c;
516 List<Condition> conditions = new ArrayList<Condition>();
517 Selector selLeft;
518 LinkSelector selLink = null;
519 Selector selRight = null;
[3902]520}
521{
[4069]522 selLeft=selector() w()
[3902]523 (
[6607]524 (
[6927]525 (
[7166]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); } )*
[6927]534 |
[7166]535 <ELEMENT_OF> { type = Selector.ChildOrParentSelectorType.ELEMENT_OF; }
[6927]536 |
[7166]537 <CROSSING> { type = Selector.ChildOrParentSelectorType.CROSSING; }
[6927]538 )
[7166]539 w()
[6607]540 |
[7166]541 { /* <GREATER> is optional for child selector */ type = Selector.ChildOrParentSelectorType.CHILD; }
[6607]542 )
[4069]543 { selLink = new LinkSelector(conditions); }
544 selRight=selector() w()
[3902]545 )?
[6607]546 { return selRight != null ? new ChildOrParentSelector(selLeft, selLink, selRight, type) : selLeft; }
[3902]547}
548
[3848]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() )?
[6560]560 ( ( c=condition(Context.PRIMITIVE) | c=class_or_pseudoclass(Context.PRIMITIVE) ) { conditions.add(c); } )*
[3848]561 ( sub=subpart() )?
[3902]562 { return new GeneralSelector(base.image, r, conditions, sub); }
[3848]563}
564
565Pair<Integer, Integer> zoom() :
566{
567 Integer min = 0;
568 Integer max = Integer.MAX_VALUE;
569}
570{
571 <PIPE_Z>
572 (
[3876]573 <MINUS> max=uint()
574 |
[4280]575 LOOKAHEAD(2)
576 min=uint() <MINUS> ( max=uint() )?
577 |
578 min=uint() { max = min; }
[3848]579 )
580 { return new Pair<Integer, Integer>(min, max); }
581}
582
[4069]583Condition condition(Context context) :
[3848]584{
[3856]585 Condition c;
586 Expression e;
587}
588{
[3911]589 <LSQUARE> s()
[3876]590 (
[4069]591 LOOKAHEAD( simple_key_condition(context) s() <RSQUARE> )
592 c=simple_key_condition(context) s() <RSQUARE> { return c; }
[3876]593 |
[4069]594 LOOKAHEAD( simple_key_value_condition(context) s() <RSQUARE> )
595 c=simple_key_value_condition(context) s() <RSQUARE> { return c; }
[3876]596 |
[6560]597 e=expression() <RSQUARE> { return Condition.createExpressionCondition(e, context); }
[3856]598 )
599}
600
[3902]601String tag_key() :
602{
[6896]603 String s, s2;
[3902]604 Token t;
605}
606{
607 s=string() { return s; }
608 |
[6896]609 s=ident() ( <COLON> s2=ident() { s += ':' + s2; } )* { return s; }
[3902]610}
611
[4069]612Condition simple_key_condition(Context context) :
[3856]613{
[3848]614 boolean not = false;
[6547]615 Condition.KeyMatchType matchType = null;;
[3848]616 String key;
617}
618{
619 ( <EXCLAMATION> { not = true; } )?
[6547]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; } )?
[6560]627 { return Condition.createKeyCondition(key, not, matchType, context); }
[3848]628}
629
[4069]630Condition simple_key_value_condition(Context context) :
[3856]631{
632 String key;
633 String val;
[3876]634 float f;
[3911]635 int i;
[3876]636 Condition.Op op;
[6554]637 boolean considerValAsKey = false;
[3856]638}
639{
[3911]640 key=tag_key() s()
[3876]641 (
[6554]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()
[3911]651 |
[3876]652 (
[3911]653 <EXCLAMATION> <EQUAL> { op=Condition.Op.NEQ; }
[3876]654 |
655 <EQUAL> { op=Condition.Op.EQ; }
656 |
[3912]657 <TILDE> <EQUAL> { op=Condition.Op.ONE_OF; }
[3876]658 |
[3912]659 <CARET> <EQUAL> { op=Condition.Op.BEGINS_WITH; }
[3876]660 |
[3912]661 <DOLLAR> <EQUAL> { op=Condition.Op.ENDS_WITH; }
[3876]662 |
[3912]663 <STAR> <EQUAL> { op=Condition.Op.CONTAINS; }
[3876]664 )
[3911]665 s()
[6554]666 ( <STAR> { considerValAsKey=true; } )?
[3911]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 )
[3876]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 )
[3911]685 s()
686 f=float_() { val=Float.toString(f); }
[3876]687 )
[6560]688 { return Condition.createKeyValueCondition(key, val, op, context, considerValAsKey); }
[3856]689}
690
[6560]691Condition class_or_pseudoclass(Context context) :
[3848]692{
[6896]693 String s;
[3848]694 boolean not = false;
[6560]695 boolean pseudo;
[3848]696}
697{
[3888]698 ( <EXCLAMATION> { not = true; } )?
[6560]699 (
700 <FULLSTOP> { pseudo = false; }
701 |
702 <COLON> { pseudo = true; }
703 )
[6896]704 s=ident()
[6560]705 { return pseudo
[6896]706 ? Condition.createPseudoClassCondition(s, not, context)
707 : Condition.createClassCondition(s, not, context); }
[3848]708}
709
710String subpart() :
711{
[6896]712 String s;
[3848]713}
714{
[3876]715 <DCOLON>
[6896]716 ( s=ident() { return s; } | <STAR> { return "*"; } )
[3848]717}
718
[7064]719Declaration declaration() :
[3848]720{
721 List<Instruction> ins = new ArrayList<Instruction>();
722 Instruction i;
723 Token key;
[6560]724 Object val = null;
[3848]725}
726{
[3876]727 <LBRACE> w()
[3848]728 (
[6560]729 (
[6561]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() )?
[6611]734 { ins.add(new Instruction.AssignmentInstruction(key.image, val == null ? true : val, true)); }
[7064]735 ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
[6560]736 )
[6561]737 |
[3876]738 key=<IDENT> w() <COLON> w()
739 (
740 LOOKAHEAD( float_array() w() ( <SEMICOLON> | <RBRACE> ) )
741 val=float_array()
[6611]742 { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
[3876]743 w()
[7064]744 ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
[3876]745 |
746 LOOKAHEAD( expression() ( <SEMICOLON> | <RBRACE> ) )
747 val=expression()
[6611]748 { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
[7064]749 ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
[3876]750 |
[6611]751 val=readRaw() w() { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
[3876]752 )
753 )*
754 <RBRACE>
[7064]755 { return new Declaration(ins, declarationCounter++); }
[3848]756}
757
758Expression expression():
759{
760 List<Expression> args = new ArrayList<Expression>();
761 Expression e;
762 String op = null;
763}
764{
765 (
[3876]766 <EXCLAMATION> { op = "not"; } w() e=primary() { args.add(e); } w()
[3867]767 |
[3876]768 <MINUS> { op = "minus"; } w() e=primary() { args.add(e); } w()
[3867]769 |
[3876]770
[3867]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 |
[3876]782 <GREATER_EQUAL> { op = "greater_equal"; } w() e=primary() { args.add(e); } w()
[3867]783 |
[3876]784 <LESS_EQUAL> { op = "less_equal"; } w() e=primary() { args.add(e); } w()
[3867]785 |
[3876]786 <GREATER> { op = "greater"; } w() e=primary() { args.add(e); } w()
[3867]787 |
[3876]788 <EQUAL> ( <EQUAL> )? { op = "equal"; } w() e=primary() { args.add(e); } w()
[3867]789 |
[3876]790 <LESS> { op = "less"; } w() e=primary() { args.add(e); } w()
[3867]791 |
[3876]792 <AMPERSAND> <AMPERSAND> { op = "and"; } w() e=primary() { args.add(e); } w()
[3867]793 |
[3876]794 <PIPE> <PIPE> { op = "or"; } w() e=primary() { args.add(e); } w()
[3867]795 |
[3876]796 <QUESTION> { op = "cond"; } w() e=primary() { args.add(e); } w() <COLON> w() e=primary() { args.add(e); } w()
[3867]797 )?
798 )
799 )
[3848]800 {
[3856]801 if (op == null)
[3848]802 return args.get(0);
[5705]803 return ExpressionFactory.createFunctionExpression(op, args);
[3848]804 }
805}
806
807Expression primary() :
808{
809 Expression nested;
[5705]810 Expression fn;
[3848]811 Object lit;
812}
813{
[3971]814 LOOKAHEAD(3) // both function and identifier start with an identifier (+ optional whitespace)
[3848]815 fn=function() { return fn; }
816 |
817 lit=literal() { return new LiteralExpression(lit); }
818 |
[3876]819 <LPAR> w() nested=expression() <RPAR> { return nested; }
[3848]820}
821
[5705]822Expression function() :
[3848]823{
824 Expression arg;
825 String name;
826 List<Expression> args = new ArrayList<Expression>();
827}
828{
[6896]829 name=ident() w()
[3848]830 <LPAR> w()
[3856]831 (
832 arg=expression() { args.add(arg); }
833 ( <COMMA> w() arg=expression() { args.add(arg); } )*
834 )?
[3848]835 <RPAR>
[5705]836 { return ExpressionFactory.createFunctionExpression(name, args); }
[3848]837}
838
839Object literal() :
840{
[6970]841 String val, pref;
842 Token t;
[3848]843 float f;
844}
845{
[6740]846 LOOKAHEAD(2)
[6970]847 pref=ident() t=<HEXCOLOR>
848 { return Main.pref.getColor("mappaint." + (sheet == null ? "MapCSS" : sheet.title) + "." + pref, ColorHelper.html2color(t.image)); }
[6740]849 |
[3967]850 t=<IDENT> { return new Keyword(t.image); }
[3848]851 |
[3967]852 val=string() { return val; }
853 |
[3876]854 <PLUS> f=ufloat() { return new Instruction.RelativeFloat(f); }
[3848]855 |
856 f=ufloat() { return f; }
857 |
[6740]858 t=<HEXCOLOR> { return ColorHelper.html2color(t.image); }
[3848]859}
860
861JAVACODE
[4069]862void error_skipto(int kind, MapCSSException me) {
[3893]863 if (token.kind == EOF)
864 throw new ParseException("Reached end of file while parsing");
[4069]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
[6248]877 Main.error("Skipping to the next rule, because of an error:");
878 Main.error(e);
[3863]879 if (sheet != null) {
[4069]880 sheet.logError(e);
[3863]881 }
882 Token t;
883 do {
884 t = getNextToken();
[3867]885 } while (t.kind != kind && t.kind != EOF);
886 if (t.kind == EOF)
887 throw new ParseException("Reached end of file while parsing");
[3848]888}
889
[3876]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();
[3893]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));
[6248]902 Main.error(e);
[3893]903 if (sheet != null) {
904 sheet.logError(e);
905 }
906 }
[3876]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.