| 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.awt.Color;
|
|---|
| 11 | import java.util.ArrayList;
|
|---|
| 12 | import java.util.List;
|
|---|
| 13 |
|
|---|
| 14 | import org.openstreetmap.josm.gui.mappaint.Keyword;
|
|---|
| 15 | import org.openstreetmap.josm.gui.mappaint.mapcss.Condition;
|
|---|
| 16 | import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context;
|
|---|
| 17 | import org.openstreetmap.josm.gui.mappaint.mapcss.Expression;
|
|---|
| 18 | import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction;
|
|---|
| 19 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule;
|
|---|
| 20 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
|
|---|
| 21 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector;
|
|---|
| 22 | import org.openstreetmap.josm.gui.mappaint.mapcss.ExpressionFactory;
|
|---|
| 23 | import org.openstreetmap.josm.gui.mappaint.mapcss.LiteralExpression;
|
|---|
| 24 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSException;
|
|---|
| 25 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector;
|
|---|
| 26 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector;
|
|---|
| 27 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.LinkSelector;
|
|---|
| 28 | import org.openstreetmap.josm.tools.Pair;
|
|---|
| 29 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 30 | import org.openstreetmap.josm.Main;
|
|---|
| 31 |
|
|---|
| 32 | public class MapCSSParser {
|
|---|
| 33 | MapCSSStyleSource sheet;
|
|---|
| 34 | }
|
|---|
| 35 | PARSER_END(MapCSSParser)
|
|---|
| 36 |
|
|---|
| 37 | /*************
|
|---|
| 38 | * Token definitions
|
|---|
| 39 | */
|
|---|
| 40 |
|
|---|
| 41 | <DEFAULT>
|
|---|
| 42 | TOKEN:
|
|---|
| 43 | {
|
|---|
| 44 | < IDENT: ["a"-"z","A"-"Z","_"] ( ["a"-"z","A"-"Z","_","-","0"-"9"] )* >
|
|---|
| 45 | | < UINT: ["1"-"9"] ( ["0"-"9"] )* >
|
|---|
| 46 | | < UFLOAT: ( ["0"-"9"] )+ ( "." ( ["0"-"9"] )+ )? >
|
|---|
| 47 | | < STRING: "\"" ( [" ","!","#"-"[","]"-"~","\u0080"-"\uFFFF"] | "\\\"" | "\\\\" )* "\"" >
|
|---|
| 48 | | < #REGEX_CHAR_WITHOUT_STAR: [" "-")","+"-".","0"-"[","]"-"~","\u0080"-"\uFFFF"] | "\\/" | "\\\\" | "\\[" | "\\]" | "\\+" | "\\." | "\\'" | "\\\"" >
|
|---|
| 49 | | < REGEX: "/" <REGEX_CHAR_WITHOUT_STAR> ( <REGEX_CHAR_WITHOUT_STAR> | "*" )* "/" >
|
|---|
| 50 | | < #H: ["0"-"9","a"-"f","A"-"F"] >
|
|---|
| 51 | | < HEXCOLOR: "#" ( <H><H><H><H><H><H> | <H><H><H> ) >
|
|---|
| 52 | | < S: ( " " | "\t" | "\n" | "\r" | "\f" )+ >
|
|---|
| 53 | | < STAR: "*" >
|
|---|
| 54 | | < SLASH: "/" >
|
|---|
| 55 | | < LBRACE: "{" >
|
|---|
| 56 | | < RBRACE: "}" >
|
|---|
| 57 | | < LSQUARE: "[" >
|
|---|
| 58 | | < RSQUARE: "]" >
|
|---|
| 59 | | < LPAR: "(" >
|
|---|
| 60 | | < RPAR: ")" >
|
|---|
| 61 | | < GREATER_EQUAL: ">=" >
|
|---|
| 62 | | < LESS_EQUAL: "<=" >
|
|---|
| 63 | | < GREATER: ">" >
|
|---|
| 64 | | < LESS: "<" >
|
|---|
| 65 | | < EQUAL: "=" >
|
|---|
| 66 | | < EXCLAMATION: "!" >
|
|---|
| 67 | | < TILDE: "~" >
|
|---|
| 68 | | < COLON: ":" >
|
|---|
| 69 | | < DCOLON: "::" >
|
|---|
| 70 | | < SEMICOLON: ";" >
|
|---|
| 71 | | < COMMA: "," >
|
|---|
| 72 | | < PIPE: "|" >
|
|---|
| 73 | | < PIPE_Z: "|z" >
|
|---|
| 74 | | < PLUS: "+" >
|
|---|
| 75 | | < MINUS: "-" >
|
|---|
| 76 | | < AMPERSAND: "&" >
|
|---|
| 77 | | < QUESTION: "?" >
|
|---|
| 78 | | < DOLLAR: "$" >
|
|---|
| 79 | | < CARET: "^" >
|
|---|
| 80 | | < COMMENT_START: "/*" > : COMMENT
|
|---|
| 81 | | < UNEXPECTED_CHAR : ~[] > // avoid TokenMgrErrors because they are hard to recover from
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | <COMMENT>
|
|---|
| 85 | TOKEN:
|
|---|
| 86 | {
|
|---|
| 87 | < COMMENT_END: "*/" > : DEFAULT
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | <COMMENT>
|
|---|
| 91 | SKIP:
|
|---|
| 92 | {
|
|---|
| 93 | < ~[] >
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /*************
|
|---|
| 97 | * Parser definitions
|
|---|
| 98 | *
|
|---|
| 99 | * rule
|
|---|
| 100 | * _______________________|______________________________
|
|---|
| 101 | * | |
|
|---|
| 102 | * selector declaration
|
|---|
| 103 | * _________|___________________ _________|____________
|
|---|
| 104 | * | | | |
|
|---|
| 105 | *
|
|---|
| 106 | * way|z11-12[highway=residential] { color: red; width: 3 }
|
|---|
| 107 | *
|
|---|
| 108 | * |_____||___________________| |_________|
|
|---|
| 109 | * | | |
|
|---|
| 110 | * zoom condition instruction
|
|---|
| 111 | *
|
|---|
| 112 | * more general:
|
|---|
| 113 | *
|
|---|
| 114 | * way|z13-[a=b][c=d]::subpart, way|z-3[u=v]:closed::subpart2 { p1 : val; p2 : val; }
|
|---|
| 115 | *
|
|---|
| 116 | * 'val' can be a literal, or an expression like "prop(width, default) + 0.8".
|
|---|
| 117 | *
|
|---|
| 118 | */
|
|---|
| 119 |
|
|---|
| 120 | int uint() :
|
|---|
| 121 | {
|
|---|
| 122 | Token i;
|
|---|
| 123 | }
|
|---|
| 124 | {
|
|---|
| 125 | i=<UINT> { return Integer.parseInt(i.image); }
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | int int_() :
|
|---|
| 129 | {
|
|---|
| 130 | int i;
|
|---|
| 131 | }
|
|---|
| 132 | {
|
|---|
| 133 | <MINUS> i=uint() { return -i; } | i=uint() { return i; }
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | float ufloat() :
|
|---|
| 137 | {
|
|---|
| 138 | Token f;
|
|---|
| 139 | }
|
|---|
| 140 | {
|
|---|
| 141 | ( f=<UFLOAT> | f=<UINT> )
|
|---|
| 142 | { return Float.parseFloat(f.image); }
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | float float_() :
|
|---|
| 146 | {
|
|---|
| 147 | float f;
|
|---|
| 148 | }
|
|---|
| 149 | {
|
|---|
| 150 | <MINUS> f=ufloat() { return -f; } | f=ufloat() { return f; }
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | String string() :
|
|---|
| 154 | {
|
|---|
| 155 | Token t;
|
|---|
| 156 | }
|
|---|
| 157 | {
|
|---|
| 158 | t=<STRING>
|
|---|
| 159 | { return t.image.substring(1, t.image.length() - 1).replace("\\\"", "\"").replace("\\\\", "\\"); }
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | String string_or_ident() :
|
|---|
| 163 | {
|
|---|
| 164 | Token t;
|
|---|
| 165 | String s;
|
|---|
| 166 | }
|
|---|
| 167 | {
|
|---|
| 168 | t=<IDENT> { return t.image; } | s=string() { return s; }
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | String regex() :
|
|---|
| 172 | {
|
|---|
| 173 | Token t;
|
|---|
| 174 | }
|
|---|
| 175 | {
|
|---|
| 176 | t=<REGEX>
|
|---|
| 177 | { return t.image.substring(1, t.image.length() - 1); }
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | /**
|
|---|
| 181 | * white-space
|
|---|
| 182 | */
|
|---|
| 183 | void s() :
|
|---|
| 184 | {
|
|---|
| 185 | }
|
|---|
| 186 | {
|
|---|
| 187 | ( <S> )?
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | /**
|
|---|
| 191 | * mix of white-space and comments
|
|---|
| 192 | */
|
|---|
| 193 | void w() :
|
|---|
| 194 | {
|
|---|
| 195 | }
|
|---|
| 196 | {
|
|---|
| 197 | ( <S> | <COMMENT_START> <COMMENT_END> )*
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | /**
|
|---|
| 201 | * comma delimited list of floats (at least 2, all >= 0)
|
|---|
| 202 | */
|
|---|
| 203 | List<Float> float_array() :
|
|---|
| 204 | {
|
|---|
| 205 | float f;
|
|---|
| 206 | List<Float> fs = new ArrayList<Float>();
|
|---|
| 207 | }
|
|---|
| 208 | {
|
|---|
| 209 | f=ufloat() { fs.add(f); }
|
|---|
| 210 | (
|
|---|
| 211 | <COMMA> s()
|
|---|
| 212 | f=ufloat() { fs.add(f); }
|
|---|
| 213 | )+
|
|---|
| 214 | {
|
|---|
| 215 | return fs;
|
|---|
| 216 | }
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | /**
|
|---|
| 220 | * root
|
|---|
| 221 | */
|
|---|
| 222 | void sheet(MapCSSStyleSource sheet):
|
|---|
| 223 | {
|
|---|
| 224 | MapCSSRule r;
|
|---|
| 225 | Token com = null;
|
|---|
| 226 | }
|
|---|
| 227 | {
|
|---|
| 228 | { this.sheet = sheet; }
|
|---|
| 229 | w()
|
|---|
| 230 | (
|
|---|
| 231 | try {
|
|---|
| 232 | r=rule() { if (r != null) { sheet.rules.add(r); } } w()
|
|---|
| 233 | } catch (MapCSSException mex) {
|
|---|
| 234 | error_skipto(RBRACE, mex);
|
|---|
| 235 | w();
|
|---|
| 236 | } catch (ParseException ex) {
|
|---|
| 237 | error_skipto(RBRACE, null);
|
|---|
| 238 | w();
|
|---|
| 239 | }
|
|---|
| 240 | )*
|
|---|
| 241 | <EOF>
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | MapCSSRule rule():
|
|---|
| 245 | {
|
|---|
| 246 | List<Selector> selectors = new ArrayList<Selector>();
|
|---|
| 247 | Selector sel;
|
|---|
| 248 | List<Instruction> decl;
|
|---|
| 249 | }
|
|---|
| 250 | {
|
|---|
| 251 | sel=child_selector() { selectors.add(sel); }
|
|---|
| 252 | (
|
|---|
| 253 | <COMMA> w()
|
|---|
| 254 | sel=child_selector() { selectors.add(sel); }
|
|---|
| 255 | )*
|
|---|
| 256 | decl=declaration()
|
|---|
| 257 | { return new MapCSSRule(selectors, decl); }
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | Selector child_selector() :
|
|---|
| 261 | {
|
|---|
| 262 | boolean parentSelector = false;
|
|---|
| 263 | Condition c;
|
|---|
| 264 | List<Condition> conditions = new ArrayList<Condition>();
|
|---|
| 265 | Selector selLeft;
|
|---|
| 266 | LinkSelector selLink = null;
|
|---|
| 267 | Selector selRight = null;
|
|---|
| 268 | }
|
|---|
| 269 | {
|
|---|
| 270 | selLeft=selector() w()
|
|---|
| 271 | (
|
|---|
| 272 | ( <GREATER> { parentSelector = false; } | <LESS> { parentSelector = true; } )
|
|---|
| 273 | ( ( c=condition(Context.LINK) | c=pseudoclass(Context.LINK) ) { conditions.add(c); } )*
|
|---|
| 274 | { selLink = new LinkSelector(conditions); }
|
|---|
| 275 | w()
|
|---|
| 276 | selRight=selector() w()
|
|---|
| 277 | )?
|
|---|
| 278 | { return selRight != null ? new ChildOrParentSelector(selLeft, selLink, selRight, parentSelector) : selLeft; }
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | Selector selector() :
|
|---|
| 282 | {
|
|---|
| 283 | Token base;
|
|---|
| 284 | Condition c;
|
|---|
| 285 | Pair<Integer, Integer> r = null;
|
|---|
| 286 | List<Condition> conditions = new ArrayList<Condition>();
|
|---|
| 287 | String sub = null;
|
|---|
| 288 | }
|
|---|
| 289 | {
|
|---|
| 290 | ( base=<IDENT> | base=<STAR> )
|
|---|
| 291 | ( r=zoom() )?
|
|---|
| 292 | ( ( c=condition(Context.PRIMITIVE) | c=pseudoclass(Context.PRIMITIVE) ) { conditions.add(c); } )*
|
|---|
| 293 | ( sub=subpart() )?
|
|---|
| 294 | { return new GeneralSelector(base.image, r, conditions, sub); }
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | Pair<Integer, Integer> zoom() :
|
|---|
| 298 | {
|
|---|
| 299 | Integer min = 0;
|
|---|
| 300 | Integer max = Integer.MAX_VALUE;
|
|---|
| 301 | }
|
|---|
| 302 | {
|
|---|
| 303 | <PIPE_Z>
|
|---|
| 304 | (
|
|---|
| 305 | <MINUS> max=uint()
|
|---|
| 306 | |
|
|---|
| 307 | LOOKAHEAD(2)
|
|---|
| 308 | min=uint() <MINUS> ( max=uint() )?
|
|---|
| 309 | |
|
|---|
| 310 | min=uint() { max = min; }
|
|---|
| 311 | )
|
|---|
| 312 | { return new Pair<Integer, Integer>(min, max); }
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | Condition condition(Context context) :
|
|---|
| 316 | {
|
|---|
| 317 | Condition c;
|
|---|
| 318 | Expression e;
|
|---|
| 319 | }
|
|---|
| 320 | {
|
|---|
| 321 | <LSQUARE> s()
|
|---|
| 322 | (
|
|---|
| 323 | LOOKAHEAD( simple_key_condition(context) s() <RSQUARE> )
|
|---|
| 324 | c=simple_key_condition(context) s() <RSQUARE> { return c; }
|
|---|
| 325 | |
|
|---|
| 326 | LOOKAHEAD( simple_key_value_condition(context) s() <RSQUARE> )
|
|---|
| 327 | c=simple_key_value_condition(context) s() <RSQUARE> { return c; }
|
|---|
| 328 | |
|
|---|
| 329 | e=expression() <RSQUARE> { return Condition.create(e, context); }
|
|---|
| 330 | )
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | String tag_key() :
|
|---|
| 334 | {
|
|---|
| 335 | String s;
|
|---|
| 336 | Token t;
|
|---|
| 337 | }
|
|---|
| 338 | {
|
|---|
| 339 | s=string() { return s; }
|
|---|
| 340 | |
|
|---|
| 341 | t=<IDENT> { s = t.image; } ( <COLON> t=<IDENT> { s += ':' + t.image; } )* { return s; }
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | Condition simple_key_condition(Context context) :
|
|---|
| 345 | {
|
|---|
| 346 | boolean not = false;
|
|---|
| 347 | boolean yes = false;
|
|---|
| 348 | boolean no = false;
|
|---|
| 349 | String key;
|
|---|
| 350 | }
|
|---|
| 351 | {
|
|---|
| 352 | ( <EXCLAMATION> { not = true; } )?
|
|---|
| 353 | key=tag_key()
|
|---|
| 354 | ( LOOKAHEAD(2) <QUESTION> <EXCLAMATION> { no = true; } )?
|
|---|
| 355 | ( <QUESTION> { yes = true; } )?
|
|---|
| 356 | { return Condition.create(key, not, yes, no, context); }
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | Condition simple_key_value_condition(Context context) :
|
|---|
| 360 | {
|
|---|
| 361 | String key;
|
|---|
| 362 | String val;
|
|---|
| 363 | float f;
|
|---|
| 364 | int i;
|
|---|
| 365 | Condition.Op op;
|
|---|
| 366 | }
|
|---|
| 367 | {
|
|---|
| 368 | key=tag_key() s()
|
|---|
| 369 | (
|
|---|
| 370 | LOOKAHEAD(2)
|
|---|
| 371 | <EQUAL> <TILDE> { op=Condition.Op.REGEX; } s() val=regex()
|
|---|
| 372 | |
|
|---|
| 373 | LOOKAHEAD(2)
|
|---|
| 374 | <EXCLAMATION> <TILDE> { op=Condition.Op.NREGEX; } s() val=regex()
|
|---|
| 375 | |
|
|---|
| 376 | (
|
|---|
| 377 | <EXCLAMATION> <EQUAL> { op=Condition.Op.NEQ; }
|
|---|
| 378 | |
|
|---|
| 379 | <EQUAL> { op=Condition.Op.EQ; }
|
|---|
| 380 | |
|
|---|
| 381 | <TILDE> <EQUAL> { op=Condition.Op.ONE_OF; }
|
|---|
| 382 | |
|
|---|
| 383 | <CARET> <EQUAL> { op=Condition.Op.BEGINS_WITH; }
|
|---|
| 384 | |
|
|---|
| 385 | <DOLLAR> <EQUAL> { op=Condition.Op.ENDS_WITH; }
|
|---|
| 386 | |
|
|---|
| 387 | <STAR> <EQUAL> { op=Condition.Op.CONTAINS; }
|
|---|
| 388 | )
|
|---|
| 389 | s()
|
|---|
| 390 | (
|
|---|
| 391 | LOOKAHEAD(2)
|
|---|
| 392 | i=int_() { val=Integer.toString(i); }
|
|---|
| 393 | |
|
|---|
| 394 | f=float_() { val=Float.toString(f); }
|
|---|
| 395 | |
|
|---|
| 396 | val=string_or_ident()
|
|---|
| 397 | )
|
|---|
| 398 | |
|
|---|
| 399 | (
|
|---|
| 400 | <GREATER_EQUAL> { op=Condition.Op.GREATER_OR_EQUAL; }
|
|---|
| 401 | |
|
|---|
| 402 | <GREATER> { op=Condition.Op.GREATER; }
|
|---|
| 403 | |
|
|---|
| 404 | <LESS_EQUAL> { op=Condition.Op.LESS_OR_EQUAL; }
|
|---|
| 405 | |
|
|---|
| 406 | <LESS> { op=Condition.Op.LESS; }
|
|---|
| 407 | )
|
|---|
| 408 | s()
|
|---|
| 409 | f=float_() { val=Float.toString(f); }
|
|---|
| 410 | )
|
|---|
| 411 | { return Condition.create(key, val, op, context); }
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | Condition pseudoclass(Context context) :
|
|---|
| 415 | {
|
|---|
| 416 | Token t;
|
|---|
| 417 | boolean not = false;
|
|---|
| 418 | }
|
|---|
| 419 | {
|
|---|
| 420 | ( <EXCLAMATION> { not = true; } )?
|
|---|
| 421 | <COLON>
|
|---|
| 422 | t=<IDENT>
|
|---|
| 423 | { return Condition.create(t.image, not, context); }
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 | String subpart() :
|
|---|
| 427 | {
|
|---|
| 428 | Token t;
|
|---|
| 429 | }
|
|---|
| 430 | {
|
|---|
| 431 | <DCOLON>
|
|---|
| 432 | ( t=<IDENT> | t=<STAR> )
|
|---|
| 433 | { return t.image; }
|
|---|
| 434 | }
|
|---|
| 435 |
|
|---|
| 436 | List<Instruction> declaration() :
|
|---|
| 437 | {
|
|---|
| 438 | List<Instruction> ins = new ArrayList<Instruction>();
|
|---|
| 439 | Instruction i;
|
|---|
| 440 | Token key;
|
|---|
| 441 | Object val;
|
|---|
| 442 | }
|
|---|
| 443 | {
|
|---|
| 444 | <LBRACE> w()
|
|---|
| 445 | (
|
|---|
| 446 | key=<IDENT> w() <COLON> w()
|
|---|
| 447 | (
|
|---|
| 448 | LOOKAHEAD( float_array() w() ( <SEMICOLON> | <RBRACE> ) )
|
|---|
| 449 | val=float_array()
|
|---|
| 450 | { ins.add(new Instruction.AssignmentInstruction(key.image, val)); }
|
|---|
| 451 | w()
|
|---|
| 452 | ( <RBRACE> { return ins; } | <SEMICOLON> w() )
|
|---|
| 453 | |
|
|---|
| 454 | LOOKAHEAD( expression() ( <SEMICOLON> | <RBRACE> ) )
|
|---|
| 455 | val=expression()
|
|---|
| 456 | { ins.add(new Instruction.AssignmentInstruction(key.image, val)); }
|
|---|
| 457 | ( <RBRACE> { return ins; } | <SEMICOLON> w() )
|
|---|
| 458 | |
|
|---|
| 459 | val=readRaw() w() { ins.add(new Instruction.AssignmentInstruction(key.image, val)); }
|
|---|
| 460 | )
|
|---|
| 461 | )*
|
|---|
| 462 | <RBRACE>
|
|---|
| 463 | { return ins; }
|
|---|
| 464 | }
|
|---|
| 465 |
|
|---|
| 466 | Expression expression():
|
|---|
| 467 | {
|
|---|
| 468 | List<Expression> args = new ArrayList<Expression>();
|
|---|
| 469 | Expression e;
|
|---|
| 470 | String op = null;
|
|---|
| 471 | }
|
|---|
| 472 | {
|
|---|
| 473 | (
|
|---|
| 474 | <EXCLAMATION> { op = "not"; } w() e=primary() { args.add(e); } w()
|
|---|
| 475 | |
|
|---|
| 476 | <MINUS> { op = "minus"; } w() e=primary() { args.add(e); } w()
|
|---|
| 477 | |
|
|---|
| 478 |
|
|---|
| 479 | (
|
|---|
| 480 | e=primary() { args.add(e); } w()
|
|---|
| 481 | (
|
|---|
| 482 | ( <PLUS> { op = "plus"; } w() e=primary() { args.add(e); } w() )+
|
|---|
| 483 | |
|
|---|
| 484 | ( <STAR> { op = "times"; } w() e=primary() { args.add(e); } w() )+
|
|---|
| 485 | |
|
|---|
| 486 | ( <MINUS> { op = "minus"; } w() e=primary() { args.add(e); } w() )+
|
|---|
| 487 | |
|
|---|
| 488 | ( <SLASH> { op = "divided_by"; } w() e=primary() { args.add(e); } w() )+
|
|---|
| 489 | |
|
|---|
| 490 | <GREATER_EQUAL> { op = "greater_equal"; } w() e=primary() { args.add(e); } w()
|
|---|
| 491 | |
|
|---|
| 492 | <LESS_EQUAL> { op = "less_equal"; } w() e=primary() { args.add(e); } w()
|
|---|
| 493 | |
|
|---|
| 494 | <GREATER> { op = "greater"; } w() e=primary() { args.add(e); } w()
|
|---|
| 495 | |
|
|---|
| 496 | <EQUAL> ( <EQUAL> )? { op = "equal"; } w() e=primary() { args.add(e); } w()
|
|---|
| 497 | |
|
|---|
| 498 | <LESS> { op = "less"; } w() e=primary() { args.add(e); } w()
|
|---|
| 499 | |
|
|---|
| 500 | <AMPERSAND> <AMPERSAND> { op = "and"; } w() e=primary() { args.add(e); } w()
|
|---|
| 501 | |
|
|---|
| 502 | <PIPE> <PIPE> { op = "or"; } w() e=primary() { args.add(e); } w()
|
|---|
| 503 | |
|
|---|
| 504 | <QUESTION> { op = "cond"; } w() e=primary() { args.add(e); } w() <COLON> w() e=primary() { args.add(e); } w()
|
|---|
| 505 | )?
|
|---|
| 506 | )
|
|---|
| 507 | )
|
|---|
| 508 | {
|
|---|
| 509 | if (op == null)
|
|---|
| 510 | return args.get(0);
|
|---|
| 511 | return ExpressionFactory.createFunctionExpression(op, args);
|
|---|
| 512 | }
|
|---|
| 513 | }
|
|---|
| 514 |
|
|---|
| 515 | Expression primary() :
|
|---|
| 516 | {
|
|---|
| 517 | Expression nested;
|
|---|
| 518 | Expression fn;
|
|---|
| 519 | Object lit;
|
|---|
| 520 | }
|
|---|
| 521 | {
|
|---|
| 522 | LOOKAHEAD(3) // both function and identifier start with an identifier (+ optional whitespace)
|
|---|
| 523 | fn=function() { return fn; }
|
|---|
| 524 | |
|
|---|
| 525 | lit=literal() { return new LiteralExpression(lit); }
|
|---|
| 526 | |
|
|---|
| 527 | <LPAR> w() nested=expression() <RPAR> { return nested; }
|
|---|
| 528 | }
|
|---|
| 529 |
|
|---|
| 530 | Expression function() :
|
|---|
| 531 | {
|
|---|
| 532 | Token tmp;
|
|---|
| 533 | Expression arg;
|
|---|
| 534 | String name;
|
|---|
| 535 | List<Expression> args = new ArrayList<Expression>();
|
|---|
| 536 | }
|
|---|
| 537 | {
|
|---|
| 538 | tmp=<IDENT> { name = tmp.image; } w()
|
|---|
| 539 | <LPAR> w()
|
|---|
| 540 | (
|
|---|
| 541 | arg=expression() { args.add(arg); }
|
|---|
| 542 | ( <COMMA> w() arg=expression() { args.add(arg); } )*
|
|---|
| 543 | )?
|
|---|
| 544 | <RPAR>
|
|---|
| 545 | { return ExpressionFactory.createFunctionExpression(name, args); }
|
|---|
| 546 | }
|
|---|
| 547 |
|
|---|
| 548 | Object literal() :
|
|---|
| 549 | {
|
|---|
| 550 | String val;
|
|---|
| 551 | Token t;
|
|---|
| 552 | float f;
|
|---|
| 553 | }
|
|---|
| 554 | {
|
|---|
| 555 | t=<IDENT> { return new Keyword(t.image); }
|
|---|
| 556 | |
|
|---|
| 557 | val=string() { return val; }
|
|---|
| 558 | |
|
|---|
| 559 | <PLUS> f=ufloat() { return new Instruction.RelativeFloat(f); }
|
|---|
| 560 | |
|
|---|
| 561 | f=ufloat() { return f; }
|
|---|
| 562 | |
|
|---|
| 563 | t=<HEXCOLOR> { return Utils.hexToColor(t.image); }
|
|---|
| 564 | }
|
|---|
| 565 |
|
|---|
| 566 | JAVACODE
|
|---|
| 567 | void error_skipto(int kind, MapCSSException me) {
|
|---|
| 568 | if (token.kind == EOF)
|
|---|
| 569 | throw new ParseException("Reached end of file while parsing");
|
|---|
| 570 |
|
|---|
| 571 | Exception e = null;
|
|---|
| 572 | ParseException pe = generateParseException();
|
|---|
| 573 |
|
|---|
| 574 | if (me != null) {
|
|---|
| 575 | me.setLine(pe.currentToken.next.beginLine);
|
|---|
| 576 | me.setColumn(pe.currentToken.next.beginColumn);
|
|---|
| 577 | e = me;
|
|---|
| 578 | } else {
|
|---|
| 579 | e = new ParseException(pe.getMessage()); // prevent memory leak
|
|---|
| 580 | }
|
|---|
| 581 |
|
|---|
| 582 | Main.error("Skipping to the next rule, because of an error:");
|
|---|
| 583 | Main.error(e);
|
|---|
| 584 | if (sheet != null) {
|
|---|
| 585 | sheet.logError(e);
|
|---|
| 586 | }
|
|---|
| 587 | Token t;
|
|---|
| 588 | do {
|
|---|
| 589 | t = getNextToken();
|
|---|
| 590 | } while (t.kind != kind && t.kind != EOF);
|
|---|
| 591 | if (t.kind == EOF)
|
|---|
| 592 | throw new ParseException("Reached end of file while parsing");
|
|---|
| 593 | }
|
|---|
| 594 |
|
|---|
| 595 | JAVACODE
|
|---|
| 596 | /**
|
|---|
| 597 | * read everything to the next semicolon
|
|---|
| 598 | */
|
|---|
| 599 | String readRaw() {
|
|---|
| 600 | Token t;
|
|---|
| 601 | StringBuilder s = new StringBuilder();
|
|---|
| 602 | while (true) {
|
|---|
| 603 | t = getNextToken();
|
|---|
| 604 | if ((t.kind == S || t.kind == STRING || t.kind == UNEXPECTED_CHAR) &&
|
|---|
| 605 | t.image.contains("\n")) {
|
|---|
| 606 | ParseException e = new ParseException(String.format("Warning: end of line while reading an unquoted string at line %s column %s.", t.beginLine, t.beginColumn));
|
|---|
| 607 | Main.error(e);
|
|---|
| 608 | if (sheet != null) {
|
|---|
| 609 | sheet.logError(e);
|
|---|
| 610 | }
|
|---|
| 611 | }
|
|---|
| 612 | if (t.kind == SEMICOLON || t.kind == EOF)
|
|---|
| 613 | break;
|
|---|
| 614 | s.append(t.image);
|
|---|
| 615 | }
|
|---|
| 616 | if (t.kind == EOF)
|
|---|
| 617 | throw new ParseException("Reached end of file while parsing");
|
|---|
| 618 | return s.toString();
|
|---|
| 619 | }
|
|---|
| 620 |
|
|---|