Skip to content

Commit

Permalink
Some small date parsing improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpoole committed Oct 26, 2017
1 parent a634a43 commit ad66ab9
Showing 1 changed file with 44 additions and 24 deletions.
68 changes: 44 additions & 24 deletions src/main/java/ch/poole/openinghoursparser/OpeningHoursParser.jj
Expand Up @@ -26,8 +26,7 @@
package ch.poole.openinghoursparser;
import java.util.ArrayList;import java.util.Collections;
import java.util.Locale;import java.io.ByteArrayInputStream;public class OpeningHoursParser{
boolean strict = false;
boolean startOfRange = false;}PARSER_END(OpeningHoursParser)SKIP :{ "\r"| "\n"| " "| "\t"
boolean strict = false;}PARSER_END(OpeningHoursParser)SKIP :{ "\r"| "\n"| " "| "\t"
| "\u200A"
| "\u2009"
| "\u00A0"
Expand Down Expand Up @@ -276,7 +275,6 @@ WeekDayRange weekday_range() : { String startDay = null; String endDay = nul
DateWithOffset endDate = null;
Token plus = null;
Token to = null;
startOfRange = true;
}
{
(
Expand All @@ -292,9 +290,6 @@ WeekDayRange weekday_range() : { String startDay = null; String endDay = nul
(
( < HYPHEN > | (to = < TO >)
)
{
startOfRange = false;
}
(
endDate = datewithoffset()
{
Expand Down Expand Up @@ -353,12 +348,12 @@ int year() :
)
|
// 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
LOOKAHEAD(12, < MONTH > weekday()"[" (< HYPHEN >)? one2five() "]" ( (< HYPHEN > | < PLUS >) number() "day" ("s")?)?,
LOOKAHEAD(13, < MONTH > weekday()"[" (< HYPHEN >)? one2five() "]" ( (< HYPHEN > | < PLUS >) number() "day" ("s")?)?,
{!(
(getToken(4).kind != HYPHEN
&& (getToken(6).kind == COMMA || (getToken(8).image.equals("day") && (getToken(9).kind == COMMA || (getToken(10).image.equals("s") && getToken(11).kind == COMMA)))))
&& (getToken(6).kind == COMMA || (getToken(8).image.equals("day") && (getToken(9).kind == COMMA || (getToken(10).image.equals("s") && getToken(11).kind == COMMA && getToken(12).kind == WEEKDAY)))))
||(getToken(4).kind == HYPHEN
&& (getToken(7).kind == COMMA || (getToken(9).image.equals("day") && (getToken(10).kind == COMMA || (getToken(11).image.equals("s") && getToken(12).kind == COMMA)))))
&& (getToken(7).kind == COMMA || (getToken(9).image.equals("day") && (getToken(10).kind == COMMA || (getToken(11).image.equals("s") && getToken(13).kind == COMMA && getToken(12).kind == WEEKDAY)))))
)})
(
m = < MONTH >
Expand Down Expand Up @@ -425,23 +420,48 @@ int year() :
Month lastMonth = null; ArrayList < DateRange > result = new ArrayList < DateRange > ();}{
// the fist date either need to be a vardate or have a month
LOOKAHEAD( { (getToken(1).kind == NUMBER && (getToken(2).kind == MONTH || getToken(2).kind == VARDATE))
|| getToken(1).kind == MONTH || getToken(1).kind == VARDATE }) // this should work with regexps mdr = date_range() ( < COMMA > mdr1 = date_range() {
DateWithOffset dwo = mdr1.getStartDate();
// if the month is missing in the start date patch it up here
// in strict mode we will never get to this point
if (dwo.getVarDate()==null && dwo.getMonth()==null) {
if (lastMonth==null) {
DateWithOffset first = mdr.getStartDate();
lastMonth = first.getMonth();
if (lastMonth==null) {
// fail, can't fudge the month
throw new ParseException("Missing month " + token.next.beginLine);
|| getToken(1).kind == MONTH || getToken(1).kind == VARDATE })
(
mdr = date_range() (
< COMMA > mdr1 = date_range()
{
DateWithOffset dwo = mdr1.getStartDate();
// if the month is missing in the start date patch it up here
// in strict mode we will never get to this point
if (dwo.getVarDate() == null)
{
if (dwo.getMonth() == null)
{
if (strict) {
throw new ParseException("Missing start month " + token.next.beginLine);
}
if (lastMonth == null)
{
DateWithOffset first = mdr.getStartDate();
lastMonth = first.getMonth();
if (lastMonth == null)
{
// fail, can't fudge the month
throw new ParseException("Missing start month " + token.next.beginLine);
}
}
dwo.setMonth(lastMonth);
}
else if (dwo.getMonth() != null)
{
lastMonth = dwo.getMonth();
}
}
dwo.setMonth(lastMonth);
} else if (dwo.getMonth()!=null) {
lastMonth = dwo.getMonth();
} result.add(0, mdr1); } )* { result.add(mdr); Collections.reverse(result); return result; }}int weeknum() :{ Token n = null;}{ ( LOOKAHEAD({ getToken(1).kind == NUMBER && Integer.parseInt(getToken(1).image) >= 1 && Integer.parseInt(getToken(1).image) <= 54 }) n = < NUMBER > ) { return Integer.parseInt(n.image); }}WeekRange week_range() :{ WeekRange wr = new WeekRange(); Token w = null; Token w1 = null;
result.add(0, mdr1);
}
)*
{
result.add(mdr);
Collections.reverse(result);
return result;
}
)
}int weeknum() :{ Token n = null;}{ ( LOOKAHEAD({ getToken(1).kind == NUMBER && Integer.parseInt(getToken(1).image) >= 1 && Integer.parseInt(getToken(1).image) <= 54 }) n = < NUMBER > ) { return Integer.parseInt(n.image); }}WeekRange week_range() :{ WeekRange wr = new WeekRange(); Token w = null; Token w1 = null;
Token to = null;}{ wr.startWeek = weeknum() ( ( < HYPHEN > | (to = < TO >)
)
(
Expand Down

0 comments on commit ad66ab9

Please sign in to comment.