OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 989 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1000 return Expression::Default(); | 1000 return Expression::Default(); |
1001 } | 1001 } |
1002 return expression; | 1002 return expression; |
1003 } | 1003 } |
1004 | 1004 |
1005 | 1005 |
1006 PreParser::Expression PreParser::ParseLeftHandSideExpression(bool* ok) { | 1006 PreParser::Expression PreParser::ParseLeftHandSideExpression(bool* ok) { |
1007 // LeftHandSideExpression :: | 1007 // LeftHandSideExpression :: |
1008 // (NewExpression | MemberExpression) ... | 1008 // (NewExpression | MemberExpression) ... |
1009 | 1009 |
1010 Expression result = Expression::Default(); | 1010 Expression result = ParseMemberWithNewPrefixesExpression(CHECK_OK); |
1011 if (peek() == Token::NEW) { | |
1012 result = ParseNewExpression(CHECK_OK); | |
1013 } else { | |
1014 result = ParseMemberExpression(CHECK_OK); | |
1015 } | |
1016 | 1011 |
1017 while (true) { | 1012 while (true) { |
1018 switch (peek()) { | 1013 switch (peek()) { |
1019 case Token::LBRACK: { | 1014 case Token::LBRACK: { |
1020 Consume(Token::LBRACK); | 1015 Consume(Token::LBRACK); |
1021 ParseExpression(true, CHECK_OK); | 1016 ParseExpression(true, CHECK_OK); |
1022 Expect(Token::RBRACK, CHECK_OK); | 1017 Expect(Token::RBRACK, CHECK_OK); |
1023 if (result.IsThis()) { | 1018 if (result.IsThis()) { |
1024 result = Expression::ThisProperty(); | 1019 result = Expression::ThisProperty(); |
1025 } else { | 1020 } else { |
(...skipping 19 matching lines...) Expand all Loading... |
1045 break; | 1040 break; |
1046 } | 1041 } |
1047 | 1042 |
1048 default: | 1043 default: |
1049 return result; | 1044 return result; |
1050 } | 1045 } |
1051 } | 1046 } |
1052 } | 1047 } |
1053 | 1048 |
1054 | 1049 |
1055 PreParser::Expression PreParser::ParseNewExpression(bool* ok) { | 1050 PreParser::Expression PreParser::ParseMemberWithNewPrefixesExpression( |
| 1051 bool* ok) { |
1056 // NewExpression :: | 1052 // NewExpression :: |
1057 // ('new')+ MemberExpression | 1053 // ('new')+ MemberExpression |
1058 | 1054 |
1059 // The grammar for new expressions is pretty warped. The keyword | 1055 // See Parser::ParseNewExpression. |
1060 // 'new' can either be a part of the new expression (where it isn't | 1056 |
1061 // followed by an argument list) or a part of the member expression, | 1057 if (peek() == Token::NEW) { |
1062 // where it must be followed by an argument list. To accommodate | |
1063 // this, we parse the 'new' keywords greedily and keep track of how | |
1064 // many we have parsed. This information is then passed on to the | |
1065 // member expression parser, which is only allowed to match argument | |
1066 // lists as long as it has 'new' prefixes left | |
1067 unsigned new_count = 0; | |
1068 do { | |
1069 Consume(Token::NEW); | 1058 Consume(Token::NEW); |
1070 new_count++; | 1059 ParseMemberWithNewPrefixesExpression(CHECK_OK); |
1071 } while (peek() == Token::NEW); | 1060 if (peek() == Token::LPAREN) { |
1072 | 1061 // NewExpression with arguments. |
1073 return ParseMemberWithNewPrefixesExpression(new_count, ok); | 1062 ParseArguments(CHECK_OK); |
| 1063 // The expression can still continue with . or [ after the arguments. |
| 1064 ParseMemberExpressionContinuation(Expression::Default(), CHECK_OK); |
| 1065 } |
| 1066 return Expression::Default(); |
| 1067 } |
| 1068 // No 'new' keyword. |
| 1069 return ParseMemberExpression(ok); |
1074 } | 1070 } |
1075 | 1071 |
1076 | 1072 |
1077 PreParser::Expression PreParser::ParseMemberExpression(bool* ok) { | 1073 PreParser::Expression PreParser::ParseMemberExpression(bool* ok) { |
1078 return ParseMemberWithNewPrefixesExpression(0, ok); | |
1079 } | |
1080 | |
1081 | |
1082 PreParser::Expression PreParser::ParseMemberWithNewPrefixesExpression( | |
1083 unsigned new_count, bool* ok) { | |
1084 // MemberExpression :: | 1074 // MemberExpression :: |
1085 // (PrimaryExpression | FunctionLiteral) | 1075 // (PrimaryExpression | FunctionLiteral) |
1086 // ('[' Expression ']' | '.' Identifier | Arguments)* | 1076 // ('[' Expression ']' | '.' Identifier | Arguments)* |
1087 | 1077 |
| 1078 // The '[' Expression ']' and '.' Identifier parts are parsed by |
| 1079 // ParseMemberExpressionContinuation, and the Arguments part is parsed by the |
| 1080 // caller. |
| 1081 |
1088 // Parse the initial primary or function expression. | 1082 // Parse the initial primary or function expression. |
1089 Expression result = Expression::Default(); | 1083 Expression result = Expression::Default(); |
1090 if (peek() == Token::FUNCTION) { | 1084 if (peek() == Token::FUNCTION) { |
1091 Consume(Token::FUNCTION); | 1085 Consume(Token::FUNCTION); |
1092 | 1086 |
1093 bool is_generator = allow_generators() && Check(Token::MUL); | 1087 bool is_generator = allow_generators() && Check(Token::MUL); |
1094 Identifier name = Identifier::Default(); | 1088 Identifier name = Identifier::Default(); |
1095 bool is_strict_reserved_name = false; | 1089 bool is_strict_reserved_name = false; |
1096 Scanner::Location function_name_location = Scanner::Location::invalid(); | 1090 Scanner::Location function_name_location = Scanner::Location::invalid(); |
1097 if (peek_any_identifier()) { | 1091 if (peek_any_identifier()) { |
1098 name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name, | 1092 name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name, |
1099 CHECK_OK); | 1093 CHECK_OK); |
1100 function_name_location = scanner()->location(); | 1094 function_name_location = scanner()->location(); |
1101 } | 1095 } |
1102 result = ParseFunctionLiteral(name, | 1096 result = ParseFunctionLiteral(name, |
1103 function_name_location, | 1097 function_name_location, |
1104 is_strict_reserved_name, | 1098 is_strict_reserved_name, |
1105 is_generator, | 1099 is_generator, |
1106 CHECK_OK); | 1100 CHECK_OK); |
1107 } else { | 1101 } else { |
1108 result = ParsePrimaryExpression(CHECK_OK); | 1102 result = ParsePrimaryExpression(CHECK_OK); |
1109 } | 1103 } |
| 1104 result = ParseMemberExpressionContinuation(result, CHECK_OK); |
| 1105 return result; |
| 1106 } |
1110 | 1107 |
| 1108 |
| 1109 PreParser::Expression PreParser::ParseMemberExpressionContinuation( |
| 1110 PreParserExpression expression, bool* ok) { |
| 1111 // Parses this part of MemberExpression: |
| 1112 // ('[' Expression ']' | '.' Identifier)* |
1111 while (true) { | 1113 while (true) { |
1112 switch (peek()) { | 1114 switch (peek()) { |
1113 case Token::LBRACK: { | 1115 case Token::LBRACK: { |
1114 Consume(Token::LBRACK); | 1116 Consume(Token::LBRACK); |
1115 ParseExpression(true, CHECK_OK); | 1117 ParseExpression(true, CHECK_OK); |
1116 Expect(Token::RBRACK, CHECK_OK); | 1118 Expect(Token::RBRACK, CHECK_OK); |
1117 if (result.IsThis()) { | 1119 if (expression.IsThis()) { |
1118 result = Expression::ThisProperty(); | 1120 expression = Expression::ThisProperty(); |
1119 } else { | 1121 } else { |
1120 result = Expression::Default(); | 1122 expression = Expression::Default(); |
1121 } | 1123 } |
1122 break; | 1124 break; |
1123 } | 1125 } |
1124 case Token::PERIOD: { | 1126 case Token::PERIOD: { |
1125 Consume(Token::PERIOD); | 1127 Consume(Token::PERIOD); |
1126 ParseIdentifierName(CHECK_OK); | 1128 ParseIdentifierName(CHECK_OK); |
1127 if (result.IsThis()) { | 1129 if (expression.IsThis()) { |
1128 result = Expression::ThisProperty(); | 1130 expression = Expression::ThisProperty(); |
1129 } else { | 1131 } else { |
1130 result = Expression::Default(); | 1132 expression = Expression::Default(); |
1131 } | 1133 } |
1132 break; | 1134 break; |
1133 } | 1135 } |
1134 case Token::LPAREN: { | |
1135 if (new_count == 0) return result; | |
1136 // Consume one of the new prefixes (already parsed). | |
1137 ParseArguments(CHECK_OK); | |
1138 new_count--; | |
1139 result = Expression::Default(); | |
1140 break; | |
1141 } | |
1142 default: | 1136 default: |
1143 return result; | 1137 return expression; |
1144 } | 1138 } |
1145 } | 1139 } |
| 1140 ASSERT(false); |
| 1141 return PreParserExpression::Default(); |
1146 } | 1142 } |
1147 | 1143 |
1148 | 1144 |
1149 PreParser::Expression PreParser::ParseArrayLiteral(bool* ok) { | 1145 PreParser::Expression PreParser::ParseArrayLiteral(bool* ok) { |
1150 // ArrayLiteral :: | 1146 // ArrayLiteral :: |
1151 // '[' Expression? (',' Expression?)* ']' | 1147 // '[' Expression? (',' Expression?)* ']' |
1152 Expect(Token::LBRACK, CHECK_OK); | 1148 Expect(Token::LBRACK, CHECK_OK); |
1153 while (peek() != Token::RBRACK) { | 1149 while (peek() != Token::RBRACK) { |
1154 if (peek() != Token::COMMA) { | 1150 if (peek() != Token::COMMA) { |
1155 ParseAssignmentExpression(true, CHECK_OK); | 1151 ParseAssignmentExpression(true, CHECK_OK); |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1420 int identifier_pos = position(); | 1416 int identifier_pos = position(); |
1421 if (scanner()->is_literal_ascii()) { | 1417 if (scanner()->is_literal_ascii()) { |
1422 log_->LogAsciiSymbol(identifier_pos, scanner()->literal_ascii_string()); | 1418 log_->LogAsciiSymbol(identifier_pos, scanner()->literal_ascii_string()); |
1423 } else { | 1419 } else { |
1424 log_->LogUtf16Symbol(identifier_pos, scanner()->literal_utf16_string()); | 1420 log_->LogUtf16Symbol(identifier_pos, scanner()->literal_utf16_string()); |
1425 } | 1421 } |
1426 } | 1422 } |
1427 | 1423 |
1428 | 1424 |
1429 } } // v8::internal | 1425 } } // v8::internal |
OLD | NEW |