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

Side by Side Diff: src/parser-base.h

Issue 1233913008: Split off ParserBase into src/parser-base.h (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Copy whole ParserBase, not just the class Created 5 years, 5 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
« no previous file with comments | « src/parser.h ('k') | src/preparser.h » ('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 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef V8_PREPARSER_H 5 #ifndef V8_PARSER_BASE_H
6 #define V8_PREPARSER_H 6 #define V8_PARSER_BASE_H
7 7
8 #include "src/v8.h" 8 #include "src/v8.h"
9 9
10 #include "src/bailout-reason.h"
11 #include "src/expression-classifier.h" 10 #include "src/expression-classifier.h"
12 #include "src/func-name-inferrer.h" 11 #include "src/func-name-inferrer.h"
13 #include "src/hashmap.h"
14 #include "src/messages.h" 12 #include "src/messages.h"
15 #include "src/scanner.h" 13 #include "src/scanner.h"
16 #include "src/scopes.h" 14 #include "src/scopes.h"
17 #include "src/token.h" 15 #include "src/token.h"
18 16
19 namespace v8 { 17 namespace v8 {
20 namespace internal { 18 namespace internal {
21 19
22
23 enum FunctionNameValidity { 20 enum FunctionNameValidity {
24 kFunctionNameIsStrictReserved, 21 kFunctionNameIsStrictReserved,
25 kSkipFunctionNameCheck, 22 kSkipFunctionNameCheck,
26 kFunctionNameValidityUnknown 23 kFunctionNameValidityUnknown
27 }; 24 };
28 25
29 26
30 // Common base class shared between parser and pre-parser. Traits encapsulate 27 // Common base class shared between parser and pre-parser. Traits encapsulate
31 // the differences between Parser and PreParser: 28 // the differences between Parser and PreParser:
32 29
(...skipping 768 matching lines...) Expand 10 before | Expand all | Expand 10 after
801 bool allow_harmony_rest_params_; 798 bool allow_harmony_rest_params_;
802 bool allow_harmony_spreadcalls_; 799 bool allow_harmony_spreadcalls_;
803 bool allow_harmony_destructuring_; 800 bool allow_harmony_destructuring_;
804 bool allow_harmony_spread_arrays_; 801 bool allow_harmony_spread_arrays_;
805 bool allow_harmony_new_target_; 802 bool allow_harmony_new_target_;
806 bool allow_strong_mode_; 803 bool allow_strong_mode_;
807 bool allow_legacy_const_; 804 bool allow_legacy_const_;
808 }; 805 };
809 806
810 807
811 class PreParserIdentifier {
812 public:
813 PreParserIdentifier() : type_(kUnknownIdentifier) {}
814 static PreParserIdentifier Default() {
815 return PreParserIdentifier(kUnknownIdentifier);
816 }
817 static PreParserIdentifier Eval() {
818 return PreParserIdentifier(kEvalIdentifier);
819 }
820 static PreParserIdentifier Arguments() {
821 return PreParserIdentifier(kArgumentsIdentifier);
822 }
823 static PreParserIdentifier Undefined() {
824 return PreParserIdentifier(kUndefinedIdentifier);
825 }
826 static PreParserIdentifier FutureReserved() {
827 return PreParserIdentifier(kFutureReservedIdentifier);
828 }
829 static PreParserIdentifier FutureStrictReserved() {
830 return PreParserIdentifier(kFutureStrictReservedIdentifier);
831 }
832 static PreParserIdentifier Let() {
833 return PreParserIdentifier(kLetIdentifier);
834 }
835 static PreParserIdentifier Static() {
836 return PreParserIdentifier(kStaticIdentifier);
837 }
838 static PreParserIdentifier Yield() {
839 return PreParserIdentifier(kYieldIdentifier);
840 }
841 static PreParserIdentifier Prototype() {
842 return PreParserIdentifier(kPrototypeIdentifier);
843 }
844 static PreParserIdentifier Constructor() {
845 return PreParserIdentifier(kConstructorIdentifier);
846 }
847 bool IsEval() const { return type_ == kEvalIdentifier; }
848 bool IsArguments() const { return type_ == kArgumentsIdentifier; }
849 bool IsEvalOrArguments() const { return IsEval() || IsArguments(); }
850 bool IsUndefined() const { return type_ == kUndefinedIdentifier; }
851 bool IsLet() const { return type_ == kLetIdentifier; }
852 bool IsStatic() const { return type_ == kStaticIdentifier; }
853 bool IsYield() const { return type_ == kYieldIdentifier; }
854 bool IsPrototype() const { return type_ == kPrototypeIdentifier; }
855 bool IsConstructor() const { return type_ == kConstructorIdentifier; }
856 bool IsFutureReserved() const { return type_ == kFutureReservedIdentifier; }
857 bool IsFutureStrictReserved() const {
858 return type_ == kFutureStrictReservedIdentifier ||
859 type_ == kLetIdentifier || type_ == kStaticIdentifier ||
860 type_ == kYieldIdentifier;
861 }
862
863 // Allow identifier->name()[->length()] to work. The preparser
864 // does not need the actual positions/lengths of the identifiers.
865 const PreParserIdentifier* operator->() const { return this; }
866 const PreParserIdentifier raw_name() const { return *this; }
867
868 int position() const { return 0; }
869 int length() const { return 0; }
870
871 private:
872 enum Type {
873 kUnknownIdentifier,
874 kFutureReservedIdentifier,
875 kFutureStrictReservedIdentifier,
876 kLetIdentifier,
877 kStaticIdentifier,
878 kYieldIdentifier,
879 kEvalIdentifier,
880 kArgumentsIdentifier,
881 kUndefinedIdentifier,
882 kPrototypeIdentifier,
883 kConstructorIdentifier
884 };
885
886 explicit PreParserIdentifier(Type type) : type_(type) {}
887 Type type_;
888
889 friend class PreParserExpression;
890 };
891
892
893 class PreParserExpression {
894 public:
895 static PreParserExpression Default() {
896 return PreParserExpression(TypeField::encode(kExpression));
897 }
898
899 static PreParserExpression Spread(PreParserExpression expression) {
900 return PreParserExpression(TypeField::encode(kSpreadExpression));
901 }
902
903 static PreParserExpression FromIdentifier(PreParserIdentifier id) {
904 return PreParserExpression(TypeField::encode(kIdentifierExpression) |
905 IdentifierTypeField::encode(id.type_));
906 }
907
908 static PreParserExpression BinaryOperation(PreParserExpression left,
909 Token::Value op,
910 PreParserExpression right) {
911 return PreParserExpression(TypeField::encode(kBinaryOperationExpression));
912 }
913
914 static PreParserExpression StringLiteral() {
915 return PreParserExpression(TypeField::encode(kStringLiteralExpression));
916 }
917
918 static PreParserExpression UseStrictStringLiteral() {
919 return PreParserExpression(TypeField::encode(kStringLiteralExpression) |
920 IsUseStrictField::encode(true));
921 }
922
923 static PreParserExpression UseStrongStringLiteral() {
924 return PreParserExpression(TypeField::encode(kStringLiteralExpression) |
925 IsUseStrongField::encode(true));
926 }
927
928 static PreParserExpression This() {
929 return PreParserExpression(TypeField::encode(kExpression) |
930 ExpressionTypeField::encode(kThisExpression));
931 }
932
933 static PreParserExpression ThisProperty() {
934 return PreParserExpression(
935 TypeField::encode(kExpression) |
936 ExpressionTypeField::encode(kThisPropertyExpression));
937 }
938
939 static PreParserExpression Property() {
940 return PreParserExpression(
941 TypeField::encode(kExpression) |
942 ExpressionTypeField::encode(kPropertyExpression));
943 }
944
945 static PreParserExpression Call() {
946 return PreParserExpression(TypeField::encode(kExpression) |
947 ExpressionTypeField::encode(kCallExpression));
948 }
949
950 static PreParserExpression NoTemplateTag() {
951 return PreParserExpression(TypeField::encode(kExpression) |
952 ExpressionTypeField::encode(
953 kNoTemplateTagExpression));
954 }
955
956 bool IsIdentifier() const {
957 return TypeField::decode(code_) == kIdentifierExpression;
958 }
959
960 PreParserIdentifier AsIdentifier() const {
961 DCHECK(IsIdentifier());
962 return PreParserIdentifier(IdentifierTypeField::decode(code_));
963 }
964
965 bool IsStringLiteral() const {
966 return TypeField::decode(code_) == kStringLiteralExpression;
967 }
968
969 bool IsUseStrictLiteral() const {
970 return TypeField::decode(code_) == kStringLiteralExpression &&
971 IsUseStrictField::decode(code_);
972 }
973
974 bool IsUseStrongLiteral() const {
975 return TypeField::decode(code_) == kStringLiteralExpression &&
976 IsUseStrongField::decode(code_);
977 }
978
979 bool IsThis() const {
980 return TypeField::decode(code_) == kExpression &&
981 ExpressionTypeField::decode(code_) == kThisExpression;
982 }
983
984 bool IsThisProperty() const {
985 return TypeField::decode(code_) == kExpression &&
986 ExpressionTypeField::decode(code_) == kThisPropertyExpression;
987 }
988
989 bool IsProperty() const {
990 return TypeField::decode(code_) == kExpression &&
991 (ExpressionTypeField::decode(code_) == kPropertyExpression ||
992 ExpressionTypeField::decode(code_) == kThisPropertyExpression);
993 }
994
995 bool IsCall() const {
996 return TypeField::decode(code_) == kExpression &&
997 ExpressionTypeField::decode(code_) == kCallExpression;
998 }
999
1000 bool IsValidReferenceExpression() const {
1001 return IsIdentifier() || IsProperty();
1002 }
1003
1004 // At the moment PreParser doesn't track these expression types.
1005 bool IsFunctionLiteral() const { return false; }
1006 bool IsCallNew() const { return false; }
1007
1008 bool IsNoTemplateTag() const {
1009 return TypeField::decode(code_) == kExpression &&
1010 ExpressionTypeField::decode(code_) == kNoTemplateTagExpression;
1011 }
1012
1013 bool IsSpreadExpression() const {
1014 return TypeField::decode(code_) == kSpreadExpression;
1015 }
1016
1017 PreParserExpression AsFunctionLiteral() { return *this; }
1018
1019 bool IsBinaryOperation() const {
1020 return TypeField::decode(code_) == kBinaryOperationExpression;
1021 }
1022
1023 // Dummy implementation for making expression->somefunc() work in both Parser
1024 // and PreParser.
1025 PreParserExpression* operator->() { return this; }
1026
1027 // More dummy implementations of things PreParser doesn't need to track:
1028 void set_index(int index) {} // For YieldExpressions
1029 void set_should_eager_compile() {}
1030
1031 int position() const { return RelocInfo::kNoPosition; }
1032 void set_function_token_position(int position) {}
1033
1034 private:
1035 enum Type {
1036 kExpression,
1037 kIdentifierExpression,
1038 kStringLiteralExpression,
1039 kBinaryOperationExpression,
1040 kSpreadExpression
1041 };
1042
1043 enum ExpressionType {
1044 kThisExpression,
1045 kThisPropertyExpression,
1046 kPropertyExpression,
1047 kCallExpression,
1048 kNoTemplateTagExpression
1049 };
1050
1051 explicit PreParserExpression(uint32_t expression_code)
1052 : code_(expression_code) {}
1053
1054 // The first three bits are for the Type.
1055 typedef BitField<Type, 0, 3> TypeField;
1056
1057 // The rest of the bits are interpreted depending on the value
1058 // of the Type field, so they can share the storage.
1059 typedef BitField<ExpressionType, TypeField::kNext, 3> ExpressionTypeField;
1060 typedef BitField<bool, TypeField::kNext, 1> IsUseStrictField;
1061 typedef BitField<bool, IsUseStrictField::kNext, 1> IsUseStrongField;
1062 typedef BitField<PreParserIdentifier::Type, TypeField::kNext, 10>
1063 IdentifierTypeField;
1064
1065 uint32_t code_;
1066 };
1067
1068
1069 // The pre-parser doesn't need to build lists of expressions, identifiers, or
1070 // the like.
1071 template <typename T>
1072 class PreParserList {
1073 public:
1074 // These functions make list->Add(some_expression) work (and do nothing).
1075 PreParserList() : length_(0) {}
1076 PreParserList* operator->() { return this; }
1077 void Add(T, void*) { ++length_; }
1078 int length() const { return length_; }
1079 private:
1080 int length_;
1081 };
1082
1083
1084 typedef PreParserList<PreParserExpression> PreParserExpressionList;
1085
1086
1087 class PreParserStatement {
1088 public:
1089 static PreParserStatement Default() {
1090 return PreParserStatement(kUnknownStatement);
1091 }
1092
1093 static PreParserStatement Jump() {
1094 return PreParserStatement(kJumpStatement);
1095 }
1096
1097 static PreParserStatement FunctionDeclaration() {
1098 return PreParserStatement(kFunctionDeclaration);
1099 }
1100
1101 // Creates expression statement from expression.
1102 // Preserves being an unparenthesized string literal, possibly
1103 // "use strict".
1104 static PreParserStatement ExpressionStatement(
1105 PreParserExpression expression) {
1106 if (expression.IsUseStrictLiteral()) {
1107 return PreParserStatement(kUseStrictExpressionStatement);
1108 }
1109 if (expression.IsUseStrongLiteral()) {
1110 return PreParserStatement(kUseStrongExpressionStatement);
1111 }
1112 if (expression.IsStringLiteral()) {
1113 return PreParserStatement(kStringLiteralExpressionStatement);
1114 }
1115 return Default();
1116 }
1117
1118 bool IsStringLiteral() {
1119 return code_ == kStringLiteralExpressionStatement;
1120 }
1121
1122 bool IsUseStrictLiteral() {
1123 return code_ == kUseStrictExpressionStatement;
1124 }
1125
1126 bool IsUseStrongLiteral() { return code_ == kUseStrongExpressionStatement; }
1127
1128 bool IsFunctionDeclaration() {
1129 return code_ == kFunctionDeclaration;
1130 }
1131
1132 bool IsJumpStatement() {
1133 return code_ == kJumpStatement;
1134 }
1135
1136 private:
1137 enum Type {
1138 kUnknownStatement,
1139 kJumpStatement,
1140 kStringLiteralExpressionStatement,
1141 kUseStrictExpressionStatement,
1142 kUseStrongExpressionStatement,
1143 kFunctionDeclaration
1144 };
1145
1146 explicit PreParserStatement(Type code) : code_(code) {}
1147 Type code_;
1148 };
1149
1150
1151 typedef PreParserList<PreParserStatement> PreParserStatementList;
1152
1153
1154 class PreParserFactory {
1155 public:
1156 explicit PreParserFactory(void* unused_value_factory) {}
1157 PreParserExpression NewStringLiteral(PreParserIdentifier identifier,
1158 int pos) {
1159 return PreParserExpression::Default();
1160 }
1161 PreParserExpression NewNumberLiteral(double number,
1162 int pos) {
1163 return PreParserExpression::Default();
1164 }
1165 PreParserExpression NewRegExpLiteral(PreParserIdentifier js_pattern,
1166 PreParserIdentifier js_flags,
1167 int literal_index,
1168 bool is_strong,
1169 int pos) {
1170 return PreParserExpression::Default();
1171 }
1172 PreParserExpression NewArrayLiteral(PreParserExpressionList values,
1173 int literal_index,
1174 bool is_strong,
1175 int pos) {
1176 return PreParserExpression::Default();
1177 }
1178 PreParserExpression NewObjectLiteralProperty(PreParserExpression key,
1179 PreParserExpression value,
1180 ObjectLiteralProperty::Kind kind,
1181 bool is_static,
1182 bool is_computed_name) {
1183 return PreParserExpression::Default();
1184 }
1185 PreParserExpression NewObjectLiteralProperty(PreParserExpression key,
1186 PreParserExpression value,
1187 bool is_static,
1188 bool is_computed_name) {
1189 return PreParserExpression::Default();
1190 }
1191 PreParserExpression NewObjectLiteral(PreParserExpressionList properties,
1192 int literal_index,
1193 int boilerplate_properties,
1194 bool has_function,
1195 bool is_strong,
1196 int pos) {
1197 return PreParserExpression::Default();
1198 }
1199 PreParserExpression NewVariableProxy(void* variable) {
1200 return PreParserExpression::Default();
1201 }
1202 PreParserExpression NewProperty(PreParserExpression obj,
1203 PreParserExpression key,
1204 int pos) {
1205 if (obj.IsThis()) {
1206 return PreParserExpression::ThisProperty();
1207 }
1208 return PreParserExpression::Property();
1209 }
1210 PreParserExpression NewUnaryOperation(Token::Value op,
1211 PreParserExpression expression,
1212 int pos) {
1213 return PreParserExpression::Default();
1214 }
1215 PreParserExpression NewBinaryOperation(Token::Value op,
1216 PreParserExpression left,
1217 PreParserExpression right, int pos) {
1218 return PreParserExpression::BinaryOperation(left, op, right);
1219 }
1220 PreParserExpression NewCompareOperation(Token::Value op,
1221 PreParserExpression left,
1222 PreParserExpression right, int pos) {
1223 return PreParserExpression::Default();
1224 }
1225 PreParserExpression NewAssignment(Token::Value op,
1226 PreParserExpression left,
1227 PreParserExpression right,
1228 int pos) {
1229 return PreParserExpression::Default();
1230 }
1231 PreParserExpression NewYield(PreParserExpression generator_object,
1232 PreParserExpression expression,
1233 Yield::Kind yield_kind,
1234 int pos) {
1235 return PreParserExpression::Default();
1236 }
1237 PreParserExpression NewConditional(PreParserExpression condition,
1238 PreParserExpression then_expression,
1239 PreParserExpression else_expression,
1240 int pos) {
1241 return PreParserExpression::Default();
1242 }
1243 PreParserExpression NewCountOperation(Token::Value op,
1244 bool is_prefix,
1245 PreParserExpression expression,
1246 int pos) {
1247 return PreParserExpression::Default();
1248 }
1249 PreParserExpression NewCall(PreParserExpression expression,
1250 PreParserExpressionList arguments,
1251 int pos) {
1252 return PreParserExpression::Call();
1253 }
1254 PreParserExpression NewCallNew(PreParserExpression expression,
1255 PreParserExpressionList arguments,
1256 int pos) {
1257 return PreParserExpression::Default();
1258 }
1259 PreParserExpression NewCallRuntime(const AstRawString* name,
1260 const Runtime::Function* function,
1261 PreParserExpressionList arguments,
1262 int pos) {
1263 return PreParserExpression::Default();
1264 }
1265 PreParserStatement NewReturnStatement(PreParserExpression expression,
1266 int pos) {
1267 return PreParserStatement::Default();
1268 }
1269 PreParserExpression NewFunctionLiteral(
1270 PreParserIdentifier name, AstValueFactory* ast_value_factory,
1271 Scope* scope, PreParserStatementList body, int materialized_literal_count,
1272 int expected_property_count, int parameter_count,
1273 FunctionLiteral::ParameterFlag has_duplicate_parameters,
1274 FunctionLiteral::FunctionType function_type,
1275 FunctionLiteral::IsFunctionFlag is_function,
1276 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
1277 int position) {
1278 return PreParserExpression::Default();
1279 }
1280
1281 PreParserExpression NewSpread(PreParserExpression expression, int pos) {
1282 return PreParserExpression::Spread(expression);
1283 }
1284
1285 // Return the object itself as AstVisitor and implement the needed
1286 // dummy method right in this class.
1287 PreParserFactory* visitor() { return this; }
1288 int* ast_properties() {
1289 static int dummy = 42;
1290 return &dummy;
1291 }
1292 };
1293
1294
1295 struct PreParserFormalParameterParsingState {
1296 explicit PreParserFormalParameterParsingState(Scope* scope)
1297 : scope(scope),
1298 has_rest(false),
1299 is_simple_parameter_list(true),
1300 materialized_literals_count(0) {}
1301 Scope* scope;
1302 bool has_rest;
1303 bool is_simple_parameter_list;
1304 int materialized_literals_count;
1305 };
1306
1307
1308 class PreParser;
1309
1310 class PreParserTraits {
1311 public:
1312 struct Type {
1313 // TODO(marja): To be removed. The Traits object should contain all the data
1314 // it needs.
1315 typedef PreParser* Parser;
1316
1317 // PreParser doesn't need to store generator variables.
1318 typedef void GeneratorVariable;
1319
1320 typedef int AstProperties;
1321
1322 // Return types for traversing functions.
1323 typedef PreParserIdentifier Identifier;
1324 typedef PreParserExpression Expression;
1325 typedef PreParserExpression YieldExpression;
1326 typedef PreParserExpression FunctionLiteral;
1327 typedef PreParserExpression ClassLiteral;
1328 typedef PreParserExpression ObjectLiteralProperty;
1329 typedef PreParserExpression Literal;
1330 typedef PreParserExpressionList ExpressionList;
1331 typedef PreParserExpressionList PropertyList;
1332 typedef PreParserIdentifier FormalParameter;
1333 typedef PreParserStatementList StatementList;
1334 typedef PreParserFormalParameterParsingState FormalParameterParsingState;
1335
1336 // For constructing objects returned by the traversing functions.
1337 typedef PreParserFactory Factory;
1338 };
1339
1340 explicit PreParserTraits(PreParser* pre_parser) : pre_parser_(pre_parser) {}
1341
1342 // Helper functions for recursive descent.
1343 static bool IsEval(PreParserIdentifier identifier) {
1344 return identifier.IsEval();
1345 }
1346
1347 static bool IsArguments(PreParserIdentifier identifier) {
1348 return identifier.IsArguments();
1349 }
1350
1351 static bool IsEvalOrArguments(PreParserIdentifier identifier) {
1352 return identifier.IsEvalOrArguments();
1353 }
1354
1355 static bool IsUndefined(PreParserIdentifier identifier) {
1356 return identifier.IsUndefined();
1357 }
1358
1359 static bool IsPrototype(PreParserIdentifier identifier) {
1360 return identifier.IsPrototype();
1361 }
1362
1363 static bool IsConstructor(PreParserIdentifier identifier) {
1364 return identifier.IsConstructor();
1365 }
1366
1367 // Returns true if the expression is of type "this.foo".
1368 static bool IsThisProperty(PreParserExpression expression) {
1369 return expression.IsThisProperty();
1370 }
1371
1372 static bool IsIdentifier(PreParserExpression expression) {
1373 return expression.IsIdentifier();
1374 }
1375
1376 static PreParserIdentifier AsIdentifier(PreParserExpression expression) {
1377 return expression.AsIdentifier();
1378 }
1379
1380 static bool IsFutureStrictReserved(PreParserIdentifier identifier) {
1381 return identifier.IsFutureStrictReserved();
1382 }
1383
1384 static bool IsBoilerplateProperty(PreParserExpression property) {
1385 // PreParser doesn't count boilerplate properties.
1386 return false;
1387 }
1388
1389 static bool IsArrayIndex(PreParserIdentifier string, uint32_t* index) {
1390 return false;
1391 }
1392
1393 static PreParserExpression GetPropertyValue(PreParserExpression property) {
1394 return PreParserExpression::Default();
1395 }
1396
1397 // Functions for encapsulating the differences between parsing and preparsing;
1398 // operations interleaved with the recursive descent.
1399 static void PushLiteralName(FuncNameInferrer* fni, PreParserIdentifier id) {
1400 // PreParser should not use FuncNameInferrer.
1401 UNREACHABLE();
1402 }
1403
1404 static void PushPropertyName(FuncNameInferrer* fni,
1405 PreParserExpression expression) {
1406 // PreParser should not use FuncNameInferrer.
1407 UNREACHABLE();
1408 }
1409
1410 static void InferFunctionName(FuncNameInferrer* fni,
1411 PreParserExpression expression) {
1412 // PreParser should not use FuncNameInferrer.
1413 UNREACHABLE();
1414 }
1415
1416 static void CheckFunctionLiteralInsideTopLevelObjectLiteral(
1417 Scope* scope, PreParserExpression property, bool* has_function) {}
1418
1419 static void CheckAssigningFunctionLiteralToProperty(
1420 PreParserExpression left, PreParserExpression right) {}
1421
1422 static void CheckPossibleEvalCall(PreParserExpression expression,
1423 Scope* scope) {
1424 if (IsIdentifier(expression) && IsEval(AsIdentifier(expression))) {
1425 scope->DeclarationScope()->RecordEvalCall();
1426 scope->RecordEvalCall();
1427 }
1428 }
1429
1430 static PreParserExpression MarkExpressionAsAssigned(
1431 PreParserExpression expression) {
1432 // TODO(marja): To be able to produce the same errors, the preparser needs
1433 // to start tracking which expressions are variables and which are assigned.
1434 return expression;
1435 }
1436
1437 bool ShortcutNumericLiteralBinaryExpression(PreParserExpression* x,
1438 PreParserExpression y,
1439 Token::Value op,
1440 int pos,
1441 PreParserFactory* factory) {
1442 return false;
1443 }
1444
1445 PreParserExpression BuildUnaryExpression(PreParserExpression expression,
1446 Token::Value op, int pos,
1447 PreParserFactory* factory) {
1448 return PreParserExpression::Default();
1449 }
1450
1451 PreParserExpression NewThrowReferenceError(MessageTemplate::Template message,
1452 int pos) {
1453 return PreParserExpression::Default();
1454 }
1455 PreParserExpression NewThrowSyntaxError(MessageTemplate::Template message,
1456 Handle<Object> arg, int pos) {
1457 return PreParserExpression::Default();
1458 }
1459 PreParserExpression NewThrowTypeError(MessageTemplate::Template message,
1460 Handle<Object> arg, int pos) {
1461 return PreParserExpression::Default();
1462 }
1463
1464 // Reporting errors.
1465 void ReportMessageAt(Scanner::Location location,
1466 MessageTemplate::Template message,
1467 const char* arg = NULL,
1468 ParseErrorType error_type = kSyntaxError);
1469 void ReportMessageAt(int start_pos, int end_pos,
1470 MessageTemplate::Template message,
1471 const char* arg = NULL,
1472 ParseErrorType error_type = kSyntaxError);
1473
1474 // "null" return type creators.
1475 static PreParserIdentifier EmptyIdentifier() {
1476 return PreParserIdentifier::Default();
1477 }
1478 static PreParserIdentifier EmptyIdentifierString() {
1479 return PreParserIdentifier::Default();
1480 }
1481 static PreParserExpression EmptyExpression() {
1482 return PreParserExpression::Default();
1483 }
1484 static PreParserExpression EmptyLiteral() {
1485 return PreParserExpression::Default();
1486 }
1487 static PreParserExpression EmptyObjectLiteralProperty() {
1488 return PreParserExpression::Default();
1489 }
1490 static PreParserExpression EmptyFunctionLiteral() {
1491 return PreParserExpression::Default();
1492 }
1493 static PreParserExpressionList NullExpressionList() {
1494 return PreParserExpressionList();
1495 }
1496
1497 // Odd-ball literal creators.
1498 static PreParserExpression GetLiteralTheHole(int position,
1499 PreParserFactory* factory) {
1500 return PreParserExpression::Default();
1501 }
1502
1503 // Producing data during the recursive descent.
1504 PreParserIdentifier GetSymbol(Scanner* scanner);
1505 PreParserIdentifier GetNumberAsSymbol(Scanner* scanner);
1506
1507 static PreParserIdentifier GetNextSymbol(Scanner* scanner) {
1508 return PreParserIdentifier::Default();
1509 }
1510
1511 static PreParserExpression ThisExpression(Scope* scope,
1512 PreParserFactory* factory,
1513 int pos) {
1514 return PreParserExpression::This();
1515 }
1516
1517 static PreParserExpression SuperPropertyReference(Scope* scope,
1518 PreParserFactory* factory,
1519 int pos) {
1520 return PreParserExpression::Default();
1521 }
1522
1523 static PreParserExpression SuperCallReference(Scope* scope,
1524 PreParserFactory* factory,
1525 int pos) {
1526 return PreParserExpression::Default();
1527 }
1528
1529 static PreParserExpression NewTargetExpression(Scope* scope,
1530 PreParserFactory* factory,
1531 int pos) {
1532 return PreParserExpression::Default();
1533 }
1534
1535 static PreParserExpression DefaultConstructor(bool call_super, Scope* scope,
1536 int pos, int end_pos) {
1537 return PreParserExpression::Default();
1538 }
1539
1540 static PreParserExpression ExpressionFromLiteral(
1541 Token::Value token, int pos, Scanner* scanner,
1542 PreParserFactory* factory) {
1543 return PreParserExpression::Default();
1544 }
1545
1546 static PreParserExpression ExpressionFromIdentifier(
1547 PreParserIdentifier name, int start_position, int end_position,
1548 Scope* scope, PreParserFactory* factory) {
1549 return PreParserExpression::FromIdentifier(name);
1550 }
1551
1552 PreParserExpression ExpressionFromString(int pos,
1553 Scanner* scanner,
1554 PreParserFactory* factory = NULL);
1555
1556 PreParserExpression GetIterator(PreParserExpression iterable,
1557 PreParserFactory* factory) {
1558 return PreParserExpression::Default();
1559 }
1560
1561 static PreParserExpressionList NewExpressionList(int size, Zone* zone) {
1562 return PreParserExpressionList();
1563 }
1564
1565 static PreParserStatementList NewStatementList(int size, Zone* zone) {
1566 return PreParserStatementList();
1567 }
1568
1569 static PreParserExpressionList NewPropertyList(int size, Zone* zone) {
1570 return PreParserExpressionList();
1571 }
1572
1573 static void AddParameterInitializationBlock(
1574 const PreParserFormalParameterParsingState& formal_parameters,
1575 PreParserStatementList list, bool* ok) {}
1576
1577 V8_INLINE void SkipLazyFunctionBody(int* materialized_literal_count,
1578 int* expected_property_count, bool* ok) {
1579 UNREACHABLE();
1580 }
1581
1582 V8_INLINE PreParserStatementList ParseEagerFunctionBody(
1583 PreParserIdentifier function_name, int pos,
1584 const PreParserFormalParameterParsingState& formal_parameters,
1585 Variable* fvar, Token::Value fvar_init_op, FunctionKind kind, bool* ok);
1586
1587 V8_INLINE void ParseArrowFunctionFormalParameters(
1588 PreParserFormalParameterParsingState* parsing_state,
1589 PreParserExpression expression, const Scanner::Location& params_loc,
1590 Scanner::Location* duplicate_loc, bool* ok);
1591
1592 void ReindexLiterals(
1593 const PreParserFormalParameterParsingState& parsing_state) {}
1594
1595 struct TemplateLiteralState {};
1596
1597 TemplateLiteralState OpenTemplateLiteral(int pos) {
1598 return TemplateLiteralState();
1599 }
1600 void AddTemplateSpan(TemplateLiteralState*, bool) {}
1601 void AddTemplateExpression(TemplateLiteralState*, PreParserExpression) {}
1602 PreParserExpression CloseTemplateLiteral(TemplateLiteralState*, int,
1603 PreParserExpression tag) {
1604 if (IsTaggedTemplate(tag)) {
1605 // Emulate generation of array literals for tag callsite
1606 // 1st is array of cooked strings, second is array of raw strings
1607 MaterializeTemplateCallsiteLiterals();
1608 }
1609 return EmptyExpression();
1610 }
1611 inline void MaterializeTemplateCallsiteLiterals();
1612 PreParserExpression NoTemplateTag() {
1613 return PreParserExpression::NoTemplateTag();
1614 }
1615 static bool IsTaggedTemplate(const PreParserExpression tag) {
1616 return !tag.IsNoTemplateTag();
1617 }
1618
1619 void DeclareFormalParameter(void* parsing_state, PreParserExpression pattern,
1620 ExpressionClassifier* classifier, bool is_rest) {}
1621
1622 void CheckConflictingVarDeclarations(Scope* scope, bool* ok) {}
1623
1624 // Temporary glue; these functions will move to ParserBase.
1625 PreParserExpression ParseV8Intrinsic(bool* ok);
1626 PreParserExpression ParseFunctionLiteral(
1627 PreParserIdentifier name, Scanner::Location function_name_location,
1628 FunctionNameValidity function_name_validity, FunctionKind kind,
1629 int function_token_position, FunctionLiteral::FunctionType type,
1630 FunctionLiteral::ArityRestriction arity_restriction, bool* ok);
1631
1632 PreParserExpression ParseClassLiteral(PreParserIdentifier name,
1633 Scanner::Location class_name_location,
1634 bool name_is_strict_reserved, int pos,
1635 bool* ok);
1636
1637 PreParserExpressionList PrepareSpreadArguments(PreParserExpressionList list) {
1638 return list;
1639 }
1640
1641 inline void MaterializeUnspreadArgumentsLiterals(int count);
1642
1643 inline PreParserExpression SpreadCall(PreParserExpression function,
1644 PreParserExpressionList args, int pos);
1645
1646 inline PreParserExpression SpreadCallNew(PreParserExpression function,
1647 PreParserExpressionList args,
1648 int pos);
1649
1650 private:
1651 PreParser* pre_parser_;
1652 };
1653
1654
1655 // Preparsing checks a JavaScript program and emits preparse-data that helps
1656 // a later parsing to be faster.
1657 // See preparse-data-format.h for the data format.
1658
1659 // The PreParser checks that the syntax follows the grammar for JavaScript,
1660 // and collects some information about the program along the way.
1661 // The grammar check is only performed in order to understand the program
1662 // sufficiently to deduce some information about it, that can be used
1663 // to speed up later parsing. Finding errors is not the goal of pre-parsing,
1664 // rather it is to speed up properly written and correct programs.
1665 // That means that contextual checks (like a label being declared where
1666 // it is used) are generally omitted.
1667 class PreParser : public ParserBase<PreParserTraits> {
1668 public:
1669 typedef PreParserIdentifier Identifier;
1670 typedef PreParserExpression Expression;
1671 typedef PreParserStatement Statement;
1672
1673 enum PreParseResult {
1674 kPreParseStackOverflow,
1675 kPreParseSuccess
1676 };
1677
1678 PreParser(Zone* zone, Scanner* scanner, AstValueFactory* ast_value_factory,
1679 ParserRecorder* log, uintptr_t stack_limit)
1680 : ParserBase<PreParserTraits>(zone, scanner, stack_limit, NULL,
1681 ast_value_factory, log, this) {}
1682
1683 // Pre-parse the program from the character stream; returns true on
1684 // success (even if parsing failed, the pre-parse data successfully
1685 // captured the syntax error), and false if a stack-overflow happened
1686 // during parsing.
1687 PreParseResult PreParseProgram(int* materialized_literals = 0) {
1688 Scope* scope = NewScope(scope_, SCRIPT_SCOPE);
1689 PreParserFactory factory(NULL);
1690 FunctionState top_scope(&function_state_, &scope_, scope, kNormalFunction,
1691 &factory);
1692 bool ok = true;
1693 int start_position = scanner()->peek_location().beg_pos;
1694 ParseStatementList(Token::EOS, &ok);
1695 if (stack_overflow()) return kPreParseStackOverflow;
1696 if (!ok) {
1697 ReportUnexpectedToken(scanner()->current_token());
1698 } else if (is_strict(scope_->language_mode())) {
1699 CheckStrictOctalLiteral(start_position, scanner()->location().end_pos,
1700 &ok);
1701 }
1702 if (materialized_literals) {
1703 *materialized_literals = function_state_->materialized_literal_count();
1704 }
1705 return kPreParseSuccess;
1706 }
1707
1708 // Parses a single function literal, from the opening parentheses before
1709 // parameters to the closing brace after the body.
1710 // Returns a FunctionEntry describing the body of the function in enough
1711 // detail that it can be lazily compiled.
1712 // The scanner is expected to have matched the "function" or "function*"
1713 // keyword and parameters, and have consumed the initial '{'.
1714 // At return, unless an error occurred, the scanner is positioned before the
1715 // the final '}'.
1716 PreParseResult PreParseLazyFunction(
1717 LanguageMode language_mode, FunctionKind kind, ParserRecorder* log,
1718 Scanner::BookmarkScope* bookmark = nullptr);
1719
1720 private:
1721 friend class PreParserTraits;
1722
1723 static const int kLazyParseTrialLimit = 200;
1724
1725 // These types form an algebra over syntactic categories that is just
1726 // rich enough to let us recognize and propagate the constructs that
1727 // are either being counted in the preparser data, or is important
1728 // to throw the correct syntax error exceptions.
1729
1730 // All ParseXXX functions take as the last argument an *ok parameter
1731 // which is set to false if parsing failed; it is unchanged otherwise.
1732 // By making the 'exception handling' explicit, we are forced to check
1733 // for failure at the call sites.
1734 Statement ParseStatementListItem(bool* ok);
1735 void ParseStatementList(int end_token, bool* ok,
1736 Scanner::BookmarkScope* bookmark = nullptr);
1737 Statement ParseStatement(bool* ok);
1738 Statement ParseSubStatement(bool* ok);
1739 Statement ParseFunctionDeclaration(bool* ok);
1740 Statement ParseClassDeclaration(bool* ok);
1741 Statement ParseBlock(bool* ok);
1742 Statement ParseVariableStatement(VariableDeclarationContext var_context,
1743 bool* ok);
1744 Statement ParseVariableDeclarations(VariableDeclarationContext var_context,
1745 int* num_decl,
1746 Scanner::Location* first_initializer_loc,
1747 Scanner::Location* bindings_loc,
1748 bool* ok);
1749 Statement ParseExpressionOrLabelledStatement(bool* ok);
1750 Statement ParseIfStatement(bool* ok);
1751 Statement ParseContinueStatement(bool* ok);
1752 Statement ParseBreakStatement(bool* ok);
1753 Statement ParseReturnStatement(bool* ok);
1754 Statement ParseWithStatement(bool* ok);
1755 Statement ParseSwitchStatement(bool* ok);
1756 Statement ParseDoWhileStatement(bool* ok);
1757 Statement ParseWhileStatement(bool* ok);
1758 Statement ParseForStatement(bool* ok);
1759 Statement ParseThrowStatement(bool* ok);
1760 Statement ParseTryStatement(bool* ok);
1761 Statement ParseDebuggerStatement(bool* ok);
1762 Expression ParseConditionalExpression(bool accept_IN, bool* ok);
1763 Expression ParseObjectLiteral(bool* ok);
1764 Expression ParseV8Intrinsic(bool* ok);
1765
1766 V8_INLINE void SkipLazyFunctionBody(int* materialized_literal_count,
1767 int* expected_property_count, bool* ok);
1768 V8_INLINE PreParserStatementList
1769 ParseEagerFunctionBody(PreParserIdentifier function_name, int pos,
1770 const FormalParameterParsingStateT& formal_parameters,
1771 Variable* fvar, Token::Value fvar_init_op,
1772 FunctionKind kind, bool* ok);
1773
1774 Expression ParseFunctionLiteral(
1775 Identifier name, Scanner::Location function_name_location,
1776 FunctionNameValidity function_name_validity, FunctionKind kind,
1777 int function_token_pos, FunctionLiteral::FunctionType function_type,
1778 FunctionLiteral::ArityRestriction arity_restriction, bool* ok);
1779 void ParseLazyFunctionLiteralBody(bool* ok,
1780 Scanner::BookmarkScope* bookmark = nullptr);
1781
1782 PreParserExpression ParseClassLiteral(PreParserIdentifier name,
1783 Scanner::Location class_name_location,
1784 bool name_is_strict_reserved, int pos,
1785 bool* ok);
1786 };
1787
1788
1789 void PreParserTraits::MaterializeTemplateCallsiteLiterals() {
1790 pre_parser_->function_state_->NextMaterializedLiteralIndex();
1791 pre_parser_->function_state_->NextMaterializedLiteralIndex();
1792 }
1793
1794
1795 void PreParserTraits::MaterializeUnspreadArgumentsLiterals(int count) {
1796 for (int i = 0; i < count; ++i) {
1797 pre_parser_->function_state_->NextMaterializedLiteralIndex();
1798 }
1799 }
1800
1801
1802 PreParserExpression PreParserTraits::SpreadCall(PreParserExpression function,
1803 PreParserExpressionList args,
1804 int pos) {
1805 return pre_parser_->factory()->NewCall(function, args, pos);
1806 }
1807
1808 PreParserExpression PreParserTraits::SpreadCallNew(PreParserExpression function,
1809 PreParserExpressionList args,
1810 int pos) {
1811 return pre_parser_->factory()->NewCallNew(function, args, pos);
1812 }
1813
1814
1815 void PreParserTraits::ParseArrowFunctionFormalParameters(
1816 PreParserFormalParameterParsingState* parsing_state,
1817 PreParserExpression params, const Scanner::Location& params_loc,
1818 Scanner::Location* duplicate_loc, bool* ok) {
1819 // TODO(wingo): Detect duplicated identifiers in paramlists. Detect parameter
1820 // lists that are too long.
1821 }
1822
1823
1824 PreParserStatementList PreParser::ParseEagerFunctionBody(
1825 PreParserIdentifier function_name, int pos,
1826 const PreParserFormalParameterParsingState& formal_parameters,
1827 Variable* fvar, Token::Value fvar_init_op, FunctionKind kind, bool* ok) {
1828 ParsingModeScope parsing_mode(this, PARSE_EAGERLY);
1829
1830 ParseStatementList(Token::RBRACE, ok);
1831 if (!*ok) return PreParserStatementList();
1832
1833 Expect(Token::RBRACE, ok);
1834 return PreParserStatementList();
1835 }
1836
1837
1838 PreParserStatementList PreParserTraits::ParseEagerFunctionBody(
1839 PreParserIdentifier function_name, int pos,
1840 const PreParserFormalParameterParsingState& formal_parameters,
1841 Variable* fvar, Token::Value fvar_init_op, FunctionKind kind, bool* ok) {
1842 return pre_parser_->ParseEagerFunctionBody(
1843 function_name, pos, formal_parameters, fvar, fvar_init_op, kind, ok);
1844 }
1845
1846
1847 template <class Traits> 808 template <class Traits>
1848 ParserBase<Traits>::FunctionState::FunctionState( 809 ParserBase<Traits>::FunctionState::FunctionState(
1849 FunctionState** function_state_stack, Scope** scope_stack, Scope* scope, 810 FunctionState** function_state_stack, Scope** scope_stack, Scope* scope,
1850 FunctionKind kind, typename Traits::Type::Factory* factory) 811 FunctionKind kind, typename Traits::Type::Factory* factory)
1851 : next_materialized_literal_index_(0), 812 : next_materialized_literal_index_(0),
1852 expected_property_count_(0), 813 expected_property_count_(0),
1853 this_location_(Scanner::Location::invalid()), 814 this_location_(Scanner::Location::invalid()),
1854 return_location_(Scanner::Location::invalid()), 815 return_location_(Scanner::Location::invalid()),
1855 super_location_(Scanner::Location::invalid()), 816 super_location_(Scanner::Location::invalid()),
1856 kind_(kind), 817 kind_(kind),
(...skipping 2120 matching lines...) Expand 10 before | Expand all | Expand 10 after
3977 } 2938 }
3978 if (has_seen_constructor_) { 2939 if (has_seen_constructor_) {
3979 this->parser()->ReportMessage(MessageTemplate::kDuplicateConstructor); 2940 this->parser()->ReportMessage(MessageTemplate::kDuplicateConstructor);
3980 *ok = false; 2941 *ok = false;
3981 return; 2942 return;
3982 } 2943 }
3983 has_seen_constructor_ = true; 2944 has_seen_constructor_ = true;
3984 return; 2945 return;
3985 } 2946 }
3986 } 2947 }
2948
3987 } } // v8::internal 2949 } } // v8::internal
3988 2950
3989 #endif // V8_PREPARSER_H 2951 #endif // V8_PARSER_BASE_H
OLDNEW
« no previous file with comments | « src/parser.h ('k') | src/preparser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698