• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_TEMPORAL_TEMPORAL_PARSER_H_
6 #define V8_TEMPORAL_TEMPORAL_PARSER_H_
7 
8 #include "src/execution/isolate.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 /**
14  * ParsedISO8601Result contains the parsed result of ISO 8601 grammar
15  * documented in #sec-temporal-iso8601grammar
16  * for TemporalInstantString, TemporalZonedDateTimeString,
17  * TemporalCalendarString, TemporalDateString, TemporalDateTimeString,
18  * TemporalMonthDayString, TemporalRelativeToString, TemporalTimeString,
19  * TemporalTimeZoneString, and TemporalYearMonthString. For all the fields
20  * represented by int32_t, a special value kMinInt31 is used to represent the
21  * field is "undefined" after parsing.
22  */
23 struct ParsedISO8601Result {
24   int32_t date_year;    // DateYear production
25   int32_t date_month;   // DateMonth production
26   int32_t date_day;     // DateDay production
27   int32_t time_hour;    // TimeHour production
28   int32_t time_minute;  // TimeMinute production
29   int32_t time_second;  // TimeSecond production
30   int32_t
31       time_nanosecond;  // TimeFractionalPart production stored in nanosecond
32   int32_t tzuo_sign;    // TimeZoneUTCOffsetSign production
33   int32_t tzuo_hour;    // TimeZoneUTCOffsetHour production
34   int32_t tzuo_minute;  // TimeZoneUTCOffsetMinute production
35   int32_t tzuo_second;  // TimeZoneUTCOffsetSecond production
36   int32_t
37       tzuo_nanosecond;  // TimeZoneUTCOffsetFractionalPart stored in nanosecond
38   bool utc_designator;  // UTCDesignator is presented
39   int32_t tzi_name_start;   // Starting offset of TimeZoneIANAName in the input
40                             // string.
41   int32_t tzi_name_length;  // Length of TimeZoneIANAName production
42   int32_t calendar_name_start;  // Starting offset of CalendarName production in
43                                 // the input string.
44   int32_t calendar_name_length;  // Length of CalendarName production.
45 
ParsedISO8601ResultParsedISO8601Result46   ParsedISO8601Result()
47       : date_year(kMinInt31),
48         date_month(kMinInt31),
49         date_day(kMinInt31),
50         time_hour(kMinInt31),
51         time_minute(kMinInt31),
52         time_second(kMinInt31),
53         time_nanosecond(kMinInt31),
54         tzuo_sign(kMinInt31),
55         tzuo_hour(kMinInt31),
56         tzuo_minute(kMinInt31),
57         tzuo_second(kMinInt31),
58         tzuo_nanosecond(kMinInt31),
59         utc_designator(false),
60         tzi_name_start(0),
61         tzi_name_length(0),
62         calendar_name_start(0),
63         calendar_name_length(0) {}
64 
date_year_is_undefinedParsedISO8601Result65   bool date_year_is_undefined() const { return date_year == kMinInt31; }
date_month_is_undefinedParsedISO8601Result66   bool date_month_is_undefined() const { return date_month == kMinInt31; }
date_day_is_undefinedParsedISO8601Result67   bool date_day_is_undefined() const { return date_day == kMinInt31; }
time_hour_is_undefinedParsedISO8601Result68   bool time_hour_is_undefined() const { return time_hour == kMinInt31; }
time_minute_is_undefinedParsedISO8601Result69   bool time_minute_is_undefined() const { return time_minute == kMinInt31; }
time_second_is_undefinedParsedISO8601Result70   bool time_second_is_undefined() const { return time_second == kMinInt31; }
time_nanosecond_is_undefinedParsedISO8601Result71   bool time_nanosecond_is_undefined() const {
72     return time_nanosecond == kMinInt31;
73   }
tzuo_hour_is_undefinedParsedISO8601Result74   bool tzuo_hour_is_undefined() const { return tzuo_hour == kMinInt31; }
tzuo_minute_is_undefinedParsedISO8601Result75   bool tzuo_minute_is_undefined() const { return tzuo_minute == kMinInt31; }
tzuo_second_is_undefinedParsedISO8601Result76   bool tzuo_second_is_undefined() const { return tzuo_second == kMinInt31; }
tzuo_sign_is_undefinedParsedISO8601Result77   bool tzuo_sign_is_undefined() const { return tzuo_sign == kMinInt31; }
tzuo_nanosecond_is_undefinedParsedISO8601Result78   bool tzuo_nanosecond_is_undefined() const {
79     return tzuo_nanosecond == kMinInt31;
80   }
81 };
82 
83 /**
84  * ParsedISO8601Duration contains the parsed result of ISO 8601 grammar
85  * documented in #prod-TemporalDurationString
86  * for TemporalDurationString.
87  */
88 struct ParsedISO8601Duration {
89   int64_t sign;              // Sign production
90   int64_t years;             // DurationYears production
91   int64_t months;            // DurationMonths production
92   int64_t weeks;             // DurationWeeks production
93   int64_t days;              // DurationDays production
94   int64_t whole_hours;       // DurationWholeHours production
95   int64_t hours_fraction;    // DurationHoursFraction, in unit of 1e-9 hours
96   int64_t whole_minutes;     // DurationWholeMinutes production
97   int64_t minutes_fraction;  // DurationMinuteFraction, in unit of 1e-9 minutes
98   int64_t whole_seconds;     // DurationWholeSeconds production
99   int64_t seconds_fraction;  // DurationSecondFraction, in unit of nanosecond (
100                              // 1e-9 seconds).
101 
ParsedISO8601DurationParsedISO8601Duration102   ParsedISO8601Duration()
103       : sign(1),
104         years(0),
105         months(0),
106         weeks(0),
107         days(0),
108         whole_hours(0),
109         hours_fraction(0),
110         whole_minutes(0),
111         minutes_fraction(0),
112         whole_seconds(0),
113         seconds_fraction(0) {}
114 };
115 
116 /**
117  * TemporalParser is low level parsing functions to support the implementation
118  * of various ParseTemporal*String Abstract Operations listed after
119  * #sec-temporal-parsetemporalinstantstring.
120  * All the methods take an Isolate, a Handle<String> as input, and also a
121  * pointer to a bool to answer the "satisfy the syntax of a Temporal*String"
122  * question and return the parsed result.
123  */
124 class V8_EXPORT_PRIVATE TemporalParser {
125  public:
126 #define DEFINE_PARSE_METHOD(R, NAME)                                  \
127   V8_WARN_UNUSED_RESULT static Maybe<R> Parse##NAME(Isolate* isolate, \
128                                                     Handle<String> iso_string)
129   DEFINE_PARSE_METHOD(ParsedISO8601Result, TemporalDateString);
130   DEFINE_PARSE_METHOD(ParsedISO8601Result, TemporalDateTimeString);
131   DEFINE_PARSE_METHOD(ParsedISO8601Result, TemporalTimeString);
132   DEFINE_PARSE_METHOD(ParsedISO8601Result, TemporalYearMonthString);
133   DEFINE_PARSE_METHOD(ParsedISO8601Result, TemporalMonthDayString);
134   DEFINE_PARSE_METHOD(ParsedISO8601Result, TemporalInstantString);
135   DEFINE_PARSE_METHOD(ParsedISO8601Result, TemporalZonedDateTimeString);
136   DEFINE_PARSE_METHOD(ParsedISO8601Result, TemporalTimeZoneString);
137   DEFINE_PARSE_METHOD(ParsedISO8601Result, TemporalRelativeToString);
138   DEFINE_PARSE_METHOD(ParsedISO8601Result, TemporalCalendarString);
139   DEFINE_PARSE_METHOD(ParsedISO8601Duration, TemporalDurationString);
140   DEFINE_PARSE_METHOD(ParsedISO8601Result, TimeZoneNumericUTCOffset);
141 };
142 #undef DEFINE_PARSE_METHOD
143 
144 }  // namespace internal
145 }  // namespace v8
146 
147 #endif  // V8_TEMPORAL_TEMPORAL_PARSER_H_
148