| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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. |
| 4 | |
| 5 | // Features shared by parsing and pre-parsing scanners. |
| 6 | |
| 7 | #include "src/parsing/scanner.h" |
| 8 | |
| 9 | #include <stdint.h> |
| 10 | |
| 11 | #include <cmath> |
| 12 | |
| 13 | #include "src/ast/ast-value-factory.h" |
| 14 | #include "src/char-predicates-inl.h" |
| 15 | #include "src/conversions-inl.h" |
| 16 | #include "src/list-inl.h" |
| 17 | #include "src/parsing/parser.h" |
| 18 | |
| 19 | namespace v8 { |
| 20 | namespace internal { |
| 21 | |
| 22 | |
| 23 | Handle<String> LiteralBuffer::Internalize(Isolate* isolate) const { |
| 24 | if (is_one_byte()) { |
| 25 | return isolate->factory()->InternalizeOneByteString(one_byte_literal()); |
| 26 | } |
| 27 | return isolate->factory()->InternalizeTwoByteString(two_byte_literal()); |
| 28 | } |
| 29 | |
| 30 | |
| 31 | // Default implementation for streams that do not support bookmarks. |
| 32 | bool Utf16CharacterStream::SetBookmark() { return false; } |
| 33 | void Utf16CharacterStream::ResetToBookmark() { UNREACHABLE(); } |
| 34 | |
| 35 | |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | // Scanner |
| 38 | |
| 39 | Scanner::Scanner(UnicodeCache* unicode_cache) |
| 40 | : unicode_cache_(unicode_cache), |
| 41 | bookmark_c0_(kNoBookmark), |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 42 | octal_pos_(Location::invalid()), |
| 43 | found_html_comment_(false) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 44 | bookmark_current_.literal_chars = &bookmark_current_literal_; |
| 45 | bookmark_current_.raw_literal_chars = &bookmark_current_raw_literal_; |
| 46 | bookmark_next_.literal_chars = &bookmark_next_literal_; |
| 47 | bookmark_next_.raw_literal_chars = &bookmark_next_raw_literal_; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | void Scanner::Initialize(Utf16CharacterStream* source) { |
| 52 | source_ = source; |
| 53 | // Need to capture identifiers in order to recognize "get" and "set" |
| 54 | // in object literals. |
| 55 | Init(); |
| 56 | // Skip initial whitespace allowing HTML comment ends just like |
| 57 | // after a newline and scan first token. |
| 58 | has_line_terminator_before_next_ = true; |
| 59 | SkipWhiteSpace(); |
| 60 | Scan(); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | template <bool capture_raw> |
| 65 | uc32 Scanner::ScanHexNumber(int expected_length) { |
| 66 | DCHECK(expected_length <= 4); // prevent overflow |
| 67 | |
| 68 | uc32 x = 0; |
| 69 | for (int i = 0; i < expected_length; i++) { |
| 70 | int d = HexValue(c0_); |
| 71 | if (d < 0) { |
| 72 | return -1; |
| 73 | } |
| 74 | x = x * 16 + d; |
| 75 | Advance<capture_raw>(); |
| 76 | } |
| 77 | |
| 78 | return x; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | template <bool capture_raw> |
| 83 | uc32 Scanner::ScanUnlimitedLengthHexNumber(int max_value) { |
| 84 | uc32 x = 0; |
| 85 | int d = HexValue(c0_); |
| 86 | if (d < 0) { |
| 87 | return -1; |
| 88 | } |
| 89 | while (d >= 0) { |
| 90 | x = x * 16 + d; |
| 91 | if (x > max_value) return -1; |
| 92 | Advance<capture_raw>(); |
| 93 | d = HexValue(c0_); |
| 94 | } |
| 95 | return x; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | // Ensure that tokens can be stored in a byte. |
| 100 | STATIC_ASSERT(Token::NUM_TOKENS <= 0x100); |
| 101 | |
| 102 | // Table of one-character tokens, by character (0x00..0x7f only). |
| 103 | static const byte one_char_tokens[] = { |
| 104 | Token::ILLEGAL, |
| 105 | Token::ILLEGAL, |
| 106 | Token::ILLEGAL, |
| 107 | Token::ILLEGAL, |
| 108 | Token::ILLEGAL, |
| 109 | Token::ILLEGAL, |
| 110 | Token::ILLEGAL, |
| 111 | Token::ILLEGAL, |
| 112 | Token::ILLEGAL, |
| 113 | Token::ILLEGAL, |
| 114 | Token::ILLEGAL, |
| 115 | Token::ILLEGAL, |
| 116 | Token::ILLEGAL, |
| 117 | Token::ILLEGAL, |
| 118 | Token::ILLEGAL, |
| 119 | Token::ILLEGAL, |
| 120 | Token::ILLEGAL, |
| 121 | Token::ILLEGAL, |
| 122 | Token::ILLEGAL, |
| 123 | Token::ILLEGAL, |
| 124 | Token::ILLEGAL, |
| 125 | Token::ILLEGAL, |
| 126 | Token::ILLEGAL, |
| 127 | Token::ILLEGAL, |
| 128 | Token::ILLEGAL, |
| 129 | Token::ILLEGAL, |
| 130 | Token::ILLEGAL, |
| 131 | Token::ILLEGAL, |
| 132 | Token::ILLEGAL, |
| 133 | Token::ILLEGAL, |
| 134 | Token::ILLEGAL, |
| 135 | Token::ILLEGAL, |
| 136 | Token::ILLEGAL, |
| 137 | Token::ILLEGAL, |
| 138 | Token::ILLEGAL, |
| 139 | Token::ILLEGAL, |
| 140 | Token::ILLEGAL, |
| 141 | Token::ILLEGAL, |
| 142 | Token::ILLEGAL, |
| 143 | Token::ILLEGAL, |
| 144 | Token::LPAREN, // 0x28 |
| 145 | Token::RPAREN, // 0x29 |
| 146 | Token::ILLEGAL, |
| 147 | Token::ILLEGAL, |
| 148 | Token::COMMA, // 0x2c |
| 149 | Token::ILLEGAL, |
| 150 | Token::ILLEGAL, |
| 151 | Token::ILLEGAL, |
| 152 | Token::ILLEGAL, |
| 153 | Token::ILLEGAL, |
| 154 | Token::ILLEGAL, |
| 155 | Token::ILLEGAL, |
| 156 | Token::ILLEGAL, |
| 157 | Token::ILLEGAL, |
| 158 | Token::ILLEGAL, |
| 159 | Token::ILLEGAL, |
| 160 | Token::ILLEGAL, |
| 161 | Token::ILLEGAL, |
| 162 | Token::COLON, // 0x3a |
| 163 | Token::SEMICOLON, // 0x3b |
| 164 | Token::ILLEGAL, |
| 165 | Token::ILLEGAL, |
| 166 | Token::ILLEGAL, |
| 167 | Token::CONDITIONAL, // 0x3f |
| 168 | Token::ILLEGAL, |
| 169 | Token::ILLEGAL, |
| 170 | Token::ILLEGAL, |
| 171 | Token::ILLEGAL, |
| 172 | Token::ILLEGAL, |
| 173 | Token::ILLEGAL, |
| 174 | Token::ILLEGAL, |
| 175 | Token::ILLEGAL, |
| 176 | Token::ILLEGAL, |
| 177 | Token::ILLEGAL, |
| 178 | Token::ILLEGAL, |
| 179 | Token::ILLEGAL, |
| 180 | Token::ILLEGAL, |
| 181 | Token::ILLEGAL, |
| 182 | Token::ILLEGAL, |
| 183 | Token::ILLEGAL, |
| 184 | Token::ILLEGAL, |
| 185 | Token::ILLEGAL, |
| 186 | Token::ILLEGAL, |
| 187 | Token::ILLEGAL, |
| 188 | Token::ILLEGAL, |
| 189 | Token::ILLEGAL, |
| 190 | Token::ILLEGAL, |
| 191 | Token::ILLEGAL, |
| 192 | Token::ILLEGAL, |
| 193 | Token::ILLEGAL, |
| 194 | Token::ILLEGAL, |
| 195 | Token::LBRACK, // 0x5b |
| 196 | Token::ILLEGAL, |
| 197 | Token::RBRACK, // 0x5d |
| 198 | Token::ILLEGAL, |
| 199 | Token::ILLEGAL, |
| 200 | Token::ILLEGAL, |
| 201 | Token::ILLEGAL, |
| 202 | Token::ILLEGAL, |
| 203 | Token::ILLEGAL, |
| 204 | Token::ILLEGAL, |
| 205 | Token::ILLEGAL, |
| 206 | Token::ILLEGAL, |
| 207 | Token::ILLEGAL, |
| 208 | Token::ILLEGAL, |
| 209 | Token::ILLEGAL, |
| 210 | Token::ILLEGAL, |
| 211 | Token::ILLEGAL, |
| 212 | Token::ILLEGAL, |
| 213 | Token::ILLEGAL, |
| 214 | Token::ILLEGAL, |
| 215 | Token::ILLEGAL, |
| 216 | Token::ILLEGAL, |
| 217 | Token::ILLEGAL, |
| 218 | Token::ILLEGAL, |
| 219 | Token::ILLEGAL, |
| 220 | Token::ILLEGAL, |
| 221 | Token::ILLEGAL, |
| 222 | Token::ILLEGAL, |
| 223 | Token::ILLEGAL, |
| 224 | Token::ILLEGAL, |
| 225 | Token::ILLEGAL, |
| 226 | Token::ILLEGAL, |
| 227 | Token::LBRACE, // 0x7b |
| 228 | Token::ILLEGAL, |
| 229 | Token::RBRACE, // 0x7d |
| 230 | Token::BIT_NOT, // 0x7e |
| 231 | Token::ILLEGAL |
| 232 | }; |
| 233 | |
| 234 | |
| 235 | Token::Value Scanner::Next() { |
| 236 | if (next_.token == Token::EOS) { |
| 237 | next_.location.beg_pos = current_.location.beg_pos; |
| 238 | next_.location.end_pos = current_.location.end_pos; |
| 239 | } |
| 240 | current_ = next_; |
| 241 | if (V8_UNLIKELY(next_next_.token != Token::UNINITIALIZED)) { |
| 242 | next_ = next_next_; |
| 243 | next_next_.token = Token::UNINITIALIZED; |
| 244 | return current_.token; |
| 245 | } |
| 246 | has_line_terminator_before_next_ = false; |
| 247 | has_multiline_comment_before_next_ = false; |
| 248 | if (static_cast<unsigned>(c0_) <= 0x7f) { |
| 249 | Token::Value token = static_cast<Token::Value>(one_char_tokens[c0_]); |
| 250 | if (token != Token::ILLEGAL) { |
| 251 | int pos = source_pos(); |
| 252 | next_.token = token; |
| 253 | next_.location.beg_pos = pos; |
| 254 | next_.location.end_pos = pos + 1; |
| 255 | Advance(); |
| 256 | return current_.token; |
| 257 | } |
| 258 | } |
| 259 | Scan(); |
| 260 | return current_.token; |
| 261 | } |
| 262 | |
| 263 | |
| 264 | Token::Value Scanner::PeekAhead() { |
| 265 | if (next_next_.token != Token::UNINITIALIZED) { |
| 266 | return next_next_.token; |
| 267 | } |
| 268 | TokenDesc prev = current_; |
| 269 | Next(); |
| 270 | Token::Value ret = next_.token; |
| 271 | next_next_ = next_; |
| 272 | next_ = current_; |
| 273 | current_ = prev; |
| 274 | return ret; |
| 275 | } |
| 276 | |
| 277 | |
| 278 | // TODO(yangguo): check whether this is actually necessary. |
| 279 | static inline bool IsLittleEndianByteOrderMark(uc32 c) { |
| 280 | // The Unicode value U+FFFE is guaranteed never to be assigned as a |
| 281 | // Unicode character; this implies that in a Unicode context the |
| 282 | // 0xFF, 0xFE byte pattern can only be interpreted as the U+FEFF |
| 283 | // character expressed in little-endian byte order (since it could |
| 284 | // not be a U+FFFE character expressed in big-endian byte |
| 285 | // order). Nevertheless, we check for it to be compatible with |
| 286 | // Spidermonkey. |
| 287 | return c == 0xFFFE; |
| 288 | } |
| 289 | |
| 290 | |
| 291 | bool Scanner::SkipWhiteSpace() { |
| 292 | int start_position = source_pos(); |
| 293 | |
| 294 | while (true) { |
| 295 | while (true) { |
| 296 | // The unicode cache accepts unsigned inputs. |
| 297 | if (c0_ < 0) break; |
| 298 | // Advance as long as character is a WhiteSpace or LineTerminator. |
| 299 | // Remember if the latter is the case. |
| 300 | if (unicode_cache_->IsLineTerminator(c0_)) { |
| 301 | has_line_terminator_before_next_ = true; |
| 302 | } else if (!unicode_cache_->IsWhiteSpace(c0_) && |
| 303 | !IsLittleEndianByteOrderMark(c0_)) { |
| 304 | break; |
| 305 | } |
| 306 | Advance(); |
| 307 | } |
| 308 | |
| 309 | // If there is an HTML comment end '-->' at the beginning of a |
| 310 | // line (with only whitespace in front of it), we treat the rest |
| 311 | // of the line as a comment. This is in line with the way |
| 312 | // SpiderMonkey handles it. |
| 313 | if (c0_ == '-' && has_line_terminator_before_next_) { |
| 314 | Advance(); |
| 315 | if (c0_ == '-') { |
| 316 | Advance(); |
| 317 | if (c0_ == '>') { |
| 318 | // Treat the rest of the line as a comment. |
| 319 | SkipSingleLineComment(); |
| 320 | // Continue skipping white space after the comment. |
| 321 | continue; |
| 322 | } |
| 323 | PushBack('-'); // undo Advance() |
| 324 | } |
| 325 | PushBack('-'); // undo Advance() |
| 326 | } |
| 327 | // Return whether or not we skipped any characters. |
| 328 | return source_pos() != start_position; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | |
| 333 | Token::Value Scanner::SkipSingleLineComment() { |
| 334 | Advance(); |
| 335 | |
| 336 | // The line terminator at the end of the line is not considered |
| 337 | // to be part of the single-line comment; it is recognized |
| 338 | // separately by the lexical grammar and becomes part of the |
| 339 | // stream of input elements for the syntactic grammar (see |
| 340 | // ECMA-262, section 7.4). |
| 341 | while (c0_ >= 0 && !unicode_cache_->IsLineTerminator(c0_)) { |
| 342 | Advance(); |
| 343 | } |
| 344 | |
| 345 | return Token::WHITESPACE; |
| 346 | } |
| 347 | |
| 348 | |
| 349 | Token::Value Scanner::SkipSourceURLComment() { |
| 350 | TryToParseSourceURLComment(); |
| 351 | while (c0_ >= 0 && !unicode_cache_->IsLineTerminator(c0_)) { |
| 352 | Advance(); |
| 353 | } |
| 354 | |
| 355 | return Token::WHITESPACE; |
| 356 | } |
| 357 | |
| 358 | |
| 359 | void Scanner::TryToParseSourceURLComment() { |
| 360 | // Magic comments are of the form: //[#@]\s<name>=\s*<value>\s*.* and this |
| 361 | // function will just return if it cannot parse a magic comment. |
| 362 | if (c0_ < 0 || !unicode_cache_->IsWhiteSpace(c0_)) return; |
| 363 | Advance(); |
| 364 | LiteralBuffer name; |
| 365 | while (c0_ >= 0 && !unicode_cache_->IsWhiteSpaceOrLineTerminator(c0_) && |
| 366 | c0_ != '=') { |
| 367 | name.AddChar(c0_); |
| 368 | Advance(); |
| 369 | } |
| 370 | if (!name.is_one_byte()) return; |
| 371 | Vector<const uint8_t> name_literal = name.one_byte_literal(); |
| 372 | LiteralBuffer* value; |
| 373 | if (name_literal == STATIC_CHAR_VECTOR("sourceURL")) { |
| 374 | value = &source_url_; |
| 375 | } else if (name_literal == STATIC_CHAR_VECTOR("sourceMappingURL")) { |
| 376 | value = &source_mapping_url_; |
| 377 | } else { |
| 378 | return; |
| 379 | } |
| 380 | if (c0_ != '=') |
| 381 | return; |
| 382 | Advance(); |
| 383 | value->Reset(); |
| 384 | while (c0_ >= 0 && unicode_cache_->IsWhiteSpace(c0_)) { |
| 385 | Advance(); |
| 386 | } |
| 387 | while (c0_ >= 0 && !unicode_cache_->IsLineTerminator(c0_)) { |
| 388 | // Disallowed characters. |
| 389 | if (c0_ == '"' || c0_ == '\'') { |
| 390 | value->Reset(); |
| 391 | return; |
| 392 | } |
| 393 | if (unicode_cache_->IsWhiteSpace(c0_)) { |
| 394 | break; |
| 395 | } |
| 396 | value->AddChar(c0_); |
| 397 | Advance(); |
| 398 | } |
| 399 | // Allow whitespace at the end. |
| 400 | while (c0_ >= 0 && !unicode_cache_->IsLineTerminator(c0_)) { |
| 401 | if (!unicode_cache_->IsWhiteSpace(c0_)) { |
| 402 | value->Reset(); |
| 403 | break; |
| 404 | } |
| 405 | Advance(); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | |
| 410 | Token::Value Scanner::SkipMultiLineComment() { |
| 411 | DCHECK(c0_ == '*'); |
| 412 | Advance(); |
| 413 | |
| 414 | while (c0_ >= 0) { |
| 415 | uc32 ch = c0_; |
| 416 | Advance(); |
| 417 | if (c0_ >= 0 && unicode_cache_->IsLineTerminator(ch)) { |
| 418 | // Following ECMA-262, section 7.4, a comment containing |
| 419 | // a newline will make the comment count as a line-terminator. |
| 420 | has_multiline_comment_before_next_ = true; |
| 421 | } |
| 422 | // If we have reached the end of the multi-line comment, we |
| 423 | // consume the '/' and insert a whitespace. This way all |
| 424 | // multi-line comments are treated as whitespace. |
| 425 | if (ch == '*' && c0_ == '/') { |
| 426 | c0_ = ' '; |
| 427 | return Token::WHITESPACE; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | // Unterminated multi-line comment. |
| 432 | return Token::ILLEGAL; |
| 433 | } |
| 434 | |
| 435 | |
| 436 | Token::Value Scanner::ScanHtmlComment() { |
| 437 | // Check for <!-- comments. |
| 438 | DCHECK(c0_ == '!'); |
| 439 | Advance(); |
| 440 | if (c0_ == '-') { |
| 441 | Advance(); |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 442 | if (c0_ == '-') { |
| 443 | found_html_comment_ = true; |
| 444 | return SkipSingleLineComment(); |
| 445 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 446 | PushBack('-'); // undo Advance() |
| 447 | } |
| 448 | PushBack('!'); // undo Advance() |
| 449 | DCHECK(c0_ == '!'); |
| 450 | return Token::LT; |
| 451 | } |
| 452 | |
| 453 | |
| 454 | void Scanner::Scan() { |
| 455 | next_.literal_chars = NULL; |
| 456 | next_.raw_literal_chars = NULL; |
| 457 | Token::Value token; |
| 458 | do { |
| 459 | // Remember the position of the next token |
| 460 | next_.location.beg_pos = source_pos(); |
| 461 | |
| 462 | switch (c0_) { |
| 463 | case ' ': |
| 464 | case '\t': |
| 465 | Advance(); |
| 466 | token = Token::WHITESPACE; |
| 467 | break; |
| 468 | |
| 469 | case '\n': |
| 470 | Advance(); |
| 471 | has_line_terminator_before_next_ = true; |
| 472 | token = Token::WHITESPACE; |
| 473 | break; |
| 474 | |
| 475 | case '"': case '\'': |
| 476 | token = ScanString(); |
| 477 | break; |
| 478 | |
| 479 | case '<': |
| 480 | // < <= << <<= <!-- |
| 481 | Advance(); |
| 482 | if (c0_ == '=') { |
| 483 | token = Select(Token::LTE); |
| 484 | } else if (c0_ == '<') { |
| 485 | token = Select('=', Token::ASSIGN_SHL, Token::SHL); |
| 486 | } else if (c0_ == '!') { |
| 487 | token = ScanHtmlComment(); |
| 488 | } else { |
| 489 | token = Token::LT; |
| 490 | } |
| 491 | break; |
| 492 | |
| 493 | case '>': |
| 494 | // > >= >> >>= >>> >>>= |
| 495 | Advance(); |
| 496 | if (c0_ == '=') { |
| 497 | token = Select(Token::GTE); |
| 498 | } else if (c0_ == '>') { |
| 499 | // >> >>= >>> >>>= |
| 500 | Advance(); |
| 501 | if (c0_ == '=') { |
| 502 | token = Select(Token::ASSIGN_SAR); |
| 503 | } else if (c0_ == '>') { |
| 504 | token = Select('=', Token::ASSIGN_SHR, Token::SHR); |
| 505 | } else { |
| 506 | token = Token::SAR; |
| 507 | } |
| 508 | } else { |
| 509 | token = Token::GT; |
| 510 | } |
| 511 | break; |
| 512 | |
| 513 | case '=': |
| 514 | // = == === => |
| 515 | Advance(); |
| 516 | if (c0_ == '=') { |
| 517 | token = Select('=', Token::EQ_STRICT, Token::EQ); |
| 518 | } else if (c0_ == '>') { |
| 519 | token = Select(Token::ARROW); |
| 520 | } else { |
| 521 | token = Token::ASSIGN; |
| 522 | } |
| 523 | break; |
| 524 | |
| 525 | case '!': |
| 526 | // ! != !== |
| 527 | Advance(); |
| 528 | if (c0_ == '=') { |
| 529 | token = Select('=', Token::NE_STRICT, Token::NE); |
| 530 | } else { |
| 531 | token = Token::NOT; |
| 532 | } |
| 533 | break; |
| 534 | |
| 535 | case '+': |
| 536 | // + ++ += |
| 537 | Advance(); |
| 538 | if (c0_ == '+') { |
| 539 | token = Select(Token::INC); |
| 540 | } else if (c0_ == '=') { |
| 541 | token = Select(Token::ASSIGN_ADD); |
| 542 | } else { |
| 543 | token = Token::ADD; |
| 544 | } |
| 545 | break; |
| 546 | |
| 547 | case '-': |
| 548 | // - -- --> -= |
| 549 | Advance(); |
| 550 | if (c0_ == '-') { |
| 551 | Advance(); |
| 552 | if (c0_ == '>' && has_line_terminator_before_next_) { |
| 553 | // For compatibility with SpiderMonkey, we skip lines that |
| 554 | // start with an HTML comment end '-->'. |
| 555 | token = SkipSingleLineComment(); |
| 556 | } else { |
| 557 | token = Token::DEC; |
| 558 | } |
| 559 | } else if (c0_ == '=') { |
| 560 | token = Select(Token::ASSIGN_SUB); |
| 561 | } else { |
| 562 | token = Token::SUB; |
| 563 | } |
| 564 | break; |
| 565 | |
| 566 | case '*': |
| 567 | // * *= |
| 568 | token = Select('=', Token::ASSIGN_MUL, Token::MUL); |
| 569 | break; |
| 570 | |
| 571 | case '%': |
| 572 | // % %= |
| 573 | token = Select('=', Token::ASSIGN_MOD, Token::MOD); |
| 574 | break; |
| 575 | |
| 576 | case '/': |
| 577 | // / // /* /= |
| 578 | Advance(); |
| 579 | if (c0_ == '/') { |
| 580 | Advance(); |
| 581 | if (c0_ == '#' || c0_ == '@') { |
| 582 | Advance(); |
| 583 | token = SkipSourceURLComment(); |
| 584 | } else { |
| 585 | PushBack(c0_); |
| 586 | token = SkipSingleLineComment(); |
| 587 | } |
| 588 | } else if (c0_ == '*') { |
| 589 | token = SkipMultiLineComment(); |
| 590 | } else if (c0_ == '=') { |
| 591 | token = Select(Token::ASSIGN_DIV); |
| 592 | } else { |
| 593 | token = Token::DIV; |
| 594 | } |
| 595 | break; |
| 596 | |
| 597 | case '&': |
| 598 | // & && &= |
| 599 | Advance(); |
| 600 | if (c0_ == '&') { |
| 601 | token = Select(Token::AND); |
| 602 | } else if (c0_ == '=') { |
| 603 | token = Select(Token::ASSIGN_BIT_AND); |
| 604 | } else { |
| 605 | token = Token::BIT_AND; |
| 606 | } |
| 607 | break; |
| 608 | |
| 609 | case '|': |
| 610 | // | || |= |
| 611 | Advance(); |
| 612 | if (c0_ == '|') { |
| 613 | token = Select(Token::OR); |
| 614 | } else if (c0_ == '=') { |
| 615 | token = Select(Token::ASSIGN_BIT_OR); |
| 616 | } else { |
| 617 | token = Token::BIT_OR; |
| 618 | } |
| 619 | break; |
| 620 | |
| 621 | case '^': |
| 622 | // ^ ^= |
| 623 | token = Select('=', Token::ASSIGN_BIT_XOR, Token::BIT_XOR); |
| 624 | break; |
| 625 | |
| 626 | case '.': |
| 627 | // . Number |
| 628 | Advance(); |
| 629 | if (IsDecimalDigit(c0_)) { |
| 630 | token = ScanNumber(true); |
| 631 | } else { |
| 632 | token = Token::PERIOD; |
| 633 | if (c0_ == '.') { |
| 634 | Advance(); |
| 635 | if (c0_ == '.') { |
| 636 | Advance(); |
| 637 | token = Token::ELLIPSIS; |
| 638 | } else { |
| 639 | PushBack('.'); |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | break; |
| 644 | |
| 645 | case ':': |
| 646 | token = Select(Token::COLON); |
| 647 | break; |
| 648 | |
| 649 | case ';': |
| 650 | token = Select(Token::SEMICOLON); |
| 651 | break; |
| 652 | |
| 653 | case ',': |
| 654 | token = Select(Token::COMMA); |
| 655 | break; |
| 656 | |
| 657 | case '(': |
| 658 | token = Select(Token::LPAREN); |
| 659 | break; |
| 660 | |
| 661 | case ')': |
| 662 | token = Select(Token::RPAREN); |
| 663 | break; |
| 664 | |
| 665 | case '[': |
| 666 | token = Select(Token::LBRACK); |
| 667 | break; |
| 668 | |
| 669 | case ']': |
| 670 | token = Select(Token::RBRACK); |
| 671 | break; |
| 672 | |
| 673 | case '{': |
| 674 | token = Select(Token::LBRACE); |
| 675 | break; |
| 676 | |
| 677 | case '}': |
| 678 | token = Select(Token::RBRACE); |
| 679 | break; |
| 680 | |
| 681 | case '?': |
| 682 | token = Select(Token::CONDITIONAL); |
| 683 | break; |
| 684 | |
| 685 | case '~': |
| 686 | token = Select(Token::BIT_NOT); |
| 687 | break; |
| 688 | |
| 689 | case '`': |
| 690 | token = ScanTemplateStart(); |
| 691 | break; |
| 692 | |
| 693 | default: |
| 694 | if (c0_ < 0) { |
| 695 | token = Token::EOS; |
| 696 | } else if (unicode_cache_->IsIdentifierStart(c0_)) { |
| 697 | token = ScanIdentifierOrKeyword(); |
| 698 | } else if (IsDecimalDigit(c0_)) { |
| 699 | token = ScanNumber(false); |
| 700 | } else if (SkipWhiteSpace()) { |
| 701 | token = Token::WHITESPACE; |
| 702 | } else { |
| 703 | token = Select(Token::ILLEGAL); |
| 704 | } |
| 705 | break; |
| 706 | } |
| 707 | |
| 708 | // Continue scanning for tokens as long as we're just skipping |
| 709 | // whitespace. |
| 710 | } while (token == Token::WHITESPACE); |
| 711 | |
| 712 | next_.location.end_pos = source_pos(); |
| 713 | next_.token = token; |
| 714 | } |
| 715 | |
| 716 | |
| 717 | void Scanner::SeekForward(int pos) { |
| 718 | // After this call, we will have the token at the given position as |
| 719 | // the "next" token. The "current" token will be invalid. |
| 720 | if (pos == next_.location.beg_pos) return; |
| 721 | int current_pos = source_pos(); |
| 722 | DCHECK_EQ(next_.location.end_pos, current_pos); |
| 723 | // Positions inside the lookahead token aren't supported. |
| 724 | DCHECK(pos >= current_pos); |
| 725 | if (pos != current_pos) { |
| 726 | source_->SeekForward(pos - source_->pos()); |
| 727 | Advance(); |
| 728 | // This function is only called to seek to the location |
| 729 | // of the end of a function (at the "}" token). It doesn't matter |
| 730 | // whether there was a line terminator in the part we skip. |
| 731 | has_line_terminator_before_next_ = false; |
| 732 | has_multiline_comment_before_next_ = false; |
| 733 | } |
| 734 | Scan(); |
| 735 | } |
| 736 | |
| 737 | |
| 738 | template <bool capture_raw, bool in_template_literal> |
| 739 | bool Scanner::ScanEscape() { |
| 740 | uc32 c = c0_; |
| 741 | Advance<capture_raw>(); |
| 742 | |
| 743 | // Skip escaped newlines. |
| 744 | if (!in_template_literal && c0_ >= 0 && unicode_cache_->IsLineTerminator(c)) { |
| 745 | // Allow CR+LF newlines in multiline string literals. |
| 746 | if (IsCarriageReturn(c) && IsLineFeed(c0_)) Advance<capture_raw>(); |
| 747 | // Allow LF+CR newlines in multiline string literals. |
| 748 | if (IsLineFeed(c) && IsCarriageReturn(c0_)) Advance<capture_raw>(); |
| 749 | return true; |
| 750 | } |
| 751 | |
| 752 | switch (c) { |
| 753 | case '\'': // fall through |
| 754 | case '"' : // fall through |
| 755 | case '\\': break; |
| 756 | case 'b' : c = '\b'; break; |
| 757 | case 'f' : c = '\f'; break; |
| 758 | case 'n' : c = '\n'; break; |
| 759 | case 'r' : c = '\r'; break; |
| 760 | case 't' : c = '\t'; break; |
| 761 | case 'u' : { |
| 762 | c = ScanUnicodeEscape<capture_raw>(); |
| 763 | if (c < 0) return false; |
| 764 | break; |
| 765 | } |
| 766 | case 'v': |
| 767 | c = '\v'; |
| 768 | break; |
| 769 | case 'x': { |
| 770 | c = ScanHexNumber<capture_raw>(2); |
| 771 | if (c < 0) return false; |
| 772 | break; |
| 773 | } |
| 774 | case '0': // Fall through. |
| 775 | case '1': // fall through |
| 776 | case '2': // fall through |
| 777 | case '3': // fall through |
| 778 | case '4': // fall through |
| 779 | case '5': // fall through |
| 780 | case '6': // fall through |
| 781 | case '7': |
| 782 | c = ScanOctalEscape<capture_raw>(c, 2); |
| 783 | break; |
| 784 | } |
| 785 | |
| 786 | // According to ECMA-262, section 7.8.4, characters not covered by the |
| 787 | // above cases should be illegal, but they are commonly handled as |
| 788 | // non-escaped characters by JS VMs. |
| 789 | AddLiteralChar(c); |
| 790 | return true; |
| 791 | } |
| 792 | |
| 793 | |
| 794 | // Octal escapes of the forms '\0xx' and '\xxx' are not a part of |
| 795 | // ECMA-262. Other JS VMs support them. |
| 796 | template <bool capture_raw> |
| 797 | uc32 Scanner::ScanOctalEscape(uc32 c, int length) { |
| 798 | uc32 x = c - '0'; |
| 799 | int i = 0; |
| 800 | for (; i < length; i++) { |
| 801 | int d = c0_ - '0'; |
| 802 | if (d < 0 || d > 7) break; |
| 803 | int nx = x * 8 + d; |
| 804 | if (nx >= 256) break; |
| 805 | x = nx; |
| 806 | Advance<capture_raw>(); |
| 807 | } |
| 808 | // Anything except '\0' is an octal escape sequence, illegal in strict mode. |
| 809 | // Remember the position of octal escape sequences so that an error |
| 810 | // can be reported later (in strict mode). |
| 811 | // We don't report the error immediately, because the octal escape can |
| 812 | // occur before the "use strict" directive. |
| 813 | if (c != '0' || i > 0) { |
| 814 | octal_pos_ = Location(source_pos() - i - 1, source_pos() - 1); |
| 815 | } |
| 816 | return x; |
| 817 | } |
| 818 | |
| 819 | |
| 820 | const int kMaxAscii = 127; |
| 821 | |
| 822 | |
| 823 | Token::Value Scanner::ScanString() { |
| 824 | uc32 quote = c0_; |
| 825 | Advance<false, false>(); // consume quote |
| 826 | |
| 827 | LiteralScope literal(this); |
| 828 | while (true) { |
| 829 | if (c0_ > kMaxAscii) { |
| 830 | HandleLeadSurrogate(); |
| 831 | break; |
| 832 | } |
| 833 | if (c0_ < 0 || c0_ == '\n' || c0_ == '\r') return Token::ILLEGAL; |
| 834 | if (c0_ == quote) { |
| 835 | literal.Complete(); |
| 836 | Advance<false, false>(); |
| 837 | return Token::STRING; |
| 838 | } |
| 839 | uc32 c = c0_; |
| 840 | if (c == '\\') break; |
| 841 | Advance<false, false>(); |
| 842 | AddLiteralChar(c); |
| 843 | } |
| 844 | |
| 845 | while (c0_ != quote && c0_ >= 0 |
| 846 | && !unicode_cache_->IsLineTerminator(c0_)) { |
| 847 | uc32 c = c0_; |
| 848 | Advance(); |
| 849 | if (c == '\\') { |
| 850 | if (c0_ < 0 || !ScanEscape<false, false>()) return Token::ILLEGAL; |
| 851 | } else { |
| 852 | AddLiteralChar(c); |
| 853 | } |
| 854 | } |
| 855 | if (c0_ != quote) return Token::ILLEGAL; |
| 856 | literal.Complete(); |
| 857 | |
| 858 | Advance(); // consume quote |
| 859 | return Token::STRING; |
| 860 | } |
| 861 | |
| 862 | |
| 863 | Token::Value Scanner::ScanTemplateSpan() { |
| 864 | // When scanning a TemplateSpan, we are looking for the following construct: |
| 865 | // TEMPLATE_SPAN :: |
| 866 | // ` LiteralChars* ${ |
| 867 | // | } LiteralChars* ${ |
| 868 | // |
| 869 | // TEMPLATE_TAIL :: |
| 870 | // ` LiteralChars* ` |
| 871 | // | } LiteralChar* ` |
| 872 | // |
| 873 | // A TEMPLATE_SPAN should always be followed by an Expression, while a |
| 874 | // TEMPLATE_TAIL terminates a TemplateLiteral and does not need to be |
| 875 | // followed by an Expression. |
| 876 | |
| 877 | Token::Value result = Token::TEMPLATE_SPAN; |
| 878 | LiteralScope literal(this); |
| 879 | StartRawLiteral(); |
| 880 | const bool capture_raw = true; |
| 881 | const bool in_template_literal = true; |
| 882 | |
| 883 | while (true) { |
| 884 | uc32 c = c0_; |
| 885 | Advance<capture_raw>(); |
| 886 | if (c == '`') { |
| 887 | result = Token::TEMPLATE_TAIL; |
| 888 | ReduceRawLiteralLength(1); |
| 889 | break; |
| 890 | } else if (c == '$' && c0_ == '{') { |
| 891 | Advance<capture_raw>(); // Consume '{' |
| 892 | ReduceRawLiteralLength(2); |
| 893 | break; |
| 894 | } else if (c == '\\') { |
| 895 | if (c0_ > 0 && unicode_cache_->IsLineTerminator(c0_)) { |
| 896 | // The TV of LineContinuation :: \ LineTerminatorSequence is the empty |
| 897 | // code unit sequence. |
| 898 | uc32 lastChar = c0_; |
| 899 | Advance<capture_raw>(); |
| 900 | if (lastChar == '\r') { |
| 901 | ReduceRawLiteralLength(1); // Remove \r |
| 902 | if (c0_ == '\n') { |
| 903 | Advance<capture_raw>(); // Adds \n |
| 904 | } else { |
| 905 | AddRawLiteralChar('\n'); |
| 906 | } |
| 907 | } |
| 908 | } else if (!ScanEscape<capture_raw, in_template_literal>()) { |
| 909 | return Token::ILLEGAL; |
| 910 | } |
| 911 | } else if (c < 0) { |
| 912 | // Unterminated template literal |
| 913 | PushBack(c); |
| 914 | break; |
| 915 | } else { |
| 916 | // The TRV of LineTerminatorSequence :: <CR> is the CV 0x000A. |
| 917 | // The TRV of LineTerminatorSequence :: <CR><LF> is the sequence |
| 918 | // consisting of the CV 0x000A. |
| 919 | if (c == '\r') { |
| 920 | ReduceRawLiteralLength(1); // Remove \r |
| 921 | if (c0_ == '\n') { |
| 922 | Advance<capture_raw>(); // Adds \n |
| 923 | } else { |
| 924 | AddRawLiteralChar('\n'); |
| 925 | } |
| 926 | c = '\n'; |
| 927 | } |
| 928 | AddLiteralChar(c); |
| 929 | } |
| 930 | } |
| 931 | literal.Complete(); |
| 932 | next_.location.end_pos = source_pos(); |
| 933 | next_.token = result; |
| 934 | return result; |
| 935 | } |
| 936 | |
| 937 | |
| 938 | Token::Value Scanner::ScanTemplateStart() { |
| 939 | DCHECK(c0_ == '`'); |
| 940 | next_.location.beg_pos = source_pos(); |
| 941 | Advance(); // Consume ` |
| 942 | return ScanTemplateSpan(); |
| 943 | } |
| 944 | |
| 945 | |
| 946 | Token::Value Scanner::ScanTemplateContinuation() { |
| 947 | DCHECK_EQ(next_.token, Token::RBRACE); |
| 948 | next_.location.beg_pos = source_pos() - 1; // We already consumed } |
| 949 | return ScanTemplateSpan(); |
| 950 | } |
| 951 | |
| 952 | |
| 953 | void Scanner::ScanDecimalDigits() { |
| 954 | while (IsDecimalDigit(c0_)) |
| 955 | AddLiteralCharAdvance(); |
| 956 | } |
| 957 | |
| 958 | |
| 959 | Token::Value Scanner::ScanNumber(bool seen_period) { |
| 960 | DCHECK(IsDecimalDigit(c0_)); // the first digit of the number or the fraction |
| 961 | |
| 962 | enum { DECIMAL, HEX, OCTAL, IMPLICIT_OCTAL, BINARY } kind = DECIMAL; |
| 963 | |
| 964 | LiteralScope literal(this); |
| 965 | bool at_start = !seen_period; |
| 966 | if (seen_period) { |
| 967 | // we have already seen a decimal point of the float |
| 968 | AddLiteralChar('.'); |
| 969 | ScanDecimalDigits(); // we know we have at least one digit |
| 970 | |
| 971 | } else { |
| 972 | // if the first character is '0' we must check for octals and hex |
| 973 | if (c0_ == '0') { |
| 974 | int start_pos = source_pos(); // For reporting octal positions. |
| 975 | AddLiteralCharAdvance(); |
| 976 | |
| 977 | // either 0, 0exxx, 0Exxx, 0.xxx, a hex number, a binary number or |
| 978 | // an octal number. |
| 979 | if (c0_ == 'x' || c0_ == 'X') { |
| 980 | // hex number |
| 981 | kind = HEX; |
| 982 | AddLiteralCharAdvance(); |
| 983 | if (!IsHexDigit(c0_)) { |
| 984 | // we must have at least one hex digit after 'x'/'X' |
| 985 | return Token::ILLEGAL; |
| 986 | } |
| 987 | while (IsHexDigit(c0_)) { |
| 988 | AddLiteralCharAdvance(); |
| 989 | } |
| 990 | } else if (c0_ == 'o' || c0_ == 'O') { |
| 991 | kind = OCTAL; |
| 992 | AddLiteralCharAdvance(); |
| 993 | if (!IsOctalDigit(c0_)) { |
| 994 | // we must have at least one octal digit after 'o'/'O' |
| 995 | return Token::ILLEGAL; |
| 996 | } |
| 997 | while (IsOctalDigit(c0_)) { |
| 998 | AddLiteralCharAdvance(); |
| 999 | } |
| 1000 | } else if (c0_ == 'b' || c0_ == 'B') { |
| 1001 | kind = BINARY; |
| 1002 | AddLiteralCharAdvance(); |
| 1003 | if (!IsBinaryDigit(c0_)) { |
| 1004 | // we must have at least one binary digit after 'b'/'B' |
| 1005 | return Token::ILLEGAL; |
| 1006 | } |
| 1007 | while (IsBinaryDigit(c0_)) { |
| 1008 | AddLiteralCharAdvance(); |
| 1009 | } |
| 1010 | } else if ('0' <= c0_ && c0_ <= '7') { |
| 1011 | // (possible) octal number |
| 1012 | kind = IMPLICIT_OCTAL; |
| 1013 | while (true) { |
| 1014 | if (c0_ == '8' || c0_ == '9') { |
| 1015 | at_start = false; |
| 1016 | kind = DECIMAL; |
| 1017 | break; |
| 1018 | } |
| 1019 | if (c0_ < '0' || '7' < c0_) { |
| 1020 | // Octal literal finished. |
| 1021 | octal_pos_ = Location(start_pos, source_pos()); |
| 1022 | break; |
| 1023 | } |
| 1024 | AddLiteralCharAdvance(); |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | // Parse decimal digits and allow trailing fractional part. |
| 1030 | if (kind == DECIMAL) { |
| 1031 | if (at_start) { |
| 1032 | uint64_t value = 0; |
| 1033 | while (IsDecimalDigit(c0_)) { |
| 1034 | value = 10 * value + (c0_ - '0'); |
| 1035 | |
| 1036 | uc32 first_char = c0_; |
| 1037 | Advance<false, false>(); |
| 1038 | AddLiteralChar(first_char); |
| 1039 | } |
| 1040 | |
| 1041 | if (next_.literal_chars->one_byte_literal().length() <= 10 && |
| 1042 | value <= Smi::kMaxValue && c0_ != '.' && c0_ != 'e' && c0_ != 'E') { |
| 1043 | next_.smi_value_ = static_cast<int>(value); |
| 1044 | literal.Complete(); |
| 1045 | HandleLeadSurrogate(); |
| 1046 | |
| 1047 | return Token::SMI; |
| 1048 | } |
| 1049 | HandleLeadSurrogate(); |
| 1050 | } |
| 1051 | |
| 1052 | ScanDecimalDigits(); // optional |
| 1053 | if (c0_ == '.') { |
| 1054 | AddLiteralCharAdvance(); |
| 1055 | ScanDecimalDigits(); // optional |
| 1056 | } |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | // scan exponent, if any |
| 1061 | if (c0_ == 'e' || c0_ == 'E') { |
| 1062 | DCHECK(kind != HEX); // 'e'/'E' must be scanned as part of the hex number |
| 1063 | if (kind != DECIMAL) return Token::ILLEGAL; |
| 1064 | // scan exponent |
| 1065 | AddLiteralCharAdvance(); |
| 1066 | if (c0_ == '+' || c0_ == '-') |
| 1067 | AddLiteralCharAdvance(); |
| 1068 | if (!IsDecimalDigit(c0_)) { |
| 1069 | // we must have at least one decimal digit after 'e'/'E' |
| 1070 | return Token::ILLEGAL; |
| 1071 | } |
| 1072 | ScanDecimalDigits(); |
| 1073 | } |
| 1074 | |
| 1075 | // The source character immediately following a numeric literal must |
| 1076 | // not be an identifier start or a decimal digit; see ECMA-262 |
| 1077 | // section 7.8.3, page 17 (note that we read only one decimal digit |
| 1078 | // if the value is 0). |
| 1079 | if (IsDecimalDigit(c0_) || |
| 1080 | (c0_ >= 0 && unicode_cache_->IsIdentifierStart(c0_))) |
| 1081 | return Token::ILLEGAL; |
| 1082 | |
| 1083 | literal.Complete(); |
| 1084 | |
| 1085 | return Token::NUMBER; |
| 1086 | } |
| 1087 | |
| 1088 | |
| 1089 | uc32 Scanner::ScanIdentifierUnicodeEscape() { |
| 1090 | Advance(); |
| 1091 | if (c0_ != 'u') return -1; |
| 1092 | Advance(); |
| 1093 | return ScanUnicodeEscape<false>(); |
| 1094 | } |
| 1095 | |
| 1096 | |
| 1097 | template <bool capture_raw> |
| 1098 | uc32 Scanner::ScanUnicodeEscape() { |
| 1099 | // Accept both \uxxxx and \u{xxxxxx}. In the latter case, the number of |
| 1100 | // hex digits between { } is arbitrary. \ and u have already been read. |
| 1101 | if (c0_ == '{') { |
| 1102 | Advance<capture_raw>(); |
| 1103 | uc32 cp = ScanUnlimitedLengthHexNumber<capture_raw>(0x10ffff); |
| 1104 | if (cp < 0) { |
| 1105 | return -1; |
| 1106 | } |
| 1107 | if (c0_ != '}') { |
| 1108 | return -1; |
| 1109 | } |
| 1110 | Advance<capture_raw>(); |
| 1111 | return cp; |
| 1112 | } |
| 1113 | return ScanHexNumber<capture_raw>(4); |
| 1114 | } |
| 1115 | |
| 1116 | |
| 1117 | // ---------------------------------------------------------------------------- |
| 1118 | // Keyword Matcher |
| 1119 | |
| 1120 | #define KEYWORDS(KEYWORD_GROUP, KEYWORD) \ |
| 1121 | KEYWORD_GROUP('b') \ |
| 1122 | KEYWORD("break", Token::BREAK) \ |
| 1123 | KEYWORD_GROUP('c') \ |
| 1124 | KEYWORD("case", Token::CASE) \ |
| 1125 | KEYWORD("catch", Token::CATCH) \ |
| 1126 | KEYWORD("class", Token::CLASS) \ |
| 1127 | KEYWORD("const", Token::CONST) \ |
| 1128 | KEYWORD("continue", Token::CONTINUE) \ |
| 1129 | KEYWORD_GROUP('d') \ |
| 1130 | KEYWORD("debugger", Token::DEBUGGER) \ |
| 1131 | KEYWORD("default", Token::DEFAULT) \ |
| 1132 | KEYWORD("delete", Token::DELETE) \ |
| 1133 | KEYWORD("do", Token::DO) \ |
| 1134 | KEYWORD_GROUP('e') \ |
| 1135 | KEYWORD("else", Token::ELSE) \ |
| 1136 | KEYWORD("enum", Token::FUTURE_RESERVED_WORD) \ |
| 1137 | KEYWORD("export", Token::EXPORT) \ |
| 1138 | KEYWORD("extends", Token::EXTENDS) \ |
| 1139 | KEYWORD_GROUP('f') \ |
| 1140 | KEYWORD("false", Token::FALSE_LITERAL) \ |
| 1141 | KEYWORD("finally", Token::FINALLY) \ |
| 1142 | KEYWORD("for", Token::FOR) \ |
| 1143 | KEYWORD("function", Token::FUNCTION) \ |
| 1144 | KEYWORD_GROUP('i') \ |
| 1145 | KEYWORD("if", Token::IF) \ |
| 1146 | KEYWORD("implements", Token::FUTURE_STRICT_RESERVED_WORD) \ |
| 1147 | KEYWORD("import", Token::IMPORT) \ |
| 1148 | KEYWORD("in", Token::IN) \ |
| 1149 | KEYWORD("instanceof", Token::INSTANCEOF) \ |
| 1150 | KEYWORD("interface", Token::FUTURE_STRICT_RESERVED_WORD) \ |
| 1151 | KEYWORD_GROUP('l') \ |
| 1152 | KEYWORD("let", Token::LET) \ |
| 1153 | KEYWORD_GROUP('n') \ |
| 1154 | KEYWORD("new", Token::NEW) \ |
| 1155 | KEYWORD("null", Token::NULL_LITERAL) \ |
| 1156 | KEYWORD_GROUP('p') \ |
| 1157 | KEYWORD("package", Token::FUTURE_STRICT_RESERVED_WORD) \ |
| 1158 | KEYWORD("private", Token::FUTURE_STRICT_RESERVED_WORD) \ |
| 1159 | KEYWORD("protected", Token::FUTURE_STRICT_RESERVED_WORD) \ |
| 1160 | KEYWORD("public", Token::FUTURE_STRICT_RESERVED_WORD) \ |
| 1161 | KEYWORD_GROUP('r') \ |
| 1162 | KEYWORD("return", Token::RETURN) \ |
| 1163 | KEYWORD_GROUP('s') \ |
| 1164 | KEYWORD("static", Token::STATIC) \ |
| 1165 | KEYWORD("super", Token::SUPER) \ |
| 1166 | KEYWORD("switch", Token::SWITCH) \ |
| 1167 | KEYWORD_GROUP('t') \ |
| 1168 | KEYWORD("this", Token::THIS) \ |
| 1169 | KEYWORD("throw", Token::THROW) \ |
| 1170 | KEYWORD("true", Token::TRUE_LITERAL) \ |
| 1171 | KEYWORD("try", Token::TRY) \ |
| 1172 | KEYWORD("typeof", Token::TYPEOF) \ |
| 1173 | KEYWORD_GROUP('v') \ |
| 1174 | KEYWORD("var", Token::VAR) \ |
| 1175 | KEYWORD("void", Token::VOID) \ |
| 1176 | KEYWORD_GROUP('w') \ |
| 1177 | KEYWORD("while", Token::WHILE) \ |
| 1178 | KEYWORD("with", Token::WITH) \ |
| 1179 | KEYWORD_GROUP('y') \ |
| 1180 | KEYWORD("yield", Token::YIELD) |
| 1181 | |
| 1182 | |
| 1183 | static Token::Value KeywordOrIdentifierToken(const uint8_t* input, |
| 1184 | int input_length, bool escaped) { |
| 1185 | DCHECK(input_length >= 1); |
| 1186 | const int kMinLength = 2; |
| 1187 | const int kMaxLength = 10; |
| 1188 | if (input_length < kMinLength || input_length > kMaxLength) { |
| 1189 | return Token::IDENTIFIER; |
| 1190 | } |
| 1191 | switch (input[0]) { |
| 1192 | default: |
| 1193 | #define KEYWORD_GROUP_CASE(ch) \ |
| 1194 | break; \ |
| 1195 | case ch: |
| 1196 | #define KEYWORD(keyword, token) \ |
| 1197 | { \ |
| 1198 | /* 'keyword' is a char array, so sizeof(keyword) is */ \ |
| 1199 | /* strlen(keyword) plus 1 for the NUL char. */ \ |
| 1200 | const int keyword_length = sizeof(keyword) - 1; \ |
| 1201 | STATIC_ASSERT(keyword_length >= kMinLength); \ |
| 1202 | STATIC_ASSERT(keyword_length <= kMaxLength); \ |
| 1203 | if (input_length == keyword_length && input[1] == keyword[1] && \ |
| 1204 | (keyword_length <= 2 || input[2] == keyword[2]) && \ |
| 1205 | (keyword_length <= 3 || input[3] == keyword[3]) && \ |
| 1206 | (keyword_length <= 4 || input[4] == keyword[4]) && \ |
| 1207 | (keyword_length <= 5 || input[5] == keyword[5]) && \ |
| 1208 | (keyword_length <= 6 || input[6] == keyword[6]) && \ |
| 1209 | (keyword_length <= 7 || input[7] == keyword[7]) && \ |
| 1210 | (keyword_length <= 8 || input[8] == keyword[8]) && \ |
| 1211 | (keyword_length <= 9 || input[9] == keyword[9])) { \ |
| 1212 | if (escaped) { \ |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1213 | /* TODO(adamk): YIELD should be handled specially. */ \ |
| 1214 | return (token == Token::FUTURE_STRICT_RESERVED_WORD || \ |
| 1215 | token == Token::LET || token == Token::STATIC) \ |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1216 | ? Token::ESCAPED_STRICT_RESERVED_WORD \ |
| 1217 | : Token::ESCAPED_KEYWORD; \ |
| 1218 | } \ |
| 1219 | return token; \ |
| 1220 | } \ |
| 1221 | } |
| 1222 | KEYWORDS(KEYWORD_GROUP_CASE, KEYWORD) |
| 1223 | } |
| 1224 | return Token::IDENTIFIER; |
| 1225 | } |
| 1226 | |
| 1227 | |
| 1228 | bool Scanner::IdentifierIsFutureStrictReserved( |
| 1229 | const AstRawString* string) const { |
| 1230 | // Keywords are always 1-byte strings. |
| 1231 | if (!string->is_one_byte()) return false; |
| 1232 | if (string->IsOneByteEqualTo("let") || string->IsOneByteEqualTo("static") || |
| 1233 | string->IsOneByteEqualTo("yield")) { |
| 1234 | return true; |
| 1235 | } |
| 1236 | return Token::FUTURE_STRICT_RESERVED_WORD == |
| 1237 | KeywordOrIdentifierToken(string->raw_data(), string->length(), false); |
| 1238 | } |
| 1239 | |
| 1240 | |
| 1241 | Token::Value Scanner::ScanIdentifierOrKeyword() { |
| 1242 | DCHECK(unicode_cache_->IsIdentifierStart(c0_)); |
| 1243 | LiteralScope literal(this); |
| 1244 | if (IsInRange(c0_, 'a', 'z')) { |
| 1245 | do { |
| 1246 | uc32 first_char = c0_; |
| 1247 | Advance<false, false>(); |
| 1248 | AddLiteralChar(first_char); |
| 1249 | } while (IsInRange(c0_, 'a', 'z')); |
| 1250 | |
| 1251 | if (IsDecimalDigit(c0_) || IsInRange(c0_, 'A', 'Z') || c0_ == '_' || |
| 1252 | c0_ == '$') { |
| 1253 | // Identifier starting with lowercase. |
| 1254 | uc32 first_char = c0_; |
| 1255 | Advance<false, false>(); |
| 1256 | AddLiteralChar(first_char); |
| 1257 | while (IsAsciiIdentifier(c0_)) { |
| 1258 | uc32 first_char = c0_; |
| 1259 | Advance<false, false>(); |
| 1260 | AddLiteralChar(first_char); |
| 1261 | } |
| 1262 | if (c0_ <= kMaxAscii && c0_ != '\\') { |
| 1263 | literal.Complete(); |
| 1264 | return Token::IDENTIFIER; |
| 1265 | } |
| 1266 | } else if (c0_ <= kMaxAscii && c0_ != '\\') { |
| 1267 | // Only a-z+: could be a keyword or identifier. |
| 1268 | literal.Complete(); |
| 1269 | Vector<const uint8_t> chars = next_.literal_chars->one_byte_literal(); |
| 1270 | return KeywordOrIdentifierToken(chars.start(), chars.length(), false); |
| 1271 | } |
| 1272 | |
| 1273 | HandleLeadSurrogate(); |
| 1274 | } else if (IsInRange(c0_, 'A', 'Z') || c0_ == '_' || c0_ == '$') { |
| 1275 | do { |
| 1276 | uc32 first_char = c0_; |
| 1277 | Advance<false, false>(); |
| 1278 | AddLiteralChar(first_char); |
| 1279 | } while (IsAsciiIdentifier(c0_)); |
| 1280 | |
| 1281 | if (c0_ <= kMaxAscii && c0_ != '\\') { |
| 1282 | literal.Complete(); |
| 1283 | return Token::IDENTIFIER; |
| 1284 | } |
| 1285 | |
| 1286 | HandleLeadSurrogate(); |
| 1287 | } else if (c0_ == '\\') { |
| 1288 | // Scan identifier start character. |
| 1289 | uc32 c = ScanIdentifierUnicodeEscape(); |
| 1290 | // Only allow legal identifier start characters. |
| 1291 | if (c < 0 || |
| 1292 | c == '\\' || // No recursive escapes. |
| 1293 | !unicode_cache_->IsIdentifierStart(c)) { |
| 1294 | return Token::ILLEGAL; |
| 1295 | } |
| 1296 | AddLiteralChar(c); |
| 1297 | return ScanIdentifierSuffix(&literal, true); |
| 1298 | } else { |
| 1299 | uc32 first_char = c0_; |
| 1300 | Advance(); |
| 1301 | AddLiteralChar(first_char); |
| 1302 | } |
| 1303 | |
| 1304 | // Scan the rest of the identifier characters. |
| 1305 | while (c0_ >= 0 && unicode_cache_->IsIdentifierPart(c0_)) { |
| 1306 | if (c0_ != '\\') { |
| 1307 | uc32 next_char = c0_; |
| 1308 | Advance(); |
| 1309 | AddLiteralChar(next_char); |
| 1310 | continue; |
| 1311 | } |
| 1312 | // Fallthrough if no longer able to complete keyword. |
| 1313 | return ScanIdentifierSuffix(&literal, false); |
| 1314 | } |
| 1315 | |
| 1316 | literal.Complete(); |
| 1317 | |
| 1318 | if (next_.literal_chars->is_one_byte()) { |
| 1319 | Vector<const uint8_t> chars = next_.literal_chars->one_byte_literal(); |
| 1320 | return KeywordOrIdentifierToken(chars.start(), chars.length(), false); |
| 1321 | } |
| 1322 | return Token::IDENTIFIER; |
| 1323 | } |
| 1324 | |
| 1325 | |
| 1326 | Token::Value Scanner::ScanIdentifierSuffix(LiteralScope* literal, |
| 1327 | bool escaped) { |
| 1328 | // Scan the rest of the identifier characters. |
| 1329 | while (c0_ >= 0 && unicode_cache_->IsIdentifierPart(c0_)) { |
| 1330 | if (c0_ == '\\') { |
| 1331 | uc32 c = ScanIdentifierUnicodeEscape(); |
| 1332 | escaped = true; |
| 1333 | // Only allow legal identifier part characters. |
| 1334 | if (c < 0 || |
| 1335 | c == '\\' || |
| 1336 | !unicode_cache_->IsIdentifierPart(c)) { |
| 1337 | return Token::ILLEGAL; |
| 1338 | } |
| 1339 | AddLiteralChar(c); |
| 1340 | } else { |
| 1341 | AddLiteralChar(c0_); |
| 1342 | Advance(); |
| 1343 | } |
| 1344 | } |
| 1345 | literal->Complete(); |
| 1346 | |
| 1347 | if (escaped && next_.literal_chars->is_one_byte()) { |
| 1348 | Vector<const uint8_t> chars = next_.literal_chars->one_byte_literal(); |
| 1349 | return KeywordOrIdentifierToken(chars.start(), chars.length(), true); |
| 1350 | } |
| 1351 | return Token::IDENTIFIER; |
| 1352 | } |
| 1353 | |
| 1354 | |
| 1355 | bool Scanner::ScanRegExpPattern(bool seen_equal) { |
| 1356 | // Scan: ('/' | '/=') RegularExpressionBody '/' RegularExpressionFlags |
| 1357 | bool in_character_class = false; |
| 1358 | |
| 1359 | // Previous token is either '/' or '/=', in the second case, the |
| 1360 | // pattern starts at =. |
| 1361 | next_.location.beg_pos = source_pos() - (seen_equal ? 2 : 1); |
| 1362 | next_.location.end_pos = source_pos() - (seen_equal ? 1 : 0); |
| 1363 | |
| 1364 | // Scan regular expression body: According to ECMA-262, 3rd, 7.8.5, |
| 1365 | // the scanner should pass uninterpreted bodies to the RegExp |
| 1366 | // constructor. |
| 1367 | LiteralScope literal(this); |
| 1368 | if (seen_equal) { |
| 1369 | AddLiteralChar('='); |
| 1370 | } |
| 1371 | |
| 1372 | while (c0_ != '/' || in_character_class) { |
| 1373 | if (c0_ < 0 || unicode_cache_->IsLineTerminator(c0_)) return false; |
| 1374 | if (c0_ == '\\') { // Escape sequence. |
| 1375 | AddLiteralCharAdvance(); |
| 1376 | if (c0_ < 0 || unicode_cache_->IsLineTerminator(c0_)) return false; |
| 1377 | AddLiteralCharAdvance(); |
| 1378 | // If the escape allows more characters, i.e., \x??, \u????, or \c?, |
| 1379 | // only "safe" characters are allowed (letters, digits, underscore), |
| 1380 | // otherwise the escape isn't valid and the invalid character has |
| 1381 | // its normal meaning. I.e., we can just continue scanning without |
| 1382 | // worrying whether the following characters are part of the escape |
| 1383 | // or not, since any '/', '\\' or '[' is guaranteed to not be part |
| 1384 | // of the escape sequence. |
| 1385 | |
| 1386 | // TODO(896): At some point, parse RegExps more throughly to capture |
| 1387 | // octal esacpes in strict mode. |
| 1388 | } else { // Unescaped character. |
| 1389 | if (c0_ == '[') in_character_class = true; |
| 1390 | if (c0_ == ']') in_character_class = false; |
| 1391 | AddLiteralCharAdvance(); |
| 1392 | } |
| 1393 | } |
| 1394 | Advance(); // consume '/' |
| 1395 | |
| 1396 | literal.Complete(); |
| 1397 | |
| 1398 | return true; |
| 1399 | } |
| 1400 | |
| 1401 | |
| 1402 | Maybe<RegExp::Flags> Scanner::ScanRegExpFlags() { |
| 1403 | // Scan regular expression flags. |
| 1404 | LiteralScope literal(this); |
| 1405 | int flags = 0; |
| 1406 | while (c0_ >= 0 && unicode_cache_->IsIdentifierPart(c0_)) { |
| 1407 | RegExp::Flags flag = RegExp::kNone; |
| 1408 | switch (c0_) { |
| 1409 | case 'g': |
| 1410 | flag = RegExp::kGlobal; |
| 1411 | break; |
| 1412 | case 'i': |
| 1413 | flag = RegExp::kIgnoreCase; |
| 1414 | break; |
| 1415 | case 'm': |
| 1416 | flag = RegExp::kMultiline; |
| 1417 | break; |
| 1418 | case 'u': |
| 1419 | if (!FLAG_harmony_unicode_regexps) return Nothing<RegExp::Flags>(); |
| 1420 | flag = RegExp::kUnicode; |
| 1421 | break; |
| 1422 | case 'y': |
| 1423 | if (!FLAG_harmony_regexps) return Nothing<RegExp::Flags>(); |
| 1424 | flag = RegExp::kSticky; |
| 1425 | break; |
| 1426 | default: |
| 1427 | return Nothing<RegExp::Flags>(); |
| 1428 | } |
| 1429 | if (flags & flag) return Nothing<RegExp::Flags>(); |
| 1430 | AddLiteralCharAdvance(); |
| 1431 | flags |= flag; |
| 1432 | } |
| 1433 | literal.Complete(); |
| 1434 | |
| 1435 | next_.location.end_pos = source_pos(); |
| 1436 | return Just(RegExp::Flags(flags)); |
| 1437 | } |
| 1438 | |
| 1439 | |
| 1440 | const AstRawString* Scanner::CurrentSymbol(AstValueFactory* ast_value_factory) { |
| 1441 | if (is_literal_one_byte()) { |
| 1442 | return ast_value_factory->GetOneByteString(literal_one_byte_string()); |
| 1443 | } |
| 1444 | return ast_value_factory->GetTwoByteString(literal_two_byte_string()); |
| 1445 | } |
| 1446 | |
| 1447 | |
| 1448 | const AstRawString* Scanner::NextSymbol(AstValueFactory* ast_value_factory) { |
| 1449 | if (is_next_literal_one_byte()) { |
| 1450 | return ast_value_factory->GetOneByteString(next_literal_one_byte_string()); |
| 1451 | } |
| 1452 | return ast_value_factory->GetTwoByteString(next_literal_two_byte_string()); |
| 1453 | } |
| 1454 | |
| 1455 | |
| 1456 | const AstRawString* Scanner::CurrentRawSymbol( |
| 1457 | AstValueFactory* ast_value_factory) { |
| 1458 | if (is_raw_literal_one_byte()) { |
| 1459 | return ast_value_factory->GetOneByteString(raw_literal_one_byte_string()); |
| 1460 | } |
| 1461 | return ast_value_factory->GetTwoByteString(raw_literal_two_byte_string()); |
| 1462 | } |
| 1463 | |
| 1464 | |
| 1465 | double Scanner::DoubleValue() { |
| 1466 | DCHECK(is_literal_one_byte()); |
| 1467 | return StringToDouble( |
| 1468 | unicode_cache_, |
| 1469 | literal_one_byte_string(), |
| 1470 | ALLOW_HEX | ALLOW_OCTAL | ALLOW_IMPLICIT_OCTAL | ALLOW_BINARY); |
| 1471 | } |
| 1472 | |
| 1473 | |
| 1474 | bool Scanner::ContainsDot() { |
| 1475 | DCHECK(is_literal_one_byte()); |
| 1476 | Vector<const uint8_t> str = literal_one_byte_string(); |
| 1477 | return std::find(str.begin(), str.end(), '.') != str.end(); |
| 1478 | } |
| 1479 | |
| 1480 | |
| 1481 | int Scanner::FindSymbol(DuplicateFinder* finder, int value) { |
| 1482 | if (is_literal_one_byte()) { |
| 1483 | return finder->AddOneByteSymbol(literal_one_byte_string(), value); |
| 1484 | } |
| 1485 | return finder->AddTwoByteSymbol(literal_two_byte_string(), value); |
| 1486 | } |
| 1487 | |
| 1488 | |
| 1489 | bool Scanner::SetBookmark() { |
| 1490 | if (c0_ != kNoBookmark && bookmark_c0_ == kNoBookmark && |
| 1491 | next_next_.token == Token::UNINITIALIZED && source_->SetBookmark()) { |
| 1492 | bookmark_c0_ = c0_; |
| 1493 | CopyTokenDesc(&bookmark_current_, ¤t_); |
| 1494 | CopyTokenDesc(&bookmark_next_, &next_); |
| 1495 | return true; |
| 1496 | } |
| 1497 | return false; |
| 1498 | } |
| 1499 | |
| 1500 | |
| 1501 | void Scanner::ResetToBookmark() { |
| 1502 | DCHECK(BookmarkHasBeenSet()); // Caller hasn't called SetBookmark. |
| 1503 | |
| 1504 | source_->ResetToBookmark(); |
| 1505 | c0_ = bookmark_c0_; |
| 1506 | StartLiteral(); |
| 1507 | StartRawLiteral(); |
| 1508 | CopyTokenDesc(&next_, &bookmark_current_); |
| 1509 | current_ = next_; |
| 1510 | StartLiteral(); |
| 1511 | StartRawLiteral(); |
| 1512 | CopyTokenDesc(&next_, &bookmark_next_); |
| 1513 | |
| 1514 | bookmark_c0_ = kBookmarkWasApplied; |
| 1515 | } |
| 1516 | |
| 1517 | |
| 1518 | bool Scanner::BookmarkHasBeenSet() { return bookmark_c0_ >= 0; } |
| 1519 | |
| 1520 | |
| 1521 | bool Scanner::BookmarkHasBeenReset() { |
| 1522 | return bookmark_c0_ == kBookmarkWasApplied; |
| 1523 | } |
| 1524 | |
| 1525 | |
| 1526 | void Scanner::DropBookmark() { bookmark_c0_ = kNoBookmark; } |
| 1527 | |
| 1528 | |
| 1529 | void Scanner::CopyTokenDesc(TokenDesc* to, TokenDesc* from) { |
| 1530 | DCHECK_NOT_NULL(to); |
| 1531 | DCHECK_NOT_NULL(from); |
| 1532 | to->token = from->token; |
| 1533 | to->location = from->location; |
| 1534 | to->literal_chars->CopyFrom(from->literal_chars); |
| 1535 | to->raw_literal_chars->CopyFrom(from->raw_literal_chars); |
| 1536 | } |
| 1537 | |
| 1538 | |
| 1539 | int DuplicateFinder::AddOneByteSymbol(Vector<const uint8_t> key, int value) { |
| 1540 | return AddSymbol(key, true, value); |
| 1541 | } |
| 1542 | |
| 1543 | |
| 1544 | int DuplicateFinder::AddTwoByteSymbol(Vector<const uint16_t> key, int value) { |
| 1545 | return AddSymbol(Vector<const uint8_t>::cast(key), false, value); |
| 1546 | } |
| 1547 | |
| 1548 | |
| 1549 | int DuplicateFinder::AddSymbol(Vector<const uint8_t> key, |
| 1550 | bool is_one_byte, |
| 1551 | int value) { |
| 1552 | uint32_t hash = Hash(key, is_one_byte); |
| 1553 | byte* encoding = BackupKey(key, is_one_byte); |
| 1554 | HashMap::Entry* entry = map_.LookupOrInsert(encoding, hash); |
| 1555 | int old_value = static_cast<int>(reinterpret_cast<intptr_t>(entry->value)); |
| 1556 | entry->value = |
| 1557 | reinterpret_cast<void*>(static_cast<intptr_t>(value | old_value)); |
| 1558 | return old_value; |
| 1559 | } |
| 1560 | |
| 1561 | |
| 1562 | int DuplicateFinder::AddNumber(Vector<const uint8_t> key, int value) { |
| 1563 | DCHECK(key.length() > 0); |
| 1564 | // Quick check for already being in canonical form. |
| 1565 | if (IsNumberCanonical(key)) { |
| 1566 | return AddOneByteSymbol(key, value); |
| 1567 | } |
| 1568 | |
| 1569 | int flags = ALLOW_HEX | ALLOW_OCTAL | ALLOW_IMPLICIT_OCTAL | ALLOW_BINARY; |
| 1570 | double double_value = StringToDouble( |
| 1571 | unicode_constants_, key, flags, 0.0); |
| 1572 | int length; |
| 1573 | const char* string; |
| 1574 | if (!std::isfinite(double_value)) { |
| 1575 | string = "Infinity"; |
| 1576 | length = 8; // strlen("Infinity"); |
| 1577 | } else { |
| 1578 | string = DoubleToCString(double_value, |
| 1579 | Vector<char>(number_buffer_, kBufferSize)); |
| 1580 | length = StrLength(string); |
| 1581 | } |
| 1582 | return AddSymbol(Vector<const byte>(reinterpret_cast<const byte*>(string), |
| 1583 | length), true, value); |
| 1584 | } |
| 1585 | |
| 1586 | |
| 1587 | bool DuplicateFinder::IsNumberCanonical(Vector<const uint8_t> number) { |
| 1588 | // Test for a safe approximation of number literals that are already |
| 1589 | // in canonical form: max 15 digits, no leading zeroes, except an |
| 1590 | // integer part that is a single zero, and no trailing zeros below |
| 1591 | // the decimal point. |
| 1592 | int pos = 0; |
| 1593 | int length = number.length(); |
| 1594 | if (number.length() > 15) return false; |
| 1595 | if (number[pos] == '0') { |
| 1596 | pos++; |
| 1597 | } else { |
| 1598 | while (pos < length && |
| 1599 | static_cast<unsigned>(number[pos] - '0') <= ('9' - '0')) pos++; |
| 1600 | } |
| 1601 | if (length == pos) return true; |
| 1602 | if (number[pos] != '.') return false; |
| 1603 | pos++; |
| 1604 | bool invalid_last_digit = true; |
| 1605 | while (pos < length) { |
| 1606 | uint8_t digit = number[pos] - '0'; |
| 1607 | if (digit > '9' - '0') return false; |
| 1608 | invalid_last_digit = (digit == 0); |
| 1609 | pos++; |
| 1610 | } |
| 1611 | return !invalid_last_digit; |
| 1612 | } |
| 1613 | |
| 1614 | |
| 1615 | uint32_t DuplicateFinder::Hash(Vector<const uint8_t> key, bool is_one_byte) { |
| 1616 | // Primitive hash function, almost identical to the one used |
| 1617 | // for strings (except that it's seeded by the length and representation). |
| 1618 | int length = key.length(); |
| 1619 | uint32_t hash = (length << 1) | (is_one_byte ? 1 : 0); |
| 1620 | for (int i = 0; i < length; i++) { |
| 1621 | uint32_t c = key[i]; |
| 1622 | hash = (hash + c) * 1025; |
| 1623 | hash ^= (hash >> 6); |
| 1624 | } |
| 1625 | return hash; |
| 1626 | } |
| 1627 | |
| 1628 | |
| 1629 | bool DuplicateFinder::Match(void* first, void* second) { |
| 1630 | // Decode lengths. |
| 1631 | // Length + representation is encoded as base 128, most significant heptet |
| 1632 | // first, with a 8th bit being non-zero while there are more heptets. |
| 1633 | // The value encodes the number of bytes following, and whether the original |
| 1634 | // was Latin1. |
| 1635 | byte* s1 = reinterpret_cast<byte*>(first); |
| 1636 | byte* s2 = reinterpret_cast<byte*>(second); |
| 1637 | uint32_t length_one_byte_field = 0; |
| 1638 | byte c1; |
| 1639 | do { |
| 1640 | c1 = *s1; |
| 1641 | if (c1 != *s2) return false; |
| 1642 | length_one_byte_field = (length_one_byte_field << 7) | (c1 & 0x7f); |
| 1643 | s1++; |
| 1644 | s2++; |
| 1645 | } while ((c1 & 0x80) != 0); |
| 1646 | int length = static_cast<int>(length_one_byte_field >> 1); |
| 1647 | return memcmp(s1, s2, length) == 0; |
| 1648 | } |
| 1649 | |
| 1650 | |
| 1651 | byte* DuplicateFinder::BackupKey(Vector<const uint8_t> bytes, |
| 1652 | bool is_one_byte) { |
| 1653 | uint32_t one_byte_length = (bytes.length() << 1) | (is_one_byte ? 1 : 0); |
| 1654 | backing_store_.StartSequence(); |
| 1655 | // Emit one_byte_length as base-128 encoded number, with the 7th bit set |
| 1656 | // on the byte of every heptet except the last, least significant, one. |
| 1657 | if (one_byte_length >= (1 << 7)) { |
| 1658 | if (one_byte_length >= (1 << 14)) { |
| 1659 | if (one_byte_length >= (1 << 21)) { |
| 1660 | if (one_byte_length >= (1 << 28)) { |
| 1661 | backing_store_.Add( |
| 1662 | static_cast<uint8_t>((one_byte_length >> 28) | 0x80)); |
| 1663 | } |
| 1664 | backing_store_.Add( |
| 1665 | static_cast<uint8_t>((one_byte_length >> 21) | 0x80u)); |
| 1666 | } |
| 1667 | backing_store_.Add( |
| 1668 | static_cast<uint8_t>((one_byte_length >> 14) | 0x80u)); |
| 1669 | } |
| 1670 | backing_store_.Add(static_cast<uint8_t>((one_byte_length >> 7) | 0x80u)); |
| 1671 | } |
| 1672 | backing_store_.Add(static_cast<uint8_t>(one_byte_length & 0x7f)); |
| 1673 | |
| 1674 | backing_store_.AddBlock(bytes); |
| 1675 | return backing_store_.EndSequence().start(); |
| 1676 | } |
| 1677 | |
| 1678 | } // namespace internal |
| 1679 | } // namespace v8 |