OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 unibrow::Predicate<unibrow::LineTerminator, 128> Scanner::kIsLineTerminator; | 42 unibrow::Predicate<unibrow::LineTerminator, 128> Scanner::kIsLineTerminator; |
43 unibrow::Predicate<unibrow::WhiteSpace, 128> Scanner::kIsWhiteSpace; | 43 unibrow::Predicate<unibrow::WhiteSpace, 128> Scanner::kIsWhiteSpace; |
44 | 44 |
45 | 45 |
46 StaticResource<Scanner::Utf8Decoder> Scanner::utf8_decoder_; | 46 StaticResource<Scanner::Utf8Decoder> Scanner::utf8_decoder_; |
47 | 47 |
48 | 48 |
49 // ---------------------------------------------------------------------------- | 49 // ---------------------------------------------------------------------------- |
50 // UTF8Buffer | 50 // UTF8Buffer |
51 | 51 |
52 UTF8Buffer::UTF8Buffer() { | 52 UTF8Buffer::UTF8Buffer() : data_(NULL), limit_(NULL) { } |
53 static const int kInitialCapacity = 1 * KB; | 53 |
54 data_ = NewArray<char>(kInitialCapacity); | 54 |
55 limit_ = ComputeLimit(data_, kInitialCapacity); | 55 UTF8Buffer::~UTF8Buffer() { |
56 Reset(); | 56 if (data_ != NULL) DeleteArray(data_); |
57 ASSERT(Capacity() == kInitialCapacity && pos() == 0); | |
58 } | 57 } |
59 | 58 |
60 | 59 |
61 UTF8Buffer::~UTF8Buffer() { | |
62 DeleteArray(data_); | |
63 } | |
64 | |
65 | |
66 void UTF8Buffer::AddCharSlow(uc32 c) { | 60 void UTF8Buffer::AddCharSlow(uc32 c) { |
67 static const int kCapacityGrowthLimit = 1 * MB; | 61 static const int kCapacityGrowthLimit = 1 * MB; |
68 if (cursor_ > limit_) { | 62 if (cursor_ > limit_) { |
69 int old_capacity = Capacity(); | 63 int old_capacity = Capacity(); |
70 int old_position = pos(); | 64 int old_position = pos(); |
71 int new_capacity = | 65 int new_capacity = |
72 Min(old_capacity * 2, old_capacity + kCapacityGrowthLimit); | 66 Min(old_capacity * 3, old_capacity + kCapacityGrowthLimit); |
73 char* new_data = NewArray<char>(new_capacity); | 67 char* new_data = NewArray<char>(new_capacity); |
74 memcpy(new_data, data_, old_position); | 68 memcpy(new_data, data_, old_position); |
75 DeleteArray(data_); | 69 DeleteArray(data_); |
76 data_ = new_data; | 70 data_ = new_data; |
77 cursor_ = new_data + old_position; | 71 cursor_ = new_data + old_position; |
78 limit_ = ComputeLimit(new_data, new_capacity); | 72 limit_ = ComputeLimit(new_data, new_capacity); |
79 ASSERT(Capacity() == new_capacity && pos() == old_position); | 73 ASSERT(Capacity() == new_capacity && pos() == old_position); |
80 } | 74 } |
81 if (static_cast<unsigned>(c) <= unibrow::Utf8::kMaxOneByteChar) { | 75 if (static_cast<unsigned>(c) <= unibrow::Utf8::kMaxOneByteChar) { |
82 *cursor_++ = c; // Common case: 7-bit ASCII. | 76 *cursor_++ = c; // Common case: 7-bit ASCII. |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 two_byte_string_buffer_.Initialize( | 333 two_byte_string_buffer_.Initialize( |
340 Handle<ExternalTwoByteString>::cast(source)); | 334 Handle<ExternalTwoByteString>::cast(source)); |
341 source_ = &two_byte_string_buffer_; | 335 source_ = &two_byte_string_buffer_; |
342 } else { | 336 } else { |
343 char_stream_buffer_.Initialize(source, stream); | 337 char_stream_buffer_.Initialize(source, stream); |
344 source_ = &char_stream_buffer_; | 338 source_ = &char_stream_buffer_; |
345 } | 339 } |
346 | 340 |
347 position_ = position; | 341 position_ = position; |
348 | 342 |
349 // Reset literals buffer | |
350 literals_.Reset(); | |
351 | |
352 // Set c0_ (one character ahead) | 343 // Set c0_ (one character ahead) |
353 ASSERT(kCharacterLookaheadBufferSize == 1); | 344 ASSERT(kCharacterLookaheadBufferSize == 1); |
354 Advance(); | 345 Advance(); |
| 346 // Initializer current_ to not refer to a literal buffer. |
| 347 current_.literal_buffer = NULL; |
355 | 348 |
356 // Skip initial whitespace allowing HTML comment ends just like | 349 // Skip initial whitespace allowing HTML comment ends just like |
357 // after a newline and scan first token. | 350 // after a newline and scan first token. |
358 has_line_terminator_before_next_ = true; | 351 has_line_terminator_before_next_ = true; |
359 SkipWhiteSpace(); | 352 SkipWhiteSpace(); |
360 Scan(); | 353 Scan(); |
361 } | 354 } |
362 | 355 |
363 | 356 |
364 Handle<String> Scanner::SubString(int start, int end) { | 357 Handle<String> Scanner::SubString(int start, int end) { |
(...skipping 12 matching lines...) Expand all Loading... |
377 stack_overflow_ = true; | 370 stack_overflow_ = true; |
378 next_.token = Token::ILLEGAL; | 371 next_.token = Token::ILLEGAL; |
379 } else { | 372 } else { |
380 Scan(); | 373 Scan(); |
381 } | 374 } |
382 return current_.token; | 375 return current_.token; |
383 } | 376 } |
384 | 377 |
385 | 378 |
386 void Scanner::StartLiteral() { | 379 void Scanner::StartLiteral() { |
387 next_.literal_pos = literals_.pos(); | 380 // Use the first buffer unless it's currently in use by the current_ token. |
| 381 // In most cases we won't have two literals/identifiers in a row, so |
| 382 // the second buffer won't be used very often and is unlikely to grow much. |
| 383 UTF8Buffer* free_buffer = |
| 384 (current_.literal_buffer != &literal_buffer_1_) ? &literal_buffer_1_ |
| 385 : &literal_buffer_2_; |
| 386 next_.literal_buffer = free_buffer; |
| 387 free_buffer->Reset(); |
388 } | 388 } |
389 | 389 |
390 | 390 |
391 void Scanner::AddChar(uc32 c) { | 391 void Scanner::AddChar(uc32 c) { |
392 literals_.AddChar(c); | 392 next_.literal_buffer->AddChar(c); |
393 } | 393 } |
394 | 394 |
395 | 395 |
396 void Scanner::TerminateLiteral() { | 396 void Scanner::TerminateLiteral() { |
397 next_.literal_end = literals_.pos(); | |
398 AddChar(0); | 397 AddChar(0); |
399 } | 398 } |
400 | 399 |
401 | 400 |
402 void Scanner::AddCharAdvance() { | 401 void Scanner::AddCharAdvance() { |
403 AddChar(c0_); | 402 AddChar(c0_); |
404 Advance(); | 403 Advance(); |
405 } | 404 } |
406 | 405 |
407 | 406 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
507 if (c0_ == '-') return SkipSingleLineComment(); | 506 if (c0_ == '-') return SkipSingleLineComment(); |
508 PushBack('-'); // undo Advance() | 507 PushBack('-'); // undo Advance() |
509 } | 508 } |
510 PushBack('!'); // undo Advance() | 509 PushBack('!'); // undo Advance() |
511 ASSERT(c0_ == '!'); | 510 ASSERT(c0_ == '!'); |
512 return Token::LT; | 511 return Token::LT; |
513 } | 512 } |
514 | 513 |
515 | 514 |
516 void Scanner::Scan() { | 515 void Scanner::Scan() { |
| 516 next_.literal_buffer = NULL; |
517 Token::Value token; | 517 Token::Value token; |
518 has_line_terminator_before_next_ = false; | 518 has_line_terminator_before_next_ = false; |
519 do { | 519 do { |
520 // Remember the position of the next token | 520 // Remember the position of the next token |
521 next_.location.beg_pos = source_pos(); | 521 next_.location.beg_pos = source_pos(); |
522 | 522 |
523 switch (c0_) { | 523 switch (c0_) { |
524 case ' ': | 524 case ' ': |
525 case '\t': | 525 case '\t': |
526 Advance(); | 526 Advance(); |
(...skipping 562 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1089 } | 1089 } |
1090 AddCharAdvance(); | 1090 AddCharAdvance(); |
1091 } | 1091 } |
1092 TerminateLiteral(); | 1092 TerminateLiteral(); |
1093 | 1093 |
1094 next_.location.end_pos = source_pos() - 1; | 1094 next_.location.end_pos = source_pos() - 1; |
1095 return true; | 1095 return true; |
1096 } | 1096 } |
1097 | 1097 |
1098 } } // namespace v8::internal | 1098 } } // namespace v8::internal |
OLD | NEW |