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