2011-04-12 08:27:38 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2009-03-17 13:27:21 +00:00
|
|
|
|
2009-05-04 13:36:43 +00:00
|
|
|
#ifndef V8_DATEPARSER_INL_H_
|
|
|
|
#define V8_DATEPARSER_INL_H_
|
|
|
|
|
2015-08-12 07:32:36 +00:00
|
|
|
#include "src/char-predicates-inl.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/dateparser.h"
|
2015-08-20 11:46:54 +00:00
|
|
|
#include "src/unicode-cache-inl.h"
|
2009-10-15 15:01:36 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2009-03-17 13:27:21 +00:00
|
|
|
|
|
|
|
template <typename Char>
|
2011-04-12 08:27:38 +00:00
|
|
|
bool DateParser::Parse(Vector<Char> str,
|
|
|
|
FixedArray* out,
|
|
|
|
UnicodeCache* unicode_cache) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(out->length() >= OUTPUT_SIZE);
|
2011-04-12 08:27:38 +00:00
|
|
|
InputReader<Char> in(unicode_cache, str);
|
2011-07-01 11:41:45 +00:00
|
|
|
DateStringTokenizer<Char> scanner(&in);
|
2009-03-17 13:27:21 +00:00
|
|
|
TimeZoneComposer tz;
|
|
|
|
TimeComposer time;
|
|
|
|
DayComposer day;
|
|
|
|
|
2011-07-01 11:41:45 +00:00
|
|
|
// Specification:
|
2015-10-15 12:17:56 +00:00
|
|
|
// Accept ES5 ISO 8601 date-time-strings or legacy dates compatible
|
2011-07-01 11:41:45 +00:00
|
|
|
// with Safari.
|
2015-10-15 12:17:56 +00:00
|
|
|
// ES5 ISO 8601 dates:
|
2011-07-01 11:41:45 +00:00
|
|
|
// [('-'|'+')yy]yyyy[-MM[-DD]][THH:mm[:ss[.sss]][Z|(+|-)hh:mm]]
|
|
|
|
// where yyyy is in the range 0000..9999 and
|
|
|
|
// +/-yyyyyy is in the range -999999..+999999 -
|
|
|
|
// but -000000 is invalid (year zero must be positive),
|
|
|
|
// MM is in the range 01..12,
|
|
|
|
// DD is in the range 01..31,
|
|
|
|
// MM and DD defaults to 01 if missing,,
|
|
|
|
// HH is generally in the range 00..23, but can be 24 if mm, ss
|
|
|
|
// and sss are zero (or missing), representing midnight at the
|
|
|
|
// end of a day,
|
|
|
|
// mm and ss are in the range 00..59,
|
|
|
|
// sss is in the range 000..999,
|
|
|
|
// hh is in the range 00..23,
|
|
|
|
// mm, ss, and sss default to 00 if missing, and
|
2015-10-15 12:17:56 +00:00
|
|
|
// timezone defaults to Z if missing
|
|
|
|
// (following Safari, ISO actually demands local time).
|
2011-07-01 11:41:45 +00:00
|
|
|
// Extensions:
|
|
|
|
// We also allow sss to have more or less than three digits (but at
|
|
|
|
// least one).
|
|
|
|
// We allow hh:mm to be specified as hhmm.
|
|
|
|
// Legacy dates:
|
|
|
|
// Any unrecognized word before the first number is ignored.
|
|
|
|
// Parenthesized text is ignored.
|
|
|
|
// An unsigned number followed by ':' is a time value, and is
|
|
|
|
// added to the TimeComposer. A number followed by '::' adds a second
|
|
|
|
// zero as well. A number followed by '.' is also a time and must be
|
|
|
|
// followed by milliseconds.
|
|
|
|
// Any other number is a date component and is added to DayComposer.
|
|
|
|
// A month name (or really: any word having the same first three letters
|
|
|
|
// as a month name) is recorded as a named month in the Day composer.
|
|
|
|
// A word recognizable as a time-zone is recorded as such, as is
|
|
|
|
// '(+|-)(hhmm|hh:)'.
|
|
|
|
// Legacy dates don't allow extra signs ('+' or '-') or umatched ')'
|
|
|
|
// after a number has been read (before the first number, any garbage
|
|
|
|
// is allowed).
|
|
|
|
// Intersection of the two:
|
|
|
|
// A string that matches both formats (e.g. 1970-01-01) will be
|
2015-10-15 12:17:56 +00:00
|
|
|
// parsed as an ES5 date-time string - which means it will default
|
|
|
|
// to UTC time-zone. That's unavoidable if following the ES5
|
|
|
|
// specification.
|
|
|
|
// After a valid "T" has been read while scanning an ES5 datetime string,
|
2011-07-01 11:41:45 +00:00
|
|
|
// the input can no longer be a valid legacy date, since the "T" is a
|
|
|
|
// garbage string after a number has been read.
|
|
|
|
|
2015-10-15 12:17:56 +00:00
|
|
|
// First try getting as far as possible with as ES5 Date Time String.
|
|
|
|
DateToken next_unhandled_token = ParseES5DateTime(&scanner, &day, &time, &tz);
|
2011-07-01 11:41:45 +00:00
|
|
|
if (next_unhandled_token.IsInvalid()) return false;
|
|
|
|
bool has_read_number = !day.IsEmpty();
|
|
|
|
// If there's anything left, continue with the legacy parser.
|
|
|
|
for (DateToken token = next_unhandled_token;
|
|
|
|
!token.IsEndOfInput();
|
|
|
|
token = scanner.Next()) {
|
|
|
|
if (token.IsNumber()) {
|
|
|
|
has_read_number = true;
|
|
|
|
int n = token.number();
|
|
|
|
if (scanner.SkipSymbol(':')) {
|
|
|
|
if (scanner.SkipSymbol(':')) {
|
2009-03-17 13:27:21 +00:00
|
|
|
// n + "::"
|
|
|
|
if (!time.IsEmpty()) return false;
|
|
|
|
time.Add(n);
|
|
|
|
time.Add(0);
|
|
|
|
} else {
|
|
|
|
// n + ":"
|
|
|
|
if (!time.Add(n)) return false;
|
2011-07-01 11:41:45 +00:00
|
|
|
if (scanner.Peek().IsSymbol('.')) scanner.Next();
|
2009-03-17 13:27:21 +00:00
|
|
|
}
|
2011-07-01 11:41:45 +00:00
|
|
|
} else if (scanner.SkipSymbol('.') && time.IsExpecting(n)) {
|
2010-05-03 06:43:25 +00:00
|
|
|
time.Add(n);
|
2011-07-01 11:41:45 +00:00
|
|
|
if (!scanner.Peek().IsNumber()) return false;
|
|
|
|
int n = ReadMilliseconds(scanner.Next());
|
|
|
|
if (n < 0) return false;
|
2010-05-03 06:43:25 +00:00
|
|
|
time.AddFinal(n);
|
2009-03-17 13:27:21 +00:00
|
|
|
} else if (tz.IsExpecting(n)) {
|
|
|
|
tz.SetAbsoluteMinute(n);
|
|
|
|
} else if (time.IsExpecting(n)) {
|
|
|
|
time.AddFinal(n);
|
2010-09-10 07:00:28 +00:00
|
|
|
// Require end, white space, "Z", "+" or "-" immediately after
|
|
|
|
// finalizing time.
|
2011-07-01 11:41:45 +00:00
|
|
|
DateToken peek = scanner.Peek();
|
|
|
|
if (!peek.IsEndOfInput() &&
|
|
|
|
!peek.IsWhiteSpace() &&
|
|
|
|
!peek.IsKeywordZ() &&
|
|
|
|
!peek.IsAsciiSign()) return false;
|
2009-03-17 13:27:21 +00:00
|
|
|
} else {
|
|
|
|
if (!day.Add(n)) return false;
|
2011-07-01 11:41:45 +00:00
|
|
|
scanner.SkipSymbol('-');
|
2009-03-17 13:27:21 +00:00
|
|
|
}
|
2011-07-01 11:41:45 +00:00
|
|
|
} else if (token.IsKeyword()) {
|
2009-03-17 13:27:21 +00:00
|
|
|
// Parse a "word" (sequence of chars. >= 'A').
|
2011-07-01 11:41:45 +00:00
|
|
|
KeywordType type = token.keyword_type();
|
|
|
|
int value = token.keyword_value();
|
2009-03-17 13:27:21 +00:00
|
|
|
if (type == AM_PM && !time.IsEmpty()) {
|
2011-07-01 11:41:45 +00:00
|
|
|
time.SetHourOffset(value);
|
2009-03-17 13:27:21 +00:00
|
|
|
} else if (type == MONTH_NAME) {
|
2011-07-01 11:41:45 +00:00
|
|
|
day.SetNamedMonth(value);
|
|
|
|
scanner.SkipSymbol('-');
|
|
|
|
} else if (type == TIME_ZONE_NAME && has_read_number) {
|
|
|
|
tz.Set(value);
|
2009-03-17 13:27:21 +00:00
|
|
|
} else {
|
|
|
|
// Garbage words are illegal if a number has been read.
|
2011-07-01 11:41:45 +00:00
|
|
|
if (has_read_number) return false;
|
2012-06-13 09:10:37 +00:00
|
|
|
// The first number has to be separated from garbage words by
|
|
|
|
// whitespace or other separators.
|
|
|
|
if (scanner.Peek().IsNumber()) return false;
|
2009-03-17 13:27:21 +00:00
|
|
|
}
|
2011-07-01 11:41:45 +00:00
|
|
|
} else if (token.IsAsciiSign() && (tz.IsUTC() || !time.IsEmpty())) {
|
2009-03-17 13:27:21 +00:00
|
|
|
// Parse UTC offset (only after UTC or time).
|
2011-07-01 11:41:45 +00:00
|
|
|
tz.SetSign(token.ascii_sign());
|
|
|
|
// The following number may be empty.
|
|
|
|
int n = 0;
|
2016-01-04 23:25:27 +00:00
|
|
|
int length = 0;
|
2011-07-01 11:41:45 +00:00
|
|
|
if (scanner.Peek().IsNumber()) {
|
2016-01-04 23:25:27 +00:00
|
|
|
DateToken token = scanner.Next();
|
|
|
|
length = token.length();
|
|
|
|
n = token.number();
|
2011-07-01 11:41:45 +00:00
|
|
|
}
|
|
|
|
has_read_number = true;
|
|
|
|
|
|
|
|
if (scanner.Peek().IsSymbol(':')) {
|
2009-03-17 13:27:21 +00:00
|
|
|
tz.SetAbsoluteHour(n);
|
2016-01-04 23:25:27 +00:00
|
|
|
// TODO(littledan): Use minutes as part of timezone?
|
2009-03-17 13:27:21 +00:00
|
|
|
tz.SetAbsoluteMinute(kNone);
|
2016-01-04 23:25:27 +00:00
|
|
|
} else if (length == 2 || length == 1) {
|
|
|
|
// Handle time zones like GMT-8
|
|
|
|
tz.SetAbsoluteHour(n);
|
|
|
|
tz.SetAbsoluteMinute(0);
|
|
|
|
} else if (length == 4 || length == 3) {
|
|
|
|
// Looks like the hhmm format
|
2009-03-17 13:27:21 +00:00
|
|
|
tz.SetAbsoluteHour(n / 100);
|
|
|
|
tz.SetAbsoluteMinute(n % 100);
|
2016-01-04 23:25:27 +00:00
|
|
|
} else {
|
|
|
|
// No need to accept time zones like GMT-12345
|
|
|
|
return false;
|
2009-03-17 13:27:21 +00:00
|
|
|
}
|
2011-07-01 11:41:45 +00:00
|
|
|
} else if ((token.IsAsciiSign() || token.IsSymbol(')')) &&
|
|
|
|
has_read_number) {
|
2009-03-17 13:27:21 +00:00
|
|
|
// Extra sign or ')' is illegal if a number has been read.
|
|
|
|
return false;
|
|
|
|
} else {
|
2011-07-01 11:41:45 +00:00
|
|
|
// Ignore other characters and whitespace.
|
2009-03-17 13:27:21 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-01 11:41:45 +00:00
|
|
|
|
2009-03-17 13:27:21 +00:00
|
|
|
return day.Write(out) && time.Write(out) && tz.Write(out);
|
|
|
|
}
|
|
|
|
|
2011-07-01 11:41:45 +00:00
|
|
|
|
|
|
|
template<typename CharType>
|
|
|
|
DateParser::DateToken DateParser::DateStringTokenizer<CharType>::Scan() {
|
|
|
|
int pre_pos = in_->position();
|
|
|
|
if (in_->IsEnd()) return DateToken::EndOfInput();
|
|
|
|
if (in_->IsAsciiDigit()) {
|
|
|
|
int n = in_->ReadUnsignedNumeral();
|
|
|
|
int length = in_->position() - pre_pos;
|
|
|
|
return DateToken::Number(n, length);
|
|
|
|
}
|
|
|
|
if (in_->Skip(':')) return DateToken::Symbol(':');
|
|
|
|
if (in_->Skip('-')) return DateToken::Symbol('-');
|
|
|
|
if (in_->Skip('+')) return DateToken::Symbol('+');
|
|
|
|
if (in_->Skip('.')) return DateToken::Symbol('.');
|
|
|
|
if (in_->Skip(')')) return DateToken::Symbol(')');
|
|
|
|
if (in_->IsAsciiAlphaOrAbove()) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(KeywordTable::kPrefixLength == 3);
|
2011-07-01 11:41:45 +00:00
|
|
|
uint32_t buffer[3] = {0, 0, 0};
|
|
|
|
int length = in_->ReadWord(buffer, 3);
|
|
|
|
int index = KeywordTable::Lookup(buffer, length);
|
|
|
|
return DateToken::Keyword(KeywordTable::GetType(index),
|
|
|
|
KeywordTable::GetValue(index),
|
|
|
|
length);
|
|
|
|
}
|
|
|
|
if (in_->SkipWhiteSpace()) {
|
|
|
|
return DateToken::WhiteSpace(in_->position() - pre_pos);
|
|
|
|
}
|
|
|
|
if (in_->SkipParentheses()) {
|
|
|
|
return DateToken::Unknown();
|
|
|
|
}
|
|
|
|
in_->Next();
|
|
|
|
return DateToken::Unknown();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-20 11:46:54 +00:00
|
|
|
template <typename Char>
|
|
|
|
bool DateParser::InputReader<Char>::SkipWhiteSpace() {
|
|
|
|
if (unicode_cache_->IsWhiteSpaceOrLineTerminator(ch_)) {
|
|
|
|
Next();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Char>
|
|
|
|
bool DateParser::InputReader<Char>::SkipParentheses() {
|
|
|
|
if (ch_ != '(') return false;
|
|
|
|
int balance = 0;
|
|
|
|
do {
|
|
|
|
if (ch_ == ')') --balance;
|
|
|
|
else if (ch_ == '(') ++balance;
|
|
|
|
Next();
|
|
|
|
} while (balance > 0 && ch_);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-01 11:41:45 +00:00
|
|
|
template <typename Char>
|
2015-10-15 12:17:56 +00:00
|
|
|
DateParser::DateToken DateParser::ParseES5DateTime(
|
|
|
|
DateStringTokenizer<Char>* scanner, DayComposer* day, TimeComposer* time,
|
2011-07-01 11:41:45 +00:00
|
|
|
TimeZoneComposer* tz) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(day->IsEmpty());
|
|
|
|
DCHECK(time->IsEmpty());
|
|
|
|
DCHECK(tz->IsEmpty());
|
2011-07-01 11:41:45 +00:00
|
|
|
|
|
|
|
// Parse mandatory date string: [('-'|'+')yy]yyyy[':'MM[':'DD]]
|
|
|
|
if (scanner->Peek().IsAsciiSign()) {
|
|
|
|
// Keep the sign token, so we can pass it back to the legacy
|
|
|
|
// parser if we don't use it.
|
|
|
|
DateToken sign_token = scanner->Next();
|
|
|
|
if (!scanner->Peek().IsFixedLengthNumber(6)) return sign_token;
|
|
|
|
int sign = sign_token.ascii_sign();
|
|
|
|
int year = scanner->Next().number();
|
|
|
|
if (sign < 0 && year == 0) return sign_token;
|
|
|
|
day->Add(sign * year);
|
|
|
|
} else if (scanner->Peek().IsFixedLengthNumber(4)) {
|
|
|
|
day->Add(scanner->Next().number());
|
|
|
|
} else {
|
|
|
|
return scanner->Next();
|
|
|
|
}
|
|
|
|
if (scanner->SkipSymbol('-')) {
|
|
|
|
if (!scanner->Peek().IsFixedLengthNumber(2) ||
|
|
|
|
!DayComposer::IsMonth(scanner->Peek().number())) return scanner->Next();
|
|
|
|
day->Add(scanner->Next().number());
|
|
|
|
if (scanner->SkipSymbol('-')) {
|
|
|
|
if (!scanner->Peek().IsFixedLengthNumber(2) ||
|
|
|
|
!DayComposer::IsDay(scanner->Peek().number())) return scanner->Next();
|
|
|
|
day->Add(scanner->Next().number());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check for optional time string: 'T'HH':'mm[':'ss['.'sss]]Z
|
|
|
|
if (!scanner->Peek().IsKeywordType(TIME_SEPARATOR)) {
|
|
|
|
if (!scanner->Peek().IsEndOfInput()) return scanner->Next();
|
|
|
|
} else {
|
2015-10-15 12:17:56 +00:00
|
|
|
// ES5 Date Time String time part is present.
|
2011-07-01 11:41:45 +00:00
|
|
|
scanner->Next();
|
|
|
|
if (!scanner->Peek().IsFixedLengthNumber(2) ||
|
|
|
|
!Between(scanner->Peek().number(), 0, 24)) {
|
|
|
|
return DateToken::Invalid();
|
|
|
|
}
|
|
|
|
// Allow 24:00[:00[.000]], but no other time starting with 24.
|
|
|
|
bool hour_is_24 = (scanner->Peek().number() == 24);
|
|
|
|
time->Add(scanner->Next().number());
|
|
|
|
if (!scanner->SkipSymbol(':')) return DateToken::Invalid();
|
|
|
|
if (!scanner->Peek().IsFixedLengthNumber(2) ||
|
|
|
|
!TimeComposer::IsMinute(scanner->Peek().number()) ||
|
|
|
|
(hour_is_24 && scanner->Peek().number() > 0)) {
|
|
|
|
return DateToken::Invalid();
|
|
|
|
}
|
|
|
|
time->Add(scanner->Next().number());
|
|
|
|
if (scanner->SkipSymbol(':')) {
|
|
|
|
if (!scanner->Peek().IsFixedLengthNumber(2) ||
|
|
|
|
!TimeComposer::IsSecond(scanner->Peek().number()) ||
|
|
|
|
(hour_is_24 && scanner->Peek().number() > 0)) {
|
|
|
|
return DateToken::Invalid();
|
|
|
|
}
|
|
|
|
time->Add(scanner->Next().number());
|
|
|
|
if (scanner->SkipSymbol('.')) {
|
|
|
|
if (!scanner->Peek().IsNumber() ||
|
|
|
|
(hour_is_24 && scanner->Peek().number() > 0)) {
|
|
|
|
return DateToken::Invalid();
|
|
|
|
}
|
|
|
|
// Allow more or less than the mandated three digits.
|
|
|
|
time->Add(ReadMilliseconds(scanner->Next()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check for optional timezone designation: 'Z' | ('+'|'-')hh':'mm
|
|
|
|
if (scanner->Peek().IsKeywordZ()) {
|
|
|
|
scanner->Next();
|
|
|
|
tz->Set(0);
|
|
|
|
} else if (scanner->Peek().IsSymbol('+') ||
|
|
|
|
scanner->Peek().IsSymbol('-')) {
|
|
|
|
tz->SetSign(scanner->Next().symbol() == '+' ? 1 : -1);
|
|
|
|
if (scanner->Peek().IsFixedLengthNumber(4)) {
|
|
|
|
// hhmm extension syntax.
|
|
|
|
int hourmin = scanner->Next().number();
|
|
|
|
int hour = hourmin / 100;
|
|
|
|
int min = hourmin % 100;
|
|
|
|
if (!TimeComposer::IsHour(hour) || !TimeComposer::IsMinute(min)) {
|
|
|
|
return DateToken::Invalid();
|
|
|
|
}
|
|
|
|
tz->SetAbsoluteHour(hour);
|
|
|
|
tz->SetAbsoluteMinute(min);
|
|
|
|
} else {
|
|
|
|
// hh:mm standard syntax.
|
|
|
|
if (!scanner->Peek().IsFixedLengthNumber(2) ||
|
|
|
|
!TimeComposer::IsHour(scanner->Peek().number())) {
|
|
|
|
return DateToken::Invalid();
|
|
|
|
}
|
|
|
|
tz->SetAbsoluteHour(scanner->Next().number());
|
|
|
|
if (!scanner->SkipSymbol(':')) return DateToken::Invalid();
|
|
|
|
if (!scanner->Peek().IsFixedLengthNumber(2) ||
|
|
|
|
!TimeComposer::IsMinute(scanner->Peek().number())) {
|
|
|
|
return DateToken::Invalid();
|
|
|
|
}
|
|
|
|
tz->SetAbsoluteMinute(scanner->Next().number());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!scanner->Peek().IsEndOfInput()) return DateToken::Invalid();
|
|
|
|
}
|
2015-10-15 12:17:56 +00:00
|
|
|
// Successfully parsed ES5 Date Time String. Default to UTC if no TZ given.
|
|
|
|
if (tz->IsEmpty()) tz->Set(0);
|
2011-07-01 11:41:45 +00:00
|
|
|
day->set_iso_date();
|
|
|
|
return DateToken::EndOfInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-30 13:46:56 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
2009-05-04 13:36:43 +00:00
|
|
|
|
|
|
|
#endif // V8_DATEPARSER_INL_H_
|