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

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

Issue 23452043: Introduce newline tokens into the token stream. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 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
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/flags.h" 8 #include "vm/flags.h"
9 #include "vm/object.h" 9 #include "vm/object.h"
10 #include "vm/object_store.h" 10 #include "vm/object_store.h"
(...skipping 24 matching lines...) Expand all
35 Token::Kind token = static_cast<Token::Kind>(Token::kFirstKeyword + i); 35 Token::Kind token = static_cast<Token::Kind>(Token::kFirstKeyword + i);
36 keywords_[i].kind = token; 36 keywords_[i].kind = token;
37 keywords_[i].keyword_chars = Token::Str(token); 37 keywords_[i].keyword_chars = Token::Str(token);
38 keywords_[i].keyword_len = strlen(Token::Str(token)); 38 keywords_[i].keyword_len = strlen(Token::Str(token));
39 keywords_[i].keyword_symbol = NULL; 39 keywords_[i].keyword_symbol = NULL;
40 } 40 }
41 } 41 }
42 42
43 43
44 void Scanner::Reset() { 44 void Scanner::Reset() {
45 // Non-chaning newline properties.
46 newline_token_.kind = Token::kNEWLINE;
47 newline_token_.literal = NULL;
48 newline_token_.length = 1;
49 // We don't preserve the column information.
50 newline_token_.position.column = 0;
51
45 lookahead_pos_ = -1; 52 lookahead_pos_ = -1;
46 token_start_ = 0; 53 token_start_ = 0;
47 c0_ = '\0'; 54 c0_ = '\0';
48 newline_seen_ = false; 55 newline_seen_ = 0;
56 newline_after_ = 0;
49 while (saved_context_ != NULL) { 57 while (saved_context_ != NULL) {
50 ScanContext* ctx = saved_context_; 58 ScanContext* ctx = saved_context_;
51 saved_context_ = ctx->next; 59 saved_context_ = ctx->next;
52 delete ctx; 60 delete ctx;
53 } 61 }
54 string_delimiter_ = '\0'; 62 string_delimiter_ = '\0';
55 string_is_multiline_ = false; 63 string_is_multiline_ = false;
56 brace_level_ = 0; 64 brace_level_ = 0;
57 c0_pos_.line = 1; 65 c0_pos_.line = 1;
58 c0_pos_.column = 0; 66 c0_pos_.column = 0;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 string_delimiter_ = ctx->string_delimiter; 118 string_delimiter_ = ctx->string_delimiter;
111 ASSERT(string_delimiter_ != '\0'); 119 ASSERT(string_delimiter_ != '\0');
112 string_is_multiline_ = ctx->string_is_multiline; 120 string_is_multiline_ = ctx->string_is_multiline;
113 brace_level_ = ctx->brace_level; 121 brace_level_ = ctx->brace_level;
114 delete ctx; 122 delete ctx;
115 } 123 }
116 124
117 125
118 void Scanner::BeginStringLiteral(const char delimiter) { 126 void Scanner::BeginStringLiteral(const char delimiter) {
119 string_delimiter_ = delimiter; 127 string_delimiter_ = delimiter;
128 // Save the current newline counter, since newlines observed within a
129 // multi-line string literal should be appended after the actual token.
130 newline_save_ = newline_seen_;
120 } 131 }
121 132
122 133
123 void Scanner::EndStringLiteral() { 134 void Scanner::EndStringLiteral() {
124 string_delimiter_ = '\0'; 135 string_delimiter_ = '\0';
125 string_is_multiline_ = false; 136 string_is_multiline_ = false;
137 // Update the counter for appending newlines after the actual token and
138 // restore the newline count.
139 newline_after_ = newline_seen_ - newline_save_;
140 newline_seen_ = newline_save_;
126 } 141 }
127 142
128 143
129 bool Scanner::IsLetter(int32_t c) { 144 bool Scanner::IsLetter(int32_t c) {
130 return (('A' <= c) && (c <= 'Z')) || (('a' <= c) && (c <= 'z')); 145 return (('A' <= c) && (c <= 'Z')) || (('a' <= c) && (c <= 'z'));
131 } 146 }
132 147
133 148
134 bool Scanner::IsDecimalDigit(int32_t c) { 149 bool Scanner::IsDecimalDigit(int32_t c) {
135 return '0' <= c && c <= '9'; 150 return '0' <= c && c <= '9';
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 *value = tokens[1].literal; 215 *value = tokens[1].literal;
201 return true; 216 return true;
202 } 217 }
203 return false; 218 return false;
204 } 219 }
205 220
206 221
207 void Scanner::ReadChar() { 222 void Scanner::ReadChar() {
208 if (lookahead_pos_ < source_length_) { 223 if (lookahead_pos_ < source_length_) {
209 if (c0_ == '\n') { 224 if (c0_ == '\n') {
210 newline_seen_ = true; 225 newline_seen_++;
211 c0_pos_.line++; 226 c0_pos_.line++;
212 c0_pos_.column = 0; 227 c0_pos_.column = 0;
213 if (source_.CharAt(lookahead_pos_) == '\r') { 228 if (source_.CharAt(lookahead_pos_) == '\r') {
214 // Replace a sequence of '\r' '\n' with a single '\n'. 229 // Replace a sequence of '\r' '\n' with a single '\n'.
215 if (LookaheadChar(1) == '\n') { 230 if (LookaheadChar(1) == '\n') {
216 lookahead_pos_++; 231 lookahead_pos_++;
217 } 232 }
218 } 233 }
219 } 234 }
220 lookahead_pos_++; 235 lookahead_pos_++;
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 } 607 }
593 } 608 }
594 string_chars.Add(ch1); 609 string_chars.Add(ch1);
595 } 610 }
596 ReadChar(); 611 ReadChar();
597 } 612 }
598 } 613 }
599 614
600 615
601 void Scanner::Scan() { 616 void Scanner::Scan() {
602 newline_seen_ = false; 617 newline_seen_ = 0;
618 newline_after_ = 0;
603 619
604 do { 620 do {
605 if (!IsScanningString()) { 621 if (!IsScanningString()) {
606 ConsumeWhiteSpace(); 622 ConsumeWhiteSpace();
607 } 623 }
608 token_start_ = lookahead_pos_; 624 token_start_ = lookahead_pos_;
609 current_token_.offset = lookahead_pos_; 625 current_token_.offset = lookahead_pos_;
610 current_token_.position = c0_pos_; 626 current_token_.position = c0_pos_;
611 current_token_.literal = NULL; 627 current_token_.literal = NULL;
612 current_token_.kind = Token::kILLEGAL; 628 current_token_.kind = Token::kILLEGAL;
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
858 } 874 }
859 } while (current_token_.kind == Token::kWHITESP); 875 } while (current_token_.kind == Token::kWHITESP);
860 current_token_.length = lookahead_pos_ - token_start_; 876 current_token_.length = lookahead_pos_ - token_start_;
861 } 877 }
862 878
863 879
864 void Scanner::ScanAll(GrowableTokenStream* token_stream) { 880 void Scanner::ScanAll(GrowableTokenStream* token_stream) {
865 Reset(); 881 Reset();
866 do { 882 do {
867 Scan(); 883 Scan();
884
885 // Account for all newlines that should be pushed to the stream before the
886 // actual token.
887 for (; newline_seen_ > 0; newline_seen_--) {
888 newline_token_.position.line =
889 current_token_.position.line - newline_seen_;
890 token_stream->Add(newline_token_);
891 }
892
868 token_stream->Add(current_token_); 893 token_stream->Add(current_token_);
894
895 // Account for all newlines that should be pushed after the token, i.e., all
hausner 2013/09/18 22:27:47 Why so complicated? Is it not enough to remember t
Michael Lippautz (Google) 2013/09/18 23:59:31 Done. Makes everything much simpler.
896 // newlines seen in multiline strings.
897 for (intptr_t max_after = newline_after_;
898 newline_after_ > 0;
899 newline_after_--) {
900 newline_token_.position.line =
901 current_token_.position.line + max_after - newline_after_;
902 token_stream->Add(newline_token_);
903 }
869 } while (current_token_.kind != Token::kEOS); 904 } while (current_token_.kind != Token::kEOS);
870 } 905 }
871 906
872 907
873 void Scanner::ScanTo(intptr_t token_index) { 908 void Scanner::ScanTo(intptr_t token_index) {
874 int index = 0; 909 int index = 0;
875 Reset(); 910 Reset();
876 do { 911 do {
877 Scan(); 912 Scan();
878 index++; 913 index++;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
950 "%c%#" Px "", kPrivateKeySeparator, key_value); 985 "%c%#" Px "", kPrivateKeySeparator, key_value);
951 const String& result = String::Handle(String::New(private_key, Heap::kOld)); 986 const String& result = String::Handle(String::New(private_key, Heap::kOld));
952 return result.raw(); 987 return result.raw();
953 } 988 }
954 989
955 990
956 void Scanner::InitOnce() { 991 void Scanner::InitOnce() {
957 } 992 }
958 993
959 } // namespace dart 994 } // namespace dart
OLDNEW
« runtime/vm/scanner.h ('K') | « runtime/vm/scanner.h ('k') | runtime/vm/scanner_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698