1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:38:11 +00:00

LibJS: Simplify ParseTemporalTimeZoneString

This is an editorial change in the Temporal spec.
See: eec8efab
This commit is contained in:
Idan Horowitz 2022-10-20 00:19:09 +03:00 committed by Linus Groh
parent 0c61552b81
commit d38aeddd77
3 changed files with 21 additions and 52 deletions

View file

@ -1666,41 +1666,30 @@ ThrowCompletionOr<TemporalTime> parse_temporal_time_string(VM& vm, String const&
return TemporalTime { .hour = result.hour, .minute = result.minute, .second = result.second, .millisecond = result.millisecond, .microsecond = result.microsecond, .nanosecond = result.nanosecond, .calendar = move(result.calendar) };
}
// 13.38 ParseTemporalTimeZoneString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaltimezonestring
ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(VM& vm, String const& iso_string)
// 13.38 ParseTemporalTimeZoneString ( timeZoneString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaltimezonestring
ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(VM& vm, String const& time_zone_string)
{
// 1. Let parseResult be ParseText(StringToCodePoints(isoString), TemporalTimeZoneString).
auto parse_result = parse_iso8601(Production::TemporalTimeZoneString, iso_string);
// 1. Let parseResult be ParseText(StringToCodePoints(timeZoneString), TimeZoneIdentifier).
auto parse_result = parse_iso8601(Production::TimeZoneIdentifier, time_zone_string);
// 2. If parseResult is a List of errors, throw a RangeError exception.
if (!parse_result.has_value())
return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidTimeZoneString, iso_string);
// 3. Let each of z, offsetString, and name be the source text matched by the respective UTCDesignator, TimeZoneNumericUTCOffset, and TimeZoneIdentifier Parse Nodes contained within parseResult, or an empty sequence of code points if not present.
auto z = parse_result->utc_designator;
auto offset_string = parse_result->time_zone_numeric_utc_offset;
auto name = parse_result->time_zone_identifier;
// 4. If name is empty, then
// a. Set name to undefined.
// 5. Else,
// a. Set name to CodePointsToString(name).
// NOTE: No-op.
// 6. If z is not empty, then
if (z.has_value()) {
// a. Return the Record { [[Z]]: true, [[OffsetString]]: undefined, [[Name]]: name }.
return TemporalTimeZone { .z = true, .offset_string = {}, .name = Optional<String>(move(name)) };
// 2. If parseResult is a Parse Node, then
if (parse_result.has_value()) {
// a. Return the Record { [[Z]]: false, [[OffsetString]]: undefined, [[Name]]: timeZoneString }.
return TemporalTimeZone { .z = false, .offset_string = {}, .name = time_zone_string };
}
// 7. If offsetString is empty, then
// a. Set offsetString to undefined.
// 8. Else,
// a. Set offsetString to CodePointsToString(offsetString).
// NOTE: No-op.
// 3. Let result be ? ParseISODateTime(timeZoneString).
auto result = TRY(parse_iso_date_time(vm, time_zone_string));
// 9. Return the Record { [[Z]]: false, [[OffsetString]]: offsetString, [[Name]]: name }.
return TemporalTimeZone { .z = false, .offset_string = Optional<String>(move(offset_string)), .name = Optional<String>(move(name)) };
// 4. Let timeZoneResult be result.[[TimeZone]].
auto const& time_zone_result = result.time_zone;
// 5. If timeZoneResult.[[Z]] is false, timeZoneResult.[[OffsetString]] is undefined, and timeZoneResult.[[Name]] is undefined, throw a RangeError exception.
if (!time_zone_result.z && !time_zone_result.offset_string.has_value() && !time_zone_result.name.has_value())
return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidTimeZoneString, time_zone_string);
// 6. Return timeZoneResult.
return time_zone_result;
}
// 13.39 ParseTemporalYearMonthString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalyearmonthstring

View file

@ -1400,25 +1400,6 @@ bool ISO8601Parser::parse_temporal_time_string()
|| parse_calendar_time();
}
// https://tc39.es/proposal-temporal/#prod-TemporalTimeZoneString
bool ISO8601Parser::parse_temporal_time_zone_string()
{
// TemporalTimeZoneString :
// TimeZoneIdentifier
// Date TimeSpecSeparator[opt] TimeZone Calendar[opt]
StateTransaction transaction { *this };
if (!parse_time_zone_identifier()) {
if (!parse_date())
return false;
(void)parse_time_spec_separator();
if (!parse_time_zone())
return false;
(void)parse_calendar();
}
transaction.commit();
return true;
}
// https://tc39.es/proposal-temporal/#prod-TemporalYearMonthString
bool ISO8601Parser::parse_temporal_year_month_string()
{
@ -1455,9 +1436,9 @@ bool ISO8601Parser::parse_temporal_zoned_date_time_string()
__JS_ENUMERATE(TemporalDurationString, parse_temporal_duration_string) \
__JS_ENUMERATE(TemporalMonthDayString, parse_temporal_month_day_string) \
__JS_ENUMERATE(TemporalTimeString, parse_temporal_time_string) \
__JS_ENUMERATE(TemporalTimeZoneString, parse_temporal_time_zone_string) \
__JS_ENUMERATE(TemporalYearMonthString, parse_temporal_year_month_string) \
__JS_ENUMERATE(TemporalZonedDateTimeString, parse_temporal_zoned_date_time_string) \
__JS_ENUMERATE(TimeZoneIdentifier, parse_time_zone_identifier) \
__JS_ENUMERATE(TimeZoneNumericUTCOffset, parse_time_zone_numeric_utc_offset) \
__JS_ENUMERATE(CalendarName, parse_calendar_name) \
__JS_ENUMERATE(DateMonth, parse_date_month)

View file

@ -50,9 +50,9 @@ enum class Production {
TemporalDurationString,
TemporalMonthDayString,
TemporalTimeString,
TemporalTimeZoneString,
TemporalYearMonthString,
TemporalZonedDateTimeString,
TimeZoneIdentifier,
TimeZoneNumericUTCOffset,
CalendarName,
DateMonth,
@ -165,7 +165,6 @@ public:
[[nodiscard]] bool parse_temporal_duration_string();
[[nodiscard]] bool parse_temporal_month_day_string();
[[nodiscard]] bool parse_temporal_time_string();
[[nodiscard]] bool parse_temporal_time_zone_string();
[[nodiscard]] bool parse_temporal_year_month_string();
[[nodiscard]] bool parse_temporal_zoned_date_time_string();