| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | |
| 2 // Redistribution and use in source and binary forms, with or without | |
| 3 // modification, are permitted provided that the following conditions are | |
| 4 // met: | |
| 5 // | |
| 6 // * Redistributions of source code must retain the above copyright | |
| 7 // notice, this list of conditions and the following disclaimer. | |
| 8 // * Redistributions in binary form must reproduce the above | |
| 9 // copyright notice, this list of conditions and the following | |
| 10 // disclaimer in the documentation and/or other materials provided | |
| 11 // with the distribution. | |
| 12 // * Neither the name of Google Inc. nor the names of its | |
| 13 // contributors may be used to endorse or promote products derived | |
| 14 // from this software without specific prior written permission. | |
| 15 // | |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 27 | |
| 28 #ifndef V8_LEXER_EXPERIMENTAL_SCANNER_H | |
| 29 #define V8_LEXER_EXPERIMENTAL_SCANNER_H | |
| 30 | |
| 31 #include "compiler.h" | |
| 32 #include "isolate.h" | |
| 33 #include "scanner.h" // UnicodeCache. | |
| 34 #include "token.h" | |
| 35 #include "utils.h" | |
| 36 #include "v8stdint.h" | |
| 37 #include "char-predicates-inl.h" | |
| 38 | |
| 39 namespace v8 { | |
| 40 namespace internal { | |
| 41 | |
| 42 class UnicodeCache; | |
| 43 | |
| 44 // Base class for scanners for different encodings. The meat is the pure virtual | |
| 45 // Scan() which each of them specializes. | |
| 46 class ScannerBase { | |
| 47 public: | |
| 48 struct Location { | |
| 49 Location(int b, int e) : beg_pos(b), end_pos(e) { } | |
| 50 Location() : beg_pos(0), end_pos(0) { } | |
| 51 | |
| 52 bool IsValid() const { | |
| 53 return beg_pos >= 0 && end_pos >= beg_pos; | |
| 54 } | |
| 55 | |
| 56 static Location invalid() { return Location(-1, -1); } | |
| 57 | |
| 58 int beg_pos; | |
| 59 int end_pos; | |
| 60 }; | |
| 61 | |
| 62 explicit ScannerBase(Isolate* isolate) | |
| 63 : isolate_(isolate), | |
| 64 unicode_cache_(isolate->unicode_cache()), | |
| 65 has_line_terminator_before_next_(true), | |
| 66 has_multiline_comment_before_next_(false), | |
| 67 current_literal_(&literals_[0]), | |
| 68 next_literal_(&literals_[1]), | |
| 69 harmony_numeric_literals_(false), | |
| 70 harmony_modules_(false), | |
| 71 harmony_scoping_(false) { | |
| 72 isolate->AddScanner(this); | |
| 73 } | |
| 74 | |
| 75 virtual ~ScannerBase() { | |
| 76 isolate_->RemoveScanner(this); | |
| 77 } | |
| 78 | |
| 79 // Has to be called after creating the scanner and setting the flags. | |
| 80 virtual void Init() = 0; | |
| 81 | |
| 82 // Seek forward to the given position. This operation works for simple cases | |
| 83 // such as seeking forward until simple delimiter tokens, which is what it is | |
| 84 // used for. After this call, we will have the token at the given position as | |
| 85 // the "next" token. The "current" token will be invalid. FIXME: for utf-8, | |
| 86 // we need to decide if pos is counted in characters or in bytes. | |
| 87 virtual void SeekForward(int pos) = 0; | |
| 88 virtual void SetEnd(int pos) = 0; | |
| 89 | |
| 90 // Scans the input as a regular expression pattern, previous character(s) must | |
| 91 // be /(=). Returns true if a pattern is scanned. FIXME: this won't work for | |
| 92 // utf-8 newlines. | |
| 93 virtual bool ScanRegExpPattern(bool seen_equal) = 0; | |
| 94 // Returns true if regexp flags are scanned (always since flags can | |
| 95 // be empty). | |
| 96 virtual bool ScanRegExpFlags() = 0; | |
| 97 | |
| 98 // Returns the location of the last seen octal literal. | |
| 99 virtual Location octal_position() const = 0; | |
| 100 virtual void clear_octal_position() = 0; | |
| 101 | |
| 102 // Sets the raw string pointer based on the string handle. Needs to be called | |
| 103 // right after GC. | |
| 104 virtual void UpdateBufferBasedOnHandle() = 0; | |
| 105 | |
| 106 // Returns the next token and advances input. | |
| 107 Token::Value Next() { | |
| 108 has_line_terminator_before_next_ = false; | |
| 109 has_multiline_comment_before_next_ = false; | |
| 110 current_ = next_; | |
| 111 std::swap(current_literal_, next_literal_); | |
| 112 Scan(); // Virtual! Will fill in next_. | |
| 113 return current_.token; | |
| 114 } | |
| 115 | |
| 116 // Returns the current token again. | |
| 117 Token::Value current_token() { return current_.token; } | |
| 118 | |
| 119 // Returns the location information for the current token | |
| 120 // (the token last returned by Next()). | |
| 121 Location location() { | |
| 122 return Location(current_.beg_pos, current_.end_pos); | |
| 123 } | |
| 124 | |
| 125 // One token look-ahead (past the token returned by Next()). | |
| 126 Token::Value peek() const { return next_.token; } | |
| 127 | |
| 128 Location peek_location() const { | |
| 129 return Location(next_.beg_pos, next_.end_pos); | |
| 130 } | |
| 131 | |
| 132 UnicodeCache* unicode_cache() { return unicode_cache_; } | |
| 133 | |
| 134 bool HarmonyScoping() const { | |
| 135 return harmony_scoping_; | |
| 136 } | |
| 137 void SetHarmonyScoping(bool scoping) { | |
| 138 harmony_scoping_ = scoping; | |
| 139 } | |
| 140 bool HarmonyModules() const { | |
| 141 return harmony_modules_; | |
| 142 } | |
| 143 void SetHarmonyModules(bool modules) { | |
| 144 harmony_modules_ = modules; | |
| 145 } | |
| 146 bool HarmonyNumericLiterals() const { | |
| 147 return harmony_numeric_literals_; | |
| 148 } | |
| 149 void SetHarmonyNumericLiterals(bool numeric_literals) { | |
| 150 harmony_numeric_literals_ = numeric_literals; | |
| 151 } | |
| 152 | |
| 153 // Returns true if there was a line terminator before the peek'ed token, | |
| 154 // possibly inside a multi-line comment. | |
| 155 bool HasAnyLineTerminatorBeforeNext() const { | |
| 156 return has_line_terminator_before_next_ || | |
| 157 has_multiline_comment_before_next_; | |
| 158 } | |
| 159 | |
| 160 Handle<String> GetLiteralSymbol() { | |
| 161 EnsureCurrentLiteralIsValid(); | |
| 162 return InternalizeLiteral(current_literal_); | |
| 163 } | |
| 164 | |
| 165 Handle<String> GetLiteralString(PretenureFlag tenured) { | |
| 166 EnsureCurrentLiteralIsValid(); | |
| 167 return AllocateLiteral(current_literal_, tenured); | |
| 168 } | |
| 169 | |
| 170 Handle<String> GetNextLiteralString(PretenureFlag tenured) { | |
| 171 EnsureNextLiteralIsValid(); | |
| 172 return AllocateLiteral(next_literal_, tenured); | |
| 173 } | |
| 174 | |
| 175 Vector<const char> literal_ascii_string() { | |
| 176 EnsureCurrentLiteralIsValid(); | |
| 177 return current_literal_->ascii_string; | |
| 178 } | |
| 179 | |
| 180 Vector<const uc16> literal_utf16_string() { | |
| 181 EnsureCurrentLiteralIsValid(); | |
| 182 return current_literal_->utf16_string; | |
| 183 } | |
| 184 | |
| 185 int literal_length() { | |
| 186 EnsureCurrentLiteralIsValid(); | |
| 187 return current_literal_->length; | |
| 188 } | |
| 189 | |
| 190 // This should be is_onebyte or is_latin1; it doesn't mean ASCII for real. | |
| 191 bool is_literal_ascii() { | |
| 192 EnsureCurrentLiteralIsValid(); | |
| 193 return current_literal_->is_ascii; | |
| 194 } | |
| 195 | |
| 196 bool is_literal_contextual_keyword(Vector<const char> keyword) { | |
| 197 if (!is_literal_ascii()) return false; | |
| 198 Vector<const char> literal = literal_ascii_string(); | |
| 199 return literal.length() == keyword.length() && | |
| 200 (memcmp(literal.start(), keyword.start(), literal.length()) == 0); | |
| 201 } | |
| 202 | |
| 203 bool literal_contains_escapes() const { | |
| 204 return current_.has_escapes; | |
| 205 } | |
| 206 | |
| 207 Vector<const char> next_literal_ascii_string() { | |
| 208 EnsureNextLiteralIsValid(); | |
| 209 return next_literal_->ascii_string; | |
| 210 } | |
| 211 | |
| 212 Vector<const uc16> next_literal_utf16_string() { | |
| 213 EnsureNextLiteralIsValid(); | |
| 214 return next_literal_->utf16_string; | |
| 215 } | |
| 216 | |
| 217 int next_literal_length() { | |
| 218 EnsureNextLiteralIsValid(); | |
| 219 return next_literal_->length; | |
| 220 } | |
| 221 | |
| 222 bool is_next_literal_ascii() { | |
| 223 EnsureNextLiteralIsValid(); | |
| 224 return next_literal_->is_ascii; | |
| 225 } | |
| 226 | |
| 227 bool is_next_contextual_keyword(Vector<const char> keyword) { | |
| 228 if (!is_next_literal_ascii()) return false; | |
| 229 Vector<const char> literal = next_literal_ascii_string(); | |
| 230 return literal.length() == keyword.length() && | |
| 231 (memcmp(literal.start(), keyword.start(), literal.length()) == 0); | |
| 232 } | |
| 233 | |
| 234 protected: | |
| 235 struct TokenDesc { | |
| 236 Token::Value token; | |
| 237 int beg_pos; | |
| 238 int end_pos; | |
| 239 bool has_escapes; | |
| 240 bool is_onebyte; | |
| 241 }; | |
| 242 | |
| 243 struct LiteralDesc { | |
| 244 int beg_pos; | |
| 245 bool is_ascii; | |
| 246 bool is_in_buffer; | |
| 247 int offset; | |
| 248 int length; | |
| 249 Vector<const char> ascii_string; | |
| 250 Vector<const uc16> utf16_string; | |
| 251 LiteralBuffer buffer; | |
| 252 LiteralDesc() : beg_pos(-1), is_ascii(false), is_in_buffer(false), | |
| 253 offset(0), length(0) { } | |
| 254 bool Valid(int pos) { return beg_pos == pos; } | |
| 255 }; | |
| 256 | |
| 257 virtual void Scan() = 0; | |
| 258 virtual bool FillLiteral(const TokenDesc& token, LiteralDesc* literal) = 0; | |
| 259 | |
| 260 void ResetLiterals() { | |
| 261 if (!current_literal_->is_in_buffer) current_literal_->beg_pos = -1; | |
| 262 if (!next_literal_->is_in_buffer) next_literal_->beg_pos = -1; | |
| 263 } | |
| 264 | |
| 265 void EnsureCurrentLiteralIsValid() { | |
| 266 if (!current_literal_->Valid(current_.beg_pos)) { | |
| 267 FillLiteral(current_, current_literal_); | |
| 268 } | |
| 269 } | |
| 270 | |
| 271 void EnsureNextLiteralIsValid() { | |
| 272 if (!next_literal_->Valid(next_.beg_pos)) { | |
| 273 FillLiteral(next_, next_literal_); | |
| 274 } | |
| 275 } | |
| 276 | |
| 277 virtual Handle<String> InternalizeLiteral(LiteralDesc* literal) = 0; | |
| 278 virtual Handle<String> AllocateLiteral(LiteralDesc* literal, | |
| 279 PretenureFlag tenured) = 0; | |
| 280 | |
| 281 Isolate* isolate_; | |
| 282 UnicodeCache* unicode_cache_; | |
| 283 | |
| 284 bool has_line_terminator_before_next_; | |
| 285 // Whether there is a multiline comment *with a line break* before the next | |
| 286 // token. | |
| 287 bool has_multiline_comment_before_next_; | |
| 288 | |
| 289 TokenDesc current_; // desc for current token (as returned by Next()) | |
| 290 TokenDesc next_; // desc for next token (one token look-ahead) | |
| 291 | |
| 292 LiteralDesc* current_literal_; | |
| 293 LiteralDesc* next_literal_; | |
| 294 LiteralDesc literals_[2]; | |
| 295 | |
| 296 bool harmony_numeric_literals_; | |
| 297 bool harmony_modules_; | |
| 298 bool harmony_scoping_; | |
| 299 }; | |
| 300 | |
| 301 | |
| 302 template<typename Char> | |
| 303 class ExperimentalScanner : public ScannerBase { | |
| 304 public: | |
| 305 explicit ExperimentalScanner( | |
| 306 Handle<String> source, | |
| 307 Isolate* isolate) | |
| 308 : ScannerBase(isolate), | |
| 309 source_handle_(source), | |
| 310 buffer_(NULL), | |
| 311 buffer_end_(NULL), | |
| 312 start_(NULL), | |
| 313 cursor_(NULL), | |
| 314 last_octal_end_(NULL) { | |
| 315 ASSERT(source->IsFlat()); | |
| 316 UpdateBufferBasedOnHandle(); | |
| 317 current_.beg_pos = current_.end_pos = next_.beg_pos = next_.end_pos = 0; | |
| 318 } | |
| 319 | |
| 320 virtual void Init() { | |
| 321 Scan(); | |
| 322 } | |
| 323 | |
| 324 virtual ~ExperimentalScanner() { } | |
| 325 | |
| 326 virtual void SeekForward(int pos); | |
| 327 virtual void SetEnd(int pos); | |
| 328 virtual bool ScanRegExpPattern(bool seen_equal); | |
| 329 virtual bool ScanRegExpFlags(); | |
| 330 virtual Location octal_position() const; | |
| 331 virtual void clear_octal_position() { | |
| 332 last_octal_end_ = NULL; | |
| 333 } | |
| 334 | |
| 335 virtual void UpdateBufferBasedOnHandle() { | |
| 336 // We get a raw pointer from the Handle, but we also update it every time | |
| 337 // there is a GC, so it is safe. | |
| 338 DisallowHeapAllocation no_gc; | |
| 339 const Char* new_buffer = GetNewBufferBasedOnHandle(); | |
| 340 if (new_buffer != buffer_) { | |
| 341 int start_offset = start_ - buffer_; | |
| 342 int cursor_offset = cursor_ - buffer_; | |
| 343 int last_octal_end_offset = last_octal_end_ - buffer_; | |
| 344 buffer_ = new_buffer; | |
| 345 buffer_end_ = buffer_ + source_handle_->length(); | |
| 346 start_ = buffer_ + start_offset; | |
| 347 cursor_ = buffer_ + cursor_offset; | |
| 348 if (last_octal_end_ != NULL) { | |
| 349 last_octal_end_ = buffer_ + last_octal_end_offset; | |
| 350 } | |
| 351 ResetLiterals(); | |
| 352 } | |
| 353 } | |
| 354 | |
| 355 protected: | |
| 356 virtual void Scan(); | |
| 357 | |
| 358 const Char* GetNewBufferBasedOnHandle() const; | |
| 359 | |
| 360 virtual bool FillLiteral(const TokenDesc& token, LiteralDesc* literal); | |
| 361 virtual Handle<String> InternalizeLiteral(LiteralDesc* literal); | |
| 362 virtual Handle<String> AllocateLiteral(LiteralDesc* literal, | |
| 363 PretenureFlag tenured); | |
| 364 | |
| 365 | |
| 366 private: | |
| 367 bool ValidIdentifierPart() { | |
| 368 return unicode_cache_->IsIdentifierPart(ScanHexNumber(4)); | |
| 369 } | |
| 370 | |
| 371 bool ValidIdentifierStart() { | |
| 372 return unicode_cache_->IsIdentifierStart(ScanHexNumber(4)); | |
| 373 } | |
| 374 | |
| 375 uc32 ScanHexNumber(int length); | |
| 376 bool ScanLiteralUnicodeEscape(); | |
| 377 | |
| 378 const Char* ScanHexNumber(const Char* start, | |
| 379 const Char* end, | |
| 380 uc32* result); | |
| 381 const Char* ScanOctalEscape(const Char* start, | |
| 382 const Char* end, | |
| 383 uc32* result); | |
| 384 const Char* ScanIdentifierUnicodeEscape(const Char* start, | |
| 385 const Char* end, | |
| 386 uc32* result); | |
| 387 const Char* ScanEscape(const Char* start, | |
| 388 const Char* end, | |
| 389 LiteralBuffer* literal); | |
| 390 | |
| 391 // Returns true if the literal of the token can be represented as a | |
| 392 // substring of the source. | |
| 393 bool IsSubstringOfSource(const TokenDesc& token); | |
| 394 | |
| 395 bool CopyToLiteralBuffer(const Char* start, | |
| 396 const Char* end, | |
| 397 const TokenDesc& token, | |
| 398 LiteralDesc* literal); | |
| 399 | |
| 400 Handle<String> source_handle_; | |
| 401 const Char* buffer_; | |
| 402 const Char* buffer_end_; | |
| 403 const Char* start_; | |
| 404 const Char* cursor_; | |
| 405 | |
| 406 // Where we have seen the last octal number or an octal escape inside a | |
| 407 // string. Used by octal_position(). | |
| 408 const Char* last_octal_end_; | |
| 409 }; | |
| 410 | |
| 411 | |
| 412 template<typename Char> | |
| 413 void ExperimentalScanner<Char>::SeekForward(int pos) { | |
| 414 cursor_ = buffer_ + pos; | |
| 415 start_ = cursor_; | |
| 416 has_line_terminator_before_next_ = false; | |
| 417 has_multiline_comment_before_next_ = false; | |
| 418 Scan(); // Fills in next_. | |
| 419 } | |
| 420 | |
| 421 | |
| 422 template<typename Char> | |
| 423 void ExperimentalScanner<Char>::SetEnd(int pos) { | |
| 424 buffer_end_ = buffer_ + pos; | |
| 425 } | |
| 426 | |
| 427 | |
| 428 template<typename Char> | |
| 429 bool ExperimentalScanner<Char>::ScanRegExpPattern(bool seen_equal) { | |
| 430 // Scan: ('/' | '/=') RegularExpressionBody '/' RegularExpressionFlags | |
| 431 bool in_character_class = false; | |
| 432 | |
| 433 // Previous token is either '/' or '/=', in the second case, the | |
| 434 // pattern starts at =. | |
| 435 next_.beg_pos = next_.end_pos = (cursor_ - buffer_) - (seen_equal ? 1 : 0); | |
| 436 | |
| 437 // Scan regular expression body: According to ECMA-262, 3rd, 7.8.5, | |
| 438 // the scanner should pass uninterpreted bodies to the RegExp | |
| 439 // constructor. | |
| 440 if (cursor_ >= buffer_end_) return false; | |
| 441 | |
| 442 while (*cursor_ != '/' || in_character_class) { | |
| 443 if (unicode_cache_->IsLineTerminator(*cursor_)) return false; | |
| 444 if (*cursor_ == '\\') { // Escape sequence. | |
| 445 ++cursor_; | |
| 446 if (cursor_ >= buffer_end_ || unicode_cache_->IsLineTerminator(*cursor_)) | |
| 447 return false; | |
| 448 ++cursor_; | |
| 449 if (cursor_ >= buffer_end_) return false; | |
| 450 // If the escape allows more characters, i.e., \x??, \u????, or \c?, | |
| 451 // only "safe" characters are allowed (letters, digits, underscore), | |
| 452 // otherwise the escape isn't valid and the invalid character has | |
| 453 // its normal meaning. I.e., we can just continue scanning without | |
| 454 // worrying whether the following characters are part of the escape | |
| 455 // or not, since any '/', '\\' or '[' is guaranteed to not be part | |
| 456 // of the escape sequence. | |
| 457 | |
| 458 // TODO(896): At some point, parse RegExps more throughly to capture | |
| 459 // octal esacpes in strict mode. | |
| 460 } else { // Unescaped character. | |
| 461 if (*cursor_ == '[') in_character_class = true; | |
| 462 if (*cursor_ == ']') in_character_class = false; | |
| 463 if (++cursor_ >= buffer_end_) return false; | |
| 464 } | |
| 465 } | |
| 466 next_.end_pos = (cursor_ - buffer_); | |
| 467 ++cursor_; // consume '/' | |
| 468 return true; | |
| 469 } | |
| 470 | |
| 471 | |
| 472 template<typename Char> | |
| 473 bool ExperimentalScanner<Char>::ScanRegExpFlags() { | |
| 474 next_.beg_pos = cursor_ - buffer_; | |
| 475 // Scan regular expression flags. | |
| 476 while (cursor_ < buffer_end_ && unicode_cache_->IsIdentifierPart(*cursor_)) { | |
| 477 if (*cursor_ != '\\') { | |
| 478 if (++cursor_ >= buffer_end_) break; | |
| 479 } else { | |
| 480 if (!ScanLiteralUnicodeEscape()) break; | |
| 481 if (++cursor_ >= buffer_end_) break; | |
| 482 } | |
| 483 } | |
| 484 next_.end_pos = cursor_ - buffer_; | |
| 485 return true; | |
| 486 } | |
| 487 | |
| 488 | |
| 489 template<typename Char> | |
| 490 uc32 ExperimentalScanner<Char>::ScanHexNumber(int length) { | |
| 491 // We have seen \uXXXX, let's see what it is. | |
| 492 uc32 x = 0; | |
| 493 for (const Char* s = cursor_ - length; s != cursor_; ++s) { | |
| 494 int d = HexValue(*s); | |
| 495 if (d < 0) { | |
| 496 return -1; | |
| 497 } | |
| 498 x = x * 16 + d; | |
| 499 } | |
| 500 return x; | |
| 501 } | |
| 502 | |
| 503 | |
| 504 template<typename Char> | |
| 505 const Char* ExperimentalScanner<Char>::ScanHexNumber( | |
| 506 const Char* cursor, const Char* end, uc32* result) { | |
| 507 uc32 x = 0; | |
| 508 for ( ; cursor < end; ++cursor) { | |
| 509 int d = HexValue(*cursor); | |
| 510 if (d < 0) { | |
| 511 *result = -1; | |
| 512 return NULL; | |
| 513 } | |
| 514 x = x * 16 + d; | |
| 515 } | |
| 516 *result = x; | |
| 517 return cursor; | |
| 518 } | |
| 519 | |
| 520 | |
| 521 // Octal escapes of the forms '\0xx' and '\xxx' are not a part of | |
| 522 // ECMA-262. Other JS VMs support them. | |
| 523 template<typename Char> | |
| 524 const Char* ExperimentalScanner<Char>::ScanOctalEscape( | |
| 525 const Char* start, const Char* end, uc32* result) { | |
| 526 uc32 x = *result - '0'; | |
| 527 const Char* cursor; | |
| 528 for (cursor = start; cursor < end; cursor++) { | |
| 529 int d = *cursor - '0'; | |
| 530 if (d < 0 || d > 7) break; | |
| 531 int nx = x * 8 + d; | |
| 532 if (nx >= 256) break; | |
| 533 x = nx; | |
| 534 } | |
| 535 *result = x; | |
| 536 return cursor; | |
| 537 } | |
| 538 | |
| 539 | |
| 540 template<typename Char> | |
| 541 bool ExperimentalScanner<Char>::ScanLiteralUnicodeEscape() { | |
| 542 ASSERT(cursor_ < buffer_end_); | |
| 543 Char primary_char = *(cursor_); | |
| 544 ASSERT(primary_char == '\\'); | |
| 545 if (++cursor_ >= buffer_end_) return false; | |
| 546 primary_char = *(cursor_); | |
| 547 int i = 1; | |
| 548 if (primary_char == 'u') { | |
| 549 i++; | |
| 550 while (i < 6) { | |
| 551 if (++cursor_ >= buffer_end_) return false; | |
| 552 primary_char = *(cursor_); | |
| 553 if (!IsHexDigit(primary_char)) break; | |
| 554 i++; | |
| 555 } | |
| 556 } | |
| 557 return i == 6; | |
| 558 } | |
| 559 | |
| 560 | |
| 561 template<typename Char> | |
| 562 const Char* ExperimentalScanner<Char>::ScanIdentifierUnicodeEscape( | |
| 563 const Char* cursor, const Char* end, uc32* result) { | |
| 564 ASSERT(*cursor == '\\'); | |
| 565 if (++cursor >= end) return NULL; | |
| 566 if (*cursor != 'u') return NULL; | |
| 567 ++cursor; | |
| 568 if (cursor + 4 > end) return NULL; | |
| 569 cursor = ScanHexNumber(cursor, cursor + 4, result); | |
| 570 return cursor; | |
| 571 } | |
| 572 | |
| 573 | |
| 574 template<typename Char> | |
| 575 const Char* ExperimentalScanner<Char>::ScanEscape( | |
| 576 const Char* cursor, const Char* end, LiteralBuffer* literal) { | |
| 577 ASSERT(*cursor == '\\'); | |
| 578 if (++cursor >= end) return NULL; | |
| 579 uc32 c = *cursor; | |
| 580 if (++cursor > end) return NULL; | |
| 581 // Skip escaped newlines. | |
| 582 if (unicode_cache_->IsLineTerminator(c)) { | |
| 583 uc32 peek = *cursor; | |
| 584 // Allow CR+LF newlines in multiline string literals. | |
| 585 if (IsCarriageReturn(c) && IsLineFeed(peek)) cursor++; | |
| 586 // Allow LF+CR newlines in multiline string literals. | |
| 587 if (IsLineFeed(c) && IsCarriageReturn(peek)) cursor++; | |
| 588 return cursor; | |
| 589 } | |
| 590 | |
| 591 switch (c) { | |
| 592 case '\'': // fall through | |
| 593 case '"' : // fall through | |
| 594 case '\\': break; | |
| 595 case 'b' : c = '\b'; break; | |
| 596 case 'f' : c = '\f'; break; | |
| 597 case 'n' : c = '\n'; break; | |
| 598 case 'r' : c = '\r'; break; | |
| 599 case 't' : c = '\t'; break; | |
| 600 case 'u' : { | |
| 601 ASSERT(cursor + 4 <= end); | |
| 602 cursor = ScanHexNumber(cursor, cursor + 4, &c); | |
| 603 if (cursor == NULL) return NULL; | |
| 604 break; | |
| 605 } | |
| 606 case 'v' : c = '\v'; break; | |
| 607 case 'x' : { | |
| 608 ASSERT(cursor + 2 <= end); | |
| 609 cursor = ScanHexNumber(cursor, cursor + 2, &c); | |
| 610 if (cursor == NULL) return NULL; | |
| 611 break; | |
| 612 } | |
| 613 case '0' : // fall through | |
| 614 case '1' : // fall through | |
| 615 case '2' : // fall through | |
| 616 case '3' : // fall through | |
| 617 case '4' : // fall through | |
| 618 case '5' : // fall through | |
| 619 case '6' : // fall through | |
| 620 case '7' : | |
| 621 if (end > cursor + 2) end = cursor + 2; | |
| 622 cursor = ScanOctalEscape(cursor, end, &c); break; | |
| 623 } | |
| 624 | |
| 625 // According to ECMA-262, section 7.8.4, characters not covered by the | |
| 626 // above cases should be illegal, but they are commonly handled as | |
| 627 // non-escaped characters by JS VMs. | |
| 628 literal->AddChar(c); | |
| 629 return cursor; | |
| 630 } | |
| 631 | |
| 632 | |
| 633 template<typename Char> | |
| 634 ScannerBase::Location ExperimentalScanner<Char>::octal_position() const { | |
| 635 if (!last_octal_end_) | |
| 636 return Location::invalid(); | |
| 637 // The last octal might be an octal escape or an octal number. Whichever it | |
| 638 // is, we'll find the start by just scanning back until we hit a non-octal | |
| 639 // character. | |
| 640 const Char* temp_cursor = last_octal_end_ - 1; | |
| 641 while (temp_cursor >= buffer_ && *temp_cursor >= '0' && *temp_cursor <= '7') | |
| 642 --temp_cursor; | |
| 643 return Location(temp_cursor - buffer_ + 1, last_octal_end_ - buffer_); | |
| 644 } | |
| 645 | |
| 646 } } | |
| 647 | |
| 648 #endif // V8_LEXER_EXPERIMENTAL_SCANNER_H | |
| OLD | NEW |