| OLD | NEW |
| 1 // Copyright 2015 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_PARSING_EXPRESSION_CLASSIFIER_H | 5 #ifndef V8_PARSING_EXPRESSION_CLASSIFIER_H |
| 6 #define V8_PARSING_EXPRESSION_CLASSIFIER_H | 6 #define V8_PARSING_EXPRESSION_CLASSIFIER_H |
| 7 | 7 |
| 8 #include "src/messages.h" | 8 #include "src/messages.h" |
| 9 #include "src/parsing/scanner.h" | 9 #include "src/parsing/scanner.h" |
| 10 #include "src/parsing/token.h" | 10 #include "src/parsing/token.h" |
| 11 | 11 |
| 12 namespace v8 { | 12 namespace v8 { |
| 13 namespace internal { | 13 namespace internal { |
| 14 | 14 |
| 15 | 15 |
| 16 class ExpressionClassifier { | 16 class ExpressionClassifier { |
| 17 public: | 17 public: |
| 18 struct Error { | 18 struct Error { |
| 19 Error() | 19 Error() |
| 20 : location(Scanner::Location::invalid()), | 20 : location(Scanner::Location::invalid()), |
| 21 message(MessageTemplate::kNone), | 21 message(MessageTemplate::kNone), |
| 22 type(kSyntaxError), |
| 22 arg(nullptr) {} | 23 arg(nullptr) {} |
| 23 | 24 |
| 24 Scanner::Location location; | 25 Scanner::Location location; |
| 25 MessageTemplate::Template message; | 26 MessageTemplate::Template message : 30; |
| 27 ParseErrorType type : 2; |
| 26 const char* arg; | 28 const char* arg; |
| 27 }; | 29 }; |
| 28 | 30 |
| 29 enum TargetProduction { | 31 enum TargetProduction { |
| 30 ExpressionProduction = 1 << 0, | 32 ExpressionProduction = 1 << 0, |
| 31 FormalParameterInitializerProduction = 1 << 1, | 33 FormalParameterInitializerProduction = 1 << 1, |
| 32 BindingPatternProduction = 1 << 2, | 34 BindingPatternProduction = 1 << 2, |
| 33 AssignmentPatternProduction = 1 << 3, | 35 AssignmentPatternProduction = 1 << 3, |
| 34 DistinctFormalParametersProduction = 1 << 4, | 36 DistinctFormalParametersProduction = 1 << 4, |
| 35 StrictModeFormalParametersProduction = 1 << 5, | 37 StrictModeFormalParametersProduction = 1 << 5, |
| 36 StrongModeFormalParametersProduction = 1 << 6, | 38 StrongModeFormalParametersProduction = 1 << 6, |
| 37 ArrowFormalParametersProduction = 1 << 7, | 39 ArrowFormalParametersProduction = 1 << 7, |
| 38 LetPatternProduction = 1 << 8, | 40 LetPatternProduction = 1 << 8, |
| 41 CoverInitializedNameProduction = 1 << 9, |
| 39 | 42 |
| 40 ExpressionProductions = | 43 ExpressionProductions = |
| 41 (ExpressionProduction | FormalParameterInitializerProduction), | 44 (ExpressionProduction | FormalParameterInitializerProduction), |
| 42 PatternProductions = (BindingPatternProduction | | 45 PatternProductions = (BindingPatternProduction | |
| 43 AssignmentPatternProduction | LetPatternProduction), | 46 AssignmentPatternProduction | LetPatternProduction), |
| 44 FormalParametersProductions = (DistinctFormalParametersProduction | | 47 FormalParametersProductions = (DistinctFormalParametersProduction | |
| 45 StrictModeFormalParametersProduction | | 48 StrictModeFormalParametersProduction | |
| 46 StrongModeFormalParametersProduction), | 49 StrongModeFormalParametersProduction), |
| 47 StandardProductions = ExpressionProductions | PatternProductions, | 50 StandardProductions = ExpressionProductions | PatternProductions, |
| 48 AllProductions = (StandardProductions | FormalParametersProductions | | 51 AllProductions = |
| 49 ArrowFormalParametersProduction) | 52 (StandardProductions | FormalParametersProductions | |
| 53 ArrowFormalParametersProduction | CoverInitializedNameProduction) |
| 50 }; | 54 }; |
| 51 | 55 |
| 52 enum FunctionProperties { NonSimpleParameter = 1 << 0 }; | 56 enum FunctionProperties { NonSimpleParameter = 1 << 0 }; |
| 53 | 57 |
| 54 ExpressionClassifier() | 58 ExpressionClassifier() |
| 55 : invalid_productions_(0), | 59 : invalid_productions_(0), |
| 56 function_properties_(0), | 60 function_properties_(0), |
| 57 duplicate_finder_(nullptr) {} | 61 duplicate_finder_(nullptr) {} |
| 58 | 62 |
| 59 explicit ExpressionClassifier(DuplicateFinder* duplicate_finder) | 63 explicit ExpressionClassifier(DuplicateFinder* duplicate_finder) |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 const Error& strict_mode_formal_parameter_error() const { | 130 const Error& strict_mode_formal_parameter_error() const { |
| 127 return strict_mode_formal_parameter_error_; | 131 return strict_mode_formal_parameter_error_; |
| 128 } | 132 } |
| 129 | 133 |
| 130 const Error& strong_mode_formal_parameter_error() const { | 134 const Error& strong_mode_formal_parameter_error() const { |
| 131 return strong_mode_formal_parameter_error_; | 135 return strong_mode_formal_parameter_error_; |
| 132 } | 136 } |
| 133 | 137 |
| 134 const Error& let_pattern_error() const { return let_pattern_error_; } | 138 const Error& let_pattern_error() const { return let_pattern_error_; } |
| 135 | 139 |
| 140 bool has_cover_initialized_name() const { |
| 141 return !is_valid(CoverInitializedNameProduction); |
| 142 } |
| 143 const Error& cover_initialized_name_error() const { |
| 144 return cover_initialized_name_error_; |
| 145 } |
| 146 |
| 136 bool is_simple_parameter_list() const { | 147 bool is_simple_parameter_list() const { |
| 137 return !(function_properties_ & NonSimpleParameter); | 148 return !(function_properties_ & NonSimpleParameter); |
| 138 } | 149 } |
| 139 | 150 |
| 140 void RecordNonSimpleParameter() { | 151 void RecordNonSimpleParameter() { |
| 141 function_properties_ |= NonSimpleParameter; | 152 function_properties_ |= NonSimpleParameter; |
| 142 } | 153 } |
| 143 | 154 |
| 144 void RecordExpressionError(const Scanner::Location& loc, | 155 void RecordExpressionError(const Scanner::Location& loc, |
| 145 MessageTemplate::Template message, | 156 MessageTemplate::Template message, |
| 146 const char* arg = nullptr) { | 157 const char* arg = nullptr) { |
| 147 if (!is_valid_expression()) return; | 158 if (!is_valid_expression()) return; |
| 148 invalid_productions_ |= ExpressionProduction; | 159 invalid_productions_ |= ExpressionProduction; |
| 149 expression_error_.location = loc; | 160 expression_error_.location = loc; |
| 150 expression_error_.message = message; | 161 expression_error_.message = message; |
| 151 expression_error_.arg = arg; | 162 expression_error_.arg = arg; |
| 152 } | 163 } |
| 153 | 164 |
| 165 void RecordExpressionError(const Scanner::Location& loc, |
| 166 MessageTemplate::Template message, |
| 167 ParseErrorType type, const char* arg = nullptr) { |
| 168 if (!is_valid_expression()) return; |
| 169 invalid_productions_ |= ExpressionProduction; |
| 170 expression_error_.location = loc; |
| 171 expression_error_.message = message; |
| 172 expression_error_.arg = arg; |
| 173 expression_error_.type = type; |
| 174 } |
| 175 |
| 154 void RecordFormalParameterInitializerError(const Scanner::Location& loc, | 176 void RecordFormalParameterInitializerError(const Scanner::Location& loc, |
| 155 MessageTemplate::Template message, | 177 MessageTemplate::Template message, |
| 156 const char* arg = nullptr) { | 178 const char* arg = nullptr) { |
| 157 if (!is_valid_formal_parameter_initializer()) return; | 179 if (!is_valid_formal_parameter_initializer()) return; |
| 158 invalid_productions_ |= FormalParameterInitializerProduction; | 180 invalid_productions_ |= FormalParameterInitializerProduction; |
| 159 formal_parameter_initializer_error_.location = loc; | 181 formal_parameter_initializer_error_.location = loc; |
| 160 formal_parameter_initializer_error_.message = message; | 182 formal_parameter_initializer_error_.message = message; |
| 161 formal_parameter_initializer_error_.arg = arg; | 183 formal_parameter_initializer_error_.arg = arg; |
| 162 } | 184 } |
| 163 | 185 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 174 void RecordAssignmentPatternError(const Scanner::Location& loc, | 196 void RecordAssignmentPatternError(const Scanner::Location& loc, |
| 175 MessageTemplate::Template message, | 197 MessageTemplate::Template message, |
| 176 const char* arg = nullptr) { | 198 const char* arg = nullptr) { |
| 177 if (!is_valid_assignment_pattern()) return; | 199 if (!is_valid_assignment_pattern()) return; |
| 178 invalid_productions_ |= AssignmentPatternProduction; | 200 invalid_productions_ |= AssignmentPatternProduction; |
| 179 assignment_pattern_error_.location = loc; | 201 assignment_pattern_error_.location = loc; |
| 180 assignment_pattern_error_.message = message; | 202 assignment_pattern_error_.message = message; |
| 181 assignment_pattern_error_.arg = arg; | 203 assignment_pattern_error_.arg = arg; |
| 182 } | 204 } |
| 183 | 205 |
| 206 void RecordPatternError(const Scanner::Location& loc, |
| 207 MessageTemplate::Template message, |
| 208 const char* arg = nullptr) { |
| 209 RecordBindingPatternError(loc, message, arg); |
| 210 RecordAssignmentPatternError(loc, message, arg); |
| 211 } |
| 212 |
| 184 void RecordArrowFormalParametersError(const Scanner::Location& loc, | 213 void RecordArrowFormalParametersError(const Scanner::Location& loc, |
| 185 MessageTemplate::Template message, | 214 MessageTemplate::Template message, |
| 186 const char* arg = nullptr) { | 215 const char* arg = nullptr) { |
| 187 if (!is_valid_arrow_formal_parameters()) return; | 216 if (!is_valid_arrow_formal_parameters()) return; |
| 188 invalid_productions_ |= ArrowFormalParametersProduction; | 217 invalid_productions_ |= ArrowFormalParametersProduction; |
| 189 arrow_formal_parameters_error_.location = loc; | 218 arrow_formal_parameters_error_.location = loc; |
| 190 arrow_formal_parameters_error_.message = message; | 219 arrow_formal_parameters_error_.message = message; |
| 191 arrow_formal_parameters_error_.arg = arg; | 220 arrow_formal_parameters_error_.arg = arg; |
| 192 } | 221 } |
| 193 | 222 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 void RecordLetPatternError(const Scanner::Location& loc, | 254 void RecordLetPatternError(const Scanner::Location& loc, |
| 226 MessageTemplate::Template message, | 255 MessageTemplate::Template message, |
| 227 const char* arg = nullptr) { | 256 const char* arg = nullptr) { |
| 228 if (!is_valid_let_pattern()) return; | 257 if (!is_valid_let_pattern()) return; |
| 229 invalid_productions_ |= LetPatternProduction; | 258 invalid_productions_ |= LetPatternProduction; |
| 230 let_pattern_error_.location = loc; | 259 let_pattern_error_.location = loc; |
| 231 let_pattern_error_.message = message; | 260 let_pattern_error_.message = message; |
| 232 let_pattern_error_.arg = arg; | 261 let_pattern_error_.arg = arg; |
| 233 } | 262 } |
| 234 | 263 |
| 264 void RecordCoverInitializedNameError(const Scanner::Location& loc, |
| 265 MessageTemplate::Template message, |
| 266 const char* arg = nullptr) { |
| 267 if (has_cover_initialized_name()) return; |
| 268 invalid_productions_ |= CoverInitializedNameProduction; |
| 269 cover_initialized_name_error_.location = loc; |
| 270 cover_initialized_name_error_.message = message; |
| 271 cover_initialized_name_error_.arg = arg; |
| 272 } |
| 273 |
| 274 void ForgiveCoverInitializedNameError() { |
| 275 invalid_productions_ &= ~CoverInitializedNameProduction; |
| 276 cover_initialized_name_error_ = Error(); |
| 277 } |
| 278 |
| 279 void ForgiveAssignmentPatternError() { |
| 280 invalid_productions_ &= ~AssignmentPatternProduction; |
| 281 assignment_pattern_error_ = Error(); |
| 282 } |
| 283 |
| 235 void Accumulate(const ExpressionClassifier& inner, | 284 void Accumulate(const ExpressionClassifier& inner, |
| 236 unsigned productions = StandardProductions) { | 285 unsigned productions = StandardProductions) { |
| 237 // Propagate errors from inner, but don't overwrite already recorded | 286 // Propagate errors from inner, but don't overwrite already recorded |
| 238 // errors. | 287 // errors. |
| 239 unsigned non_arrow_inner_invalid_productions = | 288 unsigned non_arrow_inner_invalid_productions = |
| 240 inner.invalid_productions_ & ~ArrowFormalParametersProduction; | 289 inner.invalid_productions_ & ~ArrowFormalParametersProduction; |
| 241 if (non_arrow_inner_invalid_productions == 0) return; | 290 if (non_arrow_inner_invalid_productions == 0) return; |
| 242 unsigned non_arrow_productions = | 291 unsigned non_arrow_productions = |
| 243 productions & ~ArrowFormalParametersProduction; | 292 productions & ~ArrowFormalParametersProduction; |
| 244 unsigned errors = | 293 unsigned errors = |
| (...skipping 14 matching lines...) Expand all Loading... |
| 259 duplicate_formal_parameter_error_ = | 308 duplicate_formal_parameter_error_ = |
| 260 inner.duplicate_formal_parameter_error_; | 309 inner.duplicate_formal_parameter_error_; |
| 261 if (errors & StrictModeFormalParametersProduction) | 310 if (errors & StrictModeFormalParametersProduction) |
| 262 strict_mode_formal_parameter_error_ = | 311 strict_mode_formal_parameter_error_ = |
| 263 inner.strict_mode_formal_parameter_error_; | 312 inner.strict_mode_formal_parameter_error_; |
| 264 if (errors & StrongModeFormalParametersProduction) | 313 if (errors & StrongModeFormalParametersProduction) |
| 265 strong_mode_formal_parameter_error_ = | 314 strong_mode_formal_parameter_error_ = |
| 266 inner.strong_mode_formal_parameter_error_; | 315 inner.strong_mode_formal_parameter_error_; |
| 267 if (errors & LetPatternProduction) | 316 if (errors & LetPatternProduction) |
| 268 let_pattern_error_ = inner.let_pattern_error_; | 317 let_pattern_error_ = inner.let_pattern_error_; |
| 318 if (errors & CoverInitializedNameProduction) |
| 319 cover_initialized_name_error_ = inner.cover_initialized_name_error_; |
| 269 } | 320 } |
| 270 | 321 |
| 271 // As an exception to the above, the result continues to be a valid arrow | 322 // As an exception to the above, the result continues to be a valid arrow |
| 272 // formal parameters if the inner expression is a valid binding pattern. | 323 // formal parameters if the inner expression is a valid binding pattern. |
| 273 if (productions & ArrowFormalParametersProduction && | 324 if (productions & ArrowFormalParametersProduction && |
| 274 is_valid_arrow_formal_parameters()) { | 325 is_valid_arrow_formal_parameters()) { |
| 275 // Also copy function properties if expecting an arrow function | 326 // Also copy function properties if expecting an arrow function |
| 276 // parameter. | 327 // parameter. |
| 277 function_properties_ |= inner.function_properties_; | 328 function_properties_ |= inner.function_properties_; |
| 278 | 329 |
| 279 if (!inner.is_valid_binding_pattern()) { | 330 if (!inner.is_valid_binding_pattern()) { |
| 280 invalid_productions_ |= ArrowFormalParametersProduction; | 331 invalid_productions_ |= ArrowFormalParametersProduction; |
| 281 arrow_formal_parameters_error_ = inner.binding_pattern_error_; | 332 arrow_formal_parameters_error_ = inner.binding_pattern_error_; |
| 282 } | 333 } |
| 283 } | 334 } |
| 284 } | 335 } |
| 285 | 336 |
| 286 private: | 337 private: |
| 287 unsigned invalid_productions_; | 338 unsigned invalid_productions_; |
| 288 unsigned function_properties_; | 339 unsigned function_properties_; |
| 289 Error expression_error_; | 340 Error expression_error_; |
| 290 Error formal_parameter_initializer_error_; | 341 Error formal_parameter_initializer_error_; |
| 291 Error binding_pattern_error_; | 342 Error binding_pattern_error_; |
| 292 Error assignment_pattern_error_; | 343 Error assignment_pattern_error_; |
| 293 Error arrow_formal_parameters_error_; | 344 Error arrow_formal_parameters_error_; |
| 294 Error duplicate_formal_parameter_error_; | 345 Error duplicate_formal_parameter_error_; |
| 295 Error strict_mode_formal_parameter_error_; | 346 Error strict_mode_formal_parameter_error_; |
| 296 Error strong_mode_formal_parameter_error_; | 347 Error strong_mode_formal_parameter_error_; |
| 297 Error let_pattern_error_; | 348 Error let_pattern_error_; |
| 349 Error cover_initialized_name_error_; |
| 298 DuplicateFinder* duplicate_finder_; | 350 DuplicateFinder* duplicate_finder_; |
| 299 }; | 351 }; |
| 300 | 352 |
| 301 } // namespace internal | 353 } // namespace internal |
| 302 } // namespace v8 | 354 } // namespace v8 |
| 303 | 355 |
| 304 #endif // V8_PARSING_EXPRESSION_CLASSIFIER_H | 356 #endif // V8_PARSING_EXPRESSION_CLASSIFIER_H |
| OLD | NEW |