diff options
| author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2015-05-20 09:56:07 +0000 |
|---|---|---|
| committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2015-05-20 09:56:07 +0000 |
| commit | 41386e9cb918eed93b3f13648cbef387e371e451 (patch) | |
| tree | a97f9d7bd1d9d091833286085f72da9d83fd0606 /Source/JavaScriptCore/parser/Parser.cpp | |
| parent | e15dd966d523731101f70ccf768bba12435a0208 (diff) | |
| download | WebKitGtk-tarball-41386e9cb918eed93b3f13648cbef387e371e451.tar.gz | |
webkitgtk-2.4.9webkitgtk-2.4.9
Diffstat (limited to 'Source/JavaScriptCore/parser/Parser.cpp')
| -rw-r--r-- | Source/JavaScriptCore/parser/Parser.cpp | 2134 |
1 files changed, 402 insertions, 1732 deletions
diff --git a/Source/JavaScriptCore/parser/Parser.cpp b/Source/JavaScriptCore/parser/Parser.cpp index aee70b6a7..297666e72 100644 --- a/Source/JavaScriptCore/parser/Parser.cpp +++ b/Source/JavaScriptCore/parser/Parser.cpp @@ -28,11 +28,12 @@ #include "Debugger.h" #include "JSCJSValueInlines.h" #include "Lexer.h" -#include "JSCInlines.h" +#include "NodeInfo.h" #include "SourceProvider.h" #include "VM.h" #include <utility> #include <wtf/HashFunctions.h> +#include <wtf/OwnPtr.h> #include <wtf/StringPrintStream.h> #include <wtf/WTFThreadData.h> @@ -48,7 +49,7 @@ #define failWithMessage(...) do { { handleErrorToken(); updateErrorMessage(true, __VA_ARGS__); } return 0; } while (0) #define failWithStackOverflow() do { updateErrorMessage(false, "Stack exhausted"); m_hasStackOverflow = true; return 0; } while (0) #define failIfFalse(cond, ...) do { if (!(cond)) { handleErrorToken(); internalFailWithMessage(true, __VA_ARGS__); } } while (0) -#define failIfTrue(cond, ...) do { if (cond) { handleErrorToken(); internalFailWithMessage(true, __VA_ARGS__); } } while (0) +#define failIfTrue(cond, ...) do { if ((cond)) { handleErrorToken(); internalFailWithMessage(true, __VA_ARGS__); } } while (0) #define failIfTrueIfStrict(cond, ...) do { if ((cond) && strictMode()) internalFailWithMessage(false, __VA_ARGS__); } while (0) #define failIfFalseIfStrict(cond, ...) do { if ((!(cond)) && strictMode()) internalFailWithMessage(false, __VA_ARGS__); } while (0) #define consumeOrFail(tokenType, ...) do { if (!consume(tokenType)) { handleErrorToken(); internalFailWithMessage(true, __VA_ARGS__); } } while (0) @@ -56,7 +57,7 @@ #define matchOrFail(tokenType, ...) do { if (!match(tokenType)) { handleErrorToken(); internalFailWithMessage(true, __VA_ARGS__); } } while (0) #define failIfStackOverflow() do { if (!canRecurse()) failWithStackOverflow(); } while (0) #define semanticFail(...) do { internalFailWithMessage(false, __VA_ARGS__); } while (0) -#define semanticFailIfTrue(cond, ...) do { if (cond) internalFailWithMessage(false, __VA_ARGS__); } while (0) +#define semanticFailIfTrue(cond, ...) do { if ((cond)) internalFailWithMessage(false, __VA_ARGS__); } while (0) #define semanticFailIfFalse(cond, ...) do { if (!(cond)) internalFailWithMessage(false, __VA_ARGS__); } while (0) #define regexFail(failure) do { setErrorMessage(failure); return 0; } while (0) #define failDueToUnexpectedToken() do {\ @@ -190,10 +191,7 @@ void Parser<LexerType>::logError(bool shouldPrintToken, const A& value1, const B } template <typename LexerType> -Parser<LexerType>::Parser( - VM* vm, const SourceCode& source, JSParserBuiltinMode builtinMode, - JSParserStrictMode strictMode, SourceParseMode parseMode, - ConstructorKind defaultConstructorKind, ThisTDZMode thisTDZMode) +Parser<LexerType>::Parser(VM* vm, const SourceCode& source, FunctionParameters* parameters, const Identifier& name, JSParserStrictness strictness, JSParserMode parserMode) : m_vm(vm) , m_source(&source) , m_hasStackOverflow(false) @@ -206,25 +204,30 @@ Parser<LexerType>::Parser( , m_lastIdentifier(0) , m_lastFunctionName(nullptr) , m_sourceElements(0) - , m_parsingBuiltin(builtinMode == JSParserBuiltinMode::Builtin) - , m_defaultConstructorKind(defaultConstructorKind) - , m_thisTDZMode(thisTDZMode) { - m_lexer = std::make_unique<LexerType>(vm, builtinMode); - m_lexer->setCode(source, &m_parserArena); + m_lexer = adoptPtr(new LexerType(vm)); + m_arena = m_vm->parserArena.get(); + m_lexer->setCode(source, m_arena); m_token.m_location.line = source.firstLine(); m_token.m_location.startOffset = source.startOffset(); m_token.m_location.endOffset = source.startOffset(); m_token.m_location.lineStartOffset = source.startOffset(); m_functionCache = vm->addSourceProviderCache(source.provider()); ScopeRef scope = pushScope(); - if (isFunctionParseMode(parseMode)) + if (parserMode == JSParseFunctionCode) scope->setIsFunction(); - if (isModuleParseMode(parseMode)) - scope->setIsModule(); - if (strictMode == JSParserStrictMode::Strict) + if (strictness == JSParseStrict) scope->setStrictMode(); - + if (parameters) { + for (unsigned i = 0; i < parameters->size(); i++) { + auto parameter = parameters->at(i); + if (!parameter->isBindingNode()) + continue; + scope->declareParameter(&static_cast<BindingNode*>(parameter)->boundProperty()); + } + } + if (!name.isNull()) + scope->declareCallee(&name); next(); } @@ -234,60 +237,16 @@ Parser<LexerType>::~Parser() } template <typename LexerType> -String Parser<LexerType>::parseInner(const Identifier& calleeName, SourceParseMode parseMode) +String Parser<LexerType>::parseInner() { String parseError = String(); - - ASTBuilder context(const_cast<VM*>(m_vm), m_parserArena, const_cast<SourceCode*>(m_source)); - ScopeRef scope = currentScope(); - scope->setIsLexicalScope(); - - bool isArrowFunctionBodyExpression = false; - if (m_lexer->isReparsingFunction()) { - ParserFunctionInfo<ASTBuilder> functionInfo; - parseFunctionParameters(context, parseMode, functionInfo); - m_parameters = functionInfo.parameters; - -#if ENABLE(ES6_ARROWFUNCTION_SYNTAX) - if (parseMode == SourceParseMode::ArrowFunctionMode && !hasError()) { - // The only way we could have an error wile reparsing is if we run out of stack space. - RELEASE_ASSERT(match(ARROWFUNCTION)); - next(); - isArrowFunctionBodyExpression = !match(OPENBRACE); - } -#endif - } - - if (!calleeName.isNull()) - scope->declareCallee(&calleeName); - - if (m_lexer->isReparsingFunction()) + + ASTBuilder context(const_cast<VM*>(m_vm), const_cast<SourceCode*>(m_source)); + if (m_lexer->isReparsing()) m_statementDepth--; - - SourceElements* sourceElements = nullptr; - // The only way we can error this early is if we reparse a function and we run out of stack space. - if (!hasError()) { - if (isArrowFunctionBodyExpression) - sourceElements = parseArrowFunctionSingleExpressionBodySourceElements(context); -#if ENABLE(ES6_MODULES) - else if (isModuleParseMode(parseMode)) - sourceElements = parseModuleSourceElements(context, parseMode); -#endif - else - sourceElements = parseSourceElements(context, CheckForStrictMode); - } - - bool validEnding; - if (isArrowFunctionBodyExpression) { - ASSERT(m_lexer->isReparsingFunction()); - // When we reparse and stack overflow, we're not guaranteed a valid ending. If we don't run out of stack space, - // then of course this will always be valid because we already parsed for syntax errors. But we must - // be cautious in case we run out of stack space. - validEnding = isEndOfArrowFunction(); - } else - validEnding = consume(EOFTOK); - - if (!sourceElements || !validEnding) { + ScopeRef scope = currentScope(); + SourceElements* sourceElements = parseSourceElements(context, CheckForStrictMode); + if (!sourceElements || !consume(EOFTOK)) { if (hasError()) parseError = m_errorMessage; else @@ -296,12 +255,7 @@ String Parser<LexerType>::parseInner(const Identifier& calleeName, SourceParseMo IdentifierSet capturedVariables; bool modifiedParameter = false; - bool modifiedArguments = false; - scope->getCapturedVars(capturedVariables, modifiedParameter, modifiedArguments); - VariableEnvironment& varDeclarations = scope->declaredVariables(); - for (auto& entry : capturedVariables) - varDeclarations.markVariableAsCaptured(entry); - + scope->getCapturedVariables(capturedVariables, modifiedParameter); CodeFeatures features = context.features(); if (scope->strictMode()) features |= StrictModeFeature; @@ -309,52 +263,21 @@ String Parser<LexerType>::parseInner(const Identifier& calleeName, SourceParseMo features |= ShadowsArgumentsFeature; if (modifiedParameter) features |= ModifiedParameterFeature; - if (modifiedArguments) - features |= ModifiedArgumentsFeature; - Vector<RefPtr<UniquedStringImpl>> closedVariables; - if (m_parsingBuiltin) { - IdentifierSet usedVariables; - scope->getUsedVariables(usedVariables); - // FIXME: This needs to be changed if we want to allow builtins to use lexical declarations. - for (const auto& variable : usedVariables) { - Identifier identifier = Identifier::fromUid(m_vm, variable.get()); - if (scope->hasDeclaredVariable(identifier)) - continue; - - if (scope->hasDeclaredParameter(identifier)) - continue; - - if (variable == m_vm->propertyNames->arguments.impl()) - continue; - closedVariables.append(variable); - } - - if (!capturedVariables.isEmpty()) { - for (const auto& capturedVariable : capturedVariables) { - if (scope->hasDeclaredVariable(capturedVariable)) - continue; - - if (scope->hasDeclaredParameter(capturedVariable)) - continue; - - RELEASE_ASSERT_NOT_REACHED(); - } - } - } - didFinishParsing(sourceElements, context.funcDeclarations(), varDeclarations, features, context.numConstants(), WTF::move(closedVariables)); + didFinishParsing(sourceElements, context.varDeclarations(), context.funcDeclarations(), features, + context.numConstants(), capturedVariables); return parseError; } template <typename LexerType> -void Parser<LexerType>::didFinishParsing(SourceElements* sourceElements, DeclarationStacks::FunctionStack& funcStack, - VariableEnvironment& varDeclarations, CodeFeatures features, int numConstants, const Vector<RefPtr<UniquedStringImpl>>&& closedVariables) +void Parser<LexerType>::didFinishParsing(SourceElements* sourceElements, ParserArenaData<DeclarationStacks::VarStack>* varStack, + ParserArenaData<DeclarationStacks::FunctionStack>* funcStack, CodeFeatures features, int numConstants, IdentifierSet& capturedVars) { m_sourceElements = sourceElements; - m_funcDeclarations.swap(funcStack); - m_varDeclarations.swap(varDeclarations); - m_closedVariables = closedVariables; + m_varDeclarations = varStack; + m_funcDeclarations = funcStack; + m_capturedVariables.swap(capturedVars); m_features = features; m_numConstants = numConstants; } @@ -375,8 +298,7 @@ template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseSourceEl unsigned directiveLiteralLength = 0; auto savePoint = createSavePoint(); bool hasSetStrict = false; - - while (TreeStatement statement = parseStatementListItem(context, directive, &directiveLiteralLength)) { + while (TreeStatement statement = parseStatement(context, directive, &directiveLiteralLength)) { if (mode == CheckForStrictMode && !seenNonDirective) { if (directive) { // "use strict" must be the exact literal without escape sequences or line continuation. @@ -411,116 +333,35 @@ template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseSourceEl } template <typename LexerType> -template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseModuleSourceElements(TreeBuilder& context, SourceParseMode parseMode) +template <class TreeBuilder> TreeStatement Parser<LexerType>::parseVarDeclaration(TreeBuilder& context) { - TreeSourceElements sourceElements = context.createSourceElements(); - SyntaxChecker syntaxChecker(const_cast<VM*>(m_vm), m_lexer.get()); - - while (true) { - TreeStatement statement = 0; - if (match(IMPORT)) - statement = parseImportDeclaration(context); - else if (match(EXPORT)) - statement = parseExportDeclaration(context); - else { - const Identifier* directive = 0; - unsigned directiveLiteralLength = 0; - if (parseMode == SourceParseMode::ModuleAnalyzeMode) { - if (!parseStatementListItem(syntaxChecker, directive, &directiveLiteralLength)) - break; - continue; - } - statement = parseStatementListItem(context, directive, &directiveLiteralLength); - } - - if (!statement) - break; - context.appendStatement(sourceElements, statement); - } - + ASSERT(match(VAR)); + JSTokenLocation location(tokenLocation()); + int start = tokenLine(); + int end = 0; + int scratch; + TreeDeconstructionPattern scratch1 = 0; + TreeExpression scratch2 = 0; + JSTextPosition scratch3; + TreeExpression varDecls = parseVarDeclarationList(context, scratch, scratch1, scratch2, scratch3, scratch3, scratch3); propagateError(); - - for (const auto& uid : currentScope()->moduleScopeData().exportedBindings()) { - if (currentScope()->hasDeclaredVariable(uid)) { - currentScope()->declaredVariables().markVariableAsExported(uid); - continue; - } - - if (currentScope()->hasLexicallyDeclaredVariable(uid)) { - currentScope()->lexicalVariables().markVariableAsExported(uid); - continue; - } - - semanticFail("Exported binding '", uid.get(), "' needs to refer to a top-level declared variable"); - } - - return sourceElements; -} - -template <typename LexerType> -template <class TreeBuilder> TreeStatement Parser<LexerType>::parseStatementListItem(TreeBuilder& context, const Identifier*& directive, unsigned* directiveLiteralLength) -{ - // The grammar is documented here: - // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-statements - DepthManager statementDepth(&m_statementDepth); - m_statementDepth++; - TreeStatement result = 0; - bool shouldSetEndOffset = true; - switch (m_token.m_type) { - case CONSTTOKEN: - result = parseVariableDeclaration(context, DeclarationType::ConstDeclaration); - break; - case LET: { - bool shouldParseVariableDeclaration = true; - if (!strictMode()) { - SavePoint savePoint = createSavePoint(); - next(); - if (!match(IDENT) && !match(OPENBRACE) && !match(OPENBRACKET)) - shouldParseVariableDeclaration = false; - restoreSavePoint(savePoint); - } - if (shouldParseVariableDeclaration) - result = parseVariableDeclaration(context, DeclarationType::LetDeclaration); - else - result = parseExpressionOrLabelStatement(context); // Treat this as an IDENT. This is how ::parseStatement() handles IDENT. - - break; - } -#if ENABLE(ES6_CLASS_SYNTAX) - case CLASSTOKEN: - result = parseClassDeclaration(context); - break; -#endif - default: - m_statementDepth--; // parseStatement() increments the depth. - result = parseStatement(context, directive, directiveLiteralLength); - shouldSetEndOffset = false; - break; - } - - if (result && shouldSetEndOffset) - context.setEndOffset(result, m_lastTokenEndPosition.offset); - - return result; + failIfFalse(autoSemiColon(), "Expected ';' after var declaration"); + + return context.createVarStatement(location, varDecls, start, end); } template <typename LexerType> -template <class TreeBuilder> TreeStatement Parser<LexerType>::parseVariableDeclaration(TreeBuilder& context, DeclarationType declarationType, ExportType exportType) +template <class TreeBuilder> TreeStatement Parser<LexerType>::parseConstDeclaration(TreeBuilder& context) { - ASSERT(match(VAR) || match(LET) || match(CONSTTOKEN)); + ASSERT(match(CONSTTOKEN)); JSTokenLocation location(tokenLocation()); int start = tokenLine(); int end = 0; - int scratch; - TreeDestructuringPattern scratch1 = 0; - TreeExpression scratch2 = 0; - JSTextPosition scratch3; - bool scratchBool; - TreeExpression variableDecls = parseVariableDeclarationList(context, scratch, scratch1, scratch2, scratch3, scratch3, scratch3, VarDeclarationContext, declarationType, exportType, scratchBool); + TreeConstDeclList constDecls = parseConstDeclarationList(context); propagateError(); - failIfFalse(autoSemiColon(), "Expected ';' after variable declaration"); + failIfFalse(autoSemiColon(), "Expected ';' after const declaration"); - return context.createDeclarationStatement(location, variableDecls, start, end); + return context.createConstStatement(location, constDecls, start, end); } template <typename LexerType> @@ -571,49 +412,27 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseWhileStatemen } template <typename LexerType> -template <class TreeBuilder> TreeExpression Parser<LexerType>::parseVariableDeclarationList(TreeBuilder& context, int& declarations, TreeDestructuringPattern& lastPattern, TreeExpression& lastInitializer, JSTextPosition& identStart, JSTextPosition& initStart, JSTextPosition& initEnd, VarDeclarationListContext declarationListContext, DeclarationType declarationType, ExportType exportType, bool& forLoopConstDoesNotHaveInitializer) +template <class TreeBuilder> TreeExpression Parser<LexerType>::parseVarDeclarationList(TreeBuilder& context, int& declarations, TreeDeconstructionPattern& lastPattern, TreeExpression& lastInitializer, JSTextPosition& identStart, JSTextPosition& initStart, JSTextPosition& initEnd) { - ASSERT(declarationType == DeclarationType::LetDeclaration || declarationType == DeclarationType::VarDeclaration || declarationType == DeclarationType::ConstDeclaration); - TreeExpression head = 0; - TreeExpression tail = 0; + TreeExpression varDecls = 0; const Identifier* lastIdent; - JSToken lastIdentToken; - AssignmentContext assignmentContext = assignmentContextFromDeclarationType(declarationType); do { lastIdent = 0; - lastPattern = TreeDestructuringPattern(0); + lastPattern = 0; JSTokenLocation location(tokenLocation()); next(); TreeExpression node = 0; declarations++; bool hasInitializer = false; - if (match(IDENT) || isLETMaskedAsIDENT()) { - failIfTrue(match(LET) && (declarationType == DeclarationType::LetDeclaration || declarationType == DeclarationType::ConstDeclaration), - "Can't use 'let' as an identifier name for a LexicalDeclaration"); + if (match(IDENT)) { JSTextPosition varStart = tokenStartPosition(); - JSTokenLocation varStartLocation(tokenLocation()); identStart = varStart; const Identifier* name = m_token.m_data.ident; lastIdent = name; - lastIdentToken = m_token; next(); hasInitializer = match(EQUAL); - DeclarationResultMask declarationResult = declareVariable(name, declarationType); - if (declarationResult != DeclarationResult::Valid) { - failIfTrueIfStrict(declarationResult & DeclarationResult::InvalidStrictMode, "Cannot declare a variable named ", name->impl(), " in strict mode"); - if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration) { - if (declarationType == DeclarationType::LetDeclaration) - internalFailWithMessage(false, "Cannot declare a let variable twice: '", name->impl(), "'"); - if (declarationType == DeclarationType::ConstDeclaration) - internalFailWithMessage(false, "Cannot declare a const variable twice: '", name->impl(), "'"); - RELEASE_ASSERT_NOT_REACHED(); - } - } - if (exportType == ExportType::Exported) { - semanticFailIfFalse(exportName(*name), "Cannot export a duplicate name '", name->impl(), "'"); - currentScope()->moduleScopeData().exportBinding(*name); - } - + failIfFalseIfStrict(declareVariable(name), "Cannot declare a variable named ", name->impl(), " in strict mode"); + context.addVar(name, (hasInitializer || (!m_allowsIn && (match(INTOKEN) || isofToken()))) ? DeclarationStacks::HasInitializer : 0); if (hasInitializer) { JSTextPosition varDivot = tokenStartPosition() + 1; initStart = tokenStartPosition(); @@ -623,82 +442,73 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parseVariableDecl lastInitializer = initializer; failIfFalse(initializer, "Expected expression as the intializer for the variable '", name->impl(), "'"); - node = context.createAssignResolve(location, *name, initializer, varStart, varDivot, lastTokenEndPosition(), assignmentContext); - } else { - if (declarationListContext == ForLoopContext && declarationType == DeclarationType::ConstDeclaration) - forLoopConstDoesNotHaveInitializer = true; - failIfTrue(declarationListContext != ForLoopContext && declarationType == DeclarationType::ConstDeclaration, "const declared variable '", name->impl(), "'", " must have an initializer"); - if (declarationType == DeclarationType::VarDeclaration) - node = context.createEmptyVarExpression(varStartLocation, *name); - else - node = context.createEmptyLetExpression(varStartLocation, *name); + node = context.createAssignResolve(location, *name, initializer, varStart, varDivot, lastTokenEndPosition()); } } else { lastIdent = 0; - auto pattern = parseDestructuringPattern(context, destructuringKindFromDeclarationType(declarationType), exportType, nullptr, nullptr, assignmentContext); - failIfFalse(pattern, "Cannot parse this destructuring pattern"); + auto pattern = parseDeconstructionPattern(context, DeconstructToVariables); + failIfFalse(pattern, "Cannot parse this deconstruction pattern"); hasInitializer = match(EQUAL); - failIfTrue(declarationListContext == VarDeclarationContext && !hasInitializer, "Expected an initializer in destructuring variable declaration"); lastPattern = pattern; if (hasInitializer) { next(TreeBuilder::DontBuildStrings); // consume '=' - TreeExpression rhs = parseAssignmentExpression(context); - node = context.createDestructuringAssignment(location, pattern, rhs); - lastInitializer = rhs; + TreeExpression rhs = parseExpression(context); + node = context.createDeconstructingAssignment(location, pattern, rhs); } } - if (!head) - head = node; - else if (!tail) { - head = context.createCommaExpr(location, head); - tail = context.appendToCommaExpr(location, head, head, node); - } else - tail = context.appendToCommaExpr(location, head, tail, node); + if (hasInitializer) { + if (!varDecls) + varDecls = node; + else + varDecls = context.combineCommaNodes(location, varDecls, node); + } } while (match(COMMA)); if (lastIdent) - lastPattern = context.createBindingLocation(lastIdentToken.m_location, *lastIdent, lastIdentToken.m_startPosition, lastIdentToken.m_endPosition, assignmentContext); - - return head; + lastPattern = createBindingPattern(context, DeconstructToVariables, *lastIdent, 0); + return varDecls; } template <typename LexerType> -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) +template <class TreeBuilder> TreeDeconstructionPattern Parser<LexerType>::createBindingPattern(TreeBuilder& context, DeconstructionKind kind, const Identifier& name, int depth) { + ASSERT(!name.isEmpty()); ASSERT(!name.isNull()); - ASSERT(name.impl()->isAtomic() || name.impl()->isSymbol()); - - if (kind == DestructureToVariables) - failIfTrueIfStrict(declareVariable(&name) & DeclarationResult::InvalidStrictMode, "Cannot declare a variable named '", name.impl(), "' in strict mode"); - else if (kind == DestructureToLet || kind == DestructureToConst) { - DeclarationResultMask declarationResult = declareVariable(&name, kind == DestructureToLet ? DeclarationType::LetDeclaration : DeclarationType::ConstDeclaration); - if (declarationResult != DeclarationResult::Valid) { - failIfTrueIfStrict(declarationResult & DeclarationResult::InvalidStrictMode, "Cannot destructure to a variable named '", name.impl(), "' in strict mode"); - failIfTrue(declarationResult & DeclarationResult::InvalidDuplicateDeclaration, "Cannot declare a lexical variable twice: '", name.impl(), "'"); - } - } else if (kind == DestructureToParameters) { - if (depth) { + ASSERT(name.impl()->isIdentifier()); + if (depth) { + if (kind == DeconstructToVariables) + failIfFalseIfStrict(declareVariable(&name), "Cannot deconstruct to a variable named '", name.impl(), "' in strict mode"); + if (kind == DeconstructToParameters) { auto bindingResult = declareBoundParameter(&name); if (bindingResult == Scope::StrictBindingFailed && strictMode()) { - semanticFailIfTrue(isEvalOrArguments(&name), "Cannot destructure to a parameter name '", name.impl(), "' in strict mode"); + semanticFailIfTrue(m_vm->propertyNames->arguments == name || m_vm->propertyNames->eval == name, "Cannot deconstruct to a parameter name '", name.impl(), "' in strict mode"); if (m_lastFunctionName && name == *m_lastFunctionName) - semanticFail("Cannot destructure to '", name.impl(), "' as it shadows the name of a strict mode function"); + semanticFail("Cannot deconstruct to '", name.impl(), "' as it shadows the name of a strict mode function"); semanticFailureDueToKeyword("bound parameter name"); if (hasDeclaredParameter(name)) - semanticFail("Cannot destructure to '", name.impl(), "' as it has already been declared"); + semanticFail("Cannot deconstruct to '", name.impl(), "' as it has already been declared"); semanticFail("Cannot bind to a parameter named '", name.impl(), "' in strict mode"); } if (bindingResult == Scope::BindingFailed) { semanticFailureDueToKeyword("bound parameter name"); if (hasDeclaredParameter(name)) - semanticFail("Cannot destructure to '", name.impl(), "' as it has already been declared"); - semanticFail("Cannot destructure to a parameter named '", name.impl(), "'"); + semanticFail("Cannot deconstruct to '", name.impl(), "' as it has already been declared"); + semanticFail("Cannot deconstruct to a parameter named '", name.impl(), "'"); } - } else { - DeclarationResultMask declarationResult = declareParameter(&name); - if ((declarationResult & DeclarationResult::InvalidStrictMode) && strictMode()) { - semanticFailIfTrue(isEvalOrArguments(&name), "Cannot destructure to a parameter name '", name.impl(), "' in strict mode"); + } + if (kind != DeconstructToExpressions) + context.addVar(&name, kind == DeconstructToParameters ? 0 : DeclarationStacks::HasInitializer); + } else { + if (kind == DeconstructToVariables) { + failIfFalseIfStrict(declareVariable(&name), "Cannot declare a variable named '", name.impl(), "' in strict mode"); + context.addVar(&name, DeclarationStacks::HasInitializer); + } + + if (kind == DeconstructToParameters) { + bool declarationResult = declareParameter(&name); + if (!declarationResult && strictMode()) { + semanticFailIfTrue(m_vm->propertyNames->arguments == name || m_vm->propertyNames->eval == name, "Cannot deconstruct to a parameter name '", name.impl(), "' in strict mode"); if (m_lastFunctionName && name == *m_lastFunctionName) semanticFail("Cannot declare a parameter named '", name.impl(), "' as it shadows the name of a strict mode function"); semanticFailureDueToKeyword("parameter name"); @@ -706,197 +516,122 @@ template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::createB semanticFail("Cannot declare a parameter named '", name.impl(), "' in strict mode as it has already been declared"); semanticFail("Cannot declare a parameter named '", name.impl(), "' in strict mode"); } - if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration) { - // It's not always an error to define a duplicate parameter. - // It's only an error when there are default parameter values or destructuring parameters. - // We note this value now so we can check it later. - if (duplicateIdentifier) - *duplicateIdentifier = &name; - } } } - - if (exportType == ExportType::Exported) { - semanticFailIfFalse(exportName(name), "Cannot export a duplicate name '", name.impl(), "'"); - currentScope()->moduleScopeData().exportBinding(name); - } - return context.createBindingLocation(token.m_location, name, token.m_startPosition, token.m_endPosition, bindingContext); -} - -template <typename LexerType> -template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseArrowFunctionSingleExpressionBodySourceElements(TreeBuilder& context) -{ - ASSERT(!match(OPENBRACE)); - - JSTokenLocation location(tokenLocation()); - JSTextPosition start = tokenStartPosition(); - - failIfStackOverflow(); - TreeExpression expr = parseAssignmentExpression(context); - failIfFalse(expr, "Cannot parse the arrow function expression"); - - context.setEndOffset(expr, m_lastTokenEndPosition.offset); - - failIfFalse(isEndOfArrowFunction(), "Expected a ';', ']', '}', ')', ',', line terminator or EOF following a arrow function statement"); - - JSTextPosition end = tokenEndPosition(); - - if (!m_lexer->prevTerminator()) - setEndOfStatement(); - - TreeSourceElements sourceElements = context.createSourceElements(); - TreeStatement body = context.createReturnStatement(location, expr, start, end); - context.setEndOffset(body, m_lastTokenEndPosition.offset); - context.appendStatement(sourceElements, body); - - return sourceElements; + return context.createBindingLocation(m_token.m_location, name, m_token.m_startPosition, m_token.m_endPosition); } template <typename LexerType> -template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::tryParseDestructuringPatternExpression(TreeBuilder& context, AssignmentContext bindingContext) +template <class TreeBuilder> TreeDeconstructionPattern Parser<LexerType>::tryParseDeconstructionPatternExpression(TreeBuilder& context) { - return parseDestructuringPattern(context, DestructureToExpressions, ExportType::NotExported, nullptr, nullptr, bindingContext); + return parseDeconstructionPattern(context, DeconstructToExpressions); } template <typename LexerType> -template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::parseDestructuringPattern(TreeBuilder& context, DestructuringKind kind, ExportType exportType, const Identifier** duplicateIdentifier, bool* hasDestructuringPattern, AssignmentContext bindingContext, int depth) +template <class TreeBuilder> TreeDeconstructionPattern Parser<LexerType>::parseDeconstructionPattern(TreeBuilder& context, DeconstructionKind kind, int depth) { failIfStackOverflow(); int nonLHSCount = m_nonLHSCount; - TreeDestructuringPattern pattern; + TreeDeconstructionPattern pattern; switch (m_token.m_type) { case OPENBRACKET: { - JSTextPosition divotStart = tokenStartPosition(); auto arrayPattern = context.createArrayPattern(m_token.m_location); next(); - - if (hasDestructuringPattern) - *hasDestructuringPattern = true; - - bool restElementWasFound = false; - + if (kind == DeconstructToExpressions && match(CLOSEBRACKET)) + return 0; + failIfTrue(match(CLOSEBRACKET), "There must be at least one bound property in an array deconstruction pattern"); do { while (match(COMMA)) { context.appendArrayPatternSkipEntry(arrayPattern, m_token.m_location); next(); } propagateError(); - - if (match(CLOSEBRACKET)) - break; - - if (UNLIKELY(match(DOTDOTDOT))) { - JSTokenLocation location = m_token.m_location; - next(); - auto innerPattern = parseDestructuringPattern(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1); - if (kind == DestructureToExpressions && !innerPattern) - return 0; - failIfFalse(innerPattern, "Cannot parse this destructuring pattern"); - - failIfTrue(kind != DestructureToExpressions && !context.isBindingNode(innerPattern), "Expected identifier for a rest element destructuring pattern"); - - context.appendArrayPatternRestEntry(arrayPattern, location, innerPattern); - restElementWasFound = true; - break; - } - JSTokenLocation location = m_token.m_location; - auto innerPattern = parseDestructuringPattern(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1); - if (kind == DestructureToExpressions && !innerPattern) + auto innerPattern = parseDeconstructionPattern(context, kind, depth + 1); + if (kind == DeconstructToExpressions && !innerPattern) return 0; - failIfFalse(innerPattern, "Cannot parse this destructuring pattern"); - TreeExpression defaultValue = parseDefaultValueForDestructuringPattern(context); - context.appendArrayPatternEntry(arrayPattern, location, innerPattern, defaultValue); + failIfFalse(innerPattern, "Cannot parse this deconstruction pattern"); + context.appendArrayPatternEntry(arrayPattern, location, innerPattern); } while (consume(COMMA)); - - if (kind == DestructureToExpressions && !match(CLOSEBRACKET)) + + if (kind == DeconstructToExpressions && !match(CLOSEBRACKET)) return 0; - consumeOrFail(CLOSEBRACKET, restElementWasFound ? "Expected a closing ']' following a rest element destructuring pattern" : "Expected either a closing ']' or a ',' following an element destructuring pattern"); - context.finishArrayPattern(arrayPattern, divotStart, divotStart, lastTokenEndPosition()); + + consumeOrFail(CLOSEBRACKET, "Expected either a closing ']' or a ',' following an element deconstruction pattern"); pattern = arrayPattern; break; } case OPENBRACE: { - auto objectPattern = context.createObjectPattern(m_token.m_location); next(); + + if (kind == DeconstructToExpressions && match(CLOSEBRACE)) + return 0; - if (hasDestructuringPattern) - *hasDestructuringPattern = true; - + failIfTrue(match(CLOSEBRACE), "There must be at least one bound property in an object deconstruction pattern"); + auto objectPattern = context.createObjectPattern(m_token.m_location); + bool wasString = false; do { - bool wasString = false; - - if (match(CLOSEBRACE)) - break; - - const Identifier* propertyName = nullptr; - TreeDestructuringPattern innerPattern = 0; + Identifier propertyName; + TreeDeconstructionPattern innerPattern = 0; JSTokenLocation location = m_token.m_location; - if (match(IDENT) || isLETMaskedAsIDENT()) { - failIfTrue(match(LET) && (kind == DestructureToLet || kind == DestructureToConst), "Can't use 'let' as an identifier name for a LexicalDeclaration"); - propertyName = m_token.m_data.ident; - JSToken identifierToken = m_token; + if (match(IDENT)) { + propertyName = *m_token.m_data.ident; next(); if (consume(COLON)) - innerPattern = parseDestructuringPattern(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1); + innerPattern = parseDeconstructionPattern(context, kind, depth + 1); else - innerPattern = createBindingPattern(context, kind, exportType, *propertyName, depth + 1, identifierToken, bindingContext, duplicateIdentifier); + innerPattern = createBindingPattern(context, kind, propertyName, depth); } else { JSTokenType tokenType = m_token.m_type; switch (m_token.m_type) { - case DOUBLE: - case INTEGER: - propertyName = &m_parserArena.identifierArena().makeNumericIdentifier(const_cast<VM*>(m_vm), m_token.m_data.doubleValue); + case NUMBER: + propertyName = Identifier::from(m_vm, m_token.m_data.doubleValue); break; case STRING: - propertyName = m_token.m_data.ident; + propertyName = *m_token.m_data.ident; wasString = true; break; default: if (m_token.m_type != RESERVED && m_token.m_type != RESERVED_IF_STRICT && !(m_token.m_type & KeywordTokenFlag)) { - if (kind == DestructureToExpressions) + if (kind == DeconstructToExpressions) return 0; failWithMessage("Expected a property name"); } - propertyName = m_token.m_data.ident; + propertyName = *m_token.m_data.ident; break; } next(); if (!consume(COLON)) { - if (kind == DestructureToExpressions) + if (kind == DeconstructToExpressions) return 0; - semanticFailIfTrue(tokenType == RESERVED, "Cannot use abbreviated destructuring syntax for reserved name '", propertyName->impl(), "'"); - semanticFailIfTrue(tokenType == RESERVED_IF_STRICT, "Cannot use abbreviated destructuring syntax for reserved name '", propertyName->impl(), "' in strict mode"); - semanticFailIfTrue(tokenType & KeywordTokenFlag, "Cannot use abbreviated destructuring syntax for keyword '", propertyName->impl(), "'"); + semanticFailIfTrue(tokenType == RESERVED, "Cannot use abbreviated deconstruction syntax for reserved name '", propertyName.impl(), "'"); + semanticFailIfTrue(tokenType == RESERVED_IF_STRICT, "Cannot use abbreviated deconstruction syntax for reserved name '", propertyName.impl(), "' in strict mode"); + semanticFailIfTrue(tokenType & KeywordTokenFlag, "Cannot use abbreviated deconstruction syntax for keyword '", propertyName.impl(), "'"); - failWithMessage("Expected a ':' prior to a named destructuring property"); + failWithMessage("Expected a ':' prior to named property deconstruction"); } - innerPattern = parseDestructuringPattern(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1); + innerPattern = parseDeconstructionPattern(context, kind, depth + 1); } - if (kind == DestructureToExpressions && !innerPattern) + if (kind == DeconstructToExpressions && !innerPattern) return 0; - failIfFalse(innerPattern, "Cannot parse this destructuring pattern"); - TreeExpression defaultValue = parseDefaultValueForDestructuringPattern(context); - ASSERT(propertyName); - context.appendObjectPatternEntry(objectPattern, location, wasString, *propertyName, innerPattern, defaultValue); + failIfFalse(innerPattern, "Cannot parse this deconstruction pattern"); + context.appendObjectPatternEntry(objectPattern, location, wasString, propertyName, innerPattern); } while (consume(COMMA)); - - if (kind == DestructureToExpressions && !match(CLOSEBRACE)) + if (kind == DeconstructToExpressions && !match(CLOSEBRACE)) return 0; - consumeOrFail(CLOSEBRACE, "Expected either a closing '}' or an ',' after a property destructuring pattern"); + consumeOrFail(CLOSEBRACE, "Expected either a closing '}' or an ',' after a property deconstruction pattern"); pattern = objectPattern; break; } default: { - if (!match(IDENT) && !isLETMaskedAsIDENT()) { - if (kind == DestructureToExpressions) + if (!match(IDENT)) { + if (kind == DeconstructToExpressions) return 0; semanticFailureDueToKeyword("variable name"); failWithMessage("Expected a parameter pattern or a ')' in parameter list"); } - failIfTrue(match(LET) && (kind == DestructureToLet || kind == DestructureToConst), "Can't use 'let' as an identifier name for a LexicalDeclaration"); - pattern = createBindingPattern(context, kind, exportType, *m_token.m_data.ident, depth, m_token, bindingContext, duplicateIdentifier); + pattern = createBindingPattern(context, kind, *m_token.m_data.ident, depth); next(); break; } @@ -906,13 +641,32 @@ template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::parseDe } template <typename LexerType> -template <class TreeBuilder> TreeExpression Parser<LexerType>::parseDefaultValueForDestructuringPattern(TreeBuilder& context) +template <class TreeBuilder> TreeConstDeclList Parser<LexerType>::parseConstDeclarationList(TreeBuilder& context) { - if (!match(EQUAL)) - return 0; - - next(TreeBuilder::DontBuildStrings); // consume '=' - return parseAssignmentExpression(context); + failIfTrue(strictMode(), "Const declarations are not supported in strict mode"); + TreeConstDeclList constDecls = 0; + TreeConstDeclList tail = 0; + do { + JSTokenLocation location(tokenLocation()); + next(); + matchOrFail(IDENT, "Expected an identifier name in const declaration"); + const Identifier* name = m_token.m_data.ident; + next(); + bool hasInitializer = match(EQUAL); + declareVariable(name); + context.addVar(name, DeclarationStacks::IsConstant | (hasInitializer ? DeclarationStacks::HasInitializer : 0)); + + TreeExpression initializer = 0; + if (hasInitializer) { + next(TreeBuilder::DontBuildStrings); // consume '=' + initializer = parseAssignmentExpression(context); + failIfFalse(!!initializer, "Unable to parse initializer"); + } + tail = context.appendConstDecl(location, tail, name, initializer); + if (!constDecls) + constDecls = tail; + } while (match(COMMA)); + return constDecls; } template <typename LexerType> @@ -928,56 +682,18 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseForStatement( JSTextPosition declsStart; JSTextPosition declsEnd; TreeExpression decls = 0; - TreeDestructuringPattern pattern = 0; - bool isVarDeclaraton = match(VAR); - bool isLetDeclaration = match(LET); - bool isConstDeclaration = match(CONSTTOKEN); - bool forLoopConstDoesNotHaveInitializer = false; - - VariableEnvironment dummySet; - VariableEnvironment* lexicalVariables = nullptr; - AutoCleanupLexicalScope lexicalScope; - - auto gatherLexicalVariablesIfNecessary = [&] { - if (isLetDeclaration || isConstDeclaration) { - ScopeRef scope = lexicalScope.scope(); - lexicalVariables = &scope->finalizeLexicalEnvironment(); - } else - lexicalVariables = &dummySet; - }; - - auto popLexicalScopeIfNecessary = [&] { - if (isLetDeclaration || isConstDeclaration) - popScope(lexicalScope, TreeBuilder::NeedsFreeVariableInfo); - }; - - if (isVarDeclaraton || isLetDeclaration || isConstDeclaration) { + TreeDeconstructionPattern pattern = 0; + if (match(VAR)) { /* - for (var/let/const IDENT in/of expression) statement - for (var/let/const varDeclarationList; expressionOpt; expressionOpt) + for (var IDENT in expression) statement + for (var varDeclarationList; expressionOpt; expressionOpt) */ - if (isLetDeclaration || isConstDeclaration) { - ScopeRef newScope = pushScope(); - newScope->setIsLexicalScope(); - newScope->preventVarDeclarations(); - lexicalScope.setIsValid(newScope, this); - } - - TreeDestructuringPattern forInTarget = 0; + TreeDeconstructionPattern forInTarget = 0; TreeExpression forInInitializer = 0; m_allowsIn = false; JSTextPosition initStart; JSTextPosition initEnd; - DeclarationType declarationType; - if (isVarDeclaraton) - declarationType = DeclarationType::VarDeclaration; - else if (isLetDeclaration) - declarationType = DeclarationType::LetDeclaration; - else if (isConstDeclaration) - declarationType = DeclarationType::ConstDeclaration; - else - RELEASE_ASSERT_NOT_REACHED(); - decls = parseVariableDeclarationList(context, declarations, forInTarget, forInInitializer, declsStart, initStart, initEnd, ForLoopContext, declarationType, ExportType::NotExported, forLoopConstDoesNotHaveInitializer); + decls = parseVarDeclarationList(context, declarations, forInTarget, forInInitializer, declsStart, initStart, initEnd); m_allowsIn = true; propagateError(); @@ -985,21 +701,18 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseForStatement( if (match(SEMICOLON)) goto standardForLoop; - failIfFalse(declarations == 1, "can only declare a single variable in an enumeration"); - failIfTrueIfStrict(forInInitializer, "Cannot use initialiser syntax in a strict mode enumeration"); - - if (forInInitializer) - failIfFalse(context.isBindingNode(forInTarget), "Cannot use initialiser syntax when binding to a pattern during enumeration"); - + failIfFalse(declarations == 1, "must declare variables after 'var'"); + failIfTrue(forInInitializer, "Cannot use initialiser syntax in a for-in loop"); + // Handle for-in with var declaration JSTextPosition inLocation = tokenStartPosition(); bool isOfEnumeration = false; if (!consume(INTOKEN)) { failIfFalse(match(IDENT) && *m_token.m_data.ident == m_vm->propertyNames->of, "Expected either 'in' or 'of' in enumeration syntax"); isOfEnumeration = true; - failIfTrue(forInInitializer, "Cannot use initialiser syntax in a for-of enumeration"); next(); } + TreeExpression expr = parseExpression(context); failIfFalse(expr, "Expected expression to enumerate"); JSTextPosition exprEnd = lastTokenEndPosition(); @@ -1013,25 +726,20 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseForStatement( TreeStatement statement = parseStatement(context, unused); endLoop(); failIfFalse(statement, "Expected statement as body of for-", isOfEnumeration ? "of" : "in", " statement"); - gatherLexicalVariablesIfNecessary(); - TreeStatement result; if (isOfEnumeration) - result = context.createForOfLoop(location, forInTarget, expr, statement, declsStart, inLocation, exprEnd, startLine, endLine, *lexicalVariables); - else - result = context.createForInLoop(location, forInTarget, expr, statement, declsStart, inLocation, exprEnd, startLine, endLine, *lexicalVariables); - popLexicalScopeIfNecessary(); - return result; + return context.createForOfLoop(location, forInTarget, expr, statement, declsStart, inLocation, exprEnd, startLine, endLine); + return context.createForInLoop(location, forInTarget, expr, statement, declsStart, inLocation, exprEnd, startLine, endLine); } if (!match(SEMICOLON)) { if (match(OPENBRACE) || match(OPENBRACKET)) { SavePoint savePoint = createSavePoint(); declsStart = tokenStartPosition(); - pattern = tryParseDestructuringPatternExpression(context, AssignmentContext::DeclarationStatement); + pattern = tryParseDeconstructionPatternExpression(context); declsEnd = lastTokenEndPosition(); if (pattern && (match(INTOKEN) || (match(IDENT) && *m_token.m_data.ident == m_vm->propertyNames->of))) goto enumerationLoop; - pattern = TreeDestructuringPattern(0); + pattern = 0; restoreSavePoint(savePoint); } m_allowsIn = false; @@ -1047,7 +755,6 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseForStatement( // Standard for loop next(); TreeExpression condition = 0; - failIfTrue(forLoopConstDoesNotHaveInitializer && isConstDeclaration, "const variables in for loops must have initializers"); if (!match(SEMICOLON)) { condition = parseExpression(context); @@ -1067,13 +774,10 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseForStatement( TreeStatement statement = parseStatement(context, unused); endLoop(); failIfFalse(statement, "Expected a statement as the body of a for loop"); - gatherLexicalVariablesIfNecessary(); - TreeStatement result = context.createForLoop(location, decls, condition, increment, statement, startLine, endLine, *lexicalVariables); - popLexicalScopeIfNecessary(); - return result; + return context.createForLoop(location, decls, condition, increment, statement, startLine, endLine); } - // For-in and For-of loop + // For-in loop enumerationLoop: failIfFalse(nonLHSCount == m_nonLHSCount, "Expected a reference on the left hand side of an enumeration statement"); bool isOfEnumeration = false; @@ -1093,24 +797,15 @@ enumerationLoop: TreeStatement statement = parseStatement(context, unused); endLoop(); failIfFalse(statement, "Expected a statement as the body of a for-", isOfEnumeration ? "of" : "in", "loop"); - gatherLexicalVariablesIfNecessary(); - TreeStatement result; if (pattern) { ASSERT(!decls); if (isOfEnumeration) - result = context.createForOfLoop(location, pattern, expr, statement, declsStart, declsEnd, exprEnd, startLine, endLine, *lexicalVariables); - else - result = context.createForInLoop(location, pattern, expr, statement, declsStart, declsEnd, exprEnd, startLine, endLine, *lexicalVariables); - - popLexicalScopeIfNecessary(); - return result; + return context.createForOfLoop(location, pattern, expr, statement, declsStart, declsEnd, exprEnd, startLine, endLine); + return context.createForInLoop(location, pattern, expr, statement, declsStart, declsEnd, exprEnd, startLine, endLine); } if (isOfEnumeration) - result = context.createForOfLoop(location, decls, expr, statement, declsStart, declsEnd, exprEnd, startLine, endLine, *lexicalVariables); - else - result = context.createForInLoop(location, decls, expr, statement, declsStart, declsEnd, exprEnd, startLine, endLine, *lexicalVariables); - popLexicalScopeIfNecessary(); - return result; + return context.createForOfLoop(location, decls, expr, statement, declsStart, declsEnd, exprEnd, startLine, endLine); + return context.createForInLoop(location, decls, expr, statement, declsStart, declsEnd, exprEnd, startLine, endLine); } template <typename LexerType> @@ -1124,9 +819,9 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseBreakStatemen if (autoSemiColon()) { semanticFailIfFalse(breakIsValid(), "'break' is only valid inside a switch or loop statement"); - return context.createBreakStatement(location, &m_vm->propertyNames->nullIdentifier, start, end); + return context.createBreakStatement(location, start, end); } - failIfFalse(match(IDENT) || isLETMaskedAsIDENT(), "Expected an identifier as the target for a break statement"); + matchOrFail(IDENT, "Expected an identifier as the target for a break statement"); const Identifier* ident = m_token.m_data.ident; semanticFailIfFalse(getLabel(ident), "Cannot use the undeclared label '", ident->impl(), "'"); end = tokenEndPosition(); @@ -1146,13 +841,13 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseContinueState if (autoSemiColon()) { semanticFailIfFalse(continueIsValid(), "'continue' is only valid inside a loop statement"); - return context.createContinueStatement(location, &m_vm->propertyNames->nullIdentifier, start, end); + return context.createContinueStatement(location, start, end); } - failIfFalse(match(IDENT) || isLETMaskedAsIDENT(), "Expected an identifier as the target for a continue statement"); + matchOrFail(IDENT, "Expected an identifier as the target for a continue statement"); const Identifier* ident = m_token.m_data.ident; ScopeLabelInfo* label = getLabel(ident); semanticFailIfFalse(label, "Cannot use the undeclared label '", ident->impl(), "'"); - semanticFailIfFalse(label->isLoop, "Cannot continue to the label '", ident->impl(), "' as it is not targeting a loop"); + semanticFailIfFalse(label->m_isLoop, "Cannot continue to the label '", ident->impl(), "' as it is not targeting a loop"); end = tokenEndPosition(); next(); failIfFalse(autoSemiColon(), "Expected a ';' following a targeted continue statement"); @@ -1242,9 +937,6 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseSwitchStateme handleProductionOrFail(CLOSEPAREN, ")", "end", "subject of a 'switch'"); handleProductionOrFail(OPENBRACE, "{", "start", "body of a 'switch'"); - AutoPopScopeRef lexicalScope(this, pushScope()); - lexicalScope->setIsLexicalScope(); - lexicalScope->preventVarDeclarations(); startSwitch(); TreeClauseList firstClauses = parseSwitchClauses(context); propagateError(); @@ -1257,9 +949,8 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseSwitchStateme endSwitch(); handleProductionOrFail(CLOSEBRACE, "}", "end", "body of a 'switch'"); - TreeStatement result = context.createSwitchStatement(location, expr, firstClauses, defaultClause, secondClauses, startLine, endLine, lexicalScope->finalizeLexicalEnvironment()); - popScope(lexicalScope, TreeBuilder::NeedsFreeVariableInfo); - return result; + return context.createSwitchStatement(location, expr, firstClauses, defaultClause, secondClauses, startLine, endLine); + } template <typename LexerType> @@ -1267,7 +958,6 @@ template <class TreeBuilder> TreeClauseList Parser<LexerType>::parseSwitchClause { if (!match(CASE)) return 0; - unsigned startOffset = tokenStart(); next(); TreeExpression condition = parseExpression(context); failIfFalse(condition, "Cannot parse switch clause"); @@ -1275,12 +965,10 @@ template <class TreeBuilder> TreeClauseList Parser<LexerType>::parseSwitchClause TreeSourceElements statements = parseSourceElements(context, DontCheckForStrictMode); failIfFalse(statements, "Cannot parse the body of a switch clause"); TreeClause clause = context.createClause(condition, statements); - context.setStartOffset(clause, startOffset); TreeClauseList clauseList = context.createClauseList(clause); TreeClauseList tail = clauseList; while (match(CASE)) { - startOffset = tokenStart(); next(); TreeExpression condition = parseExpression(context); failIfFalse(condition, "Cannot parse switch case expression"); @@ -1288,7 +976,6 @@ template <class TreeBuilder> TreeClauseList Parser<LexerType>::parseSwitchClause TreeSourceElements statements = parseSourceElements(context, DontCheckForStrictMode); failIfFalse(statements, "Cannot parse the body of a switch clause"); clause = context.createClause(condition, statements); - context.setStartOffset(clause, startOffset); tail = context.createClauseList(tail, clause); } return clauseList; @@ -1299,14 +986,11 @@ template <class TreeBuilder> TreeClause Parser<LexerType>::parseSwitchDefaultCla { if (!match(DEFAULT)) return 0; - unsigned startOffset = tokenStart(); next(); consumeOrFail(COLON, "Expected a ':' after switch default clause"); TreeSourceElements statements = parseSourceElements(context, DontCheckForStrictMode); failIfFalse(statements, "Cannot parse the body of a switch default clause"); - TreeClause result = context.createClause(0, statements); - context.setStartOffset(result, startOffset); - return result; + return context.createClause(0, statements); } template <typename LexerType> @@ -1325,28 +1009,26 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseTryStatement( tryBlock = parseBlockStatement(context); failIfFalse(tryBlock, "Cannot parse the body of try block"); int lastLine = m_lastTokenEndPosition.line; - VariableEnvironment catchEnvironment; + if (match(CATCH)) { + currentScope()->setNeedsFullActivation(); next(); handleProductionOrFail(OPENPAREN, "(", "start", "'catch' target"); - if (!(match(IDENT) || isLETMaskedAsIDENT())) { + if (!match(IDENT)) { semanticFailureDueToKeyword("catch variable name"); failWithMessage("Expected identifier name as catch target"); } ident = m_token.m_data.ident; next(); AutoPopScopeRef catchScope(this, pushScope()); - catchScope->setIsLexicalScope(); - catchScope->preventVarDeclarations(); - failIfTrueIfStrict(catchScope->declareLexicalVariable(ident, false) & DeclarationResult::InvalidStrictMode, "Cannot declare a catch variable named '", ident->impl(), "' in strict mode"); + failIfFalseIfStrict(declareVariable(ident), "Cannot declare a catch variable named '", ident->impl(), "' in strict mode"); + catchScope->preventNewDecls(); handleProductionOrFail(CLOSEPAREN, ")", "end", "'catch' target"); matchOrFail(OPENBRACE, "Expected exception handler to be a block statement"); catchBlock = parseBlockStatement(context); failIfFalse(catchBlock, "Unable to parse 'catch' block"); - catchEnvironment = catchScope->finalizeLexicalEnvironment(); - RELEASE_ASSERT(catchEnvironment.size() == 1 && catchEnvironment.contains(ident->impl())); - popScope(catchScope, TreeBuilder::NeedsFreeVariableInfo); + failIfFalse(popScope(catchScope, TreeBuilder::NeedsFreeVariableInfo), "Parse error"); } if (match(FINALLY)) { @@ -1356,7 +1038,7 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseTryStatement( failIfFalse(finallyBlock, "Cannot parse finally body"); } failIfFalse(catchBlock || finallyBlock, "Try statements must have at least a catch or finally block"); - return context.createTryStatement(location, tryBlock, ident, catchBlock, finallyBlock, firstLine, lastLine, catchEnvironment); + return context.createTryStatement(location, tryBlock, ident, catchBlock, finallyBlock, firstLine, lastLine); } template <typename LexerType> @@ -1377,44 +1059,18 @@ template <typename LexerType> template <class TreeBuilder> TreeStatement Parser<LexerType>::parseBlockStatement(TreeBuilder& context) { ASSERT(match(OPENBRACE)); - - // We should treat the first block statement of the function (the body of the function) as the lexical - // scope of the function itself, and not the lexical scope of a 'block' statement within the function. - AutoCleanupLexicalScope lexicalScope; - bool shouldPushLexicalScope = m_statementDepth > 0; - if (shouldPushLexicalScope) { - ScopeRef newScope = pushScope(); - newScope->setIsLexicalScope(); - newScope->preventVarDeclarations(); - lexicalScope.setIsValid(newScope, this); - } JSTokenLocation location(tokenLocation()); - int startOffset = m_token.m_data.offset; int start = tokenLine(); - VariableEnvironment emptyEnvironment; next(); if (match(CLOSEBRACE)) { - int endOffset = m_token.m_data.offset; next(); - TreeStatement result = context.createBlockStatement(location, 0, start, m_lastTokenEndPosition.line, shouldPushLexicalScope ? currentScope()->finalizeLexicalEnvironment() : emptyEnvironment); - context.setStartOffset(result, startOffset); - context.setEndOffset(result, endOffset); - if (shouldPushLexicalScope) - popScope(lexicalScope, TreeBuilder::NeedsFreeVariableInfo); - return result; + return context.createBlockStatement(location, 0, start, m_lastTokenEndPosition.line); } TreeSourceElements subtree = parseSourceElements(context, DontCheckForStrictMode); failIfFalse(subtree, "Cannot parse the body of the block statement"); matchOrFail(CLOSEBRACE, "Expected a closing '}' at the end of a block statement"); - int endOffset = m_token.m_data.offset; next(); - TreeStatement result = context.createBlockStatement(location, subtree, start, m_lastTokenEndPosition.line, shouldPushLexicalScope ? currentScope()->finalizeLexicalEnvironment() : emptyEnvironment); - context.setStartOffset(result, startOffset); - context.setEndOffset(result, endOffset); - if (shouldPushLexicalScope) - popScope(lexicalScope, TreeBuilder::NeedsFreeVariableInfo); - - return result; + return context.createBlockStatement(location, subtree, start, m_lastTokenEndPosition.line); } template <typename LexerType> @@ -1425,63 +1081,45 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseStatement(Tre directive = 0; int nonTrivialExpressionCount = 0; failIfStackOverflow(); - TreeStatement result = 0; - bool shouldSetEndOffset = true; - switch (m_token.m_type) { case OPENBRACE: - result = parseBlockStatement(context); - shouldSetEndOffset = false; - break; + return parseBlockStatement(context); case VAR: - result = parseVariableDeclaration(context, DeclarationType::VarDeclaration); - break; + return parseVarDeclaration(context); + case CONSTTOKEN: + return parseConstDeclaration(context); case FUNCTION: - failIfFalseIfStrict(m_statementDepth == 1, "Strict mode does not allow function declarations in a lexically nested statement"); - result = parseFunctionDeclaration(context); - break; + failIfFalseIfStrict(m_statementDepth == 1, "Nested functions cannot be declared in strict mode"); + return parseFunctionDeclaration(context); case SEMICOLON: { JSTokenLocation location(tokenLocation()); next(); - result = context.createEmptyStatement(location); - break; + return context.createEmptyStatement(location); } case IF: - result = parseIfStatement(context); - break; + return parseIfStatement(context); case DO: - result = parseDoWhileStatement(context); - break; + return parseDoWhileStatement(context); case WHILE: - result = parseWhileStatement(context); - break; + return parseWhileStatement(context); case FOR: - result = parseForStatement(context); - break; + return parseForStatement(context); case CONTINUE: - result = parseContinueStatement(context); - break; + return parseContinueStatement(context); case BREAK: - result = parseBreakStatement(context); - break; + return parseBreakStatement(context); case RETURN: - result = parseReturnStatement(context); - break; + return parseReturnStatement(context); case WITH: - result = parseWithStatement(context); - break; + return parseWithStatement(context); case SWITCH: - result = parseSwitchStatement(context); - break; + return parseSwitchStatement(context); case THROW: - result = parseThrowStatement(context); - break; + return parseThrowStatement(context); case TRY: - result = parseTryStatement(context); - break; + return parseTryStatement(context); case DEBUGGER: - result = parseDebuggerStatement(context); - break; + return parseDebuggerStatement(context); case EOFTOK: case CASE: case CLOSEBRACE: @@ -1489,8 +1127,7 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseStatement(Tre // These tokens imply the end of a set of source elements return 0; case IDENT: - result = parseExpressionOrLabelStatement(context); - break; + return parseExpressionOrLabelStatement(context); case STRING: directive = m_token.m_data.ident; if (directiveLiteralLength) @@ -1501,539 +1138,187 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseStatement(Tre TreeStatement exprStatement = parseExpressionStatement(context); if (directive && nonTrivialExpressionCount != m_nonTrivialExpressionCount) directive = 0; - result = exprStatement; - break; + return exprStatement; } - - if (result && shouldSetEndOffset) - context.setEndOffset(result, m_lastTokenEndPosition.offset); - return result; } template <typename LexerType> -template <class TreeBuilder> bool Parser<LexerType>::parseFormalParameters(TreeBuilder& context, TreeFormalParameterList list, unsigned& parameterCount) +template <class TreeBuilder> TreeFormalParameterList Parser<LexerType>::parseFormalParameters(TreeBuilder& context) { -#define failFromDuplicate() \ - if (duplicateParameter) {\ - semanticFailIfTrue(defaultValue, "Duplicate parameter '", duplicateParameter->impl(), "' not allowed in function with default parameter values");\ - semanticFailIfTrue(hasDestructuringPattern, "Duplicate parameter '", duplicateParameter->impl(), "' not allowed in function with destructuring parameters");\ - } - - const Identifier* duplicateParameter = nullptr; - bool hasDestructuringPattern = false; - auto parameter = parseDestructuringPattern(context, DestructureToParameters, ExportType::NotExported, &duplicateParameter, &hasDestructuringPattern); + auto parameter = parseDeconstructionPattern(context, DeconstructToParameters); failIfFalse(parameter, "Cannot parse parameter pattern"); - auto defaultValue = parseDefaultValueForDestructuringPattern(context); - propagateError(); - failFromDuplicate(); - context.appendParameter(list, parameter, defaultValue); - parameterCount++; + TreeFormalParameterList list = context.createFormalParameterList(parameter); + TreeFormalParameterList tail = list; while (consume(COMMA)) { - parameter = parseDestructuringPattern(context, DestructureToParameters, ExportType::NotExported, &duplicateParameter, &hasDestructuringPattern); + parameter = parseDeconstructionPattern(context, DeconstructToParameters); failIfFalse(parameter, "Cannot parse parameter pattern"); - defaultValue = parseDefaultValueForDestructuringPattern(context); - propagateError(); - failFromDuplicate(); - context.appendParameter(list, parameter, defaultValue); - parameterCount++; + tail = context.createFormalParameterList(tail, parameter); } - return true; -#undef failFromDuplicate + return list; } template <typename LexerType> -template <class TreeBuilder> TreeFunctionBody Parser<LexerType>::parseFunctionBody( - TreeBuilder& context, const JSTokenLocation& startLocation, int startColumn, int functionKeywordStart, int functionNameStart, int parametersStart, - ConstructorKind constructorKind, FunctionBodyType bodyType, unsigned parameterCount, SourceParseMode parseMode) +template <class TreeBuilder> TreeFunctionBody Parser<LexerType>::parseFunctionBody(TreeBuilder& context) { - if (bodyType == StandardFunctionBodyBlock || bodyType == ArrowFunctionBodyBlock) { - next(); - if (match(CLOSEBRACE)) { - unsigned endColumn = tokenColumn(); - return context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, endColumn, functionKeywordStart, functionNameStart, parametersStart, strictMode(), constructorKind, parameterCount, parseMode); - } - } + JSTokenLocation startLocation(tokenLocation()); + unsigned startColumn = tokenColumn(); + next(); + if (match(CLOSEBRACE)) { + unsigned endColumn = tokenColumn(); + return context.createFunctionBody(startLocation, tokenLocation(), startColumn, endColumn, strictMode()); + } DepthManager statementDepth(&m_statementDepth); m_statementDepth = 0; - SyntaxChecker syntaxChecker(const_cast<VM*>(m_vm), m_lexer.get()); - if (bodyType == ArrowFunctionBodyExpression) - failIfFalse(parseArrowFunctionSingleExpressionBodySourceElements(syntaxChecker), "Cannot parse body of this arrow function"); - else - failIfFalse(parseSourceElements(syntaxChecker, CheckForStrictMode), bodyType == StandardFunctionBodyBlock ? "Cannot parse body of this function" : "Cannot parse body of this arrow function"); + typename TreeBuilder::FunctionBodyBuilder bodyBuilder(const_cast<VM*>(m_vm), m_lexer.get()); + failIfFalse(parseSourceElements(bodyBuilder, CheckForStrictMode), "Cannot parse body of this function"); unsigned endColumn = tokenColumn(); - return context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, endColumn, functionKeywordStart, functionNameStart, parametersStart, strictMode(), constructorKind, parameterCount, parseMode); + return context.createFunctionBody(startLocation, tokenLocation(), startColumn, endColumn, strictMode()); } -static const char* stringForFunctionMode(SourceParseMode mode) +static const char* stringForFunctionMode(FunctionParseMode mode) { switch (mode) { - case SourceParseMode::GetterMode: + case GetterMode: return "getter"; - case SourceParseMode::SetterMode: + case SetterMode: return "setter"; - case SourceParseMode::NormalFunctionMode: + case FunctionMode: return "function"; - case SourceParseMode::MethodMode: - return "method"; - case SourceParseMode::ArrowFunctionMode: - return "arrow function"; - case SourceParseMode::ProgramMode: - case SourceParseMode::ModuleAnalyzeMode: - case SourceParseMode::ModuleEvaluateMode: - RELEASE_ASSERT_NOT_REACHED(); - return ""; } RELEASE_ASSERT_NOT_REACHED(); return nullptr; } -template <typename LexerType> template <class TreeBuilder> int Parser<LexerType>::parseFunctionParameters(TreeBuilder& context, SourceParseMode mode, ParserFunctionInfo<TreeBuilder>& functionInfo) -{ - RELEASE_ASSERT(mode != SourceParseMode::ProgramMode && mode != SourceParseMode::ModuleAnalyzeMode && mode != SourceParseMode::ModuleEvaluateMode); - int parametersStart = m_token.m_location.startOffset; - TreeFormalParameterList parameterList = context.createFormalParameterList(); - functionInfo.parameters = parameterList; - functionInfo.startOffset = parametersStart; - - if (mode == SourceParseMode::ArrowFunctionMode) { - if (!match(IDENT) && !match(OPENPAREN)) { - semanticFailureDueToKeyword(stringForFunctionMode(mode), " name"); - failWithMessage("Expected an arrow function input parameter"); - } else { - if (match(OPENPAREN)) { - next(); - - if (match(CLOSEPAREN)) - functionInfo.parameterCount = 0; - else - failIfFalse(parseFormalParameters(context, parameterList, functionInfo.parameterCount), "Cannot parse parameters for this ", stringForFunctionMode(mode)); - - consumeOrFail(CLOSEPAREN, "Expected a ')' or a ',' after a parameter declaration"); - } else { - functionInfo.parameterCount = 1; - auto parameter = parseDestructuringPattern(context, DestructureToParameters, ExportType::NotExported); - failIfFalse(parameter, "Cannot parse parameter pattern"); - context.appendParameter(parameterList, parameter, 0); - } - } - - return parametersStart; - } - - if (!consume(OPENPAREN)) { - semanticFailureDueToKeyword(stringForFunctionMode(mode), " name"); - failWithMessage("Expected an opening '(' before a ", stringForFunctionMode(mode), "'s parameter list"); - } - - if (mode == SourceParseMode::GetterMode) { - consumeOrFail(CLOSEPAREN, "getter functions must have no parameters"); - functionInfo.parameterCount = 0; - } else if (mode == SourceParseMode::SetterMode) { - failIfTrue(match(CLOSEPAREN), "setter functions must have one parameter"); - const Identifier* duplicateParameter = nullptr; - auto parameter = parseDestructuringPattern(context, DestructureToParameters, ExportType::NotExported, &duplicateParameter); - failIfFalse(parameter, "setter functions must have one parameter"); - auto defaultValue = parseDefaultValueForDestructuringPattern(context); - propagateError(); - semanticFailIfTrue(duplicateParameter && defaultValue, "Duplicate parameter '", duplicateParameter->impl(), "' not allowed in function with default parameter values"); - context.appendParameter(parameterList, parameter, defaultValue); - functionInfo.parameterCount = 1; - failIfTrue(match(COMMA), "setter functions must have one parameter"); - consumeOrFail(CLOSEPAREN, "Expected a ')' after a parameter declaration"); - } else { - if (match(CLOSEPAREN)) - functionInfo.parameterCount = 0; - else - failIfFalse(parseFormalParameters(context, parameterList, functionInfo.parameterCount), "Cannot parse parameters for this ", stringForFunctionMode(mode)); - consumeOrFail(CLOSEPAREN, "Expected a ')' or a ',' after a parameter declaration"); - } - - return parametersStart; -} - template <typename LexerType> -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) +template <class TreeBuilder> bool Parser<LexerType>::parseFunctionInfo(TreeBuilder& context, FunctionRequirements requirements, FunctionParseMode mode, bool nameIsInContainingScope, const Identifier*& name, TreeFormalParameterList& parameters, TreeFunctionBody& body, unsigned& openBraceOffset, unsigned& closeBraceOffset, int& bodyStartLine, unsigned& bodyStartColumn) { - RELEASE_ASSERT(isFunctionParseMode(mode)); - AutoPopScopeRef functionScope(this, pushScope()); functionScope->setIsFunction(); int functionNameStart = m_token.m_location.startOffset; const Identifier* lastFunctionName = m_lastFunctionName; m_lastFunctionName = nullptr; - int parametersStart; - JSTokenLocation startLocation; - int startColumn; - FunctionBodyType functionBodyType; - - switch (parseType) { - case StandardFunctionParseType: { - RELEASE_ASSERT(mode != SourceParseMode::ArrowFunctionMode); - if (match(IDENT) || isLETMaskedAsIDENT()) { - functionInfo.name = m_token.m_data.ident; - m_lastFunctionName = functionInfo.name; - next(); - if (!nameIsInContainingScope) - failIfTrueIfStrict(functionScope->declareVariable(functionInfo.name) & DeclarationResult::InvalidStrictMode, "'", functionInfo.name->impl(), "' is not a valid ", stringForFunctionMode(mode), " name in strict mode"); - } else if (requirements == FunctionNeedsName) { - if (match(OPENPAREN) && mode == SourceParseMode::NormalFunctionMode) - semanticFail("Function statements must have a name"); - semanticFailureDueToKeyword(stringForFunctionMode(mode), " name"); - failDueToUnexpectedToken(); - return false; - } - - startLocation = tokenLocation(); - functionInfo.startLine = tokenLine(); - startColumn = tokenColumn(); - - parametersStart = parseFunctionParameters(context, mode, functionInfo); - propagateError(); - - matchOrFail(OPENBRACE, "Expected an opening '{' at the start of a ", stringForFunctionMode(mode), " body"); - - // BytecodeGenerator emits code to throw TypeError when a class constructor is "call"ed. - // Set ConstructorKind to None for non-constructor methods of classes. - - if (m_defaultConstructorKind != ConstructorKind::None) { - constructorKind = m_defaultConstructorKind; - expectedSuperBinding = m_defaultConstructorKind == ConstructorKind::Derived ? SuperBinding::Needed : SuperBinding::NotNeeded; - } - - functionBodyType = StandardFunctionBodyBlock; - - break; - } -#if ENABLE(ES6_ARROWFUNCTION_SYNTAX) - case ArrowFunctionParseType: { - RELEASE_ASSERT(mode == SourceParseMode::ArrowFunctionMode); - - startLocation = tokenLocation(); - functionInfo.startLine = tokenLine(); - startColumn = tokenColumn(); - - parametersStart = parseFunctionParameters(context, mode, functionInfo); - propagateError(); - - matchOrFail(ARROWFUNCTION, "Expected a '=>' after arrow function parameter declaration"); - - if (m_lexer->prevTerminator()) - failDueToUnexpectedToken(); - - ASSERT(constructorKind == ConstructorKind::None); - - // Check if arrow body start with {. If it true it mean that arrow function is Fat arrow function - // and we need use common approach to parse function body + if (match(IDENT)) { + name = m_token.m_data.ident; + m_lastFunctionName = name; next(); - functionBodyType = match(OPENBRACE) ? ArrowFunctionBodyBlock : ArrowFunctionBodyExpression; - - break; + if (!nameIsInContainingScope) + failIfFalseIfStrict(functionScope->declareVariable(name), "'", name->impl(), "' is not a valid ", stringForFunctionMode(mode), " name in strict mode"); + } else if (requirements == FunctionNeedsName) { + if (match(OPENPAREN) && mode == FunctionMode) + semanticFail("Function statements must have a name"); + semanticFailureDueToKeyword(stringForFunctionMode(mode), " name"); + failDueToUnexpectedToken(); + return false; } -#else - default: - RELEASE_ASSERT_NOT_REACHED(); -#endif + if (!consume(OPENPAREN)) { + semanticFailureDueToKeyword(stringForFunctionMode(mode), " name"); + failWithMessage("Expected an opening '(' before a ", stringForFunctionMode(mode), "'s parameter list"); + } + if (!match(CLOSEPAREN)) { + parameters = parseFormalParameters(context); + failIfFalse(parameters, "Cannot parse parameters for this ", stringForFunctionMode(mode)); } + consumeOrFail(CLOSEPAREN, "Expected a ')' or a ',' after a parameter declaration"); + matchOrFail(OPENBRACE, "Expected an opening '{' at the start of a ", stringForFunctionMode(mode), " body"); - bool isClassConstructor = constructorKind != ConstructorKind::None; - - functionInfo.bodyStartColumn = startColumn; + openBraceOffset = m_token.m_data.offset; + bodyStartLine = tokenLine(); + bodyStartColumn = m_token.m_data.offset - m_token.m_data.lineStartOffset; + JSTokenLocation startLocation(tokenLocation()); // If we know about this function already, we can use the cached info and skip the parser to the end of the function. - if (const SourceProviderCacheItem* cachedInfo = TreeBuilder::CanUseFunctionCache ? findCachedFunctionInfo(functionInfo.startOffset) : 0) { + if (const SourceProviderCacheItem* cachedInfo = TreeBuilder::CanUseFunctionCache ? findCachedFunctionInfo(openBraceOffset) : 0) { // If we're in a strict context, the cached function info must say it was strict too. ASSERT(!strictMode() || cachedInfo->strictMode); JSTokenLocation endLocation; - endLocation.line = cachedInfo->lastTockenLine; - endLocation.startOffset = cachedInfo->lastTockenStartOffset; - endLocation.lineStartOffset = cachedInfo->lastTockenLineStartOffset; + endLocation.line = cachedInfo->closeBraceLine; + endLocation.startOffset = cachedInfo->closeBraceOffset; + endLocation.lineStartOffset = cachedInfo->closeBraceLineStartOffset; - bool endColumnIsOnStartLine = (endLocation.line == functionInfo.startLine); + bool endColumnIsOnStartLine = (endLocation.line == bodyStartLine); ASSERT(endLocation.startOffset >= endLocation.lineStartOffset); unsigned bodyEndColumn = endColumnIsOnStartLine ? endLocation.startOffset - m_token.m_data.lineStartOffset : endLocation.startOffset - endLocation.lineStartOffset; - unsigned currentLineStartOffset = m_token.m_location.lineStartOffset; - functionInfo.body = context.createFunctionMetadata( - startLocation, endLocation, functionInfo.bodyStartColumn, bodyEndColumn, - functionKeywordStart, functionNameStart, parametersStart, - cachedInfo->strictMode, constructorKind, cachedInfo->parameterCount, mode); + body = context.createFunctionBody(startLocation, endLocation, bodyStartColumn, bodyEndColumn, cachedInfo->strictMode); functionScope->restoreFromSourceProviderCache(cachedInfo); - popScope(functionScope, TreeBuilder::NeedsFreeVariableInfo); - - m_token = cachedInfo->endFunctionToken(); + failIfFalse(popScope(functionScope, TreeBuilder::NeedsFreeVariableInfo), "Parser error"); - if (endColumnIsOnStartLine) - m_token.m_location.lineStartOffset = currentLineStartOffset; + closeBraceOffset = cachedInfo->closeBraceOffset; + + context.setFunctionNameStart(body, functionNameStart); + m_token = cachedInfo->closeBraceToken(); m_lexer->setOffset(m_token.m_location.endOffset, m_token.m_location.lineStartOffset); m_lexer->setLineNumber(m_token.m_location.line); - functionInfo.endOffset = cachedInfo->endFunctionOffset; - - if (parseType == ArrowFunctionParseType) - functionBodyType = cachedInfo->isBodyArrowExpression ? ArrowFunctionBodyExpression : ArrowFunctionBodyBlock; - else - functionBodyType = StandardFunctionBodyBlock; - switch (functionBodyType) { - case ArrowFunctionBodyExpression: - next(); - context.setEndOffset(functionInfo.body, m_lexer->currentOffset()); - break; - case ArrowFunctionBodyBlock: - case StandardFunctionBodyBlock: - context.setEndOffset(functionInfo.body, m_lexer->currentOffset()); - next(); - break; - } - functionInfo.endLine = m_lastTokenEndPosition.line; + next(); return true; } - m_lastFunctionName = lastFunctionName; ParserState oldState = saveState(); - - functionInfo.body = parseFunctionBody(context, startLocation, startColumn, functionKeywordStart, functionNameStart, parametersStart, constructorKind, functionBodyType, functionInfo.parameterCount, mode); - + body = parseFunctionBody(context); restoreState(oldState); - failIfFalse(functionInfo.body, "Cannot parse the body of this ", stringForFunctionMode(mode)); - context.setEndOffset(functionInfo.body, m_lexer->currentOffset()); - if (functionScope->strictMode() && functionInfo.name) { - RELEASE_ASSERT(mode == SourceParseMode::NormalFunctionMode || mode == SourceParseMode::MethodMode || mode == SourceParseMode::ArrowFunctionMode); - semanticFailIfTrue(m_vm->propertyNames->arguments == *functionInfo.name, "'", functionInfo.name->impl(), "' is not a valid function name in strict mode"); - semanticFailIfTrue(m_vm->propertyNames->eval == *functionInfo.name, "'", functionInfo.name->impl(), "' is not a valid function name in strict mode"); - } - if (functionScope->hasDirectSuper()) { - semanticFailIfTrue(!isClassConstructor, "Cannot call super() outside of a class constructor"); - semanticFailIfTrue(constructorKind != ConstructorKind::Derived, "Cannot call super() in a base class constructor"); - } - if (functionScope->needsSuperBinding()) - semanticFailIfTrue(expectedSuperBinding == SuperBinding::NotNeeded, "super can only be used in a method of a derived class"); - - JSTokenLocation location = JSTokenLocation(m_token.m_location); - functionInfo.endOffset = m_token.m_data.offset; - - if (functionBodyType == ArrowFunctionBodyExpression) { - location = locationBeforeLastToken(); - functionInfo.endOffset = location.endOffset; + failIfFalse(body, "Cannot parse the body of this ", stringForFunctionMode(mode)); + if (functionScope->strictMode() && name) { + RELEASE_ASSERT(mode == FunctionMode); + semanticFailIfTrue(m_vm->propertyNames->arguments == *name, "'", name->impl(), "' is not a valid function name in strict mode"); + semanticFailIfTrue(m_vm->propertyNames->eval == *name, "'", name->impl(), "' is not a valid function name in strict mode"); } + closeBraceOffset = m_token.m_data.offset; + unsigned closeBraceLine = m_token.m_data.line; + unsigned closeBraceLineStartOffset = m_token.m_data.lineStartOffset; // Cache the tokenizer state and the function scope the first time the function is parsed. // Any future reparsing can then skip the function. static const int minimumFunctionLengthToCache = 16; std::unique_ptr<SourceProviderCacheItem> newInfo; - int functionLength = functionInfo.endOffset - functionInfo.startOffset; + int functionLength = closeBraceOffset - openBraceOffset; if (TreeBuilder::CanUseFunctionCache && m_functionCache && functionLength > minimumFunctionLengthToCache) { SourceProviderCacheItemCreationParameters parameters; - parameters.endFunctionOffset = functionInfo.endOffset; parameters.functionNameStart = functionNameStart; - parameters.lastTockenLine = location.line; - parameters.lastTockenStartOffset = location.startOffset; - parameters.lastTockenEndOffset = location.endOffset; - parameters.lastTockenLineStartOffset = location.lineStartOffset; - parameters.parameterCount = functionInfo.parameterCount; - if (functionBodyType == ArrowFunctionBodyExpression) { - parameters.isBodyArrowExpression = true; - parameters.tokenType = m_token.m_type; - } + parameters.closeBraceLine = closeBraceLine; + parameters.closeBraceOffset = closeBraceOffset; + parameters.closeBraceLineStartOffset = closeBraceLineStartOffset; functionScope->fillParametersForSourceProviderCache(parameters); newInfo = SourceProviderCacheItem::create(parameters); + } + context.setFunctionNameStart(body, functionNameStart); - popScope(functionScope, TreeBuilder::NeedsFreeVariableInfo); - - if (functionBodyType == ArrowFunctionBodyExpression) - failIfFalse(isEndOfArrowFunction(), "Expected the closing ';' ',' ']' ')' '}', line terminator or EOF after arrow function"); - else { - matchOrFail(CLOSEBRACE, "Expected a closing '}' after a ", stringForFunctionMode(mode), " body"); - next(); - } + failIfFalse(popScope(functionScope, TreeBuilder::NeedsFreeVariableInfo), "Parser error"); + matchOrFail(CLOSEBRACE, "Expected a closing '}' after a ", stringForFunctionMode(mode), " body"); if (newInfo) - m_functionCache->add(functionInfo.startOffset, WTF::move(newInfo)); + m_functionCache->add(openBraceOffset, std::move(newInfo)); - functionInfo.endLine = m_lastTokenEndPosition.line; + next(); return true; } template <typename LexerType> -template <class TreeBuilder> TreeStatement Parser<LexerType>::parseFunctionDeclaration(TreeBuilder& context, ExportType exportType) +template <class TreeBuilder> TreeStatement Parser<LexerType>::parseFunctionDeclaration(TreeBuilder& context) { ASSERT(match(FUNCTION)); JSTokenLocation location(tokenLocation()); - unsigned functionKeywordStart = tokenStart(); next(); - ParserFunctionInfo<TreeBuilder> functionInfo; - failIfFalse((parseFunctionInfo(context, FunctionNeedsName, SourceParseMode::NormalFunctionMode, true, ConstructorKind::None, SuperBinding::NotNeeded, - functionKeywordStart, functionInfo, StandardFunctionParseType)), "Cannot parse this function"); - failIfFalse(functionInfo.name, "Function statements must have a name"); - failIfTrueIfStrict(declareVariable(functionInfo.name) & DeclarationResult::InvalidStrictMode, "Cannot declare a function named '", functionInfo.name->impl(), "' in strict mode"); - if (exportType == ExportType::Exported) { - semanticFailIfFalse(exportName(*functionInfo.name), "Cannot export a duplicate function name: '", functionInfo.name->impl(), "'"); - currentScope()->moduleScopeData().exportBinding(*functionInfo.name); - } - return context.createFuncDeclStatement(location, functionInfo); -} - -#if ENABLE(ES6_CLASS_SYNTAX) -template <typename LexerType> -template <class TreeBuilder> TreeStatement Parser<LexerType>::parseClassDeclaration(TreeBuilder& context, ExportType exportType) -{ - ASSERT(match(CLASSTOKEN)); - JSTokenLocation location(tokenLocation()); - JSTextPosition classStart = tokenStartPosition(); - unsigned classStartLine = tokenLine(); - - ParserClassInfo<TreeBuilder> info; - TreeClassExpression classExpr = parseClass(context, FunctionNeedsName, info); - failIfFalse(classExpr, "Failed to parse class"); - - DeclarationResultMask declarationResult = declareVariable(info.className, DeclarationType::LetDeclaration); - if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration) - internalFailWithMessage(false, "Cannot declare a class twice: '", info.className->impl(), "'"); - if (exportType == ExportType::Exported) { - semanticFailIfFalse(exportName(*info.className), "Cannot export a duplicate class name: '", info.className->impl(), "'"); - currentScope()->moduleScopeData().exportBinding(*info.className); - } - - JSTextPosition classEnd = lastTokenEndPosition(); - unsigned classEndLine = tokenLine(); - - return context.createClassDeclStatement(location, classExpr, classStart, classEnd, classStartLine, classEndLine); + const Identifier* name = 0; + TreeFormalParameterList parameters = 0; + TreeFunctionBody body = 0; + unsigned openBraceOffset = 0; + unsigned closeBraceOffset = 0; + int bodyStartLine = 0; + unsigned bodyStartColumn = 0; + failIfFalse((parseFunctionInfo(context, FunctionNeedsName, FunctionMode, true, name, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn)), "Cannot parse this function"); + failIfFalse(name, "Function statements must have a name"); + failIfFalseIfStrict(declareVariable(name), "Cannot declare a function named '", name->impl(), "' in strict mode"); + return context.createFuncDeclStatement(location, name, body, parameters, openBraceOffset, closeBraceOffset, bodyStartLine, m_lastTokenEndPosition.line, bodyStartColumn); } -template <typename LexerType> -template <class TreeBuilder> TreeClassExpression Parser<LexerType>::parseClass(TreeBuilder& context, FunctionRequirements requirements, ParserClassInfo<TreeBuilder>& info) -{ - ASSERT(match(CLASSTOKEN)); - JSTokenLocation location(tokenLocation()); - next(); - - AutoPopScopeRef classScope(this, pushScope()); - classScope->setStrictMode(); - - const Identifier* className = nullptr; - if (match(IDENT)) { - className = m_token.m_data.ident; - info.className = className; - next(); - failIfTrue(classScope->declareVariable(className) & DeclarationResult::InvalidStrictMode, "'", className->impl(), "' is not a valid class name"); - } else if (requirements == FunctionNeedsName) { - if (match(OPENBRACE)) - semanticFail("Class statements must have a name"); - semanticFailureDueToKeyword("class name"); - failDueToUnexpectedToken(); - } else - className = &m_vm->propertyNames->nullIdentifier; - ASSERT(className); - - TreeExpression parentClass = 0; - if (consume(EXTENDS)) { - parentClass = parseMemberExpression(context); - failIfFalse(parentClass, "Cannot parse the parent class name"); - } - const ConstructorKind constructorKind = parentClass ? ConstructorKind::Derived : ConstructorKind::Base; - - consumeOrFail(OPENBRACE, "Expected opening '{' at the start of a class body"); - - TreeExpression constructor = 0; - TreePropertyList staticMethods = 0; - TreePropertyList instanceMethods = 0; - TreePropertyList instanceMethodsTail = 0; - TreePropertyList staticMethodsTail = 0; - while (!match(CLOSEBRACE)) { - if (match(SEMICOLON)) { - next(); - continue; - } - - JSTokenLocation methodLocation(tokenLocation()); - unsigned methodStart = tokenStart(); - - // For backwards compatibility, "static" is a non-reserved keyword in non-strict mode. - bool isStaticMethod = match(RESERVED_IF_STRICT) && *m_token.m_data.ident == m_vm->propertyNames->staticKeyword; - if (isStaticMethod) - next(); - - // FIXME: Figure out a way to share more code with parseProperty. - const CommonIdentifiers& propertyNames = *m_vm->propertyNames; - const Identifier* ident = nullptr; - bool isGetter = false; - bool isSetter = false; - switch (m_token.m_type) { - case STRING: - ident = m_token.m_data.ident; - ASSERT(ident); - next(); - break; - case IDENT: - ident = m_token.m_data.ident; - ASSERT(ident); - next(); - if (match(IDENT) || match(STRING) || match(DOUBLE) || match(INTEGER)) { - isGetter = *ident == propertyNames.get; - isSetter = *ident == propertyNames.set; - } - break; - case DOUBLE: - case INTEGER: - ident = &m_parserArena.identifierArena().makeNumericIdentifier(const_cast<VM*>(m_vm), m_token.m_data.doubleValue); - ASSERT(ident); - next(); - break; - default: - failDueToUnexpectedToken(); - } - - TreeProperty property; - const bool alwaysStrictInsideClass = true; - if (isGetter || isSetter) { - property = parseGetterSetter(context, alwaysStrictInsideClass, isGetter ? PropertyNode::Getter : PropertyNode::Setter, methodStart, - ConstructorKind::None, SuperBinding::Needed); - failIfFalse(property, "Cannot parse this method"); - } else { - ParserFunctionInfo<TreeBuilder> methodInfo; - bool isConstructor = !isStaticMethod && *ident == propertyNames.constructor; - failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SourceParseMode::MethodMode, false, isConstructor ? constructorKind : ConstructorKind::None, SuperBinding::Needed, methodStart, methodInfo, StandardFunctionParseType)), "Cannot parse this method"); - failIfTrue(!ident || (declareVariable(ident) & DeclarationResult::InvalidStrictMode), "Cannot declare a method named '", methodInfo.name->impl(), "'"); - methodInfo.name = isConstructor ? className : ident; - - TreeExpression method = context.createFunctionExpr(methodLocation, methodInfo); - if (isConstructor) { - semanticFailIfTrue(constructor, "Cannot declare multiple constructors in a single class"); - constructor = method; - continue; - } - - // FIXME: Syntax error when super() is called - semanticFailIfTrue(isStaticMethod && methodInfo.name && *methodInfo.name == propertyNames.prototype, - "Cannot declare a static method named 'prototype'"); - property = context.createProperty(methodInfo.name, method, PropertyNode::Constant, PropertyNode::Unknown, alwaysStrictInsideClass, SuperBinding::Needed); - } - - TreePropertyList& tail = isStaticMethod ? staticMethodsTail : instanceMethodsTail; - if (tail) - tail = context.createPropertyList(methodLocation, property, tail); - else { - tail = context.createPropertyList(methodLocation, property); - if (isStaticMethod) - staticMethods = tail; - else - instanceMethods = tail; - } - } - - popScope(classScope, TreeBuilder::NeedsFreeVariableInfo); - consumeOrFail(CLOSEBRACE, "Expected a closing '}' after a class body"); - - return context.createClassExpr(location, *className, constructor, parentClass, instanceMethods, staticMethods); -} -#endif - struct LabelInfo { LabelInfo(const Identifier* ident, const JSTextPosition& start, const JSTextPosition& end) : m_ident(ident) @@ -2081,7 +1366,7 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseExpressionOrL failIfTrue(getLabel(ident), "Cannot find scope for the label '", ident->impl(), "'"); labels.append(LabelInfo(ident, start, end)); } - } while (match(IDENT) || isLETMaskedAsIDENT()); + } while (match(IDENT)); bool isLoop = false; switch (m_token.m_type) { case FOR: @@ -2094,7 +1379,6 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseExpressionOrL break; } const Identifier* unused = 0; - ScopeRef labelScope = currentScope(); if (!m_syntaxAlreadyValidated) { for (size_t i = 0; i < labels.size(); i++) pushLabel(labels[i].m_ident, isLoop); @@ -2102,7 +1386,7 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseExpressionOrL TreeStatement statement = parseStatement(context, unused); if (!m_syntaxAlreadyValidated) { for (size_t i = 0; i < labels.size(); i++) - popLabel(labelScope); + popLabel(); } failIfFalse(statement, "Cannot parse statement"); for (size_t i = 0; i < labels.size(); i++) { @@ -2115,19 +1399,6 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseExpressionOrL template <typename LexerType> template <class TreeBuilder> TreeStatement Parser<LexerType>::parseExpressionStatement(TreeBuilder& context) { - switch (m_token.m_type) { - // Consult: http://www.ecma-international.org/ecma-262/6.0/index.html#sec-expression-statement - // The ES6 spec mandates that we should fail from FUNCTION token here. We handle this case - // in parseStatement() which is the only caller of parseExpressionStatement(). - // We actually allow FUNCTION in situations where it should not be allowed unless we're in strict mode. - case CLASSTOKEN: - failWithMessage("'class' declaration is not directly within a block statement"); - break; - default: - // FIXME: when implementing 'let' we should fail when we see the token sequence "let [". - // https://bugs.webkit.org/show_bug.cgi?id=142944 - break; - } JSTextPosition start = tokenStartPosition(); JSTokenLocation location(tokenLocation()); TreeExpression expression = parseExpression(context); @@ -2200,9 +1471,7 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseIfStatement(T posStack.removeLast(); JSTokenLocation elseLocation = tokenLocationStack.last(); tokenLocationStack.removeLast(); - TreeStatement ifStatement = context.createIfStatement(elseLocation, condition, trueBlock, 0, pos.first, pos.second); - context.setEndOffset(ifStatement, context.endOffset(trueBlock)); - statementStack.append(ifStatement); + statementStack.append(context.createIfStatement(elseLocation, condition, trueBlock, 0, pos.first, pos.second)); } while (!exprStack.isEmpty()) { @@ -2216,392 +1485,19 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseIfStatement(T posStack.removeLast(); JSTokenLocation elseLocation = tokenLocationStack.last(); tokenLocationStack.removeLast(); - TreeStatement ifStatement = context.createIfStatement(elseLocation, condition, trueBlock, falseBlock, pos.first, pos.second); - context.setEndOffset(ifStatement, context.endOffset(falseBlock)); - statementStack.append(ifStatement); + statementStack.append(context.createIfStatement(elseLocation, condition, trueBlock, falseBlock, pos.first, pos.second)); } return context.createIfStatement(ifLocation, condition, trueBlock, statementStack.last(), start, end); } template <typename LexerType> -template <class TreeBuilder> typename TreeBuilder::ModuleName Parser<LexerType>::parseModuleName(TreeBuilder& context) -{ - // ModuleName (ModuleSpecifier in the spec) represents the module name imported by the script. - // http://www.ecma-international.org/ecma-262/6.0/#sec-imports - // http://www.ecma-international.org/ecma-262/6.0/#sec-exports - JSTokenLocation specifierLocation(tokenLocation()); - failIfFalse(match(STRING), "Imported modules names must be string literals"); - const Identifier* moduleName = m_token.m_data.ident; - next(); - return context.createModuleName(specifierLocation, *moduleName); -} - -template <typename LexerType> -template <class TreeBuilder> typename TreeBuilder::ImportSpecifier Parser<LexerType>::parseImportClauseItem(TreeBuilder& context, ImportSpecifierType specifierType) -{ - // Produced node is the item of the ImportClause. - // That is the ImportSpecifier, ImportedDefaultBinding or NameSpaceImport. - // http://www.ecma-international.org/ecma-262/6.0/#sec-imports - JSTokenLocation specifierLocation(tokenLocation()); - JSToken localNameToken; - const Identifier* importedName = nullptr; - const Identifier* localName = nullptr; - - switch (specifierType) { - case ImportSpecifierType::NamespaceImport: { - // NameSpaceImport : - // * as ImportedBinding - // e.g. - // * as namespace - ASSERT(match(TIMES)); - importedName = &m_vm->propertyNames->timesIdentifier; - next(); - - failIfFalse(matchContextualKeyword(m_vm->propertyNames->as), "Expected 'as' before imported binding name"); - next(); - - matchOrFail(IDENT, "Expected a variable name for the import declaration"); - localNameToken = m_token; - localName = m_token.m_data.ident; - next(); - break; - } - - case ImportSpecifierType::NamedImport: { - // ImportSpecifier : - // ImportedBinding - // IdentifierName as ImportedBinding - // e.g. - // A - // A as B - ASSERT(matchIdentifierOrKeyword()); - localNameToken = m_token; - localName = m_token.m_data.ident; - importedName = localName; - next(); - - if (matchContextualKeyword(m_vm->propertyNames->as)) { - next(); - matchOrFail(IDENT, "Expected a variable name for the import declaration"); - localNameToken = m_token; - localName = m_token.m_data.ident; - next(); - } - break; - } - - case ImportSpecifierType::DefaultImport: { - // ImportedDefaultBinding : - // ImportedBinding - ASSERT(match(IDENT)); - localNameToken = m_token; - localName = m_token.m_data.ident; - importedName = &m_vm->propertyNames->defaultKeyword; - next(); - break; - } - } - - semanticFailIfTrue(localNameToken.m_type & KeywordTokenFlag, "Cannot use keyword as imported binding name"); - DeclarationResultMask declarationResult = declareVariable(localName, DeclarationType::ConstDeclaration, DeclarationImportType::Imported); - if (declarationResult != DeclarationResult::Valid) { - failIfTrueIfStrict(declarationResult & DeclarationResult::InvalidStrictMode, "Cannot declare an imported binding named ", localName->impl(), " in strict mode"); - if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration) - internalFailWithMessage(false, "Cannot declare an imported binding name twice: '", localName->impl(), "'"); - } - - return context.createImportSpecifier(specifierLocation, *importedName, *localName); -} - -template <typename LexerType> -template <class TreeBuilder> TreeStatement Parser<LexerType>::parseImportDeclaration(TreeBuilder& context) -{ - // http://www.ecma-international.org/ecma-262/6.0/#sec-imports - ASSERT(match(IMPORT)); - JSTokenLocation importLocation(tokenLocation()); - next(); - - auto specifierList = context.createImportSpecifierList(); - - if (match(STRING)) { - // import ModuleSpecifier ; - auto moduleName = parseModuleName(context); - failIfFalse(moduleName, "Cannot parse the module name"); - failIfFalse(autoSemiColon(), "Expected a ';' following a targeted import declaration"); - return context.createImportDeclaration(importLocation, specifierList, moduleName); - } - - bool isFinishedParsingImport = false; - if (match(IDENT)) { - // ImportedDefaultBinding : - // ImportedBinding - auto specifier = parseImportClauseItem(context, ImportSpecifierType::DefaultImport); - failIfFalse(specifier, "Cannot parse the default import"); - context.appendImportSpecifier(specifierList, specifier); - if (match(COMMA)) - next(); - else - isFinishedParsingImport = true; - } - - if (!isFinishedParsingImport) { - if (match(TIMES)) { - // import NameSpaceImport FromClause ; - auto specifier = parseImportClauseItem(context, ImportSpecifierType::NamespaceImport); - failIfFalse(specifier, "Cannot parse the namespace import"); - context.appendImportSpecifier(specifierList, specifier); - } else if (match(OPENBRACE)) { - // NamedImports : - // { } - // { ImportsList } - // { ImportsList , } - next(); - - while (!match(CLOSEBRACE)) { - failIfFalse(matchIdentifierOrKeyword(), "Expected an imported name for the import declaration"); - auto specifier = parseImportClauseItem(context, ImportSpecifierType::NamedImport); - failIfFalse(specifier, "Cannot parse the named import"); - context.appendImportSpecifier(specifierList, specifier); - if (!consume(COMMA)) - break; - } - handleProductionOrFail(CLOSEBRACE, "}", "end", "import list"); - } else - failWithMessage("Expected namespace import or import list"); - } - - // FromClause : - // from ModuleSpecifier - - failIfFalse(matchContextualKeyword(m_vm->propertyNames->from), "Expected 'from' before imported module name"); - next(); - - auto moduleName = parseModuleName(context); - failIfFalse(moduleName, "Cannot parse the module name"); - failIfFalse(autoSemiColon(), "Expected a ';' following a targeted import declaration"); - - return context.createImportDeclaration(importLocation, specifierList, moduleName); -} - -template <typename LexerType> -template <class TreeBuilder> typename TreeBuilder::ExportSpecifier Parser<LexerType>::parseExportSpecifier(TreeBuilder& context, Vector<const Identifier*>& maybeLocalNames, bool& hasKeywordForLocalBindings) -{ - // ExportSpecifier : - // IdentifierName - // IdentifierName as IdentifierName - // http://www.ecma-international.org/ecma-262/6.0/#sec-exports - ASSERT(matchIdentifierOrKeyword()); - JSTokenLocation specifierLocation(tokenLocation()); - if (m_token.m_type & KeywordTokenFlag) - hasKeywordForLocalBindings = true; - const Identifier* localName = m_token.m_data.ident; - const Identifier* exportedName = localName; - next(); - - if (matchContextualKeyword(m_vm->propertyNames->as)) { - next(); - failIfFalse(matchIdentifierOrKeyword(), "Expected an exported name for the export declaration"); - exportedName = m_token.m_data.ident; - next(); - } - - semanticFailIfFalse(exportName(*exportedName), "Cannot export a duplicate name '", exportedName->impl(), "'"); - maybeLocalNames.append(localName); - return context.createExportSpecifier(specifierLocation, *localName, *exportedName); -} - -template <typename LexerType> -template <class TreeBuilder> TreeStatement Parser<LexerType>::parseExportDeclaration(TreeBuilder& context) -{ - // http://www.ecma-international.org/ecma-262/6.0/#sec-exports - ASSERT(match(EXPORT)); - JSTokenLocation exportLocation(tokenLocation()); - next(); - - switch (m_token.m_type) { - case TIMES: { - // export * FromClause ; - next(); - - failIfFalse(matchContextualKeyword(m_vm->propertyNames->from), "Expected 'from' before exported module name"); - next(); - auto moduleName = parseModuleName(context); - failIfFalse(moduleName, "Cannot parse the 'from' clause"); - failIfFalse(autoSemiColon(), "Expected a ';' following a targeted export declaration"); - - return context.createExportAllDeclaration(exportLocation, moduleName); - } - - case DEFAULT: { - // export default HoistableDeclaration[Default] - // export default ClassDeclaration[Default] - // export default [lookahead not-in {function, class}] AssignmentExpression[In] ; - - next(); - - TreeStatement result = 0; - bool isFunctionOrClassDeclaration = false; - const Identifier* localName = nullptr; - SavePoint savePoint = createSavePoint(); - if (match(FUNCTION) -#if ENABLE(ES6_CLASS_SYNTAX) - || match(CLASSTOKEN) -#endif - ) { - isFunctionOrClassDeclaration = true; - next(); - // FIXME: When landing ES6 generators, we need to take care of that '*' comes. - if (match(IDENT)) - localName = m_token.m_data.ident; - restoreSavePoint(savePoint); - } - - if (localName) { - if (match(FUNCTION)) - result = parseFunctionDeclaration(context); -#if ENABLE(ES6_CLASS_SYNTAX) - else { - ASSERT(match(CLASSTOKEN)); - result = parseClassDeclaration(context); - } -#endif - } else { - // export default expr; - // - // It should be treated as the same to the following. - // - // const *default* = expr; - // export { *default* as default } - // - // In the above example, *default* is the invisible variable to the users. - // We use the private symbol to represent the name of this variable. - JSTokenLocation location(tokenLocation()); - JSTextPosition start = tokenStartPosition(); - TreeExpression expression = parseAssignmentExpression(context); - failIfFalse(expression, "Cannot parse expression"); - - DeclarationResultMask declarationResult = declareVariable(&m_vm->propertyNames->starDefaultPrivateName, DeclarationType::ConstDeclaration); - if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration) - internalFailWithMessage(false, "Only one 'default' export is allowed"); - - TreeExpression assignment = context.createAssignResolve(location, m_vm->propertyNames->starDefaultPrivateName, expression, start, start, tokenEndPosition(), AssignmentContext::ConstDeclarationStatement); - result = context.createExprStatement(location, assignment, start, tokenEndPosition()); - if (!isFunctionOrClassDeclaration) - failIfFalse(autoSemiColon(), "Expected a ';' following a targeted export declaration"); - localName = &m_vm->propertyNames->starDefaultPrivateName; - } - failIfFalse(result, "Cannot parse the declaration"); - - semanticFailIfFalse(exportName(m_vm->propertyNames->defaultKeyword), "Only one 'default' export is allowed"); - currentScope()->moduleScopeData().exportBinding(*localName); - return context.createExportDefaultDeclaration(exportLocation, result, *localName); - } - - case OPENBRACE: { - // export ExportClause FromClause ; - // export ExportClause ; - // - // ExportClause : - // { } - // { ExportsList } - // { ExportsList , } - // - // ExportsList : - // ExportSpecifier - // ExportsList , ExportSpecifier - - next(); - - auto specifierList = context.createExportSpecifierList(); - Vector<const Identifier*> maybeLocalNames; - - bool hasKeywordForLocalBindings = false; - while (!match(CLOSEBRACE)) { - failIfFalse(matchIdentifierOrKeyword(), "Expected a variable name for the export declaration"); - auto specifier = parseExportSpecifier(context, maybeLocalNames, hasKeywordForLocalBindings); - failIfFalse(specifier, "Cannot parse the named export"); - context.appendExportSpecifier(specifierList, specifier); - if (!consume(COMMA)) - break; - } - handleProductionOrFail(CLOSEBRACE, "}", "end", "export list"); - - typename TreeBuilder::ModuleName moduleName = 0; - if (matchContextualKeyword(m_vm->propertyNames->from)) { - next(); - moduleName = parseModuleName(context); - failIfFalse(moduleName, "Cannot parse the 'from' clause"); - } - failIfFalse(autoSemiColon(), "Expected a ';' following a targeted export declaration"); - - if (!moduleName) { - semanticFailIfTrue(hasKeywordForLocalBindings, "Cannot use keyword as exported variable name"); - // Since this export declaration does not have module specifier part, it exports the local bindings. - // While the export declaration with module specifier does not have any effect on the current module's scope, - // the export named declaration without module specifier references the the local binding names. - // For example, - // export { A, B, C as D } from "mod" - // does not have effect on the current module's scope. But, - // export { A, B, C as D } - // will reference the current module's bindings. - for (const Identifier* localName : maybeLocalNames) { - currentScope()->useVariable(localName, m_vm->propertyNames->eval == *localName); - currentScope()->moduleScopeData().exportBinding(*localName); - } - } - - return context.createExportNamedDeclaration(exportLocation, specifierList, moduleName); - } - - default: { - // export VariableStatement - // export Declaration - TreeStatement result = 0; - switch (m_token.m_type) { - case VAR: - result = parseVariableDeclaration(context, DeclarationType::VarDeclaration, ExportType::Exported); - break; - - case CONSTTOKEN: - result = parseVariableDeclaration(context, DeclarationType::ConstDeclaration, ExportType::Exported); - break; - - case LET: - result = parseVariableDeclaration(context, DeclarationType::LetDeclaration, ExportType::Exported); - break; - - case FUNCTION: - result = parseFunctionDeclaration(context, ExportType::Exported); - break; - -#if ENABLE(ES6_CLASS_SYNTAX) - case CLASSTOKEN: - result = parseClassDeclaration(context, ExportType::Exported); - break; -#endif - - default: - failWithMessage("Expected either a declaration or a variable statement"); - break; - } - failIfFalse(result, "Cannot parse the declaration"); - return context.createExportLocalDeclaration(exportLocation, result); - } - } - - RELEASE_ASSERT_NOT_REACHED(); - return 0; -} - -template <typename LexerType> template <class TreeBuilder> TreeExpression Parser<LexerType>::parseExpression(TreeBuilder& context) { failIfStackOverflow(); JSTokenLocation location(tokenLocation()); TreeExpression node = parseAssignmentExpression(context); failIfFalse(node, "Cannot parse expression"); - context.setEndOffset(node, m_lastTokenEndPosition.offset); if (!match(COMMA)) return node; next(); @@ -2609,21 +1505,16 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parseExpression(T m_nonLHSCount++; TreeExpression right = parseAssignmentExpression(context); failIfFalse(right, "Cannot parse expression in a comma expression"); - context.setEndOffset(right, m_lastTokenEndPosition.offset); - typename TreeBuilder::Comma head = context.createCommaExpr(location, node); - typename TreeBuilder::Comma tail = context.appendToCommaExpr(location, head, head, right); + typename TreeBuilder::Comma commaNode = context.createCommaExpr(location, node, right); while (match(COMMA)) { next(TreeBuilder::DontBuildStrings); right = parseAssignmentExpression(context); failIfFalse(right, "Cannot parse expression in a comma expression"); - context.setEndOffset(right, m_lastTokenEndPosition.offset); - tail = context.appendToCommaExpr(location, head, tail, right); + context.appendToComma(commaNode, right); } - context.setEndOffset(head, m_lastTokenEndPosition.offset); - return head; + return commaNode; } - template <typename LexerType> template <typename TreeBuilder> TreeExpression Parser<LexerType>::parseAssignmentExpression(TreeBuilder& context) { @@ -2634,20 +1525,14 @@ template <typename TreeBuilder> TreeExpression Parser<LexerType>::parseAssignmen int initialNonLHSCount = m_nonLHSCount; if (match(OPENBRACE) || match(OPENBRACKET)) { SavePoint savePoint = createSavePoint(); - auto pattern = tryParseDestructuringPatternExpression(context, AssignmentContext::AssignmentExpression); + auto pattern = tryParseDeconstructionPatternExpression(context); if (pattern && consume(EQUAL)) { auto rhs = parseAssignmentExpression(context); if (rhs) - return context.createDestructuringAssignment(location, pattern, rhs); + return context.createDeconstructingAssignment(location, pattern, rhs); } restoreSavePoint(savePoint); } - -#if ENABLE(ES6_ARROWFUNCTION_SYNTAX) - if (isArrowFunctionParamters()) - return parseArrowFunctionExpression(context); -#endif - TreeExpression lhs = parseConditionalExpression(context); failIfFalse(lhs, "Cannot parse expression"); if (initialNonLHSCount != m_nonLHSCount) { @@ -2723,12 +1608,10 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parseConditionalE next(TreeBuilder::DontBuildStrings); TreeExpression lhs = parseAssignmentExpression(context); failIfFalse(lhs, "Cannot parse left hand side of ternary operator"); - context.setEndOffset(lhs, m_lastTokenEndPosition.offset); consumeOrFailWithFlags(COLON, TreeBuilder::DontBuildStrings, "Expected ':' in ternary operator"); TreeExpression rhs = parseAssignmentExpression(context); failIfFalse(rhs, "Cannot parse right hand side of ternary operator"); - context.setEndOffset(rhs, m_lastTokenEndPosition.offset); return context.createConditionalExpr(location, cond, lhs, rhs); } @@ -2748,6 +1631,7 @@ int Parser<LexerType>::isBinaryOperator(JSTokenType token) template <typename LexerType> template <class TreeBuilder> TreeExpression Parser<LexerType>::parseBinaryExpression(TreeBuilder& context) { + int operandStackDepth = 0; int operatorStackDepth = 0; typename TreeBuilder::BinaryExprContext binaryExprContext(context); @@ -2801,36 +1685,25 @@ template <class TreeBuilder> TreeProperty Parser<LexerType>::parseProperty(TreeB FALLTHROUGH; case STRING: { const Identifier* ident = m_token.m_data.ident; - unsigned getterOrSetterStartOffset = tokenStart(); if (complete || (wasIdent && (*ident == m_vm->propertyNames->get || *ident == m_vm->propertyNames->set))) nextExpectIdentifier(LexerFlagsIgnoreReservedWords); else nextExpectIdentifier(LexerFlagsIgnoreReservedWords | TreeBuilder::DontBuildKeywords); - + if (match(COLON)) { next(); TreeExpression node = parseAssignmentExpression(context); failIfFalse(node, "Cannot parse expression for property declaration"); - context.setEndOffset(node, m_lexer->currentOffset()); - return context.createProperty(ident, node, PropertyNode::Constant, PropertyNode::Unknown, complete); + return context.createProperty(ident, node, PropertyNode::Constant, complete); } - - if (match(OPENPAREN)) { - auto method = parsePropertyMethod(context, ident); - propagateError(); - return context.createProperty(ident, method, PropertyNode::Constant, PropertyNode::KnownDirect, complete); - } - failIfFalse(wasIdent, "Expected an identifier as property name"); - - if (match(COMMA) || match(CLOSEBRACE)) { - JSTextPosition start = tokenStartPosition(); - JSTokenLocation location(tokenLocation()); - currentScope()->useVariable(ident, m_vm->propertyNames->eval == *ident); - TreeExpression node = context.createResolve(location, ident, start); - return context.createProperty(ident, node, static_cast<PropertyNode::Type>(PropertyNode::Constant | PropertyNode::Shorthand), PropertyNode::KnownDirect, complete); - } - + const Identifier* accessorName = 0; + TreeFormalParameterList parameters = 0; + TreeFunctionBody body = 0; + unsigned openBraceOffset = 0; + unsigned closeBraceOffset = 0; + int bodyStartLine = 0; + unsigned bodyStartColumn = 0; PropertyNode::Type type; if (*ident == m_vm->propertyNames->get) type = PropertyNode::Getter; @@ -2838,43 +1711,45 @@ template <class TreeBuilder> TreeProperty Parser<LexerType>::parseProperty(TreeB type = PropertyNode::Setter; else failWithMessage("Expected a ':' following the property name '", ident->impl(), "'"); - return parseGetterSetter(context, complete, type, getterOrSetterStartOffset); + const Identifier* stringPropertyName = 0; + double numericPropertyName = 0; + if (m_token.m_type == IDENT || m_token.m_type == STRING) + stringPropertyName = m_token.m_data.ident; + else if (m_token.m_type == NUMBER) + numericPropertyName = m_token.m_data.doubleValue; + else + failDueToUnexpectedToken(); + JSTokenLocation location(tokenLocation()); + next(); + if (type == PropertyNode::Getter) { + failIfFalse(match(OPENPAREN), "Expected a parameter list for getter definition"); + failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, GetterMode, false, accessorName, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn)), "Cannot parse getter definition"); + } else { + failIfFalse(match(OPENPAREN), "Expected a parameter list for setter definition"); + failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SetterMode, false, accessorName, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn)), "Cannot parse setter definition"); + } + if (stringPropertyName) + return context.createGetterOrSetterProperty(location, type, complete, stringPropertyName, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, m_lastTokenEndPosition.line, bodyStartColumn); + return context.createGetterOrSetterProperty(const_cast<VM*>(m_vm), location, type, complete, numericPropertyName, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, m_lastTokenEndPosition.line, bodyStartColumn); } - case DOUBLE: - case INTEGER: { + case NUMBER: { double propertyName = m_token.m_data.doubleValue; next(); - - if (match(OPENPAREN)) { - const Identifier& ident = m_parserArena.identifierArena().makeNumericIdentifier(const_cast<VM*>(m_vm), propertyName); - auto method = parsePropertyMethod(context, &ident); - propagateError(); - return context.createProperty(&ident, method, PropertyNode::Constant, PropertyNode::Unknown, complete); - } - consumeOrFail(COLON, "Expected ':' after property name"); TreeExpression node = parseAssignmentExpression(context); failIfFalse(node, "Cannot parse expression for property declaration"); - context.setEndOffset(node, m_lexer->currentOffset()); - return context.createProperty(const_cast<VM*>(m_vm), m_parserArena, propertyName, node, PropertyNode::Constant, PropertyNode::Unknown, complete); + return context.createProperty(const_cast<VM*>(m_vm), propertyName, node, PropertyNode::Constant, complete); } case OPENBRACKET: { next(); - auto propertyName = parseAssignmentExpression(context); + auto propertyName = parseExpression(context); failIfFalse(propertyName, "Cannot parse computed property name"); + handleProductionOrFail(CLOSEBRACKET, "]", "end", "computed property name"); - - if (match(OPENPAREN)) { - auto method = parsePropertyMethod(context, &m_vm->propertyNames->nullIdentifier); - propagateError(); - return context.createProperty(propertyName, method, static_cast<PropertyNode::Type>(PropertyNode::Constant | PropertyNode::Computed), PropertyNode::KnownDirect, complete); - } - consumeOrFail(COLON, "Expected ':' after property name"); TreeExpression node = parseAssignmentExpression(context); failIfFalse(node, "Cannot parse expression for property declaration"); - context.setEndOffset(node, m_lexer->currentOffset()); - return context.createProperty(propertyName, node, static_cast<PropertyNode::Type>(PropertyNode::Constant | PropertyNode::Computed), PropertyNode::Unknown, complete); + return context.createProperty(const_cast<VM*>(m_vm), propertyName, node, PropertyNode::Constant, complete); } default: failIfFalse(m_token.m_type & KeywordTokenFlag, "Expected a property name"); @@ -2883,71 +1758,14 @@ template <class TreeBuilder> TreeProperty Parser<LexerType>::parseProperty(TreeB } template <typename LexerType> -template <class TreeBuilder> TreeExpression Parser<LexerType>::parsePropertyMethod(TreeBuilder& context, const Identifier* methodName) -{ - JSTokenLocation methodLocation(tokenLocation()); - unsigned methodStart = tokenStart(); - ParserFunctionInfo<TreeBuilder> methodInfo; - failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SourceParseMode::MethodMode, false, ConstructorKind::None, SuperBinding::NotNeeded, methodStart, methodInfo, StandardFunctionParseType)), "Cannot parse this method"); - methodInfo.name = methodName; - return context.createFunctionExpr(methodLocation, methodInfo); -} - -template <typename LexerType> -template <class TreeBuilder> TreeProperty Parser<LexerType>::parseGetterSetter(TreeBuilder& context, bool strict, PropertyNode::Type type, unsigned getterOrSetterStartOffset, - ConstructorKind constructorKind, SuperBinding superBinding) -{ - const Identifier* stringPropertyName = 0; - double numericPropertyName = 0; - if (m_token.m_type == IDENT || m_token.m_type == STRING || isLETMaskedAsIDENT()) { - stringPropertyName = m_token.m_data.ident; - semanticFailIfTrue(superBinding == SuperBinding::Needed && *stringPropertyName == m_vm->propertyNames->prototype, - "Cannot declare a static method named 'prototype'"); - semanticFailIfTrue(superBinding == SuperBinding::Needed && *stringPropertyName == m_vm->propertyNames->constructor, - "Cannot declare a getter or setter named 'constructor'"); - } else if (m_token.m_type == DOUBLE || m_token.m_type == INTEGER) - numericPropertyName = m_token.m_data.doubleValue; - else - failDueToUnexpectedToken(); - JSTokenLocation location(tokenLocation()); - next(); - ParserFunctionInfo<TreeBuilder> info; - if (type & PropertyNode::Getter) { - failIfFalse(match(OPENPAREN), "Expected a parameter list for getter definition"); - failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SourceParseMode::GetterMode, false, constructorKind, superBinding, - getterOrSetterStartOffset, info, StandardFunctionParseType)), "Cannot parse getter definition"); - } else { - failIfFalse(match(OPENPAREN), "Expected a parameter list for setter definition"); - failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SourceParseMode::SetterMode, false, constructorKind, superBinding, - getterOrSetterStartOffset, info, StandardFunctionParseType)), "Cannot parse setter definition"); - } - if (stringPropertyName) - return context.createGetterOrSetterProperty(location, type, strict, stringPropertyName, info, superBinding); - return context.createGetterOrSetterProperty(const_cast<VM*>(m_vm), m_parserArena, location, type, strict, numericPropertyName, info, superBinding); -} - -template <typename LexerType> -template <class TreeBuilder> bool Parser<LexerType>::shouldCheckPropertyForUnderscoreProtoDuplicate(TreeBuilder& context, const TreeProperty& property) -{ - if (m_syntaxAlreadyValidated) - return false; - - if (!context.getName(property)) - return false; - - // A Constant property that is not a Computed or Shorthand Constant property. - return context.getType(property) == PropertyNode::Constant; -} - -template <typename LexerType> template <class TreeBuilder> TreeExpression Parser<LexerType>::parseObjectLiteral(TreeBuilder& context) { auto savePoint = createSavePoint(); consumeOrFailWithFlags(OPENBRACE, TreeBuilder::DontBuildStrings, "Expected opening '{' at the start of an object literal"); + JSTokenLocation location(tokenLocation()); int oldNonLHSCount = m_nonLHSCount; - - JSTokenLocation location(tokenLocation()); + if (match(CLOSEBRACE)) { next(); return context.createObjectLiteral(location); @@ -2955,35 +1773,24 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parseObjectLitera TreeProperty property = parseProperty(context, false); failIfFalse(property, "Cannot parse object literal property"); - - if (!m_syntaxAlreadyValidated && context.getType(property) & (PropertyNode::Getter | PropertyNode::Setter)) { + if (!m_syntaxAlreadyValidated && context.getType(property) != PropertyNode::Constant) { restoreSavePoint(savePoint); return parseStrictObjectLiteral(context); } - - bool seenUnderscoreProto = false; - if (shouldCheckPropertyForUnderscoreProtoDuplicate(context, property)) - seenUnderscoreProto = *context.getName(property) == m_vm->propertyNames->underscoreProto; - TreePropertyList propertyList = context.createPropertyList(location, property); TreePropertyList tail = propertyList; while (match(COMMA)) { next(TreeBuilder::DontBuildStrings); + // allow extra comma, see http://bugs.webkit.org/show_bug.cgi?id=5939 if (match(CLOSEBRACE)) break; JSTokenLocation propertyLocation(tokenLocation()); property = parseProperty(context, false); failIfFalse(property, "Cannot parse object literal property"); - if (!m_syntaxAlreadyValidated && context.getType(property) & (PropertyNode::Getter | PropertyNode::Setter)) { + if (!m_syntaxAlreadyValidated && context.getType(property) != PropertyNode::Constant) { restoreSavePoint(savePoint); return parseStrictObjectLiteral(context); } - if (shouldCheckPropertyForUnderscoreProtoDuplicate(context, property)) { - if (*context.getName(property) == m_vm->propertyNames->underscoreProto) { - semanticFailIfTrue(seenUnderscoreProto, "Attempted to redefine __proto__ property"); - seenUnderscoreProto = true; - } - } tail = context.createPropertyList(propertyLocation, property, tail); } @@ -3010,24 +1817,30 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parseStrictObject TreeProperty property = parseProperty(context, true); failIfFalse(property, "Cannot parse object literal property"); - - bool seenUnderscoreProto = false; - if (shouldCheckPropertyForUnderscoreProtoDuplicate(context, property)) - seenUnderscoreProto = *context.getName(property) == m_vm->propertyNames->underscoreProto; - + + typedef HashMap<RefPtr<StringImpl>, unsigned, IdentifierRepHash> ObjectValidationMap; + ObjectValidationMap objectValidator; + // Add the first property + if (!m_syntaxAlreadyValidated && context.getName(property)) + objectValidator.add(context.getName(property)->impl(), context.getType(property)); + TreePropertyList propertyList = context.createPropertyList(location, property); TreePropertyList tail = propertyList; while (match(COMMA)) { next(); + // allow extra comma, see http://bugs.webkit.org/show_bug.cgi?id=5939 if (match(CLOSEBRACE)) break; JSTokenLocation propertyLocation(tokenLocation()); property = parseProperty(context, true); failIfFalse(property, "Cannot parse object literal property"); - if (shouldCheckPropertyForUnderscoreProtoDuplicate(context, property)) { - if (*context.getName(property) == m_vm->propertyNames->underscoreProto) { - semanticFailIfTrue(seenUnderscoreProto, "Attempted to redefine __proto__ property"); - seenUnderscoreProto = true; + if (!m_syntaxAlreadyValidated && context.getName(property)) { + ObjectValidationMap::AddResult propertyEntry = objectValidator.add(context.getName(property)->impl(), context.getType(property)); + if (!propertyEntry.isNewEntry) { + semanticFailIfTrue(propertyEntry.iterator->value == PropertyNode::Constant, "Attempted to redefine property '", propertyEntry.iterator->key.get(), "'"); + semanticFailIfTrue(context.getType(property) == PropertyNode::Constant, "Attempted to redefine property '", propertyEntry.iterator->key.get(), "'"); + semanticFailIfTrue(context.getType(property) & propertyEntry.iterator->value, "Attempted to redefine property '", propertyEntry.iterator->key.get(), "'"); + propertyEntry.iterator->value |= context.getType(property); } } tail = context.createPropertyList(propertyLocation, property, tail); @@ -3115,86 +1928,11 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parseArrayLiteral return context.createArray(location, elementList); } -#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) -template <typename LexerType> -template <class TreeBuilder> typename TreeBuilder::TemplateString Parser<LexerType>::parseTemplateString(TreeBuilder& context, bool isTemplateHead, typename LexerType::RawStringsBuildMode rawStringsBuildMode, bool& elementIsTail) -{ - if (!isTemplateHead) { - matchOrFail(CLOSEBRACE, "Expected a closing '}' following an expression in template literal"); - // Re-scan the token to recognize it as Template Element. - m_token.m_type = m_lexer->scanTrailingTemplateString(&m_token, rawStringsBuildMode); - } - matchOrFail(TEMPLATE, "Expected an template element"); - const Identifier* cooked = m_token.m_data.cooked; - const Identifier* raw = m_token.m_data.raw; - elementIsTail = m_token.m_data.isTail; - JSTokenLocation location(tokenLocation()); - next(); - return context.createTemplateString(location, *cooked, *raw); -} - -template <typename LexerType> -template <class TreeBuilder> typename TreeBuilder::TemplateLiteral Parser<LexerType>::parseTemplateLiteral(TreeBuilder& context, typename LexerType::RawStringsBuildMode rawStringsBuildMode) -{ - JSTokenLocation location(tokenLocation()); - bool elementIsTail = false; - - auto headTemplateString = parseTemplateString(context, true, rawStringsBuildMode, elementIsTail); - failIfFalse(headTemplateString, "Cannot parse head template element"); - - typename TreeBuilder::TemplateStringList templateStringList = context.createTemplateStringList(headTemplateString); - typename TreeBuilder::TemplateStringList templateStringTail = templateStringList; - - if (elementIsTail) - return context.createTemplateLiteral(location, templateStringList); - - failIfTrue(match(CLOSEBRACE), "Template literal expression cannot be empty"); - TreeExpression expression = parseExpression(context); - failIfFalse(expression, "Cannot parse expression in template literal"); - - typename TreeBuilder::TemplateExpressionList templateExpressionList = context.createTemplateExpressionList(expression); - typename TreeBuilder::TemplateExpressionList templateExpressionTail = templateExpressionList; - - auto templateString = parseTemplateString(context, false, rawStringsBuildMode, elementIsTail); - failIfFalse(templateString, "Cannot parse template element"); - templateStringTail = context.createTemplateStringList(templateStringTail, templateString); - - while (!elementIsTail) { - failIfTrue(match(CLOSEBRACE), "Template literal expression cannot be empty"); - TreeExpression expression = parseExpression(context); - failIfFalse(expression, "Cannot parse expression in template literal"); - - templateExpressionTail = context.createTemplateExpressionList(templateExpressionTail, expression); - - auto templateString = parseTemplateString(context, false, rawStringsBuildMode, elementIsTail); - failIfFalse(templateString, "Cannot parse template element"); - templateStringTail = context.createTemplateStringList(templateStringTail, templateString); - } - - return context.createTemplateLiteral(location, templateStringList, templateExpressionList); -} -#endif - template <typename LexerType> template <class TreeBuilder> TreeExpression Parser<LexerType>::parsePrimaryExpression(TreeBuilder& context) { failIfStackOverflow(); switch (m_token.m_type) { - case FUNCTION: { - JSTokenLocation location(tokenLocation()); - unsigned functionKeywordStart = tokenStart(); - next(); - ParserFunctionInfo<TreeBuilder> info; - info.name = &m_vm->propertyNames->nullIdentifier; - failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SourceParseMode::NormalFunctionMode, false, ConstructorKind::None, SuperBinding::NotNeeded, functionKeywordStart, info, StandardFunctionParseType)), "Cannot parse function expression"); - return context.createFunctionExpr(location, info); - } -#if ENABLE(ES6_CLASS_SYNTAX) - case CLASSTOKEN: { - ParserClassInfo<TreeBuilder> info; - return parseClass(context, FunctionNoRequirements, info); - } -#endif case OPENBRACE: if (strictMode()) return parseStrictObjectLiteral(context); @@ -3212,10 +1950,9 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parsePrimaryExpre case THISTOKEN: { JSTokenLocation location(tokenLocation()); next(); - return context.createThisExpr(location, m_thisTDZMode); + return context.thisExpr(location); } case IDENT: { - identifierExpression: JSTextPosition start = tokenStartPosition(); const Identifier* ident = m_token.m_data.ident; JSTokenLocation location(tokenLocation()); @@ -3230,17 +1967,11 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parsePrimaryExpre next(); return context.createString(location, ident); } - case DOUBLE: { - double d = m_token.m_data.doubleValue; - JSTokenLocation location(tokenLocation()); - next(); - return context.createDoubleExpr(location, d); - } - case INTEGER: { + case NUMBER: { double d = m_token.m_data.doubleValue; JSTokenLocation location(tokenLocation()); next(); - return context.createIntegerExpr(location, d); + return context.createNumberExpr(location, d); } case NULLTOKEN: { JSTokenLocation location(tokenLocation()); @@ -3277,14 +2008,6 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parsePrimaryExpre } return re; } -#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) - case TEMPLATE: - return parseTemplateLiteral(context, LexerType::RawStringsBuildMode::DontBuildRawStrings); -#endif - case LET: - if (!strictMode()) - goto identifierExpression; - FALLTHROUGH; default: failDueToUnexpectedToken(); } @@ -3340,45 +2063,27 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parseMemberExpres TreeExpression base = 0; JSTextPosition expressionStart = tokenStartPosition(); int newCount = 0; - JSTokenLocation startLocation = tokenLocation(); JSTokenLocation location; while (match(NEW)) { next(); newCount++; } - -#if ENABLE(ES6_CLASS_SYNTAX) - bool baseIsSuper = match(SUPER); - semanticFailIfTrue(baseIsSuper && newCount, "Cannot use new with super"); -#else - bool baseIsSuper = false; -#endif - - bool baseIsNewTarget = false; - if (newCount && match(DOT)) { - next(); - if (match(IDENT)) { - const Identifier* ident = m_token.m_data.ident; - if (m_vm->propertyNames->target == *ident) { - semanticFailIfFalse(currentScope()->isFunction(), "new.target is only valid inside functions"); - baseIsNewTarget = true; - base = context.createNewTargetExpr(location); - newCount--; - next(); - } else - failWithMessage("\"new.\" can only followed with target"); - } else - failDueToUnexpectedToken(); - } - - if (baseIsSuper) { - semanticFailIfFalse(currentScope()->isFunction(), "super is only valid inside functions"); - base = context.createSuperExpr(location); + + if (match(FUNCTION)) { + const Identifier* name = &m_vm->propertyNames->nullIdentifier; + TreeFormalParameterList parameters = 0; + TreeFunctionBody body = 0; + unsigned openBraceOffset = 0; + unsigned closeBraceOffset = 0; + int bodyStartLine = 0; + unsigned bodyStartColumn = 0; + location = tokenLocation(); next(); - currentScope()->setNeedsSuperBinding(); - } else if (!baseIsNewTarget) + failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, FunctionMode, false, name, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn)), "Cannot parse function expression"); + base = context.createFunctionExpr(location, name, body, parameters, openBraceOffset, closeBraceOffset, bodyStartLine, m_lastTokenEndPosition.line, bodyStartColumn); + } else base = parsePrimaryExpression(context); - + failIfFalse(base, "Cannot parse base expression"); while (true) { location = tokenLocation(); @@ -3402,16 +2107,14 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parseMemberExpres if (newCount) { newCount--; JSTextPosition expressionEnd = lastTokenEndPosition(); - TreeArguments arguments = parseArguments(context, AllowSpread); + TreeArguments arguments = parseArguments(context, DontAllowSpread); failIfFalse(arguments, "Cannot parse call arguments"); base = context.createNewExpr(location, base, arguments, expressionStart, expressionEnd, lastTokenEndPosition()); } else { JSTextPosition expressionEnd = lastTokenEndPosition(); TreeArguments arguments = parseArguments(context, AllowSpread); failIfFalse(arguments, "Cannot parse call arguments"); - if (baseIsSuper) - currentScope()->setHasDirectSuper(); - base = context.makeFunctionCallNode(startLocation, base, arguments, expressionStart, expressionEnd, lastTokenEndPosition()); + base = context.makeFunctionCallNode(location, base, arguments, expressionStart, expressionEnd, lastTokenEndPosition()); } m_nonLHSCount = nonLHSCount; break; @@ -3425,44 +2128,16 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parseMemberExpres next(); break; } -#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) - case TEMPLATE: { - semanticFailIfTrue(baseIsSuper, "Cannot use super as tag for tagged templates"); - JSTextPosition expressionEnd = lastTokenEndPosition(); - int nonLHSCount = m_nonLHSCount; - typename TreeBuilder::TemplateLiteral templateLiteral = parseTemplateLiteral(context, LexerType::RawStringsBuildMode::BuildRawStrings); - failIfFalse(templateLiteral, "Cannot parse template literal"); - base = context.createTaggedTemplate(location, base, templateLiteral, expressionStart, expressionEnd, lastTokenEndPosition()); - m_nonLHSCount = nonLHSCount; - break; - } -#endif default: goto endMemberExpression; } - baseIsSuper = false; } endMemberExpression: - semanticFailIfTrue(baseIsSuper, "Cannot reference super"); while (newCount--) base = context.createNewExpr(location, base, expressionStart, lastTokenEndPosition()); return base; } -template <typename LexerType> -template <class TreeBuilder> TreeExpression Parser<LexerType>::parseArrowFunctionExpression(TreeBuilder& context) -{ - JSTokenLocation location; - - unsigned functionKeywordStart = tokenStart(); - location = tokenLocation(); - ParserFunctionInfo<TreeBuilder> info; - info.name = &m_vm->propertyNames->nullIdentifier; - failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SourceParseMode::ArrowFunctionMode, true, ConstructorKind::None, SuperBinding::NotNeeded, functionKeywordStart, info, ArrowFunctionParseType)), "Cannot parse arrow function expression"); - - return context.createArrowFunctionExpr(location, info); -} - static const char* operatorString(bool prefix, unsigned tok) { switch (tok) { @@ -3660,8 +2335,7 @@ template <typename LexerType> void Parser<LexerType>::printUnexpectedTokenText(W case STRING: out.print("Unexpected string literal ", getToken()); return; - case INTEGER: - case DOUBLE: + case NUMBER: out.print("Unexpected number '", getToken(), "'"); return; @@ -3672,10 +2346,6 @@ template <typename LexerType> void Parser<LexerType>::printUnexpectedTokenText(W case RESERVED: out.print("Unexpected use of reserved word '", getToken(), "'"); return; - - case INVALID_PRIVATE_NAME_ERRORTOK: - out.print("Invalid private name '", getToken(), "'"); - return; case IDENT: out.print("Unexpected identifier '", getToken(), "'"); |
