| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 #include "runtime.h" | 50 #include "runtime.h" |
| 51 #include "scanner-character-streams.h" | 51 #include "scanner-character-streams.h" |
| 52 #include "scopeinfo.h" | 52 #include "scopeinfo.h" |
| 53 #include "string-stream.h" | 53 #include "string-stream.h" |
| 54 | 54 |
| 55 namespace v8 { | 55 namespace v8 { |
| 56 namespace internal { | 56 namespace internal { |
| 57 | 57 |
| 58 class UnicodeCache; | 58 class UnicodeCache; |
| 59 | 59 |
| 60 struct ScannerLocation { | 60 // Base class for scanners for different encodings. The meat is the pure virtual |
| 61 ScannerLocation(int b, int e) : beg_pos(b), end_pos(e) { } | 61 // Scan() which each of them specializes. |
| 62 ScannerLocation() : beg_pos(0), end_pos(0) { } | 62 class ScannerBase { |
| 63 public: |
| 64 struct Location { |
| 65 Location(int b, int e) : beg_pos(b), end_pos(e) { } |
| 66 Location() : beg_pos(0), end_pos(0) { } |
| 63 | 67 |
| 64 bool IsValid() const { | 68 bool IsValid() const { |
| 65 return beg_pos >= 0 && end_pos >= beg_pos; | 69 return beg_pos >= 0 && end_pos >= beg_pos; |
| 70 } |
| 71 |
| 72 static Location invalid() { return Location(-1, -1); } |
| 73 |
| 74 int beg_pos; |
| 75 int end_pos; |
| 76 }; |
| 77 |
| 78 explicit ScannerBase(Isolate* isolate) |
| 79 : unicode_cache_(isolate->unicode_cache()), |
| 80 has_line_terminator_before_next_(true), |
| 81 harmony_numeric_literals_(false), |
| 82 harmony_modules_(false), |
| 83 harmony_scoping_(false) { |
| 66 } | 84 } |
| 67 | 85 |
| 68 static ScannerLocation invalid() { return ScannerLocation(-1, -1); } | 86 virtual ~ScannerBase() { } |
| 69 | |
| 70 int beg_pos; | |
| 71 int end_pos; | |
| 72 }; | |
| 73 | |
| 74 template<typename YYCTYPE> | |
| 75 class ExperimentalScanner { | |
| 76 public: | |
| 77 explicit ExperimentalScanner( | |
| 78 YYCTYPE* source, | |
| 79 YYCTYPE* source_end, | |
| 80 Isolate* isolate); | |
| 81 | |
| 82 ~ExperimentalScanner(); | |
| 83 | 87 |
| 84 // Returns the next token and advances input. | 88 // Returns the next token and advances input. |
| 85 Token::Value Next() { | 89 Token::Value Next() { |
| 86 has_line_terminator_before_next_ = false; | 90 has_line_terminator_before_next_ = false; |
| 87 current_ = next_; | 91 current_ = next_; |
| 88 Scan(); // will fill in next_. | 92 Scan(); // Virtual! Will fill in next_. |
| 89 return current_.token; | 93 return current_.token; |
| 90 } | 94 } |
| 91 | 95 |
| 92 // Returns the current token again. | 96 // Returns the current token again. |
| 93 Token::Value current_token() { return current_.token; } | 97 Token::Value current_token() { return current_.token; } |
| 94 | 98 |
| 95 // Returns the location information for the current token | 99 // Returns the location information for the current token |
| 96 // (the token last returned by Next()). | 100 // (the token last returned by Next()). |
| 97 ScannerLocation location() { | 101 Location location() { |
| 98 return ScannerLocation(current_.beg_pos, current_.end_pos); | 102 return Location(current_.beg_pos, current_.end_pos); |
| 99 } | 103 } |
| 100 | 104 |
| 101 // One token look-ahead (past the token returned by Next()). | 105 // One token look-ahead (past the token returned by Next()). |
| 102 Token::Value peek() const { return next_.token; } | 106 Token::Value peek() const { return next_.token; } |
| 103 | 107 |
| 104 ScannerLocation peek_location() const { return next_.location; } | 108 Location peek_location() const { |
| 109 return Location(next_.beg_pos, next_.end_pos); |
| 110 } |
| 105 | 111 |
| 106 UnicodeCache* unicode_cache() { return unicode_cache_; } | 112 UnicodeCache* unicode_cache() { return unicode_cache_; } |
| 107 | 113 |
| 108 bool HarmonyScoping() const { | 114 bool HarmonyScoping() const { |
| 109 return harmony_scoping_; | 115 return harmony_scoping_; |
| 110 } | 116 } |
| 111 void SetHarmonyScoping(bool scoping) { | 117 void SetHarmonyScoping(bool scoping) { |
| 112 harmony_scoping_ = scoping; | 118 harmony_scoping_ = scoping; |
| 113 } | 119 } |
| 114 bool HarmonyModules() const { | 120 bool HarmonyModules() const { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 } | 169 } |
| 164 bool is_next_contextual_keyword(Vector<const char> keyword) { | 170 bool is_next_contextual_keyword(Vector<const char> keyword) { |
| 165 return false; // FIXME | 171 return false; // FIXME |
| 166 } | 172 } |
| 167 int next_literal_length() const { | 173 int next_literal_length() const { |
| 168 return 0; // FIXME | 174 return 0; // FIXME |
| 169 } | 175 } |
| 170 | 176 |
| 171 uc32 ScanOctalEscape(uc32 c, int length) { return 0; } // FIXME | 177 uc32 ScanOctalEscape(uc32 c, int length) { return 0; } // FIXME |
| 172 | 178 |
| 173 ScannerLocation octal_position() const { | 179 Location octal_position() const { |
| 174 return ScannerLocation(0, 0); // FIXME | 180 return Location(0, 0); // FIXME |
| 175 } | 181 } |
| 176 void clear_octal_position() { } // FIXME | 182 void clear_octal_position() { } // FIXME |
| 177 | 183 |
| 178 void SeekForward(int pos) { } // FIXME | 184 void SeekForward(int pos) { } // FIXME |
| 179 | 185 |
| 180 // Scans the input as a regular expression pattern, previous | 186 // Scans the input as a regular expression pattern, previous |
| 181 // character(s) must be /(=). Returns true if a pattern is scanned. | 187 // character(s) must be /(=). Returns true if a pattern is scanned. |
| 182 bool ScanRegExpPattern(bool seen_equal) { return false; } // FIXME | 188 bool ScanRegExpPattern(bool seen_equal) { return false; } // FIXME |
| 183 // Returns true if regexp flags are scanned (always since flags can | 189 // Returns true if regexp flags are scanned (always since flags can |
| 184 // be empty). | 190 // be empty). |
| 185 bool ScanRegExpFlags() { return false; } // FIXME | 191 bool ScanRegExpFlags() { return false; } // FIXME |
| 186 | 192 |
| 187 private: | 193 protected: |
| 188 struct TokenDesc { | 194 struct TokenDesc { |
| 189 Token::Value token; | 195 Token::Value token; |
| 190 int beg_pos; | 196 int beg_pos; |
| 191 int end_pos; | 197 int end_pos; |
| 192 }; | 198 }; |
| 193 | 199 |
| 194 void Scan(); | 200 virtual void Scan() = 0; |
| 201 virtual uc32 ScanHexNumber(int length) = 0; |
| 195 | 202 |
| 196 bool ValidIdentifierStart(); | 203 bool ValidIdentifierPart() { |
| 197 bool ValidIdentifierPart(); | 204 return unicode_cache_->IsIdentifierPart(ScanHexNumber(4)); |
| 198 uc32 ScanHexNumber(int length); | 205 } |
| 206 |
| 207 bool ValidIdentifierStart() { |
| 208 return unicode_cache_->IsIdentifierStart(ScanHexNumber(4)); |
| 209 } |
| 199 | 210 |
| 200 UnicodeCache* unicode_cache_; | 211 UnicodeCache* unicode_cache_; |
| 201 | 212 |
| 202 YYCTYPE* buffer_; | |
| 203 YYCTYPE* buffer_end_; | |
| 204 YYCTYPE* start_; | |
| 205 YYCTYPE* cursor_; | |
| 206 YYCTYPE* marker_; | |
| 207 bool has_line_terminator_before_next_; | 213 bool has_line_terminator_before_next_; |
| 208 | 214 |
| 209 YYCTYPE yych; | |
| 210 | |
| 211 TokenDesc current_; // desc for current token (as returned by Next()) | 215 TokenDesc current_; // desc for current token (as returned by Next()) |
| 212 TokenDesc next_; // desc for next token (one token look-ahead) | 216 TokenDesc next_; // desc for next token (one token look-ahead) |
| 213 | 217 |
| 214 bool harmony_numeric_literals_; | 218 bool harmony_numeric_literals_; |
| 215 bool harmony_modules_; | 219 bool harmony_modules_; |
| 216 bool harmony_scoping_; | 220 bool harmony_scoping_; |
| 217 }; | 221 }; |
| 218 | 222 |
| 219 const byte* ReadFile(const char* name, Isolate* isolate, int* size, int repeat); | 223 |
| 224 template<typename YYCTYPE> |
| 225 class ExperimentalScanner : public ScannerBase { |
| 226 public: |
| 227 explicit ExperimentalScanner( |
| 228 YYCTYPE* source, |
| 229 YYCTYPE* source_end, |
| 230 Isolate* isolate); |
| 231 |
| 232 virtual ~ExperimentalScanner(); |
| 233 |
| 234 virtual void Scan(); |
| 235 virtual uc32 ScanHexNumber(int length); |
| 236 |
| 237 private: |
| 238 YYCTYPE yych; |
| 239 YYCTYPE* buffer_; |
| 240 YYCTYPE* buffer_end_; |
| 241 YYCTYPE* start_; |
| 242 YYCTYPE* cursor_; |
| 243 YYCTYPE* marker_; |
| 244 }; |
| 245 |
| 220 | 246 |
| 221 template<typename YYCTYPE> | 247 template<typename YYCTYPE> |
| 222 ExperimentalScanner<YYCTYPE>::ExperimentalScanner( | 248 ExperimentalScanner<YYCTYPE>::ExperimentalScanner( |
| 223 YYCTYPE* source, | 249 YYCTYPE* source, |
| 224 YYCTYPE* source_end, | 250 YYCTYPE* source_end, |
| 225 Isolate* isolate) | 251 Isolate* isolate) |
| 226 : unicode_cache_(isolate->unicode_cache()), | 252 : ScannerBase(isolate), |
| 227 has_line_terminator_before_next_(true), | 253 buffer_(source), |
| 228 harmony_numeric_literals_(false), | 254 buffer_end_(source_end), |
| 229 harmony_modules_(false), | 255 start_(source), |
| 230 harmony_scoping_(false) { | 256 cursor_(source), |
| 231 buffer_ = source; | 257 marker_(source) { |
| 232 buffer_end_ = source_end; | |
| 233 start_ = buffer_; | |
| 234 cursor_ = buffer_; | |
| 235 marker_ = buffer_; | |
| 236 Scan(); | 258 Scan(); |
| 237 } | 259 } |
| 238 | 260 |
| 239 | 261 |
| 240 template<typename YYCTYPE> | 262 template<typename YYCTYPE> |
| 241 ExperimentalScanner<YYCTYPE>::~ExperimentalScanner() { | 263 ExperimentalScanner<YYCTYPE>::~ExperimentalScanner() { |
| 242 delete[] buffer_; | 264 delete[] buffer_; |
| 243 } | 265 } |
| 244 | 266 |
| 245 | 267 |
| 246 template<typename YYCTYPE> | 268 template<typename YYCTYPE> |
| 247 uc32 ExperimentalScanner<YYCTYPE>::ScanHexNumber(int length) { | 269 uc32 ExperimentalScanner<YYCTYPE>::ScanHexNumber(int length) { |
| 248 // We have seen \uXXXX, let's see what it is. | 270 // We have seen \uXXXX, let's see what it is. |
| 249 // FIXME: we never end up in here if only a subset of the 4 chars are valid | 271 // FIXME: we never end up in here if only a subset of the 4 chars are valid |
| 250 // hex digits -> handle the case where they're not. | 272 // hex digits -> handle the case where they're not. |
| 251 uc32 x = 0; | 273 uc32 x = 0; |
| 252 for (YYCTYPE* s = cursor_ - length; s != cursor_; ++s) { | 274 for (YYCTYPE* s = cursor_ - length; s != cursor_; ++s) { |
| 253 int d = HexValue(*s); | 275 int d = HexValue(*s); |
| 254 if (d < 0) { | 276 if (d < 0) { |
| 255 return -1; | 277 return -1; |
| 256 } | 278 } |
| 257 x = x * 16 + d; | 279 x = x * 16 + d; |
| 258 } | 280 } |
| 259 return x; | 281 return x; |
| 260 } | 282 } |
| 261 | 283 |
| 262 | 284 |
| 263 template<typename YYCTYPE> | |
| 264 bool ExperimentalScanner<YYCTYPE>::ValidIdentifierPart() { | |
| 265 return unicode_cache_->IsIdentifierPart(ScanHexNumber(4)); | |
| 266 } | |
| 267 | |
| 268 | |
| 269 template<typename YYCTYPE> | |
| 270 bool ExperimentalScanner<YYCTYPE>::ValidIdentifierStart() { | |
| 271 return unicode_cache_->IsIdentifierStart(ScanHexNumber(4)); | |
| 272 } | |
| 273 | |
| 274 } } | 285 } } |
| 275 | 286 |
| 276 #endif // V8_LEXER_EXPERIMENTAL_SCANNER_H | 287 #endif // V8_LEXER_EXPERIMENTAL_SCANNER_H |
| OLD | NEW |