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

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

Issue 315303002: Improve scanner performance by remembering string's CharAt function as the string type does not cha… (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 6 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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 // Scanner class for the Dart language. The scanner reads source text 5 // Scanner class for the Dart language. The scanner reads source text
6 // and produces a stream of tokens which is used by the parser. 6 // and produces a stream of tokens which is used by the parser.
7 // 7 //
8 8
9 #ifndef VM_SCANNER_H_ 9 #ifndef VM_SCANNER_H_
10 #define VM_SCANNER_H_ 10 #define VM_SCANNER_H_
11 11
12 #include "vm/growable_array.h" 12 #include "vm/growable_array.h"
13 #include "vm/token.h" 13 #include "vm/token.h"
14 14
15 namespace dart { 15 namespace dart {
16 16
17 // Forward declarations. 17 // Forward declarations.
18 class Array; 18 class Array;
19 class Library; 19 class Library;
20 class RawString; 20 class RawString;
21 class String; 21 class String;
22 22
23 // A call to Scan() scans the source one token at at time. 23 // A call to Scan() scans the source one token at at time.
24 // The scanned token is returned by cur_token(). 24 // The scanned token is returned by cur_token().
25 // GetStream() scans the entire source text and returns a stream of tokens. 25 // GetStream() scans the entire source text and returns a stream of tokens.
26 class Scanner : ValueObject { 26 class Scanner : ValueObject {
27 public: 27 public:
28 typedef int32_t (*CharAtFunc)(const String& str, intptr_t index);
29
28 // SourcePosition describes a text location in user friendly 30 // SourcePosition describes a text location in user friendly
29 // terms of line number and column. 31 // terms of line number and column.
30 struct SourcePosition { 32 struct SourcePosition {
31 int line; 33 int line;
32 int column; 34 int column;
33 }; 35 };
34 36
35 // TokenDesc defines the kind of a token and its location in 37 // TokenDesc defines the kind of a token and its location in
36 // the source text. 38 // the source text.
37 struct TokenDescriptor { 39 struct TokenDescriptor {
(...skipping 30 matching lines...) Expand all
68 bool NewlineBeforeToken() const { return newline_seen_; } 70 bool NewlineBeforeToken() const { return newline_seen_; }
69 71
70 // Source code line number and column of current token. 72 // Source code line number and column of current token.
71 const SourcePosition& CurrentPosition() const { 73 const SourcePosition& CurrentPosition() const {
72 return current_token_.position; 74 return current_token_.position;
73 } 75 }
74 76
75 static void InitOnce(); 77 static void InitOnce();
76 78
77 // Return true if str is an identifier. 79 // Return true if str is an identifier.
78 static bool IsIdent(const String& str); 80 bool IsIdent(const String& str);
79 81
80 // Does the token stream contain a valid literal. This is used to implement 82 // Does the token stream contain a valid literal. This is used to implement
81 // the Dart methods int.parse and double.parse. 83 // the Dart methods int.parse and double.parse.
82 static bool IsValidLiteral(const Scanner::GrowableTokenStream& tokens, 84 static bool IsValidLiteral(const Scanner::GrowableTokenStream& tokens,
83 Token::Kind literal_kind, 85 Token::Kind literal_kind,
84 bool* is_positive, 86 bool* is_positive,
85 const String** value); 87 const String** value);
86 88
87 private: 89 private:
88 static const int kNumLowercaseChars = 26; 90 static const int kNumLowercaseChars = 26;
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 } 177 }
176 void ScanIdentNoDollar() { 178 void ScanIdentNoDollar() {
177 ScanIdentChars(false); 179 ScanIdentChars(false);
178 } 180 }
179 181
180 // Reads a number literal. 182 // Reads a number literal.
181 void ScanNumber(bool dec_point_seen); 183 void ScanNumber(bool dec_point_seen);
182 184
183 void ScanScriptTag(); 185 void ScanScriptTag();
184 186
187 CharAtFunc CallCharAt() const { return char_at_func_; }
188
185 static void PrintTokens(const GrowableTokenStream& ts); 189 static void PrintTokens(const GrowableTokenStream& ts);
186 190
187 TokenDescriptor current_token_; // Current token. 191 TokenDescriptor current_token_; // Current token.
188 TokenDescriptor newline_token_; // Newline token. 192 TokenDescriptor newline_token_; // Newline token.
189 TokenDescriptor empty_string_token_; // Token for "". 193 TokenDescriptor empty_string_token_; // Token for "".
190 const String& source_; // The source text being tokenized. 194 const String& source_; // The source text being tokenized.
191 intptr_t source_length_; // The length of the source text. 195 intptr_t source_length_; // The length of the source text.
192 intptr_t lookahead_pos_; // Position of lookahead character 196 intptr_t lookahead_pos_; // Position of lookahead character
193 // within source_. 197 // within source_.
194 intptr_t token_start_; // Begin of current token in src_. 198 intptr_t token_start_; // Begin of current token in src_.
195 int32_t c0_; // Lookahead character. 199 int32_t c0_; // Lookahead character.
196 bool newline_seen_; // Newline before current token. 200 bool newline_seen_; // Newline before current token.
197 intptr_t prev_token_line_; // Line number of the previous token. 201 intptr_t prev_token_line_; // Line number of the previous token.
198 202
199 // The following fields keep track whether we are scanning a string literal 203 // The following fields keep track whether we are scanning a string literal
200 // and its interpolated expressions. 204 // and its interpolated expressions.
201 ScanContext* saved_context_; 205 ScanContext* saved_context_;
202 int32_t string_delimiter_; 206 int32_t string_delimiter_;
203 bool string_is_multiline_; 207 bool string_is_multiline_;
204 int brace_level_; 208 int brace_level_;
205 209
206 const String& private_key_; 210 const String& private_key_;
207 211
208 SourcePosition c0_pos_; // Source position of lookahead character c0_. 212 SourcePosition c0_pos_; // Source position of lookahead character c0_.
209 213
214 const CharAtFunc char_at_func_;
215
210 static KeywordTable keywords_[Token::kNumKeywords]; 216 static KeywordTable keywords_[Token::kNumKeywords];
211 static int keywords_char_offset_[kNumLowercaseChars]; 217 static int keywords_char_offset_[kNumLowercaseChars];
212 }; 218 };
213 219
214 220
215 } // namespace dart 221 } // namespace dart
216 222
217 #endif // VM_SCANNER_H_ 223 #endif // VM_SCANNER_H_
OLDNEW
« runtime/vm/object.cc ('K') | « runtime/vm/object.cc ('k') | runtime/vm/scanner.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698