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
3 // found in the LICENSE file.
5 #ifndef V8_DATEPARSER_H_
6 #define V8_DATEPARSER_H_
8 #include "src/allocation.h"
9 #include "src/char-predicates.h"
10 #include "src/scanner.h"
15 class DateParser : public AllStatic {
17 // Parse the string as a date. If parsing succeeds, return true after
18 // filling out the output array as follows (all integers are Smis):
20 // [1]: month (0 = Jan, 1 = Feb, ...)
26 // [7]: UTC offset in seconds, or null value if no timezone specified
27 // If parsing fails, return false (content of output array is not defined).
28 template <typename Char>
29 static bool Parse(Vector<Char> str, FixedArray* output, UnicodeCache* cache);
32 YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, MILLISECOND, UTC_OFFSET, OUTPUT_SIZE
37 static inline bool Between(int x, int lo, int hi) {
38 return static_cast<unsigned>(x - lo) <= static_cast<unsigned>(hi - lo);
41 // Indicates a missing value.
42 static const int kNone = kMaxInt;
44 // Maximal number of digits used to build the value of a numeral.
45 // Remaining digits are ignored.
46 static const int kMaxSignificantDigits = 9;
48 // InputReader provides basic string parsing and character classification.
49 template <typename Char>
50 class InputReader BASE_EMBEDDED {
52 InputReader(UnicodeCache* unicode_cache, Vector<Char> s)
55 unicode_cache_(unicode_cache) {
59 int position() { return index_; }
61 // Advance to the next character of the string.
63 ch_ = (index_ < buffer_.length()) ? buffer_[index_] : 0;
67 // Read a string of digits as an unsigned number. Cap value at
68 // kMaxSignificantDigits, but skip remaining digits if the numeral
70 int ReadUnsignedNumeral() {
73 while (IsAsciiDigit()) {
74 if (i < kMaxSignificantDigits) n = n * 10 + ch_ - '0';
81 // Read a word (sequence of chars. >= 'A'), fill the given buffer with a
82 // lower-case prefix, and pad any remainder of the buffer with zeroes.
83 // Return word length.
84 int ReadWord(uint32_t* prefix, int prefix_size) {
86 for (len = 0; IsAsciiAlphaOrAbove(); Next(), len++) {
87 if (len < prefix_size) prefix[len] = AsciiAlphaToLower(ch_);
89 for (int i = len; i < prefix_size; i++) prefix[i] = 0;
93 // The skip methods return whether they actually skipped something.
94 bool Skip(uint32_t c) {
102 inline bool SkipWhiteSpace();
103 inline bool SkipParentheses();
105 // Character testing/classification. Non-ASCII digits are not supported.
106 bool Is(uint32_t c) const { return ch_ == c; }
107 bool IsEnd() const { return ch_ == 0; }
108 bool IsAsciiDigit() const { return IsDecimalDigit(ch_); }
109 bool IsAsciiAlphaOrAbove() const { return ch_ >= 'A'; }
110 bool IsAsciiSign() const { return ch_ == '+' || ch_ == '-'; }
112 // Return 1 for '+' and -1 for '-'.
113 int GetAsciiSignValue() const { return 44 - static_cast<int>(ch_); }
117 Vector<Char> buffer_;
119 UnicodeCache* unicode_cache_;
123 INVALID, MONTH_NAME, TIME_ZONE_NAME, TIME_SEPARATOR, AM_PM
128 bool IsInvalid() { return tag_ == kInvalidTokenTag; }
129 bool IsUnknown() { return tag_ == kUnknownTokenTag; }
130 bool IsNumber() { return tag_ == kNumberTag; }
131 bool IsSymbol() { return tag_ == kSymbolTag; }
132 bool IsWhiteSpace() { return tag_ == kWhiteSpaceTag; }
133 bool IsEndOfInput() { return tag_ == kEndOfInputTag; }
134 bool IsKeyword() { return tag_ >= kKeywordTagStart; }
136 int length() { return length_; }
142 KeywordType keyword_type() {
144 return static_cast<KeywordType>(tag_);
146 int keyword_value() {
152 return static_cast<char>(value_);
154 bool IsSymbol(char symbol) {
155 return IsSymbol() && this->symbol() == symbol;
157 bool IsKeywordType(KeywordType tag) {
160 bool IsFixedLengthNumber(int length) {
161 return IsNumber() && length_ == length;
164 return tag_ == kSymbolTag && (value_ == '-' || value_ == '+');
167 DCHECK(IsAsciiSign());
171 return IsKeywordType(TIME_ZONE_NAME) && length_ == 1 && value_ == 0;
173 bool IsUnknown(int character) {
174 return IsUnknown() && value_ == character;
176 // Factory functions.
177 static DateToken Keyword(KeywordType tag, int value, int length) {
178 return DateToken(tag, length, value);
180 static DateToken Number(int value, int length) {
181 return DateToken(kNumberTag, length, value);
183 static DateToken Symbol(char symbol) {
184 return DateToken(kSymbolTag, 1, symbol);
186 static DateToken EndOfInput() {
187 return DateToken(kEndOfInputTag, 0, -1);
189 static DateToken WhiteSpace(int length) {
190 return DateToken(kWhiteSpaceTag, length, -1);
192 static DateToken Unknown() {
193 return DateToken(kUnknownTokenTag, 1, -1);
195 static DateToken Invalid() {
196 return DateToken(kInvalidTokenTag, 0, -1);
201 kInvalidTokenTag = -6,
202 kUnknownTokenTag = -5,
209 DateToken(int tag, int length, int value)
215 int length_; // Number of characters.
219 template <typename Char>
220 class DateStringTokenizer {
222 explicit DateStringTokenizer(InputReader<Char>* in)
223 : in_(in), next_(Scan()) { }
225 DateToken result = next_;
233 bool SkipSymbol(char symbol) {
234 if (next_.IsSymbol(symbol)) {
244 InputReader<Char>* in_;
248 static int ReadMilliseconds(DateToken number);
250 // KeywordTable maps names of months, time zones, am/pm to numbers.
251 class KeywordTable : public AllStatic {
253 // Look up a word in the keyword table and return an index.
254 // 'pre' contains a prefix of the word, zero-padded to size kPrefixLength
255 // and 'len' is the word length.
256 static int Lookup(const uint32_t* pre, int len);
257 // Get the type of the keyword at index i.
258 static KeywordType GetType(int i) {
259 return static_cast<KeywordType>(array[i][kTypeOffset]);
261 // Get the value of the keyword at index i.
262 static int GetValue(int i) { return array[i][kValueOffset]; }
264 static const int kPrefixLength = 3;
265 static const int kTypeOffset = kPrefixLength;
266 static const int kValueOffset = kTypeOffset + 1;
267 static const int kEntrySize = kValueOffset + 1;
268 static const int8_t array[][kEntrySize];
271 class TimeZoneComposer BASE_EMBEDDED {
273 TimeZoneComposer() : sign_(kNone), hour_(kNone), minute_(kNone) {}
274 void Set(int offset_in_hours) {
275 sign_ = offset_in_hours < 0 ? -1 : 1;
276 hour_ = offset_in_hours * sign_;
279 void SetSign(int sign) { sign_ = sign < 0 ? -1 : 1; }
280 void SetAbsoluteHour(int hour) { hour_ = hour; }
281 void SetAbsoluteMinute(int minute) { minute_ = minute; }
282 bool IsExpecting(int n) const {
283 return hour_ != kNone && minute_ == kNone && TimeComposer::IsMinute(n);
285 bool IsUTC() const { return hour_ == 0 && minute_ == 0; }
286 bool Write(FixedArray* output);
287 bool IsEmpty() { return hour_ == kNone; }
294 class TimeComposer BASE_EMBEDDED {
296 TimeComposer() : index_(0), hour_offset_(kNone) {}
297 bool IsEmpty() const { return index_ == 0; }
298 bool IsExpecting(int n) const {
299 return (index_ == 1 && IsMinute(n)) ||
300 (index_ == 2 && IsSecond(n)) ||
301 (index_ == 3 && IsMillisecond(n));
304 return index_ < kSize ? (comp_[index_++] = n, true) : false;
306 bool AddFinal(int n) {
307 if (!Add(n)) return false;
308 while (index_ < kSize) comp_[index_++] = 0;
311 void SetHourOffset(int n) { hour_offset_ = n; }
312 bool Write(FixedArray* output);
314 static bool IsMinute(int x) { return Between(x, 0, 59); }
315 static bool IsHour(int x) { return Between(x, 0, 23); }
316 static bool IsSecond(int x) { return Between(x, 0, 59); }
319 static bool IsHour12(int x) { return Between(x, 0, 12); }
320 static bool IsMillisecond(int x) { return Between(x, 0, 999); }
322 static const int kSize = 4;
328 class DayComposer BASE_EMBEDDED {
330 DayComposer() : index_(0), named_month_(kNone), is_iso_date_(false) {}
331 bool IsEmpty() const { return index_ == 0; }
333 if (index_ < kSize) {
340 void SetNamedMonth(int n) { named_month_ = n; }
341 bool Write(FixedArray* output);
342 void set_iso_date() { is_iso_date_ = true; }
343 static bool IsMonth(int x) { return Between(x, 1, 12); }
344 static bool IsDay(int x) { return Between(x, 1, 31); }
347 static const int kSize = 3;
351 // If set, ensures that data is always parsed in year-month-date order.
355 // Tries to parse an ES6 Date Time String. Returns the next token
356 // to continue with in the legacy date string parser. If parsing is
357 // complete, returns DateToken::EndOfInput(). If terminally unsuccessful,
358 // returns DateToken::Invalid(). Otherwise parsing continues in the
360 template <typename Char>
361 static DateParser::DateToken ParseES6DateTime(
362 DateStringTokenizer<Char>* scanner,
365 TimeZoneComposer* tz);
369 } } // namespace v8::internal
371 #endif // V8_DATEPARSER_H_