| 1 |
/** |
| 2 |
* Parser for OpenStreetMap opening_hours values |
| 3 |
* See <a href="http://wiki.openstreetmap.org/wiki/Key:opening_hours/specification>http://wiki.openstreetmap.org/wiki/Key:opening_hours/specification</a> |
| 4 |
* |
| 5 |
* @author Simon Poole |
| 6 |
* |
| 7 |
* Copyright (c) 2015 Simon Poole |
| 8 |
* |
| 9 |
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| 10 |
* and associated documentation files (the "Software"), to deal in the Software without restriction, |
| 11 |
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 12 |
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, |
| 13 |
* subject to the following conditions: |
| 14 |
* |
| 15 |
* The above copyright notice and this permission notice shall be included in all copies |
| 16 |
* or substantial portions of the Software. |
| 17 |
* |
| 18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| 19 |
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 22 |
" OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
options |
| 26 |
{ |
| 27 |
JDK_VERSION = "1.8"; |
| 28 |
|
| 29 |
static = false; |
| 30 |
|
| 31 |
LOOKAHEAD= 6; |
| 32 |
IGNORE_CASE = true; |
| 33 |
FORCE_LA_CHECK = true; |
| 34 |
CHOICE_AMBIGUITY_CHECK = 2; |
| 35 |
OTHER_AMBIGUITY_CHECK = 2; |
| 36 |
DEBUG_PARSER = false; |
| 37 |
DEBUG_LOOKAHEAD = false; |
| 38 |
DEBUG_TOKEN_MANAGER = false; |
| 39 |
UNICODE_INPUT = true; |
| 40 |
} |
| 41 |
|
| 42 |
PARSER_BEGIN(OpeningHoursParser) |
| 43 |
|
| 44 |
package ch.poole.openinghoursparser; |
| 45 |
|
| 46 |
import java.util.ArrayList; |
| 47 |
import java.util.List; |
| 48 |
import java.util.Collections; |
| 49 |
import java.util.Locale; |
| 50 |
import java.io.ByteArrayInputStream; |
| 51 |
import java.io.IOException; |
| 52 |
|
| 53 |
import ch.poole.openinghoursparser.Util; |
| 54 |
|
| 55 |
import static ch.poole.openinghoursparser.I18n.tr; |
| 56 |
|
| 57 |
public class OpeningHoursParser |
| 58 |
{ |
| 59 |
static int TWELVEHOURS = 12*60; |
| 60 |
|
| 61 |
boolean strict = false; |
| 62 |
boolean strictTime = false; |
| 63 |
enum AMPM { AM, PM }; |
| 64 |
boolean monthOnly = false; |
| 65 |
List < ParseException > exceptions = new ArrayList < ParseException > (); |
| 66 |
|
| 67 |
/** |
| 68 |
* Get the output of the parser |
| 69 |
* |
| 70 |
* @param strict if true the parser will run in strict mode |
| 71 |
* @return a List of Rule elements |
| 72 |
* @throws OpeningHoursParseException on parse errors |
| 73 |
*/ |
| 74 |
public List < Rule > rules(boolean strict) throws OpeningHoursParseException |
| 75 |
{ |
| 76 |
return rules(strict, true); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Get the output of the parser |
| 81 |
* |
| 82 |
* @param strict if true the parser will run in strict mode |
| 83 |
* @param strictTime if false and strict is true, earlier than the start time endings will be fixed up silently |
| 84 |
* @return a List of Rule elements |
| 85 |
* @throws OpeningHoursParseException on parse errors |
| 86 |
*/ |
| 87 |
public List < Rule > rules(boolean strict, boolean strictTime) throws OpeningHoursParseException |
| 88 |
{ |
| 89 |
try |
| 90 |
{ |
| 91 |
List < Rule > r = rules0(strict, strictTime); |
| 92 |
if (exceptions.size() > 1) |
| 93 |
{ |
| 94 |
throw new OpeningHoursParseException(exceptions); |
| 95 |
} |
| 96 |
else if (exceptions.size() == 1) |
| 97 |
{ |
| 98 |
ParseException t = exceptions.get(0); |
| 99 |
if (t instanceof OpeningHoursParseException) |
| 100 |
{ |
| 101 |
throw (OpeningHoursParseException) t; |
| 102 |
} |
| 103 |
throw new OpeningHoursParseException(t); |
| 104 |
} |
| 105 |
return r; |
| 106 |
} |
| 107 |
catch (ParseException pex) |
| 108 |
{ // signature requires this |
| 109 |
throw (OpeningHoursParseException) pex; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
PARSER_END(OpeningHoursParser) |
| 115 |
|
| 116 |
JAVACODE |
| 117 |
|
| 118 |
void error_skip(ParseException e) { |
| 119 |
exceptions.add(e); |
| 120 |
Token t; |
| 121 |
do { |
| 122 |
t = getNextToken(); |
| 123 |
} while (t.kind != SEMICOLON && t.kind != COMMA && t.kind != FALLBACKSEPERATOR && t.kind != WS && t.kind != HYPHEN && t.kind != EOF); |
| 124 |
} |
| 125 |
|
| 126 |
JAVACODE |
| 127 |
|
| 128 |
boolean precedingWs(Token t) { |
| 129 |
return t.specialToken != null && t.specialToken.kind == WS; |
| 130 |
} |
| 131 |
|
| 132 |
SPECIAL_TOKEN : |
| 133 |
{ |
| 134 |
< WS : ["\r", "\n", " ", "\t", "\u200A", "\u2009", "\u00A0", "\u2008", "\u2002", "\u2007", "\u3000", "\u2003", "\u2006", "\u2005", "\u2004"] > |
| 135 |
} |
| 136 |
|
| 137 |
TOKEN : |
| 138 |
{ |
| 139 |
< COMMA : "," > |
| 140 |
| < SEMICOLON : ";" > |
| 141 |
| < COLON : ":" > |
| 142 |
| < PLUS : "+" > |
| 143 |
| < HYPHEN : [ "-", "\u2013", "\u2014" ] > |
| 144 |
| < SLASH : "/" > |
| 145 |
| < STOP : "." > |
| 146 |
| < H : "h" > |
| 147 |
| < BRACKET_LEFT : "[" > |
| 148 |
| < BRACKET_RIGHT : "]" > |
| 149 |
} |
| 150 |
|
| 151 |
TOKEN : |
| 152 |
{ |
| 153 |
< QUOTE : "\"" > : COMMENT_STATE |
| 154 |
} |
| 155 |
|
| 156 |
< COMMENT_STATE > |
| 157 |
TOKEN : |
| 158 |
{ |
| 159 |
< ENDQUOTE : < QUOTE >> : DEFAULT |
| 160 |
| < CHAR : ~[ "\"", "\\" ] > |
| 161 |
} |
| 162 |
|
| 163 |
// parse all numbers as number and do the range checking in syntactic lookahead |
| 164 |
TOKEN : |
| 165 |
{ |
| 166 |
< NUMBER : (< DIGIT >)+ > |
| 167 |
} |
| 168 |
|
| 169 |
TOKEN : |
| 170 |
{ |
| 171 |
< DIGIT : [ "0"-"9" ] > |
| 172 |
} |
| 173 |
|
| 174 |
TOKEN : |
| 175 |
{ |
| 176 |
< FALLBACKSEPERATOR : "||" > |
| 177 |
} |
| 178 |
|
| 179 |
TOKEN : |
| 180 |
{ |
| 181 |
< ALWAYS : "24/7" > |
| 182 |
} |
| 183 |
|
| 184 |
TOKEN : |
| 185 |
{ |
| 186 |
< DAYS : "day" ("s")? > |
| 187 |
| < WEEK : "week" > |
| 188 |
} |
| 189 |
|
| 190 |
TOKEN : |
| 191 |
{ |
| 192 |
< WEEKDAY : |
| 193 |
"Mo" |
| 194 |
| "Tu" |
| 195 |
| "We" |
| 196 |
| "Th" |
| 197 |
| "Fr" |
| 198 |
| "Sa" |
| 199 |
| "Su" > |
| 200 |
{ |
| 201 |
matchedToken.image = Util.capitalize(image.toString()); |
| 202 |
} |
| 203 |
| < WEEKDAY3 : |
| 204 |
"Mon" |
| 205 |
| "Tue" |
| 206 |
| "Wed" |
| 207 |
| "Thu" |
| 208 |
| "Fri" |
| 209 |
| "Sat" |
| 210 |
| "Sun" > |
| 211 |
{ |
| 212 |
matchedToken.image = Util.capitalize(image.toString()).substring(0, 2); |
| 213 |
} |
| 214 |
| |
| 215 |
< WEEKDAYDE : |
| 216 |
"Mo" |
| 217 |
| "Di" |
| 218 |
| "Mi" |
| 219 |
| "Do" |
| 220 |
| "Fr" |
| 221 |
| "Sa" |
| 222 |
| "So" > |
| 223 |
{ |
| 224 |
matchedToken.image = Util.deWeekDays2En(image.toString()); |
| 225 |
} |
| 226 |
| < MONTH : |
| 227 |
"Jan" |
| 228 |
| "Feb" |
| 229 |
| "Mar" |
| 230 |
| "Apr" |
| 231 |
| "May" |
| 232 |
| "Jun" |
| 233 |
| "Jul" |
| 234 |
| "Aug" |
| 235 |
| "Sep" |
| 236 |
| "Oct" |
| 237 |
| "Nov" |
| 238 |
| "Dec" > |
| 239 |
{ |
| 240 |
matchedToken.image = Util.capitalize(image.toString()); |
| 241 |
} |
| 242 |
| < HOLIDAYS : |
| 243 |
"SH" |
| 244 |
| "PH" > |
| 245 |
{ |
| 246 |
matchedToken.image = image.toString().toUpperCase(Locale.US); |
| 247 |
} |
| 248 |
| < EVENTS : |
| 249 |
"sunrise" |
| 250 |
| "sunset" |
| 251 |
| "dawn" |
| 252 |
| "dusk" > |
| 253 |
{ |
| 254 |
matchedToken.image = image.toString().toLowerCase(Locale.US); |
| 255 |
} |
| 256 |
| < VARDATE : "easter" > |
| 257 |
{ |
| 258 |
matchedToken.image = image.toString().toLowerCase(Locale.US); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
TOKEN : |
| 263 |
{ |
| 264 |
< MODIFIER : |
| 265 |
"open" |
| 266 |
| "closed" |
| 267 |
| "off" |
| 268 |
| "unknown" > |
| 269 |
{ |
| 270 |
matchedToken.image = image.toString().toLowerCase(Locale.US); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
TOKEN : |
| 275 |
{ |
| 276 |
< AM : "am" | ("a"< STOP >"m"(< STOP >)?) > |
| 277 |
| < PM : "pm" | ("p"< STOP >"m"(< STOP >)?) > |
| 278 |
} |
| 279 |
|
| 280 |
TOKEN : |
| 281 |
{ |
| 282 |
< TO : " to " | " a " | " \u00E0 " | " \u0434\u043E " | " bis " > |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* match a number between 1 and 5 |
| 287 |
*/ |
| 288 |
Token one2five() : |
| 289 |
{ |
| 290 |
Token n = null; |
| 291 |
} |
| 292 |
{ |
| 293 |
LOOKAHEAD({ getToken(1).kind == NUMBER && Util.between(getToken(1).image, 1, 5) }) |
| 294 |
n = < NUMBER > |
| 295 |
{ |
| 296 |
return n; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* match a positive number larger than 1 |
| 302 |
*/ |
| 303 |
Token number() : |
| 304 |
{ |
| 305 |
Token n = null; |
| 306 |
} |
| 307 |
{ |
| 308 |
LOOKAHEAD({ getToken(1).kind == NUMBER && Util.equalsOrMore(getToken(1).image, 1) }) |
| 309 |
n = < NUMBER > |
| 310 |
{ |
| 311 |
return n; |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Match a comment. |
| 317 |
*/ |
| 318 |
String comment() : |
| 319 |
{ |
| 320 |
StringBuilder builder = new StringBuilder(); |
| 321 |
} |
| 322 |
{ |
| 323 |
< QUOTE > |
| 324 |
( |
| 325 |
getChar(builder) |
| 326 |
)* |
| 327 |
< ENDQUOTE > |
| 328 |
{ |
| 329 |
return builder.toString(); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Match char inside quoted string. |
| 335 |
*/ |
| 336 |
void getChar(StringBuilder builder) : |
| 337 |
{ |
| 338 |
Token t; |
| 339 |
} |
| 340 |
{ |
| 341 |
(t = < CHAR >) |
| 342 |
{ |
| 343 |
if (t.image.length() < 2) |
| 344 |
{ |
| 345 |
// CHAR |
| 346 |
builder.append(t.image.charAt(0)); |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* returns a number Token if between 0 and 24 |
| 353 |
* uses semantic lookahead |
| 354 |
*/ |
| 355 |
Token hours() : |
| 356 |
{ |
| 357 |
Token h = null; |
| 358 |
} |
| 359 |
{ |
| 360 |
LOOKAHEAD({ getToken(1).kind == NUMBER && Util.between(getToken(1).image, 0, 24) }) |
| 361 |
h = < NUMBER > |
| 362 |
{ |
| 363 |
return h; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
/** |
| 368 |
* returns a number Token if between 0 and 59 and starts with a suitable delimiter |
| 369 |
* uses semantic lookahead |
| 370 |
*/ |
| 371 |
Token minutes() : |
| 372 |
{ |
| 373 |
Token delimiter = null; |
| 374 |
Token m = null; |
| 375 |
} |
| 376 |
{ |
| 377 |
( |
| 378 |
( |
| 379 |
(delimiter = < COLON > ) | (delimiter = < STOP > ) | (delimiter= < H >) |
| 380 |
) |
| 381 |
LOOKAHEAD({ getToken(1).kind == NUMBER && Util.between(getToken(1).image, 0, 59) && (getToken(1).image.length() >= 2 || "0".equals(getToken(1).image))}) |
| 382 |
m = < NUMBER > |
| 383 |
) |
| 384 |
{ |
| 385 |
if (strict && (m.image.length() != 2 || delimiter.kind != COLON || precedingWs(m) || precedingWs(delimiter))) { |
| 386 |
ParseException pex = new OpeningHoursParseException(tr("invalid_minutes"), token.next); |
| 387 |
pex.currentToken = token; |
| 388 |
throw pex; |
| 389 |
} |
| 390 |
return m; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* returns the total number of minutes for a time of the form hh:00 or just hh |
| 396 |
* |
| 397 |
* AM/PM handling, see http://www.npl.co.uk/reference/faqs/is-midnight-12-am-or-12-pm-faq-time for why this is a mess |
| 398 |
* 12:01pm to 12:59pm is 12:01 to 12:59 |
| 399 |
* 13:00pm and later is considered to be mistyped and in the 24:00 system |
| 400 |
* 12:00 pm is 12:00 |
| 401 |
* 12:01am to 12:59am is 00:01 to 00:59 |
| 402 |
* 12:00am is 00:00 |
| 403 |
* 13:00am and later is considered to be mistyped and in the 24:00 system |
| 404 |
*/ |
| 405 |
int time() : |
| 406 |
{ |
| 407 |
Token h = null; |
| 408 |
Token m = null; |
| 409 |
int result = 0; |
| 410 |
AMPM ampm = null; |
| 411 |
} |
| 412 |
{ |
| 413 |
( |
| 414 |
h = hours() |
| 415 |
) |
| 416 |
( |
| 417 |
LOOKAHEAD(minutes(), { h!=null }) |
| 418 |
m = minutes() |
| 419 |
)? |
| 420 |
( |
| 421 |
ampm = ampm() |
| 422 |
)? |
| 423 |
{ |
| 424 |
if (strict && m == null) { |
| 425 |
ParseException pex = new OpeningHoursParseException(tr("hours_without_minutes")); |
| 426 |
pex.currentToken = token; |
| 427 |
throw pex; |
| 428 |
} |
| 429 |
if (m != null) { |
| 430 |
result = Integer.parseInt(m.image); |
| 431 |
} |
| 432 |
result = result + Integer.parseInt(h.image) * 60; |
| 433 |
if (ampm == AMPM.PM && result < TWELVEHOURS) { // only use add 12h if the time is less than 12 h |
| 434 |
result = result + TWELVEHOURS; // 12:01 pm to 12:59 pm are already correct 24h times |
| 435 |
} else if (ampm == AMPM.AM && result >= TWELVEHOURS && result < TWELVEHOURS + 60) { |
| 436 |
result = result - TWELVEHOURS; // 12:00am to 12:59am are clearly early morning |
| 437 |
} |
| 438 |
return result; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
AMPM ampm() : |
| 443 |
{ |
| 444 |
String result = null; |
| 445 |
Token ampm = null; |
| 446 |
} |
| 447 |
{ |
| 448 |
( |
| 449 |
( |
| 450 |
ampm =< AM > |
| 451 |
) |
| 452 |
| |
| 453 |
( |
| 454 |
ampm =< PM > |
| 455 |
) |
| 456 |
) |
| 457 |
{ |
| 458 |
if (ampm != null) { |
| 459 |
if (strict) { |
| 460 |
ParseException pex = new OpeningHoursParseException(tr("found", ampm.image), token.next); |
| 461 |
pex.currentToken = token; |
| 462 |
throw pex; |
| 463 |
} |
| 464 |
return ampm.kind == AM ? AMPM.AM : AMPM.PM; |
| 465 |
} |
| 466 |
return null; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
/** |
| 471 |
* returns the total number of minutes for a time of the form hh:00 or just hh with hh up to 48 |
| 472 |
*/ |
| 473 |
int extendedtime() : |
| 474 |
{ |
| 475 |
Token h = null; |
| 476 |
Token m = null; |
| 477 |
int result = 0; |
| 478 |
AMPM ampm = null; |
| 479 |
} |
| 480 |
{ |
| 481 |
( |
| 482 |
LOOKAHEAD({ getToken(1).kind == NUMBER && Util.between(getToken(1).image, 0, 48) }) |
| 483 |
h = < NUMBER > |
| 484 |
) |
| 485 |
( |
| 486 |
LOOKAHEAD(minutes(), { h!=null }) |
| 487 |
m = minutes() |
| 488 |
)? |
| 489 |
( |
| 490 |
ampm = ampm() |
| 491 |
)? |
| 492 |
{ |
| 493 |
if (strict && m == null) { |
| 494 |
ParseException pex = new OpeningHoursParseException(tr("hours_without_minutes")); |
| 495 |
pex.currentToken = token; |
| 496 |
throw pex; |
| 497 |
} |
| 498 |
if (m != null) { |
| 499 |
result = Integer.parseInt(m.image); |
| 500 |
} |
| 501 |
result = result + Integer.parseInt(h.image) * 60; |
| 502 |
if (ampm == AMPM.PM && result < TWELVEHOURS) { |
| 503 |
result = result + TWELVEHOURS; |
| 504 |
} else if (ampm == AMPM.AM && result >= TWELVEHOURS && result < TWELVEHOURS + 60) { |
| 505 |
result = result - TWELVEHOURS; |
| 506 |
} |
| 507 |
return result; |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
VariableTime variabletime() : |
| 512 |
{ |
| 513 |
Token event = null; |
| 514 |
Token sign = null; |
| 515 |
int offset = 0; |
| 516 |
VariableTime vt = new VariableTime(); |
| 517 |
} |
| 518 |
{ |
| 519 |
(event = < EVENTS > |
| 520 |
{ |
| 521 |
vt.setEvent(event.image); |
| 522 |
return vt; |
| 523 |
} |
| 524 |
) |
| 525 |
| |
| 526 |
( |
| 527 |
"(" event = < EVENTS > |
| 528 |
( |
| 529 |
sign = < PLUS > |
| 530 |
| sign = < HYPHEN > |
| 531 |
) |
| 532 |
offset = time() ")" |
| 533 |
{ |
| 534 |
vt.setEvent(event.image); |
| 535 |
vt.offset = (sign.image.equals("-") ? - 1 : 1) * offset; |
| 536 |
} |
| 537 |
) |
| 538 |
{ |
| 539 |
return vt; |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
int interval() : |
| 544 |
{ |
| 545 |
Token number = null; |
| 546 |
Token hours = null; |
| 547 |
int interval = 0; |
| 548 |
} |
| 549 |
{ |
| 550 |
< SLASH > |
| 551 |
( |
| 552 |
LOOKAHEAD(hours() minutes()) |
| 553 |
( |
| 554 |
hours = hours() number = minutes() |
| 555 |
{ |
| 556 |
interval = Integer.parseInt(hours.image) * 60 + Integer.parseInt(number.image); |
| 557 |
} |
| 558 |
) |
| 559 |
| |
| 560 |
( |
| 561 |
( |
| 562 |
number = number() |
| 563 |
) |
| 564 |
{ |
| 565 |
interval = Integer.parseInt(number.image); |
| 566 |
} |
| 567 |
) |
| 568 |
) |
| 569 |
{ |
| 570 |
return interval; |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
TimeSpan timespan() : |
| 575 |
{ |
| 576 |
int start = 0; |
| 577 |
int end =-1; |
| 578 |
Token startplus = null; |
| 579 |
Token endplus = null; |
| 580 |
Token to = null; |
| 581 |
VariableTime startVT = null; |
| 582 |
VariableTime endVT = null; |
| 583 |
TimeSpan ts = new TimeSpan(); |
| 584 |
int inter = 0; |
| 585 |
} |
| 586 |
{ |
| 587 |
( |
| 588 |
( |
| 589 |
start = time() |
| 590 |
{ |
| 591 |
ts.start = start; |
| 592 |
} |
| 593 |
| startVT = variabletime() |
| 594 |
{ |
| 595 |
ts.startEvent = startVT; |
| 596 |
} |
| 597 |
) |
| 598 |
( |
| 599 |
startplus = < PLUS > |
| 600 |
| |
| 601 |
( |
| 602 |
( |
| 603 |
< HYPHEN > | (to = < TO >) |
| 604 |
) |
| 605 |
( |
| 606 |
end = extendedtime() |
| 607 |
{ |
| 608 |
if (end < start) { |
| 609 |
if (!strict || !strictTime) |
| 610 |
{ |
| 611 |
// non-strict, add 24h |
| 612 |
end = end + 24*60; |
| 613 |
} else { |
| 614 |
ParseException pex = new OpeningHoursParseException(tr("earlier_than_time"), token.next); |
| 615 |
pex.currentToken = token; |
| 616 |
throw pex; |
| 617 |
} |
| 618 |
} |
| 619 |
ts.end = end; |
| 620 |
} |
| 621 |
| endVT = variabletime() |
| 622 |
{ |
| 623 |
ts.endEvent = endVT; |
| 624 |
} |
| 625 |
) |
| 626 |
{ |
| 627 |
if (strict && to != null) { |
| 628 |
throw new OpeningHoursParseException(tr("to_instead_of_dash"), token.next); |
| 629 |
} |
| 630 |
} |
| 631 |
( |
| 632 |
endplus = < PLUS > |
| 633 |
| |
| 634 |
( |
| 635 |
inter = interval() |
| 636 |
{ |
| 637 |
ts.interval = inter; |
| 638 |
} |
| 639 |
) |
| 640 |
)? |
| 641 |
) |
| 642 |
)? |
| 643 |
{ |
| 644 |
ts.openEnded = startplus != null || endplus != null; |
| 645 |
return ts; |
| 646 |
} |
| 647 |
) |
| 648 |
} |
| 649 |
|
| 650 |
List < TimeSpan > time_selector() : |
| 651 |
{ |
| 652 |
TimeSpan ts, ts1; |
| 653 |
List < TimeSpan > result = new ArrayList < TimeSpan > (); |
| 654 |
} |
| 655 |
{ |
| 656 |
( |
| 657 |
ts = timespan() |
| 658 |
{ |
| 659 |
result.add(ts); |
| 660 |
} |
| 661 |
( |
| 662 |
< COMMA > ts1 = timespan() |
| 663 |
{ |
| 664 |
result.add(ts1); |
| 665 |
} |
| 666 |
)* |
| 667 |
) |
| 668 |
{ |
| 669 |
return result; |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
String weekday() : |
| 674 |
{ |
| 675 |
Token w = null; |
| 676 |
} |
| 677 |
{ |
| 678 |
(w = < WEEKDAY > |
| 679 |
{ |
| 680 |
return w.image; |
| 681 |
} |
| 682 |
) |
| 683 |
| |
| 684 |
( |
| 685 |
( |
| 686 |
w = < WEEKDAY3 > | w = < WEEKDAYDE > |
| 687 |
) |
| 688 |
{ |
| 689 |
if (strict) { |
| 690 |
String msg = w.kind == OpeningHoursParserConstants.WEEKDAY3 ? tr("invalid_week_day_three") : tr("invalid_week_day_german"); |
| 691 |
ParseException e = new OpeningHoursParseException(msg, token.next); |
| 692 |
e.currentToken = token; |
| 693 |
throw e; |
| 694 |
} |
| 695 |
return w.image; |
| 696 |
} |
| 697 |
) |
| 698 |
} |
| 699 |
|
| 700 |
// Mo- |
| 701 |
// Mo-Fr |
| 702 |
// Mo[1] |
| 703 |
// Mo[1,3,5] |
| 704 |
WeekDayRange weekday_range() : |
| 705 |
{ |
| 706 |
String startDay = null; |
| 707 |
String endDay = null; |
| 708 |
Nth nth = null; |
| 709 |
Nth nth1 = null; |
| 710 |
Token m = null; |
| 711 |
Token n = null; |
| 712 |
Token to = null; |
| 713 |
WeekDayRange wdr = new WeekDayRange(); |
| 714 |
} |
| 715 |
{ |
| 716 |
startDay = weekday() |
| 717 |
( |
| 718 |
( |
| 719 |
( |
| 720 |
( |
| 721 |
< HYPHEN > | (to = < TO >) |
| 722 |
) |
| 723 |
endDay = weekday() |
| 724 |
{ |
| 725 |
if (strict && to != null) { |
| 726 |
throw new OpeningHoursParseException(tr("to_instead_of_dash"), token.next); |
| 727 |
} |
| 728 |
wdr.setEndDay(endDay); |
| 729 |
} |
| 730 |
) |
| 731 |
) |
| 732 |
| |
| 733 |
( |
| 734 |
( |
| 735 |
< BRACKET_LEFT > nth = nth() |
| 736 |
{ |
| 737 |
wdr.add(nth); |
| 738 |
} |
| 739 |
( |
| 740 |
< COMMA > nth1 = nth() |
| 741 |
{ |
| 742 |
wdr.add(nth1); |
| 743 |
} |
| 744 |
)* |
| 745 |
< BRACKET_RIGHT > |
| 746 |
) |
| 747 |
( |
| 748 |
( |
| 749 |
( |
| 750 |
m = < HYPHEN > |
| 751 |
| < PLUS > |
| 752 |
) |
| 753 |
n = number() |
| 754 |
) |
| 755 |
< DAYS > |
| 756 |
{ |
| 757 |
wdr.offset = Integer.parseInt(n.image); |
| 758 |
if (m != null) |
| 759 |
{ |
| 760 |
wdr.offset = wdr.offset * -1; |
| 761 |
} |
| 762 |
} |
| 763 |
)? |
| 764 |
) |
| 765 |
)? |
| 766 |
{ |
| 767 |
wdr.setStartDay(startDay); |
| 768 |
return wdr; |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
Nth nth() : |
| 773 |
{ |
| 774 |
Nth n = new Nth(); |
| 775 |
Token t1 = null; |
| 776 |
Token t2 = null; |
| 777 |
Token sign = null; |
| 778 |
Token sign2 = null; |
| 779 |
} |
| 780 |
{ |
| 781 |
( |
| 782 |
( |
| 783 |
sign = < HYPHEN > |
| 784 |
)? |
| 785 |
t1 = one2five() |
| 786 |
{ |
| 787 |
n.startNth = Integer.parseInt(t1.image); |
| 788 |
if (sign != null) { |
| 789 |
n.startNth = n.startNth * -1; |
| 790 |
} |
| 791 |
} |
| 792 |
)? |
| 793 |
( |
| 794 |
< HYPHEN > |
| 795 |
( |
| 796 |
sign2 = < HYPHEN > |
| 797 |
)? |
| 798 |
t2 = one2five() |
| 799 |
{ |
| 800 |
n.endNth = Integer.parseInt(t2.image); |
| 801 |
if (sign2 != null) { |
| 802 |
n.endNth = n.endNth * -1; |
| 803 |
} |
| 804 |
} |
| 805 |
)? |
| 806 |
{ |
| 807 |
return n; |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
List < WeekDayRange > weekday_selector() : |
| 812 |
{ |
| 813 |
WeekDayRange wdr, wdr1; |
| 814 |
List < WeekDayRange > result = new ArrayList < WeekDayRange > (); |
| 815 |
} |
| 816 |
{ |
| 817 |
wdr = weekday_range() |
| 818 |
{ |
| 819 |
result.add(wdr); |
| 820 |
} |
| 821 |
( |
| 822 |
< COMMA > wdr1 = weekday_range() |
| 823 |
{ |
| 824 |
result.add(wdr1); |
| 825 |
} |
| 826 |
)* |
| 827 |
{ |
| 828 |
return result; |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
Holiday holiday() : |
| 833 |
{ |
| 834 |
Token h = null; |
| 835 |
Token n = null; |
| 836 |
Token m = null; |
| 837 |
Holiday result = new Holiday(); |
| 838 |
} |
| 839 |
{ |
| 840 |
h = < HOLIDAYS > |
| 841 |
( |
| 842 |
( |
| 843 |
( |
| 844 |
m = < HYPHEN > |
| 845 |
| < PLUS > |
| 846 |
) |
| 847 |
n = number() |
| 848 |
) |
| 849 |
< DAYS > |
| 850 |
{ |
| 851 |
result.offset = Integer.parseInt(n.image); |
| 852 |
if (m != null) |
| 853 |
{ |
| 854 |
result.offset = result.offset * -1; |
| 855 |
} |
| 856 |
} |
| 857 |
)? |
| 858 |
{ |
| 859 |
result.type = Holiday.Type.valueOf(h.image.toUpperCase(Locale.US)); // the enum values are in upper case |
| 860 |
return result; |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
List < Holiday > holiday_sequence() : |
| 865 |
{ |
| 866 |
Holiday h = null; |
| 867 |
Holiday h1 = null; |
| 868 |
List < Holiday > result = new ArrayList < Holiday > (); |
| 869 |
} |
| 870 |
{ |
| 871 |
h = holiday() |
| 872 |
{ |
| 873 |
result.add(h); |
| 874 |
} |
| 875 |
( |
| 876 |
< COMMA > h1 = holiday() |
| 877 |
{ |
| 878 |
result.add(h1); |
| 879 |
} |
| 880 |
)* |
| 881 |
{ |
| 882 |
return result; |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
String month() : |
| 887 |
{ |
| 888 |
Token m = null; |
| 889 |
} |
| 890 |
{ |
| 891 |
m = < MONTH > |
| 892 |
{ |
| 893 |
return m.image; |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
DateRange date_range() : |
| 898 |
{ |
| 899 |
DateRange mdr = new DateRange(); |
| 900 |
DateWithOffset startDate = null; |
| 901 |
DateWithOffset endDate = null; |
| 902 |
Token plus = null; |
| 903 |
Token to = null; |
| 904 |
} |
| 905 |
{ |
| 906 |
startDate = datewithoffset(false) |
| 907 |
{ |
| 908 |
if (strict && startDate.getVarDate()==null && startDate.getMonth()==null) { |
| 909 |
throw new OpeningHoursParseException(tr("missing_month"), token.next); |
| 910 |
} |
| 911 |
mdr.startDate = startDate; |
| 912 |
startDate.openEnded = plus != null; |
| 913 |
} |
| 914 |
( |
| 915 |
( |
| 916 |
< HYPHEN > | (to = < TO >) |
| 917 |
) |
| 918 |
( |
| 919 |
endDate = datewithoffset(startDate.undefinedDay()) |
| 920 |
( |
| 921 |
mdr.interval = interval() |
| 922 |
{ |
| 923 |
if (startDate.day != DateWithOffset.UNDEFINED_MONTH_DAY || endDate.day != DateWithOffset.UNDEFINED_MONTH_DAY || startDate.varDate!=null || endDate.varDate!=null) { |
| 924 |
throw new OpeningHoursParseException(tr("interval_not_allowed_here"), token != null ? token.next : null); |
| 925 |
} |
| 926 |
} |
| 927 |
)? |
| 928 |
{ |
| 929 |
if (strict && to != null) { |
| 930 |
throw new OpeningHoursParseException(tr("to_instead_of_dash"), token != null ? token.next : null); |
| 931 |
} |
| 932 |
if ((startDate.undefinedDay() || endDate.undefinedDay()) && mdr.interval == 0 && (!startDate.undefinedYear() || !endDate.undefinedYear())) { |
| 933 |
if (strict) { |
| 934 |
throw new OpeningHoursParseException(tr("missing_day_in_range", startDate.toString(), endDate.toString()), token != null ? token.next : null); |
| 935 |
} else { |
| 936 |
// fix this up |
| 937 |
if (startDate.undefinedDay()) { |
| 938 |
startDate.setDay(1); |
| 939 |
} |
| 940 |
if (endDate.undefinedDay()) { |
| 941 |
endDate.setDay(Month.lastDay(endDate.getYear(), endDate.getMonth())); |
| 942 |
} |
| 943 |
} |
| 944 |
} |
| 945 |
mdr.endDate = endDate; |
| 946 |
} |
| 947 |
) |
| 948 |
| |
| 949 |
< PLUS > |
| 950 |
{ |
| 951 |
startDate.openEnded = true; |
| 952 |
} |
| 953 |
)? |
| 954 |
{ |
| 955 |
return mdr; |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
int daynum() : |
| 960 |
{ |
| 961 |
Token n = null; |
| 962 |
} |
| 963 |
{ |
| 964 |
( |
| 965 |
// FIXME |
| 966 |
LOOKAHEAD({ getToken(1).kind == NUMBER && Util.between(getToken(1).image, 1, 31) && !(getToken(2).kind == COLON && getToken(3).kind == NUMBER && getToken(4).kind != COLON)}) |
| 967 |
n = < NUMBER > |
| 968 |
) |
| 969 |
{ |
| 970 |
if (strict && n.image.length() != 2) { |
| 971 |
ParseException pex = new OpeningHoursParseException(tr("invalid_month_day", n.image), token.next); |
| 972 |
pex.currentToken = token; |
| 973 |
throw pex; |
| 974 |
} |
| 975 |
return Integer.parseInt(n.image); |
| 976 |
} |
| 977 |
} |
| 978 |
|
| 979 |
int year() : |
| 980 |
{ |
| 981 |
Token n = null; |
| 982 |
} |
| 983 |
{ |
| 984 |
( |
| 985 |
LOOKAHEAD({ getToken(1).kind == NUMBER && Util.equalsOrMore(getToken(1).image, 1900)}) |
| 986 |
n = < NUMBER > |
| 987 |
) |
| 988 |
{ |
| 989 |
return Integer.parseInt(n.image); |
| 990 |
} |
| 991 |
} |
| 992 |
|
| 993 |
DateWithOffset datewithoffset(boolean mo) : |
| 994 |
{ |
| 995 |
DateWithOffset dwo = new DateWithOffset(); |
| 996 |
int y = 0; |
| 997 |
Token m = null; |
| 998 |
Token d = null; |
| 999 |
Token h = null; |
| 1000 |
Token n = null; |
| 1001 |
Token v = null; |
| 1002 |
Token minus = null; |
| 1003 |
Token num = null; |
| 1004 |
Token wd = null; |
| 1005 |
Token nth = null; |
| 1006 |
String nthWd = null; |
| 1007 |
Token sign = null; |
| 1008 |
monthOnly = mo; // hack we can't pass an argument to semantic lookahead |
| 1009 |
|
| 1010 |
} |
| 1011 |
{ |
| 1012 |
( |
| 1013 |
y = year() |
| 1014 |
{ |
| 1015 |
dwo.year = y; |
| 1016 |
} |
| 1017 |
)? |
| 1018 |
( |
| 1019 |
( |
| 1020 |
( |
| 1021 |
( |
| 1022 |
// check that the daynum is not the hour part of a time, month should really only be optional if this is the end of a range |
| 1023 |
LOOKAHEAD(((< MONTH >)? daynum()), |
| 1024 |
{!monthOnly && (getToken(1).kind == NUMBER && (getToken(2).kind != COLON || getToken(2).kind != STOP || getToken(2).kind != H)) |
| 1025 |
|| ( getToken(1).kind == MONTH && getToken(2).kind == NUMBER && (getToken(3).kind != COLON || getToken(3).kind != STOP || getToken(3).kind != H)) }) |
| 1026 |
( |
| 1027 |
( |
| 1028 |
m = < MONTH > |
| 1029 |
{ |
| 1030 |
dwo.setMonth(m.image); |
| 1031 |
} |
| 1032 |
)? |
| 1033 |
dwo.day = daynum() |
| 1034 |
) |
| 1035 |
| |
| 1036 |
// check that the occurrence spec is not actually part of a weekday range, the OH specification is ambiguous on how this should be handled if it is not a range |
| 1037 |
// examples |
| 1038 |
// Jan,Feb Sa[-2] -1 day,Sa[-2],Sa[-2] +1 day,Sa[-1] -1 day,Sa[-1],Sa[-1] +1 day .... |
| 1039 |
// Jan,Feb Sa[-2],Sa[-2],Sa[-1] -1 day,Sa[-1],Sa[-1] +1 day |
| 1040 |
LOOKAHEAD(11, < MONTH > weekday() < BRACKET_LEFT > (< HYPHEN >)? one2five() < BRACKET_RIGHT > ( (< HYPHEN > | < PLUS >) number() < DAYS >)?, |
| 1041 |
{ !monthOnly && ((getToken( getToken(4).kind == HYPHEN ? 9 : 8).kind == DAYS && getToken( getToken(4).kind == HYPHEN ? 10 : 9).kind != COMMA) |
| 1042 |
|| (getToken( getToken(4).kind == HYPHEN ? 9 : 8).kind != DAYS && getToken( getToken(4).kind == HYPHEN ? 7 : 6).kind != COMMA) |
| 1043 |
|| (getToken( getToken(4).kind == HYPHEN ? 9 : 8).kind == DAYS && getToken( getToken(4).kind == HYPHEN ? 10 : 9).kind == COMMA && getToken( getToken(4).kind == HYPHEN ? 11 : 10).kind != WEEKDAY) |
| 1044 |
|| (getToken( getToken(4).kind == HYPHEN ? 9 : 8).kind != DAYS && getToken( getToken(4).kind == HYPHEN ? 7 : 6).kind == COMMA && getToken( getToken(4).kind == HYPHEN ? 8 : 7).kind != WEEKDAY))} ) |
| 1045 |
( |
| 1046 |
m = < MONTH > |
| 1047 |
{ |
| 1048 |
dwo.setMonth(m.image); |
| 1049 |
} |
| 1050 |
nthWd = weekday() |
| 1051 |
< BRACKET_LEFT > |
| 1052 |
( |
| 1053 |
sign = < HYPHEN > |
| 1054 |
)? |
| 1055 |
nth = one2five() |
| 1056 |
< BRACKET_RIGHT > |
| 1057 |
) |
| 1058 |
{ |
| 1059 |
dwo.nthWeekDay = WeekDay.getValue(nthWd); |
| 1060 |
dwo.nth = Integer.parseInt(nth.image); |
| 1061 |
if (sign != null) { |
| 1062 |
dwo.nth = dwo.nth * -1; |
| 1063 |
} |
| 1064 |
} |
| 1065 |
) |
| 1066 |
| |
| 1067 |
( |
| 1068 |
v = < VARDATE > |
| 1069 |
{ |
| 1070 |
dwo.setVarDate(v.image); |
| 1071 |
} |
| 1072 |
) |
| 1073 |
) |
| 1074 |
( |
| 1075 |
// This tries to, as lax as possible, differentiate between Jan 31+ Mo and Jan 31+Mo, that is an open ended date vs date with an offset |
| 1076 |
LOOKAHEAD(5, ( < HYPHEN > | < PLUS > ) < WEEKDAY >, { getToken(1).kind == HYPHEN || !precedingWs(getToken(2)) || getToken(5).kind == DAYS }) |
| 1077 |
( |
| 1078 |
minus = < HYPHEN > |
| 1079 |
| < PLUS > |
| 1080 |
) |
| 1081 |
wd = < WEEKDAY > |
| 1082 |
{ |
| 1083 |
dwo.weekDayOffsetPositive = minus == null; |
| 1084 |
dwo.setWeekDayOffset(wd.image); |
| 1085 |
} |
| 1086 |
)? |
| 1087 |
( |
| 1088 |
( |
| 1089 |
( |
| 1090 |
minus = < HYPHEN > |
| 1091 |
| < PLUS > |
| 1092 |
) |
| 1093 |
num = number() |
| 1094 |
< DAYS > |
| 1095 |
) |
| 1096 |
{ |
| 1097 |
dwo.dayOffset = Integer.parseInt(num.image) * (minus != null ? - 1 : 1); |
| 1098 |
} |
| 1099 |
)? |
| 1100 |
) |
| 1101 |
| |
| 1102 |
( |
| 1103 |
m = < MONTH > |
| 1104 |
{ |
| 1105 |
dwo.setMonth(m.image); |
| 1106 |
} |
| 1107 |
) |
| 1108 |
) |
| 1109 |
{ |
| 1110 |
return dwo; |
| 1111 |
} |
| 1112 |
} |
| 1113 |
|
| 1114 |
List < DateRange > date_selector() : |
| 1115 |
{ |
| 1116 |
DateRange mdr, mdr1; |
| 1117 |
Month lastMonth = null; |
| 1118 |
int lastYear = YearRange.UNDEFINED_YEAR; |
| 1119 |
List < DateRange > result = new ArrayList < DateRange > (); |
| 1120 |
} |
| 1121 |
{ |
| 1122 |
// the first date either need to be a vardate or have a month |
| 1123 |
LOOKAHEAD( { (getToken(1).kind == NUMBER && (getToken(2).kind == MONTH || getToken(2).kind == VARDATE)) |
| 1124 |
|| getToken(1).kind == MONTH || getToken(1).kind == VARDATE }) |
| 1125 |
( |
| 1126 |
mdr = date_range() |
| 1127 |
{ |
| 1128 |
result.add(mdr); |
| 1129 |
} |
| 1130 |
( |
| 1131 |
< COMMA > mdr1 = date_range() |
| 1132 |
{ |
| 1133 |
DateWithOffset dwo = mdr1.getStartDate(); |
| 1134 |
// if the month and maybe the year is missing in the start date patch it up here |
| 1135 |
// in strict mode we will never get to this point |
| 1136 |
if (dwo.getVarDate() == null) |
| 1137 |
{ |
| 1138 |
if (dwo.getMonth() == null) |
| 1139 |
{ |
| 1140 |
if (strict) { |
| 1141 |
throw new OpeningHoursParseException(tr("missing_month_start"), token.next); |
| 1142 |
} |
| 1143 |
if (lastMonth == null) |
| 1144 |
{ |
| 1145 |
DateWithOffset first = mdr.getStartDate(); |
| 1146 |
lastMonth = first.getMonth(); |
| 1147 |
lastYear = first.getYear(); |
| 1148 |
if (lastMonth == null) |
| 1149 |
{ |
| 1150 |
// fail, can't fudge the month |
| 1151 |
throw new OpeningHoursParseException(tr("missing_month_start"), token.next); |
| 1152 |
} |
| 1153 |
} |
| 1154 |
dwo.setMonth(lastMonth); |
| 1155 |
dwo.setYear(lastYear); |
| 1156 |
} |
| 1157 |
else |
| 1158 |
{ |
| 1159 |
lastMonth = dwo.getMonth(); |
| 1160 |
lastYear = dwo.getYear(); |
| 1161 |
} |
| 1162 |
} |
| 1163 |
result.add(mdr1); |
| 1164 |
} |
| 1165 |
)* |
| 1166 |
{ |
| 1167 |
return result; |
| 1168 |
} |
| 1169 |
) |
| 1170 |
} |
| 1171 |
|
| 1172 |
int weeknum() : |
| 1173 |
{ |
| 1174 |
Token n = null; |
| 1175 |
} |
| 1176 |
{ |
| 1177 |
( |
| 1178 |
LOOKAHEAD({ getToken(1).kind == NUMBER && Util.between(getToken(1).image, 1, 54) }) |
| 1179 |
n = < NUMBER > |
| 1180 |
) |
| 1181 |
{ |
| 1182 |
return Integer.parseInt(n.image); |
| 1183 |
} |
| 1184 |
} |
| 1185 |
|
| 1186 |
WeekRange week_range() : |
| 1187 |
{ |
| 1188 |
WeekRange wr = new WeekRange(); |
| 1189 |
Token w = null; |
| 1190 |
Token w1 = null; |
| 1191 |
Token to = null; |
| 1192 |
} |
| 1193 |
{ |
| 1194 |
wr.startWeek = weeknum() |
| 1195 |
( |
| 1196 |
( |
| 1197 |
< HYPHEN > | (to = < TO >) |
| 1198 |
) |
| 1199 |
( |
| 1200 |
wr.endWeek = weeknum() |
| 1201 |
) |
| 1202 |
{ |
| 1203 |
if (strict && to != null) { |
| 1204 |
throw new OpeningHoursParseException(tr("to_instead_of_dash"), token.next); |
| 1205 |
} |
| 1206 |
} |
| 1207 |
( |
| 1208 |
wr.interval = interval() |
| 1209 |
)? |
| 1210 |
)? |
| 1211 |
{ |
| 1212 |
return wr; |
| 1213 |
} |
| 1214 |
} |
| 1215 |
|
| 1216 |
List < WeekRange > week_selector() : |
| 1217 |
{ |
| 1218 |
WeekRange wr, wr1; |
| 1219 |
List < WeekRange > result = new ArrayList < WeekRange > (); |
| 1220 |
} |
| 1221 |
{ |
| 1222 |
< WEEK > wr = week_range() |
| 1223 |
{ |
| 1224 |
result.add(wr); |
| 1225 |
} |
| 1226 |
( |
| 1227 |
< COMMA > wr1 = week_range() |
| 1228 |
{ |
| 1229 |
result.add(wr1); |
| 1230 |
} |
| 1231 |
)* |
| 1232 |
{ |
| 1233 |
return result; |
| 1234 |
} |
| 1235 |
} |
| 1236 |
|
| 1237 |
YearRange year_range() : |
| 1238 |
{ |
| 1239 |
YearRange yr = new YearRange(); |
| 1240 |
Token y = null; |
| 1241 |
Token y1 = null; |
| 1242 |
Token to = null; |
| 1243 |
} |
| 1244 |
{ |
| 1245 |
yr.startYear = year() |
| 1246 |
( |
| 1247 |
( |
| 1248 |
( |
| 1249 |
< HYPHEN > | (to = < TO >) |
| 1250 |
) |
| 1251 |
( |
| 1252 |
yr.endYear = year() |
| 1253 |
) |
| 1254 |
{ |
| 1255 |
if (strict && to != null) { |
| 1256 |
throw new OpeningHoursParseException(tr("to_instead_of_dash"), token.next); |
| 1257 |
} |
| 1258 |
} |
| 1259 |
( |
| 1260 |
yr.interval = interval() |
| 1261 |
)? |
| 1262 |
) |
| 1263 |
| |
| 1264 |
< PLUS > |
| 1265 |
{ |
| 1266 |
yr.openEnded = true; |
| 1267 |
} |
| 1268 |
)? |
| 1269 |
{ |
| 1270 |
return yr; |
| 1271 |
} |
| 1272 |
} |
| 1273 |
|
| 1274 |
|
| 1275 |
List < YearRange > year_selector() : |
| 1276 |
{ |
| 1277 |
YearRange yr, yr1; |
| 1278 |
List < YearRange > result = new ArrayList < YearRange > (); |
| 1279 |
} |
| 1280 |
{ |
| 1281 |
LOOKAHEAD({getToken(2).kind != MONTH && getToken(2).kind != VARDATE}) |
| 1282 |
|
| 1283 |
yr = year_range() |
| 1284 |
{ |
| 1285 |
result.add(yr); |
| 1286 |
} |
| 1287 |
( |
| 1288 |
< COMMA > yr1 = year_range() |
| 1289 |
{ |
| 1290 |
result.add(yr1); |
| 1291 |
} |
| 1292 |
)* |
| 1293 |
{ |
| 1294 |
return result; |
| 1295 |
} |
| 1296 |
} |
| 1297 |
|
| 1298 |
RuleModifier rule_modifier() : |
| 1299 |
{ |
| 1300 |
RuleModifier rm = new RuleModifier(); |
| 1301 |
Token m = null; |
| 1302 |
String c = null; |
| 1303 |
} |
| 1304 |
{ |
| 1305 |
( |
| 1306 |
m = < MODIFIER > |
| 1307 |
{ |
| 1308 |
rm.setModifier(m.image); |
| 1309 |
} |
| 1310 |
)? |
| 1311 |
( |
| 1312 |
c = comment() |
| 1313 |
{ |
| 1314 |
rm.comment = c; |
| 1315 |
} |
| 1316 |
)? |
| 1317 |
{ |
| 1318 |
if (rm.modifier == null && rm.comment == null) { |
| 1319 |
return null; |
| 1320 |
} |
| 1321 |
return rm; |
| 1322 |
} |
| 1323 |
} |
| 1324 |
|
| 1325 |
Rule rule() : |
| 1326 |
{ |
| 1327 |
Rule r = null; |
| 1328 |
String comment = null; |
| 1329 |
boolean always = false; |
| 1330 |
List < YearRange > ys = null; |
| 1331 |
List < WeekRange > ws = null; |
| 1332 |
List < DateRange > ms = null; |
| 1333 |
List < Holiday > hs = null; |
| 1334 |
List < Holiday > hs2 = null; |
| 1335 |
List < WeekDayRange > ds = null; |
| 1336 |
List < WeekDayRange > ds2 = null; |
| 1337 |
List < TimeSpan > ts = null; |
| 1338 |
RuleModifier rm = null; |
| 1339 |
boolean colonForClarification = false; |
| 1340 |
} |
| 1341 |
{ |
| 1342 |
( |
| 1343 |
LOOKAHEAD(comment() < COLON >) |
| 1344 |
( |
| 1345 |
comment = comment() |
| 1346 |
< COLON > |
| 1347 |
) |
| 1348 |
| |
| 1349 |
LOOKAHEAD( (year_selector() | week_selector() | date_selector())? ) |
| 1350 |
( |
| 1351 |
( |
| 1352 |
ys = year_selector() |
| 1353 |
)? |
| 1354 |
( |
| 1355 |
ws = week_selector() |
| 1356 |
)? |
| 1357 |
( |
| 1358 |
ms = date_selector() |
| 1359 |
)? |
| 1360 |
( |
| 1361 |
< COLON > |
| 1362 |
{ |
| 1363 |
colonForClarification = true; |
| 1364 |
} |
| 1365 |
)? |
| 1366 |
) |
| 1367 |
) |
| 1368 |
( |
| 1369 |
( |
| 1370 |
< ALWAYS > |
| 1371 |
{ |
| 1372 |
if (strict && (ys != null || ws != null || ms != null)) |
| 1373 |
{ |
| 1374 |
ParseException e = new OpeningHoursParseException(tr("24_7_with_other_selectors")); |
| 1375 |
e.currentToken = token; |
| 1376 |
throw e; |
| 1377 |
} |
| 1378 |
else |
| 1379 |
{ |
| 1380 |
if (ys != null || ws != null || ms != null) { |
| 1381 |
// correct to proper syntax |
| 1382 |
ts = new ArrayList < TimeSpan > (); |
| 1383 |
TimeSpan t = new TimeSpan(); |
| 1384 |
t.start = 0; |
| 1385 |
t.end = 1440; |
| 1386 |
ts.add(t); |
| 1387 |
} |
| 1388 |
else |
| 1389 |
{ |
| 1390 |
always = true; |
| 1391 |
} |
| 1392 |
} |
| 1393 |
} |
| 1394 |
) |
| 1395 |
| |
| 1396 |
( |
| 1397 |
( |
| 1398 |
( |
| 1399 |
hs = holiday_sequence() |
| 1400 |
)? |
| 1401 |
( |
| 1402 |
(
( |
| 1403 |
< COMMA > |
| 1404 |
ds = weekday_selector() |
| 1405 |
{ |
| 1406 |
if (hs != null) { |
| 1407 |
for (Holiday h:hs) { |
| 1408 |
h.setUseAsWeekDay(true); |
| 1409 |
} |
| 1410 |
} |
| 1411 |
} |
| 1412 |
) |
| 1413 |
| |
| 1414 |
ds = weekday_selector() |
| 1415 |
{ |
| 1416 |
if (hs != null) { |
| 1417 |
for (Holiday h:hs) { |
| 1418 |
h.setUseAsWeekDay(false); |
| 1419 |
} |
| 1420 |
} |
| 1421 |
} |
| 1422 |
) |
| 1423 |
( |
| 1424 |
< COMMA > |
| 1425 |
hs2 = holiday_sequence() |
| 1426 |
{ |
| 1427 |
if (hs2 != null) { |
| 1428 |
for (Holiday h:hs2) { |
| 1429 |
h.setUseAsWeekDay(true); |
| 1430 |
h.setAfterWeekDays(true); |
| 1431 |
} |
| 1432 |
} |
| 1433 |
if (hs != null) { |
| 1434 |
hs.addAll(hs2); |
| 1435 |
} else { |
| 1436 |
hs = hs2; |
| 1437 |
} |
| 1438 |
} |
| 1439 |
( |
| 1440 |
< COMMA > |
| 1441 |
ds2 = weekday_selector() |
| 1442 |
{ |
| 1443 |
if (strict && hs2 != null) |
| 1444 |
{ |
| 1445 |
ParseException e = new OpeningHoursParseException(tr("holiday_in_weekday_range"), token.next); |
| 1446 |
e.currentToken = token; |
| 1447 |
throw e; |
| 1448 |
} |
| 1449 |
if (ds != null) { |
| 1450 |
ds.addAll(ds2); |
| 1451 |
} else { |
| 1452 |
ds = ds2; |
| 1453 |
} |
| 1454 |
} |
| 1455 |
)* |
| 1456 |
)* |
| 1457 |
)? |
| 1458 |
) |
| 1459 |
// spec is not clear on this, however ":" is used in real life here a lot |
| 1460 |
( |
| 1461 |
< COLON > |
| 1462 |
)? |
| 1463 |
( |
| 1464 |
ts = time_selector() |
| 1465 |
)? |
| 1466 |
) |
| 1467 |
) |
| 1468 |
( |
| 1469 |
rm = rule_modifier() |
| 1470 |
) |
| 1471 |
{ |
| 1472 |
r = new Rule(); |
| 1473 |
r.comment = comment; |
| 1474 |
r.twentyfourseven = always; |
| 1475 |
r.years = ys; |
| 1476 |
r.weeks = ws; |
| 1477 |
r.dates = ms; |
| 1478 |
r.holidays = hs; |
| 1479 |
r.days = ds; |
| 1480 |
r.times = ts; |
| 1481 |
r.modifier = rm; |
| 1482 |
r.colonForClarification = colonForClarification; |
| 1483 |
return r; |
| 1484 |
} |
| 1485 |
} |
| 1486 |
|
| 1487 |
private List < Rule > rules0(boolean strict, boolean strictTime) : |
| 1488 |
{ |
| 1489 |
this.strict = strict; |
| 1490 |
this.strictTime = strictTime; |
| 1491 |
List < Rule > result = new ArrayList < Rule > (); |
| 1492 |
Rule r = null; |
| 1493 |
Rule r1 = null; |
| 1494 |
} |
| 1495 |
{ |
| 1496 |
try |
| 1497 |
{ |
| 1498 |
( |
| 1499 |
r = rule() |
| 1500 |
) |
| 1501 |
{ |
| 1502 |
result.add(r); |
| 1503 |
} |
| 1504 |
} |
| 1505 |
catch (ParseException e) |
| 1506 |
{ |
| 1507 |
error_skip(e); |
| 1508 |
return rules0(strict, strictTime); |
| 1509 |
} |
| 1510 |
try |
| 1511 |
{ |
| 1512 |
( |
| 1513 |
( |
| 1514 |
< COMMA > |
| 1515 |
( |
| 1516 |
r1 = rule() |
| 1517 |
{ |
| 1518 |
r1.additive = true; |
| 1519 |
r1.fallBack = false; |
| 1520 |
result.add(r1); |
| 1521 |
} |
| 1522 |
) |
| 1523 |
| < SEMICOLON > |
| 1524 |
( |
| 1525 |
r1 = rule() |
| 1526 |
{ |
| 1527 |
r1.additive = false; |
| 1528 |
r1.fallBack = false; |
| 1529 |
result.add(r1); |
| 1530 |
} |
| 1531 |
) |
| 1532 |
| < FALLBACKSEPERATOR > |
| 1533 |
( |
| 1534 |
r1 = rule() |
| 1535 |
{ |
| 1536 |
r1.additive = false; |
| 1537 |
r1.fallBack = true; |
| 1538 |
result.add(r1); |
| 1539 |
} |
| 1540 |
) |
| 1541 |
) |
| 1542 |
) * |
| 1543 |
< EOF > |
| 1544 |
} |
| 1545 |
catch (ParseException e) |
| 1546 |
{ |
| 1547 |
error_skip(e); |
| 1548 |
return rules0(strict, strictTime); |
| 1549 |
} |
| 1550 |
{ |
| 1551 |
r.additive = false; |
| 1552 |
r.fallBack = false; |
| 1553 |
return result; |
| 1554 |
} |
| 1555 |
} |
| 1556 |
|
| 1557 |
< * > TOKEN : |
| 1558 |
{ |
| 1559 |
< UNEXPECTED_CHAR : ~[ ] > |
| 1560 |
} |