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