OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Features shared by parsing and pre-parsing scanners. | 5 // Features shared by parsing and pre-parsing scanners. |
6 | 6 |
7 #ifndef V8_PARSING_SCANNER_H_ | 7 #ifndef V8_PARSING_SCANNER_H_ |
8 #define V8_PARSING_SCANNER_H_ | 8 #define V8_PARSING_SCANNER_H_ |
9 | 9 |
10 #include "src/allocation.h" | 10 #include "src/allocation.h" |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 | 155 |
156 class LiteralBuffer { | 156 class LiteralBuffer { |
157 public: | 157 public: |
158 LiteralBuffer() : is_one_byte_(true), position_(0), backing_store_() { } | 158 LiteralBuffer() : is_one_byte_(true), position_(0), backing_store_() { } |
159 | 159 |
160 ~LiteralBuffer() { backing_store_.Dispose(); } | 160 ~LiteralBuffer() { backing_store_.Dispose(); } |
161 | 161 |
162 INLINE(void AddChar(char code_unit)) { | 162 INLINE(void AddChar(char code_unit)) { |
163 if (position_ >= backing_store_.length()) ExpandBuffer(); | 163 if (position_ >= backing_store_.length()) ExpandBuffer(); |
164 DCHECK(is_one_byte_); | 164 DCHECK(is_one_byte_); |
165 DCHECK(0 <= code_unit && code_unit <= kMaxAscii); | 165 DCHECK(IsValidAscii(code_unit)); |
166 backing_store_[position_] = static_cast<byte>(code_unit); | 166 backing_store_[position_] = static_cast<byte>(code_unit); |
167 position_ += kOneByteSize; | 167 position_ += kOneByteSize; |
168 return; | 168 return; |
169 } | 169 } |
170 | 170 |
171 INLINE(void AddChar(uc32 code_unit)) { | 171 INLINE(void AddChar(uc32 code_unit)) { |
172 if (position_ >= backing_store_.length()) ExpandBuffer(); | 172 if (position_ >= backing_store_.length()) ExpandBuffer(); |
173 if (is_one_byte_) { | 173 if (is_one_byte_) { |
174 if (code_unit <= unibrow::Latin1::kMaxChar) { | 174 if (code_unit <= unibrow::Latin1::kMaxChar) { |
175 backing_store_[position_] = static_cast<byte>(code_unit); | 175 backing_store_[position_] = static_cast<byte>(code_unit); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 backing_store_ = other->backing_store_.Clone(); | 244 backing_store_ = other->backing_store_.Clone(); |
245 } | 245 } |
246 } | 246 } |
247 } | 247 } |
248 | 248 |
249 private: | 249 private: |
250 static const int kInitialCapacity = 16; | 250 static const int kInitialCapacity = 16; |
251 static const int kGrowthFactory = 4; | 251 static const int kGrowthFactory = 4; |
252 static const int kMinConversionSlack = 256; | 252 static const int kMinConversionSlack = 256; |
253 static const int kMaxGrowth = 1 * MB; | 253 static const int kMaxGrowth = 1 * MB; |
| 254 |
| 255 inline bool IsValidAscii(char code_unit) { |
| 256 // Control characters and printable characters span the range of |
| 257 // valid ASCII characters (0-127). Chars are unsigned on some |
| 258 // platforms which causes compiler warnings if the validity check |
| 259 // tests the lower bound >= 0 as it's always true. |
| 260 return iscntrl(code_unit) || isprint(code_unit); |
| 261 } |
| 262 |
254 inline int NewCapacity(int min_capacity) { | 263 inline int NewCapacity(int min_capacity) { |
255 int capacity = Max(min_capacity, backing_store_.length()); | 264 int capacity = Max(min_capacity, backing_store_.length()); |
256 int new_capacity = Min(capacity * kGrowthFactory, capacity + kMaxGrowth); | 265 int new_capacity = Min(capacity * kGrowthFactory, capacity + kMaxGrowth); |
257 return new_capacity; | 266 return new_capacity; |
258 } | 267 } |
259 | 268 |
260 void ExpandBuffer() { | 269 void ExpandBuffer() { |
261 Vector<byte> new_store = Vector<byte>::New(NewCapacity(kInitialCapacity)); | 270 Vector<byte> new_store = Vector<byte>::New(NewCapacity(kInitialCapacity)); |
262 MemCopy(new_store.start(), backing_store_.start(), position_); | 271 MemCopy(new_store.start(), backing_store_.start(), position_); |
263 backing_store_.Dispose(); | 272 backing_store_.Dispose(); |
(...skipping 558 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
822 bool allow_harmony_exponentiation_operator_; | 831 bool allow_harmony_exponentiation_operator_; |
823 | 832 |
824 MessageTemplate::Template scanner_error_; | 833 MessageTemplate::Template scanner_error_; |
825 Location scanner_error_location_; | 834 Location scanner_error_location_; |
826 }; | 835 }; |
827 | 836 |
828 } // namespace internal | 837 } // namespace internal |
829 } // namespace v8 | 838 } // namespace v8 |
830 | 839 |
831 #endif // V8_PARSING_SCANNER_H_ | 840 #endif // V8_PARSING_SCANNER_H_ |
OLD | NEW |