| OLD | NEW |
| 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/flags.h" | 8 #include "vm/flags.h" |
| 9 #include "vm/object.h" | 9 #include "vm/object.h" |
| 10 #include "vm/thread.h" | 10 #include "vm/thread.h" |
| 11 #include "vm/token.h" | 11 #include "vm/token.h" |
| 12 | 12 |
| 13 namespace dart { | 13 namespace dart { |
| 14 | 14 |
| 15 DEFINE_FLAG(bool, print_tokens, false, "Print scanned tokens."); | 15 DEFINE_FLAG(bool, print_tokens, false, "Print scanned tokens."); |
| 16 | 16 |
| 17 void Scanner::InitKeywordTable() { | 17 void Scanner::InitKeywordTable() { |
| 18 for (int i = 0; i < Token::numKeywords; i++) { | 18 for (int i = 0; i < Token::numKeywords; i++) { |
| 19 Token::Kind token = static_cast<Token::Kind>(Token::kFirstKeyword + i); | 19 Token::Kind token = static_cast<Token::Kind>(Token::kFirstKeyword + i); |
| 20 keywords_[i].kind = token; | 20 keywords_[i].kind = token; |
| 21 keywords_[i].keyword_chars = Token::Str(token); | 21 keywords_[i].keyword_chars = Token::Str(token); |
| 22 keywords_[i].keyword_len = strlen(Token::Str(token)); | 22 keywords_[i].keyword_len = strlen(Token::Str(token)); |
| 23 keywords_[i].keyword_symbol = NULL; | 23 keywords_[i].keyword_symbol = NULL; |
| 24 } | 24 } |
| 25 } | 25 } |
| 26 | 26 |
| 27 | 27 |
| 28 void Scanner::Reset() { |
| 29 lookahead_pos_ = 0; |
| 30 token_start_ = 0; |
| 31 c0_ = source_length_ == 0 ? '\0' : source_.CharAt(0); |
| 32 newline_seen_ = false; |
| 33 while (saved_context_ != NULL) { |
| 34 ScanContext* ctx = saved_context_; |
| 35 saved_context_ = ctx->next; |
| 36 delete ctx; |
| 37 } |
| 38 string_delimiter_ = '\0'; |
| 39 string_is_multiline_ = false; |
| 40 brace_level_ = 0; |
| 41 c0_pos_.line = 1; |
| 42 c0_pos_.column = 1; |
| 43 } |
| 44 |
| 45 |
| 28 Scanner::Scanner(const String& src, const String& private_key) | 46 Scanner::Scanner(const String& src, const String& private_key) |
| 29 : source_(src), | 47 : source_(src), |
| 30 source_length_(src.Length()), | 48 source_length_(src.Length()), |
| 31 lookahead_pos_(0), | |
| 32 token_start_(0), | |
| 33 c0_(source_length_ == 0 ? '\0' : src.CharAt(0)), | |
| 34 newline_seen_(false), | |
| 35 saved_context_(NULL), | 49 saved_context_(NULL), |
| 36 string_delimiter_('\0'), | |
| 37 string_is_multiline_(false), | |
| 38 brace_level_(0), | |
| 39 private_key_(String::ZoneHandle(private_key.raw())) { | 50 private_key_(String::ZoneHandle(private_key.raw())) { |
| 40 c0_pos_.line = 1; | 51 Reset(); |
| 41 c0_pos_.column = 1; | |
| 42 InitKeywordTable(); | 52 InitKeywordTable(); |
| 43 } | 53 } |
| 44 | 54 |
| 55 Scanner::~Scanner() { |
| 56 while (saved_context_ != NULL) { |
| 57 ScanContext* ctx = saved_context_; |
| 58 saved_context_ = ctx->next; |
| 59 delete ctx; |
| 60 } |
| 61 } |
| 62 |
| 63 |
| 45 void Scanner::ErrorMsg(const char* msg) { | 64 void Scanner::ErrorMsg(const char* msg) { |
| 46 current_token_.kind = Token::kERROR; | 65 current_token_.kind = Token::kERROR; |
| 47 current_token_.literal = &String::ZoneHandle(String::NewSymbol(msg)); | 66 current_token_.literal = &String::ZoneHandle(String::NewSymbol(msg)); |
| 48 current_token_.position = c0_pos_; | 67 current_token_.position = c0_pos_; |
| 49 token_start_ = lookahead_pos_; | 68 token_start_ = lookahead_pos_; |
| 50 current_token_.offset = lookahead_pos_; | 69 current_token_.offset = lookahead_pos_; |
| 51 } | 70 } |
| 52 | 71 |
| 53 | 72 |
| 54 void Scanner::PushContext() { | 73 void Scanner::PushContext() { |
| (...skipping 733 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 788 ErrorMsg(msg); | 807 ErrorMsg(msg); |
| 789 ReadChar(); | 808 ReadChar(); |
| 790 } | 809 } |
| 791 } | 810 } |
| 792 } while (current_token_.kind == Token::kWHITESP); | 811 } while (current_token_.kind == Token::kWHITESP); |
| 793 current_token_.length = lookahead_pos_ - token_start_; | 812 current_token_.length = lookahead_pos_ - token_start_; |
| 794 } | 813 } |
| 795 | 814 |
| 796 | 815 |
| 797 void Scanner::ScanAll(GrowableTokenStream* token_stream) { | 816 void Scanner::ScanAll(GrowableTokenStream* token_stream) { |
| 817 Reset(); |
| 798 do { | 818 do { |
| 799 Scan(); | 819 Scan(); |
| 800 token_stream->Add(current_token_); | 820 token_stream->Add(current_token_); |
| 801 } while (current_token_.kind != Token::kEOS); | 821 } while (current_token_.kind != Token::kEOS); |
| 802 } | 822 } |
| 803 | 823 |
| 824 |
| 804 void Scanner::ScanTo(intptr_t token_index) { | 825 void Scanner::ScanTo(intptr_t token_index) { |
| 805 int index = 0; | 826 int index = 0; |
| 827 Reset(); |
| 806 do { | 828 do { |
| 807 Scan(); | 829 Scan(); |
| 808 index++; | 830 index++; |
| 809 } while ((token_index >= index) && (current_token_.kind != Token::kEOS)); | 831 } while ((token_index >= index) && (current_token_.kind != Token::kEOS)); |
| 810 } | 832 } |
| 811 | 833 |
| 812 | 834 |
| 835 intptr_t Scanner::TokenIndexAtLine(intptr_t line_number) { |
| 836 ASSERT(line_number >= 0); |
| 837 Reset(); |
| 838 int token_index = 0; |
| 839 do { |
| 840 Scan(); |
| 841 if (current_token_.position.line >= line_number) { |
| 842 return token_index; |
| 843 } |
| 844 token_index++; |
| 845 } while (current_token_.kind != Token::kEOS); |
| 846 return -1; |
| 847 } |
| 848 |
| 849 |
| 850 |
| 813 const Scanner::GrowableTokenStream& Scanner::GetStream() { | 851 const Scanner::GrowableTokenStream& Scanner::GetStream() { |
| 814 GrowableTokenStream* ts = new GrowableTokenStream(128); | 852 GrowableTokenStream* ts = new GrowableTokenStream(128); |
| 815 ScanAll(ts); | 853 ScanAll(ts); |
| 816 if (FLAG_print_tokens) { | 854 if (FLAG_print_tokens) { |
| 817 Scanner::PrintTokens(*ts); | 855 Scanner::PrintTokens(*ts); |
| 818 } | 856 } |
| 819 return *ts; | 857 return *ts; |
| 820 } | 858 } |
| 821 | 859 |
| 822 | 860 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 845 "%c%"PRIxPTR, kPrivateKeySeparator, key_value); | 883 "%c%"PRIxPTR, kPrivateKeySeparator, key_value); |
| 846 const String& result = String::Handle(String::New(private_key)); | 884 const String& result = String::Handle(String::New(private_key)); |
| 847 return result.raw(); | 885 return result.raw(); |
| 848 } | 886 } |
| 849 | 887 |
| 850 | 888 |
| 851 void Scanner::InitOnce() { | 889 void Scanner::InitOnce() { |
| 852 } | 890 } |
| 853 | 891 |
| 854 } // namespace dart | 892 } // namespace dart |
| OLD | NEW |