Changeset 188417 in webkit for trunk/Source/JavaScriptCore/parser/Parser.cpp
- Timestamp:
- Aug 13, 2015, 4:55:35 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/parser/Parser.cpp
r188355 r188417 193 193 Parser<LexerType>::Parser( 194 194 VM* vm, const SourceCode& source, JSParserBuiltinMode builtinMode, 195 JSParserStrictMode strictMode, JSParserCodeType codeType,195 JSParserStrictMode strictMode, SourceParseMode parseMode, 196 196 ConstructorKind defaultConstructorKind, ThisTDZMode thisTDZMode) 197 197 : m_vm(vm) … … 210 210 , m_defaultConstructorKind(defaultConstructorKind) 211 211 , m_thisTDZMode(thisTDZMode) 212 , m_codeType(codeType)213 212 { 214 213 m_lexer = std::make_unique<LexerType>(vm, builtinMode); … … 220 219 m_functionCache = vm->addSourceProviderCache(source.provider()); 221 220 ScopeRef scope = pushScope(); 222 if ( codeType == JSParserCodeType::Function)221 if (isFunctionParseMode(parseMode)) 223 222 scope->setIsFunction(); 224 if ( codeType == JSParserCodeType::Module)223 if (isModuleParseMode(parseMode)) 225 224 scope->setIsModule(); 226 225 if (strictMode == JSParserStrictMode::Strict) … … 236 235 237 236 template <typename LexerType> 238 String Parser<LexerType>::parseInner(const Identifier& calleeName, FunctionParseMode parseMode, ModuleParseMode moduleParseMode) 239 { 240 UNUSED_PARAM(moduleParseMode); 241 237 String Parser<LexerType>::parseInner(const Identifier& calleeName, SourceParseMode parseMode) 238 { 242 239 String parseError = String(); 243 240 244 241 ASTBuilder context(const_cast<VM*>(m_vm), m_parserArena, const_cast<SourceCode*>(m_source)); 245 242 ScopeRef scope = currentScope(); … … 253 250 254 251 #if ENABLE(ES6_ARROWFUNCTION_SYNTAX) 255 if (parseMode == ArrowFunctionMode && !hasError()) {252 if (parseMode == SourceParseMode::ArrowFunctionMode && !hasError()) { 256 253 // The only way we could have an error wile reparsing is if we run out of stack space. 257 254 RELEASE_ASSERT(match(ARROWFUNCTION)); … … 274 271 sourceElements = parseArrowFunctionSingleExpressionBodySourceElements(context); 275 272 #if ENABLE(ES6_MODULES) 276 else if ( m_codeType == JSParserCodeType::Module)277 sourceElements = parseModuleSourceElements(context, moduleParseMode);273 else if (isModuleParseMode(parseMode)) 274 sourceElements = parseModuleSourceElements(context, parseMode); 278 275 #endif 279 276 else … … 415 412 416 413 template <typename LexerType> 417 template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseModuleSourceElements(TreeBuilder& context, ModuleParseMode moduleParseMode)414 template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseModuleSourceElements(TreeBuilder& context, SourceParseMode parseMode) 418 415 { 419 416 TreeSourceElements sourceElements = context.createSourceElements(); … … 429 426 const Identifier* directive = 0; 430 427 unsigned directiveLiteralLength = 0; 431 if ( moduleParseMode == ModuleParseMode::Analyze) {428 if (parseMode == SourceParseMode::ModuleAnalyzeMode) { 432 429 if (!parseStatementListItem(syntaxChecker, directive, &directiveLiteralLength)) 433 430 break; … … 1548 1545 template <class TreeBuilder> TreeFunctionBody Parser<LexerType>::parseFunctionBody( 1549 1546 TreeBuilder& context, const JSTokenLocation& startLocation, int startColumn, int functionKeywordStart, int functionNameStart, int parametersStart, 1550 ConstructorKind constructorKind, FunctionBodyType bodyType, unsigned parameterCount, FunctionParseMode parseMode)1547 ConstructorKind constructorKind, FunctionBodyType bodyType, unsigned parameterCount, SourceParseMode parseMode) 1551 1548 { 1552 1549 if (bodyType == StandardFunctionBodyBlock || bodyType == ArrowFunctionBodyBlock) { … … 1569 1566 } 1570 1567 1571 static const char* stringForFunctionMode( FunctionParseMode mode)1568 static const char* stringForFunctionMode(SourceParseMode mode) 1572 1569 { 1573 1570 switch (mode) { 1574 case GetterMode:1571 case SourceParseMode::GetterMode: 1575 1572 return "getter"; 1576 case S etterMode:1573 case SourceParseMode::SetterMode: 1577 1574 return "setter"; 1578 case NormalFunctionMode:1575 case SourceParseMode::NormalFunctionMode: 1579 1576 return "function"; 1580 case MethodMode:1577 case SourceParseMode::MethodMode: 1581 1578 return "method"; 1582 case ArrowFunctionMode:1579 case SourceParseMode::ArrowFunctionMode: 1583 1580 return "arrow function"; 1584 case NotAFunctionMode: 1581 case SourceParseMode::ProgramMode: 1582 case SourceParseMode::ModuleAnalyzeMode: 1583 case SourceParseMode::ModuleEvaluateMode: 1585 1584 RELEASE_ASSERT_NOT_REACHED(); 1586 1585 return ""; … … 1590 1589 } 1591 1590 1592 template <typename LexerType> template <class TreeBuilder> int Parser<LexerType>::parseFunctionParameters(TreeBuilder& context, FunctionParseMode mode, ParserFunctionInfo<TreeBuilder>& functionInfo)1593 { 1594 RELEASE_ASSERT(mode != NotAFunctionMode);1591 template <typename LexerType> template <class TreeBuilder> int Parser<LexerType>::parseFunctionParameters(TreeBuilder& context, SourceParseMode mode, ParserFunctionInfo<TreeBuilder>& functionInfo) 1592 { 1593 RELEASE_ASSERT(mode != SourceParseMode::ProgramMode && mode != SourceParseMode::ModuleAnalyzeMode && mode != SourceParseMode::ModuleEvaluateMode); 1595 1594 int parametersStart = m_token.m_location.startOffset; 1596 1595 TreeFormalParameterList parameterList = context.createFormalParameterList(); … … 1598 1597 functionInfo.startOffset = parametersStart; 1599 1598 1600 if (mode == ArrowFunctionMode) {1599 if (mode == SourceParseMode::ArrowFunctionMode) { 1601 1600 if (!match(IDENT) && !match(OPENPAREN)) { 1602 1601 semanticFailureDueToKeyword(stringForFunctionMode(mode), " name"); … … 1628 1627 } 1629 1628 1630 if (mode == GetterMode) {1629 if (mode == SourceParseMode::GetterMode) { 1631 1630 consumeOrFail(CLOSEPAREN, "getter functions must have no parameters"); 1632 1631 functionInfo.parameterCount = 0; 1633 } else if (mode == S etterMode) {1632 } else if (mode == SourceParseMode::SetterMode) { 1634 1633 failIfTrue(match(CLOSEPAREN), "setter functions must have one parameter"); 1635 1634 const Identifier* duplicateParameter = nullptr; … … 1655 1654 1656 1655 template <typename LexerType> 1657 template <class TreeBuilder> bool Parser<LexerType>::parseFunctionInfo(TreeBuilder& context, FunctionRequirements requirements, FunctionParseMode mode, bool nameIsInContainingScope, ConstructorKind constructorKind, SuperBinding expectedSuperBinding, int functionKeywordStart, ParserFunctionInfo<TreeBuilder>& functionInfo, FunctionParseType parseType)1658 { 1659 RELEASE_ASSERT( mode != NotAFunctionMode);1656 template <class TreeBuilder> bool Parser<LexerType>::parseFunctionInfo(TreeBuilder& context, FunctionRequirements requirements, SourceParseMode mode, bool nameIsInContainingScope, ConstructorKind constructorKind, SuperBinding expectedSuperBinding, int functionKeywordStart, ParserFunctionInfo<TreeBuilder>& functionInfo, FunctionParseType parseType) 1657 { 1658 RELEASE_ASSERT(isFunctionParseMode(mode)); 1660 1659 1661 1660 AutoPopScopeRef functionScope(this, pushScope()); … … 1671 1670 switch (parseType) { 1672 1671 case StandardFunctionParseType: { 1673 RELEASE_ASSERT(mode != ArrowFunctionMode);1672 RELEASE_ASSERT(mode != SourceParseMode::ArrowFunctionMode); 1674 1673 if (match(IDENT) || isLETMaskedAsIDENT()) { 1675 1674 functionInfo.name = m_token.m_data.ident; … … 1679 1678 failIfTrueIfStrict(functionScope->declareVariable(functionInfo.name) & DeclarationResult::InvalidStrictMode, "'", functionInfo.name->impl(), "' is not a valid ", stringForFunctionMode(mode), " name in strict mode"); 1680 1679 } else if (requirements == FunctionNeedsName) { 1681 if (match(OPENPAREN) && mode == NormalFunctionMode)1680 if (match(OPENPAREN) && mode == SourceParseMode::NormalFunctionMode) 1682 1681 semanticFail("Function statements must have a name"); 1683 1682 semanticFailureDueToKeyword(stringForFunctionMode(mode), " name"); … … 1709 1708 #if ENABLE(ES6_ARROWFUNCTION_SYNTAX) 1710 1709 case ArrowFunctionParseType: { 1711 RELEASE_ASSERT(mode == ArrowFunctionMode);1710 RELEASE_ASSERT(mode == SourceParseMode::ArrowFunctionMode); 1712 1711 1713 1712 startLocation = tokenLocation(); … … 1805 1804 context.setEndOffset(functionInfo.body, m_lexer->currentOffset()); 1806 1805 if (functionScope->strictMode() && functionInfo.name) { 1807 RELEASE_ASSERT(mode == NormalFunctionMode || mode == MethodMode || mode ==ArrowFunctionMode);1806 RELEASE_ASSERT(mode == SourceParseMode::NormalFunctionMode || mode == SourceParseMode::MethodMode || mode == SourceParseMode::ArrowFunctionMode); 1808 1807 semanticFailIfTrue(m_vm->propertyNames->arguments == *functionInfo.name, "'", functionInfo.name->impl(), "' is not a valid function name in strict mode"); 1809 1808 semanticFailIfTrue(m_vm->propertyNames->eval == *functionInfo.name, "'", functionInfo.name->impl(), "' is not a valid function name in strict mode"); … … 1870 1869 next(); 1871 1870 ParserFunctionInfo<TreeBuilder> functionInfo; 1872 failIfFalse((parseFunctionInfo(context, FunctionNeedsName, NormalFunctionMode, true, ConstructorKind::None, SuperBinding::NotNeeded,1871 failIfFalse((parseFunctionInfo(context, FunctionNeedsName, SourceParseMode::NormalFunctionMode, true, ConstructorKind::None, SuperBinding::NotNeeded, 1873 1872 functionKeywordStart, functionInfo, StandardFunctionParseType)), "Cannot parse this function"); 1874 1873 failIfFalse(functionInfo.name, "Function statements must have a name"); … … 2000 1999 ParserFunctionInfo<TreeBuilder> methodInfo; 2001 2000 bool isConstructor = !isStaticMethod && *ident == propertyNames.constructor; 2002 failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, MethodMode, false, isConstructor ? constructorKind : ConstructorKind::None, SuperBinding::Needed, methodStart, methodInfo, StandardFunctionParseType)), "Cannot parse this method");2001 failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SourceParseMode::MethodMode, false, isConstructor ? constructorKind : ConstructorKind::None, SuperBinding::Needed, methodStart, methodInfo, StandardFunctionParseType)), "Cannot parse this method"); 2003 2002 failIfTrue(!ident || (declareVariable(ident) & DeclarationResult::InvalidStrictMode), "Cannot declare a method named '", methodInfo.name->impl(), "'"); 2004 2003 methodInfo.name = isConstructor ? className : ident; … … 2227 2226 2228 2227 template <typename LexerType> 2229 template <class TreeBuilder> typename TreeBuilder::Module Specifier Parser<LexerType>::parseModuleSpecifier(TreeBuilder& context)2230 { 2231 // Module Specifierrepresents the module name imported by the script.2228 template <class TreeBuilder> typename TreeBuilder::ModuleName Parser<LexerType>::parseModuleName(TreeBuilder& context) 2229 { 2230 // ModuleName (ModuleSpecifier in the spec) represents the module name imported by the script. 2232 2231 // http://www.ecma-international.org/ecma-262/6.0/#sec-imports 2233 2232 // http://www.ecma-international.org/ecma-262/6.0/#sec-exports … … 2236 2235 const Identifier* moduleName = m_token.m_data.ident; 2237 2236 next(); 2238 return context.createModule Specifier(specifierLocation, *moduleName);2237 return context.createModuleName(specifierLocation, *moduleName); 2239 2238 } 2240 2239 … … 2328 2327 if (match(STRING)) { 2329 2328 // import ModuleSpecifier ; 2330 auto module Specifier = parseModuleSpecifier(context);2331 failIfFalse(module Specifier, "Cannot parse the module name");2329 auto moduleName = parseModuleName(context); 2330 failIfFalse(moduleName, "Cannot parse the module name"); 2332 2331 failIfFalse(autoSemiColon(), "Expected a ';' following a targeted import declaration"); 2333 return context.createImportDeclaration(importLocation, specifierList, module Specifier);2332 return context.createImportDeclaration(importLocation, specifierList, moduleName); 2334 2333 } 2335 2334 … … 2379 2378 next(); 2380 2379 2381 auto module Specifier = parseModuleSpecifier(context);2382 failIfFalse(module Specifier, "Cannot parse the module name");2380 auto moduleName = parseModuleName(context); 2381 failIfFalse(moduleName, "Cannot parse the module name"); 2383 2382 failIfFalse(autoSemiColon(), "Expected a ';' following a targeted import declaration"); 2384 2383 2385 return context.createImportDeclaration(importLocation, specifierList, module Specifier);2384 return context.createImportDeclaration(importLocation, specifierList, moduleName); 2386 2385 } 2387 2386 … … 2428 2427 failIfFalse(matchContextualKeyword(m_vm->propertyNames->from), "Expected 'from' before exported module name"); 2429 2428 next(); 2430 auto module Specifier = parseModuleSpecifier(context);2431 failIfFalse(module Specifier, "Cannot parse the 'from' clause");2429 auto moduleName = parseModuleName(context); 2430 failIfFalse(moduleName, "Cannot parse the 'from' clause"); 2432 2431 failIfFalse(autoSemiColon(), "Expected a ';' following a targeted export declaration"); 2433 2432 2434 return context.createExportAllDeclaration(exportLocation, module Specifier);2433 return context.createExportAllDeclaration(exportLocation, moduleName); 2435 2434 } 2436 2435 … … 2475 2474 // const *default* = expr; 2476 2475 // export { *default* as default } 2476 // 2477 // In the above example, *default* is the invisible variable to the users. 2478 // We use the private symbol to represent the name of this variable. 2477 2479 JSTokenLocation location(tokenLocation()); 2478 2480 JSTextPosition start = tokenStartPosition(); … … 2482 2484 DeclarationResultMask declarationResult = declareVariable(&m_vm->propertyNames->starDefaultPrivateName, DeclarationType::ConstDeclaration); 2483 2485 if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration) 2484 internalFailWithMessage(false, " Cannot export 'default' name twice: '");2486 internalFailWithMessage(false, "Only one 'default' export is allowed"); 2485 2487 2486 2488 TreeExpression assignment = context.createAssignResolve(location, m_vm->propertyNames->starDefaultPrivateName, expression, start, start, tokenEndPosition(), AssignmentContext::ConstDeclarationStatement); … … 2492 2494 failIfFalse(result, "Cannot parse the declaration"); 2493 2495 2494 semanticFailIfFalse(exportName(m_vm->propertyNames->defaultKeyword), " Cannot export 'default' name twice.");2496 semanticFailIfFalse(exportName(m_vm->propertyNames->defaultKeyword), "Only one 'default' export is allowed"); 2495 2497 currentScope()->moduleScopeData().exportBinding(*localName); 2496 2498 return context.createExportDefaultDeclaration(exportLocation, result, *localName); … … 2526 2528 handleProductionOrFail(CLOSEBRACE, "}", "end", "export list"); 2527 2529 2528 typename TreeBuilder::Module Specifier moduleSpecifier= 0;2530 typename TreeBuilder::ModuleName moduleName = 0; 2529 2531 if (matchContextualKeyword(m_vm->propertyNames->from)) { 2530 2532 next(); 2531 module Specifier = parseModuleSpecifier(context);2532 failIfFalse(module Specifier, "Cannot parse the 'from' clause");2533 moduleName = parseModuleName(context); 2534 failIfFalse(moduleName, "Cannot parse the 'from' clause"); 2533 2535 } 2534 2536 failIfFalse(autoSemiColon(), "Expected a ';' following a targeted export declaration"); 2535 2537 2536 if (!module Specifier) {2538 if (!moduleName) { 2537 2539 semanticFailIfTrue(hasKeywordForLocalBindings, "Cannot use keyword as exported variable name"); 2538 2540 // Since this export declaration does not have module specifier part, it exports the local bindings. … … 2550 2552 } 2551 2553 2552 return context.createExportNamedDeclaration(exportLocation, specifierList, module Specifier);2554 return context.createExportNamedDeclaration(exportLocation, specifierList, moduleName); 2553 2555 } 2554 2556 … … 2887 2889 unsigned methodStart = tokenStart(); 2888 2890 ParserFunctionInfo<TreeBuilder> methodInfo; 2889 failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, MethodMode, false, ConstructorKind::None, SuperBinding::NotNeeded, methodStart, methodInfo, StandardFunctionParseType)), "Cannot parse this method");2891 failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SourceParseMode::MethodMode, false, ConstructorKind::None, SuperBinding::NotNeeded, methodStart, methodInfo, StandardFunctionParseType)), "Cannot parse this method"); 2890 2892 methodInfo.name = methodName; 2891 2893 return context.createFunctionExpr(methodLocation, methodInfo); … … 2913 2915 if (type & PropertyNode::Getter) { 2914 2916 failIfFalse(match(OPENPAREN), "Expected a parameter list for getter definition"); 2915 failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, GetterMode, false, constructorKind, superBinding,2917 failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SourceParseMode::GetterMode, false, constructorKind, superBinding, 2916 2918 getterOrSetterStartOffset, info, StandardFunctionParseType)), "Cannot parse getter definition"); 2917 2919 } else { 2918 2920 failIfFalse(match(OPENPAREN), "Expected a parameter list for setter definition"); 2919 failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, S etterMode, false, constructorKind, superBinding,2921 failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SourceParseMode::SetterMode, false, constructorKind, superBinding, 2920 2922 getterOrSetterStartOffset, info, StandardFunctionParseType)), "Cannot parse setter definition"); 2921 2923 } … … 3185 3187 ParserFunctionInfo<TreeBuilder> info; 3186 3188 info.name = &m_vm->propertyNames->nullIdentifier; 3187 failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, NormalFunctionMode, false, ConstructorKind::None, SuperBinding::NotNeeded, functionKeywordStart, info, StandardFunctionParseType)), "Cannot parse function expression");3189 failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SourceParseMode::NormalFunctionMode, false, ConstructorKind::None, SuperBinding::NotNeeded, functionKeywordStart, info, StandardFunctionParseType)), "Cannot parse function expression"); 3188 3190 return context.createFunctionExpr(location, info); 3189 3191 } … … 3457 3459 ParserFunctionInfo<TreeBuilder> info; 3458 3460 info.name = &m_vm->propertyNames->nullIdentifier; 3459 failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, ArrowFunctionMode, true, ConstructorKind::None, SuperBinding::NotNeeded, functionKeywordStart, info, ArrowFunctionParseType)), "Cannot parse arrow function expression");3461 failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SourceParseMode::ArrowFunctionMode, true, ConstructorKind::None, SuperBinding::NotNeeded, functionKeywordStart, info, ArrowFunctionParseType)), "Cannot parse arrow function expression"); 3460 3462 3461 3463 return context.createArrowFunctionExpr(location, info);
Note:
See TracChangeset
for help on using the changeset viewer.