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