Fix unexpected token messages in expression classifier
authorwingo <wingo@igalia.com>
Wed, 24 Jun 2015 17:25:08 +0000 (10:25 -0700)
committerCommit bot <commit-bot@chromium.org>
Wed, 24 Jun 2015 17:25:25 +0000 (17:25 +0000)
Some tokens need special messages because their token corresponds to
many names.

R=arv@chromium.org
BUG=v8:4213
LOG=N

Review URL: https://codereview.chromium.org/1207743004

Cr-Commit-Position: refs/heads/master@{#29262}

src/preparser.h
test/message/arrow-strict-eval-bare-parameter.js [new file with mode: 0644]
test/message/arrow-strict-eval-bare-parameter.out [new file with mode: 0644]

index b66053b94697a7d82a59969e1f06a6cd4fd1b3fc..0653ded54a9522bf4a68e146456680ff24b29bf2 100644 (file)
@@ -503,6 +503,10 @@ class ParserBase : public Traits {
                             error_type);
   }
 
+  void GetUnexpectedTokenMessage(
+      Token::Value token, MessageTemplate::Template* message, const char** arg,
+      MessageTemplate::Template default_ = MessageTemplate::kUnexpectedToken);
+
   void ReportUnexpectedToken(Token::Value token);
   void ReportUnexpectedTokenAt(
       Scanner::Location location, Token::Value token,
@@ -572,21 +576,26 @@ class ParserBase : public Traits {
   }
 
   void ExpressionUnexpectedToken(ExpressionClassifier* classifier) {
-    classifier->RecordExpressionError(scanner()->peek_location(),
-                                      MessageTemplate::kUnexpectedToken,
-                                      Token::String(peek()));
+    MessageTemplate::Template message = MessageTemplate::kUnexpectedToken;
+    const char* arg;
+    GetUnexpectedTokenMessage(peek(), &message, &arg);
+    classifier->RecordExpressionError(scanner()->peek_location(), message, arg);
   }
 
   void BindingPatternUnexpectedToken(ExpressionClassifier* classifier) {
-    classifier->RecordBindingPatternError(scanner()->peek_location(),
-                                          MessageTemplate::kUnexpectedToken,
-                                          Token::String(peek()));
+    MessageTemplate::Template message = MessageTemplate::kUnexpectedToken;
+    const char* arg;
+    GetUnexpectedTokenMessage(peek(), &message, &arg);
+    classifier->RecordBindingPatternError(scanner()->peek_location(), message,
+                                          arg);
   }
 
   void ArrowFormalParametersUnexpectedToken(ExpressionClassifier* classifier) {
-    classifier->RecordArrowFormalParametersError(
-        scanner()->peek_location(), MessageTemplate::kUnexpectedToken,
-        Token::String(peek()));
+    MessageTemplate::Template message = MessageTemplate::kUnexpectedToken;
+    const char* arg;
+    GetUnexpectedTokenMessage(peek(), &message, &arg);
+    classifier->RecordArrowFormalParametersError(scanner()->peek_location(),
+                                                 message, arg);
   }
 
   // Recursive descent functions:
@@ -1830,53 +1839,72 @@ ParserBase<Traits>::FunctionState::~FunctionState() {
 }
 
 
-template<class Traits>
-void ParserBase<Traits>::ReportUnexpectedToken(Token::Value token) {
-  return ReportUnexpectedTokenAt(scanner_->location(), token);
-}
-
-
 template <class Traits>
-void ParserBase<Traits>::ReportUnexpectedTokenAt(
-    Scanner::Location source_location, Token::Value token,
-    MessageTemplate::Template message) {
+void ParserBase<Traits>::GetUnexpectedTokenMessage(
+    Token::Value token, MessageTemplate::Template* message, const char** arg,
+    MessageTemplate::Template default_) {
   // Four of the tokens are treated specially
   switch (token) {
     case Token::EOS:
-      return ReportMessageAt(source_location, MessageTemplate::kUnexpectedEOS);
+      *message = MessageTemplate::kUnexpectedEOS;
+      *arg = nullptr;
+      break;
     case Token::SMI:
     case Token::NUMBER:
-      return ReportMessageAt(source_location,
-                             MessageTemplate::kUnexpectedTokenNumber);
+      *message = MessageTemplate::kUnexpectedTokenNumber;
+      *arg = nullptr;
+      break;
     case Token::STRING:
-      return ReportMessageAt(source_location,
-                             MessageTemplate::kUnexpectedTokenString);
+      *message = MessageTemplate::kUnexpectedTokenString;
+      *arg = nullptr;
+      break;
     case Token::IDENTIFIER:
-      return ReportMessageAt(source_location,
-                             MessageTemplate::kUnexpectedTokenIdentifier);
+      *message = MessageTemplate::kUnexpectedTokenIdentifier;
+      *arg = nullptr;
+      break;
     case Token::FUTURE_RESERVED_WORD:
-      return ReportMessageAt(source_location,
-                             MessageTemplate::kUnexpectedReserved);
+      *message = MessageTemplate::kUnexpectedReserved;
+      *arg = nullptr;
+      break;
     case Token::LET:
     case Token::STATIC:
     case Token::YIELD:
     case Token::FUTURE_STRICT_RESERVED_WORD:
-      return ReportMessageAt(source_location,
-                             is_strict(language_mode())
-                                 ? MessageTemplate::kUnexpectedStrictReserved
-                                 : MessageTemplate::kUnexpectedTokenIdentifier);
+      *message = is_strict(language_mode())
+                     ? MessageTemplate::kUnexpectedStrictReserved
+                     : MessageTemplate::kUnexpectedTokenIdentifier;
+      *arg = nullptr;
+      break;
     case Token::TEMPLATE_SPAN:
     case Token::TEMPLATE_TAIL:
-      return Traits::ReportMessageAt(
-          source_location, MessageTemplate::kUnexpectedTemplateString);
+      *message = MessageTemplate::kUnexpectedTemplateString;
+      *arg = nullptr;
+      break;
     default:
       const char* name = Token::String(token);
       DCHECK(name != NULL);
-      Traits::ReportMessageAt(source_location, message, name);
+      *arg = name;
+      break;
   }
 }
 
 
+template <class Traits>
+void ParserBase<Traits>::ReportUnexpectedToken(Token::Value token) {
+  return ReportUnexpectedTokenAt(scanner_->location(), token);
+}
+
+
+template <class Traits>
+void ParserBase<Traits>::ReportUnexpectedTokenAt(
+    Scanner::Location source_location, Token::Value token,
+    MessageTemplate::Template message) {
+  const char* arg;
+  GetUnexpectedTokenMessage(token, &message, &arg);
+  Traits::ReportMessageAt(source_location, message, arg);
+}
+
+
 template <class Traits>
 typename ParserBase<Traits>::IdentifierT ParserBase<Traits>::ParseIdentifier(
     AllowRestrictedIdentifiers allow_restricted_identifiers, bool* ok) {
diff --git a/test/message/arrow-strict-eval-bare-parameter.js b/test/message/arrow-strict-eval-bare-parameter.js
new file mode 100644 (file)
index 0000000..d569251
--- /dev/null
@@ -0,0 +1,8 @@
+// Copyright 2015 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+//
+// Flags: --harmony-arrow-functions
+
+"use strict";
+eval => 42
diff --git a/test/message/arrow-strict-eval-bare-parameter.out b/test/message/arrow-strict-eval-bare-parameter.out
new file mode 100644 (file)
index 0000000..9a1b996
--- /dev/null
@@ -0,0 +1,4 @@
+*%(basename)s:8: SyntaxError: Unexpected identifier
+eval => 42
+^^^^
+SyntaxError: Unexpected identifier