From: cesarvh Date: Tue, 30 Oct 2018 19:00:16 +0000 (-0700) Subject: UJ-2: Split up rules to lower ambiguity X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=5a1d1546f924f8ba063fe51ca372a2b06566af45;p=tmp%2Fjakarta-migration.git UJ-2: Split up rules to lower ambiguity --- diff --git a/services/structureddate/structureddate/src/main/antlr4/org/collectionspace/services/structureddate/antlr/StructuredDate.g4 b/services/structureddate/structureddate/src/main/antlr4/org/collectionspace/services/structureddate/antlr/StructuredDate.g4 index 63fbdb664..9045f658e 100644 --- a/services/structureddate/structureddate/src/main/antlr4/org/collectionspace/services/structureddate/antlr/StructuredDate.g4 +++ b/services/structureddate/structureddate/src/main/antlr4/org/collectionspace/services/structureddate/antlr/StructuredDate.g4 @@ -57,66 +57,74 @@ quarterInYearRange: nthQuarterInYearRange date: numDate | strDate | invStrDate +| dayFirstDate +| dayOrYearFirstDate +| invStrDateEraLastDate ; month: monthYear | invMonthYear ; -yearSpanningWinter: WINTER COMMA? numYear SLASH numYear era ; +yearSpanningWinter: WINTER COMMA? numYear SLASH numYear era? ; -partialYear: partOf numYear era ; +partialYear: partOf numYear era? ; quarterYear: seasonYear | invSeasonYear | nthQuarterYear ; -halfYear: nthHalf numYear era ; +halfYear: nthHalf numYear era? ; -year: numYear era ; +year: numYear era? ; -partialDecade: partOf numDecade era ; +partialDecade: partOf numDecade era? ; -decade: numDecade era ; +decade: numDecade era? ; -partialCentury: partOf ( strCentury | numCentury ) era ; +partialCentury: partOf ( strCentury | numCentury ) era? ; -quarterCentury: nthQuarter ( strCentury | numCentury ) era ; +quarterCentury: nthQuarter ( strCentury | numCentury ) era? ; -halfCentury: nthHalf ( strCentury | numCentury ) era ; +halfCentury: nthHalf ( strCentury | numCentury ) era? ; -century: ( strCentury | numCentury ) era ; +century: ( strCentury | numCentury ) era? ; -millennium: nth MILLENNIUM era ; +millennium: nth MILLENNIUM era? ; -strDate: strMonth ( numDayOfMonth | nth ) COMMA? numYear era; +strDate: strMonth ( numDayOfMonth | nth ) COMMA? numYear era?; invStrDate: era num COMMA? strMonth num -| ( num | nth ) strMonth COMMA? num era ; -strDayInMonthRange: strMonth numDayOfMonth ( HYPHEN | DASH ) numDayOfMonth COMMA? numYear era ; -monthInYearRange: strMonth ( HYPHEN | DASH ) strMonth COMMA? numYear era ; -nthQuarterInYearRange: nthQuarter ( HYPHEN | DASH ) nthQuarter COMMA? numYear era ; -strSeasonInYearRange: strSeason ( HYPHEN | DASH ) strSeason COMMA? numYear era ; -numDayInMonthRange: numMonth SLASH numDayOfMonth ( HYPHEN | DASH ) numDayOfMonth SLASH numYear era ; -numDate: num SLASH num SLASH num era -| num HYPHEN num HYPHEN num era ; -monthYear: strMonth COMMA? numYear era ; -invMonthYear: era numYear COMMA? strMonth ; -seasonYear: strSeason COMMA? numYear era ; -invSeasonYear: era numYear COMMA? strSeason ; -nthQuarterYear: nthQuarter numYear era ; +| era? num COMMA strMonth num ; +dayFirstDate: num strMonth COMMA? num era +| num strMonth COMMA num era? +| nth strMonth COMMA? num era? ; +dayOrYearFirstDate: num strMonth num ; +invStrDateEraLastDate: num COMMA strMonth num era? ; +strDayInMonthRange: strMonth numDayOfMonth ( HYPHEN | DASH ) numDayOfMonth COMMA? numYear era? ; +monthInYearRange: strMonth ( HYPHEN | DASH ) strMonth COMMA? numYear era? ; +nthQuarterInYearRange: nthQuarter ( HYPHEN | DASH ) nthQuarter COMMA? numYear era? ; +strSeasonInYearRange: strSeason ( HYPHEN | DASH ) strSeason COMMA? numYear era? ; +numDayInMonthRange: numMonth SLASH numDayOfMonth ( HYPHEN | DASH ) numDayOfMonth SLASH numYear era? ; +numDate: num SLASH num SLASH num era? +| num HYPHEN num HYPHEN num era? ; +monthYear: strMonth COMMA? numYear era? ; +invMonthYear: era? numYear COMMA? strMonth ; +seasonYear: strSeason COMMA? numYear era? ; +invSeasonYear: era? numYear COMMA? strSeason ; +nthQuarterYear: nthQuarter numYear era? ; nthQuarter: ( nth | LAST ) QUARTER ; nthHalf: ( nth | LAST ) HALF ; numDecade: TENS ; strCentury: nth CENTURY ; numCentury: HUNDREDS ; -nthCenturyRange: allOrPartOf nth ( HYPHEN | DASH ) allOrPartOf nth CENTURY era ; +nthCenturyRange: allOrPartOf nth ( HYPHEN | DASH ) allOrPartOf nth CENTURY era? ; strSeason: SPRING | SUMMER | FALL | WINTER ; allOrPartOf: partOf | ; partOf: EARLY | MIDDLE | LATE ; nth: NTHSTR | FIRST | SECOND | THIRD | FOURTH ; strMonth: MONTH | SHORTMONTH DOT? ; -era: BC | AD | ; +era: BC | AD ; numYear: NUMBER ; numMonth: NUMBER ; numDayOfMonth: NUMBER ; diff --git a/services/structureddate/structureddate/src/main/java/org/collectionspace/services/structureddate/antlr/ANTLRStructuredDateEvaluator.java b/services/structureddate/structureddate/src/main/java/org/collectionspace/services/structureddate/antlr/ANTLRStructuredDateEvaluator.java index 5be6cec1b..1160a5aa2 100644 --- a/services/structureddate/structureddate/src/main/java/org/collectionspace/services/structureddate/antlr/ANTLRStructuredDateEvaluator.java +++ b/services/structureddate/structureddate/src/main/java/org/collectionspace/services/structureddate/antlr/ANTLRStructuredDateEvaluator.java @@ -41,6 +41,8 @@ import org.collectionspace.services.structureddate.antlr.StructuredDateParser.Be import org.collectionspace.services.structureddate.antlr.StructuredDateParser.CenturyContext; import org.collectionspace.services.structureddate.antlr.StructuredDateParser.CertainDateContext; import org.collectionspace.services.structureddate.antlr.StructuredDateParser.DateContext; +import org.collectionspace.services.structureddate.antlr.StructuredDateParser.DayFirstDateContext; +import org.collectionspace.services.structureddate.antlr.StructuredDateParser.DayOrYearFirstDateContext; import org.collectionspace.services.structureddate.antlr.StructuredDateParser.DecadeContext; import org.collectionspace.services.structureddate.antlr.StructuredDateParser.DisplayDateContext; import org.collectionspace.services.structureddate.antlr.StructuredDateParser.EraContext; @@ -50,6 +52,7 @@ import org.collectionspace.services.structureddate.antlr.StructuredDateParser.Hy import org.collectionspace.services.structureddate.antlr.StructuredDateParser.InvMonthYearContext; import org.collectionspace.services.structureddate.antlr.StructuredDateParser.InvSeasonYearContext; import org.collectionspace.services.structureddate.antlr.StructuredDateParser.InvStrDateContext; +import org.collectionspace.services.structureddate.antlr.StructuredDateParser.InvStrDateEraLastDateContext; import org.collectionspace.services.structureddate.antlr.StructuredDateParser.MillenniumContext; import org.collectionspace.services.structureddate.antlr.StructuredDateParser.MonthContext; import org.collectionspace.services.structureddate.antlr.StructuredDateParser.MonthInYearRangeContext; @@ -75,6 +78,7 @@ import org.collectionspace.services.structureddate.antlr.StructuredDateParser.Pa import org.collectionspace.services.structureddate.antlr.StructuredDateParser.QuarterCenturyContext; import org.collectionspace.services.structureddate.antlr.StructuredDateParser.QuarterInYearRangeContext; import org.collectionspace.services.structureddate.antlr.StructuredDateParser.QuarterYearContext; +import org.collectionspace.services.structureddate.antlr.StructuredDateParser.SeasonYearContext; import org.collectionspace.services.structureddate.antlr.StructuredDateParser.StrCenturyContext; import org.collectionspace.services.structureddate.antlr.StructuredDateParser.StrDateContext; import org.collectionspace.services.structureddate.antlr.StructuredDateParser.StrDayInMonthRangeContext; @@ -84,8 +88,6 @@ import org.collectionspace.services.structureddate.antlr.StructuredDateParser.St import org.collectionspace.services.structureddate.antlr.StructuredDateParser.UncertainDateContext; import org.collectionspace.services.structureddate.antlr.StructuredDateParser.YearContext; import org.collectionspace.services.structureddate.antlr.StructuredDateParser.YearSpanningWinterContext; -import org.collectionspace.services.structureddate.antlr.StructuredDateParser.SeasonYearContext; - /** * A StructuredDateEvaluator that uses an ANTLR parser to parse the display date, @@ -473,58 +475,68 @@ public class ANTLRStructuredDateEvaluator extends StructuredDateBaseListener imp stack.push(era); } - // @Override - // public void exitInvStrDate(InvStrDateContext ctx) { - // if (ctx.exception != null) return; + @Override + public void exitDayFirstDate(DayFirstDateContext ctx) { + if (ctx.exception != null) return ; + + Era era = (ctx.era() == null) ? null : (Era) stack.pop(); + Integer year = (Integer) stack.pop(); + Integer month = (Integer) stack.pop(); + Integer dayOfMonth = (Integer) stack.pop(); - // // Reorder the stack into a canonical ordering, - // // year-month-day-era. - // Era era = null; + stack.push(year); + stack.push(month); + stack.push(dayOfMonth); + stack.push(era); + } - // boolean eraLast = stack.peek() instanceof Integer; + @Override + public void exitDayOrYearFirstDate(DayOrYearFirstDateContext ctx) { + if (ctx.exception != null) return; - // // Declare nums - // Integer num1; - // Integer num2; - // Integer num3; + Era era = (stack.size() == 3) ? null : (Era) stack.pop(); + Integer year = (Integer) stack.pop(); + Integer numMonth = (Integer) stack.pop(); + Integer dayOfMonth = (Integer) stack.pop(); - // if (!eraLast) { - // era = (Era) stack.pop(); // damn eras - // } + Integer num1 = year; + Integer num2 = dayOfMonth; - // num1 = (Integer) stack.pop(); // year or day - // num2 = (Integer) stack.pop(); // month - // num3 = (Integer) stack.pop(); // day - - // if (eraLast) { - // era = (Era) stack.pop(); - // } + if (DateUtils.isValidDate(num1, numMonth, num2, era)) { + // Do nothing, it is already In the right format + } else if (DateUtils.isValidDate(num2, numMonth, num1, era)) { + // Then the first number is a year. Fix: + year = num2; + dayOfMonth = num1; + } - // Integer dayOfMonth = num1; - // Integer numMonth = num2; - // Integer year = num3; - - // if (DateUtils.isValidDate(num3, num2, num1, era)) { - // // Do nothing, already in the right format (Era Year Month Day) - // } else if (DateUtils.isValidDate(num1, num2, num3, era)) { - // // Use other format: Day Month Year Era - // dayOfMonth = num3; - // year = num1; - // } - - - // stack.push(year); - // stack.push(numMonth); - // stack.push(dayOfMonth); - // stack.push(era); - - // if (dayOfMonth > 31 || dayOfMonth <= 0) { - // throw new StructuredDateFormatException("unexpected day of month '" + Integer.toString(dayOfMonth) + "'"); - // } - // if (year == 0) { - // throw new StructuredDateFormatException("unexpected year '" + Integer.toString(year) + "'"); - // } - // } + stack.push(year); + stack.push(numMonth); + stack.push(dayOfMonth); + stack.push(era); + + if (dayOfMonth > 31 || dayOfMonth <= 0) { + throw new StructuredDateFormatException("unexpected day of month '" + Integer.toString(dayOfMonth) + "'"); + } + if (year == 0) { + throw new StructuredDateFormatException("unexpected year '" + Integer.toString(year) + "'"); + } + } + + @Override + public void exitInvStrDateEraLastDate(InvStrDateEraLastDateContext ctx) { + if (ctx.exception != null) return; + + Era era = (ctx.era() == null) ? null : (Era) stack.pop(); + Integer dayOfMonth = (Integer) stack.pop(); + Integer month = (Integer) stack.pop(); + Integer year = (Integer) stack.pop(); + + stack.push(year); + stack.push(month); + stack.push(dayOfMonth); + stack.push(era); + } @Override public void exitMonth(MonthContext ctx) { diff --git a/services/structureddate/structureddate/src/test/resources/test-dates.yaml b/services/structureddate/structureddate/src/test/resources/test-dates.yaml index 5abe10f86..eb1edd456 100644 --- a/services/structureddate/structureddate/src/test/resources/test-dates.yaml +++ b/services/structureddate/structureddate/src/test/resources/test-dates.yaml @@ -1106,15 +1106,53 @@ "3/4/2000?": # oneDisplayDate - with question mark earliestSingleDate: [2000, 3, 4, CE] - "13 april 1995": # oneDisplayDate: - SingleInterval with Day (cardinal) Month Year Format + "13 april 1995": # Rule 6 oneDisplayDate: - SingleInterval with Day (cardinal) Month Year Format earliestSingleDate: [1995, 4, 13, CE] "13 apr 1995": # oneDisplayDate: - SingleInterval with Day (cardinal) Month Year Format earliestSingleDate: [1995, 4, 13, CE] - "13th APRIL 1995": # oneDisplayDate: - SingleInterval with Day (ordinal) Month Year Format + "13th APRIL 1995": # oneDisplayDate: - SingleInterval with Day (ordinal) Month (all caps) Year Format earliestSingleDate: [1995, 4, 13, CE] + "bc 1995 april 13": # oneDisplayDate: - SingleInterval - With Era Year Month Day format + earliestSingleDate: [1995, 4, 13, BCE] + + "bc 1995, april 13": # oneDisplayDate: - SingleInterval - With Era Year Month Day format + earliestSingleDate: [1995, 4, 13, BCE] + + "13th april 1995": # oneDisplayDate: - SingleInterval with Day (ordinal) Month (all caps) Year Format + earliestSingleDate: [1995, 4, 13, CE] + + "1995, april 13": # oneDisplayDate: - SingleInterval - Year COMMA month day format (no era) + earliestSingleDate: [1995, 4, 13, CE] + + "1995, april 13 AD": # oneDisplayDate: - SingleInterval - Year COMMA month day era format + earliestSingleDate: [1995, 4, 13, CE] + + "1995 april 13": # oneDisplayDate - singleInterval - dayOrYearFirstDate - Year month day, no era + earliestSingleDate: [1995, 4, 13, CE] + + "13 april 1995": # oneDisplayDate - singleInterval - dayOrYearFirstDate - Day month year, no era + earliestSingleDate: [1995, 4, 13, CE] + + "13 april, 1995": # oneDisplayDate - singleInterval - dayFirstDate - Day Month Year + earliestSingleDate: [1995, 4, 13, CE] + + "13 april, 1995 AD": # oneDisplayDate - singleInterval - dayFirstDate - Day Month Year with comma and era + earliestSingleDate: [1995, 4, 13, CE] + + "13 april 1995 AD": # oneDisplayDate - singleInterval - dayFirstDate - Day Month Year with era, no comma + earliestSingleDate: [1995, 4, 13, CE] + + "13th april, 1995": # oneDisplayDate - singleInterval - dayFirstDate - Day (ordinal) Month Year + earliestSingleDate: [1995, 4, 13, CE] + + "13th april, 1995 - 5th may 1999": # oneDisplayDate - hyphenatedRange - dayFirstDate - Day (ordinal) Month Year + earliestSingleDate: [1995, 4, 13, CE] + latestDate: [1999, 5, 5, CE] + + # ------------------------------------------------------------------------------------------------------- # Invalid dates # -------------------------------------------------------------------------------------------------------