| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_DATEPARSER_INL_H_ | 5 #ifndef V8_DATEPARSER_INL_H_ |
| 6 #define V8_DATEPARSER_INL_H_ | 6 #define V8_DATEPARSER_INL_H_ |
| 7 | 7 |
| 8 #include "src/char-predicates-inl.h" | 8 #include "src/char-predicates-inl.h" |
| 9 #include "src/dateparser.h" | 9 #include "src/dateparser.h" |
| 10 | 10 |
| 11 namespace v8 { | 11 namespace v8 { |
| 12 namespace internal { | 12 namespace internal { |
| 13 | 13 |
| 14 template <typename Char> | 14 template <typename Char> |
| 15 bool DateParser::Parse(Vector<Char> str, | 15 bool DateParser::Parse(Vector<Char> str, |
| 16 FixedArray* out, | 16 FixedArray* out, |
| 17 UnicodeCache* unicode_cache) { | 17 UnicodeCache* unicode_cache) { |
| 18 DCHECK(out->length() >= OUTPUT_SIZE); | 18 DCHECK(out->length() >= OUTPUT_SIZE); |
| 19 InputReader<Char> in(unicode_cache, str); | 19 InputReader<Char> in(unicode_cache, str); |
| 20 DateStringTokenizer<Char> scanner(&in); | 20 DateStringTokenizer<Char> scanner(&in); |
| 21 TimeZoneComposer tz; | 21 TimeZoneComposer tz; |
| 22 TimeComposer time; | 22 TimeComposer time; |
| 23 DayComposer day; | 23 DayComposer day; |
| 24 | 24 |
| 25 // Specification: | 25 // Specification: |
| 26 // Accept ES6 ISO 8601 date-time-strings or legacy dates compatible | 26 // Accept ES5 ISO 8601 date-time-strings or legacy dates compatible |
| 27 // with Safari. | 27 // with Safari. |
| 28 // ES6 ISO 8601 dates: | 28 // ES5 ISO 8601 dates: |
| 29 // [('-'|'+')yy]yyyy[-MM[-DD]][THH:mm[:ss[.sss]][Z|(+|-)hh:mm]] | 29 // [('-'|'+')yy]yyyy[-MM[-DD]][THH:mm[:ss[.sss]][Z|(+|-)hh:mm]] |
| 30 // where yyyy is in the range 0000..9999 and | 30 // where yyyy is in the range 0000..9999 and |
| 31 // +/-yyyyyy is in the range -999999..+999999 - | 31 // +/-yyyyyy is in the range -999999..+999999 - |
| 32 // but -000000 is invalid (year zero must be positive), | 32 // but -000000 is invalid (year zero must be positive), |
| 33 // MM is in the range 01..12, | 33 // MM is in the range 01..12, |
| 34 // DD is in the range 01..31, | 34 // DD is in the range 01..31, |
| 35 // MM and DD defaults to 01 if missing,, | 35 // MM and DD defaults to 01 if missing,, |
| 36 // HH is generally in the range 00..23, but can be 24 if mm, ss | 36 // HH is generally in the range 00..23, but can be 24 if mm, ss |
| 37 // and sss are zero (or missing), representing midnight at the | 37 // and sss are zero (or missing), representing midnight at the |
| 38 // end of a day, | 38 // end of a day, |
| 39 // mm and ss are in the range 00..59, | 39 // mm and ss are in the range 00..59, |
| 40 // sss is in the range 000..999, | 40 // sss is in the range 000..999, |
| 41 // hh is in the range 00..23, | 41 // hh is in the range 00..23, |
| 42 // mm, ss, and sss default to 00 if missing, and | 42 // mm, ss, and sss default to 00 if missing, and |
| 43 // timezone defaults to local time if missing. | 43 // timezone defaults to Z if missing |
| 44 // (following Safari, ISO actually demands local time). |
| 44 // Extensions: | 45 // Extensions: |
| 45 // We also allow sss to have more or less than three digits (but at | 46 // We also allow sss to have more or less than three digits (but at |
| 46 // least one). | 47 // least one). |
| 47 // We allow hh:mm to be specified as hhmm. | 48 // We allow hh:mm to be specified as hhmm. |
| 48 // Legacy dates: | 49 // Legacy dates: |
| 49 // Any unrecognized word before the first number is ignored. | 50 // Any unrecognized word before the first number is ignored. |
| 50 // Parenthesized text is ignored. | 51 // Parenthesized text is ignored. |
| 51 // An unsigned number followed by ':' is a time value, and is | 52 // An unsigned number followed by ':' is a time value, and is |
| 52 // added to the TimeComposer. A number followed by '::' adds a second | 53 // added to the TimeComposer. A number followed by '::' adds a second |
| 53 // zero as well. A number followed by '.' is also a time and must be | 54 // zero as well. A number followed by '.' is also a time and must be |
| 54 // followed by milliseconds. | 55 // followed by milliseconds. |
| 55 // Any other number is a date component and is added to DayComposer. | 56 // Any other number is a date component and is added to DayComposer. |
| 56 // A month name (or really: any word having the same first three letters | 57 // A month name (or really: any word having the same first three letters |
| 57 // as a month name) is recorded as a named month in the Day composer. | 58 // as a month name) is recorded as a named month in the Day composer. |
| 58 // A word recognizable as a time-zone is recorded as such, as is | 59 // A word recognizable as a time-zone is recorded as such, as is |
| 59 // '(+|-)(hhmm|hh:)'. | 60 // '(+|-)(hhmm|hh:)'. |
| 60 // Legacy dates don't allow extra signs ('+' or '-') or umatched ')' | 61 // Legacy dates don't allow extra signs ('+' or '-') or umatched ')' |
| 61 // after a number has been read (before the first number, any garbage | 62 // after a number has been read (before the first number, any garbage |
| 62 // is allowed). | 63 // is allowed). |
| 63 // Intersection of the two: | 64 // Intersection of the two: |
| 64 // A string that matches both formats (e.g. 1970-01-01) will be | 65 // A string that matches both formats (e.g. 1970-01-01) will be |
| 65 // parsed as an ES6 date-time string. | 66 // parsed as an ES5 date-time string - which means it will default |
| 66 // After a valid "T" has been read while scanning an ES6 datetime string, | 67 // to UTC time-zone. That's unavoidable if following the ES5 |
| 68 // specification. |
| 69 // After a valid "T" has been read while scanning an ES5 datetime string, |
| 67 // the input can no longer be a valid legacy date, since the "T" is a | 70 // the input can no longer be a valid legacy date, since the "T" is a |
| 68 // garbage string after a number has been read. | 71 // garbage string after a number has been read. |
| 69 | 72 |
| 70 // First try getting as far as possible with as ES6 Date Time String. | 73 // First try getting as far as possible with as ES5 Date Time String. |
| 71 DateToken next_unhandled_token = ParseES6DateTime(&scanner, &day, &time, &tz); | 74 DateToken next_unhandled_token = ParseES5DateTime(&scanner, &day, &time, &tz); |
| 72 if (next_unhandled_token.IsInvalid()) return false; | 75 if (next_unhandled_token.IsInvalid()) return false; |
| 73 bool has_read_number = !day.IsEmpty(); | 76 bool has_read_number = !day.IsEmpty(); |
| 74 // If there's anything left, continue with the legacy parser. | 77 // If there's anything left, continue with the legacy parser. |
| 75 for (DateToken token = next_unhandled_token; | 78 for (DateToken token = next_unhandled_token; |
| 76 !token.IsEndOfInput(); | 79 !token.IsEndOfInput(); |
| 77 token = scanner.Next()) { | 80 token = scanner.Next()) { |
| 78 if (token.IsNumber()) { | 81 if (token.IsNumber()) { |
| 79 has_read_number = true; | 82 has_read_number = true; |
| 80 int n = token.number(); | 83 int n = token.number(); |
| 81 if (scanner.SkipSymbol(':')) { | 84 if (scanner.SkipSymbol(':')) { |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 } | 189 } |
| 187 if (in_->SkipParentheses()) { | 190 if (in_->SkipParentheses()) { |
| 188 return DateToken::Unknown(); | 191 return DateToken::Unknown(); |
| 189 } | 192 } |
| 190 in_->Next(); | 193 in_->Next(); |
| 191 return DateToken::Unknown(); | 194 return DateToken::Unknown(); |
| 192 } | 195 } |
| 193 | 196 |
| 194 | 197 |
| 195 template <typename Char> | 198 template <typename Char> |
| 196 DateParser::DateToken DateParser::ParseES6DateTime( | 199 DateParser::DateToken DateParser::ParseES5DateTime( |
| 197 DateStringTokenizer<Char>* scanner, | 200 DateStringTokenizer<Char>* scanner, |
| 198 DayComposer* day, | 201 DayComposer* day, |
| 199 TimeComposer* time, | 202 TimeComposer* time, |
| 200 TimeZoneComposer* tz) { | 203 TimeZoneComposer* tz) { |
| 201 DCHECK(day->IsEmpty()); | 204 DCHECK(day->IsEmpty()); |
| 202 DCHECK(time->IsEmpty()); | 205 DCHECK(time->IsEmpty()); |
| 203 DCHECK(tz->IsEmpty()); | 206 DCHECK(tz->IsEmpty()); |
| 204 | 207 |
| 205 // Parse mandatory date string: [('-'|'+')yy]yyyy[':'MM[':'DD]] | 208 // Parse mandatory date string: [('-'|'+')yy]yyyy[':'MM[':'DD]] |
| 206 if (scanner->Peek().IsAsciiSign()) { | 209 if (scanner->Peek().IsAsciiSign()) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 224 if (scanner->SkipSymbol('-')) { | 227 if (scanner->SkipSymbol('-')) { |
| 225 if (!scanner->Peek().IsFixedLengthNumber(2) || | 228 if (!scanner->Peek().IsFixedLengthNumber(2) || |
| 226 !DayComposer::IsDay(scanner->Peek().number())) return scanner->Next(); | 229 !DayComposer::IsDay(scanner->Peek().number())) return scanner->Next(); |
| 227 day->Add(scanner->Next().number()); | 230 day->Add(scanner->Next().number()); |
| 228 } | 231 } |
| 229 } | 232 } |
| 230 // Check for optional time string: 'T'HH':'mm[':'ss['.'sss]]Z | 233 // Check for optional time string: 'T'HH':'mm[':'ss['.'sss]]Z |
| 231 if (!scanner->Peek().IsKeywordType(TIME_SEPARATOR)) { | 234 if (!scanner->Peek().IsKeywordType(TIME_SEPARATOR)) { |
| 232 if (!scanner->Peek().IsEndOfInput()) return scanner->Next(); | 235 if (!scanner->Peek().IsEndOfInput()) return scanner->Next(); |
| 233 } else { | 236 } else { |
| 234 // ES6 Date Time String time part is present. | 237 // ES5 Date Time String time part is present. |
| 235 scanner->Next(); | 238 scanner->Next(); |
| 236 if (!scanner->Peek().IsFixedLengthNumber(2) || | 239 if (!scanner->Peek().IsFixedLengthNumber(2) || |
| 237 !Between(scanner->Peek().number(), 0, 24)) { | 240 !Between(scanner->Peek().number(), 0, 24)) { |
| 238 return DateToken::Invalid(); | 241 return DateToken::Invalid(); |
| 239 } | 242 } |
| 240 // Allow 24:00[:00[.000]], but no other time starting with 24. | 243 // Allow 24:00[:00[.000]], but no other time starting with 24. |
| 241 bool hour_is_24 = (scanner->Peek().number() == 24); | 244 bool hour_is_24 = (scanner->Peek().number() == 24); |
| 242 time->Add(scanner->Next().number()); | 245 time->Add(scanner->Next().number()); |
| 243 if (!scanner->SkipSymbol(':')) return DateToken::Invalid(); | 246 if (!scanner->SkipSymbol(':')) return DateToken::Invalid(); |
| 244 if (!scanner->Peek().IsFixedLengthNumber(2) || | 247 if (!scanner->Peek().IsFixedLengthNumber(2) || |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 if (!scanner->SkipSymbol(':')) return DateToken::Invalid(); | 293 if (!scanner->SkipSymbol(':')) return DateToken::Invalid(); |
| 291 if (!scanner->Peek().IsFixedLengthNumber(2) || | 294 if (!scanner->Peek().IsFixedLengthNumber(2) || |
| 292 !TimeComposer::IsMinute(scanner->Peek().number())) { | 295 !TimeComposer::IsMinute(scanner->Peek().number())) { |
| 293 return DateToken::Invalid(); | 296 return DateToken::Invalid(); |
| 294 } | 297 } |
| 295 tz->SetAbsoluteMinute(scanner->Next().number()); | 298 tz->SetAbsoluteMinute(scanner->Next().number()); |
| 296 } | 299 } |
| 297 } | 300 } |
| 298 if (!scanner->Peek().IsEndOfInput()) return DateToken::Invalid(); | 301 if (!scanner->Peek().IsEndOfInput()) return DateToken::Invalid(); |
| 299 } | 302 } |
| 300 // Successfully parsed ES6 Date Time String. | 303 // Successfully parsed ES5 Date Time String. Default to UTC if no TZ given. |
| 304 if (tz->IsEmpty()) tz->Set(0); |
| 301 day->set_iso_date(); | 305 day->set_iso_date(); |
| 302 return DateToken::EndOfInput(); | 306 return DateToken::EndOfInput(); |
| 303 } | 307 } |
| 304 | 308 |
| 305 | 309 |
| 306 } } // namespace v8::internal | 310 } } // namespace v8::internal |
| 307 | 311 |
| 308 #endif // V8_DATEPARSER_INL_H_ | 312 #endif // V8_DATEPARSER_INL_H_ |
| OLD | NEW |