Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(350)

Side by Side Diff: src/lexer/lexer-shell.cc

Issue 201953002: Experimental parser: small fixes (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/lexer/lexer.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 // Get utf8 length. 89 // Get utf8 length.
90 unsigned utf16_chars = 0; 90 unsigned utf16_chars = 0;
91 *is_one_byte = true; 91 *is_one_byte = true;
92 { 92 {
93 unsigned position = 0; 93 unsigned position = 0;
94 while (position < file_size) { 94 while (position < file_size) {
95 uint32_t c = char_data[position]; 95 uint32_t c = char_data[position];
96 if (c <= unibrow::Utf8::kMaxOneByteChar) { 96 if (c <= unibrow::Utf8::kMaxOneByteChar) {
97 position++; 97 position++;
98 } else { 98 } else {
99 *is_one_byte = false;
100 c = unibrow::Utf8::CalculateValue(char_data + position, 99 c = unibrow::Utf8::CalculateValue(char_data + position,
101 file_size - position, 100 file_size - position,
102 &position); 101 &position);
103 } 102 }
103 if (c > unibrow::Latin1::kMaxChar) *is_one_byte = false;
104 if (c > kMaxUtf16Character) { 104 if (c > kMaxUtf16Character) {
105 utf16_chars += 2; 105 utf16_chars += 2;
106 } else { 106 } else {
107 utf16_chars += 1; 107 utf16_chars += 1;
108 } 108 }
109 } 109 }
110 } 110 }
111 // Write new buffer out. 111 // Write new buffer out.
112 uint16_t* data = new uint16_t[utf16_chars]; 112 uint16_t* data = new uint16_t[utf16_chars];
113 unsigned position = 0; 113 unsigned position = 0;
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 } 217 }
218 218
219 219
220 static bool HasLiteral(Token::Value token) { 220 static bool HasLiteral(Token::Value token) {
221 return token == Token::IDENTIFIER || 221 return token == Token::IDENTIFIER ||
222 token == Token::STRING || 222 token == Token::STRING ||
223 token == Token::NUMBER; 223 token == Token::NUMBER;
224 } 224 }
225 225
226 226
227 template<typename Char>
228 static void Copy(const Vector<Char>& literal,
229 SmartArrayPointer<const uint16_t>* result,
230 int* literal_length) {
231 uint16_t* data = new uint16_t[literal.length()];
232 result->Reset(data);
233 for (int i = 0; i < literal.length(); i++) {
234 data[i] = literal[i];
235 }
236 *literal_length = literal.length();
237 }
238
239
240 class TokenWithLocation { 227 class TokenWithLocation {
241 public: 228 public:
242 Token::Value value;
243 int beg;
244 int end;
245 bool is_one_byte;
246 SmartArrayPointer<const uint16_t> literal;
247 int literal_length;
248 // The location of the latest octal position when the token was seen.
249 int octal_beg;
250 int octal_end;
251 TokenWithLocation(Token::Value token, 229 TokenWithLocation(Token::Value token,
252 Scanner* scanner, 230 Scanner* scanner,
253 Handle<String> literal_string) 231 Handle<String> literal_string)
254 : value(token) { 232 : value(token) {
255 beg = scanner->location().beg_pos; 233 beg = scanner->location().beg_pos;
256 end = scanner->location().end_pos; 234 end = scanner->location().end_pos;
257 octal_beg = scanner->octal_position().beg_pos; 235 octal_beg = scanner->octal_position().beg_pos;
258 octal_end = scanner->octal_position().end_pos; 236 octal_end = scanner->octal_position().end_pos;
259 is_one_byte = false; 237 is_one_byte = false;
260 literal_length = 0; 238 literal_length = 0;
261 if (!literal_string.is_null()) { 239 if (!literal_string.is_null()) {
262 DisallowHeapAllocation no_alloc; 240 DisallowHeapAllocation no_alloc;
263 String::FlatContent content = literal_string->GetFlatContent(); 241 String::FlatContent content = literal_string->GetFlatContent();
242 literal_length = literal_string->length();
243 literal.Reset(new uint16_t[literal_length]);
264 if (content.IsAscii()) { 244 if (content.IsAscii()) {
265 Copy(content.ToOneByteVector(), &literal, &literal_length); 245 is_one_byte = true;
246 CopyChars(
247 literal.get(), content.ToOneByteVector().start(), literal_length);
266 } else { 248 } else {
267 Copy(content.ToUC16Vector(), &literal, &literal_length); 249 CopyChars(
250 literal.get(), content.ToUC16Vector().start(), literal_length);
268 } 251 }
269 } 252 }
270 } 253 }
271 void Print(bool do_compare) const { 254 void Print(bool do_compare) const {
272 if (value == Token::ILLEGAL && do_compare) { 255 if (value == Token::ILLEGAL && do_compare) {
273 printf("%-15s (%d)\n", Token::Name(value), beg); 256 printf("%-15s (%d)\n", Token::Name(value), beg);
274 return; 257 return;
275 } 258 }
276 printf("%-15s (%d, %d)", Token::Name(value), beg, end); 259 printf("%-15s (%d, %d)", Token::Name(value), beg, end);
277 if (literal_length > 0) { 260 if (literal.get() != NULL) {
278 // TODO(dcarney): need some sort of checksum. 261 // TODO(dcarney): need some sort of checksum.
279 for (int i = 0; i < literal_length; i++) { 262 for (int i = 0; i < literal_length; i++) {
280 printf(is_one_byte ? " %02x" : " %04x", literal[i]); 263 printf(is_one_byte ? " %02x" : " %04x", literal[i]);
281 } 264 }
282 printf(" (is_one_byte: %d)", is_one_byte); 265 printf(" (is_one_byte: %d)", is_one_byte);
283 } 266 }
284 if (octal_beg >= 0) { 267 if (octal_beg >= 0) {
285 printf(" (last octal start: %d)", octal_beg); 268 printf(" (last octal start: %d)", octal_beg);
286 } 269 }
287 printf("\n"); 270 printf("\n");
288 } 271 }
289 272
290 private: 273 private:
274 Token::Value value;
tfarina 2014/03/19 17:36:50 could you add trailing underscore to these member
275 int beg;
276 int end;
277 bool is_one_byte;
278 SmartArrayPointer<uint16_t> literal;
279 int literal_length;
280 // The location of the latest octal position when the token was seen.
281 int octal_beg;
282 int octal_end;
283
291 DISALLOW_COPY_AND_ASSIGN(TokenWithLocation); 284 DISALLOW_COPY_AND_ASSIGN(TokenWithLocation);
292 }; 285 };
293 286
294 287
295 static TimeDelta RunLexer(const uint16_t* source, 288 static TimeDelta RunLexer(const uint16_t* source,
296 const uint8_t* source_end, 289 const uint8_t* source_end,
297 Isolate* isolate, 290 Isolate* isolate,
298 Encoding output_encoding, 291 Encoding output_encoding,
299 const LexerShellSettings& settings) { 292 const LexerShellSettings& settings) {
300 SmartPointer<Utf16CharacterStream> stream; 293 SmartPointer<Utf16CharacterStream> stream;
301 const uint8_t* one_byte_source = reinterpret_cast<const uint8_t*>(source); 294 const uint8_t* one_byte_source = reinterpret_cast<const uint8_t*>(source);
302 int bytes = source_end - one_byte_source; 295 int bytes = source_end - one_byte_source;
303 switch (output_encoding) { 296 switch (output_encoding) {
304 case UTF8: 297 case UTF8:
305 stream.Reset(new Utf8ToUtf16CharacterStream(one_byte_source, bytes)); 298 stream.Reset(new Utf8ToUtf16CharacterStream(one_byte_source, bytes));
306 break; 299 break;
307 case UTF16: { 300 case UTF16: {
308 CHECK_EQ(0, bytes % 2); 301 CHECK_EQ(0, bytes % 2);
309 Handle<String> result = isolate->factory()->NewStringFromTwoByte( 302 Handle<String> result = isolate->factory()->NewStringFromTwoByte(
310 Vector<const uint16_t>(source, bytes / 2)); 303 Vector<const uint16_t>(source, bytes / 2), false);
311 stream.Reset( 304 stream.Reset(
312 new GenericStringUtf16CharacterStream(result, 0, result->length())); 305 new GenericStringUtf16CharacterStream(result, 0, result->length()));
313 break; 306 break;
314 } 307 }
315 case LATIN1: { 308 case LATIN1: {
316 Handle<String> result = isolate->factory()->NewStringFromOneByte( 309 Handle<String> result = isolate->factory()->NewStringFromOneByte(
317 Vector<const uint8_t>(one_byte_source, bytes)); 310 Vector<const uint8_t>(one_byte_source, bytes));
318 stream.Reset( 311 stream.Reset(
319 new GenericStringUtf16CharacterStream(result, 0, result->length())); 312 new GenericStringUtf16CharacterStream(result, 0, result->length()));
320 break; 313 break;
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 ++truncate_by; 457 ++truncate_by;
465 } while (can_truncate); 458 } while (can_truncate);
466 } 459 }
467 if (!settings.print_tokens_for_compare) { 460 if (!settings.print_tokens_for_compare) {
468 printf("RunTime: %.f ms\n", total_time); 461 printf("RunTime: %.f ms\n", total_time);
469 } 462 }
470 } 463 }
471 v8::V8::Dispose(); 464 v8::V8::Dispose();
472 return 0; 465 return 0;
473 } 466 }
OLDNEW
« no previous file with comments | « src/lexer/lexer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698