Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: src/parsing/scanner.h

Issue 2724003006: [parser] Correctly handle invalid escapes in adjacent template tokens. (Closed)
Patch Set: MoveErrorTo takes a TokenDesc* Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/parsing/parser-base.h ('k') | src/parsing/scanner.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 // This error is specifically an invalid hex or unicode escape sequence.
213 bool has_error() const { return scanner_error_ != MessageTemplate::kNone; } 213 bool has_error() const { return scanner_error_ != MessageTemplate::kNone; }
214 MessageTemplate::Template error() const { return scanner_error_; } 214 MessageTemplate::Template error() const { return scanner_error_; }
215 Location error_location() const { return scanner_error_location_; } 215 Location error_location() const { return scanner_error_location_; }
216 216
217 bool has_invalid_template_escape() const { 217 bool has_invalid_template_escape() const {
218 return invalid_template_escape_message_ != MessageTemplate::kNone; 218 return current_.invalid_template_escape_message != MessageTemplate::kNone;
219 } 219 }
220 MessageTemplate::Template invalid_template_escape_message() const { 220 MessageTemplate::Template invalid_template_escape_message() const {
221 return invalid_template_escape_message_; 221 DCHECK(has_invalid_template_escape());
222 return current_.invalid_template_escape_message;
222 } 223 }
223 Location invalid_template_escape_location() const { 224 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()); 225 DCHECK(has_invalid_template_escape());
229 invalid_template_escape_message_ = MessageTemplate::kNone; 226 return current_.invalid_template_escape_location;
230 invalid_template_escape_location_ = Location::invalid();
231 } 227 }
232 228
233 // Similar functions for the upcoming token. 229 // Similar functions for the upcoming token.
234 230
235 // One token look-ahead (past the token returned by Next()). 231 // One token look-ahead (past the token returned by Next()).
236 Token::Value peek() const { return next_.token; } 232 Token::Value peek() const { return next_.token; }
237 233
238 Location peek_location() const { return next_.location; } 234 Location peek_location() const { return next_.location; }
239 235
240 bool literal_contains_escapes() const { 236 bool literal_contains_escapes() const {
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 Handle<String> SourceMappingUrl(Isolate* isolate) const { 334 Handle<String> SourceMappingUrl(Isolate* isolate) const {
339 Handle<String> tmp; 335 Handle<String> tmp;
340 if (source_mapping_url_.length() > 0) 336 if (source_mapping_url_.length() > 0)
341 tmp = source_mapping_url_.Internalize(isolate); 337 tmp = source_mapping_url_.Internalize(isolate);
342 return tmp; 338 return tmp;
343 } 339 }
344 340
345 bool FoundHtmlComment() const { return found_html_comment_; } 341 bool FoundHtmlComment() const { return found_html_comment_; }
346 342
347 private: 343 private:
344 // Scoped helper for saving & restoring scanner error state.
345 // This is used for tagged template literals, in which normally forbidden
346 // escape sequences are allowed.
347 class ErrorState;
348
348 // Scoped helper for literal recording. Automatically drops the literal 349 // Scoped helper for literal recording. Automatically drops the literal
349 // if aborting the scanning before it's complete. 350 // if aborting the scanning before it's complete.
350 class LiteralScope { 351 class LiteralScope {
351 public: 352 public:
352 explicit LiteralScope(Scanner* self) : scanner_(self), complete_(false) { 353 explicit LiteralScope(Scanner* self) : scanner_(self), complete_(false) {
353 scanner_->StartLiteral(); 354 scanner_->StartLiteral();
354 } 355 }
355 ~LiteralScope() { 356 ~LiteralScope() {
356 if (!complete_) scanner_->DropLiteral(); 357 if (!complete_) scanner_->DropLiteral();
357 } 358 }
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 DISALLOW_COPY_AND_ASSIGN(LiteralBuffer); 451 DISALLOW_COPY_AND_ASSIGN(LiteralBuffer);
451 }; 452 };
452 453
453 // The current and look-ahead token. 454 // The current and look-ahead token.
454 struct TokenDesc { 455 struct TokenDesc {
455 Location location; 456 Location location;
456 LiteralBuffer* literal_chars; 457 LiteralBuffer* literal_chars;
457 LiteralBuffer* raw_literal_chars; 458 LiteralBuffer* raw_literal_chars;
458 uint32_t smi_value_; 459 uint32_t smi_value_;
459 Token::Value token; 460 Token::Value token;
461 MessageTemplate::Template invalid_template_escape_message;
462 Location invalid_template_escape_location;
460 }; 463 };
461 464
462 static const int kCharacterLookaheadBufferSize = 1; 465 static const int kCharacterLookaheadBufferSize = 1;
463 const int kMaxAscii = 127; 466 const int kMaxAscii = 127;
464 467
465 // Scans octal escape sequence. Also accepts "\0" decimal escape sequence. 468 // Scans octal escape sequence. Also accepts "\0" decimal escape sequence.
466 template <bool capture_raw> 469 template <bool capture_raw>
467 uc32 ScanOctalEscape(uc32 c, int length); 470 uc32 ScanOctalEscape(uc32 c, int length);
468 471
469 // Call this after setting source_ to the input. 472 // Call this after setting source_ to the input.
470 void Init() { 473 void Init() {
471 // Set c0_ (one character ahead) 474 // Set c0_ (one character ahead)
472 STATIC_ASSERT(kCharacterLookaheadBufferSize == 1); 475 STATIC_ASSERT(kCharacterLookaheadBufferSize == 1);
473 Advance(); 476 Advance();
474 // Initialize current_ to not refer to a literal. 477 // Initialize current_ to not refer to a literal.
475 current_.token = Token::UNINITIALIZED; 478 current_.token = Token::UNINITIALIZED;
476 current_.literal_chars = NULL; 479 current_.literal_chars = NULL;
477 current_.raw_literal_chars = NULL; 480 current_.raw_literal_chars = NULL;
481 current_.invalid_template_escape_message = MessageTemplate::kNone;
478 next_.token = Token::UNINITIALIZED; 482 next_.token = Token::UNINITIALIZED;
479 next_.literal_chars = NULL; 483 next_.literal_chars = NULL;
480 next_.raw_literal_chars = NULL; 484 next_.raw_literal_chars = NULL;
485 next_.invalid_template_escape_message = MessageTemplate::kNone;
481 next_next_.token = Token::UNINITIALIZED; 486 next_next_.token = Token::UNINITIALIZED;
482 next_next_.literal_chars = NULL; 487 next_next_.literal_chars = NULL;
483 next_next_.raw_literal_chars = NULL; 488 next_next_.raw_literal_chars = NULL;
489 next_next_.invalid_template_escape_message = MessageTemplate::kNone;
484 found_html_comment_ = false; 490 found_html_comment_ = false;
485 scanner_error_ = MessageTemplate::kNone; 491 scanner_error_ = MessageTemplate::kNone;
486 invalid_template_escape_message_ = MessageTemplate::kNone;
487 } 492 }
488 493
489 void ReportScannerError(const Location& location, 494 void ReportScannerError(const Location& location,
490 MessageTemplate::Template error) { 495 MessageTemplate::Template error) {
491 if (has_error()) return; 496 if (has_error()) return;
492 scanner_error_ = error; 497 scanner_error_ = error;
493 scanner_error_location_ = location; 498 scanner_error_location_ = location;
494 } 499 }
495 500
496 void ReportScannerError(int pos, MessageTemplate::Template error) { 501 void ReportScannerError(int pos, MessageTemplate::Template error) {
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after
767 // Whether there is a multi-line comment that contains a 772 // Whether there is a multi-line comment that contains a
768 // line-terminator after the current token, and before the next. 773 // line-terminator after the current token, and before the next.
769 bool has_multiline_comment_before_next_; 774 bool has_multiline_comment_before_next_;
770 bool has_line_terminator_after_next_; 775 bool has_line_terminator_after_next_;
771 776
772 // Whether this scanner encountered an HTML comment. 777 // Whether this scanner encountered an HTML comment.
773 bool found_html_comment_; 778 bool found_html_comment_;
774 779
775 MessageTemplate::Template scanner_error_; 780 MessageTemplate::Template scanner_error_;
776 Location scanner_error_location_; 781 Location scanner_error_location_;
777
778 MessageTemplate::Template invalid_template_escape_message_;
779 Location invalid_template_escape_location_;
780 }; 782 };
781 783
782 } // namespace internal 784 } // namespace internal
783 } // namespace v8 785 } // namespace v8
784 786
785 #endif // V8_PARSING_SCANNER_H_ 787 #endif // V8_PARSING_SCANNER_H_
OLDNEW
« no previous file with comments | « src/parsing/parser-base.h ('k') | src/parsing/scanner.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698