Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of holidays intermingled with weekdays #91

Merged
merged 2 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 50 additions & 34 deletions src/main/java/ch/poole/openinghoursparser/OpeningHoursParser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ options

static = false;

LOOKAHEAD= 5;
LOOKAHEAD= 6;
IGNORE_CASE = true;
FORCE_LA_CHECK = true;
CHOICE_AMBIGUITY_CHECK = 2;
Expand Down Expand Up @@ -757,7 +757,7 @@ WeekDayRange weekday_range() :
wdr.offset = Integer.parseInt(n.image);
if (m != null)
{
wdr.offset = wdr.offset * - 1;
wdr.offset = wdr.offset * -1;
}
}
)?
Expand Down Expand Up @@ -851,7 +851,7 @@ Holiday holiday() :
result.offset = Integer.parseInt(n.image);
if (m != null)
{
result.offset = result.offset * - 1;
result.offset = result.offset * -1;
}
}
)?
Expand Down Expand Up @@ -1326,6 +1326,7 @@ Rule rule() :
List < WeekRange > ws = null;
List < DateRange > ms = null;
List < Holiday > hs = null;
List < Holiday > hs2 = null;
List < WeekDayRange > ds = null;
List < WeekDayRange > ds2 = null;
List < TimeSpan > ts = null;
Expand Down Expand Up @@ -1389,51 +1390,66 @@ Rule rule() :
|
(
(
LOOKAHEAD(weekday_selector() < COMMA > holiday_sequence())
(
ds = weekday_selector()
< COMMA >
hs = holiday_sequence()
(
< COMMA >
ds2 = weekday_selector()
)?
)
{
if (ds2 != null) {
if (strict)
{
ParseException e = new OpeningHoursParseException(tr("holiday_in_weekday_range"), token.next);
e.currentToken = token;
throw e;
}
ds.addAll(ds2);
}
for (Holiday h:hs) {
h.setAfterWeekDays(true);
}
}
|
)?
(
(
hs = holiday_sequence()
)?
(
(
( (
< COMMA >
ds = weekday_selector()
{
if (hs != null) {
for (Holiday h:hs) {
h.setUseAsWeekDay(true);
}
}
}
)
|
ds = weekday_selector()
{
if (hs != null) {
for (Holiday h:hs) {
h.setUseAsWeekDay(false);
h.setUseAsWeekDay(false);
}
}
}
)?
)
)
(
< COMMA >
hs2 = holiday_sequence()
{
if (hs2 != null) {
for (Holiday h:hs2) {
h.setUseAsWeekDay(true);
h.setAfterWeekDays(true);
}
}
if (hs != null) {
hs.addAll(hs2);
} else {
hs = hs2;
}
}
(
< COMMA >
ds2 = weekday_selector()
{
if (strict && hs2 != null)
{
ParseException e = new OpeningHoursParseException(tr("holiday_in_weekday_range"), token.next);
e.currentToken = token;
throw e;
}
if (ds != null) {
ds.addAll(ds2);
} else {
ds = ds2;
}
}
)*
)*
)?
)
// spec is not clear on this, however ":" is used in real life here a lot
(
Expand Down
30 changes: 1 addition & 29 deletions src/main/java/ch/poole/openinghoursparser/WeekDayRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,35 +88,7 @@ public String toString() {

@Override
public String toDebugString() {
StringBuilder b = new StringBuilder();
b.append(getClass().getSimpleName() + ":");
b.append(startDay);
if (endDay != null) {
b.append("-");
b.append(endDay);
} else if (nths != null && !nths.isEmpty()) {
b.append("[");
for (Nth n : nths) {
b.append(n.toDebugString());
if (!n.equals(nths.get(nths.size() - 1))) {
b.append(",");
}
}
b.append("]");
if (offset != 0) {
if (offset > 0) {
b.append(" +");
} else {
b.append(" -");
}
b.append(String.format(Locale.US, "%d", Math.abs(offset)));
b.append(" day");
if (Math.abs(offset) > 1) {
b.append("s");
}
}
}
return b.toString();
return getClass().getSimpleName() + ":" + toString();
}

@Override
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/ch/poole/openinghoursparser/UnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ public void holidaysVsWeekdays() {
} catch (ParseException pex) {
fail(pex.getMessage());
}
try {
OpeningHoursParser parser = new OpeningHoursParser(new ByteArrayInputStream("Su,PH,Mo 10:00-12:00".getBytes()));
List<Rule> rules = parser.rules(false);
assertEquals(1, rules.size());
} catch (ParseException pex) {
fail(pex.getMessage());
}
try {
OpeningHoursParser parser = new OpeningHoursParser(new ByteArrayInputStream("Su,PH,Mo 10:00-12:00".getBytes()));
List<Rule> rules = parser.rules(true);
Expand Down
2 changes: 2 additions & 0 deletions test-data/oh.txt-fail-debug
Original file line number Diff line number Diff line change
Expand Up @@ -25529,6 +25529,8 @@ Was expecting: <EOF>
Was expecting: <EOF>
156199 Tu-We 9:30-18:00; Th 9:30-20:00; Fr 9:30-18:00; Sa 9:00-last appointment ch.poole.openinghoursparser.OpeningHoursParseException: Encountered: <HYPHEN> "- " at line 1, column 54
Was expecting: <EOF>
156521 Tu,We,Th 08:30-10:30,15:00-18:00; Fr 08:30-10:30; SH Tu, SH Th 15:00-18:00; SH Fr 09:00-12:00 ch.poole.openinghoursparser.OpeningHoursParseException: Encountered: <WEEKDAY> "Th " at line 1, column 58
Was expecting: <EOF>
156564 Tu,We,Th,Sa 15:00-19:00; Fr 10:00-19:00; Mo off; chiuso; Su[1] 15:00-19:00 ch.poole.openinghoursparser.OpeningHoursParseException: Encountered: <UNEXPECTED_CHAR> "c " at line 1, column 48
Was expecting: <EOF>
156575 Tu-Wo 09:00-18:00;Th 09:00-21:00;Fr 09:00-18:00; Sa 09:00-17:00 ch.poole.openinghoursparser.OpeningHoursParseException: Encountered: <HYPHEN> "- " at line 1, column 1
Expand Down
Loading
Loading