1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | options {
|
---|
3 | STATIC = false;
|
---|
4 | OUTPUT_DIRECTORY = "parsergen";
|
---|
5 | }
|
---|
6 |
|
---|
7 | PARSER_BEGIN(MapCSSParser)
|
---|
8 | package org.openstreetmap.josm.gui.mappaint.mapcss.parsergen;
|
---|
9 |
|
---|
10 | import java.io.InputStream;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.gui.mappaint.Keyword;
|
---|
15 | import org.openstreetmap.josm.gui.mappaint.mapcss.Condition;
|
---|
16 | import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context;
|
---|
17 | import org.openstreetmap.josm.gui.mappaint.mapcss.Expression;
|
---|
18 | import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction;
|
---|
19 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule;
|
---|
20 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule.Declaration;
|
---|
21 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
|
---|
22 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector;
|
---|
23 | import org.openstreetmap.josm.gui.mappaint.mapcss.ExpressionFactory;
|
---|
24 | import org.openstreetmap.josm.gui.mappaint.mapcss.LiteralExpression;
|
---|
25 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSException;
|
---|
26 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector;
|
---|
27 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector;
|
---|
28 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.LinkSelector;
|
---|
29 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
30 | import org.openstreetmap.josm.tools.Pair;
|
---|
31 | import 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 |
|
---|
48 | public 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 | }
|
---|
88 | PARSER_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>
|
---|
98 | TOKEN:
|
---|
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>
|
---|
109 | TOKEN:
|
---|
110 | {
|
---|
111 | < PP_COMMENT_END: "*/" > : PREPROCESSOR
|
---|
112 | }
|
---|
113 |
|
---|
114 | <PP_COMMENT>
|
---|
115 | MORE:
|
---|
116 | {
|
---|
117 | < ~[] >
|
---|
118 | }
|
---|
119 |
|
---|
120 | <DEFAULT>
|
---|
121 | TOKEN [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>
|
---|
130 | TOKEN:
|
---|
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 | | < COLON: ":" >
|
---|
143 | }
|
---|
144 |
|
---|
145 | <PREPROCESSOR>
|
---|
146 | TOKEN:
|
---|
147 | {
|
---|
148 | < PP_SOMETHING_ELSE : ~[] >
|
---|
149 | }
|
---|
150 |
|
---|
151 | <DEFAULT>
|
---|
152 | TOKEN:
|
---|
153 | {
|
---|
154 | < UFLOAT: ( ["0"-"9"] )+ ( "." ( ["0"-"9"] )+ )? >
|
---|
155 | | < #H: ["0"-"9","a"-"f","A"-"F"] >
|
---|
156 | | < HEXCOLOR: "#" ( <H><H><H><H><H><H><H><H> | <H><H><H><H><H><H> | <H><H><H> ) >
|
---|
157 | | < S: ( " " | "\t" | "\n" | "\r" | "\f" )+ >
|
---|
158 | | < STAR: "*" >
|
---|
159 | | < SLASH: "/" >
|
---|
160 | | < LSQUARE: "[" >
|
---|
161 | | < RSQUARE: "]" >
|
---|
162 | | < GREATER_EQUAL: ">=" >
|
---|
163 | | < LESS_EQUAL: "<=" >
|
---|
164 | | < GREATER: ">" >
|
---|
165 | | < LESS: "<" >
|
---|
166 | | < EQUAL: "=" >
|
---|
167 | | < EXCLAMATION: "!" >
|
---|
168 | | < TILDE: "~" >
|
---|
169 | | < DCOLON: "::" >
|
---|
170 | | < SEMICOLON: ";" >
|
---|
171 | | < COMMA: "," >
|
---|
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>
|
---|
188 | TOKEN:
|
---|
189 | {
|
---|
190 | < COMMENT_END: "*/" > : DEFAULT
|
---|
191 | }
|
---|
192 |
|
---|
193 | <COMMENT>
|
---|
194 | SKIP:
|
---|
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 | */
|
---|
222 | String 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 | */
|
---|
239 | void 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=<COLON> | t=<IDENT> | t=<PP_SOMETHING_ELSE>) { if (write) sb.append(t.image); }
|
---|
246 | |
|
---|
247 | pp_w1()
|
---|
248 | |
|
---|
249 | pp_media()
|
---|
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 |
|
---|
255 | void pp_media():
|
---|
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)
|
---|
268 | <RBRACE>
|
---|
269 | }
|
---|
270 |
|
---|
271 | boolean 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 | */
|
---|
303 | boolean 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 |
|
---|
314 | void 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 |
|
---|
326 | void 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 |
|
---|
361 | int uint() :
|
---|
362 | {
|
---|
363 | Token i;
|
---|
364 | }
|
---|
365 | {
|
---|
366 | i=<UINT> { return Integer.parseInt(i.image); }
|
---|
367 | }
|
---|
368 |
|
---|
369 | int int_() :
|
---|
370 | {
|
---|
371 | int i;
|
---|
372 | }
|
---|
373 | {
|
---|
374 | <MINUS> i=uint() { return -i; } | i=uint() { return i; }
|
---|
375 | }
|
---|
376 |
|
---|
377 | float ufloat() :
|
---|
378 | {
|
---|
379 | Token f;
|
---|
380 | }
|
---|
381 | {
|
---|
382 | ( f=<UFLOAT> | f=<UINT> )
|
---|
383 | { return Float.parseFloat(f.image); }
|
---|
384 | }
|
---|
385 |
|
---|
386 | float float_() :
|
---|
387 | {
|
---|
388 | float f;
|
---|
389 | }
|
---|
390 | {
|
---|
391 | <MINUS> f=ufloat() { return -f; } | f=ufloat() { return f; }
|
---|
392 | }
|
---|
393 |
|
---|
394 | String string() :
|
---|
395 | {
|
---|
396 | Token t;
|
---|
397 | }
|
---|
398 | {
|
---|
399 | t=<STRING>
|
---|
400 | { return t.image.substring(1, t.image.length() - 1).replace("\\\"", "\"").replace("\\\\", "\\"); }
|
---|
401 | }
|
---|
402 |
|
---|
403 | String ident():
|
---|
404 | {
|
---|
405 | Token t;
|
---|
406 | String s;
|
---|
407 | }
|
---|
408 | {
|
---|
409 | ( t=<IDENT> | t=<SET> ) { return t.image; }
|
---|
410 | }
|
---|
411 |
|
---|
412 | String string_or_ident() :
|
---|
413 | {
|
---|
414 | Token t;
|
---|
415 | String s;
|
---|
416 | }
|
---|
417 | {
|
---|
418 | ( s=ident() | s=string() ) { return s; }
|
---|
419 | }
|
---|
420 |
|
---|
421 | String 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 | */
|
---|
433 | void s() :
|
---|
434 | {
|
---|
435 | }
|
---|
436 | {
|
---|
437 | ( <S> )?
|
---|
438 | }
|
---|
439 |
|
---|
440 | /**
|
---|
441 | * mix of white-space and comments
|
---|
442 | */
|
---|
443 | void w() :
|
---|
444 | {
|
---|
445 | }
|
---|
446 | {
|
---|
447 | ( <S> | <COMMENT_START> <COMMENT_END> )*
|
---|
448 | }
|
---|
449 |
|
---|
450 | /**
|
---|
451 | * comma delimited list of floats (at least 2, all >= 0)
|
---|
452 | */
|
---|
453 | List<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 | */
|
---|
472 | void 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 |
|
---|
492 | void 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 |
|
---|
512 | Selector 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 | <GREATER> { type = Selector.ChildOrParentSelectorType.CHILD; }
|
---|
527 | |
|
---|
528 | <LESS> { type = Selector.ChildOrParentSelectorType.PARENT; }
|
---|
529 | |
|
---|
530 | <PLUS> { type = Selector.ChildOrParentSelectorType.SIBLING; }
|
---|
531 | )
|
---|
532 | ( ( c=condition(Context.LINK) | c=class_or_pseudoclass(Context.LINK) ) { conditions.add(c); } )*
|
---|
533 | |
|
---|
534 | <ELEMENT_OF> { type = Selector.ChildOrParentSelectorType.ELEMENT_OF; }
|
---|
535 | |
|
---|
536 | <CROSSING> { type = Selector.ChildOrParentSelectorType.CROSSING; }
|
---|
537 | )
|
---|
538 | { selLink = new LinkSelector(conditions); }
|
---|
539 | w()
|
---|
540 | selRight=selector() w()
|
---|
541 | )?
|
---|
542 | { return selRight != null ? new ChildOrParentSelector(selLeft, selLink, selRight, type) : selLeft; }
|
---|
543 | }
|
---|
544 |
|
---|
545 | Selector selector() :
|
---|
546 | {
|
---|
547 | Token base;
|
---|
548 | Condition c;
|
---|
549 | Pair<Integer, Integer> r = null;
|
---|
550 | List<Condition> conditions = new ArrayList<Condition>();
|
---|
551 | String sub = null;
|
---|
552 | }
|
---|
553 | {
|
---|
554 | ( base=<IDENT> | base=<STAR> )
|
---|
555 | ( r=zoom() )?
|
---|
556 | ( ( c=condition(Context.PRIMITIVE) | c=class_or_pseudoclass(Context.PRIMITIVE) ) { conditions.add(c); } )*
|
---|
557 | ( sub=subpart() )?
|
---|
558 | { return new GeneralSelector(base.image, r, conditions, sub); }
|
---|
559 | }
|
---|
560 |
|
---|
561 | Pair<Integer, Integer> zoom() :
|
---|
562 | {
|
---|
563 | Integer min = 0;
|
---|
564 | Integer max = Integer.MAX_VALUE;
|
---|
565 | }
|
---|
566 | {
|
---|
567 | <PIPE_Z>
|
---|
568 | (
|
---|
569 | <MINUS> max=uint()
|
---|
570 | |
|
---|
571 | LOOKAHEAD(2)
|
---|
572 | min=uint() <MINUS> ( max=uint() )?
|
---|
573 | |
|
---|
574 | min=uint() { max = min; }
|
---|
575 | )
|
---|
576 | { return new Pair<Integer, Integer>(min, max); }
|
---|
577 | }
|
---|
578 |
|
---|
579 | Condition condition(Context context) :
|
---|
580 | {
|
---|
581 | Condition c;
|
---|
582 | Expression e;
|
---|
583 | }
|
---|
584 | {
|
---|
585 | <LSQUARE> s()
|
---|
586 | (
|
---|
587 | LOOKAHEAD( simple_key_condition(context) s() <RSQUARE> )
|
---|
588 | c=simple_key_condition(context) s() <RSQUARE> { return c; }
|
---|
589 | |
|
---|
590 | LOOKAHEAD( simple_key_value_condition(context) s() <RSQUARE> )
|
---|
591 | c=simple_key_value_condition(context) s() <RSQUARE> { return c; }
|
---|
592 | |
|
---|
593 | e=expression() <RSQUARE> { return Condition.createExpressionCondition(e, context); }
|
---|
594 | )
|
---|
595 | }
|
---|
596 |
|
---|
597 | String tag_key() :
|
---|
598 | {
|
---|
599 | String s, s2;
|
---|
600 | Token t;
|
---|
601 | }
|
---|
602 | {
|
---|
603 | s=string() { return s; }
|
---|
604 | |
|
---|
605 | s=ident() ( <COLON> s2=ident() { s += ':' + s2; } )* { return s; }
|
---|
606 | }
|
---|
607 |
|
---|
608 | Condition simple_key_condition(Context context) :
|
---|
609 | {
|
---|
610 | boolean not = false;
|
---|
611 | Condition.KeyMatchType matchType = null;;
|
---|
612 | String key;
|
---|
613 | }
|
---|
614 | {
|
---|
615 | ( <EXCLAMATION> { not = true; } )?
|
---|
616 | (
|
---|
617 | { matchType = Condition.KeyMatchType.REGEX; } key = regex()
|
---|
618 | |
|
---|
619 | key = tag_key()
|
---|
620 | )
|
---|
621 | ( LOOKAHEAD(2) <QUESTION> <EXCLAMATION> { matchType = Condition.KeyMatchType.FALSE; } )?
|
---|
622 | ( <QUESTION> { matchType = Condition.KeyMatchType.TRUE; } )?
|
---|
623 | { return Condition.createKeyCondition(key, not, matchType, context); }
|
---|
624 | }
|
---|
625 |
|
---|
626 | Condition simple_key_value_condition(Context context) :
|
---|
627 | {
|
---|
628 | String key;
|
---|
629 | String val;
|
---|
630 | float f;
|
---|
631 | int i;
|
---|
632 | Condition.Op op;
|
---|
633 | boolean considerValAsKey = false;
|
---|
634 | }
|
---|
635 | {
|
---|
636 | key=tag_key() s()
|
---|
637 | (
|
---|
638 | LOOKAHEAD(3)
|
---|
639 | (
|
---|
640 | <EQUAL> <TILDE> { op=Condition.Op.REGEX; }
|
---|
641 | |
|
---|
642 | <EXCLAMATION> <TILDE> { op=Condition.Op.NREGEX; }
|
---|
643 | )
|
---|
644 | s()
|
---|
645 | ( <STAR> { considerValAsKey=true; } )?
|
---|
646 | val=regex()
|
---|
647 | |
|
---|
648 | (
|
---|
649 | <EXCLAMATION> <EQUAL> { op=Condition.Op.NEQ; }
|
---|
650 | |
|
---|
651 | <EQUAL> { op=Condition.Op.EQ; }
|
---|
652 | |
|
---|
653 | <TILDE> <EQUAL> { op=Condition.Op.ONE_OF; }
|
---|
654 | |
|
---|
655 | <CARET> <EQUAL> { op=Condition.Op.BEGINS_WITH; }
|
---|
656 | |
|
---|
657 | <DOLLAR> <EQUAL> { op=Condition.Op.ENDS_WITH; }
|
---|
658 | |
|
---|
659 | <STAR> <EQUAL> { op=Condition.Op.CONTAINS; }
|
---|
660 | )
|
---|
661 | s()
|
---|
662 | ( <STAR> { considerValAsKey=true; } )?
|
---|
663 | (
|
---|
664 | LOOKAHEAD(2)
|
---|
665 | i=int_() { val=Integer.toString(i); }
|
---|
666 | |
|
---|
667 | f=float_() { val=Float.toString(f); }
|
---|
668 | |
|
---|
669 | val=string_or_ident()
|
---|
670 | )
|
---|
671 | |
|
---|
672 | (
|
---|
673 | <GREATER_EQUAL> { op=Condition.Op.GREATER_OR_EQUAL; }
|
---|
674 | |
|
---|
675 | <GREATER> { op=Condition.Op.GREATER; }
|
---|
676 | |
|
---|
677 | <LESS_EQUAL> { op=Condition.Op.LESS_OR_EQUAL; }
|
---|
678 | |
|
---|
679 | <LESS> { op=Condition.Op.LESS; }
|
---|
680 | )
|
---|
681 | s()
|
---|
682 | f=float_() { val=Float.toString(f); }
|
---|
683 | )
|
---|
684 | { return Condition.createKeyValueCondition(key, val, op, context, considerValAsKey); }
|
---|
685 | }
|
---|
686 |
|
---|
687 | Condition class_or_pseudoclass(Context context) :
|
---|
688 | {
|
---|
689 | String s;
|
---|
690 | boolean not = false;
|
---|
691 | boolean pseudo;
|
---|
692 | }
|
---|
693 | {
|
---|
694 | ( <EXCLAMATION> { not = true; } )?
|
---|
695 | (
|
---|
696 | <FULLSTOP> { pseudo = false; }
|
---|
697 | |
|
---|
698 | <COLON> { pseudo = true; }
|
---|
699 | )
|
---|
700 | s=ident()
|
---|
701 | { return pseudo
|
---|
702 | ? Condition.createPseudoClassCondition(s, not, context)
|
---|
703 | : Condition.createClassCondition(s, not, context); }
|
---|
704 | }
|
---|
705 |
|
---|
706 | String subpart() :
|
---|
707 | {
|
---|
708 | String s;
|
---|
709 | }
|
---|
710 | {
|
---|
711 | <DCOLON>
|
---|
712 | ( s=ident() { return s; } | <STAR> { return "*"; } )
|
---|
713 | }
|
---|
714 |
|
---|
715 | Declaration declaration() :
|
---|
716 | {
|
---|
717 | List<Instruction> ins = new ArrayList<Instruction>();
|
---|
718 | Instruction i;
|
---|
719 | Token key;
|
---|
720 | Object val = null;
|
---|
721 | }
|
---|
722 | {
|
---|
723 | <LBRACE> w()
|
---|
724 | (
|
---|
725 | (
|
---|
726 | <SET> w()
|
---|
727 | (<FULLSTOP>)? // specification allows "set .class" to set "class". we also support "set class"
|
---|
728 | key=<IDENT> w()
|
---|
729 | ( <EQUAL> val=expression() )?
|
---|
730 | { ins.add(new Instruction.AssignmentInstruction(key.image, val == null ? true : val, true)); }
|
---|
731 | ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
|
---|
732 | )
|
---|
733 | |
|
---|
734 | key=<IDENT> w() <COLON> w()
|
---|
735 | (
|
---|
736 | LOOKAHEAD( float_array() w() ( <SEMICOLON> | <RBRACE> ) )
|
---|
737 | val=float_array()
|
---|
738 | { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
|
---|
739 | w()
|
---|
740 | ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
|
---|
741 | |
|
---|
742 | LOOKAHEAD( expression() ( <SEMICOLON> | <RBRACE> ) )
|
---|
743 | val=expression()
|
---|
744 | { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
|
---|
745 | ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
|
---|
746 | |
|
---|
747 | val=readRaw() w() { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
|
---|
748 | )
|
---|
749 | )*
|
---|
750 | <RBRACE>
|
---|
751 | { return new Declaration(ins, declarationCounter++); }
|
---|
752 | }
|
---|
753 |
|
---|
754 | Expression expression():
|
---|
755 | {
|
---|
756 | List<Expression> args = new ArrayList<Expression>();
|
---|
757 | Expression e;
|
---|
758 | String op = null;
|
---|
759 | }
|
---|
760 | {
|
---|
761 | (
|
---|
762 | <EXCLAMATION> { op = "not"; } w() e=primary() { args.add(e); } w()
|
---|
763 | |
|
---|
764 | <MINUS> { op = "minus"; } w() e=primary() { args.add(e); } w()
|
---|
765 | |
|
---|
766 |
|
---|
767 | (
|
---|
768 | e=primary() { args.add(e); } w()
|
---|
769 | (
|
---|
770 | ( <PLUS> { op = "plus"; } w() e=primary() { args.add(e); } w() )+
|
---|
771 | |
|
---|
772 | ( <STAR> { op = "times"; } w() e=primary() { args.add(e); } w() )+
|
---|
773 | |
|
---|
774 | ( <MINUS> { op = "minus"; } w() e=primary() { args.add(e); } w() )+
|
---|
775 | |
|
---|
776 | ( <SLASH> { op = "divided_by"; } w() e=primary() { args.add(e); } w() )+
|
---|
777 | |
|
---|
778 | <GREATER_EQUAL> { op = "greater_equal"; } w() e=primary() { args.add(e); } w()
|
---|
779 | |
|
---|
780 | <LESS_EQUAL> { op = "less_equal"; } w() e=primary() { args.add(e); } w()
|
---|
781 | |
|
---|
782 | <GREATER> { op = "greater"; } w() e=primary() { args.add(e); } w()
|
---|
783 | |
|
---|
784 | <EQUAL> ( <EQUAL> )? { op = "equal"; } w() e=primary() { args.add(e); } w()
|
---|
785 | |
|
---|
786 | <LESS> { op = "less"; } w() e=primary() { args.add(e); } w()
|
---|
787 | |
|
---|
788 | <AMPERSAND> <AMPERSAND> { op = "and"; } w() e=primary() { args.add(e); } w()
|
---|
789 | |
|
---|
790 | <PIPE> <PIPE> { op = "or"; } w() e=primary() { args.add(e); } w()
|
---|
791 | |
|
---|
792 | <QUESTION> { op = "cond"; } w() e=primary() { args.add(e); } w() <COLON> w() e=primary() { args.add(e); } w()
|
---|
793 | )?
|
---|
794 | )
|
---|
795 | )
|
---|
796 | {
|
---|
797 | if (op == null)
|
---|
798 | return args.get(0);
|
---|
799 | return ExpressionFactory.createFunctionExpression(op, args);
|
---|
800 | }
|
---|
801 | }
|
---|
802 |
|
---|
803 | Expression primary() :
|
---|
804 | {
|
---|
805 | Expression nested;
|
---|
806 | Expression fn;
|
---|
807 | Object lit;
|
---|
808 | }
|
---|
809 | {
|
---|
810 | LOOKAHEAD(3) // both function and identifier start with an identifier (+ optional whitespace)
|
---|
811 | fn=function() { return fn; }
|
---|
812 | |
|
---|
813 | lit=literal() { return new LiteralExpression(lit); }
|
---|
814 | |
|
---|
815 | <LPAR> w() nested=expression() <RPAR> { return nested; }
|
---|
816 | }
|
---|
817 |
|
---|
818 | Expression function() :
|
---|
819 | {
|
---|
820 | Expression arg;
|
---|
821 | String name;
|
---|
822 | List<Expression> args = new ArrayList<Expression>();
|
---|
823 | }
|
---|
824 | {
|
---|
825 | name=ident() w()
|
---|
826 | <LPAR> w()
|
---|
827 | (
|
---|
828 | arg=expression() { args.add(arg); }
|
---|
829 | ( <COMMA> w() arg=expression() { args.add(arg); } )*
|
---|
830 | )?
|
---|
831 | <RPAR>
|
---|
832 | { return ExpressionFactory.createFunctionExpression(name, args); }
|
---|
833 | }
|
---|
834 |
|
---|
835 | Object literal() :
|
---|
836 | {
|
---|
837 | String val, pref;
|
---|
838 | Token t;
|
---|
839 | float f;
|
---|
840 | }
|
---|
841 | {
|
---|
842 | LOOKAHEAD(2)
|
---|
843 | pref=ident() t=<HEXCOLOR>
|
---|
844 | { return Main.pref.getColor("mappaint." + (sheet == null ? "MapCSS" : sheet.title) + "." + pref, ColorHelper.html2color(t.image)); }
|
---|
845 | |
|
---|
846 | t=<IDENT> { return new Keyword(t.image); }
|
---|
847 | |
|
---|
848 | val=string() { return val; }
|
---|
849 | |
|
---|
850 | <PLUS> f=ufloat() { return new Instruction.RelativeFloat(f); }
|
---|
851 | |
|
---|
852 | f=ufloat() { return f; }
|
---|
853 | |
|
---|
854 | t=<HEXCOLOR> { return ColorHelper.html2color(t.image); }
|
---|
855 | }
|
---|
856 |
|
---|
857 | JAVACODE
|
---|
858 | void error_skipto(int kind, MapCSSException me) {
|
---|
859 | if (token.kind == EOF)
|
---|
860 | throw new ParseException("Reached end of file while parsing");
|
---|
861 |
|
---|
862 | Exception e = null;
|
---|
863 | ParseException pe = generateParseException();
|
---|
864 |
|
---|
865 | if (me != null) {
|
---|
866 | me.setLine(pe.currentToken.next.beginLine);
|
---|
867 | me.setColumn(pe.currentToken.next.beginColumn);
|
---|
868 | e = me;
|
---|
869 | } else {
|
---|
870 | e = new ParseException(pe.getMessage()); // prevent memory leak
|
---|
871 | }
|
---|
872 |
|
---|
873 | Main.error("Skipping to the next rule, because of an error:");
|
---|
874 | Main.error(e);
|
---|
875 | if (sheet != null) {
|
---|
876 | sheet.logError(e);
|
---|
877 | }
|
---|
878 | Token t;
|
---|
879 | do {
|
---|
880 | t = getNextToken();
|
---|
881 | } while (t.kind != kind && t.kind != EOF);
|
---|
882 | if (t.kind == EOF)
|
---|
883 | throw new ParseException("Reached end of file while parsing");
|
---|
884 | }
|
---|
885 |
|
---|
886 | JAVACODE
|
---|
887 | /**
|
---|
888 | * read everything to the next semicolon
|
---|
889 | */
|
---|
890 | String readRaw() {
|
---|
891 | Token t;
|
---|
892 | StringBuilder s = new StringBuilder();
|
---|
893 | while (true) {
|
---|
894 | t = getNextToken();
|
---|
895 | if ((t.kind == S || t.kind == STRING || t.kind == UNEXPECTED_CHAR) &&
|
---|
896 | t.image.contains("\n")) {
|
---|
897 | ParseException e = new ParseException(String.format("Warning: end of line while reading an unquoted string at line %s column %s.", t.beginLine, t.beginColumn));
|
---|
898 | Main.error(e);
|
---|
899 | if (sheet != null) {
|
---|
900 | sheet.logError(e);
|
---|
901 | }
|
---|
902 | }
|
---|
903 | if (t.kind == SEMICOLON || t.kind == EOF)
|
---|
904 | break;
|
---|
905 | s.append(t.image);
|
---|
906 | }
|
---|
907 | if (t.kind == EOF)
|
---|
908 | throw new ParseException("Reached end of file while parsing");
|
---|
909 | return s.toString();
|
---|
910 | }
|
---|
911 |
|
---|