OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 void Initialize(Handle<ExternalTwoByteString> data); | 116 void Initialize(Handle<ExternalTwoByteString> data); |
117 virtual void PushBack(uc32 ch); | 117 virtual void PushBack(uc32 ch); |
118 virtual uc32 Advance(); | 118 virtual uc32 Advance(); |
119 virtual void SeekForward(int pos); | 119 virtual void SeekForward(int pos); |
120 | 120 |
121 private: | 121 private: |
122 const uint16_t* raw_data_; | 122 const uint16_t* raw_data_; |
123 }; | 123 }; |
124 | 124 |
125 | 125 |
| 126 class KeywordMatcher { |
| 127 // Incrementally recognize keywords. |
| 128 // |
| 129 // Recognized keywords: |
| 130 // break case catch const* continue debugger* default delete do else |
| 131 // finally false for function if in instanceof native* new null |
| 132 // return switch this throw true try typeof var void while with |
| 133 // |
| 134 // *: Actually "future reserved keywords". These are the only ones we |
| 135 // recognized, the remaining are allowed as identifiers. |
| 136 public: |
| 137 KeywordMatcher() : state_(INITIAL), token_(Token::IDENTIFIER) {} |
| 138 |
| 139 Token::Value token() { return token_; } |
| 140 |
| 141 inline void AddChar(uc32 input) { |
| 142 if (state_ != UNMATCHABLE) { |
| 143 Step(input); |
| 144 } |
| 145 } |
| 146 |
| 147 void Fail() { |
| 148 token_ = Token::IDENTIFIER; |
| 149 state_ = UNMATCHABLE; |
| 150 } |
| 151 |
| 152 private: |
| 153 enum State { |
| 154 UNMATCHABLE, |
| 155 INITIAL, |
| 156 KEYWORD_PREFIX, |
| 157 KEYWORD_MATCHED, |
| 158 C, |
| 159 CA, |
| 160 CO, |
| 161 CON, |
| 162 D, |
| 163 DE, |
| 164 F, |
| 165 I, |
| 166 IN, |
| 167 N, |
| 168 T, |
| 169 TH, |
| 170 TR, |
| 171 V, |
| 172 W |
| 173 }; |
| 174 |
| 175 struct FirstState { |
| 176 const char* keyword; |
| 177 State state; |
| 178 Token::Value token; |
| 179 }; |
| 180 |
| 181 // Range of possible first characters of a keyword. |
| 182 static const unsigned int kFirstCharRangeMin = 'b'; |
| 183 static const unsigned int kFirstCharRangeMax = 'w'; |
| 184 static const unsigned int kFirstCharRangeLength = |
| 185 kFirstCharRangeMax - kFirstCharRangeMin + 1; |
| 186 // State map for first keyword character range. |
| 187 static FirstState first_states_[kFirstCharRangeLength]; |
| 188 |
| 189 // Current state. |
| 190 State state_; |
| 191 // Token for currently added characters. |
| 192 Token::Value token_; |
| 193 |
| 194 // Matching a specific keyword string (there is only one possible valid |
| 195 // keyword with the current prefix). |
| 196 const char* keyword_; |
| 197 int counter_; |
| 198 Token::Value keyword_token_; |
| 199 |
| 200 // If input equals keyword's character at position, continue matching keyword |
| 201 // from that position. |
| 202 inline bool MatchKeywordStart(uc32 input, |
| 203 const char* keyword, |
| 204 int position, |
| 205 Token::Value token_if_match) { |
| 206 if (input == keyword[position]) { |
| 207 state_ = KEYWORD_PREFIX; |
| 208 this->keyword_ = keyword; |
| 209 this->counter_ = position + 1; |
| 210 this->keyword_token_ = token_if_match; |
| 211 return true; |
| 212 } |
| 213 return false; |
| 214 } |
| 215 |
| 216 // If input equals match character, transition to new state and return true. |
| 217 inline bool MatchState(uc32 input, char match, State new_state) { |
| 218 if (input == match) { |
| 219 state_ = new_state; |
| 220 return true; |
| 221 } |
| 222 return false; |
| 223 } |
| 224 |
| 225 inline bool MatchKeyword(uc32 input, |
| 226 char match, |
| 227 State new_state, |
| 228 Token::Value keyword_token) { |
| 229 if (input == match) { // Matched "do". |
| 230 state_ = new_state; |
| 231 token_ = keyword_token; |
| 232 return true; |
| 233 } |
| 234 return false; |
| 235 } |
| 236 |
| 237 void Step(uc32 input); |
| 238 }; |
| 239 |
| 240 |
126 class Scanner { | 241 class Scanner { |
127 public: | 242 public: |
128 | 243 |
129 typedef unibrow::Utf8InputBuffer<1024> Utf8Decoder; | 244 typedef unibrow::Utf8InputBuffer<1024> Utf8Decoder; |
130 | 245 |
131 // Construction | 246 // Construction |
132 explicit Scanner(bool is_pre_parsing); | 247 explicit Scanner(bool is_pre_parsing); |
133 | 248 |
134 // Initialize the Scanner to scan source: | 249 // Initialize the Scanner to scan source: |
135 void Init(Handle<String> source, | 250 void Init(Handle<String> source, |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 } | 397 } |
283 | 398 |
284 // Decodes a unicode escape-sequence which is part of an identifier. | 399 // Decodes a unicode escape-sequence which is part of an identifier. |
285 // If the escape sequence cannot be decoded the result is kBadRune. | 400 // If the escape sequence cannot be decoded the result is kBadRune. |
286 uc32 ScanIdentifierUnicodeEscape(); | 401 uc32 ScanIdentifierUnicodeEscape(); |
287 }; | 402 }; |
288 | 403 |
289 } } // namespace v8::internal | 404 } } // namespace v8::internal |
290 | 405 |
291 #endif // V8_SCANNER_H_ | 406 #endif // V8_SCANNER_H_ |
OLD | NEW |