OLD | NEW |
---|---|
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Features shared by parsing and pre-parsing scanners. | 5 // Features shared by parsing and pre-parsing scanners. |
6 | 6 |
7 #ifndef V8_PARSING_SCANNER_H_ | 7 #ifndef V8_PARSING_SCANNER_H_ |
8 #define V8_PARSING_SCANNER_H_ | 8 #define V8_PARSING_SCANNER_H_ |
9 | 9 |
10 #include "src/allocation.h" | 10 #include "src/allocation.h" |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 // Returns the next token and advances input. | 202 // Returns the next token and advances input. |
203 Token::Value Next(); | 203 Token::Value Next(); |
204 // Returns the token following peek() | 204 // Returns the token following peek() |
205 Token::Value PeekAhead(); | 205 Token::Value PeekAhead(); |
206 // Returns the current token again. | 206 // Returns the current token again. |
207 Token::Value current_token() { return current_.token; } | 207 Token::Value current_token() { return current_.token; } |
208 // Returns the location information for the current token | 208 // Returns the location information for the current token |
209 // (the token last returned by Next()). | 209 // (the token last returned by Next()). |
210 Location location() const { return current_.location; } | 210 Location location() const { return current_.location; } |
211 | 211 |
212 // This error is specifically an invalid hex or unicode escape sequence. | |
212 bool has_error() const { return scanner_error_ != MessageTemplate::kNone; } | 213 bool has_error() const { return scanner_error_ != MessageTemplate::kNone; } |
213 MessageTemplate::Template error() const { return scanner_error_; } | 214 MessageTemplate::Template error() const { return scanner_error_; } |
214 Location error_location() const { return scanner_error_location_; } | 215 Location error_location() const { return scanner_error_location_; } |
215 | 216 |
217 bool has_invalid_template_escape() const { | |
218 return invalid_template_escape_message_ != MessageTemplate::kNone; | |
219 } | |
220 MessageTemplate::Template invalid_template_escape_message() const { | |
221 return invalid_template_escape_message_; | |
222 } | |
223 Location invalid_template_escape_location() const { | |
224 return invalid_template_escape_location_; | |
225 } | |
226 | |
227 void clear_invalid_template_escape() { | |
228 DCHECK(has_invalid_template_escape()); | |
229 invalid_template_escape_message_ = MessageTemplate::kNone; | |
230 invalid_template_escape_location_ = Location::invalid(); | |
231 } | |
232 | |
216 // Similar functions for the upcoming token. | 233 // Similar functions for the upcoming token. |
217 | 234 |
218 // One token look-ahead (past the token returned by Next()). | 235 // One token look-ahead (past the token returned by Next()). |
219 Token::Value peek() const { return next_.token; } | 236 Token::Value peek() const { return next_.token; } |
220 | 237 |
221 Location peek_location() const { return next_.location; } | 238 Location peek_location() const { return next_.location; } |
222 | 239 |
223 bool literal_contains_escapes() const { | 240 bool literal_contains_escapes() const { |
224 return LiteralContainsEscapes(current_); | 241 return LiteralContainsEscapes(current_); |
225 } | 242 } |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
338 ~LiteralScope() { | 355 ~LiteralScope() { |
339 if (!complete_) scanner_->DropLiteral(); | 356 if (!complete_) scanner_->DropLiteral(); |
340 } | 357 } |
341 void Complete() { complete_ = true; } | 358 void Complete() { complete_ = true; } |
342 | 359 |
343 private: | 360 private: |
344 Scanner* scanner_; | 361 Scanner* scanner_; |
345 bool complete_; | 362 bool complete_; |
346 }; | 363 }; |
347 | 364 |
365 // Scoped helper for saving & restoring scanner error state. | |
366 // This is used for tagged template literals, in which normally forbidden | |
367 // escape sequences are allowed. | |
368 class ErrorState { | |
vogelheim
2017/02/22 10:03:02
One more nitpick: I'd prefer if this moved to scan
bakkot1
2017/02/22 20:23:53
Sounds good to me! I'm always happier having less
| |
369 public: | |
370 ErrorState(MessageTemplate::Template* message_stack, | |
371 Location* location_stack) | |
372 : message_stack_(message_stack), | |
373 old_message_(*message_stack), | |
374 location_stack_(location_stack), | |
375 old_location_(*location_stack) { | |
376 *message_stack_ = MessageTemplate::kNone; | |
377 *location_stack_ = Location::invalid(); | |
378 } | |
379 | |
380 ~ErrorState() { | |
381 *message_stack_ = old_message_; | |
382 *location_stack_ = old_location_; | |
383 } | |
384 | |
385 void MoveErrorTo(MessageTemplate::Template* message_dest, | |
386 Location* location_dest) { | |
387 if (*message_stack_ == MessageTemplate::kNone) { | |
388 return; | |
389 } | |
390 if (*message_dest == MessageTemplate::kNone) { | |
391 *message_dest = *message_stack_; | |
392 *location_dest = *location_stack_; | |
393 } | |
394 *message_stack_ = MessageTemplate::kNone; | |
395 *location_stack_ = Location::invalid(); | |
396 } | |
397 | |
398 private: | |
399 MessageTemplate::Template* const message_stack_; | |
400 MessageTemplate::Template const old_message_; | |
401 Location* const location_stack_; | |
402 Location const old_location_; | |
403 }; | |
404 | |
348 // LiteralBuffer - Collector of chars of literals. | 405 // LiteralBuffer - Collector of chars of literals. |
349 class LiteralBuffer { | 406 class LiteralBuffer { |
350 public: | 407 public: |
351 LiteralBuffer() : is_one_byte_(true), position_(0), backing_store_() {} | 408 LiteralBuffer() : is_one_byte_(true), position_(0), backing_store_() {} |
352 | 409 |
353 ~LiteralBuffer() { backing_store_.Dispose(); } | 410 ~LiteralBuffer() { backing_store_.Dispose(); } |
354 | 411 |
355 INLINE(void AddChar(char code_unit)) { | 412 INLINE(void AddChar(char code_unit)) { |
356 DCHECK(IsValidAscii(code_unit)); | 413 DCHECK(IsValidAscii(code_unit)); |
357 AddOneByteChar(static_cast<byte>(code_unit)); | 414 AddOneByteChar(static_cast<byte>(code_unit)); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
459 current_.literal_chars = NULL; | 516 current_.literal_chars = NULL; |
460 current_.raw_literal_chars = NULL; | 517 current_.raw_literal_chars = NULL; |
461 next_.token = Token::UNINITIALIZED; | 518 next_.token = Token::UNINITIALIZED; |
462 next_.literal_chars = NULL; | 519 next_.literal_chars = NULL; |
463 next_.raw_literal_chars = NULL; | 520 next_.raw_literal_chars = NULL; |
464 next_next_.token = Token::UNINITIALIZED; | 521 next_next_.token = Token::UNINITIALIZED; |
465 next_next_.literal_chars = NULL; | 522 next_next_.literal_chars = NULL; |
466 next_next_.raw_literal_chars = NULL; | 523 next_next_.raw_literal_chars = NULL; |
467 found_html_comment_ = false; | 524 found_html_comment_ = false; |
468 scanner_error_ = MessageTemplate::kNone; | 525 scanner_error_ = MessageTemplate::kNone; |
526 invalid_template_escape_message_ = MessageTemplate::kNone; | |
469 } | 527 } |
470 | 528 |
471 void ReportScannerError(const Location& location, | 529 void ReportScannerError(const Location& location, |
472 MessageTemplate::Template error) { | 530 MessageTemplate::Template error) { |
473 if (has_error()) return; | 531 if (has_error()) return; |
474 scanner_error_ = error; | 532 scanner_error_ = error; |
475 scanner_error_location_ = location; | 533 scanner_error_location_ = location; |
476 } | 534 } |
477 | 535 |
478 void ReportScannerError(int pos, MessageTemplate::Template error) { | 536 void ReportScannerError(int pos, MessageTemplate::Template error) { |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
749 // Whether there is a multi-line comment that contains a | 807 // Whether there is a multi-line comment that contains a |
750 // line-terminator after the current token, and before the next. | 808 // line-terminator after the current token, and before the next. |
751 bool has_multiline_comment_before_next_; | 809 bool has_multiline_comment_before_next_; |
752 bool has_line_terminator_after_next_; | 810 bool has_line_terminator_after_next_; |
753 | 811 |
754 // Whether this scanner encountered an HTML comment. | 812 // Whether this scanner encountered an HTML comment. |
755 bool found_html_comment_; | 813 bool found_html_comment_; |
756 | 814 |
757 MessageTemplate::Template scanner_error_; | 815 MessageTemplate::Template scanner_error_; |
758 Location scanner_error_location_; | 816 Location scanner_error_location_; |
817 | |
818 MessageTemplate::Template invalid_template_escape_message_; | |
819 Location invalid_template_escape_location_; | |
759 }; | 820 }; |
760 | 821 |
761 } // namespace internal | 822 } // namespace internal |
762 } // namespace v8 | 823 } // namespace v8 |
763 | 824 |
764 #endif // V8_PARSING_SCANNER_H_ | 825 #endif // V8_PARSING_SCANNER_H_ |
OLD | NEW |