OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 #include <cmath> | 5 #include <cmath> |
6 | 6 |
7 #include "src/allocation.h" | 7 #include "src/allocation.h" |
8 #include "src/base/logging.h" | 8 #include "src/base/logging.h" |
9 #include "src/conversions-inl.h" | 9 #include "src/conversions-inl.h" |
10 #include "src/conversions.h" | 10 #include "src/conversions.h" |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 | 114 |
115 // The PreParser checks that the syntax follows the grammar for JavaScript, | 115 // The PreParser checks that the syntax follows the grammar for JavaScript, |
116 // and collects some information about the program along the way. | 116 // and collects some information about the program along the way. |
117 // The grammar check is only performed in order to understand the program | 117 // The grammar check is only performed in order to understand the program |
118 // sufficiently to deduce some information about it, that can be used | 118 // sufficiently to deduce some information about it, that can be used |
119 // to speed up later parsing. Finding errors is not the goal of pre-parsing, | 119 // to speed up later parsing. Finding errors is not the goal of pre-parsing, |
120 // rather it is to speed up properly written and correct programs. | 120 // rather it is to speed up properly written and correct programs. |
121 // That means that contextual checks (like a label being declared where | 121 // That means that contextual checks (like a label being declared where |
122 // it is used) are generally omitted. | 122 // it is used) are generally omitted. |
123 | 123 |
124 PreParser::Statement PreParser::ParseHoistableDeclaration( | |
125 int pos, ParseFunctionFlags flags, ZoneList<const AstRawString*>* names, | |
126 bool default_export, bool* ok) { | |
127 const bool is_generator = flags & ParseFunctionFlags::kIsGenerator; | |
128 const bool is_async = flags & ParseFunctionFlags::kIsAsync; | |
129 DCHECK(!is_generator || !is_async); | |
130 | |
131 bool is_strict_reserved = false; | |
132 Identifier name = ParseIdentifierOrStrictReservedWord( | |
133 &is_strict_reserved, CHECK_OK); | |
134 | |
135 ParseFunctionLiteral(name, scanner()->location(), | |
136 is_strict_reserved ? kFunctionNameIsStrictReserved | |
137 : kFunctionNameValidityUnknown, | |
138 is_generator ? FunctionKind::kGeneratorFunction | |
139 : is_async ? FunctionKind::kAsyncFunction | |
140 : FunctionKind::kNormalFunction, | |
141 pos, FunctionLiteral::kDeclaration, language_mode(), | |
142 CHECK_OK); | |
143 return Statement::FunctionDeclaration(); | |
144 } | |
145 | |
146 PreParser::Statement PreParser::ParseAsyncFunctionDeclaration( | 124 PreParser::Statement PreParser::ParseAsyncFunctionDeclaration( |
147 ZoneList<const AstRawString*>* names, bool default_export, bool* ok) { | 125 ZoneList<const AstRawString*>* names, bool default_export, bool* ok) { |
148 // AsyncFunctionDeclaration :: | 126 // AsyncFunctionDeclaration :: |
149 // async [no LineTerminator here] function BindingIdentifier[Await] | 127 // async [no LineTerminator here] function BindingIdentifier[Await] |
150 // ( FormalParameters[Await] ) { AsyncFunctionBody } | 128 // ( FormalParameters[Await] ) { AsyncFunctionBody } |
151 DCHECK_EQ(scanner()->current_token(), Token::ASYNC); | 129 DCHECK_EQ(scanner()->current_token(), Token::ASYNC); |
152 int pos = position(); | 130 int pos = position(); |
153 Expect(Token::FUNCTION, CHECK_OK); | 131 Expect(Token::FUNCTION, CHECK_OK); |
154 ParseFunctionFlags flags = ParseFunctionFlags::kIsAsync; | 132 ParseFunctionFlags flags = ParseFunctionFlags::kIsAsync; |
155 return ParseHoistableDeclaration(pos, flags, names, default_export, ok); | 133 return ParseHoistableDeclaration(pos, flags, names, default_export, ok); |
156 } | 134 } |
157 | 135 |
158 PreParser::Statement PreParser::ParseHoistableDeclaration( | |
159 ZoneList<const AstRawString*>* names, bool default_export, bool* ok) { | |
160 // FunctionDeclaration :: | |
161 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}' | |
162 // GeneratorDeclaration :: | |
163 // 'function' '*' Identifier '(' FormalParameterListopt ')' | |
164 // '{' FunctionBody '}' | |
165 | |
166 Expect(Token::FUNCTION, CHECK_OK); | |
167 int pos = position(); | |
168 ParseFunctionFlags flags = ParseFunctionFlags::kIsNormal; | |
169 if (Check(Token::MUL)) { | |
170 flags |= ParseFunctionFlags::kIsGenerator; | |
171 } | |
172 return ParseHoistableDeclaration(pos, flags, names, default_export, ok); | |
173 } | |
174 | |
175 PreParser::Statement PreParser::ParseClassDeclaration( | 136 PreParser::Statement PreParser::ParseClassDeclaration( |
176 ZoneList<const AstRawString*>* names, bool default_export, bool* ok) { | 137 ZoneList<const AstRawString*>* names, bool default_export, bool* ok) { |
177 int pos = position(); | 138 int pos = position(); |
178 bool is_strict_reserved = false; | 139 bool is_strict_reserved = false; |
179 Identifier name = | 140 Identifier name = |
180 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK); | 141 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK); |
181 ExpressionClassifier no_classifier(this); | 142 ExpressionClassifier no_classifier(this); |
182 ParseClassLiteral(name, scanner()->location(), is_strict_reserved, pos, | 143 ParseClassLiteral(name, scanner()->location(), is_strict_reserved, pos, |
183 CHECK_OK); | 144 CHECK_OK); |
184 return Statement::Default(); | 145 return Statement::Default(); |
185 } | 146 } |
186 | 147 |
187 PreParser::Statement PreParser::ParseFunctionDeclaration(bool* ok) { | 148 PreParser::Statement PreParser::ParseFunctionDeclaration(bool* ok) { |
188 Consume(Token::FUNCTION); | 149 Consume(Token::FUNCTION); |
189 int pos = position(); | 150 int pos = position(); |
190 ParseFunctionFlags flags = ParseFunctionFlags::kIsNormal; | 151 ParseFunctionFlags flags = ParseFunctionFlags::kIsNormal; |
191 if (Check(Token::MUL)) { | 152 if (Check(Token::MUL)) { |
192 flags |= ParseFunctionFlags::kIsGenerator; | 153 flags |= ParseFunctionFlags::kIsGenerator; |
193 if (allow_harmony_restrictive_declarations()) { | 154 if (allow_harmony_restrictive_declarations()) { |
194 ReportMessageAt(scanner()->location(), | 155 ReportMessageAt(scanner()->location(), |
195 MessageTemplate::kGeneratorInLegacyContext); | 156 MessageTemplate::kGeneratorInLegacyContext); |
196 *ok = false; | 157 *ok = false; |
197 return Statement::Default(); | 158 return Statement::Default(); |
198 } | 159 } |
199 } | 160 } |
| 161 // PreParser is not able to parse "export default" yet (since PreParser is |
| 162 // at the moment only used for functions, and it cannot occur |
| 163 // there). TODO(marja): update this when it is. |
200 return ParseHoistableDeclaration(pos, flags, nullptr, false, ok); | 164 return ParseHoistableDeclaration(pos, flags, nullptr, false, ok); |
201 } | 165 } |
202 | 166 |
203 PreParser::Statement PreParser::ParseExpressionOrLabelledStatement( | 167 PreParser::Statement PreParser::ParseExpressionOrLabelledStatement( |
204 ZoneList<const AstRawString*>* names, | 168 ZoneList<const AstRawString*>* names, |
205 AllowLabelledFunctionStatement allow_function, bool* ok) { | 169 AllowLabelledFunctionStatement allow_function, bool* ok) { |
206 // ExpressionStatement | LabelledStatement :: | 170 // ExpressionStatement | LabelledStatement :: |
207 // Expression ';' | 171 // Expression ';' |
208 // Identifier ':' Statement | 172 // Identifier ':' Statement |
209 | 173 |
(...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
836 | 800 |
837 body->Add(PreParserStatement::ExpressionStatement(return_value), zone()); | 801 body->Add(PreParserStatement::ExpressionStatement(return_value), zone()); |
838 } | 802 } |
839 | 803 |
840 #undef CHECK_OK | 804 #undef CHECK_OK |
841 #undef CHECK_OK_CUSTOM | 805 #undef CHECK_OK_CUSTOM |
842 | 806 |
843 | 807 |
844 } // namespace internal | 808 } // namespace internal |
845 } // namespace v8 | 809 } // namespace v8 |
OLD | NEW |