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

Side by Side Diff: src/preparser.h

Issue 252423007: Parser: Introduce StatementList + NewStatementList() (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 8 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.h ('k') | src/preparser.cc » ('j') | src/preparser.cc » ('J')
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 669 matching lines...) Expand 10 before | Expand all | Expand 10 after
680 // These functions make list->Add(some_expression) work (and do nothing). 680 // These functions make list->Add(some_expression) work (and do nothing).
681 PreParserExpressionList() : length_(0) {} 681 PreParserExpressionList() : length_(0) {}
682 PreParserExpressionList* operator->() { return this; } 682 PreParserExpressionList* operator->() { return this; }
683 void Add(PreParserExpression, void*) { ++length_; } 683 void Add(PreParserExpression, void*) { ++length_; }
684 int length() const { return length_; } 684 int length() const { return length_; }
685 private: 685 private:
686 int length_; 686 int length_;
687 }; 687 };
688 688
689 689
690 class PreParserStatement {
691 public:
692 static PreParserStatement Default() {
693 return PreParserStatement(kUnknownStatement);
694 }
695
696 static PreParserStatement FunctionDeclaration() {
697 return PreParserStatement(kFunctionDeclaration);
698 }
699
700 // Creates expression statement from expression.
701 // Preserves being an unparenthesized string literal, possibly
702 // "use strict".
703 static PreParserStatement ExpressionStatement(
704 PreParserExpression expression) {
705 if (expression.IsUseStrictLiteral()) {
706 return PreParserStatement(kUseStrictExpressionStatement);
707 }
708 if (expression.IsStringLiteral()) {
709 return PreParserStatement(kStringLiteralExpressionStatement);
710 }
711 return Default();
712 }
713
714 bool IsStringLiteral() {
715 return code_ == kStringLiteralExpressionStatement;
716 }
717
718 bool IsUseStrictLiteral() {
719 return code_ == kUseStrictExpressionStatement;
720 }
721
722 bool IsFunctionDeclaration() {
723 return code_ == kFunctionDeclaration;
724 }
725
726 private:
727 enum Type {
728 kUnknownStatement,
729 kStringLiteralExpressionStatement,
730 kUseStrictExpressionStatement,
731 kFunctionDeclaration
732 };
733
734 explicit PreParserStatement(Type code) : code_(code) {}
735 Type code_;
736 };
737
738
739
740 // PreParserStatementList doesn't actually store the statements because
741 // the PreParser does not need them.
742 class PreParserStatementList {
743 public:
744 // These functions make list->Add(some_expression) work as no-ops.
745 PreParserStatementList() {}
746 PreParserStatementList* operator->() { return this; }
747 void Add(PreParserStatement, void*) {}
748 };
749
750
690 class PreParserScope { 751 class PreParserScope {
691 public: 752 public:
692 explicit PreParserScope(PreParserScope* outer_scope, ScopeType scope_type) 753 explicit PreParserScope(PreParserScope* outer_scope, ScopeType scope_type)
693 : scope_type_(scope_type) { 754 : scope_type_(scope_type) {
694 strict_mode_ = outer_scope ? outer_scope->strict_mode() : SLOPPY; 755 strict_mode_ = outer_scope ? outer_scope->strict_mode() : SLOPPY;
695 } 756 }
696 757
697 ScopeType type() { return scope_type_; } 758 ScopeType type() { return scope_type_; }
698 StrictMode strict_mode() const { return strict_mode_; } 759 StrictMode strict_mode() const { return strict_mode_; }
699 void SetStrictMode(StrictMode strict_mode) { strict_mode_ = strict_mode; } 760 void SetStrictMode(StrictMode strict_mode) { strict_mode_ = strict_mode; }
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
823 884
824 // Return types for traversing functions. 885 // Return types for traversing functions.
825 typedef PreParserIdentifier Identifier; 886 typedef PreParserIdentifier Identifier;
826 typedef PreParserExpression Expression; 887 typedef PreParserExpression Expression;
827 typedef PreParserExpression YieldExpression; 888 typedef PreParserExpression YieldExpression;
828 typedef PreParserExpression FunctionLiteral; 889 typedef PreParserExpression FunctionLiteral;
829 typedef PreParserExpression ObjectLiteralProperty; 890 typedef PreParserExpression ObjectLiteralProperty;
830 typedef PreParserExpression Literal; 891 typedef PreParserExpression Literal;
831 typedef PreParserExpressionList ExpressionList; 892 typedef PreParserExpressionList ExpressionList;
832 typedef PreParserExpressionList PropertyList; 893 typedef PreParserExpressionList PropertyList;
894 typedef PreParserStatementList StatementList;
833 895
834 // For constructing objects returned by the traversing functions. 896 // For constructing objects returned by the traversing functions.
835 typedef PreParserFactory Factory; 897 typedef PreParserFactory Factory;
836 }; 898 };
837 899
838 explicit PreParserTraits(PreParser* pre_parser) : pre_parser_(pre_parser) {} 900 explicit PreParserTraits(PreParser* pre_parser) : pre_parser_(pre_parser) {}
839 901
840 // Custom operations executed when FunctionStates are created and 902 // Custom operations executed when FunctionStates are created and
841 // destructed. (The PreParser doesn't need to do anything.) 903 // destructed. (The PreParser doesn't need to do anything.)
842 template<typename FunctionState> 904 template<typename FunctionState>
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
986 } 1048 }
987 1049
988 PreParserExpression ExpressionFromString(int pos, 1050 PreParserExpression ExpressionFromString(int pos,
989 Scanner* scanner, 1051 Scanner* scanner,
990 PreParserFactory* factory = NULL); 1052 PreParserFactory* factory = NULL);
991 1053
992 static PreParserExpressionList NewExpressionList(int size, void* zone) { 1054 static PreParserExpressionList NewExpressionList(int size, void* zone) {
993 return PreParserExpressionList(); 1055 return PreParserExpressionList();
994 } 1056 }
995 1057
1058 static PreParserStatementList NewStatementList(int size, void* zone) {
1059 return PreParserStatementList();
1060 }
1061
996 static PreParserExpressionList NewPropertyList(int size, void* zone) { 1062 static PreParserExpressionList NewPropertyList(int size, void* zone) {
997 return PreParserExpressionList(); 1063 return PreParserExpressionList();
998 } 1064 }
999 1065
1000 // Temporary glue; these functions will move to ParserBase. 1066 // Temporary glue; these functions will move to ParserBase.
1001 PreParserExpression ParseV8Intrinsic(bool* ok); 1067 PreParserExpression ParseV8Intrinsic(bool* ok);
1002 PreParserExpression ParseFunctionLiteral( 1068 PreParserExpression ParseFunctionLiteral(
1003 PreParserIdentifier name, 1069 PreParserIdentifier name,
1004 Scanner::Location function_name_location, 1070 Scanner::Location function_name_location,
1005 bool name_is_strict_reserved, 1071 bool name_is_strict_reserved,
(...skipping 16 matching lines...) Expand all
1022 // The grammar check is only performed in order to understand the program 1088 // The grammar check is only performed in order to understand the program
1023 // sufficiently to deduce some information about it, that can be used 1089 // sufficiently to deduce some information about it, that can be used
1024 // to speed up later parsing. Finding errors is not the goal of pre-parsing, 1090 // to speed up later parsing. Finding errors is not the goal of pre-parsing,
1025 // rather it is to speed up properly written and correct programs. 1091 // rather it is to speed up properly written and correct programs.
1026 // That means that contextual checks (like a label being declared where 1092 // That means that contextual checks (like a label being declared where
1027 // it is used) are generally omitted. 1093 // it is used) are generally omitted.
1028 class PreParser : public ParserBase<PreParserTraits> { 1094 class PreParser : public ParserBase<PreParserTraits> {
1029 public: 1095 public:
1030 typedef PreParserIdentifier Identifier; 1096 typedef PreParserIdentifier Identifier;
1031 typedef PreParserExpression Expression; 1097 typedef PreParserExpression Expression;
1032 1098
marja 2014/04/24 15:31:10 Add a typedef for Statement here...
1033 enum PreParseResult { 1099 enum PreParseResult {
1034 kPreParseStackOverflow, 1100 kPreParseStackOverflow,
1035 kPreParseSuccess 1101 kPreParseSuccess
1036 }; 1102 };
1037 1103
1038 PreParser(Scanner* scanner, ParserRecorder* log, uintptr_t stack_limit) 1104 PreParser(Scanner* scanner, ParserRecorder* log, uintptr_t stack_limit)
1039 : ParserBase<PreParserTraits>(scanner, stack_limit, NULL, log, NULL, 1105 : ParserBase<PreParserTraits>(scanner, stack_limit, NULL, log, NULL,
1040 this) {} 1106 this) {}
1041 1107
1042 // Pre-parse the program from the character stream; returns true on 1108 // Pre-parse the program from the character stream; returns true on
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1083 kStatement, 1149 kStatement,
1084 kForStatement 1150 kForStatement
1085 }; 1151 };
1086 1152
1087 // If a list of variable declarations includes any initializers. 1153 // If a list of variable declarations includes any initializers.
1088 enum VariableDeclarationProperties { 1154 enum VariableDeclarationProperties {
1089 kHasInitializers, 1155 kHasInitializers,
1090 kHasNoInitializers 1156 kHasNoInitializers
1091 }; 1157 };
1092 1158
1093 class Statement {
1094 public:
1095 static Statement Default() {
1096 return Statement(kUnknownStatement);
1097 }
1098
1099 static Statement FunctionDeclaration() {
1100 return Statement(kFunctionDeclaration);
1101 }
1102
1103 // Creates expression statement from expression.
1104 // Preserves being an unparenthesized string literal, possibly
1105 // "use strict".
1106 static Statement ExpressionStatement(Expression expression) {
1107 if (expression.IsUseStrictLiteral()) {
1108 return Statement(kUseStrictExpressionStatement);
1109 }
1110 if (expression.IsStringLiteral()) {
1111 return Statement(kStringLiteralExpressionStatement);
1112 }
1113 return Default();
1114 }
1115
1116 bool IsStringLiteral() {
1117 return code_ == kStringLiteralExpressionStatement;
1118 }
1119
1120 bool IsUseStrictLiteral() {
1121 return code_ == kUseStrictExpressionStatement;
1122 }
1123
1124 bool IsFunctionDeclaration() {
1125 return code_ == kFunctionDeclaration;
1126 }
1127
1128 private:
1129 enum Type {
1130 kUnknownStatement,
1131 kStringLiteralExpressionStatement,
1132 kUseStrictExpressionStatement,
1133 kFunctionDeclaration
1134 };
1135
1136 explicit Statement(Type code) : code_(code) {}
1137 Type code_;
1138 };
1139 1159
1140 enum SourceElements { 1160 enum SourceElements {
1141 kUnknownSourceElements 1161 kUnknownSourceElements
1142 }; 1162 };
1143 1163
1144 // All ParseXXX functions take as the last argument an *ok parameter 1164 // All ParseXXX functions take as the last argument an *ok parameter
marja 2014/04/24 15:31:10 So you don't need to change these lines.
1145 // which is set to false if parsing failed; it is unchanged otherwise. 1165 // which is set to false if parsing failed; it is unchanged otherwise.
1146 // By making the 'exception handling' explicit, we are forced to check 1166 // By making the 'exception handling' explicit, we are forced to check
1147 // for failure at the call sites. 1167 // for failure at the call sites.
1148 Statement ParseSourceElement(bool* ok); 1168 PreParserStatement ParseSourceElement(bool* ok);
1149 SourceElements ParseSourceElements(int end_token, bool* ok); 1169 SourceElements ParseSourceElements(int end_token, bool* ok);
1150 Statement ParseStatement(bool* ok); 1170 PreParserStatement ParseStatement(bool* ok);
1151 Statement ParseFunctionDeclaration(bool* ok); 1171 PreParserStatement ParseFunctionDeclaration(bool* ok);
1152 Statement ParseBlock(bool* ok); 1172 PreParserStatement ParseBlock(bool* ok);
1153 Statement ParseVariableStatement(VariableDeclarationContext var_context, 1173 PreParserStatement ParseVariableStatement(
1154 bool* ok); 1174 VariableDeclarationContext var_context,
1155 Statement ParseVariableDeclarations(VariableDeclarationContext var_context, 1175 bool* ok);
1156 VariableDeclarationProperties* decl_props, 1176 PreParserStatement ParseVariableDeclarations(
1157 int* num_decl, 1177 VariableDeclarationContext var_context,
1158 bool* ok); 1178 VariableDeclarationProperties* decl_props,
1159 Statement ParseExpressionOrLabelledStatement(bool* ok); 1179 int* num_decl,
1160 Statement ParseIfStatement(bool* ok); 1180 bool* ok);
1161 Statement ParseContinueStatement(bool* ok); 1181 PreParserStatement ParseExpressionOrLabelledStatement(bool* ok);
1162 Statement ParseBreakStatement(bool* ok); 1182 PreParserStatement ParseIfStatement(bool* ok);
1163 Statement ParseReturnStatement(bool* ok); 1183 PreParserStatement ParseContinueStatement(bool* ok);
1164 Statement ParseWithStatement(bool* ok); 1184 PreParserStatement ParseBreakStatement(bool* ok);
1165 Statement ParseSwitchStatement(bool* ok); 1185 PreParserStatement ParseReturnStatement(bool* ok);
1166 Statement ParseDoWhileStatement(bool* ok); 1186 PreParserStatement ParseWithStatement(bool* ok);
1167 Statement ParseWhileStatement(bool* ok); 1187 PreParserStatement ParseSwitchStatement(bool* ok);
1168 Statement ParseForStatement(bool* ok); 1188 PreParserStatement ParseDoWhileStatement(bool* ok);
1169 Statement ParseThrowStatement(bool* ok); 1189 PreParserStatement ParseWhileStatement(bool* ok);
1170 Statement ParseTryStatement(bool* ok); 1190 PreParserStatement ParseForStatement(bool* ok);
1171 Statement ParseDebuggerStatement(bool* ok); 1191 PreParserStatement ParseThrowStatement(bool* ok);
1192 PreParserStatement ParseTryStatement(bool* ok);
1193 PreParserStatement ParseDebuggerStatement(bool* ok);
1172 Expression ParseConditionalExpression(bool accept_IN, bool* ok); 1194 Expression ParseConditionalExpression(bool accept_IN, bool* ok);
1173 Expression ParseObjectLiteral(bool* ok); 1195 Expression ParseObjectLiteral(bool* ok);
1174 Expression ParseV8Intrinsic(bool* ok); 1196 Expression ParseV8Intrinsic(bool* ok);
1175 1197
1176 Expression ParseFunctionLiteral( 1198 Expression ParseFunctionLiteral(
1177 Identifier name, 1199 Identifier name,
1178 Scanner::Location function_name_location, 1200 Scanner::Location function_name_location,
1179 bool name_is_strict_reserved, 1201 bool name_is_strict_reserved,
1180 bool is_generator, 1202 bool is_generator,
1181 int function_token_pos, 1203 int function_token_pos,
(...skipping 990 matching lines...) Expand 10 before | Expand all | Expand 10 after
2172 "accessor_get_set"); 2194 "accessor_get_set");
2173 } 2195 }
2174 *ok = false; 2196 *ok = false;
2175 } 2197 }
2176 } 2198 }
2177 2199
2178 2200
2179 } } // v8::internal 2201 } } // v8::internal
2180 2202
2181 #endif // V8_PREPARSER_H 2203 #endif // V8_PREPARSER_H
OLDNEW
« no previous file with comments | « src/parser.h ('k') | src/preparser.cc » ('j') | src/preparser.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698