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

Side by Side Diff: src/preparser.h

Issue 207633003: Move new expression parsing funcs to ParserBase. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: code review Created 6 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 | Annotate | Revision Log
« no previous file with comments | « src/parser.cc ('k') | src/preparser.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 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 402 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 ExpressionT ParseArrayLiteral(bool* ok); 413 ExpressionT ParseArrayLiteral(bool* ok);
414 ExpressionT ParseObjectLiteral(bool* ok); 414 ExpressionT ParseObjectLiteral(bool* ok);
415 typename Traits::Type::ExpressionList ParseArguments(bool* ok); 415 typename Traits::Type::ExpressionList ParseArguments(bool* ok);
416 ExpressionT ParseAssignmentExpression(bool accept_IN, bool* ok); 416 ExpressionT ParseAssignmentExpression(bool accept_IN, bool* ok);
417 ExpressionT ParseYieldExpression(bool* ok); 417 ExpressionT ParseYieldExpression(bool* ok);
418 ExpressionT ParseConditionalExpression(bool accept_IN, bool* ok); 418 ExpressionT ParseConditionalExpression(bool accept_IN, bool* ok);
419 ExpressionT ParseBinaryExpression(int prec, bool accept_IN, bool* ok); 419 ExpressionT ParseBinaryExpression(int prec, bool accept_IN, bool* ok);
420 ExpressionT ParseUnaryExpression(bool* ok); 420 ExpressionT ParseUnaryExpression(bool* ok);
421 ExpressionT ParsePostfixExpression(bool* ok); 421 ExpressionT ParsePostfixExpression(bool* ok);
422 ExpressionT ParseLeftHandSideExpression(bool* ok); 422 ExpressionT ParseLeftHandSideExpression(bool* ok);
423 ExpressionT ParseMemberWithNewPrefixesExpression(bool* ok);
424 ExpressionT ParseMemberExpression(bool* ok);
425 ExpressionT ParseMemberExpressionContinuation(ExpressionT expression,
426 bool* ok);
423 427
424 // Used to detect duplicates in object literals. Each of the values 428 // Used to detect duplicates in object literals. Each of the values
425 // kGetterProperty, kSetterProperty and kValueProperty represents 429 // kGetterProperty, kSetterProperty and kValueProperty represents
426 // a type of object literal property. When parsing a property, its 430 // a type of object literal property. When parsing a property, its
427 // type value is stored in the DuplicateFinder for the property name. 431 // type value is stored in the DuplicateFinder for the property name.
428 // Values are chosen so that having intersection bits means the there is 432 // Values are chosen so that having intersection bits means the there is
429 // an incompatibility. 433 // an incompatibility.
430 // I.e., you can add a getter to a property that already has a setter, since 434 // I.e., you can add a getter to a property that already has a setter, since
431 // kGetterProperty and kSetterProperty doesn't intersect, but not if it 435 // kGetterProperty and kSetterProperty doesn't intersect, but not if it
432 // already has a getter or a value. Adding the getter to an existing 436 // already has a getter or a value. Adding the getter to an existing
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
497 bool allow_natives_syntax_; 501 bool allow_natives_syntax_;
498 bool allow_generators_; 502 bool allow_generators_;
499 bool allow_for_of_; 503 bool allow_for_of_;
500 504
501 typename Traits::Type::Zone* zone_; // Only used by Parser. 505 typename Traits::Type::Zone* zone_; // Only used by Parser.
502 }; 506 };
503 507
504 508
505 class PreParserIdentifier { 509 class PreParserIdentifier {
506 public: 510 public:
511 PreParserIdentifier() : type_(kUnknownIdentifier) {}
507 static PreParserIdentifier Default() { 512 static PreParserIdentifier Default() {
508 return PreParserIdentifier(kUnknownIdentifier); 513 return PreParserIdentifier(kUnknownIdentifier);
509 } 514 }
510 static PreParserIdentifier Eval() { 515 static PreParserIdentifier Eval() {
511 return PreParserIdentifier(kEvalIdentifier); 516 return PreParserIdentifier(kEvalIdentifier);
512 } 517 }
513 static PreParserIdentifier Arguments() { 518 static PreParserIdentifier Arguments() {
514 return PreParserIdentifier(kArgumentsIdentifier); 519 return PreParserIdentifier(kArgumentsIdentifier);
515 } 520 }
516 static PreParserIdentifier FutureReserved() { 521 static PreParserIdentifier FutureReserved() {
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
784 bool is_prefix, 789 bool is_prefix,
785 PreParserExpression expression, 790 PreParserExpression expression,
786 int pos) { 791 int pos) {
787 return PreParserExpression::Default(); 792 return PreParserExpression::Default();
788 } 793 }
789 PreParserExpression NewCall(PreParserExpression expression, 794 PreParserExpression NewCall(PreParserExpression expression,
790 PreParserExpressionList arguments, 795 PreParserExpressionList arguments,
791 int pos) { 796 int pos) {
792 return PreParserExpression::Default(); 797 return PreParserExpression::Default();
793 } 798 }
799 PreParserExpression NewCallNew(PreParserExpression expression,
800 PreParserExpressionList arguments,
801 int pos) {
802 return PreParserExpression::Default();
803 }
794 }; 804 };
795 805
796 806
797 class PreParser; 807 class PreParser;
798 808
799 class PreParserTraits { 809 class PreParserTraits {
800 public: 810 public:
801 struct Type { 811 struct Type {
802 // TODO(marja): To be removed. The Traits object should contain all the data 812 // TODO(marja): To be removed. The Traits object should contain all the data
803 // it needs. 813 // it needs.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
853 } 863 }
854 864
855 static bool IsArrayIndex(PreParserIdentifier string, uint32_t* index) { 865 static bool IsArrayIndex(PreParserIdentifier string, uint32_t* index) {
856 return false; 866 return false;
857 } 867 }
858 868
859 // Functions for encapsulating the differences between parsing and preparsing; 869 // Functions for encapsulating the differences between parsing and preparsing;
860 // operations interleaved with the recursive descent. 870 // operations interleaved with the recursive descent.
861 static void PushLiteralName(FuncNameInferrer* fni, PreParserIdentifier id) { 871 static void PushLiteralName(FuncNameInferrer* fni, PreParserIdentifier id) {
862 // PreParser should not use FuncNameInferrer. 872 // PreParser should not use FuncNameInferrer.
863 ASSERT(false); 873 UNREACHABLE();
874 }
875 static void PushPropertyName(FuncNameInferrer* fni,
876 PreParserExpression expression) {
877 // PreParser should not use FuncNameInferrer.
878 UNREACHABLE();
864 } 879 }
865 880
866 static void CheckFunctionLiteralInsideTopLevelObjectLiteral( 881 static void CheckFunctionLiteralInsideTopLevelObjectLiteral(
867 PreParserScope* scope, PreParserExpression value, bool* has_function) {} 882 PreParserScope* scope, PreParserExpression value, bool* has_function) {}
868 883
869 static void CheckAssigningFunctionLiteralToProperty( 884 static void CheckAssigningFunctionLiteralToProperty(
870 PreParserExpression left, PreParserExpression right) {} 885 PreParserExpression left, PreParserExpression right) {}
871 886
872 // PreParser doesn't need to keep track of eval calls. 887 // PreParser doesn't need to keep track of eval calls.
873 static void CheckPossibleEvalCall(PreParserExpression expression, 888 static void CheckPossibleEvalCall(PreParserExpression expression,
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
972 // Temporary glue; these functions will move to ParserBase. 987 // Temporary glue; these functions will move to ParserBase.
973 PreParserExpression ParseV8Intrinsic(bool* ok); 988 PreParserExpression ParseV8Intrinsic(bool* ok);
974 PreParserExpression ParseFunctionLiteral( 989 PreParserExpression ParseFunctionLiteral(
975 PreParserIdentifier name, 990 PreParserIdentifier name,
976 Scanner::Location function_name_location, 991 Scanner::Location function_name_location,
977 bool name_is_strict_reserved, 992 bool name_is_strict_reserved,
978 bool is_generator, 993 bool is_generator,
979 int function_token_position, 994 int function_token_position,
980 FunctionLiteral::FunctionType type, 995 FunctionLiteral::FunctionType type,
981 bool* ok); 996 bool* ok);
982 PreParserExpression ParseMemberWithNewPrefixesExpression(bool* ok);
983 997
984 private: 998 private:
985 PreParser* pre_parser_; 999 PreParser* pre_parser_;
986 }; 1000 };
987 1001
988 1002
989 // Preparsing checks a JavaScript program and emits preparse-data that helps 1003 // Preparsing checks a JavaScript program and emits preparse-data that helps
990 // a later parsing to be faster. 1004 // a later parsing to be faster.
991 // See preparse-data-format.h for the data format. 1005 // See preparse-data-format.h for the data format.
992 1006
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
1136 Statement ParseReturnStatement(bool* ok); 1150 Statement ParseReturnStatement(bool* ok);
1137 Statement ParseWithStatement(bool* ok); 1151 Statement ParseWithStatement(bool* ok);
1138 Statement ParseSwitchStatement(bool* ok); 1152 Statement ParseSwitchStatement(bool* ok);
1139 Statement ParseDoWhileStatement(bool* ok); 1153 Statement ParseDoWhileStatement(bool* ok);
1140 Statement ParseWhileStatement(bool* ok); 1154 Statement ParseWhileStatement(bool* ok);
1141 Statement ParseForStatement(bool* ok); 1155 Statement ParseForStatement(bool* ok);
1142 Statement ParseThrowStatement(bool* ok); 1156 Statement ParseThrowStatement(bool* ok);
1143 Statement ParseTryStatement(bool* ok); 1157 Statement ParseTryStatement(bool* ok);
1144 Statement ParseDebuggerStatement(bool* ok); 1158 Statement ParseDebuggerStatement(bool* ok);
1145 Expression ParseConditionalExpression(bool accept_IN, bool* ok); 1159 Expression ParseConditionalExpression(bool accept_IN, bool* ok);
1146 Expression ParseMemberExpression(bool* ok);
1147 Expression ParseMemberExpressionContinuation(PreParserExpression expression,
1148 bool* ok);
1149 Expression ParseMemberWithNewPrefixesExpression(bool* ok);
1150 Expression ParseObjectLiteral(bool* ok); 1160 Expression ParseObjectLiteral(bool* ok);
1151 Expression ParseV8Intrinsic(bool* ok); 1161 Expression ParseV8Intrinsic(bool* ok);
1152 1162
1153 Expression ParseFunctionLiteral( 1163 Expression ParseFunctionLiteral(
1154 Identifier name, 1164 Identifier name,
1155 Scanner::Location function_name_location, 1165 Scanner::Location function_name_location,
1156 bool name_is_strict_reserved, 1166 bool name_is_strict_reserved,
1157 bool is_generator, 1167 bool is_generator,
1158 int function_token_pos, 1168 int function_token_pos,
1159 FunctionLiteral::FunctionType function_type, 1169 FunctionLiteral::FunctionType function_type,
(...skipping 827 matching lines...) Expand 10 before | Expand all | Expand 10 after
1987 break; 1997 break;
1988 } 1998 }
1989 1999
1990 default: 2000 default:
1991 return result; 2001 return result;
1992 } 2002 }
1993 } 2003 }
1994 } 2004 }
1995 2005
1996 2006
2007 template <class Traits>
2008 typename ParserBase<Traits>::ExpressionT
2009 ParserBase<Traits>::ParseMemberWithNewPrefixesExpression(bool* ok) {
2010 // NewExpression ::
2011 // ('new')+ MemberExpression
2012
2013 // The grammar for new expressions is pretty warped. We can have several 'new'
2014 // keywords following each other, and then a MemberExpression. When we see '('
2015 // after the MemberExpression, it's associated with the rightmost unassociated
2016 // 'new' to create a NewExpression with arguments. However, a NewExpression
2017 // can also occur without arguments.
2018
2019 // Examples of new expression:
2020 // new foo.bar().baz means (new (foo.bar)()).baz
2021 // new foo()() means (new foo())()
2022 // new new foo()() means (new (new foo())())
2023 // new new foo means new (new foo)
2024 // new new foo() means new (new foo())
2025 // new new foo().bar().baz means (new (new foo()).bar()).baz
2026
2027 if (peek() == Token::NEW) {
2028 Consume(Token::NEW);
2029 int new_pos = position();
2030 ExpressionT result = this->ParseMemberWithNewPrefixesExpression(CHECK_OK);
2031 if (peek() == Token::LPAREN) {
2032 // NewExpression with arguments.
2033 typename Traits::Type::ExpressionList args =
2034 this->ParseArguments(CHECK_OK);
2035 result = factory()->NewCallNew(result, args, new_pos);
2036 // The expression can still continue with . or [ after the arguments.
2037 result = this->ParseMemberExpressionContinuation(result, CHECK_OK);
2038 return result;
2039 }
2040 // NewExpression without arguments.
2041 return factory()->NewCallNew(result, this->NewExpressionList(0, zone_),
2042 new_pos);
2043 }
2044 // No 'new' keyword.
2045 return this->ParseMemberExpression(ok);
2046 }
2047
2048
2049 template <class Traits>
2050 typename ParserBase<Traits>::ExpressionT
2051 ParserBase<Traits>::ParseMemberExpression(bool* ok) {
2052 // MemberExpression ::
2053 // (PrimaryExpression | FunctionLiteral)
2054 // ('[' Expression ']' | '.' Identifier | Arguments)*
2055
2056 // The '[' Expression ']' and '.' Identifier parts are parsed by
2057 // ParseMemberExpressionContinuation, and the Arguments part is parsed by the
2058 // caller.
2059
2060 // Parse the initial primary or function expression.
2061 ExpressionT result = this->EmptyExpression();
2062 if (peek() == Token::FUNCTION) {
2063 Consume(Token::FUNCTION);
2064 int function_token_position = position();
2065 bool is_generator = allow_generators() && Check(Token::MUL);
2066 IdentifierT name;
2067 bool is_strict_reserved_name = false;
2068 Scanner::Location function_name_location = Scanner::Location::invalid();
2069 FunctionLiteral::FunctionType function_type =
2070 FunctionLiteral::ANONYMOUS_EXPRESSION;
2071 if (peek_any_identifier()) {
2072 name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name,
2073 CHECK_OK);
2074 function_name_location = scanner()->location();
2075 function_type = FunctionLiteral::NAMED_EXPRESSION;
2076 }
2077 result = this->ParseFunctionLiteral(name,
2078 function_name_location,
2079 is_strict_reserved_name,
2080 is_generator,
2081 function_token_position,
2082 function_type,
2083 CHECK_OK);
2084 } else {
2085 result = ParsePrimaryExpression(CHECK_OK);
2086 }
2087
2088 result = ParseMemberExpressionContinuation(result, CHECK_OK);
2089 return result;
2090 }
2091
2092
2093 template <class Traits>
2094 typename ParserBase<Traits>::ExpressionT
2095 ParserBase<Traits>::ParseMemberExpressionContinuation(ExpressionT expression,
2096 bool* ok) {
2097 // Parses this part of MemberExpression:
2098 // ('[' Expression ']' | '.' Identifier)*
2099 while (true) {
2100 switch (peek()) {
2101 case Token::LBRACK: {
2102 Consume(Token::LBRACK);
2103 int pos = position();
2104 ExpressionT index = this->ParseExpression(true, CHECK_OK);
2105 expression = factory()->NewProperty(expression, index, pos);
2106 if (fni_ != NULL) {
2107 this->PushPropertyName(fni_, index);
2108 }
2109 Expect(Token::RBRACK, CHECK_OK);
2110 break;
2111 }
2112 case Token::PERIOD: {
2113 Consume(Token::PERIOD);
2114 int pos = position();
2115 IdentifierT name = ParseIdentifierName(CHECK_OK);
2116 expression = factory()->NewProperty(
2117 expression, factory()->NewLiteral(name, pos), pos);
2118 if (fni_ != NULL) {
2119 this->PushLiteralName(fni_, name);
2120 }
2121 break;
2122 }
2123 default:
2124 return expression;
2125 }
2126 }
2127 ASSERT(false);
2128 return this->EmptyExpression();
2129 }
2130
2131
1997 #undef CHECK_OK 2132 #undef CHECK_OK
1998 #undef CHECK_OK_CUSTOM 2133 #undef CHECK_OK_CUSTOM
1999 2134
2000 2135
2001 template <typename Traits> 2136 template <typename Traits>
2002 void ParserBase<Traits>::ObjectLiteralChecker::CheckProperty( 2137 void ParserBase<Traits>::ObjectLiteralChecker::CheckProperty(
2003 Token::Value property, 2138 Token::Value property,
2004 PropertyKind type, 2139 PropertyKind type,
2005 bool* ok) { 2140 bool* ok) {
2006 int old; 2141 int old;
(...skipping 20 matching lines...) Expand all
2027 "accessor_get_set"); 2162 "accessor_get_set");
2028 } 2163 }
2029 *ok = false; 2164 *ok = false;
2030 } 2165 }
2031 } 2166 }
2032 2167
2033 2168
2034 } } // v8::internal 2169 } } // v8::internal
2035 2170
2036 #endif // V8_PREPARSER_H 2171 #endif // V8_PREPARSER_H
OLDNEW
« no previous file with comments | « src/parser.cc ('k') | src/preparser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698