[3848] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | options {
|
---|
| 3 | STATIC = false;
|
---|
[6521] | 4 | OUTPUT_DIRECTORY = "parsergen";
|
---|
[3848] | 5 | }
|
---|
| 6 |
|
---|
| 7 | PARSER_BEGIN(MapCSSParser)
|
---|
[4252] | 8 | package org.openstreetmap.josm.gui.mappaint.mapcss.parsergen;
|
---|
[3848] | 9 |
|
---|
[6970] | 10 | import java.io.InputStream;
|
---|
[8331] | 11 | import java.io.Reader;
|
---|
[3848] | 12 | import java.util.ArrayList;
|
---|
[8256] | 13 | import java.util.Arrays;
|
---|
| 14 | import java.util.Collections;
|
---|
[3848] | 15 | import java.util.List;
|
---|
[8404] | 16 | import java.util.Locale;
|
---|
[3848] | 17 |
|
---|
[8086] | 18 | import org.openstreetmap.josm.Main;
|
---|
[3967] | 19 | import org.openstreetmap.josm.gui.mappaint.Keyword;
|
---|
[3848] | 20 | import org.openstreetmap.josm.gui.mappaint.mapcss.Condition;
|
---|
[4069] | 21 | import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context;
|
---|
[3848] | 22 | import org.openstreetmap.josm.gui.mappaint.mapcss.Expression;
|
---|
[8086] | 23 | import org.openstreetmap.josm.gui.mappaint.mapcss.ExpressionFactory;
|
---|
[8260] | 24 | import org.openstreetmap.josm.gui.mappaint.mapcss.ExpressionFactory.NullExpression;
|
---|
[3848] | 25 | import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction;
|
---|
[8086] | 26 | import org.openstreetmap.josm.gui.mappaint.mapcss.LiteralExpression;
|
---|
| 27 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSException;
|
---|
[3848] | 28 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule;
|
---|
[7064] | 29 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule.Declaration;
|
---|
[3848] | 30 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
|
---|
| 31 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector;
|
---|
[4011] | 32 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector;
|
---|
[3902] | 33 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector;
|
---|
[4069] | 34 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.LinkSelector;
|
---|
[8086] | 35 | import org.openstreetmap.josm.gui.mappaint.mapcss.Subpart;
|
---|
[6740] | 36 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
[3848] | 37 | import org.openstreetmap.josm.tools.Pair;
|
---|
[8515] | 38 | import org.openstreetmap.josm.tools.Utils;
|
---|
[3848] | 39 |
|
---|
[6970] | 40 | /**
|
---|
| 41 | * MapCSS parser.
|
---|
[6897] | 42 | *
|
---|
[6970] | 43 | * Contains two independent grammars:
|
---|
| 44 | * (a) the preprocessor and (b) the main mapcss parser.
|
---|
[6897] | 45 | *
|
---|
[8087] | 46 | * The preprocessor handles @supports and @media syntax (@media is deprecated).
|
---|
| 47 | * Basically this allows to write one style for different versions of JOSM (or different editors).
|
---|
| 48 | * When the @supports condition is not fulfilled, it should simply skip over
|
---|
[6970] | 49 | * the whole section and not attempt to parse the possibly unknown
|
---|
| 50 | * grammar. It preserves whitespace and comments, in order to keep the
|
---|
| 51 | * line and column numbers in the error messages correct for the second pass.
|
---|
[6897] | 52 | *
|
---|
| 53 | */
|
---|
| 54 |
|
---|
[3848] | 55 | public class MapCSSParser {
|
---|
[3863] | 56 | MapCSSStyleSource sheet;
|
---|
[6970] | 57 | StringBuilder sb;
|
---|
[7064] | 58 | int declarationCounter;
|
---|
[6970] | 59 |
|
---|
| 60 | /**
|
---|
| 61 | * Nicer way to refer to a lexical state.
|
---|
| 62 | */
|
---|
| 63 | public static enum LexicalState {
|
---|
| 64 | PREPROCESSOR(0), /* the preprocessor */
|
---|
| 65 | DEFAULT(2); /* the main parser */
|
---|
| 66 |
|
---|
| 67 | int idx; // the integer, which javacc assigns to this state
|
---|
| 68 |
|
---|
| 69 | LexicalState(int idx) {
|
---|
| 70 | if (!this.name().equals(MapCSSParserTokenManager.lexStateNames[idx])) {
|
---|
| 71 | throw new RuntimeException();
|
---|
| 72 | }
|
---|
| 73 | this.idx = idx;
|
---|
| 74 | }
|
---|
| 75 | };
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | * Constructor which initializes the parser with a certain lexical state.
|
---|
| 79 | */
|
---|
| 80 | public MapCSSParser(InputStream in, String encoding, LexicalState initState) {
|
---|
| 81 | this(createTokenManager(in, encoding, initState));
|
---|
[7064] | 82 | declarationCounter = 0;
|
---|
[6970] | 83 | }
|
---|
| 84 |
|
---|
| 85 | protected static MapCSSParserTokenManager createTokenManager(InputStream in, String encoding, LexicalState initState) {
|
---|
| 86 | SimpleCharStream scs;
|
---|
| 87 | try {
|
---|
| 88 | scs = new SimpleCharStream(in, encoding, 1, 1);
|
---|
[8510] | 89 | } catch (java.io.UnsupportedEncodingException e) {
|
---|
[6970] | 90 | throw new RuntimeException(e);
|
---|
| 91 | }
|
---|
| 92 | return new MapCSSParserTokenManager(scs, initState.idx);
|
---|
| 93 | }
|
---|
[8331] | 94 |
|
---|
| 95 | /**
|
---|
| 96 | * Constructor which initializes the parser with a certain lexical state.
|
---|
| 97 | */
|
---|
| 98 | public MapCSSParser(Reader in, LexicalState initState) {
|
---|
| 99 | this(createTokenManager(in, initState));
|
---|
| 100 | declarationCounter = 0;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | protected static MapCSSParserTokenManager createTokenManager(Reader in, LexicalState initState) {
|
---|
| 104 | final SimpleCharStream scs = new SimpleCharStream(in, 1, 1);
|
---|
| 105 | return new MapCSSParserTokenManager(scs, initState.idx);
|
---|
| 106 | }
|
---|
[3848] | 107 | }
|
---|
| 108 | PARSER_END(MapCSSParser)
|
---|
| 109 |
|
---|
| 110 | /*************
|
---|
| 111 | * Token definitions
|
---|
[6970] | 112 | *
|
---|
| 113 | * Lexical states for the preprocessor: <PREPROCESSOR>, <PP_COMMENT>
|
---|
| 114 | * Lexical states for the main parser: <DEFAULT>, <COMMENT>
|
---|
[3848] | 115 | */
|
---|
[6970] | 116 |
|
---|
| 117 | <PREPROCESSOR>
|
---|
| 118 | TOKEN:
|
---|
| 119 | {
|
---|
| 120 | < PP_AND: "and" >
|
---|
[8087] | 121 | | < PP_OR: "or" >
|
---|
[6970] | 122 | | < PP_NOT: "not" >
|
---|
[8087] | 123 | | < PP_SUPPORTS: "@supports" >
|
---|
[6970] | 124 | | < PP_MEDIA: "@media" >
|
---|
| 125 | | < PP_NEWLINECHAR: "\n" | "\r" | "\f" >
|
---|
| 126 | | < PP_WHITESPACE: " " | "\t" >
|
---|
| 127 | | < PP_COMMENT_START: "/*" > : PP_COMMENT
|
---|
| 128 | }
|
---|
[3848] | 129 |
|
---|
[6970] | 130 | <PP_COMMENT>
|
---|
| 131 | TOKEN:
|
---|
| 132 | {
|
---|
| 133 | < PP_COMMENT_END: "*/" > : PREPROCESSOR
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | <PP_COMMENT>
|
---|
| 137 | MORE:
|
---|
| 138 | {
|
---|
| 139 | < ~[] >
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[3848] | 142 | <DEFAULT>
|
---|
[6896] | 143 | TOKEN [IGNORE_CASE]:
|
---|
| 144 | {
|
---|
[6970] | 145 | /* Special keyword in some contexts, ordinary identifier in other contexts.
|
---|
| 146 | Use the parsing rule <code>ident()</code> to refer to a general
|
---|
| 147 | identifier, including "set". */
|
---|
| 148 | < SET: "set" >
|
---|
[6896] | 149 | }
|
---|
| 150 |
|
---|
[6970] | 151 | <DEFAULT,PREPROCESSOR>
|
---|
[3848] | 152 | TOKEN:
|
---|
| 153 | {
|
---|
[6896] | 154 | < IDENT: ["a"-"z","A"-"Z","_"] ( ["a"-"z","A"-"Z","_","-","0"-"9"] )* >
|
---|
[3848] | 155 | | < UINT: ["1"-"9"] ( ["0"-"9"] )* >
|
---|
[3876] | 156 | | < STRING: "\"" ( [" ","!","#"-"[","]"-"~","\u0080"-"\uFFFF"] | "\\\"" | "\\\\" )* "\"" >
|
---|
[7761] | 157 | | < #PREDEFINED: "\\" ["d","D","s","S","w","W","b","B","A","G","Z","z"] >
|
---|
[8535] | 158 | | < #REGEX_CHAR_WITHOUT_STAR: [" "-")","+"-".","0"-"[","]"-"~","\u0080"-"\uFFFF"] | "\\/" | "\\\\" | "\\[" | "\\]" | "\\+" | "\\." | "\\'" | "\\\"" | "\\(" | "\\)" | "\\{" | "\\}" | "\\?" | "\\*" | "\\^" | "\\$" |<PREDEFINED> >
|
---|
[3876] | 159 | | < REGEX: "/" <REGEX_CHAR_WITHOUT_STAR> ( <REGEX_CHAR_WITHOUT_STAR> | "*" )* "/" >
|
---|
[6970] | 160 | | < LBRACE: "{" >
|
---|
| 161 | | < RBRACE: "}" >
|
---|
| 162 | | < LPAR: "(" >
|
---|
| 163 | | < RPAR: ")" >
|
---|
[7277] | 164 | | < COMMA: "," >
|
---|
[6970] | 165 | | < COLON: ":" >
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | <PREPROCESSOR>
|
---|
| 169 | TOKEN:
|
---|
| 170 | {
|
---|
| 171 | < PP_SOMETHING_ELSE : ~[] >
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | <DEFAULT>
|
---|
| 175 | TOKEN:
|
---|
| 176 | {
|
---|
| 177 | < UFLOAT: ( ["0"-"9"] )+ ( "." ( ["0"-"9"] )+ )? >
|
---|
[3848] | 178 | | < #H: ["0"-"9","a"-"f","A"-"F"] >
|
---|
[6774] | 179 | | < HEXCOLOR: "#" ( <H><H><H><H><H><H><H><H> | <H><H><H><H><H><H> | <H><H><H> ) >
|
---|
[3848] | 180 | | < S: ( " " | "\t" | "\n" | "\r" | "\f" )+ >
|
---|
| 181 | | < STAR: "*" >
|
---|
| 182 | | < SLASH: "/" >
|
---|
| 183 | | < LSQUARE: "[" >
|
---|
| 184 | | < RSQUARE: "]" >
|
---|
[3867] | 185 | | < GREATER_EQUAL: ">=" >
|
---|
| 186 | | < LESS_EQUAL: "<=" >
|
---|
| 187 | | < GREATER: ">" >
|
---|
| 188 | | < LESS: "<" >
|
---|
[3848] | 189 | | < EQUAL: "=" >
|
---|
| 190 | | < EXCLAMATION: "!" >
|
---|
[3876] | 191 | | < TILDE: "~" >
|
---|
[3848] | 192 | | < DCOLON: "::" >
|
---|
| 193 | | < SEMICOLON: ";" >
|
---|
[3856] | 194 | | < PIPE: "|" >
|
---|
[3848] | 195 | | < PIPE_Z: "|z" >
|
---|
| 196 | | < PLUS: "+" >
|
---|
| 197 | | < MINUS: "-" >
|
---|
[3856] | 198 | | < AMPERSAND: "&" >
|
---|
| 199 | | < QUESTION: "?" >
|
---|
[3876] | 200 | | < DOLLAR: "$" >
|
---|
| 201 | | < CARET: "^" >
|
---|
[6560] | 202 | | < FULLSTOP: "." >
|
---|
[8260] | 203 | | < DEG: "°" >
|
---|
[6609] | 204 | | < ELEMENT_OF: "∈" >
|
---|
[6613] | 205 | | < CROSSING: "⧉" >
|
---|
[3848] | 206 | | < COMMENT_START: "/*" > : COMMENT
|
---|
| 207 | | < UNEXPECTED_CHAR : ~[] > // avoid TokenMgrErrors because they are hard to recover from
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | <COMMENT>
|
---|
| 211 | TOKEN:
|
---|
| 212 | {
|
---|
| 213 | < COMMENT_END: "*/" > : DEFAULT
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | <COMMENT>
|
---|
| 217 | SKIP:
|
---|
| 218 | {
|
---|
| 219 | < ~[] >
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[6970] | 222 |
|
---|
| 223 | /*************
|
---|
| 224 | *
|
---|
| 225 | * Preprocessor parser definitions:
|
---|
| 226 | *
|
---|
| 227 | * <pre>
|
---|
| 228 | *
|
---|
| 229 | * {@literal @media} { ... } queries are supported, following http://www.w3.org/TR/css3-mediaqueries/#syntax
|
---|
| 230 | *
|
---|
| 231 | * media_query
|
---|
| 232 | * ___________________________|_______________________________
|
---|
| 233 | * | |
|
---|
| 234 | * {@literal @media} all and (min-josm-version: 7789) and (max-josm-version: 7790), all and (user-agent: xyz) { ... }
|
---|
| 235 | * |______________________|
|
---|
| 236 | * |
|
---|
| 237 | * media_expression
|
---|
| 238 | * </pre>
|
---|
| 239 | */
|
---|
| 240 |
|
---|
| 241 |
|
---|
| 242 | /**
|
---|
| 243 | * root method for the preprocessor.
|
---|
| 244 | */
|
---|
| 245 | String pp_root(MapCSSStyleSource sheet):
|
---|
| 246 | {
|
---|
| 247 | }
|
---|
| 248 | {
|
---|
| 249 | { sb = new StringBuilder(); this.sheet = sheet; }
|
---|
| 250 | pp_black_box(true) <EOF>
|
---|
| 251 | { return sb.toString(); }
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | /**
|
---|
| 255 | * Parse any unknown grammar (black box).
|
---|
| 256 | *
|
---|
| 257 | * Only stop when "@media" is encountered and keep track of correct number of
|
---|
| 258 | * opening and closing curly brackets.
|
---|
| 259 | *
|
---|
| 260 | * @param write false if this content should be skipped (@pp_media condition is not fulfilled), true otherwise
|
---|
| 261 | */
|
---|
| 262 | void pp_black_box(boolean write):
|
---|
| 263 | {
|
---|
| 264 | Token t;
|
---|
| 265 | }
|
---|
| 266 | {
|
---|
| 267 | (
|
---|
[8087] | 268 | (t=<PP_AND> | t=<PP_OR> | 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] | 269 | |
|
---|
| 270 | pp_w1()
|
---|
| 271 | |
|
---|
[8087] | 272 | pp_supports(!write)
|
---|
| 273 | |
|
---|
[7277] | 274 | pp_media(!write)
|
---|
[6970] | 275 | |
|
---|
| 276 | t=<LBRACE> { if (write) sb.append(t.image); } pp_black_box(write) t=<RBRACE> { if (write) sb.append(t.image); }
|
---|
| 277 | )*
|
---|
| 278 | }
|
---|
| 279 |
|
---|
[8087] | 280 | /**
|
---|
| 281 | * Parses an @supports rule.
|
---|
| 282 | *
|
---|
| 283 | * @param ignore if the content of this rule should be ignored
|
---|
| 284 | * (because we are already inside a @supports block that didn't pass)
|
---|
| 285 | */
|
---|
| 286 | void pp_supports(boolean ignore):
|
---|
| 287 | {
|
---|
| 288 | boolean pass;
|
---|
| 289 | }
|
---|
| 290 | {
|
---|
| 291 | <PP_SUPPORTS> pp_w()
|
---|
| 292 | pass=pp_supports_condition()
|
---|
| 293 | <LBRACE>
|
---|
| 294 | pp_black_box(pass && !ignore)
|
---|
| 295 | <RBRACE>
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | /**
|
---|
| 299 | * Parses the condition of the @supports rule.
|
---|
| 300 | *
|
---|
| 301 | * Unlike other parsing rules, grabs trailing whitespace.
|
---|
| 302 | * @return true, if the condition is fulfilled
|
---|
| 303 | */
|
---|
| 304 | boolean pp_supports_condition():
|
---|
| 305 | {
|
---|
| 306 | boolean pass;
|
---|
| 307 | boolean q;
|
---|
| 308 | }
|
---|
| 309 | {
|
---|
| 310 | (
|
---|
| 311 | <PP_NOT> pp_w() q=pp_supports_condition_in_parens() { pass = !q; } pp_w()
|
---|
| 312 | |
|
---|
| 313 | LOOKAHEAD(pp_supports_condition_in_parens() pp_w() <PP_AND>)
|
---|
| 314 | pass=pp_supports_condition_in_parens() pp_w()
|
---|
| 315 | ( <PP_AND> pp_w() q=pp_supports_condition_in_parens() { pass = pass && q; } pp_w() )+
|
---|
| 316 | |
|
---|
| 317 | LOOKAHEAD(pp_supports_condition_in_parens() pp_w() <PP_OR>)
|
---|
| 318 | pass=pp_supports_condition_in_parens() pp_w()
|
---|
| 319 | ( <PP_OR> pp_w() q=pp_supports_condition_in_parens() { pass = pass || q; } pp_w() )+
|
---|
| 320 | |
|
---|
| 321 | pass=pp_supports_condition_in_parens() pp_w()
|
---|
| 322 | )
|
---|
| 323 | { return pass; }
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | /**
|
---|
| 327 | * Parses something in parenthesis inside the condition of the @supports rule.
|
---|
| 328 | *
|
---|
| 329 | * @return true, if the condition is fulfilled
|
---|
| 330 | */
|
---|
| 331 | boolean pp_supports_condition_in_parens():
|
---|
| 332 | {
|
---|
| 333 | boolean pass;
|
---|
| 334 | }
|
---|
| 335 | {
|
---|
| 336 | (
|
---|
| 337 | LOOKAHEAD(pp_supports_declaration_condition())
|
---|
| 338 | pass=pp_supports_declaration_condition()
|
---|
| 339 | |
|
---|
| 340 | <LPAR> pp_w() pass=pp_supports_condition() <RPAR>
|
---|
| 341 | )
|
---|
| 342 | { return pass; }
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | /**
|
---|
| 346 | * Parse an @supports declaration condition, e. g. a single (key:value) or (key) statement.
|
---|
| 347 | *
|
---|
| 348 | * The parsing rule {@link #literal()} from the main mapcss parser is reused here.
|
---|
| 349 | *
|
---|
| 350 | * @return true if the condition is fulfilled
|
---|
| 351 | */
|
---|
| 352 | boolean pp_supports_declaration_condition():
|
---|
| 353 | {
|
---|
| 354 | Token t;
|
---|
| 355 | String feature;
|
---|
| 356 | Object val = null;
|
---|
| 357 | }
|
---|
| 358 | {
|
---|
| 359 | <LPAR> pp_w() t=<IDENT> { feature = t.image; } pp_w() ( <COLON> pp_w() val=literal() )? <RPAR>
|
---|
| 360 | { return this.sheet.evalSupportsDeclCondition(feature, val); }
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | // deprecated
|
---|
[7277] | 364 | void pp_media(boolean ignore):
|
---|
[6970] | 365 | {
|
---|
| 366 | boolean pass = false;
|
---|
| 367 | boolean q;
|
---|
| 368 | boolean empty = true;
|
---|
| 369 | }
|
---|
| 370 | {
|
---|
| 371 | <PP_MEDIA> pp_w()
|
---|
| 372 | ( q=pp_media_query() { pass = pass || q; empty = false; }
|
---|
| 373 | ( <COMMA> pp_w() q=pp_media_query() { pass = pass || q; } )*
|
---|
| 374 | )?
|
---|
| 375 | <LBRACE>
|
---|
[7277] | 376 | pp_black_box((empty || pass) && !ignore)
|
---|
[6970] | 377 | <RBRACE>
|
---|
| 378 | }
|
---|
| 379 |
|
---|
[8087] | 380 | // deprecated
|
---|
[6970] | 381 | boolean pp_media_query():
|
---|
| 382 | {
|
---|
| 383 | Token t;
|
---|
| 384 | String mediatype = "all";
|
---|
| 385 | boolean pass = true;
|
---|
| 386 | boolean invert = false;
|
---|
| 387 | boolean e;
|
---|
| 388 | }
|
---|
| 389 | {
|
---|
| 390 | ( <PP_NOT> { invert = true; } pp_w() )?
|
---|
| 391 | (
|
---|
[8404] | 392 | t=<IDENT> { mediatype = t.image.toLowerCase(Locale.ENGLISH); } pp_w()
|
---|
[6970] | 393 | ( <PP_AND> pp_w() e=pp_media_expression() { pass = pass && e; } pp_w() )*
|
---|
| 394 | |
|
---|
| 395 | e=pp_media_expression() { pass = pass && e; } pp_w()
|
---|
| 396 | ( <PP_AND> pp_w() e=pp_media_expression() { pass = pass && e; } pp_w() )*
|
---|
| 397 | )
|
---|
| 398 | {
|
---|
| 399 | if (!"all".equals(mediatype)) {
|
---|
| 400 | pass = false;
|
---|
| 401 | }
|
---|
| 402 | return invert ? (!pass) : pass;
|
---|
| 403 | }
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | /**
|
---|
| 407 | * Parse an @media expression.
|
---|
| 408 | *
|
---|
| 409 | * The parsing rule {@link #literal()} from the main mapcss parser is reused here.
|
---|
| 410 | *
|
---|
| 411 | * @return true if the condition is fulfilled
|
---|
| 412 | */
|
---|
[8087] | 413 | // deprecated
|
---|
[6970] | 414 | boolean pp_media_expression():
|
---|
| 415 | {
|
---|
| 416 | Token t;
|
---|
| 417 | String feature;
|
---|
| 418 | Object val = null;
|
---|
| 419 | }
|
---|
| 420 | {
|
---|
| 421 | <LPAR> pp_w() t=<IDENT> { feature = t.image; } pp_w() ( <COLON> pp_w() val=literal() )? <RPAR>
|
---|
[8087] | 422 | { return this.sheet.evalSupportsDeclCondition(feature, val); }
|
---|
[6970] | 423 | }
|
---|
| 424 |
|
---|
| 425 | void pp_w1():
|
---|
| 426 | {
|
---|
| 427 | Token t;
|
---|
| 428 | }
|
---|
| 429 | {
|
---|
| 430 | t=<PP_NEWLINECHAR> { sb.append(t.image); }
|
---|
| 431 | |
|
---|
| 432 | t=<PP_WHITESPACE> { sb.append(t.image); }
|
---|
| 433 | |
|
---|
| 434 | t=<PP_COMMENT_START> { sb.append(t.image); } t=<PP_COMMENT_END> { sb.append(t.image); }
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | void pp_w():
|
---|
| 438 | {
|
---|
| 439 | }
|
---|
| 440 | {
|
---|
| 441 | ( pp_w1() )*
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | /*************
|
---|
| 445 | *
|
---|
| 446 | * Parser definition for the main MapCSS parser:
|
---|
| 447 | *
|
---|
| 448 | * <pre>
|
---|
| 449 | *
|
---|
| 450 | * rule
|
---|
| 451 | * _______________________|______________________________
|
---|
| 452 | * | |
|
---|
| 453 | * selector declaration
|
---|
| 454 | * _________|___________________ _________|____________
|
---|
| 455 | * | | | |
|
---|
| 456 | *
|
---|
| 457 | * way|z11-12[highway=residential] { color: red; width: 3 }
|
---|
| 458 | *
|
---|
| 459 | * |_____||___________________| |_________|
|
---|
| 460 | * | | |
|
---|
| 461 | * zoom condition instruction
|
---|
| 462 | *
|
---|
| 463 | * more general:
|
---|
| 464 | *
|
---|
| 465 | * way|z13-[a=b][c=d]::subpart, way|z-3[u=v]:closed::subpart2 { p1 : val; p2 : val; }
|
---|
| 466 | *
|
---|
| 467 | * 'val' can be a literal, or an expression like "prop(width, default) + 0.8".
|
---|
| 468 | *
|
---|
| 469 | * </pre>
|
---|
| 470 | */
|
---|
| 471 |
|
---|
[3848] | 472 | int uint() :
|
---|
| 473 | {
|
---|
| 474 | Token i;
|
---|
| 475 | }
|
---|
| 476 | {
|
---|
| 477 | i=<UINT> { return Integer.parseInt(i.image); }
|
---|
| 478 | }
|
---|
| 479 |
|
---|
[3911] | 480 | int int_() :
|
---|
| 481 | {
|
---|
| 482 | int i;
|
---|
| 483 | }
|
---|
| 484 | {
|
---|
| 485 | <MINUS> i=uint() { return -i; } | i=uint() { return i; }
|
---|
| 486 | }
|
---|
| 487 |
|
---|
[3848] | 488 | float ufloat() :
|
---|
| 489 | {
|
---|
| 490 | Token f;
|
---|
| 491 | }
|
---|
| 492 | {
|
---|
| 493 | ( f=<UFLOAT> | f=<UINT> )
|
---|
| 494 | { return Float.parseFloat(f.image); }
|
---|
| 495 | }
|
---|
| 496 |
|
---|
[3876] | 497 | float float_() :
|
---|
| 498 | {
|
---|
| 499 | float f;
|
---|
| 500 | }
|
---|
| 501 | {
|
---|
| 502 | <MINUS> f=ufloat() { return -f; } | f=ufloat() { return f; }
|
---|
| 503 | }
|
---|
| 504 |
|
---|
[3848] | 505 | String string() :
|
---|
| 506 | {
|
---|
| 507 | Token t;
|
---|
| 508 | }
|
---|
| 509 | {
|
---|
| 510 | t=<STRING>
|
---|
[3876] | 511 | { return t.image.substring(1, t.image.length() - 1).replace("\\\"", "\"").replace("\\\\", "\\"); }
|
---|
[3848] | 512 | }
|
---|
| 513 |
|
---|
[6896] | 514 | String ident():
|
---|
| 515 | {
|
---|
| 516 | Token t;
|
---|
| 517 | String s;
|
---|
| 518 | }
|
---|
| 519 | {
|
---|
[6970] | 520 | ( t=<IDENT> | t=<SET> ) { return t.image; }
|
---|
[6896] | 521 | }
|
---|
| 522 |
|
---|
[3848] | 523 | String string_or_ident() :
|
---|
| 524 | {
|
---|
| 525 | Token t;
|
---|
| 526 | String s;
|
---|
| 527 | }
|
---|
| 528 | {
|
---|
[6896] | 529 | ( s=ident() | s=string() ) { return s; }
|
---|
[3848] | 530 | }
|
---|
| 531 |
|
---|
[3876] | 532 | String regex() :
|
---|
| 533 | {
|
---|
| 534 | Token t;
|
---|
| 535 | }
|
---|
| 536 | {
|
---|
| 537 | t=<REGEX>
|
---|
| 538 | { return t.image.substring(1, t.image.length() - 1); }
|
---|
| 539 | }
|
---|
| 540 |
|
---|
[3848] | 541 | /**
|
---|
| 542 | * white-space
|
---|
| 543 | */
|
---|
| 544 | void s() :
|
---|
| 545 | {
|
---|
| 546 | }
|
---|
| 547 | {
|
---|
| 548 | ( <S> )?
|
---|
| 549 | }
|
---|
| 550 |
|
---|
| 551 | /**
|
---|
| 552 | * mix of white-space and comments
|
---|
| 553 | */
|
---|
| 554 | void w() :
|
---|
| 555 | {
|
---|
| 556 | }
|
---|
| 557 | {
|
---|
[3876] | 558 | ( <S> | <COMMENT_START> <COMMENT_END> )*
|
---|
[3848] | 559 | }
|
---|
| 560 |
|
---|
| 561 | /**
|
---|
[6830] | 562 | * comma delimited list of floats (at least 2, all >= 0)
|
---|
[3848] | 563 | */
|
---|
[3856] | 564 | List<Float> float_array() :
|
---|
[3848] | 565 | {
|
---|
| 566 | float f;
|
---|
| 567 | List<Float> fs = new ArrayList<Float>();
|
---|
| 568 | }
|
---|
| 569 | {
|
---|
| 570 | f=ufloat() { fs.add(f); }
|
---|
| 571 | (
|
---|
| 572 | <COMMA> s()
|
---|
| 573 | f=ufloat() { fs.add(f); }
|
---|
| 574 | )+
|
---|
| 575 | {
|
---|
[3856] | 576 | return fs;
|
---|
[3848] | 577 | }
|
---|
| 578 | }
|
---|
| 579 |
|
---|
| 580 | /**
|
---|
[6970] | 581 | * entry point for the main parser
|
---|
[3848] | 582 | */
|
---|
| 583 | void sheet(MapCSSStyleSource sheet):
|
---|
| 584 | {
|
---|
| 585 | }
|
---|
| 586 | {
|
---|
[3863] | 587 | { this.sheet = sheet; }
|
---|
[3848] | 588 | w()
|
---|
[3876] | 589 | (
|
---|
| 590 | try {
|
---|
[7064] | 591 | rule() w()
|
---|
[4069] | 592 | } catch (MapCSSException mex) {
|
---|
[8521] | 593 | Main.error(mex);
|
---|
[4069] | 594 | error_skipto(RBRACE, mex);
|
---|
| 595 | w();
|
---|
[3876] | 596 | } catch (ParseException ex) {
|
---|
[4069] | 597 | error_skipto(RBRACE, null);
|
---|
[3876] | 598 | w();
|
---|
| 599 | }
|
---|
| 600 | )*
|
---|
[6970] | 601 | <EOF>
|
---|
[3848] | 602 | }
|
---|
| 603 |
|
---|
[7064] | 604 | void rule():
|
---|
[3848] | 605 | {
|
---|
| 606 | List<Selector> selectors = new ArrayList<Selector>();
|
---|
| 607 | Selector sel;
|
---|
[7064] | 608 | Declaration decl;
|
---|
[3848] | 609 | }
|
---|
| 610 | {
|
---|
[3902] | 611 | sel=child_selector() { selectors.add(sel); }
|
---|
[3876] | 612 | (
|
---|
| 613 | <COMMA> w()
|
---|
[3902] | 614 | sel=child_selector() { selectors.add(sel); }
|
---|
[3876] | 615 | )*
|
---|
[3848] | 616 | decl=declaration()
|
---|
[7064] | 617 | {
|
---|
| 618 | for (Selector s : selectors) {
|
---|
| 619 | sheet.rules.add(new MapCSSRule(s, decl));
|
---|
| 620 | }
|
---|
| 621 | }
|
---|
[3848] | 622 | }
|
---|
| 623 |
|
---|
[3902] | 624 | Selector child_selector() :
|
---|
| 625 | {
|
---|
[6607] | 626 | Selector.ChildOrParentSelectorType type = null;
|
---|
[4069] | 627 | Condition c;
|
---|
| 628 | List<Condition> conditions = new ArrayList<Condition>();
|
---|
| 629 | Selector selLeft;
|
---|
| 630 | LinkSelector selLink = null;
|
---|
| 631 | Selector selRight = null;
|
---|
[3902] | 632 | }
|
---|
| 633 | {
|
---|
[4069] | 634 | selLeft=selector() w()
|
---|
[3902] | 635 | (
|
---|
[6607] | 636 | (
|
---|
[6927] | 637 | (
|
---|
[7166] | 638 | (
|
---|
| 639 | <GREATER> { type = Selector.ChildOrParentSelectorType.CHILD; }
|
---|
| 640 | |
|
---|
| 641 | <LESS> { type = Selector.ChildOrParentSelectorType.PARENT; }
|
---|
| 642 | |
|
---|
| 643 | <PLUS> { type = Selector.ChildOrParentSelectorType.SIBLING; }
|
---|
| 644 | )
|
---|
| 645 | ( ( c=condition(Context.LINK) | c=class_or_pseudoclass(Context.LINK) ) { conditions.add(c); } )*
|
---|
[6927] | 646 | |
|
---|
[7166] | 647 | <ELEMENT_OF> { type = Selector.ChildOrParentSelectorType.ELEMENT_OF; }
|
---|
[6927] | 648 | |
|
---|
[7166] | 649 | <CROSSING> { type = Selector.ChildOrParentSelectorType.CROSSING; }
|
---|
[6927] | 650 | )
|
---|
[7166] | 651 | w()
|
---|
[6607] | 652 | |
|
---|
[7166] | 653 | { /* <GREATER> is optional for child selector */ type = Selector.ChildOrParentSelectorType.CHILD; }
|
---|
[6607] | 654 | )
|
---|
[4069] | 655 | { selLink = new LinkSelector(conditions); }
|
---|
| 656 | selRight=selector() w()
|
---|
[3902] | 657 | )?
|
---|
[6607] | 658 | { return selRight != null ? new ChildOrParentSelector(selLeft, selLink, selRight, type) : selLeft; }
|
---|
[3902] | 659 | }
|
---|
| 660 |
|
---|
[3848] | 661 | Selector selector() :
|
---|
| 662 | {
|
---|
| 663 | Token base;
|
---|
| 664 | Condition c;
|
---|
| 665 | Pair<Integer, Integer> r = null;
|
---|
| 666 | List<Condition> conditions = new ArrayList<Condition>();
|
---|
[8086] | 667 | Subpart sub = null;
|
---|
[3848] | 668 | }
|
---|
| 669 | {
|
---|
| 670 | ( base=<IDENT> | base=<STAR> )
|
---|
| 671 | ( r=zoom() )?
|
---|
[6560] | 672 | ( ( c=condition(Context.PRIMITIVE) | c=class_or_pseudoclass(Context.PRIMITIVE) ) { conditions.add(c); } )*
|
---|
[3848] | 673 | ( sub=subpart() )?
|
---|
[3902] | 674 | { return new GeneralSelector(base.image, r, conditions, sub); }
|
---|
[3848] | 675 | }
|
---|
| 676 |
|
---|
| 677 | Pair<Integer, Integer> zoom() :
|
---|
| 678 | {
|
---|
| 679 | Integer min = 0;
|
---|
| 680 | Integer max = Integer.MAX_VALUE;
|
---|
| 681 | }
|
---|
| 682 | {
|
---|
| 683 | <PIPE_Z>
|
---|
| 684 | (
|
---|
[3876] | 685 | <MINUS> max=uint()
|
---|
| 686 | |
|
---|
[4280] | 687 | LOOKAHEAD(2)
|
---|
| 688 | min=uint() <MINUS> ( max=uint() )?
|
---|
| 689 | |
|
---|
| 690 | min=uint() { max = min; }
|
---|
[3848] | 691 | )
|
---|
| 692 | { return new Pair<Integer, Integer>(min, max); }
|
---|
| 693 | }
|
---|
| 694 |
|
---|
[4069] | 695 | Condition condition(Context context) :
|
---|
[3848] | 696 | {
|
---|
[3856] | 697 | Condition c;
|
---|
| 698 | Expression e;
|
---|
| 699 | }
|
---|
| 700 | {
|
---|
[3911] | 701 | <LSQUARE> s()
|
---|
[3876] | 702 | (
|
---|
[4069] | 703 | LOOKAHEAD( simple_key_condition(context) s() <RSQUARE> )
|
---|
| 704 | c=simple_key_condition(context) s() <RSQUARE> { return c; }
|
---|
[3876] | 705 | |
|
---|
[4069] | 706 | LOOKAHEAD( simple_key_value_condition(context) s() <RSQUARE> )
|
---|
| 707 | c=simple_key_value_condition(context) s() <RSQUARE> { return c; }
|
---|
[3876] | 708 | |
|
---|
[6560] | 709 | e=expression() <RSQUARE> { return Condition.createExpressionCondition(e, context); }
|
---|
[3856] | 710 | )
|
---|
| 711 | }
|
---|
| 712 |
|
---|
[3902] | 713 | String tag_key() :
|
---|
| 714 | {
|
---|
[6896] | 715 | String s, s2;
|
---|
[3902] | 716 | Token t;
|
---|
| 717 | }
|
---|
| 718 | {
|
---|
| 719 | s=string() { return s; }
|
---|
| 720 | |
|
---|
[6896] | 721 | s=ident() ( <COLON> s2=ident() { s += ':' + s2; } )* { return s; }
|
---|
[3902] | 722 | }
|
---|
| 723 |
|
---|
[4069] | 724 | Condition simple_key_condition(Context context) :
|
---|
[3856] | 725 | {
|
---|
[3848] | 726 | boolean not = false;
|
---|
[6547] | 727 | Condition.KeyMatchType matchType = null;;
|
---|
[3848] | 728 | String key;
|
---|
| 729 | }
|
---|
| 730 | {
|
---|
| 731 | ( <EXCLAMATION> { not = true; } )?
|
---|
[6547] | 732 | (
|
---|
| 733 | { matchType = Condition.KeyMatchType.REGEX; } key = regex()
|
---|
| 734 | |
|
---|
| 735 | key = tag_key()
|
---|
| 736 | )
|
---|
| 737 | ( LOOKAHEAD(2) <QUESTION> <EXCLAMATION> { matchType = Condition.KeyMatchType.FALSE; } )?
|
---|
| 738 | ( <QUESTION> { matchType = Condition.KeyMatchType.TRUE; } )?
|
---|
[6560] | 739 | { return Condition.createKeyCondition(key, not, matchType, context); }
|
---|
[3848] | 740 | }
|
---|
| 741 |
|
---|
[4069] | 742 | Condition simple_key_value_condition(Context context) :
|
---|
[3856] | 743 | {
|
---|
| 744 | String key;
|
---|
| 745 | String val;
|
---|
[3876] | 746 | float f;
|
---|
[3911] | 747 | int i;
|
---|
[3876] | 748 | Condition.Op op;
|
---|
[6554] | 749 | boolean considerValAsKey = false;
|
---|
[3856] | 750 | }
|
---|
| 751 | {
|
---|
[3911] | 752 | key=tag_key() s()
|
---|
[3876] | 753 | (
|
---|
[6554] | 754 | LOOKAHEAD(3)
|
---|
| 755 | (
|
---|
| 756 | <EQUAL> <TILDE> { op=Condition.Op.REGEX; }
|
---|
| 757 | |
|
---|
| 758 | <EXCLAMATION> <TILDE> { op=Condition.Op.NREGEX; }
|
---|
| 759 | )
|
---|
| 760 | s()
|
---|
| 761 | ( <STAR> { considerValAsKey=true; } )?
|
---|
| 762 | val=regex()
|
---|
[3911] | 763 | |
|
---|
[3876] | 764 | (
|
---|
[3911] | 765 | <EXCLAMATION> <EQUAL> { op=Condition.Op.NEQ; }
|
---|
[3876] | 766 | |
|
---|
| 767 | <EQUAL> { op=Condition.Op.EQ; }
|
---|
| 768 | |
|
---|
[3912] | 769 | <TILDE> <EQUAL> { op=Condition.Op.ONE_OF; }
|
---|
[3876] | 770 | |
|
---|
[3912] | 771 | <CARET> <EQUAL> { op=Condition.Op.BEGINS_WITH; }
|
---|
[3876] | 772 | |
|
---|
[3912] | 773 | <DOLLAR> <EQUAL> { op=Condition.Op.ENDS_WITH; }
|
---|
[3876] | 774 | |
|
---|
[3912] | 775 | <STAR> <EQUAL> { op=Condition.Op.CONTAINS; }
|
---|
[3876] | 776 | )
|
---|
[3911] | 777 | s()
|
---|
[6554] | 778 | ( <STAR> { considerValAsKey=true; } )?
|
---|
[3911] | 779 | (
|
---|
| 780 | LOOKAHEAD(2)
|
---|
| 781 | i=int_() { val=Integer.toString(i); }
|
---|
| 782 | |
|
---|
| 783 | f=float_() { val=Float.toString(f); }
|
---|
| 784 | |
|
---|
| 785 | val=string_or_ident()
|
---|
| 786 | )
|
---|
[3876] | 787 | |
|
---|
| 788 | (
|
---|
| 789 | <GREATER_EQUAL> { op=Condition.Op.GREATER_OR_EQUAL; }
|
---|
| 790 | |
|
---|
| 791 | <GREATER> { op=Condition.Op.GREATER; }
|
---|
| 792 | |
|
---|
| 793 | <LESS_EQUAL> { op=Condition.Op.LESS_OR_EQUAL; }
|
---|
| 794 | |
|
---|
| 795 | <LESS> { op=Condition.Op.LESS; }
|
---|
| 796 | )
|
---|
[3911] | 797 | s()
|
---|
| 798 | f=float_() { val=Float.toString(f); }
|
---|
[3876] | 799 | )
|
---|
[6560] | 800 | { return Condition.createKeyValueCondition(key, val, op, context, considerValAsKey); }
|
---|
[3856] | 801 | }
|
---|
| 802 |
|
---|
[6560] | 803 | Condition class_or_pseudoclass(Context context) :
|
---|
[3848] | 804 | {
|
---|
[6896] | 805 | String s;
|
---|
[3848] | 806 | boolean not = false;
|
---|
[6560] | 807 | boolean pseudo;
|
---|
[3848] | 808 | }
|
---|
| 809 | {
|
---|
[3888] | 810 | ( <EXCLAMATION> { not = true; } )?
|
---|
[6560] | 811 | (
|
---|
| 812 | <FULLSTOP> { pseudo = false; }
|
---|
| 813 | |
|
---|
| 814 | <COLON> { pseudo = true; }
|
---|
| 815 | )
|
---|
[6896] | 816 | s=ident()
|
---|
[6560] | 817 | { return pseudo
|
---|
[6896] | 818 | ? Condition.createPseudoClassCondition(s, not, context)
|
---|
| 819 | : Condition.createClassCondition(s, not, context); }
|
---|
[3848] | 820 | }
|
---|
| 821 |
|
---|
[8086] | 822 | Subpart subpart() :
|
---|
[3848] | 823 | {
|
---|
[6896] | 824 | String s;
|
---|
[8086] | 825 | Expression e;
|
---|
[3848] | 826 | }
|
---|
| 827 | {
|
---|
[3876] | 828 | <DCOLON>
|
---|
[8086] | 829 | (
|
---|
| 830 | s=ident() { return new Subpart.StringSubpart(s); }
|
---|
| 831 | |
|
---|
| 832 | <STAR> { return new Subpart.StringSubpart("*"); }
|
---|
| 833 | |
|
---|
| 834 | <LPAR> e=expression() <RPAR> { return new Subpart.ExpressionSubpart(e); }
|
---|
| 835 | )
|
---|
[3848] | 836 | }
|
---|
| 837 |
|
---|
[7064] | 838 | Declaration declaration() :
|
---|
[3848] | 839 | {
|
---|
| 840 | List<Instruction> ins = new ArrayList<Instruction>();
|
---|
| 841 | Instruction i;
|
---|
| 842 | Token key;
|
---|
[6560] | 843 | Object val = null;
|
---|
[3848] | 844 | }
|
---|
| 845 | {
|
---|
[3876] | 846 | <LBRACE> w()
|
---|
[3848] | 847 | (
|
---|
[6560] | 848 | (
|
---|
[6561] | 849 | <SET> w()
|
---|
| 850 | (<FULLSTOP>)? // specification allows "set .class" to set "class". we also support "set class"
|
---|
| 851 | key=<IDENT> w()
|
---|
| 852 | ( <EQUAL> val=expression() )?
|
---|
[6611] | 853 | { ins.add(new Instruction.AssignmentInstruction(key.image, val == null ? true : val, true)); }
|
---|
[7064] | 854 | ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
|
---|
[6560] | 855 | )
|
---|
[6561] | 856 | |
|
---|
[3876] | 857 | key=<IDENT> w() <COLON> w()
|
---|
| 858 | (
|
---|
| 859 | LOOKAHEAD( float_array() w() ( <SEMICOLON> | <RBRACE> ) )
|
---|
| 860 | val=float_array()
|
---|
[6611] | 861 | { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
|
---|
[3876] | 862 | w()
|
---|
[7064] | 863 | ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
|
---|
[3876] | 864 | |
|
---|
| 865 | LOOKAHEAD( expression() ( <SEMICOLON> | <RBRACE> ) )
|
---|
| 866 | val=expression()
|
---|
[6611] | 867 | { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
|
---|
[7064] | 868 | ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
|
---|
[3876] | 869 | |
|
---|
[6611] | 870 | val=readRaw() w() { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
|
---|
[3876] | 871 | )
|
---|
| 872 | )*
|
---|
| 873 | <RBRACE>
|
---|
[7064] | 874 | { return new Declaration(ins, declarationCounter++); }
|
---|
[3848] | 875 | }
|
---|
| 876 |
|
---|
[8256] | 877 | /**
|
---|
| 878 | * General expression.
|
---|
| 879 | * Separate production rule for each level of operator precedence (recursive descent).
|
---|
| 880 | */
|
---|
| 881 | Expression expression() :
|
---|
[3848] | 882 | {
|
---|
| 883 | Expression e;
|
---|
[8256] | 884 | }
|
---|
| 885 | {
|
---|
| 886 | e=conditional_expression()
|
---|
| 887 | {
|
---|
| 888 | return e;
|
---|
| 889 | }
|
---|
| 890 | }
|
---|
| 891 |
|
---|
| 892 | Expression conditional_expression() :
|
---|
| 893 | {
|
---|
| 894 | Expression e, e1, e2;
|
---|
[3848] | 895 | String op = null;
|
---|
| 896 | }
|
---|
| 897 | {
|
---|
[8256] | 898 | e=or_expression()
|
---|
[3848] | 899 | (
|
---|
[8256] | 900 | <QUESTION> w()
|
---|
| 901 | e1=conditional_expression()
|
---|
| 902 | <COLON> w()
|
---|
| 903 | e2=conditional_expression()
|
---|
| 904 | {
|
---|
| 905 | e = ExpressionFactory.createFunctionExpression("cond", Arrays.asList(e, e1, e2));
|
---|
| 906 | }
|
---|
| 907 | )?
|
---|
| 908 | {
|
---|
| 909 | return e;
|
---|
| 910 | }
|
---|
| 911 | }
|
---|
[3876] | 912 |
|
---|
[8256] | 913 | Expression or_expression() :
|
---|
| 914 | {
|
---|
| 915 | Expression e, e2;
|
---|
| 916 | String op = null;
|
---|
| 917 | }
|
---|
| 918 | {
|
---|
| 919 | e=and_expression()
|
---|
| 920 | (
|
---|
| 921 | <PIPE> <PIPE> w()
|
---|
| 922 | e2=and_expression()
|
---|
| 923 | {
|
---|
| 924 | e = ExpressionFactory.createFunctionExpression("or", Arrays.asList(e, e2));
|
---|
| 925 | }
|
---|
| 926 | )*
|
---|
| 927 | {
|
---|
| 928 | return e;
|
---|
| 929 | }
|
---|
| 930 | }
|
---|
| 931 |
|
---|
| 932 | Expression and_expression() :
|
---|
| 933 | {
|
---|
| 934 | Expression e, e2;
|
---|
| 935 | String op = null;
|
---|
| 936 | }
|
---|
| 937 | {
|
---|
| 938 | e=relational_expression()
|
---|
| 939 | (
|
---|
| 940 | <AMPERSAND> <AMPERSAND> w()
|
---|
| 941 | e2=relational_expression()
|
---|
| 942 | {
|
---|
| 943 | e = ExpressionFactory.createFunctionExpression("and", Arrays.asList(e, e2));
|
---|
| 944 | }
|
---|
| 945 | )*
|
---|
| 946 | {
|
---|
| 947 | return e;
|
---|
| 948 | }
|
---|
| 949 | }
|
---|
| 950 |
|
---|
| 951 | Expression relational_expression() :
|
---|
| 952 | {
|
---|
| 953 | Expression e, e2;
|
---|
| 954 | String op = null;
|
---|
| 955 | }
|
---|
| 956 | {
|
---|
| 957 | e=additive_expression()
|
---|
| 958 | (
|
---|
[3867] | 959 | (
|
---|
[8256] | 960 | <GREATER_EQUAL> { op = "greater_equal"; }
|
---|
| 961 | |
|
---|
| 962 | <LESS_EQUAL> { op = "less_equal"; }
|
---|
| 963 | |
|
---|
| 964 | <GREATER> { op = "greater"; }
|
---|
| 965 | |
|
---|
| 966 | <LESS> { op = "less"; }
|
---|
| 967 | |
|
---|
| 968 | <EQUAL> ( <EQUAL> )? { op = "equal"; }
|
---|
| 969 | |
|
---|
| 970 | <EXCLAMATION> <EQUAL> { op = "not_equal"; }
|
---|
| 971 | ) w()
|
---|
| 972 | e2=additive_expression()
|
---|
| 973 | {
|
---|
| 974 | e = ExpressionFactory.createFunctionExpression(op, Arrays.asList(e, e2));
|
---|
| 975 | }
|
---|
| 976 | )?
|
---|
[3848] | 977 | {
|
---|
[8256] | 978 | return e;
|
---|
| 979 | }
|
---|
| 980 | }
|
---|
| 981 |
|
---|
| 982 | Expression additive_expression() :
|
---|
| 983 | {
|
---|
| 984 | Expression e, e2;
|
---|
| 985 | String op = null;
|
---|
| 986 | }
|
---|
| 987 | {
|
---|
| 988 | e=multiplicative_expression()
|
---|
| 989 | (
|
---|
| 990 | ( <PLUS> { op = "plus"; } | <MINUS> { op = "minus"; } ) w()
|
---|
| 991 | e2=multiplicative_expression()
|
---|
| 992 | {
|
---|
| 993 | e = ExpressionFactory.createFunctionExpression(op, Arrays.asList(e, e2));
|
---|
| 994 | }
|
---|
| 995 | )*
|
---|
| 996 | {
|
---|
| 997 | return e;
|
---|
| 998 | }
|
---|
| 999 | }
|
---|
| 1000 |
|
---|
| 1001 | Expression multiplicative_expression() :
|
---|
| 1002 | {
|
---|
| 1003 | Expression e, e2;
|
---|
| 1004 | String op = null;
|
---|
| 1005 | }
|
---|
| 1006 | {
|
---|
| 1007 | e=unary_expression()
|
---|
| 1008 | (
|
---|
| 1009 | ( <STAR> { op = "times"; } | <SLASH> { op = "divided_by"; } ) w()
|
---|
| 1010 | e2=unary_expression()
|
---|
| 1011 | {
|
---|
| 1012 | e = ExpressionFactory.createFunctionExpression(op, Arrays.asList(e, e2));
|
---|
| 1013 | }
|
---|
| 1014 | )*
|
---|
| 1015 | {
|
---|
| 1016 | return e;
|
---|
| 1017 | }
|
---|
| 1018 | }
|
---|
| 1019 |
|
---|
| 1020 | Expression unary_expression() :
|
---|
| 1021 | {
|
---|
| 1022 | Expression e;
|
---|
| 1023 | String op = null;
|
---|
| 1024 | }
|
---|
| 1025 | {
|
---|
| 1026 | (
|
---|
| 1027 | <MINUS> { op = "minus"; } w()
|
---|
| 1028 | |
|
---|
| 1029 | <EXCLAMATION> { op = "not"; } w()
|
---|
| 1030 | )?
|
---|
| 1031 | e=primary() w()
|
---|
| 1032 | {
|
---|
[3856] | 1033 | if (op == null)
|
---|
[8256] | 1034 | return e;
|
---|
| 1035 | return ExpressionFactory.createFunctionExpression(op, Collections.singletonList(e));
|
---|
[3848] | 1036 | }
|
---|
| 1037 | }
|
---|
| 1038 |
|
---|
| 1039 | Expression primary() :
|
---|
| 1040 | {
|
---|
| 1041 | Expression nested;
|
---|
[5705] | 1042 | Expression fn;
|
---|
[3848] | 1043 | Object lit;
|
---|
| 1044 | }
|
---|
| 1045 | {
|
---|
[3971] | 1046 | LOOKAHEAD(3) // both function and identifier start with an identifier (+ optional whitespace)
|
---|
[3848] | 1047 | fn=function() { return fn; }
|
---|
| 1048 | |
|
---|
[8260] | 1049 | lit=literal()
|
---|
| 1050 | {
|
---|
| 1051 | if (lit == null)
|
---|
| 1052 | return NullExpression.INSTANCE;
|
---|
| 1053 | return new LiteralExpression(lit);
|
---|
| 1054 | }
|
---|
[3848] | 1055 | |
|
---|
[3876] | 1056 | <LPAR> w() nested=expression() <RPAR> { return nested; }
|
---|
[3848] | 1057 | }
|
---|
| 1058 |
|
---|
[5705] | 1059 | Expression function() :
|
---|
[3848] | 1060 | {
|
---|
| 1061 | Expression arg;
|
---|
| 1062 | String name;
|
---|
| 1063 | List<Expression> args = new ArrayList<Expression>();
|
---|
| 1064 | }
|
---|
| 1065 | {
|
---|
[6896] | 1066 | name=ident() w()
|
---|
[3848] | 1067 | <LPAR> w()
|
---|
[3856] | 1068 | (
|
---|
| 1069 | arg=expression() { args.add(arg); }
|
---|
| 1070 | ( <COMMA> w() arg=expression() { args.add(arg); } )*
|
---|
| 1071 | )?
|
---|
[3848] | 1072 | <RPAR>
|
---|
[5705] | 1073 | { return ExpressionFactory.createFunctionExpression(name, args); }
|
---|
[3848] | 1074 | }
|
---|
| 1075 |
|
---|
| 1076 | Object literal() :
|
---|
| 1077 | {
|
---|
[6970] | 1078 | String val, pref;
|
---|
| 1079 | Token t;
|
---|
[8260] | 1080 | Float f;
|
---|
[3848] | 1081 | }
|
---|
| 1082 | {
|
---|
[6740] | 1083 | LOOKAHEAD(2)
|
---|
[6970] | 1084 | pref=ident() t=<HEXCOLOR>
|
---|
| 1085 | { return Main.pref.getColor("mappaint." + (sheet == null ? "MapCSS" : sheet.title) + "." + pref, ColorHelper.html2color(t.image)); }
|
---|
[6740] | 1086 | |
|
---|
[3967] | 1087 | t=<IDENT> { return new Keyword(t.image); }
|
---|
[3848] | 1088 | |
|
---|
[3967] | 1089 | val=string() { return val; }
|
---|
| 1090 | |
|
---|
[3876] | 1091 | <PLUS> f=ufloat() { return new Instruction.RelativeFloat(f); }
|
---|
[3848] | 1092 | |
|
---|
[8260] | 1093 | LOOKAHEAD(2)
|
---|
| 1094 | f=ufloat_unit() { return f; }
|
---|
| 1095 | |
|
---|
[3848] | 1096 | f=ufloat() { return f; }
|
---|
| 1097 | |
|
---|
[6740] | 1098 | t=<HEXCOLOR> { return ColorHelper.html2color(t.image); }
|
---|
[3848] | 1099 | }
|
---|
| 1100 |
|
---|
[8260] | 1101 | /**
|
---|
| 1102 | * Number followed by a unit.
|
---|
| 1103 | *
|
---|
| 1104 | * Returns angles in radians and lengths in pixels.
|
---|
| 1105 | */
|
---|
| 1106 | Float ufloat_unit() :
|
---|
| 1107 | {
|
---|
| 1108 | float f;
|
---|
| 1109 | String u;
|
---|
| 1110 | }
|
---|
| 1111 | {
|
---|
| 1112 | f=ufloat() ( u=ident() | <DEG> { u = "°"; } )
|
---|
| 1113 | {
|
---|
| 1114 | Double m = unit_factor(u);
|
---|
| 1115 | if (m == null)
|
---|
| 1116 | return null;
|
---|
| 1117 | return (float) (f * m);
|
---|
| 1118 | }
|
---|
| 1119 | }
|
---|
| 1120 |
|
---|
[3848] | 1121 | JAVACODE
|
---|
[8260] | 1122 | private Double unit_factor(String unit) {
|
---|
| 1123 | switch (unit) {
|
---|
| 1124 | case "deg":
|
---|
| 1125 | case "°": return Math.PI / 180;
|
---|
| 1126 | case "rad": return 1.;
|
---|
| 1127 | case "grad": return Math.PI / 200;
|
---|
| 1128 | case "turn": return 2 * Math.PI;
|
---|
| 1129 | case "px": return 1.;
|
---|
| 1130 | case "cm": return 96/2.54;
|
---|
| 1131 | case "mm": return 9.6/2.54;
|
---|
| 1132 | case "in": return 96.;
|
---|
| 1133 | case "q": return 2.4/2.54;
|
---|
| 1134 | case "pc": return 16.;
|
---|
| 1135 | case "pt": return 96./72;
|
---|
| 1136 | default: return null;
|
---|
| 1137 | }
|
---|
| 1138 | }
|
---|
| 1139 |
|
---|
| 1140 | JAVACODE
|
---|
[4069] | 1141 | void error_skipto(int kind, MapCSSException me) {
|
---|
[3893] | 1142 | if (token.kind == EOF)
|
---|
| 1143 | throw new ParseException("Reached end of file while parsing");
|
---|
[4069] | 1144 |
|
---|
| 1145 | Exception e = null;
|
---|
| 1146 | ParseException pe = generateParseException();
|
---|
| 1147 |
|
---|
| 1148 | if (me != null) {
|
---|
[8515] | 1149 | final Token token = Utils.firstNonNull(pe.currentToken.next, pe.currentToken);
|
---|
| 1150 | me.setLine(token.beginLine);
|
---|
| 1151 | me.setColumn(token.beginColumn);
|
---|
[4069] | 1152 | e = me;
|
---|
| 1153 | } else {
|
---|
| 1154 | e = new ParseException(pe.getMessage()); // prevent memory leak
|
---|
| 1155 | }
|
---|
| 1156 |
|
---|
[6248] | 1157 | Main.error("Skipping to the next rule, because of an error:");
|
---|
| 1158 | Main.error(e);
|
---|
[3863] | 1159 | if (sheet != null) {
|
---|
[4069] | 1160 | sheet.logError(e);
|
---|
[3863] | 1161 | }
|
---|
| 1162 | Token t;
|
---|
| 1163 | do {
|
---|
| 1164 | t = getNextToken();
|
---|
[3867] | 1165 | } while (t.kind != kind && t.kind != EOF);
|
---|
| 1166 | if (t.kind == EOF)
|
---|
| 1167 | throw new ParseException("Reached end of file while parsing");
|
---|
[3848] | 1168 | }
|
---|
| 1169 |
|
---|
[3876] | 1170 | JAVACODE
|
---|
| 1171 | /**
|
---|
| 1172 | * read everything to the next semicolon
|
---|
| 1173 | */
|
---|
| 1174 | String readRaw() {
|
---|
| 1175 | Token t;
|
---|
| 1176 | StringBuilder s = new StringBuilder();
|
---|
| 1177 | while (true) {
|
---|
| 1178 | t = getNextToken();
|
---|
[3893] | 1179 | if ((t.kind == S || t.kind == STRING || t.kind == UNEXPECTED_CHAR) &&
|
---|
| 1180 | t.image.contains("\n")) {
|
---|
| 1181 | 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] | 1182 | Main.error(e);
|
---|
[3893] | 1183 | if (sheet != null) {
|
---|
| 1184 | sheet.logError(e);
|
---|
| 1185 | }
|
---|
| 1186 | }
|
---|
[3876] | 1187 | if (t.kind == SEMICOLON || t.kind == EOF)
|
---|
| 1188 | break;
|
---|
| 1189 | s.append(t.image);
|
---|
| 1190 | }
|
---|
| 1191 | if (t.kind == EOF)
|
---|
| 1192 | throw new ParseException("Reached end of file while parsing");
|
---|
| 1193 | return s.toString();
|
---|
| 1194 | }
|
---|
| 1195 |
|
---|