1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:17:45 +00:00

LibJS: Remove unused nonterminals from the ISO8601 parser

This is an editorial change in the Temporal spec.

See: fe9ef00
This commit is contained in:
Linus Groh 2022-08-25 21:41:23 +01:00
parent 42452a81a2
commit 2cf5f5c278
3 changed files with 21 additions and 292 deletions

View file

@ -1171,37 +1171,17 @@ ThrowCompletionOr<ISODateTime> parse_iso_date_time(VM& vm, ParseResult const& pa
// 3. Assert: parseResult is a Parse Node.
// NOTE: All of this is done by receiving an already parsed ISO string (ParseResult).
// 4. Let each of year, month, day, fSeconds, and calendar be the source text matched by the respective DateYear, DateMonth, DateDay, TimeFraction, and CalendarName Parse Node contained within parseResult, or an empty sequence of code points if not present.
// 4. Let each of year, month, day, hour, minute, second, fSeconds, and calendar be the source text matched by the respective DateYear, DateMonth, DateDay, TimeHour, TimeMinute, TimeSecond, TimeFraction, and CalendarName Parse Node contained within parseResult, or an empty sequence of code points if not present.
auto year = parse_result.date_year;
auto month = parse_result.date_month;
auto day = parse_result.date_day;
auto hour = parse_result.time_hour;
auto minute = parse_result.time_minute;
auto second = parse_result.time_second;
auto f_seconds = parse_result.time_fraction;
auto calendar = parse_result.calendar_name;
// 5. Let hour be the source text matched by the TimeHour, TimeHourNotValidMonth, TimeHourNotThirtyOneDayMonth, or TimeHourTwoOnly Parse Node contained within parseResult, or an empty sequence of code points if none of those are present.
auto hour = parse_result.time_hour;
if (!hour.has_value())
hour = parse_result.time_hour_not_valid_month;
if (!hour.has_value())
hour = parse_result.time_hour_not_thirty_one_day_month;
if (!hour.has_value())
hour = parse_result.time_hour_two_only;
// 6. Let minute be the source text matched by the TimeMinute, TimeMinuteNotValidDay, TimeMinuteThirtyOnly, or TimeMinuteThirtyOneOnly Parse Node contained within parseResult, or an empty sequence of code points if none of those are present.
auto minute = parse_result.time_minute;
if (!minute.has_value())
minute = parse_result.time_minute_not_valid_day;
if (!minute.has_value())
minute = parse_result.time_minute_thirty_only;
if (!minute.has_value())
minute = parse_result.time_minute_thirty_one_only;
// 7. Let second be the source text matched by the TimeSecond or TimeSecondNotValidMonth Parse Node contained within parseResult, or an empty sequence of code points if neither of those are present.
auto second = parse_result.time_second;
if (!second.has_value())
second = parse_result.time_second_not_valid_month;
// 8. If the first code point of year is U+2212 (MINUS SIGN), replace the first code point with U+002D (HYPHEN-MINUS).
// 5. If the first code point of year is U+2212 (MINUS SIGN), replace the first code point with U+002D (HYPHEN-MINUS).
Optional<String> normalized_year;
if (year.has_value()) {
normalized_year = year->starts_with("\xE2\x88\x92"sv)
@ -1209,31 +1189,31 @@ ThrowCompletionOr<ISODateTime> parse_iso_date_time(VM& vm, ParseResult const& pa
: String { *year };
}
// 9. Let yearMV be ! ToIntegerOrInfinity(CodePointsToString(year)).
// 6. Let yearMV be ! ToIntegerOrInfinity(CodePointsToString(year)).
auto year_mv = *normalized_year.value_or("0"sv).to_int<i32>();
// 10. If month is empty, then
// 7. If month is empty, then
// a. Let monthMV be 1.
// 11. Else,
// 8. Else,
// a. Let monthMV be ! ToIntegerOrInfinity(CodePointsToString(month)).
auto month_mv = *month.value_or("1"sv).to_uint<u8>();
// 12. If day is empty, then
// 9. If day is empty, then
// a. Let dayMV be 1.
// 13. Else,
// 10. Else,
// a. Let dayMV be ! ToIntegerOrInfinity(CodePointsToString(day)).
auto day_mv = *day.value_or("1"sv).to_uint<u8>();
// 14. Let hourMV be ! ToIntegerOrInfinity(CodePointsToString(hour)).
// 11. Let hourMV be ! ToIntegerOrInfinity(CodePointsToString(hour)).
auto hour_mv = *hour.value_or("0"sv).to_uint<u8>();
// 15. Let minuteMV be ! ToIntegerOrInfinity(CodePointsToString(minute)).
// 12. Let minuteMV be ! ToIntegerOrInfinity(CodePointsToString(minute)).
auto minute_mv = *minute.value_or("0"sv).to_uint<u8>();
// 16. Let secondMV be ! ToIntegerOrInfinity(CodePointsToString(second)).
// 13. Let secondMV be ! ToIntegerOrInfinity(CodePointsToString(second)).
auto second_mv = *second.value_or("0"sv).to_uint<u8>();
// 17. If secondMV is 60, then
// 14. If secondMV is 60, then
if (second_mv == 60) {
// a. Set secondMV to 59.
second_mv = 59;
@ -1243,7 +1223,7 @@ ThrowCompletionOr<ISODateTime> parse_iso_date_time(VM& vm, ParseResult const& pa
u16 microsecond_mv;
u16 nanosecond_mv;
// 18. If fSeconds is not empty, then
// 15. If fSeconds is not empty, then
if (f_seconds.has_value()) {
// a. Let fSecondsDigits be the substring of CodePointsToString(fSeconds) from 1.
auto f_seconds_digits = f_seconds->substring_view(1);
@ -1269,7 +1249,7 @@ ThrowCompletionOr<ISODateTime> parse_iso_date_time(VM& vm, ParseResult const& pa
// h. Let nanosecondMV be ! ToIntegerOrInfinity(nanosecond).
nanosecond_mv = *nanosecond.to_uint<u16>();
}
// 19. Else,
// 16. Else,
else {
// a. Let millisecondMV be 0.
millisecond_mv = 0;
@ -1281,29 +1261,29 @@ ThrowCompletionOr<ISODateTime> parse_iso_date_time(VM& vm, ParseResult const& pa
nanosecond_mv = 0;
}
// 20. If IsValidISODate(yearMV, monthMV, dayMV) is false, throw a RangeError exception.
// 17. If IsValidISODate(yearMV, monthMV, dayMV) is false, throw a RangeError exception.
if (!is_valid_iso_date(year_mv, month_mv, day_mv))
return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidISODate);
// 21. If IsValidTime(hourMV, minuteMV, secondMV, millisecondMV, microsecondMV, nanosecondMV) is false, throw a RangeError exception.
// 18. If IsValidTime(hourMV, minuteMV, secondMV, millisecondMV, microsecondMV, nanosecondMV) is false, throw a RangeError exception.
if (!is_valid_time(hour_mv, minute_mv, second_mv, millisecond_mv, microsecond_mv, nanosecond_mv))
return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidTime);
Optional<String> calendar_val;
// 22. If calendar is empty, then
// 19. If calendar is empty, then
if (!calendar.has_value()) {
// a. Let calendarVal be undefined.
calendar_val = {};
}
// 23. Else,
// 20. Else,
else {
// a. Let calendarVal be CodePointsToString(calendar).
// NOTE: This turns the StringView into a String.
calendar_val = *calendar;
}
// 24. Return the Record { [[Year]]: yearMV, [[Month]]: monthMV, [[Day]]: dayMV, [[Hour]]: hourMV, [[Minute]]: minuteMV, [[Second]]: secondMV, [[Millisecond]]: millisecondMV, [[Microsecond]]: microsecondMV, [[Nanosecond]]: nanosecondMV, [[Calendar]]: calendarVal, }.
// 21. Return the Record { [[Year]]: yearMV, [[Month]]: monthMV, [[Day]]: dayMV, [[Hour]]: hourMV, [[Minute]]: minuteMV, [[Second]]: secondMV, [[Millisecond]]: millisecondMV, [[Microsecond]]: microsecondMV, [[Nanosecond]]: nanosecondMV, [[Calendar]]: calendarVal, }.
return ISODateTime { .year = year_mv, .month = month_mv, .day = day_mv, .hour = hour_mv, .minute = minute_mv, .second = second_mv, .millisecond = millisecond_mv, .microsecond = microsecond_mv, .nanosecond = nanosecond_mv, .calendar = move(calendar_val) };
}

View file

@ -499,169 +499,6 @@ bool ISO8601Parser::parse_time_second()
return true;
}
// https://tc39.es/proposal-temporal/#prod-TimeHourNotValidMonth
bool ISO8601Parser::parse_time_hour_not_valid_month()
{
// TimeHourNotValidMonth : one of
// 00 13 14 15 16 17 18 19 20 21 23
StateTransaction transaction { *this };
auto success = m_state.lexer.consume_specific("00"sv)
|| m_state.lexer.consume_specific("13"sv)
|| m_state.lexer.consume_specific("14"sv)
|| m_state.lexer.consume_specific("15"sv)
|| m_state.lexer.consume_specific("16"sv)
|| m_state.lexer.consume_specific("17"sv)
|| m_state.lexer.consume_specific("18"sv)
|| m_state.lexer.consume_specific("19"sv)
|| m_state.lexer.consume_specific("20"sv)
|| m_state.lexer.consume_specific("21"sv)
|| m_state.lexer.consume_specific("22"sv)
|| m_state.lexer.consume_specific("23"sv);
if (!success)
return false;
m_state.parse_result.time_hour_not_valid_month = transaction.parsed_string_view();
transaction.commit();
return true;
}
// https://tc39.es/proposal-temporal/#prod-TimeHourNotThirtyOneDayMonth
bool ISO8601Parser::parse_time_hour_not_thirty_one_day_month()
{
// TimeHourNotThirtyOneDayMonth : one of
// 02 04 06 09 11
StateTransaction transaction { *this };
auto success = m_state.lexer.consume_specific("02"sv)
|| m_state.lexer.consume_specific("04"sv)
|| m_state.lexer.consume_specific("06"sv)
|| m_state.lexer.consume_specific("09"sv)
|| m_state.lexer.consume_specific("11"sv);
if (!success)
return false;
m_state.parse_result.time_hour_not_thirty_one_day_month = transaction.parsed_string_view();
transaction.commit();
return true;
}
// https://tc39.es/proposal-temporal/#prod-TimeHourTwoOnly
bool ISO8601Parser::parse_time_hour_two_only()
{
// TimeHourTwoOnly :
// 02
StateTransaction transaction { *this };
if (!m_state.lexer.consume_specific("02"sv))
return false;
m_state.parse_result.time_hour_two_only = transaction.parsed_string_view();
transaction.commit();
return true;
}
// https://tc39.es/proposal-temporal/#prod-TimeMinuteNotValidDay
bool ISO8601Parser::parse_time_minute_not_valid_day()
{
// TimeMinuteNotValidDay :
// 00
// 32
// 33
// 34
// 35
// 36
// 37
// 38
// 39
// 4 DecimalDigit
// 5 DecimalDigit
// 60
StateTransaction transaction { *this };
if (m_state.lexer.consume_specific('4') || m_state.lexer.consume_specific('5')) {
if (!parse_decimal_digit())
return false;
} else {
auto success = m_state.lexer.consume_specific("00"sv)
|| m_state.lexer.consume_specific("32"sv)
|| m_state.lexer.consume_specific("33"sv)
|| m_state.lexer.consume_specific("34"sv)
|| m_state.lexer.consume_specific("35"sv)
|| m_state.lexer.consume_specific("36"sv)
|| m_state.lexer.consume_specific("37"sv)
|| m_state.lexer.consume_specific("38"sv)
|| m_state.lexer.consume_specific("39"sv)
|| m_state.lexer.consume_specific("60"sv);
if (!success)
return false;
}
m_state.parse_result.time_minute_not_valid_day = transaction.parsed_string_view();
transaction.commit();
return true;
}
// https://tc39.es/proposal-temporal/#prod-TimeMinuteThirtyOnly
bool ISO8601Parser::parse_time_minute_thirty_only()
{
// TimeMinuteThirtyOnly :
// 30
StateTransaction transaction { *this };
if (!m_state.lexer.consume_specific("30"sv))
return false;
m_state.parse_result.time_minute_thirty_only = transaction.parsed_string_view();
transaction.commit();
return true;
}
// https://tc39.es/proposal-temporal/#prod-TimeMinuteThirtyOneOnly
bool ISO8601Parser::parse_time_minute_thirty_one_only()
{
// TimeMinuteThirtyOneOnly :
// 31
StateTransaction transaction { *this };
if (!m_state.lexer.consume_specific("31"sv))
return false;
m_state.parse_result.time_minute_thirty_one_only = transaction.parsed_string_view();
transaction.commit();
return true;
}
// https://tc39.es/proposal-temporal/#prod-TimeSecondNotValidMonth
bool ISO8601Parser::parse_time_second_not_valid_month()
{
// TimeSecondNotValidMonth :
// 00
// 13
// 14
// 15
// 16
// 17
// 18
// 19
// 2 DecimalDigit
// 3 DecimalDigit
// 4 DecimalDigit
// 5 DecimalDigit
// 60
StateTransaction transaction { *this };
if (m_state.lexer.consume_specific('2')
|| m_state.lexer.consume_specific('3')
|| m_state.lexer.consume_specific('4')
|| m_state.lexer.consume_specific('5')) {
if (!parse_decimal_digit())
return false;
} else {
auto success = m_state.lexer.consume_specific("00"sv)
|| m_state.lexer.consume_specific("13"sv)
|| m_state.lexer.consume_specific("14"sv)
|| m_state.lexer.consume_specific("15"sv)
|| m_state.lexer.consume_specific("16"sv)
|| m_state.lexer.consume_specific("17"sv)
|| m_state.lexer.consume_specific("18"sv)
|| m_state.lexer.consume_specific("19"sv)
|| m_state.lexer.consume_specific("60"sv);
if (!success)
return false;
}
m_state.parse_result.time_second_not_valid_month = transaction.parsed_string_view();
transaction.commit();
return true;
}
// https://tc39.es/proposal-temporal/#prod-FractionalPart
bool ISO8601Parser::parse_fractional_part()
{
@ -819,38 +656,6 @@ bool ISO8601Parser::parse_time_zone_utc_offset()
|| parse_utc_designator();
}
// https://tc39.es/proposal-temporal/#prod-TimeZoneNumericUTCOffsetNotAmbiguousWithDayOfMonth
bool ISO8601Parser::parse_time_zone_numeric_utc_offset_not_ambiguous_with_day_of_month()
{
// TimeZoneNumericUTCOffsetNotAmbiguousWithDayOfMonth :
// TimeZoneNumericUTCOffset but not - TimeZoneUTCOffsetHour
StateTransaction transaction { *this };
if (!parse_time_zone_numeric_utc_offset())
return false;
auto parsed = *m_state.parse_result.time_zone_numeric_utc_offset;
if (parsed.length() == 3 && parsed[0] == '-')
return false;
transaction.commit();
return true;
}
// https://tc39.es/proposal-temporal/#prod-TimeZoneNumericUTCOffsetNotAmbiguousWithMonth
bool ISO8601Parser::parse_time_zone_numeric_utc_offset_not_ambiguous_with_month()
{
// TimeZoneNumericUTCOffsetNotAmbiguousWithMonth :
// TimeZoneNumericUTCOffsetNotAmbiguousWithDayOfMonth
// - TimeHourNotValidMonth
StateTransaction transaction { *this };
if (!parse_time_zone_numeric_utc_offset_not_ambiguous_with_day_of_month()) {
if (!m_state.lexer.consume_specific('-'))
return false;
if (!parse_time_hour_not_valid_month())
return false;
}
transaction.commit();
return true;
}
// https://tc39.es/proposal-temporal/#prod-TimeZoneUTCOffsetName
bool ISO8601Parser::parse_time_zone_utc_offset_name()
{
@ -1145,45 +950,6 @@ bool ISO8601Parser::parse_time_spec()
return true;
}
// https://tc39.es/proposal-temporal/#prod-TimeHourMinuteBasicFormatNotAmbiguousWithMonthDay
bool ISO8601Parser::parse_time_hour_minute_basic_format_not_ambiguous_with_month_day()
{
// TimeHourMinuteBasicFormatNotAmbiguousWithMonthDay :
// TimeHourNotValidMonth TimeMinute
// TimeHour TimeMinuteNotValidDay
// TimeHourNotThirtyOneDayMonth TimeMinuteThirtyOneOnly
// TimeHourTwoOnly TimeMinuteThirtyOnly
{
StateTransaction transaction { *this };
if (parse_time_hour_not_valid_month() && parse_time_minute()) {
transaction.commit();
return true;
}
}
{
StateTransaction transaction { *this };
if (parse_time_hour() && parse_time_minute_not_valid_day()) {
transaction.commit();
return true;
}
}
{
StateTransaction transaction { *this };
if (parse_time_hour_not_thirty_one_day_month() && parse_time_minute_thirty_one_only()) {
transaction.commit();
return true;
}
}
{
StateTransaction transaction { *this };
if (parse_time_hour_two_only() && parse_time_minute_thirty_only()) {
transaction.commit();
return true;
}
}
return false;
}
// https://tc39.es/proposal-temporal/#prod-TimeSpecWithOptionalTimeZoneNotAmbiguous
bool ISO8601Parser::parse_time_spec_with_optional_time_zone_not_ambiguous()
{

View file

@ -22,13 +22,6 @@ struct ParseResult {
Optional<StringView> time_minute;
Optional<StringView> time_second;
Optional<StringView> time_fraction;
Optional<StringView> time_hour_not_valid_month;
Optional<StringView> time_hour_not_thirty_one_day_month;
Optional<StringView> time_hour_two_only;
Optional<StringView> time_minute_not_valid_day;
Optional<StringView> time_minute_thirty_only;
Optional<StringView> time_minute_thirty_one_only;
Optional<StringView> time_second_not_valid_month;
Optional<StringView> calendar_name;
Optional<StringView> utc_designator;
Optional<StringView> time_zone_numeric_utc_offset;
@ -113,13 +106,6 @@ public:
[[nodiscard]] bool parse_time_hour();
[[nodiscard]] bool parse_time_minute();
[[nodiscard]] bool parse_time_second();
[[nodiscard]] bool parse_time_hour_not_valid_month();
[[nodiscard]] bool parse_time_hour_not_thirty_one_day_month();
[[nodiscard]] bool parse_time_hour_two_only();
[[nodiscard]] bool parse_time_minute_not_valid_day();
[[nodiscard]] bool parse_time_minute_thirty_only();
[[nodiscard]] bool parse_time_minute_thirty_one_only();
[[nodiscard]] bool parse_time_second_not_valid_month();
[[nodiscard]] bool parse_fractional_part();
[[nodiscard]] bool parse_fraction();
[[nodiscard]] bool parse_time_fraction();
@ -131,8 +117,6 @@ public:
[[nodiscard]] bool parse_time_zone_utc_offset_fraction();
[[nodiscard]] bool parse_time_zone_numeric_utc_offset();
[[nodiscard]] bool parse_time_zone_utc_offset();
[[nodiscard]] bool parse_time_zone_numeric_utc_offset_not_ambiguous_with_day_of_month();
[[nodiscard]] bool parse_time_zone_numeric_utc_offset_not_ambiguous_with_month();
[[nodiscard]] bool parse_time_zone_utc_offset_name();
[[nodiscard]] bool parse_tz_leading_char();
[[nodiscard]] bool parse_tz_char();
@ -148,7 +132,6 @@ public:
[[nodiscard]] bool parse_calendar_name();
[[nodiscard]] bool parse_calendar();
[[nodiscard]] bool parse_time_spec();
[[nodiscard]] bool parse_time_hour_minute_basic_format_not_ambiguous_with_month_day();
[[nodiscard]] bool parse_time_spec_with_optional_time_zone_not_ambiguous();
[[nodiscard]] bool parse_time_spec_separator();
[[nodiscard]] bool parse_date_time();