Changeset 192597 in webkit for trunk/Source/JavaScriptCore/parser/Parser.cpp
- Timestamp:
- Nov 18, 2015, 4:03:26 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/parser/Parser.cpp
r192586 r192597 668 668 669 669 template <typename LexerType> 670 template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::createBindingPattern(TreeBuilder& context, DestructuringKind kind, ExportType exportType, const Identifier& name, JSToken token, AssignmentContext bindingContext, const Identifier** duplicateIdentifier)670 template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::createBindingPattern(TreeBuilder& context, DestructuringKind kind, ExportType exportType, const Identifier& name, int depth, JSToken token, AssignmentContext bindingContext, const Identifier** duplicateIdentifier) 671 671 { 672 672 ASSERT(!name.isNull()); … … 686 686 } 687 687 } else if (kind == DestructureToParameters) { 688 DeclarationResultMask declarationResult = declareParameter(&name); 689 if ((declarationResult & DeclarationResult::InvalidStrictMode) && strictMode()) { 690 semanticFailIfTrue(isEvalOrArguments(&name), "Cannot destructure to a parameter name '", name.impl(), "' in strict mode"); 691 if (m_lastFunctionName && name == *m_lastFunctionName) 692 semanticFail("Cannot declare a parameter named '", name.impl(), "' as it shadows the name of a strict mode function"); 693 semanticFailureDueToKeyword("parameter name"); 694 if (hasDeclaredParameter(name)) 695 semanticFail("Cannot declare a parameter named '", name.impl(), "' in strict mode as it has already been declared"); 696 semanticFail("Cannot declare a parameter named '", name.impl(), "' in strict mode"); 697 } 698 if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration) { 699 // It's not always an error to define a duplicate parameter. 700 // It's only an error when there are default parameter values or destructuring parameters. 701 // We note this value now so we can check it later. 702 if (duplicateIdentifier) 703 *duplicateIdentifier = &name; 688 if (depth) { 689 auto bindingResult = declareBoundParameter(&name); 690 if (bindingResult == Scope::StrictBindingFailed && strictMode()) { 691 semanticFailIfTrue(isEvalOrArguments(&name), "Cannot destructure to a parameter name '", name.impl(), "' in strict mode"); 692 if (m_lastFunctionName && name == *m_lastFunctionName) 693 semanticFail("Cannot destructure to '", name.impl(), "' as it shadows the name of a strict mode function"); 694 semanticFailureDueToKeyword("bound parameter name"); 695 if (hasDeclaredParameter(name)) 696 semanticFail("Cannot destructure to '", name.impl(), "' as it has already been declared"); 697 semanticFail("Cannot bind to a parameter named '", name.impl(), "' in strict mode"); 698 } 699 if (bindingResult == Scope::BindingFailed) { 700 semanticFailureDueToKeyword("bound parameter name"); 701 if (hasDeclaredParameter(name)) 702 semanticFail("Cannot destructure to '", name.impl(), "' as it has already been declared"); 703 semanticFail("Cannot destructure to a parameter named '", name.impl(), "'"); 704 } 705 } else { 706 DeclarationResultMask declarationResult = declareParameter(&name); 707 if ((declarationResult & DeclarationResult::InvalidStrictMode) && strictMode()) { 708 semanticFailIfTrue(isEvalOrArguments(&name), "Cannot destructure to a parameter name '", name.impl(), "' in strict mode"); 709 if (m_lastFunctionName && name == *m_lastFunctionName) 710 semanticFail("Cannot declare a parameter named '", name.impl(), "' as it shadows the name of a strict mode function"); 711 semanticFailureDueToKeyword("parameter name"); 712 if (hasDeclaredParameter(name)) 713 semanticFail("Cannot declare a parameter named '", name.impl(), "' in strict mode as it has already been declared"); 714 semanticFail("Cannot declare a parameter named '", name.impl(), "' in strict mode"); 715 } 716 if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration) { 717 // It's not always an error to define a duplicate parameter. 718 // It's only an error when there are default parameter values or destructuring parameters. 719 // We note this value now so we can check it later. 720 if (duplicateIdentifier) 721 *duplicateIdentifier = &name; 722 } 704 723 } 705 724 } … … 710 729 } 711 730 return context.createBindingLocation(token.m_location, name, token.m_startPosition, token.m_endPosition, bindingContext); 712 }713 714 template <typename LexerType>715 template <class TreeBuilder> NEVER_INLINE TreeDestructuringPattern Parser<LexerType>::createAssignmentElement(TreeBuilder& context, TreeExpression& assignmentTarget, const JSTextPosition& startPosition, const JSTextPosition& endPosition)716 {717 return context.createAssignmentElement(assignmentTarget, startPosition, endPosition);718 731 } 719 732 … … 751 764 { 752 765 return parseDestructuringPattern(context, DestructureToExpressions, ExportType::NotExported, nullptr, nullptr, bindingContext); 753 }754 755 template <typename LexerType>756 template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::parseBindingOrAssignmentElement(TreeBuilder& context, DestructuringKind kind, ExportType exportType, const Identifier** duplicateIdentifier, bool* hasDestructuringPattern, AssignmentContext bindingContext, int depth)757 {758 if (kind == DestructureToExpressions)759 return parseAssignmentElement(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth);760 return parseDestructuringPattern(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth);761 }762 763 template <typename LexerType>764 template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::parseAssignmentElement(TreeBuilder& context, DestructuringKind kind, ExportType exportType, const Identifier** duplicateIdentifier, bool* hasDestructuringPattern, AssignmentContext bindingContext, int depth)765 {766 SavePoint savePoint = createSavePoint();767 TreeDestructuringPattern assignmentTarget = 0;768 769 if (match(OPENBRACE) || match(OPENBRACKET))770 assignmentTarget = parseDestructuringPattern(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth);771 if (!assignmentTarget || match(DOT) || match(OPENBRACKET) || match(OPENPAREN) || match(TEMPLATE)) {772 restoreSavePoint(savePoint);773 JSTextPosition startPosition = tokenStartPosition();774 auto element = parseMemberExpression(context);775 776 semanticFailIfFalse(element && context.isAssignmentLocation(element), "Invalid destructuring assignment target");777 778 return createAssignmentElement(context, element, startPosition, lastTokenEndPosition());779 }780 return assignmentTarget;781 766 } 782 767 … … 811 796 JSTokenLocation location = m_token.m_location; 812 797 next(); 813 auto innerPattern = parse BindingOrAssignmentElement(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1);798 auto innerPattern = parseDestructuringPattern(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1); 814 799 if (kind == DestructureToExpressions && !innerPattern) 815 800 return 0; … … 824 809 825 810 JSTokenLocation location = m_token.m_location; 826 auto innerPattern = parse BindingOrAssignmentElement(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1);811 auto innerPattern = parseDestructuringPattern(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1); 827 812 if (kind == DestructureToExpressions && !innerPattern) 828 813 return 0; … … 832 817 } while (consume(COMMA)); 833 818 819 if (kind == DestructureToExpressions && !match(CLOSEBRACKET)) 820 return 0; 834 821 consumeOrFail(CLOSEBRACKET, restElementWasFound ? "Expected a closing ']' following a rest element destructuring pattern" : "Expected either a closing ']' or a ',' following an element destructuring pattern"); 835 822 context.finishArrayPattern(arrayPattern, divotStart, divotStart, lastTokenEndPosition()); … … 859 846 next(); 860 847 if (consume(COLON)) 861 innerPattern = parse BindingOrAssignmentElement(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1);848 innerPattern = parseDestructuringPattern(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1); 862 849 else 863 innerPattern = createBindingPattern(context, kind, exportType, *propertyName, identifierToken, bindingContext, duplicateIdentifier);850 innerPattern = createBindingPattern(context, kind, exportType, *propertyName, depth + 1, identifierToken, bindingContext, duplicateIdentifier); 864 851 } else { 865 852 JSTokenType tokenType = m_token.m_type; … … 892 879 failWithMessage("Expected a ':' prior to a named destructuring property"); 893 880 } 894 innerPattern = parse BindingOrAssignmentElement(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1);881 innerPattern = parseDestructuringPattern(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1); 895 882 } 896 883 if (kind == DestructureToExpressions && !innerPattern) … … 917 904 } 918 905 failIfTrue(match(LET) && (kind == DestructureToLet || kind == DestructureToConst), "Can't use 'let' as an identifier name for a LexicalDeclaration"); 919 pattern = createBindingPattern(context, kind, exportType, *m_token.m_data.ident, m_token, bindingContext, duplicateIdentifier);906 pattern = createBindingPattern(context, kind, exportType, *m_token.m_data.ident, depth, m_token, bindingContext, duplicateIdentifier); 920 907 next(); 921 908 break; … … 2703 2690 int initialAssignmentCount = m_assignmentCount; 2704 2691 int initialNonLHSCount = m_nonLHSCount; 2705 String assignmentPatternError = String();2706 2692 if (match(OPENBRACE) || match(OPENBRACKET)) { 2707 2693 SavePoint savePoint = createSavePoint(); … … 2712 2698 return context.createDestructuringAssignment(location, pattern, rhs); 2713 2699 } 2714 assignmentPatternError = m_errorMessage;2715 2700 restoreSavePoint(savePoint); 2716 2701 } … … 2727 2712 2728 2713 TreeExpression lhs = parseConditionalExpression(context); 2729 if (!assignmentPatternError.isNull() && (lhs && (context.isObjectOrArrayLiteral(lhs) && match(EQUAL)))) {2730 setErrorMessage(assignmentPatternError);2731 return 0;2732 }2733 2714 failIfFalse(lhs, "Cannot parse expression"); 2734 2715 if (initialNonLHSCount != m_nonLHSCount) {
Note:
See TracChangeset
for help on using the changeset viewer.