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

Side by Side Diff: runtime/vm/scanner.cc

Issue 72923002: Add Dart keyword symbols to the VM isolate (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 1 month 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
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/scanner.h" 5 #include "vm/scanner.h"
6 6
7 #include "platform/assert.h" 7 #include "platform/assert.h"
8 #include "vm/dart.h"
8 #include "vm/flags.h" 9 #include "vm/flags.h"
9 #include "vm/object.h" 10 #include "vm/object.h"
10 #include "vm/object_store.h" 11 #include "vm/object_store.h"
11 #include "vm/symbols.h" 12 #include "vm/symbols.h"
12 #include "vm/thread.h" 13 #include "vm/thread.h"
13 #include "vm/token.h" 14 #include "vm/token.h"
14 #include "vm/unicode.h" 15 #include "vm/unicode.h"
15 16
16 namespace dart { 17 namespace dart {
17 18
18 DEFINE_FLAG(bool, print_tokens, false, "Print scanned tokens."); 19 DEFINE_FLAG(bool, print_tokens, false, "Print scanned tokens.");
19 20
20 void Scanner::InitKeywordTable() { 21
21 ObjectStore* object_store = Isolate::Current()->object_store(); 22 Scanner::KeywordTable Scanner::keywords_[Token::numKeywords];
22 keyword_symbol_table_ = object_store->keyword_symbols();
23 if (keyword_symbol_table_.IsNull()) {
24 object_store->InitKeywordTable();
25 keyword_symbol_table_ = object_store->keyword_symbols();
26 ASSERT(!keyword_symbol_table_.IsNull());
27 String& symbol = String::Handle();
28 for (int i = 0; i < Token::numKeywords; i++) {
29 Token::Kind token = static_cast<Token::Kind>(Token::kFirstKeyword + i);
30 symbol = Symbols::New(Token::Str(token));
31 keyword_symbol_table_.SetAt(i, symbol);
32 }
33 }
34 for (int i = 0; i < Token::numKeywords; i++) {
35 Token::Kind token = static_cast<Token::Kind>(Token::kFirstKeyword + i);
36 keywords_[i].kind = token;
37 keywords_[i].keyword_chars = Token::Str(token);
38 keywords_[i].keyword_len = strlen(Token::Str(token));
39 keywords_[i].keyword_symbol = NULL;
40 }
41 }
42 23
43 24
44 void Scanner::Reset() { 25 void Scanner::Reset() {
45 // Non-changing newline properties. 26 // Non-changing newline properties.
46 newline_token_.kind = Token::kNEWLINE; 27 newline_token_.kind = Token::kNEWLINE;
47 newline_token_.literal = NULL; 28 newline_token_.literal = NULL;
48 // We don't preserve the column information. 29 // We don't preserve the column information.
49 newline_token_.position.column = 0; 30 newline_token_.position.column = 0;
50 31
51 // Non-changing empty string token properties. 32 // Non-changing empty string token properties.
(...skipping 17 matching lines...) Expand all
69 c0_pos_.line = 1; 50 c0_pos_.line = 1;
70 c0_pos_.column = 0; 51 c0_pos_.column = 0;
71 ReadChar(); 52 ReadChar();
72 } 53 }
73 54
74 55
75 Scanner::Scanner(const String& src, const String& private_key) 56 Scanner::Scanner(const String& src, const String& private_key)
76 : source_(src), 57 : source_(src),
77 source_length_(src.Length()), 58 source_length_(src.Length()),
78 saved_context_(NULL), 59 saved_context_(NULL),
79 private_key_(String::ZoneHandle(private_key.raw())), 60 private_key_(String::ZoneHandle(private_key.raw())) {
80 keyword_symbol_table_(Array::ZoneHandle()) {
81 Reset(); 61 Reset();
82 InitKeywordTable();
83 } 62 }
84 63
85 Scanner::~Scanner() { 64 Scanner::~Scanner() {
86 while (saved_context_ != NULL) { 65 while (saved_context_ != NULL) {
87 ScanContext* ctx = saved_context_; 66 ScanContext* ctx = saved_context_;
88 saved_context_ = ctx->next; 67 saved_context_ = ctx->next;
89 delete ctx; 68 delete ctx;
90 } 69 }
91 } 70 }
92 71
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 while (i < Token::numKeywords && 293 while (i < Token::numKeywords &&
315 keywords_[i].keyword_chars[0] <= ident_char0) { 294 keywords_[i].keyword_chars[0] <= ident_char0) {
316 if (keywords_[i].keyword_len == ident_length) { 295 if (keywords_[i].keyword_len == ident_length) {
317 const char* keyword = keywords_[i].keyword_chars; 296 const char* keyword = keywords_[i].keyword_chars;
318 int char_pos = 0; 297 int char_pos = 0;
319 while ((char_pos < ident_length) && 298 while ((char_pos < ident_length) &&
320 (keyword[char_pos] == source_.CharAt(ident_pos + char_pos))) { 299 (keyword[char_pos] == source_.CharAt(ident_pos + char_pos))) {
321 char_pos++; 300 char_pos++;
322 } 301 }
323 if (char_pos == ident_length) { 302 if (char_pos == ident_length) {
324 if (keywords_[i].keyword_symbol == NULL) {
325 String& symbol = String::ZoneHandle();
326 symbol ^= keyword_symbol_table_.At(i);
327 ASSERT(!symbol.IsNull());
328 keywords_[i].keyword_symbol = &symbol;
329 }
330 current_token_.literal = keywords_[i].keyword_symbol; 303 current_token_.literal = keywords_[i].keyword_symbol;
331 current_token_.kind = keywords_[i].kind; 304 current_token_.kind = keywords_[i].kind;
332 return; 305 return;
333 } 306 }
334 } 307 }
335 i++; 308 i++;
336 } 309 }
337 310
338 // We did not read a keyword. 311 // We did not read a keyword.
339 current_token_.kind = Token::kIDENT; 312 current_token_.kind = Token::kIDENT;
(...skipping 627 matching lines...) Expand 10 before | Expand all | Expand 10 after
967 } 940 }
968 char private_key[32]; 941 char private_key[32];
969 OS::SNPrint(private_key, sizeof(private_key), 942 OS::SNPrint(private_key, sizeof(private_key),
970 "%c%#" Px "", kPrivateKeySeparator, key_value); 943 "%c%#" Px "", kPrivateKeySeparator, key_value);
971 const String& result = String::Handle(String::New(private_key, Heap::kOld)); 944 const String& result = String::Handle(String::New(private_key, Heap::kOld));
972 return result.raw(); 945 return result.raw();
973 } 946 }
974 947
975 948
976 void Scanner::InitOnce() { 949 void Scanner::InitOnce() {
950 ASSERT(Isolate::Current() == Dart::vm_isolate());
951 for (int i = 0; i < Token::numKeywords; i++) {
952 Token::Kind token = static_cast<Token::Kind>(Token::kFirstKeyword + i);
953 keywords_[i].kind = token;
954 keywords_[i].keyword_chars = Token::Str(token);
955 keywords_[i].keyword_len = strlen(Token::Str(token));
956 keywords_[i].keyword_symbol = &Symbols::Keyword(token);
957 }
977 } 958 }
978 959
979 } // namespace dart 960 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698