| OLD | NEW |
| 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 Loading... |
| 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 Loading... |
| 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 |
| 189 Isolate* isolate() const { return isolate_; } |
| 190 |
| 185 static void PrintTokens(const GrowableTokenStream& ts); | 191 static void PrintTokens(const GrowableTokenStream& ts); |
| 186 | 192 |
| 187 TokenDescriptor current_token_; // Current token. | 193 TokenDescriptor current_token_; // Current token. |
| 188 TokenDescriptor newline_token_; // Newline token. | 194 TokenDescriptor newline_token_; // Newline token. |
| 189 TokenDescriptor empty_string_token_; // Token for "". | 195 TokenDescriptor empty_string_token_; // Token for "". |
| 190 const String& source_; // The source text being tokenized. | 196 const String& source_; // The source text being tokenized. |
| 191 intptr_t source_length_; // The length of the source text. | 197 intptr_t source_length_; // The length of the source text. |
| 192 intptr_t lookahead_pos_; // Position of lookahead character | 198 intptr_t lookahead_pos_; // Position of lookahead character |
| 193 // within source_. | 199 // within source_. |
| 194 intptr_t token_start_; // Begin of current token in src_. | 200 intptr_t token_start_; // Begin of current token in src_. |
| 195 int32_t c0_; // Lookahead character. | 201 int32_t c0_; // Lookahead character. |
| 196 bool newline_seen_; // Newline before current token. | 202 bool newline_seen_; // Newline before current token. |
| 197 intptr_t prev_token_line_; // Line number of the previous token. | 203 intptr_t prev_token_line_; // Line number of the previous token. |
| 198 | 204 |
| 199 // The following fields keep track whether we are scanning a string literal | 205 // The following fields keep track whether we are scanning a string literal |
| 200 // and its interpolated expressions. | 206 // and its interpolated expressions. |
| 201 ScanContext* saved_context_; | 207 ScanContext* saved_context_; |
| 202 int32_t string_delimiter_; | 208 int32_t string_delimiter_; |
| 203 bool string_is_multiline_; | 209 bool string_is_multiline_; |
| 204 int brace_level_; | 210 int brace_level_; |
| 205 | 211 |
| 206 const String& private_key_; | 212 const String& private_key_; |
| 207 | 213 |
| 208 SourcePosition c0_pos_; // Source position of lookahead character c0_. | 214 SourcePosition c0_pos_; // Source position of lookahead character c0_. |
| 209 | 215 |
| 216 const CharAtFunc char_at_func_; |
| 217 |
| 218 Isolate* isolate_; |
| 219 |
| 210 static KeywordTable keywords_[Token::kNumKeywords]; | 220 static KeywordTable keywords_[Token::kNumKeywords]; |
| 211 static int keywords_char_offset_[kNumLowercaseChars]; | 221 static int keywords_char_offset_[kNumLowercaseChars]; |
| 212 }; | 222 }; |
| 213 | 223 |
| 214 | 224 |
| 215 } // namespace dart | 225 } // namespace dart |
| 216 | 226 |
| 217 #endif // VM_SCANNER_H_ | 227 #endif // VM_SCANNER_H_ |
| OLD | NEW |