Changeset 188355 in webkit for trunk/Source/JavaScriptCore/parser/Parser.cpp
- Timestamp:
- Aug 12, 2015, 1:38:45 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/parser/Parser.cpp
r188219 r188355 236 236 237 237 template <typename LexerType> 238 String Parser<LexerType>::parseInner(const Identifier& calleeName, FunctionParseMode parseMode) 239 { 238 String Parser<LexerType>::parseInner(const Identifier& calleeName, FunctionParseMode parseMode, ModuleParseMode moduleParseMode) 239 { 240 UNUSED_PARAM(moduleParseMode); 241 240 242 String parseError = String(); 241 243 … … 273 275 #if ENABLE(ES6_MODULES) 274 276 else if (m_codeType == JSParserCodeType::Module) 275 sourceElements = parseModuleSourceElements(context );277 sourceElements = parseModuleSourceElements(context, moduleParseMode); 276 278 #endif 277 279 else … … 413 415 414 416 template <typename LexerType> 415 template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseModuleSourceElements(TreeBuilder& context )417 template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseModuleSourceElements(TreeBuilder& context, ModuleParseMode moduleParseMode) 416 418 { 417 419 TreeSourceElements sourceElements = context.createSourceElements(); 418 419 // FIXME: When some sort of ModuleAnalyzer TreeBuilder is landed, 420 // this SyntaxChecker will be replaced with typedef under the TreeBuilder, 421 // like TreeBuilder::ModuleStatementItemBuilder. 422 // https://bugs.webkit.org/show_bug.cgi?id=147353 423 SyntaxChecker moduleStatementItemBuilder(const_cast<VM*>(m_vm), m_lexer.get()); 420 SyntaxChecker syntaxChecker(const_cast<VM*>(m_vm), m_lexer.get()); 424 421 425 422 while (true) { 426 if (match(IMPORT) || match(EXPORT)) { 427 TreeStatement statement = 0; 428 if (match(IMPORT)) 429 statement = parseImportDeclaration(context); 430 else 431 statement = parseExportDeclaration(context); 432 433 if (!statement) 434 break; 435 context.appendStatement(sourceElements, statement); 436 } else { 423 TreeStatement statement = 0; 424 if (match(IMPORT)) 425 statement = parseImportDeclaration(context); 426 else if (match(EXPORT)) 427 statement = parseExportDeclaration(context); 428 else { 437 429 const Identifier* directive = 0; 438 430 unsigned directiveLiteralLength = 0; 439 if (!parseStatementListItem(moduleStatementItemBuilder, directive, &directiveLiteralLength)) 440 break; 441 } 431 if (moduleParseMode == ModuleParseMode::Analyze) { 432 if (!parseStatementListItem(syntaxChecker, directive, &directiveLiteralLength)) 433 break; 434 continue; 435 } 436 statement = parseStatementListItem(context, directive, &directiveLiteralLength); 437 } 438 439 if (!statement) 440 break; 441 context.appendStatement(sourceElements, statement); 442 442 } 443 443 444 444 propagateError(); 445 445 446 for (const auto& uid : currentScope()->moduleScopeData().exportedBindings()) 447 semanticFailIfFalse(currentScope()->hasDeclaredVariable(uid) || currentScope()->hasLexicallyDeclaredVariable(uid), "Exported binding '", uid.get(), "' needs to refer to a top-level declared variable"); 446 for (const auto& uid : currentScope()->moduleScopeData().exportedBindings()) { 447 if (currentScope()->hasDeclaredVariable(uid)) { 448 currentScope()->declaredVariables().markVariableAsExported(uid); 449 continue; 450 } 451 452 if (currentScope()->hasLexicallyDeclaredVariable(uid)) { 453 currentScope()->lexicalVariables().markVariableAsExported(uid); 454 continue; 455 } 456 457 semanticFail("Exported binding '", uid.get(), "' needs to refer to a top-level declared variable"); 458 } 448 459 449 460 return sourceElements; … … 602 613 } 603 614 } 604 semanticFailIfTrue(exportType == ExportType::Exported && !currentScope()->moduleScopeData().exportName(*name), "Cannot export a duplicate name '", name->impl(), "'"); 615 if (exportType == ExportType::Exported) { 616 semanticFailIfFalse(exportName(*name), "Cannot export a duplicate name '", name->impl(), "'"); 617 currentScope()->moduleScopeData().exportBinding(*name); 618 } 605 619 606 620 if (hasInitializer) { … … 706 720 } 707 721 708 semanticFailIfTrue(exportType == ExportType::Exported && !currentScope()->moduleScopeData().exportName(name), "Cannot export a duplicate name '", name.impl(), "'"); 722 if (exportType == ExportType::Exported) { 723 semanticFailIfFalse(exportName(name), "Cannot export a duplicate name '", name.impl(), "'"); 724 currentScope()->moduleScopeData().exportBinding(name); 725 } 709 726 return context.createBindingLocation(token.m_location, name, token.m_startPosition, token.m_endPosition, bindingContext); 710 727 } … … 1857 1874 failIfFalse(functionInfo.name, "Function statements must have a name"); 1858 1875 failIfTrueIfStrict(declareVariable(functionInfo.name) & DeclarationResult::InvalidStrictMode, "Cannot declare a function named '", functionInfo.name->impl(), "' in strict mode"); 1859 semanticFailIfTrue(exportType == ExportType::Exported && !currentScope()->moduleScopeData().exportName(*functionInfo.name), "Cannot export a duplicate function name: '", functionInfo.name->impl(), "'"); 1876 if (exportType == ExportType::Exported) { 1877 semanticFailIfFalse(exportName(*functionInfo.name), "Cannot export a duplicate function name: '", functionInfo.name->impl(), "'"); 1878 currentScope()->moduleScopeData().exportBinding(*functionInfo.name); 1879 } 1860 1880 return context.createFuncDeclStatement(location, functionInfo); 1861 1881 } … … 1877 1897 if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration) 1878 1898 internalFailWithMessage(false, "Cannot declare a class twice: '", info.className->impl(), "'"); 1879 semanticFailIfTrue(exportType == ExportType::Exported && !currentScope()->moduleScopeData().exportName(*info.className), "Cannot export a duplicate class name: '", info.className->impl(), "'"); 1899 if (exportType == ExportType::Exported) { 1900 semanticFailIfFalse(exportName(*info.className), "Cannot export a duplicate class name: '", info.className->impl(), "'"); 1901 currentScope()->moduleScopeData().exportBinding(*info.className); 1902 } 1880 1903 1881 1904 JSTextPosition classEnd = lastTokenEndPosition(); … … 2283 2306 2284 2307 semanticFailIfTrue(localNameToken.m_type & KeywordTokenFlag, "Cannot use keyword as imported binding name"); 2285 DeclarationResultMask declarationResult = declareVariable(localName, DeclarationType::ConstDeclaration );2308 DeclarationResultMask declarationResult = declareVariable(localName, DeclarationType::ConstDeclaration, DeclarationImportType::Imported); 2286 2309 if (declarationResult != DeclarationResult::Valid) { 2287 2310 failIfTrueIfStrict(declarationResult & DeclarationResult::InvalidStrictMode, "Cannot declare an imported binding named ", localName->impl(), " in strict mode"); … … 2385 2408 } 2386 2409 2387 semanticFailIfFalse( currentScope()->moduleScopeData().exportName(*exportedName), "Cannot export a duplicate name '", exportedName->impl(), "'");2410 semanticFailIfFalse(exportName(*exportedName), "Cannot export a duplicate name '", exportedName->impl(), "'"); 2388 2411 maybeLocalNames.append(localName); 2389 2412 return context.createExportSpecifier(specifierLocation, *localName, *exportedName); … … 2408 2431 failIfFalse(moduleSpecifier, "Cannot parse the 'from' clause"); 2409 2432 failIfFalse(autoSemiColon(), "Expected a ';' following a targeted export declaration"); 2433 2410 2434 return context.createExportAllDeclaration(exportLocation, moduleSpecifier); 2411 2435 } … … 2419 2443 2420 2444 TreeStatement result = 0; 2421 bool hasName = false;2422 2445 bool isFunctionOrClassDeclaration = false; 2446 const Identifier* localName = nullptr; 2423 2447 SavePoint savePoint = createSavePoint(); 2424 2448 if (match(FUNCTION) … … 2430 2454 next(); 2431 2455 // FIXME: When landing ES6 generators, we need to take care of that '*' comes. 2432 hasName = match(IDENT); 2456 if (match(IDENT)) 2457 localName = m_token.m_data.ident; 2433 2458 restoreSavePoint(savePoint); 2434 2459 } 2435 2460 2436 if ( hasName) {2461 if (localName) { 2437 2462 if (match(FUNCTION)) 2438 2463 result = parseFunctionDeclaration(context); … … 2444 2469 #endif 2445 2470 } else { 2471 // export default expr; 2472 // 2473 // It should be treated as the same to the following. 2474 // 2475 // const *default* = expr; 2476 // export { *default* as default } 2446 2477 JSTokenLocation location(tokenLocation()); 2447 2478 JSTextPosition start = tokenStartPosition(); 2448 2479 TreeExpression expression = parseAssignmentExpression(context); 2449 2480 failIfFalse(expression, "Cannot parse expression"); 2450 result = context.createExprStatement(location, expression, start, tokenEndPosition()); 2481 2482 DeclarationResultMask declarationResult = declareVariable(&m_vm->propertyNames->starDefaultPrivateName, DeclarationType::ConstDeclaration); 2483 if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration) 2484 internalFailWithMessage(false, "Cannot export 'default' name twice: '"); 2485 2486 TreeExpression assignment = context.createAssignResolve(location, m_vm->propertyNames->starDefaultPrivateName, expression, start, start, tokenEndPosition(), AssignmentContext::ConstDeclarationStatement); 2487 result = context.createExprStatement(location, assignment, start, tokenEndPosition()); 2451 2488 if (!isFunctionOrClassDeclaration) 2452 2489 failIfFalse(autoSemiColon(), "Expected a ';' following a targeted export declaration"); 2490 localName = &m_vm->propertyNames->starDefaultPrivateName; 2453 2491 } 2454 2492 failIfFalse(result, "Cannot parse the declaration"); 2455 semanticFailIfFalse(currentScope()->moduleScopeData().exportName(m_vm->propertyNames->defaultKeyword), "Cannot export 'default' name twice."); 2456 2457 return context.createExportDefaultDeclaration(exportLocation, result); 2493 2494 semanticFailIfFalse(exportName(m_vm->propertyNames->defaultKeyword), "Cannot export 'default' name twice."); 2495 currentScope()->moduleScopeData().exportBinding(*localName); 2496 return context.createExportDefaultDeclaration(exportLocation, result, *localName); 2458 2497 } 2459 2498
Note:
See TracChangeset
for help on using the changeset viewer.