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

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

Issue 1969563002: Eliminate GrowableTokenStream (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 7 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
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 #include "vm/token_position.h" 14 #include "vm/token_position.h"
15 15
16 namespace dart { 16 namespace dart {
17 17
18 // Forward declarations. 18 // Forward declarations.
19 class Array; 19 class Array;
20 class Library; 20 class Library;
21 class RawString; 21 class RawString;
22 class ScanContext; 22 class ScanContext;
23 class String; 23 class String;
24 24
25 // A call to Scan() scans the source one token at at time. 25 // A call to Scan() scans the source one token at at time.
26 // The scanned token is returned by cur_token(). 26 // The scanned token is returned by current_token().
27 // GetStream() scans the entire source text and returns a stream of tokens. 27 // GetStream() scans the entire source text and returns a stream of tokens.
28 class Scanner : ValueObject { 28 class Scanner : ValueObject {
29 public: 29 public:
30 typedef uint16_t (*CharAtFunc)(const String& str, intptr_t index); 30 typedef uint16_t (*CharAtFunc)(const String& str, intptr_t index);
31 31
32 // SourcePosition describes a text location in user friendly 32 // SourcePosition describes a text location in user friendly
33 // terms of line number and column. 33 // terms of line number and column.
34 struct SourcePosition { 34 struct SourcePosition {
35 int line; 35 int line;
36 int column; 36 int column;
37 }; 37 };
38 38
39 // TokenDesc defines the kind of a token and its location in 39 // TokenDesc defines the kind of a token and its location in
40 // the source text. 40 // the source text.
41 struct TokenDescriptor { 41 struct TokenDescriptor {
42 Token::Kind kind; 42 Token::Kind kind;
43 int offset; // Offset in source string. 43 int offset; // Offset in source string.
44 SourcePosition position; // Text position in source. 44 SourcePosition position; // Text position in source.
45 const String* literal; // Identifier, number or string literal. 45 const String* literal; // Identifier, number or string literal.
46 }; 46 };
47 47
48 typedef ZoneGrowableArray<TokenDescriptor> GrowableTokenStream; 48 class TokenCollector : public ValueObject {
49 public:
50 TokenCollector() { }
51 virtual ~TokenCollector() { }
52 virtual void AddToken(const TokenDescriptor& token);
53 private:
54 DISALLOW_COPY_AND_ASSIGN(TokenCollector);
55 };
49 56
50 // Initializes scanner to scan string source. 57 // Initializes scanner to scan string source.
51 Scanner(const String& source, const String& private_key); 58 Scanner(const String& source, const String& private_key);
52 ~Scanner(); 59 ~Scanner();
53 60
54 // Scans one token at a time. 61 // Scans one token at a time.
55 void Scan(); 62 void Scan();
56 63
64 // Scans the entire source and collects tokens in the provided collector.
65 void ScanAll(TokenCollector* collector);
66
57 // Scans to specified token position. 67 // Scans to specified token position.
58 // Use CurrentPosition() to extract position. 68 // Use CurrentPosition() to extract position.
59 void ScanTo(TokenPosition token_index); 69 void ScanTo(TokenPosition token_index);
60 70
61 // Scans entire source and returns a stream of tokens.
62 // Should be called only once.
63 const GrowableTokenStream& GetStream();
64
65 // Info about most recently recognized token. 71 // Info about most recently recognized token.
66 const TokenDescriptor& current_token() const { return current_token_; } 72 const TokenDescriptor& current_token() const { return current_token_; }
67 73
68 // Was there a line break before the current token? 74 // Was there a line break before the current token?
69 bool NewlineBeforeToken() const { return newline_seen_; } 75 bool NewlineBeforeToken() const { return newline_seen_; }
70 76
71 // Source code line number and column of current token. 77 // Source code line number and column of current token.
72 const SourcePosition& CurrentPosition() const { 78 const SourcePosition& CurrentPosition() const {
73 return current_token_.position; 79 return current_token_.position;
74 } 80 }
75 81
76 static void InitOnce(); 82 static void InitOnce();
77 83
78 // Return true if str is an identifier. 84 // Return true if str is an identifier.
79 bool IsIdent(const String& str); 85 bool IsIdent(const String& str);
80 86
81 // Does the token stream contain a valid literal. This is used to implement 87 // Does the token stream contain a valid integer literal.
82 // the Dart methods int.parse and double.parse. 88 static bool IsValidInteger(const String& str,
83 static bool IsValidLiteral(const Scanner::GrowableTokenStream& tokens,
84 Token::Kind literal_kind,
85 bool* is_positive, 89 bool* is_positive,
86 const String** value); 90 const String** value);
87 91
88 private: 92 private:
89 friend class ScanContext; 93 friend class ScanContext;
90 94
91 static const int kNumLowercaseChars = 26; 95 static const int kNumLowercaseChars = 26;
92 96
93 struct KeywordTable { 97 struct KeywordTable {
94 Token::Kind kind; 98 Token::Kind kind;
(...skipping 14 matching lines...) Expand all
109 // Recognizes token 'kind' and reads next character in input. 113 // Recognizes token 'kind' and reads next character in input.
110 void Recognize(Token::Kind kind) { 114 void Recognize(Token::Kind kind) {
111 ReadChar(); 115 ReadChar();
112 current_token_.kind = kind; 116 current_token_.kind = kind;
113 } 117 }
114 118
115 int32_t LookaheadChar(int how_many); 119 int32_t LookaheadChar(int how_many);
116 120
117 void ErrorMsg(const char* msg); 121 void ErrorMsg(const char* msg);
118 122
119 // Scans entire source into a given stream of tokens.
120 void ScanAll(GrowableTokenStream* token_stream);
121
122 // These functions return true if the given character is a letter, 123 // These functions return true if the given character is a letter,
123 // a decimal digit, a hexadecimal digit, etc. 124 // a decimal digit, a hexadecimal digit, etc.
124 static bool IsLetter(int32_t c); 125 static bool IsLetter(int32_t c);
125 static bool IsDecimalDigit(int32_t c); 126 static bool IsDecimalDigit(int32_t c);
126 static bool IsNumberStart(int32_t); 127 static bool IsNumberStart(int32_t);
127 static bool IsHexDigit(int32_t c); 128 static bool IsHexDigit(int32_t c);
128 static bool IsIdentStartChar(int32_t c); 129 static bool IsIdentStartChar(int32_t c);
129 static bool IsIdentChar(int32_t c); 130 static bool IsIdentChar(int32_t c);
130 131
131 // Skips up to next non-whitespace character. 132 // Skips up to next non-whitespace character.
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 // Reads a number literal. 177 // Reads a number literal.
177 void ScanNumber(bool dec_point_seen); 178 void ScanNumber(bool dec_point_seen);
178 179
179 void ScanScriptTag(); 180 void ScanScriptTag();
180 181
181 CharAtFunc CallCharAt() const { return char_at_func_; } 182 CharAtFunc CallCharAt() const { return char_at_func_; }
182 183
183 Thread* thread() const { return thread_; } 184 Thread* thread() const { return thread_; }
184 Zone* zone() const { return zone_; } 185 Zone* zone() const { return zone_; }
185 186
186 static void PrintTokens(const GrowableTokenStream& ts);
187
188 TokenDescriptor current_token_; // Current token. 187 TokenDescriptor current_token_; // Current token.
189 TokenDescriptor newline_token_; // Newline token. 188 TokenDescriptor newline_token_; // Newline token.
190 TokenDescriptor empty_string_token_; // Token for "". 189 TokenDescriptor empty_string_token_; // Token for "".
191 const String& source_; // The source text being tokenized. 190 const String& source_; // The source text being tokenized.
192 intptr_t source_length_; // The length of the source text. 191 intptr_t source_length_; // The length of the source text.
193 intptr_t lookahead_pos_; // Position of lookahead character 192 intptr_t lookahead_pos_; // Position of lookahead character
194 // within source_. 193 // within source_.
195 intptr_t token_start_; // Begin of current token in src_. 194 intptr_t token_start_; // Begin of current token in src_.
196 int32_t c0_; // Lookahead character. 195 int32_t c0_; // Lookahead character.
197 bool newline_seen_; // Newline before current token. 196 bool newline_seen_; // Newline before current token.
(...skipping 16 matching lines...) Expand all
214 Zone* zone_; 213 Zone* zone_;
215 214
216 static KeywordTable keywords_[Token::kNumKeywords]; 215 static KeywordTable keywords_[Token::kNumKeywords];
217 static int keywords_char_offset_[kNumLowercaseChars]; 216 static int keywords_char_offset_[kNumLowercaseChars];
218 }; 217 };
219 218
220 219
221 } // namespace dart 220 } // namespace dart
222 221
223 #endif // VM_SCANNER_H_ 222 #endif // VM_SCANNER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698