Skip to content

Commit fb34875

Browse files
committed
Merge branch 'fix_weekday_range_pretty_printing'
2 parents 42d1821 + 4bed1b1 commit fb34875

File tree

9 files changed

+9967
-9238
lines changed

9 files changed

+9967
-9238
lines changed

src/main/java/ch/poole/openinghoursparser/OpeningHoursParser.jj

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ options
2828

2929
static = false;
3030

31-
LOOKAHEAD= 5;
31+
LOOKAHEAD= 6;
3232
IGNORE_CASE = true;
3333
FORCE_LA_CHECK = true;
3434
CHOICE_AMBIGUITY_CHECK = 2;
@@ -757,7 +757,7 @@ WeekDayRange weekday_range() :
757757
wdr.offset = Integer.parseInt(n.image);
758758
if (m != null)
759759
{
760-
wdr.offset = wdr.offset * - 1;
760+
wdr.offset = wdr.offset * -1;
761761
}
762762
}
763763
)?
@@ -851,7 +851,7 @@ Holiday holiday() :
851851
result.offset = Integer.parseInt(n.image);
852852
if (m != null)
853853
{
854-
result.offset = result.offset * - 1;
854+
result.offset = result.offset * -1;
855855
}
856856
}
857857
)?
@@ -1326,6 +1326,7 @@ Rule rule() :
13261326
List < WeekRange > ws = null;
13271327
List < DateRange > ms = null;
13281328
List < Holiday > hs = null;
1329+
List < Holiday > hs2 = null;
13291330
List < WeekDayRange > ds = null;
13301331
List < WeekDayRange > ds2 = null;
13311332
List < TimeSpan > ts = null;
@@ -1389,51 +1390,66 @@ Rule rule() :
13891390
|
13901391
(
13911392
(
1392-
LOOKAHEAD(weekday_selector() < COMMA > holiday_sequence())
13931393
(
1394-
ds = weekday_selector()
1395-
< COMMA >
13961394
hs = holiday_sequence()
1397-
(
1398-
< COMMA >
1399-
ds2 = weekday_selector()
1400-
)?
1401-
)
1402-
{
1403-
if (ds2 != null) {
1404-
if (strict)
1405-
{
1406-
ParseException e = new OpeningHoursParseException(tr("holiday_in_weekday_range"), token.next);
1407-
e.currentToken = token;
1408-
throw e;
1409-
}
1410-
ds.addAll(ds2);
1411-
}
1412-
for (Holiday h:hs) {
1413-
h.setAfterWeekDays(true);
1414-
}
1415-
}
1416-
|
1395+
)?
14171396
(
1418-
(
1419-
hs = holiday_sequence()
1420-
)?
1421-
(
1422-
(
1397+
( (
14231398
< COMMA >
14241399
ds = weekday_selector()
1400+
{
1401+
if (hs != null) {
1402+
for (Holiday h:hs) {
1403+
h.setUseAsWeekDay(true);
1404+
}
1405+
}
1406+
}
14251407
)
14261408
|
14271409
ds = weekday_selector()
14281410
{
14291411
if (hs != null) {
14301412
for (Holiday h:hs) {
1431-
h.setUseAsWeekDay(false);
1413+
h.setUseAsWeekDay(false);
14321414
}
14331415
}
14341416
}
1435-
)?
1436-
)
1417+
)
1418+
(
1419+
< COMMA >
1420+
hs2 = holiday_sequence()
1421+
{
1422+
if (hs2 != null) {
1423+
for (Holiday h:hs2) {
1424+
h.setUseAsWeekDay(true);
1425+
h.setAfterWeekDays(true);
1426+
}
1427+
}
1428+
if (hs != null) {
1429+
hs.addAll(hs2);
1430+
} else {
1431+
hs = hs2;
1432+
}
1433+
}
1434+
(
1435+
< COMMA >
1436+
ds2 = weekday_selector()
1437+
{
1438+
if (strict && hs2 != null)
1439+
{
1440+
ParseException e = new OpeningHoursParseException(tr("holiday_in_weekday_range"), token.next);
1441+
e.currentToken = token;
1442+
throw e;
1443+
}
1444+
if (ds != null) {
1445+
ds.addAll(ds2);
1446+
} else {
1447+
ds = ds2;
1448+
}
1449+
}
1450+
)*
1451+
)*
1452+
)?
14371453
)
14381454
// spec is not clear on this, however ":" is used in real life here a lot
14391455
(

src/main/java/ch/poole/openinghoursparser/WeekDayRange.java

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -88,35 +88,7 @@ public String toString() {
8888

8989
@Override
9090
public String toDebugString() {
91-
StringBuilder b = new StringBuilder();
92-
b.append(getClass().getSimpleName() + ":");
93-
b.append(startDay);
94-
if (endDay != null) {
95-
b.append("-");
96-
b.append(endDay);
97-
} else if (nths != null && !nths.isEmpty()) {
98-
b.append("[");
99-
for (Nth n : nths) {
100-
b.append(n.toDebugString());
101-
if (!n.equals(nths.get(nths.size() - 1))) {
102-
b.append(",");
103-
}
104-
}
105-
b.append("]");
106-
if (offset != 0) {
107-
if (offset > 0) {
108-
b.append(" +");
109-
} else {
110-
b.append(" -");
111-
}
112-
b.append(String.format(Locale.US, "%d", Math.abs(offset)));
113-
b.append(" day");
114-
if (Math.abs(offset) > 1) {
115-
b.append("s");
116-
}
117-
}
118-
}
119-
return b.toString();
91+
return getClass().getSimpleName() + ":" + toString();
12092
}
12193

12294
@Override

src/test/java/ch/poole/openinghoursparser/UnitTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ public void holidaysVsWeekdays() {
6666
} catch (ParseException pex) {
6767
fail(pex.getMessage());
6868
}
69+
try {
70+
OpeningHoursParser parser = new OpeningHoursParser(new ByteArrayInputStream("Su,PH,Mo 10:00-12:00".getBytes()));
71+
List<Rule> rules = parser.rules(false);
72+
assertEquals(1, rules.size());
73+
} catch (ParseException pex) {
74+
fail(pex.getMessage());
75+
}
6976
try {
7077
OpeningHoursParser parser = new OpeningHoursParser(new ByteArrayInputStream("Su,PH,Mo 10:00-12:00".getBytes()));
7178
List<Rule> rules = parser.rules(true);

test-data/oh.txt-fail-debug

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25529,6 +25529,8 @@ Was expecting: <EOF>
2552925529
Was expecting: <EOF>
2553025530
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
2553125531
Was expecting: <EOF>
25532+
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
25533+
Was expecting: <EOF>
2553225534
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
2553325535
Was expecting: <EOF>
2553425536
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

0 commit comments

Comments
 (0)