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