Revision: 20153
Author:   [email protected]
Date:     Fri Mar 21 10:34:51 2014 UTC
Log:      Move new expression parsing funcs to ParserBase.

Functions moved: ParseMemberWithNewPrefixesExpression, ParseMemberExpression,
ParseMemberExpressionContinuation.

Now all Parse*Expression functions are in ParserBase.

[email protected]
BUG=

Review URL: https://codereview.chromium.org/207633003
http://code.google.com/p/v8/source/detail?r=20153

Modified:
 /branches/bleeding_edge/src/parser.cc
 /branches/bleeding_edge/src/parser.h
 /branches/bleeding_edge/src/preparser.cc
 /branches/bleeding_edge/src/preparser.h

=======================================
--- /branches/bleeding_edge/src/parser.cc       Fri Mar 21 09:51:33 2014 UTC
+++ /branches/bleeding_edge/src/parser.cc       Fri Mar 21 10:34:51 2014 UTC
@@ -437,6 +437,17 @@
   VariableProxy* operand = expression->AsVariableProxy();
   return operand != NULL && !operand->is_this();
 }
+
+
+void ParserTraits::PushPropertyName(FuncNameInferrer* fni,
+                                    Expression* expression) {
+  if (expression->IsPropertyName()) {
+    fni->PushLiteralName(expression->AsLiteral()->AsPropertyName());
+  } else {
+    fni->PushLiteralName(
+        parser_->isolate()->factory()->anonymous_function_string());
+  }
+}


void ParserTraits::CheckAssigningFunctionLiteralToProperty(Expression* left,
@@ -748,11 +759,6 @@
name_is_strict_reserved, is_generator,
                                        function_token_position, type, ok);
 }
-
-
-Expression* ParserTraits::ParseMemberWithNewPrefixesExpression(bool* ok) {
-  return parser_->ParseMemberWithNewPrefixesExpression(ok);
-}


 Parser::Parser(CompilationInfo* info)
@@ -3051,127 +3057,6 @@
     return loop;
   }
 }
-
-
-Expression* Parser::ParseMemberWithNewPrefixesExpression(bool* ok) {
-  // NewExpression ::
-  //   ('new')+ MemberExpression
-
- // The grammar for new expressions is pretty warped. We can have several 'new' - // keywords following each other, and then a MemberExpression. When we see '(' - // after the MemberExpression, it's associated with the rightmost unassociated - // 'new' to create a NewExpression with arguments. However, a NewExpression
-  // can also occur without arguments.
-
-  // Examples of new expression:
-  // new foo.bar().baz means (new (foo.bar)()).baz
-  // new foo()() means (new foo())()
-  // new new foo()() means (new (new foo())())
-  // new new foo means new (new foo)
-  // new new foo() means new (new foo())
-  // new new foo().bar().baz means (new (new foo()).bar()).baz
-
-  if (peek() == Token::NEW) {
-    Consume(Token::NEW);
-    int new_pos = position();
-    Expression* result = ParseMemberWithNewPrefixesExpression(CHECK_OK);
-    if (peek() == Token::LPAREN) {
-      // NewExpression with arguments.
-      ZoneList<Expression*>* args = ParseArguments(CHECK_OK);
-      result = factory()->NewCallNew(result, args, new_pos);
-      // The expression can still continue with . or [ after the arguments.
-      result = ParseMemberExpressionContinuation(result, CHECK_OK);
-      return result;
-    }
-    // NewExpression without arguments.
-    return factory()->NewCallNew(
-        result, new(zone()) ZoneList<Expression*>(0, zone()), new_pos);
-  }
-  // No 'new' keyword.
-  return ParseMemberExpression(ok);
-}
-
-
-Expression* Parser::ParseMemberExpression(bool* ok) {
-  // MemberExpression ::
-  //   (PrimaryExpression | FunctionLiteral)
-  //     ('[' Expression ']' | '.' Identifier | Arguments)*
-
-  // The '[' Expression ']' and '.' Identifier parts are parsed by
- // ParseMemberExpressionContinuation, and the Arguments part is parsed by the
-  // caller.
-
-  // Parse the initial primary or function expression.
-  Expression* result = NULL;
-  if (peek() == Token::FUNCTION) {
-    Consume(Token::FUNCTION);
-    int function_token_position = position();
-    bool is_generator = allow_generators() && Check(Token::MUL);
-    Handle<String> name;
-    bool is_strict_reserved_name = false;
- Scanner::Location function_name_location = Scanner::Location::invalid();
-    FunctionLiteral::FunctionType function_type =
-        FunctionLiteral::ANONYMOUS_EXPRESSION;
-    if (peek_any_identifier()) {
-      name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name,
-                                                 CHECK_OK);
-      function_name_location = scanner()->location();
-      function_type = FunctionLiteral::NAMED_EXPRESSION;
-    }
-    result = ParseFunctionLiteral(name,
-                                  function_name_location,
-                                  is_strict_reserved_name,
-                                  is_generator,
-                                  function_token_position,
-                                  function_type,
-                                  CHECK_OK);
-  } else {
-    result = ParsePrimaryExpression(CHECK_OK);
-  }
-
-  result = ParseMemberExpressionContinuation(result, CHECK_OK);
-  return result;
-}
-
-
-Expression* Parser::ParseMemberExpressionContinuation(Expression* expression,
-                                                      bool* ok) {
-  // Parses this part of MemberExpression:
-  // ('[' Expression ']' | '.' Identifier)*
-  while (true) {
-    switch (peek()) {
-      case Token::LBRACK: {
-        Consume(Token::LBRACK);
-        int pos = position();
-        Expression* index = ParseExpression(true, CHECK_OK);
-        expression = factory()->NewProperty(expression, index, pos);
-        if (fni_ != NULL) {
-          if (index->IsPropertyName()) {
-            fni_->PushLiteralName(index->AsLiteral()->AsPropertyName());
-          } else {
-            fni_->PushLiteralName(
-                isolate()->factory()->anonymous_function_string());
-          }
-        }
-        Expect(Token::RBRACK, CHECK_OK);
-        break;
-      }
-      case Token::PERIOD: {
-        Consume(Token::PERIOD);
-        int pos = position();
-        Handle<String> name = ParseIdentifierName(CHECK_OK);
-        expression = factory()->NewProperty(
-            expression, factory()->NewLiteral(name, pos), pos);
-        if (fni_ != NULL) fni_->PushLiteralName(name);
-        break;
-      }
-      default:
-        return expression;
-    }
-  }
-  ASSERT(false);
-  return NULL;
-}


 DebuggerStatement* Parser::ParseDebuggerStatement(bool* ok) {
=======================================
--- /branches/bleeding_edge/src/parser.h        Fri Mar 21 09:51:33 2014 UTC
+++ /branches/bleeding_edge/src/parser.h        Fri Mar 21 10:34:51 2014 UTC
@@ -475,6 +475,7 @@
   static void PushLiteralName(FuncNameInferrer* fni, Handle<String> id) {
     fni->PushLiteralName(id);
   }
+  void PushPropertyName(FuncNameInferrer* fni, Expression* expression);

   static void CheckFunctionLiteralInsideTopLevelObjectLiteral(
       Scope* scope, Expression* value, bool* has_function) {
@@ -549,6 +550,7 @@
   static Literal* EmptyLiteral() {
     return NULL;
   }
+  // Used in error return values.
   static ZoneList<Expression*>* NullExpressionList() {
     return NULL;
   }
@@ -589,7 +591,6 @@
       int function_token_position,
       FunctionLiteral::FunctionType type,
       bool* ok);
-  Expression* ParseMemberWithNewPrefixesExpression(bool* ok);

  private:
   Parser* parser_;
@@ -725,10 +726,6 @@
   // Support for hamony block scoped bindings.
   Block* ParseScopedBlock(ZoneStringList* labels, bool* ok);

-  Expression* ParseMemberWithNewPrefixesExpression(bool* ok);
-  Expression* ParseMemberExpression(bool* ok);
-  Expression* ParseMemberExpressionContinuation(Expression* expression,
-                                                bool* ok);
   // Initialize the components of a for-in / for-of statement.
   void InitializeForEachStatement(ForEachStatement* stmt,
                                   Expression* each,
=======================================
--- /branches/bleeding_edge/src/preparser.cc    Fri Mar 21 09:51:33 2014 UTC
+++ /branches/bleeding_edge/src/preparser.cc    Fri Mar 21 10:34:51 2014 UTC
@@ -144,12 +144,6 @@
       name, function_name_location, name_is_strict_reserved, is_generator,
       function_token_position, type, ok);
 }
-
-
-PreParserExpression PreParserTraits::ParseMemberWithNewPrefixesExpression(
-    bool* ok) {
-  return pre_parser_->ParseMemberWithNewPrefixesExpression(ok);
-}


 PreParser::PreParseResult PreParser::PreParseLazyFunction(
@@ -841,110 +835,6 @@
   ((void)0
 #define DUMMY )  // to make indentation work
 #undef DUMMY
-
-
-PreParser::Expression PreParser::ParseMemberWithNewPrefixesExpression(
-    bool* ok) {
-  // NewExpression ::
-  //   ('new')+ MemberExpression
-
-  // See Parser::ParseNewExpression.
-
-  if (peek() == Token::NEW) {
-    Consume(Token::NEW);
-    ParseMemberWithNewPrefixesExpression(CHECK_OK);
-    Expression expression = Expression::Default();
-    if (peek() == Token::LPAREN) {
-      // NewExpression with arguments.
-      ParseArguments(CHECK_OK);
- // The expression can still continue with . or [ after the arguments. Here
-      // we need to transmit the "is valid left hand side" property of the
-      // expression.
-      expression =
- ParseMemberExpressionContinuation(Expression::Default(), CHECK_OK);
-    }
-    return expression;
-  }
-  // No 'new' keyword.
-  return ParseMemberExpression(ok);
-}
-
-
-PreParser::Expression PreParser::ParseMemberExpression(bool* ok) {
-  // MemberExpression ::
-  //   (PrimaryExpression | FunctionLiteral)
-  //     ('[' Expression ']' | '.' Identifier | Arguments)*
-
-  // The '[' Expression ']' and '.' Identifier parts are parsed by
- // ParseMemberExpressionContinuation, and the Arguments part is parsed by the
-  // caller.
-
-  // Parse the initial primary or function expression.
-  Expression result = Expression::Default();
-  if (peek() == Token::FUNCTION) {
-    Consume(Token::FUNCTION);
-    int function_token_position = position();
-    bool is_generator = allow_generators() && Check(Token::MUL);
-    Identifier name = Identifier::Default();
-    bool is_strict_reserved_name = false;
- Scanner::Location function_name_location = Scanner::Location::invalid();
-    FunctionLiteral::FunctionType function_type =
-        FunctionLiteral::ANONYMOUS_EXPRESSION;
-    if (peek_any_identifier()) {
-      name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name,
-                                                 CHECK_OK);
-      function_name_location = scanner()->location();
-      function_type = FunctionLiteral::NAMED_EXPRESSION;
-    }
-    result = ParseFunctionLiteral(name,
-                                  function_name_location,
-                                  is_strict_reserved_name,
-                                  is_generator,
-                                  function_token_position,
-                                  function_type,
-                                  CHECK_OK);
-  } else {
-    result = ParsePrimaryExpression(CHECK_OK);
-  }
-  result = ParseMemberExpressionContinuation(result, CHECK_OK);
-  return result;
-}
-
-
-PreParser::Expression PreParser::ParseMemberExpressionContinuation(
-    PreParserExpression expression, bool* ok) {
-  // Parses this part of MemberExpression:
-  // ('[' Expression ']' | '.' Identifier)*
-  while (true) {
-    switch (peek()) {
-      case Token::LBRACK: {
-        Consume(Token::LBRACK);
-        ParseExpression(true, CHECK_OK);
-        Expect(Token::RBRACK, CHECK_OK);
-        if (expression.IsThis()) {
-          expression = Expression::ThisProperty();
-        } else {
-          expression = Expression::Property();
-        }
-        break;
-      }
-      case Token::PERIOD: {
-        Consume(Token::PERIOD);
-        ParseIdentifierName(CHECK_OK);
-        if (expression.IsThis()) {
-          expression = Expression::ThisProperty();
-        } else {
-          expression = Expression::Property();
-        }
-        break;
-      }
-      default:
-        return expression;
-    }
-  }
-  ASSERT(false);
-  return PreParserExpression::Default();
-}


 PreParser::Expression PreParser::ParseFunctionLiteral(
=======================================
--- /branches/bleeding_edge/src/preparser.h     Fri Mar 21 09:51:33 2014 UTC
+++ /branches/bleeding_edge/src/preparser.h     Fri Mar 21 10:34:51 2014 UTC
@@ -420,6 +420,10 @@
   ExpressionT ParseUnaryExpression(bool* ok);
   ExpressionT ParsePostfixExpression(bool* ok);
   ExpressionT ParseLeftHandSideExpression(bool* ok);
+  ExpressionT ParseMemberWithNewPrefixesExpression(bool* ok);
+  ExpressionT ParseMemberExpression(bool* ok);
+  ExpressionT ParseMemberExpressionContinuation(ExpressionT expression,
+                                                bool* ok);

   // Used to detect duplicates in object literals. Each of the values
   // kGetterProperty, kSetterProperty and kValueProperty represents
@@ -504,6 +508,7 @@

 class PreParserIdentifier {
  public:
+  PreParserIdentifier() : type_(kUnknownIdentifier) {}
   static PreParserIdentifier Default() {
     return PreParserIdentifier(kUnknownIdentifier);
   }
@@ -791,6 +796,11 @@
                               int pos) {
     return PreParserExpression::Default();
   }
+  PreParserExpression NewCallNew(PreParserExpression expression,
+                                 PreParserExpressionList arguments,
+                                 int pos) {
+    return PreParserExpression::Default();
+  }
 };


@@ -860,7 +870,12 @@
   // operations interleaved with the recursive descent.
static void PushLiteralName(FuncNameInferrer* fni, PreParserIdentifier id) {
     // PreParser should not use FuncNameInferrer.
-    ASSERT(false);
+    UNREACHABLE();
+  }
+  static void PushPropertyName(FuncNameInferrer* fni,
+                               PreParserExpression expression) {
+    // PreParser should not use FuncNameInferrer.
+    UNREACHABLE();
   }

   static void CheckFunctionLiteralInsideTopLevelObjectLiteral(
@@ -979,7 +994,6 @@
       int function_token_position,
       FunctionLiteral::FunctionType type,
       bool* ok);
-  PreParserExpression ParseMemberWithNewPrefixesExpression(bool* ok);

  private:
   PreParser* pre_parser_;
@@ -1143,10 +1157,6 @@
   Statement ParseTryStatement(bool* ok);
   Statement ParseDebuggerStatement(bool* ok);
   Expression ParseConditionalExpression(bool accept_IN, bool* ok);
-  Expression ParseMemberExpression(bool* ok);
- Expression ParseMemberExpressionContinuation(PreParserExpression expression,
-                                               bool* ok);
-  Expression ParseMemberWithNewPrefixesExpression(bool* ok);
   Expression ParseObjectLiteral(bool* ok);
   Expression ParseV8Intrinsic(bool* ok);

@@ -1992,6 +2002,131 @@
     }
   }
 }
+
+
+template <class Traits>
+typename ParserBase<Traits>::ExpressionT
+ParserBase<Traits>::ParseMemberWithNewPrefixesExpression(bool* ok) {
+  // NewExpression ::
+  //   ('new')+ MemberExpression
+
+ // The grammar for new expressions is pretty warped. We can have several 'new' + // keywords following each other, and then a MemberExpression. When we see '(' + // after the MemberExpression, it's associated with the rightmost unassociated + // 'new' to create a NewExpression with arguments. However, a NewExpression
+  // can also occur without arguments.
+
+  // Examples of new expression:
+  // new foo.bar().baz means (new (foo.bar)()).baz
+  // new foo()() means (new foo())()
+  // new new foo()() means (new (new foo())())
+  // new new foo means new (new foo)
+  // new new foo() means new (new foo())
+  // new new foo().bar().baz means (new (new foo()).bar()).baz
+
+  if (peek() == Token::NEW) {
+    Consume(Token::NEW);
+    int new_pos = position();
+ ExpressionT result = this->ParseMemberWithNewPrefixesExpression(CHECK_OK);
+    if (peek() == Token::LPAREN) {
+      // NewExpression with arguments.
+      typename Traits::Type::ExpressionList args =
+          this->ParseArguments(CHECK_OK);
+      result = factory()->NewCallNew(result, args, new_pos);
+      // The expression can still continue with . or [ after the arguments.
+      result = this->ParseMemberExpressionContinuation(result, CHECK_OK);
+      return result;
+    }
+    // NewExpression without arguments.
+    return factory()->NewCallNew(result, this->NewExpressionList(0, zone_),
+                                 new_pos);
+  }
+  // No 'new' keyword.
+  return this->ParseMemberExpression(ok);
+}
+
+
+template <class Traits>
+typename ParserBase<Traits>::ExpressionT
+ParserBase<Traits>::ParseMemberExpression(bool* ok) {
+  // MemberExpression ::
+  //   (PrimaryExpression | FunctionLiteral)
+  //     ('[' Expression ']' | '.' Identifier | Arguments)*
+
+  // The '[' Expression ']' and '.' Identifier parts are parsed by
+ // ParseMemberExpressionContinuation, and the Arguments part is parsed by the
+  // caller.
+
+  // Parse the initial primary or function expression.
+  ExpressionT result = this->EmptyExpression();
+  if (peek() == Token::FUNCTION) {
+    Consume(Token::FUNCTION);
+    int function_token_position = position();
+    bool is_generator = allow_generators() && Check(Token::MUL);
+    IdentifierT name;
+    bool is_strict_reserved_name = false;
+ Scanner::Location function_name_location = Scanner::Location::invalid();
+    FunctionLiteral::FunctionType function_type =
+        FunctionLiteral::ANONYMOUS_EXPRESSION;
+    if (peek_any_identifier()) {
+      name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name,
+                                                 CHECK_OK);
+      function_name_location = scanner()->location();
+      function_type = FunctionLiteral::NAMED_EXPRESSION;
+    }
+    result = this->ParseFunctionLiteral(name,
+                                        function_name_location,
+                                        is_strict_reserved_name,
+                                        is_generator,
+                                        function_token_position,
+                                        function_type,
+                                        CHECK_OK);
+  } else {
+    result = ParsePrimaryExpression(CHECK_OK);
+  }
+
+  result = ParseMemberExpressionContinuation(result, CHECK_OK);
+  return result;
+}
+
+
+template <class Traits>
+typename ParserBase<Traits>::ExpressionT
+ParserBase<Traits>::ParseMemberExpressionContinuation(ExpressionT expression,
+                                                      bool* ok) {
+  // Parses this part of MemberExpression:
+  // ('[' Expression ']' | '.' Identifier)*
+  while (true) {
+    switch (peek()) {
+      case Token::LBRACK: {
+        Consume(Token::LBRACK);
+        int pos = position();
+        ExpressionT index = this->ParseExpression(true, CHECK_OK);
+        expression = factory()->NewProperty(expression, index, pos);
+        if (fni_ != NULL) {
+          this->PushPropertyName(fni_, index);
+        }
+        Expect(Token::RBRACK, CHECK_OK);
+        break;
+      }
+      case Token::PERIOD: {
+        Consume(Token::PERIOD);
+        int pos = position();
+        IdentifierT name = ParseIdentifierName(CHECK_OK);
+        expression = factory()->NewProperty(
+            expression, factory()->NewLiteral(name, pos), pos);
+        if (fni_ != NULL) {
+          this->PushLiteralName(fni_, name);
+        }
+        break;
+      }
+      default:
+        return expression;
+    }
+  }
+  ASSERT(false);
+  return this->EmptyExpression();
+}


 #undef CHECK_OK

--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to