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

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: 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
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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 ASSERT(false);
864 } 874 }
875 static void MaybePushPropertyName(FuncNameInferrer* fni,
876 PreParserExpression expression) {
877 // PreParser should not use FuncNameInferrer.
878 ASSERT(false);
Michael Starzinger 2014/03/21 10:09:59 nit: s/ASSERT(false)/UNREACHABLE/ here and above.
marja 2014/03/21 10:28:24 Done.
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 static void CheckPossibleEvalCall(PreParserExpression expression, 887 static void CheckPossibleEvalCall(PreParserExpression expression,
873 PreParserScope* scope) {} 888 PreParserScope* scope) {}
874 889
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
971 // Temporary glue; these functions will move to ParserBase. 986 // Temporary glue; these functions will move to ParserBase.
972 PreParserExpression ParseV8Intrinsic(bool* ok); 987 PreParserExpression ParseV8Intrinsic(bool* ok);
973 PreParserExpression ParseFunctionLiteral( 988 PreParserExpression ParseFunctionLiteral(
974 PreParserIdentifier name, 989 PreParserIdentifier name,
975 Scanner::Location function_name_location, 990 Scanner::Location function_name_location,
976 bool name_is_strict_reserved, 991 bool name_is_strict_reserved,
977 bool is_generator, 992 bool is_generator,
978 int function_token_position, 993 int function_token_position,
979 FunctionLiteral::FunctionType type, 994 FunctionLiteral::FunctionType type,
980 bool* ok); 995 bool* ok);
981 PreParserExpression ParseMemberWithNewPrefixesExpression(bool* ok);
982 996
983 private: 997 private:
984 PreParser* pre_parser_; 998 PreParser* pre_parser_;
985 }; 999 };
986 1000
987 1001
988 // Preparsing checks a JavaScript program and emits preparse-data that helps 1002 // Preparsing checks a JavaScript program and emits preparse-data that helps
989 // a later parsing to be faster. 1003 // a later parsing to be faster.
990 // See preparse-data-format.h for the data format. 1004 // See preparse-data-format.h for the data format.
991 1005
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
1135 Statement ParseReturnStatement(bool* ok); 1149 Statement ParseReturnStatement(bool* ok);
1136 Statement ParseWithStatement(bool* ok); 1150 Statement ParseWithStatement(bool* ok);
1137 Statement ParseSwitchStatement(bool* ok); 1151 Statement ParseSwitchStatement(bool* ok);
1138 Statement ParseDoWhileStatement(bool* ok); 1152 Statement ParseDoWhileStatement(bool* ok);
1139 Statement ParseWhileStatement(bool* ok); 1153 Statement ParseWhileStatement(bool* ok);
1140 Statement ParseForStatement(bool* ok); 1154 Statement ParseForStatement(bool* ok);
1141 Statement ParseThrowStatement(bool* ok); 1155 Statement ParseThrowStatement(bool* ok);
1142 Statement ParseTryStatement(bool* ok); 1156 Statement ParseTryStatement(bool* ok);
1143 Statement ParseDebuggerStatement(bool* ok); 1157 Statement ParseDebuggerStatement(bool* ok);
1144 Expression ParseConditionalExpression(bool accept_IN, bool* ok); 1158 Expression ParseConditionalExpression(bool accept_IN, bool* ok);
1145 Expression ParseMemberExpression(bool* ok);
1146 Expression ParseMemberExpressionContinuation(PreParserExpression expression,
1147 bool* ok);
1148 Expression ParseMemberWithNewPrefixesExpression(bool* ok);
1149 Expression ParseObjectLiteral(bool* ok); 1159 Expression ParseObjectLiteral(bool* ok);
1150 Expression ParseV8Intrinsic(bool* ok); 1160 Expression ParseV8Intrinsic(bool* ok);
1151 1161
1152 Expression ParseFunctionLiteral( 1162 Expression ParseFunctionLiteral(
1153 Identifier name, 1163 Identifier name,
1154 Scanner::Location function_name_location, 1164 Scanner::Location function_name_location,
1155 bool name_is_strict_reserved, 1165 bool name_is_strict_reserved,
1156 bool is_generator, 1166 bool is_generator,
1157 int function_token_pos, 1167 int function_token_pos,
1158 FunctionLiteral::FunctionType function_type, 1168 FunctionLiteral::FunctionType function_type,
(...skipping 827 matching lines...) Expand 10 before | Expand all | Expand 10 after
1986 break; 1996 break;
1987 } 1997 }
1988 1998
1989 default: 1999 default:
1990 return result; 2000 return result;
1991 } 2001 }
1992 } 2002 }
1993 } 2003 }
1994 2004
1995 2005
2006 template <class Traits>
2007 typename ParserBase<Traits>::ExpressionT
2008 ParserBase<Traits>::ParseMemberWithNewPrefixesExpression(bool* ok) {
2009 // NewExpression ::
2010 // ('new')+ MemberExpression
2011
2012 // The grammar for new expressions is pretty warped. We can have several 'new'
2013 // keywords following each other, and then a MemberExpression. When we see '('
2014 // after the MemberExpression, it's associated with the rightmost unassociated
2015 // 'new' to create a NewExpression with arguments. However, a NewExpression
2016 // can also occur without arguments.
2017
2018 // Examples of new expression:
2019 // new foo.bar().baz means (new (foo.bar)()).baz
2020 // new foo()() means (new foo())()
2021 // new new foo()() means (new (new foo())())
2022 // new new foo means new (new foo)
2023 // new new foo() means new (new foo())
2024 // new new foo().bar().baz means (new (new foo()).bar()).baz
2025
2026 if (peek() == Token::NEW) {
2027 Consume(Token::NEW);
2028 int new_pos = position();
2029 ExpressionT result = this->ParseMemberWithNewPrefixesExpression(CHECK_OK);
2030 if (peek() == Token::LPAREN) {
2031 // NewExpression with arguments.
2032 typename Traits::Type::ExpressionList args =
2033 this->ParseArguments(CHECK_OK);
2034 result = factory()->NewCallNew(result, args, new_pos);
2035 // The expression can still continue with . or [ after the arguments.
2036 result = this->ParseMemberExpressionContinuation(result, CHECK_OK);
2037 return result;
2038 }
2039 // NewExpression without arguments.
2040 return factory()->NewCallNew(result, this->NewExpressionList(0, zone_),
2041 new_pos);
2042 }
2043 // No 'new' keyword.
2044 return this->ParseMemberExpression(ok);
2045 }
2046
2047
2048 template <class Traits>
2049 typename ParserBase<Traits>::ExpressionT
2050 ParserBase<Traits>::ParseMemberExpression(bool* ok) {
2051 // MemberExpression ::
2052 // (PrimaryExpression | FunctionLiteral)
2053 // ('[' Expression ']' | '.' Identifier | Arguments)*
2054
2055 // The '[' Expression ']' and '.' Identifier parts are parsed by
2056 // ParseMemberExpressionContinuation, and the Arguments part is parsed by the
2057 // caller.
2058
2059 // Parse the initial primary or function expression.
2060 ExpressionT result = this->EmptyExpression();
2061 if (peek() == Token::FUNCTION) {
2062 Consume(Token::FUNCTION);
2063 int function_token_position = position();
2064 bool is_generator = allow_generators() && Check(Token::MUL);
2065 IdentifierT name;
2066 bool is_strict_reserved_name = false;
2067 Scanner::Location function_name_location = Scanner::Location::invalid();
2068 FunctionLiteral::FunctionType function_type =
2069 FunctionLiteral::ANONYMOUS_EXPRESSION;
2070 if (peek_any_identifier()) {
2071 name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name,
2072 CHECK_OK);
2073 function_name_location = scanner()->location();
2074 function_type = FunctionLiteral::NAMED_EXPRESSION;
2075 }
2076 result = this->ParseFunctionLiteral(name,
2077 function_name_location,
2078 is_strict_reserved_name,
2079 is_generator,
2080 function_token_position,
2081 function_type,
2082 CHECK_OK);
2083 } else {
2084 result = ParsePrimaryExpression(CHECK_OK);
2085 }
2086
2087 result = ParseMemberExpressionContinuation(result, CHECK_OK);
2088 return result;
2089 }
2090
2091
2092 template <class Traits>
2093 typename ParserBase<Traits>::ExpressionT
2094 ParserBase<Traits>::ParseMemberExpressionContinuation(ExpressionT expression,
2095 bool* ok) {
2096 // Parses this part of MemberExpression:
2097 // ('[' Expression ']' | '.' Identifier)*
2098 while (true) {
2099 switch (peek()) {
2100 case Token::LBRACK: {
2101 Consume(Token::LBRACK);
2102 int pos = position();
2103 ExpressionT index = this->ParseExpression(true, CHECK_OK);
2104 expression = factory()->NewProperty(expression, index, pos);
2105 if (fni_ != NULL) {
2106 this->MaybePushPropertyName(fni_, index);
2107 }
2108 Expect(Token::RBRACK, CHECK_OK);
2109 break;
2110 }
2111 case Token::PERIOD: {
2112 Consume(Token::PERIOD);
2113 int pos = position();
2114 IdentifierT name = ParseIdentifierName(CHECK_OK);
2115 expression = factory()->NewProperty(
2116 expression, factory()->NewLiteral(name, pos), pos);
2117 if (fni_ != NULL) {
2118 this->PushLiteralName(fni_, name);
2119 }
2120 break;
2121 }
2122 default:
2123 return expression;
2124 }
2125 }
2126 ASSERT(false);
2127 return this->EmptyExpression();
2128 }
2129
2130
1996 #undef CHECK_OK 2131 #undef CHECK_OK
1997 #undef CHECK_OK_CUSTOM 2132 #undef CHECK_OK_CUSTOM
1998 2133
1999 2134
2000 template <typename Traits> 2135 template <typename Traits>
2001 void ParserBase<Traits>::ObjectLiteralChecker::CheckProperty( 2136 void ParserBase<Traits>::ObjectLiteralChecker::CheckProperty(
2002 Token::Value property, 2137 Token::Value property,
2003 PropertyKind type, 2138 PropertyKind type,
2004 bool* ok) { 2139 bool* ok) {
2005 int old; 2140 int old;
(...skipping 20 matching lines...) Expand all
2026 "accessor_get_set"); 2161 "accessor_get_set");
2027 } 2162 }
2028 *ok = false; 2163 *ok = false;
2029 } 2164 }
2030 } 2165 }
2031 2166
2032 2167
2033 } } // v8::internal 2168 } } // v8::internal
2034 2169
2035 #endif // V8_PREPARSER_H 2170 #endif // V8_PREPARSER_H
OLDNEW
« src/parser.h ('K') | « src/parser.cc ('k') | src/preparser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698