/*
 *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
 *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
 *  Copyright (C) 2003-2021 Apple Inc. All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"
#include "Parser.h"

#include "ASTBuilder.h"
#include "CodeBlock.h"
#include "Debugger.h"
#include "JSGlobalData.h"
#include "JSValueInlines.h"
#include "Lexer.h"
#include "Operations.h"
#include "SourceProvider.h"
#include <type_traits>
#include <utility>
#include <wtf/HashFunctions.h>
#include <wtf/OwnPtr.h>
//#include <wtf/Scope.h>
#include <wtf/SetForScope.h>
#include <wtf/StringPrintStream.h>
#include <wtf/WTFThreadData.h>

#ifdef IMPORT
#undef IMPORT
#endif

namespace WTF {

void ALWAYS_INLINE printInternal(PrintStream& out, const JSC::Identifier* identifier)
{
	printInternal(out, identifier->impl());
}

} // namespace WTF

#define updateErrorMessage(shouldPrintToken, ...) do {\
    propagateError(); \
    logError(shouldPrintToken, __VA_ARGS__); \
} while (0)

#define propagateError() do { if (UNLIKELY(hasError())) return 0; } while (0)
#define internalFailWithMessage(shouldPrintToken, ...) do { updateErrorMessage(shouldPrintToken, __VA_ARGS__); return 0; } while (0)
#define handleErrorToken() do { if (m_token.m_type == EOFTOK || m_token.m_type & CanBeErrorTokenFlag) { failDueToUnexpectedToken(); } } while (0)
#define failWithMessage(...) do { { handleErrorToken(); updateErrorMessage(true, __VA_ARGS__); } return 0; } while (0)
#define failWithStackOverflow() do { updateErrorMessage(false, ASCIILiteral("Stack exhausted")); m_hasStackOverflow = true; return 0; } while (0)
#define failIfFalse(cond, ...) do { if (UNLIKELY(!(cond))) { handleErrorToken(); internalFailWithMessage(true, __VA_ARGS__); } } while (0)
#define failIfTrue(cond, ...) do { if (UNLIKELY(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 (UNLIKELY(!consume(tokenType))) { handleErrorToken(); internalFailWithMessage(true, __VA_ARGS__); } } while (0)
#define consumeOrFailWithFlags(tokenType, flags, ...) do { if (UNLIKELY(!consume(tokenType, flags))) { handleErrorToken(); internalFailWithMessage(true, __VA_ARGS__); } } while (0)
#define matchOrFail(tokenType, ...) do { if (UNLIKELY(!match(tokenType))) { handleErrorToken(); internalFailWithMessage(true, __VA_ARGS__); } } while (0)
#define failIfStackOverflow() do { if (UNLIKELY(!canRecurse())) failWithStackOverflow(); } while (0)
#define semanticFail(...) do { internalFailWithMessage(false, __VA_ARGS__); } while (0)
#define semanticFailIfTrue(cond, ...) do { if (UNLIKELY(cond)) internalFailWithMessage(false, __VA_ARGS__); } while (0)
#define semanticFailIfFalse(cond, ...) do { if (UNLIKELY(!(cond))) internalFailWithMessage(false, __VA_ARGS__); } while (0)
#define regexFail(failure) do { setErrorMessage(failure); return 0; } while (0)
#define failDueToUnexpectedToken() do {\
	logError(true);\
    return 0;\
} while (0)

#define handleProductionOrFail(token, tokenString, operation, production) do {\
    consumeOrFail(token, ASCIILiteral("Expected '"), tokenString, ASCIILiteral("' to "), operation, ASCIILiteral(" a "), production);\
} while (0)

#define handleProductionOrFail2(token, tokenString, operation, production) do {\
    consumeOrFail(token, ASCIILiteral("Expected '"), tokenString, ASCIILiteral("' to "), operation, ASCIILiteral(" an "), production);\
} while (0)

#define semanticFailureDueToKeywordCheckingToken(token, ...) do { \
    if (strictMode() && token.m_type == RESERVED_IF_STRICT) \
        semanticFail(ASCIILiteral("Cannot use the reserved word '"), getToken(token), ASCIILiteral("' as a "), __VA_ARGS__, ASCIILiteral(" in strict mode")); \
    if (token.m_type == RESERVED || token.m_type == RESERVED_IF_STRICT) \
        semanticFail(ASCIILiteral("Cannot use the reserved word '"), getToken(token), ASCIILiteral("' as a "), __VA_ARGS__); \
    if (token.m_type & KeywordTokenFlag) { \
        if (!isContextualKeyword(token)) \
            semanticFail(ASCIILiteral("Cannot use the keyword '"), getToken(token), ASCIILiteral("' as a "), __VA_ARGS__); \
		if (token.m_type == LET && strictMode())\
            semanticFail(ASCIILiteral("Cannot use 'let' as a "), __VA_ARGS__, ASCIILiteral(" "), disallowedIdentifierLetReason()); \
		if (token.m_type == AWAIT && !canUseIdentifierAwait()) \
            semanticFail(ASCIILiteral("Cannot use 'await' as a "), __VA_ARGS__, ASCIILiteral(" "), disallowedIdentifierAwaitReason()); \
		if (token.m_type == YIELD && !canUseIdentifierYield()) \
            semanticFail(ASCIILiteral("Cannot use 'yield' as a "), __VA_ARGS__, ASCIILiteral(" "), disallowedIdentifierYieldReason()); \
    } \
} while (0)

#define semanticFailureDueToKeyword(...) semanticFailureDueToKeywordCheckingToken(m_token, __VA_ARGS__);

using namespace std;

namespace JSC {

ALWAYS_INLINE static SourceParseMode getAsynFunctionBodyParseMode(SourceParseMode parseMode)
{
    if (isAsyncGeneratorWrapperParseMode(parseMode))
        return SourceParseMode::AsyncGeneratorBodyMode;

    if (parseMode == SourceParseMode::AsyncArrowFunctionMode)
        return SourceParseMode::AsyncArrowFunctionBodyMode;

    return SourceParseMode::AsyncFunctionBodyMode;
}

template <typename LexerType>
NEVER_INLINE void Parser<LexerType>::logError(bool)
{
    if (hasError())
        return;
    StringPrintStream stream;
    printUnexpectedTokenText(stream);
    setErrorMessage(stream.toStringWithLatin1Fallback());
}

template <typename LexerType> template <typename A>
NEVER_INLINE void Parser<LexerType>::logError(bool shouldPrintToken, const A& value1)
{
    if (hasError())
        return;
    StringPrintStream stream;
    if (shouldPrintToken) {
        printUnexpectedTokenText(stream);
        stream.print(". ");
    }
    stream.print(value1, ".");
    setErrorMessage(stream.toStringWithLatin1Fallback());
}

template <typename LexerType> template <typename A, typename B>
NEVER_INLINE void Parser<LexerType>::logError(bool shouldPrintToken, const A& value1, const B& value2)
{
    if (hasError())
        return;
    StringPrintStream stream;
    if (shouldPrintToken) {
        printUnexpectedTokenText(stream);
        stream.print(". ");
    }
    stream.print(value1, value2, ".");
    setErrorMessage(stream.toStringWithLatin1Fallback());
}

template <typename LexerType> template <typename A, typename B, typename C>
NEVER_INLINE void Parser<LexerType>::logError(bool shouldPrintToken, const A& value1, const B& value2, const C& value3)
{
    if (hasError())
        return;
    StringPrintStream stream;
    if (shouldPrintToken) {
        printUnexpectedTokenText(stream);
        stream.print(". ");
    }
    stream.print(value1, value2, value3, ".");
    setErrorMessage(stream.toStringWithLatin1Fallback());
}

template <typename LexerType> template <typename A, typename B, typename C, typename D>
NEVER_INLINE void Parser<LexerType>::logError(bool shouldPrintToken, const A& value1, const B& value2, const C& value3, const D& value4)
{
    if (hasError())
        return;
    StringPrintStream stream;
    if (shouldPrintToken) {
        printUnexpectedTokenText(stream);
        stream.print(". ");
    }
    stream.print(value1, value2, value3, value4, ".");
    setErrorMessage(stream.toStringWithLatin1Fallback());
}

template <typename LexerType> template <typename A, typename B, typename C, typename D, typename E>
NEVER_INLINE void Parser<LexerType>::logError(bool shouldPrintToken, const A& value1, const B& value2, const C& value3, const D& value4, const E& value5)
{
    if (hasError())
        return;
    StringPrintStream stream;
    if (shouldPrintToken) {
        printUnexpectedTokenText(stream);
        stream.print(". ");
    }
    stream.print(value1, value2, value3, value4, value5, ".");
    setErrorMessage(stream.toStringWithLatin1Fallback());
}

template <typename LexerType> template <typename A, typename B, typename C, typename D, typename E, typename F>
NEVER_INLINE void Parser<LexerType>::logError(bool shouldPrintToken, const A& value1, const B& value2, const C& value3, const D& value4, const E& value5, const F& value6)
{
    if (hasError())
        return;
    StringPrintStream stream;
    if (shouldPrintToken) {
        printUnexpectedTokenText(stream);
        stream.print(". ");
    }
    stream.print(value1, value2, value3, value4, value5, value6, ".");
    setErrorMessage(stream.toStringWithLatin1Fallback());
}

template <typename LexerType> template <typename A, typename B, typename C, typename D, typename E, typename F, typename G>
NEVER_INLINE void Parser<LexerType>::logError(bool shouldPrintToken, const A& value1, const B& value2, const C& value3, const D& value4, const E& value5, const F& value6, const G& value7)
{
    if (hasError())
        return;
    StringPrintStream stream;
    if (shouldPrintToken) {
        printUnexpectedTokenText(stream);
        stream.print(". ");
    }
    stream.print(value1, value2, value3, value4, value5, value6, value7, ".");
    setErrorMessage(stream.toStringWithLatin1Fallback());
}

template <typename LexerType>
Parser<LexerType>::Parser(JSGlobalData* vm, const SourceCode& source, JSParserBuiltinMode builtinMode, JSParserStrictMode strictMode, JSParserScriptMode scriptMode, SourceParseMode parseMode, FunctionMode functionMode, SuperBinding superBinding, ConstructorKind constructorKind, DerivedContextType derivedContextType, bool isEvalContext, EvalContextType evalContextType)
    : m_vm(vm)
    , m_source(&source)
	, m_parserArena(builtinMode == JSParserBuiltinMode::Builtin)
	, m_lexer(vm, builtinMode, scriptMode)
    , m_stack(wtfThreadData().stack())
    , m_useObjectRestSpread(Options::useObjectRestSpread())
    , m_allowsIn(true)
    , m_parsingBuiltin(builtinMode == JSParserBuiltinMode::Builtin)
    , m_parseMode(parseMode)
    , m_functionMode(functionMode)
	, m_scriptMode(scriptMode)
    , m_superBinding(superBinding)
{
    m_lexer.setCode(source, &m_parserArena);
    m_token.m_location.endOffset = source.startOffset();
    m_token.m_location.lineStartOffset = source.startOffset();
    m_functionCache = vm->addSourceProviderCache(source.provider());

    ScopeRef scope = pushScope();
    scope->setSourceParseMode(parseMode);
    scope->setIsEvalContext(isEvalContext);
    if (isEvalContext)
        scope->setEvalContextType(evalContextType);

    if (scope->isFunction())
        scope->setConstructorKind(constructorKind);
    else
        ASSERT(constructorKind == ConstructorKind::None);

    scope->setDerivedContextType(derivedContextType);
    if (derivedContextType != DerivedContextType::None)
        scope->setExpectedSuperBinding(SuperBinding::Needed);

    if (strictMode == JSParserStrictMode::Strict)
        scope->setStrictMode();

    if (isModuleParseMode(parseMode))
        m_moduleScopeData = ModuleScopeData::create();

    next();
}

class Scope::MaybeParseAsGeneratorForScope {
public:
	ALWAYS_INLINE MaybeParseAsGeneratorForScope(ScopeRef& scope, bool shouldParseAsGenerator)
        : m_scope(scope)
        , m_oldValue(scope->m_isGenerator)
    {
        m_scope->m_isGenerator = shouldParseAsGenerator;
    }

	ALWAYS_INLINE ~MaybeParseAsGeneratorForScope()
    {
        m_scope->m_isGenerator = m_oldValue;
    }

private:
    ScopeRef m_scope;
    bool m_oldValue;
};

namespace {
struct DepthManager : private SetForScope<int> {
public:
    ALWAYS_INLINE DepthManager(int* depth)
        : SetForScope<int>(*depth, *depth)
    {
    }
};
}

template <typename LexerType>
Parser<LexerType>::~Parser() = default;

static ALWAYS_INLINE bool isPrivateFieldName(UniquedStringImpl* uid)
{
    return uid->length() && uid->at(0) == '#';
}

template <typename LexerType>
typename Parser<LexerType>::ParseInnerResult Parser<LexerType>::parseInner(const Identifier& calleeName, ParsingContext parsingContext, Optional<int> functionConstructorParametersEndPosition, const Vector<JSTextPosition>* classFieldLocations, const PrivateNameEnvironment* parentScopePrivateNames)
{
    OwnPtr<ASTBuilder> context = adoptPtr(new ASTBuilder(const_cast<JSGlobalData*>(m_vm), m_parserArena, const_cast<SourceCode*>(m_source)));
    SourceParseMode parseMode = sourceParseMode();
	ScopeRef scope = currentScope();
    scope->setIsLexicalScope();

    bool hasPrivateNames = scope->isEvalContext() && parentScopePrivateNames && parentScopePrivateNames->size();

    if (hasPrivateNames) {
        scope->setIsPrivateNameScope();
        scope->lexicalVariables().addPrivateNamesFrom(parentScopePrivateNames);
    }

    SetForScope<FunctionParsePhase> functionParsePhasePoisoner(m_parserState.functionParsePhase, FunctionParsePhase::Body);

    FunctionParameters* parameters = nullptr;
    bool isArrowFunctionBodyExpression = parseMode == SourceParseMode::AsyncArrowFunctionBodyMode && !match(OPENBRACE);
	if (m_lexer.isReparsingFunction()) {
        ParserFunctionInfo<ASTBuilder> functionInfo;
        if (isGeneratorOrAsyncFunctionBodyParseMode(parseMode))
			parameters = createGeneratorParameters(*context, functionInfo.parameterCount);
        else if (parseMode == SourceParseMode::ClassFieldInitializerMode)
            parameters = context->createFormalParameterList();
        else
        	parameters = parseFunctionParameters(*context, functionInfo);

        if (SourceParseModeSet(SourceParseMode::ArrowFunctionMode, SourceParseMode::AsyncArrowFunctionMode).contains(parseMode) && !hasError()) {
        	// FIXME:
			// Logically, this should be an assert, since we already successfully parsed the arrow
			// function when syntax checking. So logically, we should see the arrow token here.
			// But we're seeing crashes in the wild when making this an assert. Instead, we'll just
			// handle it as an error in release builds, and an assert on debug builds, with the hopes
			// of fixing it in the future.
			// https://bugs.webkit.org/show_bug.cgi?id=221633
			if (UNLIKELY(!match(ARROWFUNCTION))) {
				ASSERT_NOT_REACHED();
				return ParseInnerResult { nullptr, nullptr, DeclarationStacks::FunctionStack(), VariableEnvironment(), VariableEnvironment(), NoFeatures, 0, ASCIILiteral("Parser error") };
			}
            next();
            isArrowFunctionBodyExpression = !match(OPENBRACE);
        }
    }

    if (functionNameIsInScope(calleeName, functionMode()))
        scope->declareCallee(&calleeName);

    if (m_lexer.isReparsingFunction())
        m_statementDepth--;

    SourceElements* sourceElements = 0;
    // The only way we can error this early is if we reparse a function and we run out of stack space.
    if (!hasError()) {
    	if (isAsyncFunctionWrapperParseMode(parseMode))
			sourceElements = parseAsyncFunctionSourceElements(*context, isArrowFunctionBodyExpression, CheckForStrictMode);
		else if (isArrowFunctionBodyExpression)
            sourceElements = parseArrowFunctionSingleExpressionBodySourceElements(*context);
        else if (isModuleParseMode(parseMode))
			sourceElements = parseModuleSourceElements(*context);
        else if (isGeneratorWrapperParseMode(parseMode))
			sourceElements = parseGeneratorFunctionSourceElements(*context, calleeName, CheckForStrictMode);
        else if (isAsyncGeneratorWrapperParseMode(parseMode))
			sourceElements = parseAsyncGeneratorFunctionSourceElements(*context, isArrowFunctionBodyExpression, CheckForStrictMode);
        else if (parsingContext == ParsingContext::FunctionConstructor)
			sourceElements = parseSingleFunction(*context, functionConstructorParametersEndPosition);
		else if (parseMode == SourceParseMode::ClassFieldInitializerMode) {
			ASSERT(classFieldLocations && !classFieldLocations->isEmpty());
			sourceElements = parseClassFieldInitializerSourceElements(*context, *classFieldLocations);
		} else
			sourceElements = parseSourceElements(*context, CheckForStrictMode);
    }

    bool validEnding = consume(EOFTOK);
    if (!sourceElements || !validEnding)
		return ParseInnerResult { nullptr, nullptr, DeclarationStacks::FunctionStack(), VariableEnvironment(), VariableEnvironment(), NoFeatures, 0, hasError() ? m_errorMessage : ASCIILiteral("Parser error") };

    if (!m_lexer.isReparsingFunction() && m_seenPrivateNameUseInNonReparsingFunctionMode) {
        String errorMessage;
        scope->forEachUsedVariable([&] (UniquedStringImpl* impl) {
            if (!isPrivateFieldName(impl))
                return IterationStatus::Continue;
            if (parentScopePrivateNames && parentScopePrivateNames->contains(impl))
                return IterationStatus::Continue;
            if (scope->lexicalVariables().contains(impl))
                return IterationStatus::Continue;
            errorMessage = tryMakeString("Cannot reference undeclared private names: \"", String(impl), "\"");
            return IterationStatus::Done;
        });
        if (!errorMessage.isNull())
            return ParseInnerResult { nullptr, nullptr, DeclarationStacks::FunctionStack(), VariableEnvironment(), VariableEnvironment(), NoFeatures, 0, errorMessage };
    }

    // It's essential to finalize the hoisting before computing captured variables.
    scope->finalizeSloppyModeFunctionHoisting();

    IdentifierSet capturedVariables;
    scope->getCapturedVars(capturedVariables);

    VariableEnvironment& varDeclarations = scope->declaredVariables();
    for (auto& entry : capturedVariables)
		varDeclarations.markVariableAsCaptured(entry.get());
    scope->finalizeLexicalEnvironment();

	if (isGeneratorWrapperParseMode(parseMode) || isAsyncFunctionOrAsyncGeneratorWrapperParseMode(parseMode)) {
        if (scope->usedVariablesContains(m_vm->propertyNames->arguments.impl()))
            context->propagateArgumentsUse();
    }

    CodeFeatures features = context->features();
    if (scope->strictMode())
        features |= StrictModeFeature;
    if (scope->shadowsArguments())
        features |= ShadowsArgumentsFeature;
    if (m_seenTaggedTemplateInNonReparsingFunctionMode)
        features |= NoEvalCacheFeature;
    if (scope->hasNonSimpleParameterList())
        features |= NonSimpleParameterListFeature;

#ifndef NDEBUG
    if (m_parsingBuiltin) {
		// 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();
			}
		}
	}
#endif // NDEBUG

    return ParseInnerResult { parameters, sourceElements, scope->takeFunctionDeclarations(), scope->takeDeclaredVariables(), scope->takeLexicalEnvironment(), features, context->numConstants(), String() };
}

template <typename LexerType>
template <class TreeBuilder> bool Parser<LexerType>::isArrowFunctionParameters(TreeBuilder& context)
{
    if (match(OPENPAREN)) {
        SavePoint saveArrowFunctionPoint = createSavePoint(context);
        next();
        bool isArrowFunction = false;
        if (match(CLOSEPAREN)) {
            next();
            isArrowFunction = match(ARROWFUNCTION);
        } else {
            SyntaxChecker syntaxChecker(const_cast<VM*>(m_vm), &m_lexer);
            // We make fake scope, otherwise parseFormalParameters will add variable to current scope that lead to errors
            AutoPopScopeRef fakeScope(this, pushScope());
            fakeScope->setSourceParseMode(SourceParseMode::ArrowFunctionMode);

            unsigned parametersCount = 0;
            bool isArrowFunctionParameterList = true;
            bool isMethod = false;
            isArrowFunction = parseFormalParameters(syntaxChecker, syntaxChecker.createFormalParameterList(), isArrowFunctionParameterList, isMethod, parametersCount) && consume(CLOSEPAREN) && match(ARROWFUNCTION);
			propagateError();
            popScope(fakeScope, syntaxChecker.NeedsFreeVariableInfo);
        }
        restoreSavePoint(context, saveArrowFunctionPoint);
        return isArrowFunction;
    }

    if (matchSpecIdentifier()) {
    	semanticFailIfTrue(isDisallowedIdentifierAwait(m_token), ASCIILiteral("Cannot use 'await' as a parameter name "), disallowedIdentifierAwaitReason());
		SavePoint saveArrowFunctionPoint = createSavePoint(context);
        next();
        bool isArrowFunction = match(ARROWFUNCTION);
        restoreSavePoint(context, saveArrowFunctionPoint);
        return isArrowFunction;
    }

    return false;
}

template <typename LexerType>
bool Parser<LexerType>::allowAutomaticSemicolon()
{
    return match(CLOSEBRACE) || match(EOFTOK) || m_lexer.hasLineTerminatorBeforeToken();
}

template <typename LexerType>
template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseSourceElements(TreeBuilder& context, SourceElementsMode mode)
{
    const unsigned lengthOfUseStrictLiteral = 12; // "use strict".length
    TreeSourceElements sourceElements = context.createSourceElements();
    failIfFalse(sourceElements, ASCIILiteral("Out of memory"));
    const Identifier* directive = 0;
    unsigned directiveLiteralLength = 0;
    SavePoint savePoint = createSavePoint(context);
    bool shouldCheckForUseStrict = mode == CheckForStrictMode;

    while (TreeStatement statement = parseStatementListItem(context, directive, &directiveLiteralLength)) {
    	if (shouldCheckForUseStrict) {
            if (directive) {
                // "use strict" must be the exact literal without escape sequences or line continuation.
                if (directiveLiteralLength == lengthOfUseStrictLiteral && m_vm->propertyNames->useStrictIdentifier == *directive) {
                    setStrictMode();
                    shouldCheckForUseStrict = false; // We saw "use strict", there is no need to keep checking for it.
                    if (!isValidStrictMode()) {
						if (m_parserState.lastFunctionName) {
							if (m_vm->propertyNames->arguments == *m_parserState.lastFunctionName)
								semanticFail(ASCIILiteral("Cannot name a function 'arguments' in strict mode"));
							if (m_vm->propertyNames->eval == *m_parserState.lastFunctionName)
								semanticFail(ASCIILiteral("Cannot name a function 'eval' in strict mode"));
						}
						if (hasDeclaredVariable(m_vm->propertyNames->arguments))
							semanticFail(ASCIILiteral("Cannot declare a variable named 'arguments' in strict mode"));
						if (hasDeclaredVariable(m_vm->propertyNames->eval))
							semanticFail(ASCIILiteral("Cannot declare a variable named 'eval' in strict mode"));
						semanticFailIfTrue(currentScope()->hasNonSimpleParameterList(), ASCIILiteral("'use strict' directive not allowed inside a function with a non-simple parameter list"));
						semanticFailIfFalse(isValidStrictMode(), ASCIILiteral("Invalid parameters or function name in strict mode"));
					}
                    // Since strict mode is changed, restoring lexer state by calling next() may cause errors.
                    restoreSavePoint(context, savePoint);
                    propagateError();
                    continue;
                }

                // We saw a directive, but it wasn't "use strict". We reset our state to
                // see if the next statement we parse is also a directive.
                directive = 0;
            } else {
                // We saw a statement that wasn't in the form of a directive. The spec says that "use strict"
                // is only allowed as the first statement, or after a sequence of directives before it, but
                // not after non-directive statements.
                shouldCheckForUseStrict = false;
            }
        }
        context.appendStatement(sourceElements, statement);
    }

    propagateError();
    return sourceElements;
}

template <typename LexerType>
template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseModuleSourceElements(TreeBuilder& context)
{
    TreeSourceElements sourceElements = context.createSourceElements();
    SyntaxChecker syntaxChecker(const_cast<VM*>(m_vm), &m_lexer);
    while (true) {
		TreeStatement statement = 0;
        switch (m_token.m_type) {
        case EXPORT:
            statement = parseExportDeclaration(context);
            if (statement)
                recordPauseLocation(context.breakpointLocation(statement));
            break;

        case IMPORT: {
            SavePoint savePoint = createSavePoint(context);
            next();
            bool isImportDeclaration = !match(OPENPAREN) && !match(DOT);
            restoreSavePoint(context, savePoint);
            if (isImportDeclaration) {
                statement = parseImportDeclaration(context);
                if (statement)
                    recordPauseLocation(context.breakpointLocation(statement));
                break;
            }

            // This is `import("...")` call or `import.meta` meta property case.
            FALLTHROUGH;
        }

        default: {
			const Identifier* directive = nullptr;
			unsigned directiveLiteralLength = 0;
            if (sourceParseMode() == SourceParseMode::ModuleAnalyzeMode) {
				if (!parseStatementListItem(syntaxChecker, directive, &directiveLiteralLength))
					goto end;
				continue;
			}
			statement = parseStatementListItem(context, directive, &directiveLiteralLength);
			break;
		}
		}

		if (!statement)
			goto end;
		context.appendStatement(sourceElements, statement);
	}

end:
	propagateError();

    for (auto iter = m_moduleScopeData->exportedBindings().begin(); iter != m_moduleScopeData->exportedBindings().end(); ++iter) {
        const RefPtr<UniquedStringImpl>& uid = iter->first;
		if (currentScope()->hasDeclaredVariable(uid)) {
			currentScope()->declaredVariables().markVariableAsExported(uid.get());
			continue;
		}

		if (currentScope()->hasLexicallyDeclaredVariable(uid)) {
			currentScope()->lexicalVariables().markVariableAsExported(uid.get());
			continue;
		}

		semanticFail(ASCIILiteral("Exported binding '"), uid.get(), ASCIILiteral("' needs to refer to a top-level declared variable"));
	}

    return sourceElements;
}

template <typename LexerType>
template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseGeneratorFunctionSourceElements(TreeBuilder& context, const Identifier& name, SourceElementsMode mode)
{
    auto sourceElements = context.createSourceElements();

    unsigned functionKeywordStart = tokenStart();
    JSTokenLocation startLocation(tokenLocation());
    JSTextPosition start = tokenStartPosition();
    unsigned startColumn = tokenColumn();
    int functionNameStart = m_token.m_location.startOffset;
    int parametersStart = m_token.m_location.startOffset;

    ParserFunctionInfo<TreeBuilder> info;
    info.name = &m_vm->propertyNames->nullIdentifier;
    createGeneratorParameters(context, info.parameterCount);
    info.startOffset = parametersStart;
    info.startLine = tokenLine();

    {
        AutoPopScopeRef generatorBodyScope(this, pushScope());
        generatorBodyScope->setSourceParseMode(SourceParseMode::GeneratorBodyMode);
        generatorBodyScope->setConstructorKind(ConstructorKind::None);
		generatorBodyScope->setExpectedSuperBinding(m_superBinding);

        SyntaxChecker generatorFunctionContext(const_cast<VM*>(m_vm), &m_lexer);
        failIfFalse(parseSourceElements(generatorFunctionContext, mode), ASCIILiteral("Cannot parse the body of a generator"));
        popScope(generatorBodyScope, TreeBuilder::NeedsFreeVariableInfo);
    }
    info.body = context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, tokenColumn(), functionKeywordStart, functionNameStart, parametersStart, strictMode(), ConstructorKind::None, m_superBinding, info.parameterCount, SourceParseMode::GeneratorBodyMode, false);

    info.endLine = tokenLine();
    info.endOffset = m_token.m_data.offset;
    info.parametersStartColumn = startColumn;

    auto functionExpr = context.createGeneratorFunctionBody(startLocation, info, name);
    auto statement = context.createExprStatement(startLocation, functionExpr, start, m_lastTokenEndPosition.line);
    context.appendStatement(sourceElements, statement);

    return sourceElements;
}

template <typename LexerType>
template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseAsyncFunctionSourceElements(TreeBuilder& context, bool isArrowFunctionBodyExpression, SourceElementsMode mode)
{
    ASSERT(isAsyncFunctionOrAsyncGeneratorWrapperParseMode(sourceParseMode()));
    auto sourceElements = context.createSourceElements();

    unsigned functionKeywordStart = tokenStart();
    JSTokenLocation startLocation(tokenLocation());
    JSTextPosition start = tokenStartPosition();
    unsigned startColumn = tokenColumn();
    int functionNameStart = m_token.m_location.startOffset;
    int parametersStart = m_token.m_location.startOffset;

    ParserFunctionInfo<TreeBuilder> info;
    info.name = &m_vm->propertyNames->nullIdentifier;
    createGeneratorParameters(context, info.parameterCount);
    info.startOffset = parametersStart;
    info.startLine = tokenLine();

    SourceParseMode parseMode = getAsynFunctionBodyParseMode(sourceParseMode());
    SetForScope<SourceParseMode> innerParseMode(m_parseMode, parseMode);
    {
        AutoPopScopeRef asyncFunctionBodyScope(this, pushScope());
        asyncFunctionBodyScope->setSourceParseMode(sourceParseMode());
        SyntaxChecker syntaxChecker(const_cast<VM*>(m_vm), &m_lexer);
        if (isArrowFunctionBodyExpression) {
#if 0
        	if (m_debuggerParseData)
                failIfFalse(parseArrowFunctionSingleExpressionBodySourceElements(context), "Cannot parse the body of async arrow function");
            else
#endif
                failIfFalse(parseArrowFunctionSingleExpressionBodySourceElements(syntaxChecker), ASCIILiteral("Cannot parse the body of async arrow function"));
        } else {
#if 0
        	if (m_debuggerParseData)
                failIfFalse(parseSourceElements(context, mode), "Cannot parse the body of async function");
            else
#endif
                failIfFalse(parseSourceElements(syntaxChecker, mode), ASCIILiteral("Cannot parse the body of async function"));
        }
        popScope(asyncFunctionBodyScope, TreeBuilder::NeedsFreeVariableInfo);
    }
    info.body = context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, tokenColumn(), functionKeywordStart, functionNameStart, parametersStart, strictMode(), ConstructorKind::None, m_superBinding, info.parameterCount, sourceParseMode(), isArrowFunctionBodyExpression);

    info.endLine = tokenLine();
    info.endOffset = isArrowFunctionBodyExpression ? tokenLocation().endOffset : m_token.m_data.offset;
    info.parametersStartColumn = startColumn;

    auto functionExpr = context.createAsyncFunctionBody(startLocation, info, parseMode);
    auto statement = context.createExprStatement(startLocation, functionExpr, start, m_lastTokenEndPosition.line);
    context.appendStatement(sourceElements, statement);

    return sourceElements;
}

template <typename LexerType>
template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseAsyncGeneratorFunctionSourceElements(TreeBuilder& context, bool isArrowFunctionBodyExpression, SourceElementsMode mode)
{
    ASSERT(isAsyncGeneratorWrapperParseMode(sourceParseMode()));
    auto sourceElements = context.createSourceElements();

    unsigned functionKeywordStart = tokenStart();
    JSTokenLocation startLocation(tokenLocation());
    JSTextPosition start = tokenStartPosition();
    unsigned startColumn = tokenColumn();
    int functionNameStart = m_token.m_location.startOffset;
    int parametersStart = m_token.m_location.startOffset;

    ParserFunctionInfo<TreeBuilder> info;
    info.name = &m_vm->propertyNames->nullIdentifier;
    createGeneratorParameters(context, info.parameterCount);
    info.startOffset = parametersStart;
    info.startLine = tokenLine();

    SourceParseMode parseMode = SourceParseMode::AsyncGeneratorBodyMode;
    SetForScope<SourceParseMode> innerParseMode(m_parseMode, parseMode);
    {
        AutoPopScopeRef asyncFunctionBodyScope(this, pushScope());
        asyncFunctionBodyScope->setSourceParseMode(sourceParseMode());
        SyntaxChecker syntaxChecker(const_cast<VM*>(m_vm), &m_lexer);
        if (isArrowFunctionBodyExpression) {
#if 0
            if (m_debuggerParseData)
                failIfFalse(parseArrowFunctionSingleExpressionBodySourceElements(context), "Cannot parse the body of async arrow function");
            else
#endif
                failIfFalse(parseArrowFunctionSingleExpressionBodySourceElements(syntaxChecker), ASCIILiteral("Cannot parse the body of async arrow function"));
        } else {
#if 0
            if (m_debuggerParseData)
                failIfFalse(parseSourceElements(context, mode), "Cannot parse the body of async function");
            else
#endif
                failIfFalse(parseSourceElements(syntaxChecker, mode), ASCIILiteral("Cannot parse the body of async function"));
        }
        popScope(asyncFunctionBodyScope, TreeBuilder::NeedsFreeVariableInfo);
    }
    info.body = context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, tokenColumn(), functionKeywordStart, functionNameStart, parametersStart, strictMode(), ConstructorKind::None, m_superBinding, info.parameterCount, parseMode, isArrowFunctionBodyExpression);

    info.endLine = tokenLine();
    info.endOffset = isArrowFunctionBodyExpression ? tokenLocation().endOffset : m_token.m_data.offset;
    info.parametersStartColumn = startColumn;

    auto functionExpr = context.createAsyncFunctionBody(startLocation, info, parseMode);
    auto statement = context.createExprStatement(startLocation, functionExpr, start, m_lastTokenEndPosition.line);
    context.appendStatement(sourceElements, statement);

    return sourceElements;
}

template <typename LexerType>
template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseSingleFunction(TreeBuilder& context, Optional<int> functionConstructorParametersEndPosition)
{
    TreeSourceElements sourceElements = context.createSourceElements();
    TreeStatement statement = 0;
    switch (m_token.m_type) {
    case FUNCTION:
        statement = parseFunctionDeclaration(context, FunctionDeclarationType::Declaration, ExportType::NotExported, DeclarationDefaultContext::Standard, functionConstructorParametersEndPosition);
        break;
    case IDENT:
        if (*m_token.m_data.ident == m_vm->propertyNames->async && !m_token.m_data.escaped) {
            next();
            failIfFalse(match(FUNCTION) && !m_lexer.hasLineTerminatorBeforeToken(), ASCIILiteral("Cannot parse the async function"));
            statement = parseAsyncFunctionDeclaration(context, ExportType::NotExported, DeclarationDefaultContext::Standard, functionConstructorParametersEndPosition);
            break;
        }
        FALLTHROUGH;
    default:
        failDueToUnexpectedToken();
        break;
    }

    if (statement) {
        context.setEndOffset(statement, m_lastTokenEndPosition.offset);
        context.appendStatement(sourceElements, statement);
    }

    propagateError();
    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++;
    failIfStackOverflow();
	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(context);
            next();
            // Intentionally use `matchIdentifierOrPossiblyEscapedContextualKeyword()` and not `matchSpecIdentifier()`.
			// We would like contextual keywords to fall under parseVariableDeclaration even when not used as identifiers.
			// For example, under a generator context, matchSpecIdentifier() for "yield" returns `false`.
			// But we would like to enter parseVariableDeclaration and raise an error under the context of parseVariableDeclaration
			// to raise consistent errors between "var", "const" and "let".
			if (!matchIdentifierOrPossiblyEscapedContextualKeyword() && !match(OPENBRACE) && !match(OPENBRACKET))
				shouldParseVariableDeclaration = false;
            restoreSavePoint(context, savePoint);
        }
        if (shouldParseVariableDeclaration)
            result = parseVariableDeclaration(context, DeclarationType::LetDeclaration);
        else {
			bool allowFunctionDeclarationAsStatement = true;
			result = parseExpressionOrLabelStatement(context, allowFunctionDeclarationAsStatement);
		}

        break;
    }
    case CLASSTOKEN:
        result = parseClassDeclaration(context);
        break;
    case FUNCTION:
        result = parseFunctionDeclaration(context);
        break;
    case ESCAPED_KEYWORD:
        if (!matchAllowedEscapedContextualKeyword()) {
            failDueToUnexpectedToken();
            break;
        }
        FALLTHROUGH;
    case IDENT:
        if (UNLIKELY(*m_token.m_data.ident == m_vm->propertyNames->async && !m_token.m_data.escaped)) {
            // Eagerly parse as AsyncFunctionDeclaration. This is the uncommon case,
			// but could be mistakenly parsed as an AsyncFunctionExpression.
			SavePoint savePoint = createSavePoint(context);
			next();
			if (UNLIKELY(match(FUNCTION) && !m_lexer.hasLineTerminatorBeforeToken())) {
				result = parseAsyncFunctionDeclaration(context);
				break;
			}
			restoreSavePoint(context, savePoint);
        }
        FALLTHROUGH;
    case AWAIT:
    case YIELD: {
        if (UNLIKELY(currentScope()->isStaticBlock())) {
            failIfTrue(match(YIELD), ASCIILiteral("Cannot use 'yield' within static block"));
            failIfTrue(match(AWAIT), ASCIILiteral("Cannot use 'await' within static block"));
        }
        // This is a convenient place to notice labeled statements
        // (even though we also parse them as normal statements)
        // because we allow the following type of code in sloppy mode:
        // ``` function foo() { label: function bar() { } } ```
        bool allowFunctionDeclarationAsStatement = true;
        result = parseExpressionOrLabelStatement(context, allowFunctionDeclarationAsStatement);
        break;
    }
    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;
}

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseVariableDeclaration(TreeBuilder& context, DeclarationType declarationType, ExportType exportType)
{
    ASSERT(match(VAR) || match(LET) || 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);
    propagateError();
    failIfFalse(autoSemiColon(), ASCIILiteral("Expected ';' after variable declaration"));
    
    return context.createDeclarationStatement(location, variableDecls, start, end);
}

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseDoWhileStatement(TreeBuilder& context)
{
    ASSERT(match(DO));
    int startLine = tokenLine();
    next();
    const Identifier* unused = 0;
    startLoop();
    TreeStatement statement = parseStatement(context, unused);
    endLoop();
    failIfFalse(statement, ASCIILiteral("Expected a statement following 'do'"));
    int endLine = tokenLine();
    JSTokenLocation location(tokenLocation());
    handleProductionOrFail(WHILE, ASCIILiteral("while"), ASCIILiteral("end"), ASCIILiteral("do-while loop"));
	handleProductionOrFail(OPENPAREN, ASCIILiteral("("), ASCIILiteral("start"), ASCIILiteral("do-while loop condition"));
	semanticFailIfTrue(match(CLOSEPAREN), ASCIILiteral("Must provide an expression as a do-while loop condition"));
    TreeExpression expr = parseExpression(context);
    failIfFalse(expr, ASCIILiteral("Unable to parse do-while loop condition"));
    handleProductionOrFail(CLOSEPAREN, ASCIILiteral(")"), ASCIILiteral("end"), ASCIILiteral("do-while loop condition"));
    if (match(SEMICOLON))
        next(); // Always performs automatic semicolon insertion.
    return context.createDoWhileStatement(location, statement, expr, startLine, endLine);
}

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseWhileStatement(TreeBuilder& context)
{
    ASSERT(match(WHILE));
    JSTokenLocation location(tokenLocation());
    int startLine = tokenLine();
    next();

    handleProductionOrFail(OPENPAREN, ASCIILiteral("("), ASCIILiteral("start"), ASCIILiteral("while loop condition"));
	semanticFailIfTrue(match(CLOSEPAREN), ASCIILiteral("Must provide an expression as a while loop condition"));
    TreeExpression expr = parseExpression(context);
    failIfFalse(expr, ASCIILiteral("Unable to parse while loop condition"));
    int endLine = tokenLine();
    handleProductionOrFail(CLOSEPAREN, ASCIILiteral(")"), ASCIILiteral("end"), ASCIILiteral("while loop condition"));

    const Identifier* unused = 0;
    startLoop();
    TreeStatement statement = parseStatement(context, unused);
    endLoop();
    failIfFalse(statement, ASCIILiteral("Expected a statement as the body of a while loop"));
    return context.createWhileStatement(location, expr, statement, startLine, endLine);
}

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)
{
	ASSERT(declarationType == DeclarationType::LexicalDeclaration || declarationType == DeclarationType::VarDeclaration);
	TreeExpression head = 0;
    TreeExpression tail = 0;
    const Identifier* lastIdent;
    JSToken lastIdentToken;
    AssignmentContext assignmentContext = assignmentContextFromDeclarationType(declarationType);
    do {
        lastIdent = 0;
        lastPattern = 0;
        JSTokenLocation location(tokenLocation());
        next();
        TreeExpression node = 0;
        declarations++;
        bool hasInitializer = false;

        failIfTrue(match(PRIVATENAME), ASCIILiteral("Cannot use a private name to declare a variable"));
        if (matchSpecIdentifier()) {
        	semanticFailIfTrue(currentScope()->isStaticBlock() && isArgumentsIdentifier(), ASCIILiteral("Cannot use 'arguments' as an identifier in static block"));
			failIfTrue(isPossiblyEscapedLet(m_token) && (declarationType == DeclarationType::LetDeclaration || declarationType == DeclarationType::ConstDeclaration),
				ASCIILiteral("Cannot use 'let' as an identifier name for a LexicalDeclaration"));
        	semanticFailIfTrue(isDisallowedIdentifierAwait(m_token), ASCIILiteral("Cannot use 'await' as a "), declarationTypeToVariableKind(declarationType), ASCIILiteral(" "), disallowedIdentifierAwaitReason());
			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, ASCIILiteral("Cannot declare a variable named "), name, ASCIILiteral(" in strict mode"));
                if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration) {
                    if (declarationType == DeclarationType::LetDeclaration)
                        internalFailWithMessage(false, ASCIILiteral("Cannot declare a let variable twice: '"), name, ASCIILiteral("'"));
                    if (declarationType == DeclarationType::ConstDeclaration)
                        internalFailWithMessage(false, ASCIILiteral("Cannot declare a const variable twice: '"), name, ASCIILiteral("'"));
                    ASSERT(declarationType == DeclarationType::VarDeclaration);
					internalFailWithMessage(false, ASCIILiteral("Cannot declare a var variable that shadows a let/const/class variable: '"), name, ASCIILiteral("'"));
				}
            }
            if (exportType == ExportType::Exported) {
                semanticFailIfFalse(exportName(*name), ASCIILiteral("Cannot export a duplicate name '"), name, ASCIILiteral("'"));
                m_moduleScopeData->exportBinding(*name);
            }

			if (hasInitializer) {
	            JSTextPosition varDivot = tokenStartPosition() + 1;
	            initStart = tokenStartPosition();
				next(TreeBuilder::DontBuildStrings()); // consume '='
				propagateError();
				TreeExpression initializer = parseAssignmentExpression(context);
				initEnd = lastTokenEndPosition();
				lastInitializer = initializer;
				failIfFalse(initializer, ASCIILiteral("Expected expression as the intializer for the variable '"), name, ASCIILiteral("'"));

				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, ASCIILiteral("const declared variable '"), name, ASCIILiteral("'"), ASCIILiteral(" must have an initializer"));
			    if (declarationType == DeclarationType::VarDeclaration)
                	node = context.createEmptyVarExpression(varStartLocation, *name);
                else
                	node = context.createEmptyLetExpression(varStartLocation, *name);
        	}
        } else {
            lastIdent = nullptr;
            TreeDestructuringPattern pattern;
			{
				bool allowsInOperator = true;
				SetForScope<bool> allowsInScope(m_allowsIn, allowsInOperator);
				pattern = parseDestructuringPattern(context, destructuringKindFromDeclarationType(declarationType), exportType, nullptr, nullptr, assignmentContext);
			}
			failIfFalse(pattern, ASCIILiteral("Cannot parse this destructuring pattern"));
            hasInitializer = match(EQUAL);
            failIfTrue(declarationListContext == VarDeclarationContext && !hasInitializer, ASCIILiteral("Expected an initializer in destructuring variable declaration"));
			lastPattern = pattern;
            if (hasInitializer) {
                next(TreeBuilder::DontBuildStrings()); // consume '='
                TreeExpression rhs = parseAssignmentExpression(context);
                propagateError();
                ASSERT(rhs);
                node = context.createDestructuringAssignment(location, pattern, rhs);
                lastInitializer = rhs;
            }
        }

        if (node) {
			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);
        }
    } while (match(COMMA));
    if (lastIdent)
    	lastPattern = context.createBindingLocation(lastIdentToken.m_location, *lastIdent, lastIdentToken.m_startPosition, lastIdentToken.m_endPosition, assignmentContext);

    return head;
}

template <typename LexerType>
bool Parser<LexerType>::declareRestOrNormalParameter(const Identifier& name, const Identifier** duplicateIdentifier)
{
	DeclarationResultMask declarationResult = declareParameter(&name);
    if ((declarationResult & DeclarationResult::InvalidStrictMode) && strictMode()) {
        semanticFailIfTrue(m_vm->propertyNames->arguments == name || m_vm->propertyNames->eval == name, ASCIILiteral("Cannot destructure to a parameter name '"), &name, ASCIILiteral("' in strict mode"));
        if (m_parserState.lastFunctionName && name == *m_parserState.lastFunctionName)
            semanticFail(ASCIILiteral("Cannot declare a parameter named '"), &name, ASCIILiteral("' as it shadows the name of a strict mode function"));
        semanticFailureDueToKeyword(ASCIILiteral("parameter name"));
        if (!m_lexer.isReparsingFunction() && hasDeclaredParameter(name))
            semanticFail(ASCIILiteral("Cannot declare a parameter named '"), &name, ASCIILiteral("' in strict mode as it has already been declared"));
        semanticFail(ASCIILiteral("Cannot declare a parameter named '"), &name, ASCIILiteral("' 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;
    }

    return true;
}

template <typename LexerType>
template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::createBindingPattern(TreeBuilder& context, DestructuringKind kind, ExportType exportType, const Identifier& name, JSToken token, AssignmentContext bindingContext, const Identifier** duplicateIdentifier)
{
    ASSERT(name.impl()->isAtomic() || name.impl()->isSymbol());
    switch (kind) {
    case DestructuringKind::DestructureToVariables: {
		DeclarationResultMask declarationResult = declareVariable(&name);
		failIfTrueIfStrict(declarationResult & DeclarationResult::InvalidStrictMode, ASCIILiteral("Cannot declare a variable named '"), &name, ASCIILiteral("' in strict mode"));
		if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration)
			internalFailWithMessage(false, ASCIILiteral("Cannot declare a var variable that shadows a let/const/class variable: '"), &name, ASCIILiteral("'"));
		break;
	}

	case DestructuringKind::DestructureToLet:
	case DestructuringKind::DestructureToConst:
	case DestructuringKind::DestructureToCatchParameters: {
		DeclarationResultMask declarationResult = declareVariable(&name, kind == DestructuringKind::DestructureToConst ? DeclarationType::ConstDeclaration : DeclarationType::LetDeclaration);
		if (declarationResult != DeclarationResult::Valid) {
			failIfTrueIfStrict(declarationResult & DeclarationResult::InvalidStrictMode, ASCIILiteral("Cannot destructure to a variable named '"), &name, ASCIILiteral("' in strict mode"));
			failIfTrue(declarationResult & DeclarationResult::InvalidDuplicateDeclaration, ASCIILiteral("Cannot declare a lexical variable twice: '"), &name, ASCIILiteral("'"));
		}
        break;
    }

    case DestructuringKind::DestructureToParameters: {
		declareRestOrNormalParameter(name, duplicateIdentifier);
		propagateError();
        break;
    }

    case DestructuringKind::DestructureToExpressions: {
        break;
    }
    }

    if (exportType == ExportType::Exported) {
        semanticFailIfFalse(exportName(name), ASCIILiteral("Cannot export a duplicate name '"), &name, ASCIILiteral("'"));
        m_moduleScopeData->exportBinding(name);
    }
    return context.createBindingLocation(token.m_location, name, m_token.m_startPosition, m_token.m_endPosition, bindingContext);
}

template <typename LexerType>
template <class TreeBuilder> NEVER_INLINE TreeDestructuringPattern Parser<LexerType>::createAssignmentElement(TreeBuilder& context, TreeExpression& assignmentTarget, const JSTextPosition& startPosition, const JSTextPosition& endPosition)
{
    return context.createAssignmentElement(assignmentTarget, startPosition, endPosition);
}

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, ASCIILiteral("Cannot parse the arrow function expression"));

    context.setEndOffset(expr, m_lastTokenEndPosition.offset);

    JSTextPosition end = tokenEndPosition();

    TreeSourceElements sourceElements = context.createSourceElements();
    TreeStatement body = context.createReturnStatement(location, expr, start, end);
    context.setEndOffset(body, m_lastTokenEndPosition.offset);
    context.appendStatement(sourceElements, body);

    return sourceElements;
}

template <typename LexerType>
template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::tryParseDestructuringPatternExpression(TreeBuilder& context, AssignmentContext bindingContext)
{
    return parseDestructuringPattern(context, DestructuringKind::DestructureToExpressions, ExportType::NotExported, 0, 0, bindingContext);
}

template <typename LexerType>
template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::parseBindingOrAssignmentElement(TreeBuilder& context, DestructuringKind kind, ExportType exportType, const Identifier** duplicateIdentifier, bool* hasDestructuringPattern, AssignmentContext bindingContext, int depth)
{
    if (kind == DestructuringKind::DestructureToExpressions)
        return parseAssignmentElement(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth);
    return parseDestructuringPattern(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth);
}

template <typename LexerType>
template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::parseObjectRestAssignmentElement(TreeBuilder& context)
{
    JSTextPosition startPosition = tokenStartPosition();
    auto element = parseMemberExpression(context);

    if (!element || !context.isAssignmentLocation(element)) {
        reclassifyExpressionError(ErrorIndicatesPattern, ErrorIndicatesNothing);
        semanticFail(ASCIILiteral("Invalid destructuring assignment target"));
    }

    if (strictMode() && m_parserState.lastIdentifier && context.isResolve(element)) {
        bool isEvalOrArguments = m_vm->propertyNames->eval == *m_parserState.lastIdentifier || m_vm->propertyNames->arguments == *m_parserState.lastIdentifier;
        if (isEvalOrArguments && strictMode())
            reclassifyExpressionError(ErrorIndicatesPattern, ErrorIndicatesNothing);
        failIfTrueIfStrict(isEvalOrArguments, ASCIILiteral("Cannot modify '"), m_parserState.lastIdentifier, ASCIILiteral("' in strict mode"));
    }

    return createAssignmentElement(context, element, startPosition, lastTokenEndPosition());
}

template <typename LexerType>
template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::parseAssignmentElement(TreeBuilder& context, DestructuringKind kind, ExportType exportType, const Identifier** duplicateIdentifier, bool* hasDestructuringPattern, AssignmentContext bindingContext, int depth)
{
    TreeDestructuringPattern assignmentTarget = 0;

    if (match(OPENBRACE) || match(OPENBRACKET)) {
        SavePoint savePoint = createSavePoint(context);
        assignmentTarget = parseDestructuringPattern(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth);
        if (assignmentTarget && !match(DOT) && !match(OPENBRACKET) && !match(OPENPAREN) && !match(BACKQUOTE))
            return assignmentTarget;
        restoreSavePoint(context, savePoint);
    }

	JSTextPosition startPosition = tokenStartPosition();
	auto element = parseMemberExpression(context);

	semanticFailIfFalse(element && context.isAssignmentLocation(element), ASCIILiteral("Invalid destructuring assignment target"));

    if (strictMode() && m_parserState.lastIdentifier && context.isResolve(element)) {
    	bool isEvalOrArguments = m_vm->propertyNames->eval == *m_parserState.lastIdentifier || m_vm->propertyNames->arguments == *m_parserState.lastIdentifier;
		failIfTrueIfStrict(isEvalOrArguments, ASCIILiteral("Cannot modify '"), m_parserState.lastIdentifier, ASCIILiteral("' in strict mode"));
	}

	return createAssignmentElement(context, element, startPosition, lastTokenEndPosition());
}

static ASCIILiteral destructuringKindToVariableKindName(DestructuringKind kind)
{
    switch (kind) {
    case DestructuringKind::DestructureToLet:
    case DestructuringKind::DestructureToConst:
        return ASCIILiteral("lexical variable name");
    case DestructuringKind::DestructureToVariables:
        return ASCIILiteral("variable name");
    case DestructuringKind::DestructureToParameters:
        return ASCIILiteral("parameter name");
    case DestructuringKind::DestructureToCatchParameters:
        return ASCIILiteral("catch parameter name");
    case DestructuringKind::DestructureToExpressions:
        return ASCIILiteral("expression name");
    }
    RELEASE_ASSERT_NOT_REACHED();
    return ASCIILiteral("invalid");
}

template <typename LexerType>
template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::parseObjectRestElement(TreeBuilder& context, DestructuringKind kind, ExportType exportType, const Identifier** duplicateIdentifier, AssignmentContext bindingContext)
{
    ASSERT(kind != DestructuringKind::DestructureToExpressions);
    failIfStackOverflow();
    TreeDestructuringPattern pattern;

    if (!matchSpecIdentifier()) {
        semanticFailureDueToKeyword(destructuringKindToVariableKindName(kind));
        failWithMessage(ASCIILiteral("Expected a binding element"));
    }
    failIfTrue(match(LET) && (kind == DestructuringKind::DestructureToLet || kind == DestructuringKind::DestructureToConst), ASCIILiteral("Cannot use 'let' as an identifier name for a LexicalDeclaration"));
    semanticFailIfTrue(isDisallowedIdentifierAwait(m_token), ASCIILiteral("Cannot use 'await' as a "), destructuringKindToVariableKindName(kind), ASCIILiteral(" "), disallowedIdentifierAwaitReason());
    pattern = createBindingPattern(context, kind, exportType, *m_token.m_data.ident, m_token, bindingContext, duplicateIdentifier);
    next();
    return pattern;
}

template <typename LexerType>
template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::parseObjectRestBindingOrAssignmentElement(TreeBuilder& context, DestructuringKind kind, ExportType exportType, const Identifier** duplicateIdentifier, AssignmentContext bindingContext)
{
    if (kind == DestructuringKind::DestructureToExpressions)
        return parseObjectRestAssignmentElement(context);
    return parseObjectRestElement(context, kind, exportType, duplicateIdentifier, bindingContext);
}

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)
{
    failIfStackOverflow();
    m_parserState.assignmentCount++;
    SetForScope<int> nonLHSCountScope(m_parserState.nonLHSCount);
    TreeDestructuringPattern pattern;
    switch (m_token.m_type) {
    case OPENBRACKET: {
        JSTextPosition divotStart = tokenStartPosition();
    	typename TreeBuilder::ArrayPattern arrayPattern = context.createArrayPattern(m_token.m_location);
        next();

        if (hasDestructuringPattern)
            *hasDestructuringPattern = true;

        bool restElementWasFound = false;

        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 = parseBindingOrAssignmentElement(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1);
                if (kind == DestructuringKind::DestructureToExpressions && !innerPattern)
                    return 0;
                failIfFalse(innerPattern, ASCIILiteral("Cannot parse this destructuring pattern"));
                context.appendArrayPatternRestEntry(arrayPattern, location, innerPattern);
                restElementWasFound = true;
                break;
            }

            JSTokenLocation location(tokenLocation());
            TreeDestructuringPattern innerPattern = parseBindingOrAssignmentElement(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1);
            if (kind == DestructuringKind::DestructureToExpressions && !innerPattern)
				return 0;
            failIfFalse(innerPattern, ASCIILiteral("Cannot parse this destructuring pattern"));
            propagateError();
            TreeExpression defaultValue = parseDefaultValueForDestructuringPattern(context);
            propagateError();
			context.appendArrayPatternEntry(arrayPattern, location, innerPattern, defaultValue);
        } while (consume(COMMA));

        consumeOrFail(CLOSEBRACKET, restElementWasFound ? ASCIILiteral("Expected a closing ']' following a rest element destructuring pattern") : ASCIILiteral("Expected either a closing ']' or a ',' following an element destructuring pattern"));
		context.finishArrayPattern(arrayPattern, divotStart, divotStart, lastTokenEndPosition());
		pattern = arrayPattern;
        break;
    }
    case OPENBRACE: {
        JSTextPosition divotStart = tokenStartPosition();
        auto objectPattern = context.createObjectPattern(m_token.m_location);
        next();

        if (hasDestructuringPattern)
            *hasDestructuringPattern = true;

        bool restElementWasFound = false;

        do {
            bool wasString = false;

            if (match(CLOSEBRACE))
                break;

            if (match(DOTDOTDOT) && m_useObjectRestSpread) {
                JSTokenLocation location = m_token.m_location;
                next();
                auto innerPattern = parseObjectRestBindingOrAssignmentElement(context, kind, exportType, duplicateIdentifier, bindingContext);
                propagateError();
                if (!innerPattern)
                    return 0;
                context.appendObjectPatternRestEntry(*m_vm, objectPattern, location, innerPattern);
                restElementWasFound = true;
                context.setContainsObjectRestElement(objectPattern, restElementWasFound);
                break;
            }

            const Identifier* propertyName = 0;
            TreeExpression propertyExpression = 0;
            TreeDestructuringPattern innerPattern = 0;
            JSTokenLocation location(tokenLocation());
            bool escapedKeyword = match(ESCAPED_KEYWORD);
            if (escapedKeyword || matchSpecIdentifier()) {
            	failIfTrue(match(LET) && (kind == DestructuringKind::DestructureToLet || kind == DestructuringKind::DestructureToConst), ASCIILiteral("Cannot use 'let' as an identifier name for a LexicalDeclaration"));
				propertyName = m_token.m_data.ident;
                JSToken identifierToken = m_token;
                next();
                if (consume(COLON))
                    innerPattern = parseBindingOrAssignmentElement(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1);
                else {
                	semanticFailIfTrue(escapedKeyword, "Cannot use abbreviated destructuring syntax for keyword '", propertyName->impl(), "'");
                	semanticFailIfTrue(isDisallowedIdentifierAwait(identifierToken), ASCIILiteral("Cannot use 'await' as a "), destructuringKindToVariableKindName(kind), ASCIILiteral(" "), disallowedIdentifierAwaitReason());
					if (kind == DestructuringKind::DestructureToExpressions) {
                        bool isEvalOrArguments = m_vm->propertyNames->eval == *propertyName || m_vm->propertyNames->arguments == *propertyName;
                        if (isEvalOrArguments && strictMode())
                            reclassifyExpressionError(ErrorIndicatesPattern, ErrorIndicatesNothing);
                        failIfTrueIfStrict(isEvalOrArguments, ASCIILiteral("Cannot modify '"), propertyName, ASCIILiteral("' in strict mode"));

                        if (match(EQUAL))
                            currentScope()->useVariable(propertyName, m_vm->propertyNames->eval == *propertyName);
                    }
                    innerPattern = createBindingPattern(context, kind, exportType, *propertyName, identifierToken, bindingContext, duplicateIdentifier);
                }
            } 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);
					break;
                case STRING:
                    propertyName = m_token.m_data.ident;
                    wasString = true;
                    break;
                case BIGINT:
                    propertyName = &m_parserArena.identifierArena().makeBigIntDecimalIdentifier(const_cast<VM&>(*m_vm), *m_token.m_data.bigIntString, m_token.m_data.radix);
                    break;
                case OPENBRACKET:
                    next();
                    propertyExpression = parseAssignmentExpression(context);
                    failIfFalse(propertyExpression, ASCIILiteral("Cannot parse computed property name"));
                    matchOrFail(CLOSEBRACKET, ASCIILiteral("Expected ']' to end end a computed property name"));
                    break;
                default:
                    if (m_token.m_type != RESERVED && m_token.m_type != RESERVED_IF_STRICT && !(m_token.m_type & KeywordTokenFlag)) {
                        if (kind == DestructuringKind::DestructureToExpressions)
                            return 0;
                        failWithMessage(ASCIILiteral("Expected a property name"));
                    }
                    propertyName = m_token.m_data.ident;
                    break;
                }
                next();
                if (!consume(COLON)) {
                    if (kind == DestructuringKind::DestructureToExpressions)
                        return 0;
                    semanticFailIfTrue(tokenType == RESERVED, ASCIILiteral("Cannot use abbreviated destructuring syntax for reserved name '"), propertyName, ASCIILiteral("'"));
                    semanticFailIfTrue(tokenType == RESERVED_IF_STRICT, ASCIILiteral("Cannot use abbreviated destructuring syntax for reserved name '"), propertyName, ASCIILiteral("' in strict mode"));
                    semanticFailIfTrue(tokenType & KeywordTokenFlag, ASCIILiteral("Cannot use abbreviated destructuring syntax for keyword '"), propertyName, ASCIILiteral("'"));

                    failWithMessage(ASCIILiteral("Expected a ':' prior to a named destructuring property"));
                }
                innerPattern = parseBindingOrAssignmentElement(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1);
            }
            if (kind == DestructuringKind::DestructureToExpressions && !innerPattern)
                return 0;
            failIfFalse(innerPattern, ASCIILiteral("Cannot parse this destructuring pattern"));
            propagateError();
            TreeExpression defaultValue = parseDefaultValueForDestructuringPattern(context);
            propagateError();
            if (propertyExpression) {
                context.appendObjectPatternEntry(*m_vm, objectPattern, location, propertyExpression, innerPattern, defaultValue);
                context.setContainsComputedProperty(objectPattern, true);
            } else {
				ASSERT(propertyName);
				context.appendObjectPatternEntry(objectPattern, location, wasString, *propertyName, innerPattern, defaultValue);
			}
        } while (consume(COMMA));

        if (kind == DestructuringKind::DestructureToExpressions && !match(CLOSEBRACE))
            return 0;
        consumeOrFail(CLOSEBRACE, restElementWasFound ? ASCIILiteral("Expected a closing '}' following a rest element destructuring pattern") : ASCIILiteral("Expected either a closing '}' or an ',' after a property destructuring pattern"));
        context.finishObjectPattern(objectPattern, divotStart, divotStart, lastTokenEndPosition());
        pattern = objectPattern;
        break;
    }

    default: {
        if (!matchSpecIdentifier()) {
            if (kind == DestructuringKind::DestructureToExpressions)
                return 0;
            semanticFailureDueToKeyword(destructuringKindToVariableKindName(kind));
            if (kind != DestructuringKind::DestructureToParameters)
				failIfTrue(match(PRIVATENAME), "Cannot use a private name as a ", destructuringKindToVariableKindName(kind));
			failWithMessage(ASCIILiteral("Expected a parameter pattern or a ')' in parameter list"));
        }
        failIfTrue(match(LET) && (kind == DestructuringKind::DestructureToLet || kind == DestructuringKind::DestructureToConst), ASCIILiteral("Cannot use 'let' as an identifier name for a LexicalDeclaration"));
        semanticFailIfTrue(isDisallowedIdentifierAwait(m_token), ASCIILiteral("Cannot use 'await' as a "), destructuringKindToVariableKindName(kind), ASCIILiteral(" "), disallowedIdentifierAwaitReason());
		pattern = createBindingPattern(context, kind, exportType, *m_token.m_data.ident, m_token, bindingContext, duplicateIdentifier);
        next();
        break;
    }
    }
    return pattern;
}

template <typename LexerType>
template <class TreeBuilder> TreeExpression Parser<LexerType>::parseDefaultValueForDestructuringPattern(TreeBuilder& context)
{
    if (!match(EQUAL))
        return 0;

    next(TreeBuilder::DontBuildStrings()); // consume '='
    return parseAssignmentExpression(context);
}

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseForStatement(TreeBuilder& context)
{
    ASSERT(match(FOR));
    JSTokenLocation location(tokenLocation());
    int startLine = tokenLine();
    bool isAwaitFor = false;
    next();

    DepthManager statementDepth(&m_statementDepth);
    m_statementDepth++;

    if (Options::useAsyncIterator() && match(AWAIT)) {
        semanticFailIfFalse(currentScope()->isAsyncFunction() || (isModuleParseMode(sourceParseMode()) && Options::useTopLevelAwait()), ASCIILiteral("for-await-of can only be used in an async function or async generator"));
        isAwaitFor = true;
        next();
    }

    handleProductionOrFail(OPENPAREN, ASCIILiteral("("), ASCIILiteral("start"), ASCIILiteral("for-loop header"));
    int nonLHSCount = m_parserState.nonLHSCount;
    int declarations = 0;
    JSTextPosition declsStart = tokenStartPosition();
    TreeExpression decls = 0;
    TreeDestructuringPattern pattern = 0;
    bool isVarDeclaration = match(VAR);
    bool isLetDeclaration = match(LET);
    bool isConstDeclaration = match(CONSTTOKEN);
    bool forLoopConstDoesNotHaveInitializer = false;
    bool forLoopinitializerContainsClosure = false;

    AutoCleanupLexicalScope lexicalScope;

    auto popLexicalScopeIfNecessary = [&]() -> VariableEnvironment {
        if (isLetDeclaration || isConstDeclaration) {
        	ScopePair scopePair = popScope(lexicalScope, TreeBuilder::NeedsFreeVariableInfo);
            return WTFMove(scopePair.lexicalEnvironment);
        }
        return VariableEnvironment();
    };

    if (isVarDeclaration || isLetDeclaration || isConstDeclaration) {
        /*
         for (var/let/const IDENT in/of expression) statement
         for (var/let/const varDeclarationList; expressionOpt; expressionOpt)
         */
        if (isLetDeclaration || isConstDeclaration) {
            ScopeRef newScope = pushScope();
            newScope->setIsLexicalScope();
            newScope->preventVarDeclarations();
            lexicalScope.setIsValid(newScope, this);
        }

    	TreeDestructuringPattern forInTarget = 0;
        TreeExpression forInInitializer = 0;
        m_allowsIn = false;
        JSTextPosition initStart;
        JSTextPosition initEnd;
        DeclarationType declarationType;
        if (isVarDeclaration)
            declarationType = DeclarationType::VarDeclaration;
        else if (isLetDeclaration)
            declarationType = DeclarationType::LetDeclaration;
        else if (isConstDeclaration)
            declarationType = DeclarationType::ConstDeclaration;
        else
            RELEASE_ASSERT_NOT_REACHED();
        unsigned candidateCountBeforeInitializer = currentScope()->closedVariableCandidates().size();
        decls = parseVariableDeclarationList(context, declarations, forInTarget, forInInitializer, declsStart, initStart, initEnd, ForLoopContext, declarationType, ExportType::NotExported, forLoopConstDoesNotHaveInitializer);
        forLoopinitializerContainsClosure = currentScope()->closedVariableCandidates().size() > candidateCountBeforeInitializer;
		m_allowsIn = true;
        propagateError();
        
        // Remainder of a standard for loop is handled identically
        if (match(SEMICOLON))
            goto standardForLoop;
        
        failIfFalse(declarations == 1, ASCIILiteral("can only declare a single variable in an enumeration"));

        // Handle for-in with var declaration
        JSTextPosition inLocation = tokenStartPosition();
        bool isOfEnumeration = false;
        if (!match(INTOKEN)) {
			failIfFalse(matchContextualKeyword(m_vm->propertyNames->of), ASCIILiteral("Expected either 'in' or 'of' in enumeration syntax"));
			isOfEnumeration = true;
			next();
        } else {
            failIfFalse(!isAwaitFor, ASCIILiteral("Expected 'of' in for-await syntax"));
            next();
        }

        bool hasAnyAssignments = !!forInInitializer;
        if (hasAnyAssignments) {
            if (isOfEnumeration)
                internalFailWithMessage(false, ASCIILiteral("Cannot assign to the loop variable inside a for-of loop header"));
            if (strictMode() || (isLetDeclaration || isConstDeclaration) || !context.isBindingNode(forInTarget))
                internalFailWithMessage(false, ASCIILiteral("Cannot assign to the loop variable inside a for-in loop header"));
        }

        // While for-in uses Expression, for-of / for-await-of use AssignmentExpression.
        // https://tc39.es/ecma262/#sec-for-in-and-for-of-statements
        TreeExpression expr = 0;
        if (isOfEnumeration)
            expr = parseAssignmentExpression(context);
        else
            expr = parseExpression(context);
        failIfFalse(expr, ASCIILiteral("Expected expression to enumerate"));
        JSTextPosition exprEnd = lastTokenEndPosition();
        
        int endLine = tokenLine();
        
        handleProductionOrFail(CLOSEPAREN, ASCIILiteral(")"), ASCIILiteral("end"), (isOfEnumeration ? ASCIILiteral("for-of header") : ASCIILiteral("for-in header")));

        const Identifier* unused = nullptr;
        startLoop();
        TreeStatement statement = parseStatement(context, unused);
        endLoop();
        failIfFalse(statement, ASCIILiteral("Expected statement as body of for-"), isOfEnumeration ? ASCIILiteral("of") : ASCIILiteral("in"), ASCIILiteral(" statement"));
        VariableEnvironment lexicalVariables = popLexicalScopeIfNecessary();
		if (isOfEnumeration)
			return context.createForOfLoop(isAwaitFor, location, forInTarget, expr, statement, declsStart, inLocation, exprEnd, startLine, endLine, WTFMove(lexicalVariables));
		ASSERT(!isAwaitFor);
        if (isVarDeclaration && forInInitializer)
        	return context.createForInLoop(location, decls, expr, statement, declsStart, inLocation, exprEnd, startLine, endLine, WTFMove(lexicalVariables));
        return context.createForInLoop(location, forInTarget, expr, statement, declsStart, inLocation, exprEnd, startLine, endLine, WTFMove(lexicalVariables));
	}
    
    if (!match(SEMICOLON)) {
        if (match(OPENBRACE) || match(OPENBRACKET)) {
            SavePoint savePoint = createSavePoint(context);
            pattern = tryParseDestructuringPatternExpression(context, AssignmentContext::AssignmentExpression);
            if (pattern && (match(INTOKEN) || matchContextualKeyword(m_vm->propertyNames->of)))
                goto enumerationLoop;
            pattern = 0;
            restoreSavePoint(context, savePoint);
        }
        m_allowsIn = false;
        decls = parseExpression(context);
        m_allowsIn = true;
        failIfFalse(decls, ASCIILiteral("Cannot parse for loop declarations"));
    }
    
    if (match(SEMICOLON)) {
    standardForLoop:
    	failIfFalse(!isAwaitFor, ASCIILiteral("Unexpected a ';' in for-await-of header"));
        // Standard for loop
        next();
        TreeExpression condition = 0;
        failIfTrue(forLoopConstDoesNotHaveInitializer && isConstDeclaration, ASCIILiteral("const variables in for loops must have initializers"));
        
        if (!match(SEMICOLON)) {
            condition = parseExpression(context);
            failIfFalse(condition, ASCIILiteral("Cannot parse for loop condition expression"));
		}
		consumeOrFail(SEMICOLON, ASCIILiteral("Expected a ';' after the for loop condition expression"));
        
        TreeExpression increment = 0;
        if (!match(CLOSEPAREN)) {
            increment = parseExpression(context);
            failIfFalse(increment, ASCIILiteral("Cannot parse for loop iteration expression"));
        }
        int endLine = tokenLine();
        handleProductionOrFail(CLOSEPAREN, ASCIILiteral(")"), ASCIILiteral("end"), ASCIILiteral("for-loop header"));
        const Identifier* unused = nullptr;
        startLoop();
        TreeStatement statement = parseStatement(context, unused);
        endLoop();
        failIfFalse(statement, ASCIILiteral("Expected a statement as the body of a for loop"));
        VariableEnvironment lexicalVariables = popLexicalScopeIfNecessary();
		return context.createForLoop(location, decls, condition, increment, statement, startLine, endLine, WTFMove(lexicalVariables), forLoopinitializerContainsClosure);
	}

	// For-in and For-of loop
enumerationLoop:
    failIfFalse(nonLHSCount == m_parserState.nonLHSCount, ASCIILiteral("Expected a reference on the left hand side of an enumeration statement"));
    bool isOfEnumeration = false;
    JSTextPosition inLocation = tokenStartPosition();
    if (!match(INTOKEN)) {
        failIfFalse(matchContextualKeyword(m_vm->propertyNames->of), ASCIILiteral("Expected either 'in' or 'of' in enumeration syntax"));
        isOfEnumeration = true;
        next();
    } else {
        failIfFalse(!isAwaitFor, ASCIILiteral("Expected 'of' in for-await syntax"));
        next();
    }

    // While for-in uses Expression, for-of / for-await-of use AssignmentExpression.
    // https://tc39.es/ecma262/#sec-for-in-and-for-of-statements
    TreeExpression expr = 0;
    if (isOfEnumeration)
        expr = parseAssignmentExpression(context);
    else
        expr = parseExpression(context);
    failIfFalse(expr, ASCIILiteral("Cannot parse subject for-"), isOfEnumeration ? ASCIILiteral("of") : ASCIILiteral("in"), ASCIILiteral(" statement"));
    JSTextPosition exprEnd = lastTokenEndPosition();
    int endLine = tokenLine();

    handleProductionOrFail(CLOSEPAREN, ASCIILiteral(")"), ASCIILiteral("end"), (isOfEnumeration ? ASCIILiteral("for-of header") : ASCIILiteral("for-in header")));
	const Identifier* unused = nullptr;
    startLoop();
    TreeStatement statement = parseStatement(context, unused);
    endLoop();
    failIfFalse(statement, ASCIILiteral("Expected a statement as the body of a for-"), isOfEnumeration ? ASCIILiteral("of") : ASCIILiteral("in"), ASCIILiteral(" loop"));
	if (pattern) {
		ASSERT(!decls);
        VariableEnvironment lexicalVariables = popLexicalScopeIfNecessary();
		if (isOfEnumeration)
			return context.createForOfLoop(isAwaitFor, location, pattern, expr, statement, declsStart, inLocation, exprEnd, startLine, endLine, WTFMove(lexicalVariables));
		ASSERT(!isAwaitFor);
		return context.createForInLoop(location, pattern, expr, statement, declsStart, inLocation, exprEnd, startLine, endLine, WTFMove(lexicalVariables));
	}

	semanticFailIfFalse(isSimpleAssignmentTarget(context, decls), ASCIILiteral("Left side of assignment is not a reference"));

	VariableEnvironment lexicalVariables = popLexicalScopeIfNecessary();
	if (isOfEnumeration)
		return context.createForOfLoop(isAwaitFor, location, decls, expr, statement, declsStart, inLocation, exprEnd, startLine, endLine, WTFMove(lexicalVariables));
	ASSERT(!isAwaitFor);
	return context.createForInLoop(location, decls, expr, statement, declsStart, inLocation, exprEnd, startLine, endLine, WTFMove(lexicalVariables));
}

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseBreakStatement(TreeBuilder& context)
{
    ASSERT(match(BREAK));
    JSTokenLocation location(tokenLocation());
    JSTextPosition start = tokenStartPosition();
    JSTextPosition end = tokenEndPosition();
    next();

    Optional<bool> isBreakValid;
    if (UNLIKELY(currentScope()->isStaticBlock())) {
        isBreakValid = breakIsValid();
        semanticFailIfTrue(!currentScope()->breakIsValid() && !isBreakValid.value(), ASCIILiteral("'break' cannot cross static block boundary"));
    }
    
    if (autoSemiColon()) {
    	semanticFailIfFalse(isBreakValid.value_or(breakIsValid()), ASCIILiteral("'break' is only valid inside a switch or loop statement"));
        return context.createBreakStatement(location, &m_vm->propertyNames->nullIdentifier, start, end);
    }
    failIfFalse(matchSpecIdentifier(), ASCIILiteral("Expected an identifier as the target for a break statement"));
	const Identifier* ident = m_token.m_data.ident;
	semanticFailIfFalse(getLabel(ident), ASCIILiteral("Cannot use the undeclared label '"), ident, ASCIILiteral("'"));
	end = tokenEndPosition();
    next();
    failIfFalse(autoSemiColon(), ASCIILiteral("Expected a ';' following a targeted break statement"));
	return context.createBreakStatement(location, ident, start, end);
}

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseContinueStatement(TreeBuilder& context)
{
    ASSERT(match(CONTINUE));
    JSTokenLocation location(tokenLocation());
    JSTextPosition start = tokenStartPosition();
    JSTextPosition end = tokenEndPosition();
    next();

    Optional<bool> isContinueValid;
    if (UNLIKELY(currentScope()->isStaticBlock())) {
        isContinueValid = continueIsValid();
        semanticFailIfTrue(!currentScope()->continueIsValid() && !isContinueValid.value(), ASCIILiteral("'continue' cannot cross static block boundary"));
    }
    
    if (autoSemiColon()) {
    	semanticFailIfFalse(isContinueValid.value_or(continueIsValid()), ASCIILiteral("'continue' is only valid inside a loop statement"));
        return context.createContinueStatement(location, &m_vm->propertyNames->nullIdentifier, start, end);
    }
    failIfFalse(matchSpecIdentifier(), ASCIILiteral("Expected an identifier as the target for a continue statement"));
    const Identifier* ident = m_token.m_data.ident;
    ScopeLabelInfo* label = getLabel(ident);
    semanticFailIfFalse(label, ASCIILiteral("Cannot use the undeclared label '"), ident, ASCIILiteral("'"));
	semanticFailIfFalse(label->m_isLoop, ASCIILiteral("Cannot continue to the label '"), ident, ASCIILiteral("' as it is not targeting a loop"));
	end = tokenEndPosition();
    next();
    failIfFalse(autoSemiColon(), ASCIILiteral("Expected a ';' following a targeted continue statement"));
    return context.createContinueStatement(location, ident, start, end);
}

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseReturnStatement(TreeBuilder& context)
{
    ASSERT(match(RETURN));
    m_parserState.returnStatementCount++;
    JSTokenLocation location(tokenLocation());
    semanticFailIfFalse(currentScope()->isFunction() && !currentScope()->isStaticBlock(), ASCIILiteral("Return statements are only valid inside functions"));
    JSTextPosition start = tokenStartPosition();
    JSTextPosition end = tokenEndPosition();
    next();
    // We do the auto semicolon check before attempting to parse expression
    // as we need to ensure the a line break after the return correctly terminates
    // the statement
    if (match(SEMICOLON))
        end = tokenEndPosition();
    if (autoSemiColon())
        return context.createReturnStatement(location, 0, start, end);
    TreeExpression expr = parseExpression(context);
    failIfFalse(expr, ASCIILiteral("Cannot parse the return expression"));
    end = lastTokenEndPosition();
    if (match(SEMICOLON))
        end  = tokenEndPosition();
    if (!autoSemiColon())
        failWithMessage(ASCIILiteral("Expected a ';' following a return statement"));
    return context.createReturnStatement(location, expr, start, end);
}

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseThrowStatement(TreeBuilder& context)
{
    ASSERT(match(THROW));
    JSTokenLocation location(tokenLocation());
    JSTextPosition start = tokenStartPosition();
    next();
    
    failIfTrue(match(SEMICOLON), ASCIILiteral("Expected expression after 'throw'"));
	semanticFailIfTrue(autoSemiColon(), ASCIILiteral("Cannot have a newline after 'throw'"));
    
    TreeExpression expr = parseExpression(context);
    failIfFalse(expr, ASCIILiteral("Cannot parse expression for throw statement"));
    JSTextPosition end = lastTokenEndPosition();
    failIfFalse(autoSemiColon(), ASCIILiteral("Expected a ';' after a throw statement"));
    
    return context.createThrowStatement(location, expr, start, end);
}

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseWithStatement(TreeBuilder& context)
{
    ASSERT(match(WITH));
    JSTokenLocation location(tokenLocation());
    semanticFailIfTrue(strictMode(), ASCIILiteral("'with' statements are not valid in strict mode"));
    currentScope()->setNeedsFullActivation();
    int startLine = tokenLine();
    next();

    handleProductionOrFail(OPENPAREN, ASCIILiteral("("), ASCIILiteral("start"), ASCIILiteral("subject of a 'with' statement"));
	int start = tokenStart();
    TreeExpression expr = parseExpression(context);
    failIfFalse(expr, ASCIILiteral("Cannot parse 'with' subject expression"));
    JSTextPosition end = lastTokenEndPosition();
    int endLine = tokenLine();
    handleProductionOrFail(CLOSEPAREN, ASCIILiteral(")"), ASCIILiteral("start"), ASCIILiteral("subject of a 'with' statement"));
	const Identifier* unused = 0;
    TreeStatement statement = parseStatement(context, unused);
    failIfFalse(statement, ASCIILiteral("A 'with' statement must have a body"));
    
    return context.createWithStatement(location, expr, statement, start, end, startLine, endLine);
}

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseSwitchStatement(TreeBuilder& context)
{
    ASSERT(match(SWITCH));
    JSTokenLocation location(tokenLocation());
    int startLine = tokenLine();
    next();
    handleProductionOrFail(OPENPAREN, ASCIILiteral("("), ASCIILiteral("start"), ASCIILiteral("subject of a 'switch'"));
    TreeExpression expr = parseExpression(context);
    failIfFalse(expr, ASCIILiteral("Cannot parse switch subject expression"));
    int endLine = tokenLine();

    handleProductionOrFail(CLOSEPAREN, ASCIILiteral(")"), ASCIILiteral("end"), ASCIILiteral("subject of a 'switch'"));
    handleProductionOrFail(OPENBRACE, ASCIILiteral("{"), ASCIILiteral("start"), ASCIILiteral("body of a 'switch'"));
    AutoPopScopeRef lexicalScope(this, pushScope());
    lexicalScope->setIsLexicalScope();
    lexicalScope->preventVarDeclarations();
    startSwitch();
    TreeClauseList firstClauses = parseSwitchClauses(context);
    propagateError();
    
    TreeClause defaultClause = parseSwitchDefaultClause(context);
    propagateError();
    
    TreeClauseList secondClauses = parseSwitchClauses(context);
    propagateError();
    endSwitch();
    handleProductionOrFail(CLOSEBRACE, ASCIILiteral("}"), ASCIILiteral("end"), ASCIILiteral("body of a 'switch'"));
    
    ScopePair scopePair = popScope(lexicalScope, TreeBuilder::NeedsFreeVariableInfo);
    return context.createSwitchStatement(location, expr, firstClauses, defaultClause, secondClauses, startLine, endLine, WTFMove(scopePair.lexicalEnvironment), WTFMove(scopePair.functionDeclarations));
}

template <typename LexerType>
template <class TreeBuilder> TreeClauseList Parser<LexerType>::parseSwitchClauses(TreeBuilder& context)
{
    if (!match(CASE))
        return 0;
    next();
    TreeExpression condition = parseExpression(context);
    failIfFalse(condition, ASCIILiteral("Cannot parse switch clause"));
    consumeOrFail(COLON, ASCIILiteral("Expected a ':' after switch clause expression"));
    TreeSourceElements statements = parseSourceElements(context, DontCheckForStrictMode);
    failIfFalse(statements, ASCIILiteral("Cannot parse the body of a switch clause"));
    TreeClause clause = context.createClause(condition, statements);
    failIfFalse(clause, ASCIILiteral("Cannot create clause"));
    TreeClauseList clauseList = context.createClauseList(clause);
    failIfFalse(clauseList, ASCIILiteral("Cannot create clause list"));
    TreeClauseList tail = clauseList;
    
    while (match(CASE)) {
        next();
        TreeExpression condition = parseExpression(context);
        failIfFalse(condition, ASCIILiteral("Cannot parse switch case expression"));
		consumeOrFail(COLON, ASCIILiteral("Expected a ':' after switch clause expression"));
        TreeSourceElements statements = parseSourceElements(context, DontCheckForStrictMode);
        failIfFalse(statements, ASCIILiteral("Cannot parse the body of a switch clause"));
        clause = context.createClause(condition, statements);
        failIfFalse(clause, ASCIILiteral("Cannot create clause"));
        tail = context.createClauseList(tail, clause);
        failIfFalse(clauseList, ASCIILiteral("Cannot create clause list"));
    }
    return clauseList;
}

template <typename LexerType>
template <class TreeBuilder> TreeClause Parser<LexerType>::parseSwitchDefaultClause(TreeBuilder& context)
{
    if (!match(DEFAULT))
        return 0;
    next();
    consumeOrFail(COLON, ASCIILiteral("Expected a ':' after switch default clause"));
    TreeSourceElements statements = parseSourceElements(context, DontCheckForStrictMode);
    failIfFalse(statements, ASCIILiteral("Cannot parse the body of a switch default clause"));
    return context.createClause(0, statements);
}

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseTryStatement(TreeBuilder& context)
{
    ASSERT(match(TRY));
    JSTokenLocation location(tokenLocation());
    TreeStatement tryBlock = 0;
    TreeDestructuringPattern catchPattern = 0;
    TreeStatement catchBlock = 0;
    TreeStatement finallyBlock = 0;
    int firstLine = tokenLine();
    next();
    matchOrFail(OPENBRACE, ASCIILiteral("Expected a block statement as body of a try statement"));

    int returnStatementCountBeforeTryBlock = m_parserState.returnStatementCount;
    tryBlock = parseBlockStatement(context);
    failIfFalse(tryBlock, ASCIILiteral("Cannot parse the body of try block"));
    bool tryBlockContainsReturn = m_parserState.returnStatementCount != returnStatementCountBeforeTryBlock;
	int lastLine = m_lastTokenEndPosition.line;
    VariableEnvironment catchEnvironment;
    DeclarationStacks::FunctionStack functionStack;
    if (match(CATCH)) {
        next();

        if (match(OPENBRACE)) {
            catchBlock = parseBlockStatement(context);
            failIfFalse(catchBlock, ASCIILiteral("Unable to parse 'catch' block"));
        } else {
			handleProductionOrFail(OPENPAREN, ASCIILiteral("("), ASCIILiteral("start"), ASCIILiteral("'catch' target"));
			DepthManager statementDepth(&m_statementDepth);
			semanticFailIfTrue(currentScope()->isStaticBlock() && match(AWAIT), ASCIILiteral("Cannot use 'await' as identifier within static block"));
			m_statementDepth++;
			AutoPopScopeRef catchScope(this, pushScope());
			catchScope->setIsLexicalScope();
			catchScope->preventVarDeclarations();
			const Identifier* ident = nullptr;
			if (matchSpecIdentifier()) {
                catchScope->setIsSimpleCatchParameterScope();
				ident = m_token.m_data.ident;
				catchPattern = context.createBindingLocation(m_token.m_location, *ident, m_token.m_startPosition, m_token.m_endPosition, AssignmentContext::DeclarationStatement);
				next();
				failIfTrueIfStrict(catchScope->declareLexicalVariable(ident, false) & DeclarationResult::InvalidStrictMode, ASCIILiteral("Cannot declare a catch variable named '"), ident, ASCIILiteral("' in strict mode"));
			} else {
				catchPattern = parseDestructuringPattern(context, DestructuringKind::DestructureToCatchParameters, ExportType::NotExported);
				failIfFalse(catchPattern, ASCIILiteral("Cannot parse this destructuring pattern"));
			}
			handleProductionOrFail(CLOSEPAREN, ASCIILiteral(")"), ASCIILiteral("end"), ASCIILiteral("'catch' target"));
			matchOrFail(OPENBRACE, ASCIILiteral("Expected exception handler to be a block statement"));
			catchBlock = parseBlockStatement(context, BlockType::CatchBlock);
			failIfFalse(catchBlock, ASCIILiteral("Unable to parse 'catch' block"));
			ScopePair scopePair = popScope(catchScope, TreeBuilder::NeedsFreeVariableInfo);
			catchEnvironment = WTFMove(scopePair.lexicalEnvironment);
            functionStack = WTFMove(scopePair.functionDeclarations);
            ASSERT(functionStack.isEmpty());
			RELEASE_ASSERT(!ident || (catchEnvironment.size() == 1 && catchEnvironment.contains(ident->impl())));
		}
    }
    
    if (match(FINALLY)) {
        next();
        matchOrFail(OPENBRACE, ASCIILiteral("Expected block statement for finally body"));
		finallyBlock = parseBlockStatement(context);
		failIfFalse(finallyBlock, ASCIILiteral("Cannot parse finally body"));
	}
	failIfFalse(catchBlock || finallyBlock, ASCIILiteral("Try statements must have at least a catch or finally block"));

    if (tryBlockContainsReturn && !finallyBlock && currentFunctionScope()->constructorKind() == ConstructorKind::Extends) {
        // Empty `finally` statement is necessary to prevent BytecodeGenerator::emitReturn() from being
        // called inside the `try` block, which would otherwise result in errors thrown at steps 10-12
        // of https://tc39.es/ecma262/#sec-ecmascript-function-objects-construct-argumentslist-newtarget
        // being caught by the `catch` block.
        finallyBlock = context.createEmptyStatement(location);
    }

    return context.createTryStatement(location, tryBlock, catchPattern, catchBlock, finallyBlock, firstLine, lastLine, WTFMove(catchEnvironment));
}

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseDebuggerStatement(TreeBuilder& context)
{
    ASSERT(match(DEBUGGER));
    JSTokenLocation location(tokenLocation());
    int startLine = tokenLine();
    int endLine = startLine;
    next();
    if (match(SEMICOLON))
        startLine = tokenLine();
    failIfFalse(autoSemiColon(), ASCIILiteral("Debugger keyword must be followed by a ';'"));
	return context.createDebugger(location, startLine, endLine);
}

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseBlockStatement(TreeBuilder& context, BlockType type)
{
    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 || type == BlockType::StaticBlock;
    if (shouldPushLexicalScope) {
        ScopeRef newScope = pushScope();
        newScope->setIsLexicalScope();
        switch (type) {
        case BlockType::CatchBlock:
            newScope->setIsCatchBlockScope();
            newScope->preventVarDeclarations();
            break;
        case BlockType::StaticBlock:
            newScope->setIsStaticBlock();
            break;
        case BlockType::Normal:
            newScope->preventVarDeclarations();
            break;
        default:
            RELEASE_ASSERT_NOT_REACHED();
            break;
        }
        lexicalScope.setIsValid(newScope, this);
    }
    JSTokenLocation location(tokenLocation());
    int startOffset = m_token.m_data.offset;
    int start = tokenLine();
    VariableEnvironment lexicalEnvironment;
    DeclarationStacks::FunctionStack functionStack;
    next();
    if (match(CLOSEBRACE)) {
        int endOffset = m_token.m_data.offset;
        next();
        if (shouldPushLexicalScope) {
        	ScopePair scopePair = popScope(lexicalScope, TreeBuilder::NeedsFreeVariableInfo);
            lexicalEnvironment = WTFMove(scopePair.lexicalEnvironment);
            functionStack = WTFMove(scopePair.functionDeclarations);
        }
        TreeStatement result = context.createBlockStatement(location, 0, start, m_lastTokenEndPosition.line, WTFMove(lexicalEnvironment), WTFMove(functionStack));
        context.setStartOffset(result, startOffset);
		return result;
	}
    TreeSourceElements subtree = parseSourceElements(context, DontCheckForStrictMode);
    failIfFalse(subtree, ASCIILiteral("Cannot parse the body of the block statement"));
	matchOrFail(CLOSEBRACE, ASCIILiteral("Expected a closing '}' at the end of a block statement"));
	int endOffset = m_token.m_data.offset;
    next();
    if (shouldPushLexicalScope) {
    	ScopePair scopePair = popScope(lexicalScope, TreeBuilder::NeedsFreeVariableInfo);
        lexicalEnvironment = WTFMove(scopePair.lexicalEnvironment);
        functionStack = WTFMove(scopePair.functionDeclarations);
    }
    TreeStatement result = context.createBlockStatement(location, subtree, start, m_lastTokenEndPosition.line, WTFMove(lexicalEnvironment), WTFMove(functionStack));
    context.setStartOffset(result, startOffset);
	return result;
}

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseStatement(TreeBuilder& context, const Identifier*& directive, unsigned* directiveLiteralLength)
{
	propagateError();

    DepthManager statementDepth(&m_statementDepth);
    m_statementDepth++;
    int nonTrivialExpressionCount = 0;
    failIfStackOverflow();
    TreeStatement result = 0;
    bool shouldSetEndOffset = true;
    bool parentAllowsFunctionDeclarationAsStatement = m_immediateParentAllowsFunctionDeclarationInStatement;
    m_immediateParentAllowsFunctionDeclarationInStatement = false;

    switch (m_token.m_type) {
    case OPENBRACE:
        result = parseBlockStatement(context);
        shouldSetEndOffset = false;
        break;
    case VAR:
    	result = parseVariableDeclaration(context, DeclarationType::VarDeclaration);
		break;
    case FUNCTION:
        result = parseFunctionDeclarationStatement(context, parentAllowsFunctionDeclarationAsStatement);
        break;
    case SEMICOLON:{
        JSTokenLocation location(tokenLocation());
        next();
        result = context.createEmptyStatement(location);
        break;
    }
    case IF:
    	result = parseIfStatement(context);
        break;
    case DO:
    	result = parseDoWhileStatement(context);
        break;
    case WHILE:
    	result = parseWhileStatement(context);
        break;
    case FOR:
    	result = parseForStatement(context);
        break;
    case CONTINUE:
    	result = parseContinueStatement(context);
        break;
    case BREAK:
    	result = parseBreakStatement(context);
        break;
    case RETURN:
    	result = parseReturnStatement(context);
        break;
    case WITH:
    	result = parseWithStatement(context);
        break;
    case SWITCH:
    	result = parseSwitchStatement(context);
        break;
    case THROW:
    	result = parseThrowStatement(context);
        break;
    case TRY:
    	result = parseTryStatement(context);
        break;
    case DEBUGGER:
    	result = parseDebuggerStatement(context);
        break;
    case EOFTOK:
    case CASE:
    case CLOSEBRACE:
    case DEFAULT:
        // These tokens imply the end of a set of source elements
        return 0;
    case ESCAPED_KEYWORD:
        if (!matchAllowedEscapedContextualKeyword()) {
            failDueToUnexpectedToken();
            break;
        }
        FALLTHROUGH;
    case LET:
    case IDENT:
    case AWAIT:
    case YIELD: {
        bool allowFunctionDeclarationAsStatement = false;
        result = parseExpressionOrLabelStatement(context, allowFunctionDeclarationAsStatement);
        break;
    }
    case STRING:
        directive = m_token.m_data.ident;
        if (directiveLiteralLength)
            *directiveLiteralLength = m_token.m_location.endOffset - m_token.m_location.startOffset;
        nonTrivialExpressionCount = m_parserState.nonTrivialExpressionCount;
        FALLTHROUGH;
    default: {
        TreeStatement exprStatement = parseExpressionStatement(context);
        if (directive && nonTrivialExpressionCount != m_parserState.nonTrivialExpressionCount)
            directive = 0;
        result = exprStatement;
        break;
    }
    }

    if (result && shouldSetEndOffset)
        context.setEndOffset(result, m_lastTokenEndPosition.offset);

    return result;
}

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseFunctionDeclarationStatement(TreeBuilder& context, bool parentAllowsFunctionDeclarationAsStatement)
{
    semanticFailIfTrue(strictMode(), ASCIILiteral("Function declarations are only allowed inside blocks or switch statements in strict mode"));
    failIfFalse(parentAllowsFunctionDeclarationAsStatement, ASCIILiteral("Function declarations are only allowed inside block statements or at the top level of a program"));

    // Any function declaration that isn't in a block is a syntax error unless it's
    // in an if/else statement. If it's in an if/else statement, we will magically
    // treat it as if the if/else statement is inside a block statement.
    // to the very top like a "var". For example:
    // function a() {
    //     if (cond) function foo() { }
    // }
    // will be rewritten as:
    // function a() {
    //     if (cond) { function foo() { } }
    // }
    AutoPopScopeRef blockScope(this, pushScope());
    blockScope->setIsLexicalScope();
    blockScope->preventVarDeclarations();
    JSTokenLocation location(tokenLocation());
    int start = tokenLine();

    TreeStatement function = parseFunctionDeclaration(context, FunctionDeclarationType::Statement);
    propagateError();
    failIfFalse(function, ASCIILiteral("Expected valid function statement after 'function' keyword"));
    TreeSourceElements sourceElements = context.createSourceElements();
    context.appendStatement(sourceElements, function);
    ScopePair scopePair = popScope(blockScope, TreeBuilder::NeedsFreeVariableInfo);
    return context.createBlockStatement(location, sourceElements, start, m_lastTokenEndPosition.line, WTFMove(scopePair.lexicalEnvironment), WTFMove(scopePair.functionDeclarations));
}

template <typename LexerType>
template <class TreeBuilder> bool Parser<LexerType>::parseFormalParameters(TreeBuilder& context, TreeFormalParameterList list, bool isArrowFunction, bool isMethod, unsigned& parameterCount)
{
#define failIfDuplicateIfViolation() \
    if (duplicateParameter) {\
        semanticFailIfTrue(hasDefaultParameterValues, ASCIILiteral("Duplicate parameter '"), duplicateParameter, ASCIILiteral("' not allowed in function with default parameter values"));\
        semanticFailIfTrue(hasDestructuringPattern, ASCIILiteral("Duplicate parameter '"), duplicateParameter, ASCIILiteral("' not allowed in function with destructuring parameters"));\
        semanticFailIfTrue(isRestParameter, ASCIILiteral("Duplicate parameter '"), duplicateParameter, ASCIILiteral("' not allowed in function with a rest parameter"));\
        semanticFailIfTrue(isArrowFunction, ASCIILiteral("Duplicate parameter '"), duplicateParameter, ASCIILiteral("' not allowed in an arrow function"));\
        semanticFailIfTrue(isMethod, ASCIILiteral("Duplicate parameter '"), duplicateParameter, ASCIILiteral("' not allowed in a method"));\
	}

    bool hasDefaultParameterValues = false;
    bool hasDestructuringPattern = false;
    bool isRestParameter = false;
    const Identifier* duplicateParameter = 0;
    unsigned restParameterStart = 0;
    do {
    	TreeDestructuringPattern parameter = 0;
        TreeExpression defaultValue = 0;

        if (UNLIKELY(match(CLOSEPAREN)))
            break;

        if (match(DOTDOTDOT)) {
            next();
            semanticFailIfTrue(isDisallowedIdentifierAwait(m_token), "Cannot use 'await' as a parameter name in an async function");
			TreeDestructuringPattern destructuringPattern = parseDestructuringPattern(context, DestructuringKind::DestructureToParameters, ExportType::NotExported, &duplicateParameter, &hasDestructuringPattern);
            propagateError();
            parameter = context.createRestParameter(destructuringPattern, restParameterStart);
            failIfTrue(match(COMMA), ASCIILiteral("Rest parameter should be the last parameter in a function declaration")); // Let's have a good error message for this common case.
            isRestParameter = true;
        } else
            parameter = parseDestructuringPattern(context, DestructuringKind::DestructureToParameters, ExportType::NotExported, &duplicateParameter, &hasDestructuringPattern);
        failIfFalse(parameter, ASCIILiteral("Cannot parse parameter pattern"));
        if (!isRestParameter) {
			defaultValue = parseDefaultValueForDestructuringPattern(context);
			if (defaultValue)
				hasDefaultParameterValues = true;
		}
        propagateError();
        failIfDuplicateIfViolation();
        if (isRestParameter || defaultValue || hasDestructuringPattern)
            currentScope()->setHasNonSimpleParameterList();
        context.appendParameter(list, parameter, defaultValue);
        if (!isRestParameter) {
			restParameterStart++;
			if (!hasDefaultParameterValues)
				parameterCount++;
		}
    } while (!isRestParameter && consume(COMMA));

    return true;
#undef failIfDuplicateIfViolation
}

template <typename LexerType>
template <class TreeBuilder> TreeFunctionBody Parser<LexerType>::parseFunctionBody(
	TreeBuilder& context, SyntaxChecker& syntaxChecker, const JSTokenLocation& startLocation, int startColumn, int functionKeywordStart, int functionNameStart, int parametersStart,
	ConstructorKind constructorKind, SuperBinding superBinding, FunctionBodyType bodyType, unsigned parameterCount)
{
	SetForScope<bool> overrideParsingClassFieldInitializer(m_parserState.isParsingClassFieldInitializer, bodyType == StandardFunctionBodyBlock ? false : m_parserState.isParsingClassFieldInitializer);
	bool isArrowFunctionBodyExpression = bodyType == ArrowFunctionBodyExpression;
	if (!isArrowFunctionBodyExpression) {
    	next();
    	if (match(CLOSEBRACE)) {
            unsigned endColumn = tokenColumn();
            SuperBinding functionSuperBinding = adjustSuperBindingForBaseConstructor(constructorKind, superBinding, currentScope());
    		return context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, endColumn, functionKeywordStart, functionNameStart, parametersStart, strictMode(), constructorKind, functionSuperBinding, parameterCount, sourceParseMode(), isArrowFunctionBodyExpression);
    	}
    }

    DepthManager statementDepth(&m_statementDepth);
    m_statementDepth = 0;
    if (bodyType == ArrowFunctionBodyExpression)
        failIfFalse(parseArrowFunctionSingleExpressionBodySourceElements(syntaxChecker), ASCIILiteral("Cannot parse body of this arrow function"));
    else
        failIfFalse(parseSourceElements(syntaxChecker, CheckForStrictMode), bodyType == StandardFunctionBodyBlock ? ASCIILiteral("Cannot parse body of this function") : ASCIILiteral("Cannot parse body of this arrow function"));
    unsigned endColumn = tokenColumn();
    SuperBinding functionSuperBinding = adjustSuperBindingForBaseConstructor(constructorKind, superBinding, currentScope());
	return context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, endColumn, functionKeywordStart, functionNameStart, parametersStart, strictMode(), constructorKind, functionSuperBinding, parameterCount, sourceParseMode(), isArrowFunctionBodyExpression);
}

static ASCIILiteral stringArticleForFunctionMode(SourceParseMode mode)
{
    switch (mode) {
    case SourceParseMode::GetterMode:
    case SourceParseMode::SetterMode:
    case SourceParseMode::NormalFunctionMode:
    case SourceParseMode::MethodMode:
    case SourceParseMode::GeneratorBodyMode:
    case SourceParseMode::GeneratorWrapperFunctionMode:
    case SourceParseMode::GeneratorWrapperMethodMode:
        return ASCIILiteral("a ");
    case SourceParseMode::ArrowFunctionMode:
    case SourceParseMode::AsyncFunctionMode:
    case SourceParseMode::AsyncFunctionBodyMode:
    case SourceParseMode::AsyncMethodMode:
    case SourceParseMode::AsyncArrowFunctionBodyMode:
    case SourceParseMode::AsyncArrowFunctionMode:
    case SourceParseMode::AsyncGeneratorWrapperFunctionMode:
    case SourceParseMode::AsyncGeneratorBodyMode:
    case SourceParseMode::AsyncGeneratorWrapperMethodMode:
        return ASCIILiteral("an ");
    case SourceParseMode::ProgramMode:
    case SourceParseMode::ModuleAnalyzeMode:
    case SourceParseMode::ModuleEvaluateMode:
    case SourceParseMode::ClassFieldInitializerMode:
    case SourceParseMode::ClassStaticBlockMode:
        RELEASE_ASSERT_NOT_REACHED();
        return ASCIILiteral();
    }
    RELEASE_ASSERT_NOT_REACHED();
    return ASCIILiteral();
}

static ASCIILiteral stringForFunctionMode(SourceParseMode mode)
{
    switch (mode) {
    case SourceParseMode::GetterMode:
        return ASCIILiteral("getter");
    case SourceParseMode::SetterMode:
        return ASCIILiteral("setter");
    case SourceParseMode::NormalFunctionMode:
        return ASCIILiteral("function");
    case SourceParseMode::MethodMode:
        return ASCIILiteral("method");
    case SourceParseMode::GeneratorBodyMode:
        return ASCIILiteral("generator");
    case SourceParseMode::GeneratorWrapperFunctionMode:
    case SourceParseMode::GeneratorWrapperMethodMode:
        return ASCIILiteral("generator function");
    case SourceParseMode::ArrowFunctionMode:
        return ASCIILiteral("arrow function");
    case SourceParseMode::AsyncFunctionMode:
    case SourceParseMode::AsyncFunctionBodyMode:
        return ASCIILiteral("async function");
    case SourceParseMode::AsyncMethodMode:
        return ASCIILiteral("async method");
    case SourceParseMode::AsyncArrowFunctionBodyMode:
    case SourceParseMode::AsyncArrowFunctionMode:
        return ASCIILiteral("async arrow function");
    case SourceParseMode::AsyncGeneratorWrapperFunctionMode:
    case SourceParseMode::AsyncGeneratorBodyMode:
        return ASCIILiteral("async generator function");
    case SourceParseMode::AsyncGeneratorWrapperMethodMode:
        return ASCIILiteral("async generator method");
    case SourceParseMode::ProgramMode:
    case SourceParseMode::ModuleAnalyzeMode:
    case SourceParseMode::ModuleEvaluateMode:
    case SourceParseMode::ClassFieldInitializerMode:
    case SourceParseMode::ClassStaticBlockMode:
        RELEASE_ASSERT_NOT_REACHED();
        return ASCIILiteral();
    }
    RELEASE_ASSERT_NOT_REACHED();
    return ASCIILiteral();
}

template <typename LexerType> template <class TreeBuilder, class FunctionInfoType> typename TreeBuilder::FormalParameterList Parser<LexerType>::parseFunctionParameters(TreeBuilder& context, FunctionInfoType& functionInfo)
{
    auto mode = sourceParseMode();
	RELEASE_ASSERT(!(SourceParseModeSet(SourceParseMode::ProgramMode, SourceParseMode::ModuleAnalyzeMode, SourceParseMode::ModuleEvaluateMode).contains(mode)));
	TreeFormalParameterList parameterList = context.createFormalParameterList();
	if (mode == SourceParseMode::ClassStaticBlockMode)
		return parameterList;
	SetForScope<FunctionParsePhase> functionParsePhasePoisoner(m_parserState.functionParsePhase, FunctionParsePhase::Parameters);

    if (UNLIKELY((SourceParseModeSet(SourceParseMode::ArrowFunctionMode, SourceParseMode::AsyncArrowFunctionMode).contains(mode)))) {
        if (!matchSpecIdentifier() && !match(OPENPAREN)) {
            semanticFailureDueToKeyword(stringForFunctionMode(mode), ASCIILiteral(" name"));
            failWithMessage(ASCIILiteral("Expected an arrow function input parameter"));
        } else {
            if (match(OPENPAREN)) {
                next();

                if (match(CLOSEPAREN))
                    functionInfo.parameterCount = 0;
                else{
                	bool isArrowFunction = true;
					bool isMethod = false;
					failIfFalse(parseFormalParameters(context, parameterList, isArrowFunction, isMethod, functionInfo.parameterCount), ASCIILiteral("Cannot parse parameters for this "), stringForFunctionMode(mode));
				}

                consumeOrFail(CLOSEPAREN, ASCIILiteral("Expected a ')' or a ',' after a parameter declaration"));
            } else {
                functionInfo.parameterCount = 1;
            	TreeDestructuringPattern parameter = parseDestructuringPattern(context, DestructuringKind::DestructureToParameters, ExportType::NotExported);
                failIfFalse(parameter, ASCIILiteral("Cannot parse parameter pattern"));
                context.appendParameter(parameterList, parameter, 0);
            }
        }

        return parameterList;
    }

	if (!consume(OPENPAREN)) {
		semanticFailureDueToKeyword(stringForFunctionMode(mode), ASCIILiteral(" name"));
		failWithMessage(ASCIILiteral("Expected an opening '(' before a "), stringForFunctionMode(mode), ASCIILiteral("'s parameter list"));
	}

    if (mode == SourceParseMode::GetterMode) {
        consumeOrFail(CLOSEPAREN, ASCIILiteral("getter functions must have no parameters"));
        functionInfo.parameterCount = 0;
    } else if (mode == SourceParseMode::SetterMode) {
        failIfTrue(match(CLOSEPAREN), ASCIILiteral("setter functions must have one parameter"));
        const Identifier* duplicateParameter = 0;
        bool hasDestructuringPattern = false;
		auto parameter = parseDestructuringPattern(context, DestructuringKind::DestructureToParameters, ExportType::NotExported, &duplicateParameter, &hasDestructuringPattern);
		failIfFalse(parameter, ASCIILiteral("setter functions must have one parameter"));
		auto defaultValue = parseDefaultValueForDestructuringPattern(context);
		propagateError();
        if (defaultValue || hasDestructuringPattern) {
            semanticFailIfTrue(duplicateParameter, ASCIILiteral("Duplicate parameter '"), duplicateParameter, ASCIILiteral("' not allowed in function with non-simple parameter list"));
            currentScope()->setHasNonSimpleParameterList();
        }
        context.appendParameter(parameterList, parameter, defaultValue);
        functionInfo.parameterCount = defaultValue ? 0 : 1;
        failIfTrue(match(COMMA), ASCIILiteral("setter functions must have one parameter"));
        consumeOrFail(CLOSEPAREN, ASCIILiteral("Expected a ')' after a parameter declaration"));
    } else {
        if (match(CLOSEPAREN))
            functionInfo.parameterCount = 0;
        else{
        	bool isArrowFunction = false;
			bool isMethod = isMethodParseMode(mode);
			failIfFalse(parseFormalParameters(context, parameterList, isArrowFunction, isMethod, functionInfo.parameterCount), ASCIILiteral("Cannot parse parameters for this "), stringForFunctionMode(mode));
		}
		consumeOrFail(CLOSEPAREN, ASCIILiteral("Expected a ')' or a ',' after a parameter declaration"));
    }

    return parameterList;
}

template <typename LexerType>
template <class TreeBuilder> typename TreeBuilder::FormalParameterList Parser<LexerType>::createGeneratorParameters(TreeBuilder& context, unsigned& parameterCount)
{
    auto parameters = context.createFormalParameterList();

    JSTokenLocation location(tokenLocation());
    JSTextPosition position = tokenStartPosition();

#if 0
    auto addParameter = [&](const Identifier& name) {
        declareParameter(&name);
        auto binding = context.createBindingLocation(location, name, position, position, AssignmentContext::DeclarationStatement);
        context.appendParameter(parameters, binding, 0);
        ++parameterCount;
    };
#endif

    // @generator
    declareParameter(&m_vm->propertyNames->generatorPrivateName);
    auto generator = context.createBindingLocation(location, m_vm->propertyNames->generatorPrivateName, position, position, AssignmentContext::DeclarationStatement);
    context.appendParameter(parameters, generator, 0);
    ++parameterCount;

    // @generatorState
    declareParameter(&m_vm->propertyNames->generatorStatePrivateName);
    auto generatorState = context.createBindingLocation(location, m_vm->propertyNames->generatorStatePrivateName, position, position, AssignmentContext::DeclarationStatement);
    context.appendParameter(parameters, generatorState, 0);
    ++parameterCount;

    // @generatorValue
    declareParameter(&m_vm->propertyNames->generatorValuePrivateName);
    auto generatorValue = context.createBindingLocation(location, m_vm->propertyNames->generatorValuePrivateName, position, position, AssignmentContext::DeclarationStatement);
    context.appendParameter(parameters, generatorValue, 0);
    ++parameterCount;

    // @generatorResumeMode
    declareParameter(&m_vm->propertyNames->generatorResumeModePrivateName);
    auto generatorResumeMode = context.createBindingLocation(location, m_vm->propertyNames->generatorResumeModePrivateName, position, position, AssignmentContext::DeclarationStatement);
    context.appendParameter(parameters, generatorResumeMode, 0);
    ++parameterCount;

    // @generatorFrame
    declareParameter(&m_vm->propertyNames->generatorFramePrivateName);
    auto generatorFrame = context.createBindingLocation(location, m_vm->propertyNames->generatorFramePrivateName, position, position, AssignmentContext::DeclarationStatement);
    context.appendParameter(parameters, generatorFrame, 0);
    ++parameterCount;

    return parameters;
}

template <typename LexerType>
template <class TreeBuilder> bool Parser<LexerType>::parseFunctionInfo(TreeBuilder& context, FunctionNameRequirements requirements,
	bool nameIsInContainingScope, ConstructorKind constructorKind, SuperBinding expectedSuperBinding, int functionKeywordStart, ParserFunctionInfo<TreeBuilder>& functionInfo, FunctionDefinitionType functionDefinitionType, Optional<int> functionConstructorParametersEndPosition)
{
    auto mode = sourceParseMode();
    RELEASE_ASSERT(isFunctionParseMode(mode));

    ScopeRef parentScope = currentScope();

    bool functionNameIsAwait = isPossiblyEscapedAwait(m_token);
    ASCIILiteral isDisallowedAwaitFunctionNameReason = functionNameIsAwait && !canUseIdentifierAwait() ? disallowedIdentifierAwaitReason() : ASCIILiteral();

    AutoPopScopeRef functionScope(this, pushScope());
    functionScope->setSourceParseMode(mode);
    functionScope->setExpectedSuperBinding(expectedSuperBinding);
    functionScope->setConstructorKind(constructorKind);
    SetForScope<FunctionParsePhase> functionParsePhasePoisoner(m_parserState.functionParsePhase, FunctionParsePhase::Body);
	int functionNameStart = m_token.m_location.startOffset;
    const Identifier* lastFunctionName = m_parserState.lastFunctionName;
    m_parserState.lastFunctionName = 0;
    int parametersStart = -1;
    JSTokenLocation startLocation;
    int startColumn = -1;
    FunctionBodyType functionBodyType;

    auto loadCachedFunction = [&] () -> bool {
		if (UNLIKELY(!Options::useSourceProviderCache()))
			return false;

		ASSERT(parametersStart != -1);
		ASSERT(startColumn != -1);

		// 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(parametersStart) : nullptr) {
			// If we're in a strict context, the cached function info must say it was strict too.
			ASSERT(!strictMode() || cachedInfo->strictMode);
			JSTokenLocation endLocation;

			ConstructorKind constructorKind = static_cast<ConstructorKind>(cachedInfo->constructorKind);
			SuperBinding expectedSuperBinding = static_cast<SuperBinding>(cachedInfo->expectedSuperBinding);
			functionScope->setConstructorKind(constructorKind);
			functionScope->setExpectedSuperBinding(expectedSuperBinding);

			endLocation.line = cachedInfo->lastTokenLine;
			endLocation.startOffset = cachedInfo->lastTokenStartOffset;
			endLocation.lineStartOffset = cachedInfo->lastTokenLineStartOffset;
			ASSERT(endLocation.startOffset >= endLocation.lineStartOffset);

			bool endColumnIsOnStartLine = endLocation.line == functionInfo.startLine;
			unsigned currentLineStartOffset = m_lexer.currentLineStartOffset();
			unsigned bodyEndColumn = endColumnIsOnStartLine ? endLocation.startOffset - currentLineStartOffset : endLocation.startOffset - endLocation.lineStartOffset;

			ASSERT(endLocation.startOffset >= endLocation.lineStartOffset);

			FunctionBodyType functionBodyType;
			if (UNLIKELY(SourceParseModeSet(SourceParseMode::ArrowFunctionMode, SourceParseMode::AsyncArrowFunctionMode).contains(mode)))
				functionBodyType = cachedInfo->isBodyArrowExpression ?  ArrowFunctionBodyExpression : ArrowFunctionBodyBlock;
			else
				functionBodyType = StandardFunctionBodyBlock;

			SuperBinding functionSuperBinding = adjustSuperBindingForBaseConstructor(constructorKind, expectedSuperBinding, cachedInfo->needsSuperBinding, cachedInfo->usesEval, cachedInfo->innerArrowFunctionFeatures);

			functionInfo.body = context.createFunctionMetadata(
				startLocation, endLocation, startColumn, bodyEndColumn,
				functionKeywordStart, functionNameStart, parametersStart,
				cachedInfo->strictMode, constructorKind, functionSuperBinding, cachedInfo->parameterCount, mode, functionBodyType == ArrowFunctionBodyExpression);
			functionInfo.endOffset = cachedInfo->endFunctionOffset;
			functionInfo.parameterCount = cachedInfo->parameterCount;

			functionScope->restoreFromSourceProviderCache(cachedInfo);
			popScope(functionScope, TreeBuilder::NeedsFreeVariableInfo);

			m_token = cachedInfo->endFunctionToken();

			if (endColumnIsOnStartLine)
				m_token.m_location.lineStartOffset = currentLineStartOffset;

			m_lexer.setOffset(m_token.m_location.endOffset, m_token.m_location.lineStartOffset);
			m_lexer.setLineNumber(m_token.m_location.line);

			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;
			return true;
		}

		return false;
    };

    SyntaxChecker syntaxChecker(const_cast<VM*>(m_vm), &m_lexer);

    if (UNLIKELY((SourceParseModeSet(SourceParseMode::ArrowFunctionMode, SourceParseMode::AsyncArrowFunctionMode).contains(mode)))) {
		startLocation = tokenLocation();
        functionInfo.startLine = tokenLine();
        startColumn = tokenColumn();

        parametersStart = m_token.m_location.startOffset;
        functionInfo.startOffset = parametersStart;
        functionInfo.parametersStartColumn = startColumn;

        if (loadCachedFunction())
            return true;
        {
			// Parse formal parameters with [+Yield] parameterization, in order to ban YieldExpressions
			// in ArrowFormalParameters, per ES6 #sec-arrow-function-definitions-static-semantics-early-errors.
        	Scope::MaybeParseAsGeneratorForScope parseAsGenerator(functionScope, parentScope->isGenerator());
			SetForScope<bool> overrideAllowAwait(m_parserState.allowAwait, !parentScope->isAsyncFunction() && !isAsyncFunctionParseMode(mode));
			parseFunctionParameters(syntaxChecker, functionInfo);
			propagateError();
        }

        matchOrFail(ARROWFUNCTION, ASCIILiteral("Expected a '=>' after arrow function parameter declaration"));

        if (m_lexer.hasLineTerminatorBeforeToken())
            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
        next();
        functionBodyType = match(OPENBRACE) ? ArrowFunctionBodyBlock : ArrowFunctionBodyExpression;
    } else {
        // http://ecma-international.org/ecma-262/6.0/#sec-function-definitions
        // FunctionExpression :
        //     function BindingIdentifieropt ( FormalParameters ) { FunctionBody }
        //
        // FunctionDeclaration[Yield, Default] :
        //     function BindingIdentifier[?Yield] ( FormalParameters ) { FunctionBody }
        //     [+Default] function ( FormalParameters ) { FunctionBody }
        //
        // GeneratorDeclaration[Yield, Default] :
        //     function * BindingIdentifier[?Yield] ( FormalParameters[Yield] ) { GeneratorBody }
        //     [+Default] function * ( FormalParameters[Yield] ) { GeneratorBody }
        //
        // GeneratorExpression :
        //     function * BindingIdentifier[Yield]opt ( FormalParameters[Yield] ) { GeneratorBody }
        //
        // The name of FunctionExpression and AsyncFunctionExpression can accept "yield" even in the context of generator.
    	bool canUseYield = !strictMode();
        if (!(functionDefinitionType == FunctionDefinitionType::Expression && SourceParseModeSet(SourceParseMode::NormalFunctionMode, SourceParseMode::AsyncFunctionMode).contains(mode)))
        	canUseYield &= !parentScope->isGenerator();

        if (requirements != FunctionNameRequirements::Unnamed) {
            ASSERT_WITH_MESSAGE(!(requirements == FunctionNameRequirements::None && !functionInfo.name), ASCIILiteral("When specifying FunctionNameRequirements::None, we need to initialize functionInfo.name with the default value in the caller side."));
            if (matchSpecIdentifier(canUseYield, functionNameIsAwait)) {
				functionInfo.name = m_token.m_data.ident;
				m_parserState.lastFunctionName = functionInfo.name;
                if (UNLIKELY(isDisallowedAwaitFunctionNameReason))
					semanticFailIfTrue(functionDefinitionType == FunctionDefinitionType::Declaration || isAsyncFunctionOrAsyncGeneratorWrapperParseMode(mode), ASCIILiteral("Cannot declare function named 'await' "), isDisallowedAwaitFunctionNameReason);
				else if (isAsyncFunctionOrAsyncGeneratorWrapperParseMode(mode) && match(AWAIT) && functionDefinitionType == FunctionDefinitionType::Expression)
					semanticFail(ASCIILiteral("Cannot declare "), stringForFunctionMode(mode), ASCIILiteral(" named 'await'"));
				else if (isGeneratorOrAsyncGeneratorWrapperParseMode(mode) && match(YIELD) && functionDefinitionType == FunctionDefinitionType::Expression)
					semanticFail(ASCIILiteral("Cannot declare "), stringForFunctionMode(mode), ASCIILiteral(" named 'yield'"));
				next();
				if (!nameIsInContainingScope)
					failIfTrueIfStrict(functionScope->declareCallee(functionInfo.name) & DeclarationResult::InvalidStrictMode, ASCIILiteral("'"), functionInfo.name, ASCIILiteral("' is not a valid "), stringForFunctionMode(mode), ASCIILiteral(" name in strict mode"));
			} else if (requirements == FunctionNameRequirements::Named) {
				if (match(OPENPAREN)) {
					semanticFailIfTrue(mode == SourceParseMode::NormalFunctionMode, ASCIILiteral("Function statements must have a name"));
					semanticFailIfTrue(mode == SourceParseMode::AsyncFunctionMode, ASCIILiteral("Async function statements must have a name"));
				}
				semanticFailureDueToKeyword(stringForFunctionMode(mode), ASCIILiteral(" name"));
				failDueToUnexpectedToken();
				return false;
			}
            ASSERT(functionInfo.name);
        }

        startLocation = tokenLocation();
        functionInfo.startLine = tokenLine();
        startColumn = tokenColumn();
        functionInfo.parametersStartColumn = startColumn;

        parametersStart = m_token.m_location.startOffset;
        functionInfo.startOffset = parametersStart;

        if (loadCachedFunction())
            return true;
        {
            SetForScope<bool> overrideAllowAwait(m_parserState.allowAwait, !isAsyncFunctionParseMode(mode));
            parseFunctionParameters(syntaxChecker, functionInfo);
            propagateError();
        }

        matchOrFail(OPENBRACE, ASCIILiteral("Expected an opening '{' at the start of a "), stringForFunctionMode(mode), ASCIILiteral(" body"));

        // If the code is invoked from function constructor, we need to ensure that parameters are only composed by the string offered as parameters.
        if (UNLIKELY(functionConstructorParametersEndPosition))
            semanticFailIfFalse(lastTokenEndPosition().offset == *functionConstructorParametersEndPosition, ASCIILiteral("Parameters should match arguments offered as parameters in Function constructor"));

        // BytecodeGenerator emits code to throw TypeError when a class constructor is "call"ed.
        // Set ConstructorKind to None for non-constructor methods of classes.

        functionBodyType = StandardFunctionBodyBlock;
    }

    m_parserState.lastFunctionName = lastFunctionName;
    ParserState oldState = internalSaveParserState(context);

    // FIXME: https://bugs.webkit.org/show_bug.cgi?id=156962
    // This loop collects the set of capture candidates that aren't
    // part of the set of this function's declared parameters. We will
    // figure out which parameters are captured for this function when
    // we actually generate code for it. For now, we just propagate to
    // our parent scopes which variables we might have closed over that
    // belong to them. This is necessary for correctness when using
    // the source provider cache because we can't close over a variable
    // that we don't claim to close over. The source provider cache must
    // know this information to properly cache this function.
    // This might work itself out nicer if we declared a different
    // Scope struct for the parameters (because they are indeed implemented
    // as their own scope).
    UniquedStringImplPtrSet nonLocalCapturesFromParameterExpressions;
    functionScope->forEachUsedVariable([&] (UniquedStringImpl* impl) {
        if (!functionScope->hasDeclaredParameter(impl)) {
            nonLocalCapturesFromParameterExpressions.add(impl);
            if (TreeBuilder::NeedsFreeVariableInfo)
                parentScope->addClosedVariableCandidateUnconditionally(impl);
        }
        return IterationStatus::Continue;
    });

    // auto performParsingFunctionBody = [&] {
	//	return parseFunctionBody(context, syntaxChecker, startLocation, startColumn, functionKeywordStart, functionNameStart, parametersStart, constructorKind, expectedSuperBinding, functionBodyType, functionInfo.parameterCount, mode);
	// };

    if (isGeneratorOrAsyncFunctionWrapperParseMode(mode)) {
        AutoPopScopeRef generatorBodyScope(this, pushScope());
        SourceParseMode innerParseMode = SourceParseMode::GeneratorBodyMode;
        if (isAsyncFunctionOrAsyncGeneratorWrapperParseMode(mode))
            innerParseMode = getAsynFunctionBodyParseMode(mode);

        generatorBodyScope->setSourceParseMode(innerParseMode);
        generatorBodyScope->setConstructorKind(ConstructorKind::None);
		generatorBodyScope->setExpectedSuperBinding(expectedSuperBinding);

        // Disallow 'use strict' directives in the implicit inner function if
        // needed.
        if (functionScope->hasNonSimpleParameterList())
            generatorBodyScope->setHasNonSimpleParameterList();

        functionInfo.body = parseFunctionBody(context, syntaxChecker, startLocation, startColumn, functionKeywordStart, functionNameStart, parametersStart, constructorKind, expectedSuperBinding, functionBodyType, functionInfo.parameterCount);

        // When a generator has a "use strict" directive, a generator function wrapping it should be strict mode.
        if  (generatorBodyScope->strictMode())
            functionScope->setStrictMode();

        popScope(generatorBodyScope, TreeBuilder::NeedsFreeVariableInfo);
    } else
        functionInfo.body = parseFunctionBody(context, syntaxChecker, startLocation, startColumn, functionKeywordStart, functionNameStart, parametersStart, constructorKind, expectedSuperBinding, functionBodyType, functionInfo.parameterCount);

    restoreParserState(context, oldState);
    failIfFalse(functionInfo.body, ASCIILiteral("Cannot parse the body of this "), stringForFunctionMode(mode));
    context.setEndOffset(functionInfo.body, m_lexer.currentOffset());
    if (functionScope->strictMode() && requirements != FunctionNameRequirements::Unnamed) {
        ASSERT(functionInfo.name);
    	RELEASE_ASSERT(SourceParseModeSet(SourceParseMode::NormalFunctionMode, SourceParseMode::MethodMode, SourceParseMode::ArrowFunctionMode, SourceParseMode::GeneratorBodyMode, SourceParseMode::GeneratorWrapperFunctionMode, SourceParseMode::ClassStaticBlockMode).contains(mode) || isAsyncFunctionOrAsyncGeneratorWrapperParseMode(mode));
		semanticFailIfTrue(m_vm->propertyNames->arguments == *functionInfo.name, ASCIILiteral("'"), functionInfo.name, ASCIILiteral("' is not a valid function name in strict mode"));
		semanticFailIfTrue(m_vm->propertyNames->eval == *functionInfo.name, ASCIILiteral("'"), functionInfo.name, ASCIILiteral("' is not a valid function name in strict mode"));
	}

    JSTokenLocation location = JSTokenLocation(m_token.m_location);
    functionInfo.endOffset = m_token.m_data.offset;
    
    if (functionBodyType == ArrowFunctionBodyExpression) {
        location = locationBeforeLastToken();
        functionInfo.endOffset = location.endOffset;
    }

    // Cache the tokenizer state and the function scope the first time the function is parsed.
    // Any future reparsing can then skip the function.// For arrow function is 8 = x=>x + 4 symbols;
    // For ordinary function is 16  = function(){} + 4 symbols
    const int minimumSourceLengthToCache = functionBodyType == StandardFunctionBodyBlock ? 16 : 8;
    OwnPtr<SourceProviderCacheItem> newInfo;
    int sourceLength = functionInfo.endOffset - functionInfo.startOffset;
    if (TreeBuilder::CanUseFunctionCache && m_functionCache && sourceLength > minimumSourceLengthToCache) {
    	SourceProviderCacheItemCreationParameters parameters;
        parameters.endFunctionOffset = functionInfo.endOffset;
        parameters.lastTokenLine = location.line;
        parameters.lastTokenStartOffset = location.startOffset;
        parameters.lastTokenEndOffset = location.endOffset;
        parameters.lastTokenLineStartOffset = location.lineStartOffset;
        parameters.parameterCount = functionInfo.parameterCount;
        parameters.constructorKind = constructorKind;
        parameters.expectedSuperBinding = expectedSuperBinding;
        if (functionBodyType == ArrowFunctionBodyExpression) {
            parameters.isBodyArrowExpression = true;
            parameters.tokenType = m_token.m_type;
        }
        else {
            parameters.isBodyArrowExpression = false;
            parameters.tokenType = CLOSEBRACE;
        }
		functionScope->fillParametersForSourceProviderCache(parameters, nonLocalCapturesFromParameterExpressions);
		newInfo = SourceProviderCacheItem::create(parameters);
    }

    bool functionScopeWasStrictMode = functionScope->strictMode();

    popScope(functionScope, TreeBuilder::NeedsFreeVariableInfo);

    if (functionBodyType != ArrowFunctionBodyExpression) {
        matchOrFail(CLOSEBRACE, ASCIILiteral("Expected a closing '}' after a "), stringForFunctionMode(mode), ASCIILiteral(" body"));
        next();
    } else {
        // We need to lex the last token again because the last token is lexed under the different context because of the following possibilities.
        // 1. which may have different strict mode.
        // 2. which may not build strings for tokens.
        // But (1) is not possible because we do not recognize the string literal in ArrowFunctionBodyExpression as directive and this is correct in terms of the spec (`value => "use strict"`).
        // So we only check TreeBuilder's type here.
        ASSERT_UNUSED(functionScopeWasStrictMode, functionScopeWasStrictMode == currentScope()->strictMode());
#if GCC_VERSION < 40700
        if (!TreeBuilder::IsSyntaxChecker)
#else
		if (!std::is_same<TreeBuilder, SyntaxChecker>::value)
#endif
            lexCurrentTokenAgainUnderCurrentContext(context);
    }
    
    if (newInfo)
        m_functionCache->add(functionInfo.startOffset, newInfo.release());
    
    functionInfo.endLine = m_lastTokenEndPosition.line;
    return true;
}

static NO_RETURN_DUE_TO_CRASH FunctionMetadataNode* getMetadata(ParserFunctionInfo<SyntaxChecker>&) { RELEASE_ASSERT_NOT_REACHED(); return 0;}
static FunctionMetadataNode* getMetadata(ParserFunctionInfo<ASTBuilder>& info) { return info.body; }

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseFunctionDeclaration(TreeBuilder& context, FunctionDeclarationType declarationType, ExportType exportType, DeclarationDefaultContext declarationDefaultContext, Optional<int> functionConstructorParametersEndPosition)
{
    ASSERT(match(FUNCTION));
    JSTokenLocation location(tokenLocation());
    unsigned functionKeywordStart = tokenStart();
    next();
    SourceParseMode parseMode = SourceParseMode::NormalFunctionMode;
    if (match(TIMES)) {
        failIfTrue(declarationType == FunctionDeclarationType::Statement, ASCIILiteral("Cannot use generator function declaration in single-statement context"));
        next();
        parseMode = SourceParseMode::GeneratorWrapperFunctionMode;
    }
    SetForScope<SourceParseMode> innerParseMode(m_parseMode, parseMode);

    ParserFunctionInfo<TreeBuilder> functionInfo;
    FunctionNameRequirements requirements = FunctionNameRequirements::Named;
    if (declarationDefaultContext == DeclarationDefaultContext::ExportDefault) {
        // Under the "export default" context, function declaration does not require the function name.
        //
        //     ExportDeclaration:
        //         ...
        //         export default HoistableDeclaration[~Yield, +Default]
        //         ...
        //
        //     HoistableDeclaration[Yield, Default]:
        //         FunctionDeclaration[?Yield, ?Default]
        //         GeneratorDeclaration[?Yield, ?Default]
        //
        //     FunctionDeclaration[Yield, Default]:
        //         ...
        //         [+Default] function ( FormalParameters[~Yield] ) { FunctionBody[~Yield] }
        //
        //     GeneratorDeclaration[Yield, Default]:
        //         ...
        //         [+Default] function * ( FormalParameters[+Yield] ) { GeneratorBody }
        //
        // In this case, we use "*default*" as this function declaration's name.
        requirements = FunctionNameRequirements::None;
        functionInfo.name = &m_vm->propertyNames->starDefaultPrivateName;
    }

    failIfFalse((parseFunctionInfo(context, requirements, true, ConstructorKind::None, SuperBinding::NotNeeded, functionKeywordStart, functionInfo, FunctionDefinitionType::Declaration, functionConstructorParametersEndPosition)), ASCIILiteral("Cannot parse this function"));
    ASSERT(functionInfo.name);

    std::pair<DeclarationResultMask, ScopeRef> functionDeclaration = declareFunction(functionInfo.name);
    DeclarationResultMask declarationResult = functionDeclaration.first;
	failIfTrueIfStrict(declarationResult & DeclarationResult::InvalidStrictMode, ASCIILiteral("Cannot declare a function named '"), functionInfo.name, ASCIILiteral("' in strict mode"));
	if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration)
		internalFailWithMessage(false, ASCIILiteral("Cannot declare a function that shadows a let/const/class/function variable '"), functionInfo.name, ASCIILiteral("'"));
	if (exportType == ExportType::Exported) {
		ASSERT_WITH_MESSAGE(declarationDefaultContext != DeclarationDefaultContext::ExportDefault, ASCIILiteral("Export default case will export the name and binding in the caller."));
		semanticFailIfFalse(exportName(*functionInfo.name), ASCIILiteral("Cannot export a duplicate function name: '"), functionInfo.name, ASCIILiteral("'"));
		m_moduleScopeData->exportBinding(*functionInfo.name);
	}
    TreeStatement result = context.createFuncDeclStatement(location, functionInfo);
    if (TreeBuilder::CreatesAST) {
        FunctionMetadataNode* metadata = getMetadata(functionInfo);
        functionDeclaration.second->appendFunction(metadata);
        bool isSloppyModeHoistingCandidate = m_statementDepth != 1 && !strictMode() && m_parseMode == SourceParseMode::NormalFunctionMode;
        if (isSloppyModeHoistingCandidate) {
            // Functions declared inside a function inside a nested block scope in sloppy mode are subject to this
            // crazy rule defined inside Annex B.3.2 in the ECMA-262 spec. It basically states that we will create
            // the function as a local block scoped variable, but when we evaluate the block that the function is
            // contained in, we will assign the function to a "var" variable only if declaring such a "var" wouldn't
            // be a syntax error and if there isn't a parameter with the same name. (It would only be a syntax error if
            // there are is a let/class/const with the same name). Note that this mean we only do the "var" hoisting
            // binding if the block evaluates. For example, this means we wont won't perform the binding if it's inside
            // the untaken branch of an if statement.
            functionDeclaration.second->addSloppyModeFunctionHoistingCandidate<Scope::NeedsDuplicateDeclarationCheck::No>(metadata);
        }
    }
    return result;
}

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseAsyncFunctionDeclaration(TreeBuilder& context, ExportType exportType, DeclarationDefaultContext declarationDefaultContext, Optional<int> functionConstructorParametersEndPosition)
{
    ASSERT(match(FUNCTION));
    JSTokenLocation location(tokenLocation());
    unsigned functionKeywordStart = tokenStart();
    next();
    ParserFunctionInfo<TreeBuilder> functionInfo;
    SourceParseMode parseMode = SourceParseMode::AsyncFunctionMode;
    if (Options::useAsyncIterator() && consume(TIMES))
        parseMode = SourceParseMode::AsyncGeneratorWrapperFunctionMode;
    SetForScope<SourceParseMode> innerParseMode(m_parseMode, parseMode);

    FunctionNameRequirements requirements = FunctionNameRequirements::Named;
    if (declarationDefaultContext == DeclarationDefaultContext::ExportDefault) {
        // Under the "export default" context, function declaration does not require the function name.
        //
        //     ExportDeclaration:
        //         ...
        //         export default HoistableDeclaration[~Yield, +Default]
        //         ...
        //
        //     HoistableDeclaration[Yield, Default]:
        //         FunctionDeclaration[?Yield, ?Default]
        //         GeneratorDeclaration[?Yield, ?Default]
        //
        //     FunctionDeclaration[Yield, Default]:
        //         ...
        //         [+Default] function ( FormalParameters[~Yield] ) { FunctionBody[~Yield] }
        //
        //     GeneratorDeclaration[Yield, Default]:
        //         ...
        //         [+Default] function * ( FormalParameters[+Yield] ) { GeneratorBody }
        //
        // In this case, we use "*default*" as this function declaration's name.
        requirements = FunctionNameRequirements::None;
        functionInfo.name = &m_vm->propertyNames->starDefaultPrivateName;
    }

    failIfFalse((parseFunctionInfo(context, requirements, true, ConstructorKind::None, SuperBinding::NotNeeded, functionKeywordStart, functionInfo, FunctionDefinitionType::Declaration, functionConstructorParametersEndPosition)), ASCIILiteral("Cannot parse this async function"));
    failIfFalse(functionInfo.name, ASCIILiteral("Async function statements must have a name"));

    std::pair<DeclarationResultMask, ScopeRef> functionDeclaration = declareFunction(functionInfo.name);
    DeclarationResultMask declarationResult = functionDeclaration.first;
    failIfTrueIfStrict(declarationResult & DeclarationResult::InvalidStrictMode, ASCIILiteral("Cannot declare an async function named '"), functionInfo.name, ASCIILiteral("' in strict mode"));
    if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration)
        internalFailWithMessage(false, ASCIILiteral("Cannot declare an async function that shadows a let/const/class/function variable '"), functionInfo.name, ASCIILiteral("'"));
    if (exportType == ExportType::Exported) {
        semanticFailIfFalse(exportName(*functionInfo.name), ASCIILiteral("Cannot export a duplicate function name: '"), functionInfo.name, ASCIILiteral("'"));
        m_moduleScopeData->exportBinding(*functionInfo.name);
    }

    TreeStatement result = context.createFuncDeclStatement(location, functionInfo);
    if (TreeBuilder::CreatesAST)
        functionDeclaration.second->appendFunction(getMetadata(functionInfo));
    return result;
}

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseClassDeclaration(TreeBuilder& context, ExportType exportType, DeclarationDefaultContext declarationDefaultContext)
{
    ASSERT(match(CLASSTOKEN));
    JSTokenLocation location(tokenLocation());
    JSTextPosition classStart = tokenStartPosition();
    unsigned classStartLine = tokenLine();

    ParserClassInfo<TreeBuilder> info;
    FunctionNameRequirements requirements = FunctionNameRequirements::Named;
    if (declarationDefaultContext == DeclarationDefaultContext::ExportDefault) {
        // Under the "export default" context, class declaration does not require the class name.
        //
        //     ExportDeclaration:
        //         ...
        //         export default ClassDeclaration[~Yield, +Default]
        //         ...
        //
        //     ClassDeclaration[Yield, Default]:
        //         ...
        //         [+Default] class ClassTail[?Yield]
        //
        // In this case, we use "*default*" as this class declaration's name.
        requirements = FunctionNameRequirements::None;
        info.className = &m_vm->propertyNames->starDefaultPrivateName;
    }

    TreeClassExpression classExpr = parseClass(context, requirements, info);
    failIfFalse(classExpr, ASCIILiteral("Failed to parse class"));
    ASSERT(info.className);

    DeclarationResultMask declarationResult = declareVariable(info.className, DeclarationType::LetDeclaration);
    if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration)
        internalFailWithMessage(false, ASCIILiteral("Cannot declare a class twice: '"), info.className, ASCIILiteral("'"));
    if (exportType == ExportType::Exported) {
    	ASSERT_WITH_MESSAGE(declarationDefaultContext != DeclarationDefaultContext::ExportDefault, ASCIILiteral("Export default case will export the name and binding in the caller."));
		semanticFailIfFalse(exportName(*info.className), ASCIILiteral("Cannot export a duplicate class name: '"), info.className, ASCIILiteral("'"));
		m_moduleScopeData->exportBinding(*info.className);
	}

    JSTextPosition classEnd = lastTokenEndPosition();
    unsigned classEndLine = tokenLine();

    return context.createClassDeclStatement(location, classExpr, classStart, classEnd, classStartLine, classEndLine);
}

static ASCIILiteral instanceComputedNamePrefix("instanceComputedName");
static ASCIILiteral staticComputedNamePrefix("staticComputedName");

template <typename LexerType>
template <class TreeBuilder> TreeClassExpression Parser<LexerType>::parseClass(TreeBuilder& context, FunctionNameRequirements requirements, ParserClassInfo<TreeBuilder>& info)
{
    ASSERT(match(CLASSTOKEN));
    JSTokenLocation location(tokenLocation());
    info.startLine = location.line;
    info.startColumn = tokenColumn();
    info.startOffset = location.startOffset;

    // We have a subtle problem here. Class heritage evaluation should find class declaration's constructor name, but should not find private name evaluation.
    // For example,
    //
    //     class A extends (
    //         class {
    //             constructor() {
    //                 print(A); // This is OK.
    //                 print(A.#test); // This is SyntaxError.
    //             }
    //         }) {
    //         static #test = 42;
    //     }
    //
    // We need to create two scopes here since private name lookup will traverse scope at linking time in CodeBlock.
    // This classHeadScope is similar to functionScope in FunctionExpression with name.
    AutoPopScopeRef classHeadScope(this, pushScope());
    classHeadScope->setIsLexicalScope();
    classHeadScope->preventVarDeclarations();
    classHeadScope->setStrictMode();
    next();
    semanticFailIfTrue(currentScope()->isStaticBlock() && match(AWAIT), ASCIILiteral("Cannot use 'await' as a class name within static block"));

    ASSERT_WITH_MESSAGE(requirements != FunctionNameRequirements::Unnamed, ASCIILiteral("Currently, there is no caller that uses FunctionNameRequirements::Unnamed for class syntax."));
    ASSERT_WITH_MESSAGE(!(requirements == FunctionNameRequirements::None && !info.className), ASCIILiteral("When specifying FunctionNameRequirements::None, we need to initialize info.className with the default value in the caller side."));
    if (match(IDENT) || isAllowedIdentifierAwait(m_token)) {
        info.className = m_token.m_data.ident;
        next();
        failIfTrue(classHeadScope->declareLexicalVariable(info.className, true) & DeclarationResult::InvalidStrictMode, ASCIILiteral("'"), info.className, ASCIILiteral("' is not a valid class name"));
	} else if (requirements == FunctionNameRequirements::Named) {
        if (match(OPENBRACE))
            semanticFail(ASCIILiteral("Class statements must have a name"));
        semanticFailureDueToKeyword(ASCIILiteral("class name"));
        failDueToUnexpectedToken();
    }
    ASSERT(info.className);

    TreeExpression parentClass = 0;
    if (consume(EXTENDS)) {
        parentClass = parseMemberExpression(context);
        failIfFalse(parentClass, ASCIILiteral("Cannot parse the parent class name"));
    }
    const ConstructorKind constructorKind = parentClass ? ConstructorKind::Extends : ConstructorKind::Base;

    consumeOrFail(OPENBRACE, ASCIILiteral("Expected opening '{' at the start of a class body"));

    AutoPopScopeRef classScope(this, pushScope());
    classScope->setIsLexicalScope();
    classScope->preventVarDeclarations();
    classScope->setStrictMode();
    classScope->setIsClassScope();

    bool declaresPrivateMethod = false;
    bool declaresPrivateAccessor = false;
    bool declaresStaticPrivateMethod = false;
    bool declaresStaticPrivateAccessor = false;

    TreeExpression constructor = 0;
    TreePropertyList classElements = 0;
    TreePropertyList classElementsTail = 0;
    unsigned nextInstanceComputedFieldID = 0;
    unsigned nextStaticComputedFieldID = 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.
        ClassElementTag tag = ClassElementTag::Instance;
        SourceParseMode parseMode = SourceParseMode::MethodMode;
        PropertyNode::Type type = PropertyNode::Constant;
		if (match(RESERVED_IF_STRICT) && *m_token.m_data.ident == m_vm->propertyNames->staticKeyword) {
			auto savePoint = createSavePoint(context);
			next();
			if (match(OPENPAREN)) {
				// Reparse "static()" as a method named "static".
				restoreSavePoint(context, savePoint);
            } else {
                tag = ClassElementTag::Static;
                if (match(OPENBRACE))
                    parseMode = SourceParseMode::ClassStaticBlockMode;
            }
		}

        // FIXME: Figure out a way to share more code with parseProperty.
        const CommonIdentifiers& propertyNames = *m_vm->propertyNames;
        const Identifier* ident = &propertyNames.nullIdentifier;
        TreeExpression computedPropertyName = 0;
        bool isGetter = false;
        bool isSetter = false;
        if (consume(TIMES))
            parseMode = SourceParseMode::GeneratorWrapperMethodMode;

parseMethod:
        switch (m_token.m_type) {
        namedKeyword:
        case STRING:
            ident = m_token.m_data.ident;
            ASSERT(ident);
            next();
            break;
        case BIGINT:
            ident = &m_parserArena.identifierArena().makeBigIntDecimalIdentifier(const_cast<VM&>(*m_vm), *m_token.m_data.bigIntString, m_token.m_data.radix);
            ASSERT(ident);
            next();
            break;
        case ESCAPED_KEYWORD:
        case IDENT:
            if (UNLIKELY(*m_token.m_data.ident == m_vm->propertyNames->async && !m_token.m_data.escaped)) {
            	if (!isGeneratorMethodParseMode(parseMode) && !isAsyncMethodParseMode(parseMode)) {
					next();
                    // We match SEMICOLON as a special case for a field called 'async' without initializer.
                    if (match(OPENPAREN) || match(COLON) || match(SEMICOLON) || match(EQUAL) || m_lexer.hasLineTerminatorBeforeToken()) {
                        ident = &m_vm->propertyNames->async;
                        break;
                    }
					if (Options::useAsyncIterator() && UNLIKELY(consume(TIMES)))
						parseMode = SourceParseMode::AsyncGeneratorWrapperMethodMode;
					else
						parseMode = SourceParseMode::AsyncMethodMode;
					goto parseMethod;
				}
            }
            FALLTHROUGH;
        case AWAIT: {
            ident = m_token.m_data.ident;
            bool escaped = m_token.m_data.escaped;
            ASSERT(ident);
            next();
            if (parseMode == SourceParseMode::MethodMode && !escaped && (matchIdentifierOrKeyword() || match(STRING) || match(DOUBLE) || match(INTEGER) || match(BIGINT) || match(OPENBRACKET) || (Options::usePrivateMethods() && match(PRIVATENAME)))) {
                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;
        case OPENBRACKET:
            next();
            semanticFailIfTrue(currentScope()->isStaticBlock() && match(IDENT) && isArgumentsIdentifier(), ASCIILiteral("Cannot use 'arguments' as an identifier in static block"));
			computedPropertyName = parseAssignmentExpression(context);
            type = static_cast<PropertyNode::Type>(type | PropertyNode::Computed);
            failIfFalse(computedPropertyName, ASCIILiteral("Cannot parse computed property name"));
            handleProductionOrFail(CLOSEBRACKET, ASCIILiteral("]"), ASCIILiteral("end"), ASCIILiteral("computed property name"));
            break;
        case PRIVATENAME: {
            ident = m_token.m_data.ident;
            failIfTrue(isGetter || isSetter, ASCIILiteral("Cannot parse class method with private name"));
            ASSERT(ident);
            next();
            if (Options::usePrivateMethods() && match(OPENPAREN)) {
                semanticFailIfTrue(classScope->declarePrivateMethod(*ident, tag) & DeclarationResult::InvalidDuplicateDeclaration, ASCIILiteral("Cannot declare private method twice"));
                semanticFailIfTrue(*ident == propertyNames.constructorPrivateField, ASCIILiteral("Cannot declare a private method named '#constructor'"));

                if (tag == ClassElementTag::Static)
                	declaresStaticPrivateMethod = true;
                else
                    declaresPrivateMethod = true;

                type = static_cast<PropertyNode::Type>(type | PropertyNode::PrivateMethod);
                break;
            }

            failIfTrue(match(OPENPAREN), ASCIILiteral("Cannot parse class method with private name"));
            semanticFailIfTrue(classScope->declarePrivateField(*ident) & DeclarationResult::InvalidDuplicateDeclaration, ASCIILiteral("Cannot declare private field twice"));
            type = static_cast<PropertyNode::Type>(type | PropertyNode::PrivateField);
            break;
        }
        case OPENBRACE:
            failIfFalse(parseMode == SourceParseMode::ClassStaticBlockMode, ASCIILiteral("Cannot parse static block without 'static'"));
            type = static_cast<PropertyNode::Type>(type | PropertyNode::Block);
            break;
        default:
            if (m_token.m_type & KeywordTokenFlag)
                goto namedKeyword;
            failDueToUnexpectedToken();
        }

        TreeProperty property;
        if (isGetter || isSetter) {
            if (Options::usePrivateMethods() && match(PRIVATENAME)) {
                ident = m_token.m_data.ident;

                auto declarationResult = isSetter ? classScope->declarePrivateSetter(*ident, tag) : classScope->declarePrivateGetter(*ident, tag);
                semanticFailIfTrue(declarationResult & DeclarationResult::InvalidDuplicateDeclaration, ASCIILiteral("Declared private setter with an already used name"));
                if (tag == ClassElementTag::Static) {
                    semanticFailIfTrue(declarationResult & DeclarationResult::InvalidPrivateStaticNonStatic, ASCIILiteral("Cannot declare a private static "), (isSetter ? "setter" : "getter")  , " if there is a non-static private ", (isSetter ? "getter" : "setter"), " with used name");
                    declaresStaticPrivateAccessor = true;
                } else {
                    semanticFailIfTrue(declarationResult & DeclarationResult::InvalidPrivateStaticNonStatic, ASCIILiteral("Cannot declare a private non-static "), (isSetter ? "setter" : "getter"), " if there is a static private ", (isSetter ? "getter" : "setter"), " with used name");
                    declaresPrivateAccessor = true;
                }

                if (isSetter)
                    type = static_cast<PropertyNode::Type>(type | PropertyNode::PrivateSetter);
                else
                    type = static_cast<PropertyNode::Type>(type | PropertyNode::PrivateGetter);
            } else {
				type = static_cast<PropertyNode::Type>(type & ~PropertyNode::Constant);
				type = static_cast<PropertyNode::Type>(type | (isGetter ? PropertyNode::Getter : PropertyNode::Setter));
            }
            property = parseGetterSetter(context, type, methodStart, ConstructorKind::None, tag);
            failIfFalse(property, ASCIILiteral("Cannot parse this method"));
        } else if (!match(OPENPAREN) && parseMode == SourceParseMode::MethodMode) {
            ASSERT(!isGetter && !isSetter);
            if (ident) {
                semanticFailIfTrue(*ident == propertyNames.constructor, ASCIILiteral("Cannot declare class field named 'constructor'"));
                semanticFailIfTrue(*ident == propertyNames.constructorPrivateField, ASCIILiteral("Cannot declare private class field named '#constructor'"));
                if (tag == ClassElementTag::Static)
					semanticFailIfTrue(*ident == propertyNames.prototype, ASCIILiteral("Cannot declare a static field named 'prototype'"));
			}

            if (computedPropertyName) {
            	if (tag == ClassElementTag::Instance)
					ident = &m_parserArena.identifierArena().makePrivateIdentifier(*m_vm, instanceComputedNamePrefix, nextInstanceComputedFieldID++);
				else
					ident = &m_parserArena.identifierArena().makePrivateIdentifier(*m_vm, staticComputedNamePrefix, nextStaticComputedFieldID++);
				DeclarationResultMask declarationResult = classScope->declareLexicalVariable(ident, true);
                ASSERT_UNUSED(declarationResult, declarationResult == DeclarationResult::Valid);
                classScope->useVariable(ident, false);
                classScope->addClosedVariableCandidateUnconditionally(ident->impl());
            }

            TreeExpression initializer = 0;
            if (consume(EQUAL)) {
                size_t usedVariablesSize = currentScope()->currentUsedVariablesSize();
                currentScope()->pushUsedVariableSet();
                SetForScope<bool> overrideParsingClassFieldInitializer(m_parserState.isParsingClassFieldInitializer, true);
                classScope->setExpectedSuperBinding(SuperBinding::Needed);
                initializer = parseAssignmentExpression(context);
                classScope->setExpectedSuperBinding(SuperBinding::NotNeeded);
                failIfFalse(initializer, ASCIILiteral("Cannot parse initializer for class field"));
                classScope->markLastUsedVariablesSetAsCaptured(usedVariablesSize);
            }
            failIfFalse(autoSemiColon(), ASCIILiteral("Expected a ';' following a class field"));
            auto inferName = initializer ? InferName::Allowed : InferName::Disallowed;
            if (computedPropertyName)
                property = context.createProperty(ident, computedPropertyName, initializer, type, SuperBinding::NotNeeded, tag);
            else
                property = context.createProperty(ident, initializer, type, SuperBinding::NotNeeded, inferName, tag);
        } else if (parseMode == SourceParseMode::ClassStaticBlockMode) {
			matchOrFail(OPENBRACE, ASCIILiteral("Expected block statement for class static block"));
			size_t usedVariablesSize = currentScope()->currentUsedVariablesSize();
			currentScope()->pushUsedVariableSet();
			SetForScope<SourceParseMode> parsingWithClassStaticBlockMode(m_parseMode, parseMode);
			DepthManager statementDepth(&m_statementDepth);
            m_statementDepth = 0;
			failIfFalse(parseBlockStatement(context, BlockType::StaticBlock), ASCIILiteral("Cannot parse class static block"));
			property = context.createProperty(ident, type, SuperBinding::Needed, tag);
			classScope->markLastUsedVariablesSetAsCaptured(usedVariablesSize);
		} else {
            ParserFunctionInfo<TreeBuilder> methodInfo;
            bool isConstructor = tag == ClassElementTag::Instance && *ident == propertyNames.constructor;
            semanticFailIfTrue(isConstructor && parseMode != SourceParseMode::MethodMode,
				"Cannot declare ", stringArticleForFunctionMode(parseMode), stringForFunctionMode(parseMode), " named 'constructor'");

            methodInfo.name = isConstructor ? info.className : ident;
            SetForScope<SourceParseMode> innerParseMode(m_parseMode, parseMode);
            failIfFalse((parseFunctionInfo(context, FunctionNameRequirements::Unnamed, false, isConstructor ? constructorKind : ConstructorKind::None, SuperBinding::Needed, methodStart, methodInfo, FunctionDefinitionType::Method)), ASCIILiteral("Cannot parse this method"));

            TreeExpression method = context.createMethodDefinition(methodLocation, methodInfo);
            if (isConstructor) {
                semanticFailIfTrue(constructor, ASCIILiteral("Cannot declare multiple constructors in a single class"));
                constructor = method;
                continue;
            }

            semanticFailIfTrue(tag == ClassElementTag::Static && methodInfo.name && *methodInfo.name == propertyNames.prototype,
				ASCIILiteral("Cannot declare a static method named 'prototype'"));

            if (computedPropertyName) {
				property = context.createProperty(computedPropertyName, method, type, SuperBinding::Needed, tag);
			} else {
				property = context.createProperty(methodInfo.name, method, type, SuperBinding::Needed, InferName::Allowed, tag);
			}
        }

        if (classElementsTail)
            classElementsTail = context.createPropertyList(methodLocation, property, classElementsTail);
        else
            classElements = classElementsTail = context.createPropertyList(methodLocation, property);
    }

    info.endOffset = tokenLocation().endOffset - 1;
	consumeOrFail(CLOSEBRACE, ASCIILiteral("Expected a closing '}' after a class body"));

	if (declaresPrivateMethod || declaresPrivateAccessor || declaresStaticPrivateMethod || declaresStaticPrivateAccessor) {
		{
			Identifier privateBrandIdentifier = m_vm->propertyNames->builtinNames().privateBrandPrivateName();
			DeclarationResultMask declarationResult = classScope->declareLexicalVariable(&privateBrandIdentifier, true);
			ASSERT_UNUSED(declarationResult, declarationResult == DeclarationResult::Valid);
			classScope->useVariable(&privateBrandIdentifier, false);
			classScope->addClosedVariableCandidateUnconditionally(privateBrandIdentifier.impl());
		}
		{
			Identifier privateClassBrandIdentifier = m_vm->propertyNames->builtinNames().privateClassBrandPrivateName();
			DeclarationResultMask declarationResult = classScope->declareLexicalVariable(&privateClassBrandIdentifier, true);
			ASSERT_UNUSED(declarationResult, declarationResult == DeclarationResult::Valid);
			classScope->useVariable(&privateClassBrandIdentifier, false);
			classScope->addClosedVariableCandidateUnconditionally(privateClassBrandIdentifier.impl());
		}
	}

    context.setHasPrivateAccessors(classElements, declaresPrivateAccessor || declaresStaticPrivateAccessor);

    auto lexicalEnvironment = popScope(classScope, TreeBuilder::NeedsFreeVariableInfo).lexicalEnvironment;
    auto classHeadEnvironment = popScope(classHeadScope, TreeBuilder::NeedsFreeVariableInfo).lexicalEnvironment;
    ASSERT(functionDeclarations.isEmpty());
    ASSERT(classHeadFunctionDeclarations.isEmpty());
    return context.createClassExpr(location, info, WTFMove(classHeadEnvironment), WTFMove(lexicalEnvironment), constructor, parentClass, classElements);
}

template <typename LexerType>
template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseClassFieldInitializerSourceElements(TreeBuilder& context, const Vector<JSTextPosition>& classFieldLocations)
{
    TreeSourceElements sourceElements = context.createSourceElements();
    currentScope()->setIsClassScope();

    unsigned numComputedFields = 0;
    for (auto location : classFieldLocations) {
        // This loop will either parse only static fields or only
        // instance fields, but never a mix; we could make it slightly
        // smarter about parsing given that fact, but it's probably
        // not worth the hassle, so begin each iteration without
        // knowing which kind the next field will be.
        bool isStaticField = false;
        // We don't need to worry about hasLineTerminatorBeforeToken
        // on class fields, so we set this value to false.
        LexerState lexerState { location.offset, static_cast<unsigned>(location.lineStartOffset), static_cast<unsigned>(location.line), static_cast<unsigned>(location.line), false };
        restoreLexerState(lexerState);

        JSTokenLocation fieldLocation = tokenLocation();
        const Identifier* ident = nullptr;
        DefineFieldNode::Type type = DefineFieldNode::Type::Name;

        if (match(RESERVED_IF_STRICT) && *m_token.m_data.ident == m_vm->propertyNames->staticKeyword) {
            auto* staticIdentifier = m_token.m_data.ident;
            ASSERT(staticIdentifier);
            next();
            if (match(SEMICOLON) || match (EQUAL) || match(CLOSEBRACE) || m_lexer.hasLineTerminatorBeforeToken())
                ident = staticIdentifier;
            else
                isStaticField = true;
        }

        if (!ident) {
			switch (m_token.m_type) {
			case PRIVATENAME:
				type = DefineFieldNode::Type::PrivateName;
				FALLTHROUGH;
			case STRING:
			case IDENT:
			namedKeyword:
				ident = m_token.m_data.ident;
				ASSERT(ident);
				next();
				break;
			case BIGINT:
				ident = &m_parserArena.identifierArena().makeBigIntDecimalIdentifier(const_cast<VM&>(*m_vm), *m_token.m_data.bigIntString, m_token.m_data.radix);
				ASSERT(ident);
				next();
				break;
			case DOUBLE:
			case INTEGER:
				ident = &m_parserArena.identifierArena().makeNumericIdentifier(const_cast<VM*>(m_vm), m_token.m_data.doubleValue);
				ASSERT(ident);
				next();
				break;
			case OPENBRACKET: {
				next();
				TreeExpression computedPropertyName = parseAssignmentExpression(context);
				failIfFalse(computedPropertyName, ASCIILiteral("Cannot parse computed property name"));
				handleProductionOrFail(CLOSEBRACKET, "]", "end", "computed property name");
				ident = &m_parserArena.identifierArena().makePrivateIdentifier(*m_vm,
					isStaticField ? staticComputedNamePrefix : instanceComputedNamePrefix,
					numComputedFields++);
				type = DefineFieldNode::Type::ComputedName;
				break;
			}
            case OPENBRACE:
                RELEASE_ASSERT(isStaticField);
                break;
			default:
				if (m_token.m_type & KeywordTokenFlag)
					goto namedKeyword;
				failDueToUnexpectedToken();
			}
        }

        // Only valid class fields are handled in this function.
        ASSERT(match(EQUAL) || match(SEMICOLON) || match(CLOSEBRACE) || match(OPENBRACE) || m_lexer->hasLineTerminatorBeforeToken());

        TreeStatement statement;
        if (match(OPENBRACE) && isStaticField) {
            JSTextPosition startPosition = tokenStartPosition();
            JSTokenLocation startLocation(tokenLocation());
            unsigned expressionStart = tokenStart();

            ParserFunctionInfo<TreeBuilder> functionInfo;
            functionInfo.name = &m_vm->propertyNames->nullIdentifier;
            SetForScope<SourceParseMode> setInnerParseMode(m_parseMode, SourceParseMode::ClassStaticBlockMode);
            failIfFalse((parseFunctionInfo(context, FunctionNameRequirements::None, false, ConstructorKind::None, SuperBinding::Needed, expressionStart, functionInfo, FunctionDefinitionType::Expression)), ASCIILiteral("Cannot parse static block function"));
            TreeExpression expression = context.createFunctionExpr(startLocation, functionInfo);

            JSTextPosition endPosition = lastTokenEndPosition();
            expression = context.makeStaticBlockFunctionCallNode(startLocation, expression, endPosition, startPosition, lastTokenEndPosition());

            statement = context.createExprStatement(startLocation, expression, startPosition, m_lastTokenEndPosition.line);
        } else {
			TreeExpression initializer = 0;
			if (consume(EQUAL))
				initializer = parseAssignmentExpression(context);

			if (type == DefineFieldNode::Type::PrivateName)
				currentScope()->useVariable(ident, false);

			statement = context.createDefineField(fieldLocation, ident, initializer, type);
		}
		context.appendStatement(sourceElements, statement);
    }

    ASSERT(!hasError());
    // Trick parseInner() into believing we've parsed the entire SourceCode, in order to prevent it from producing an error.
    m_token.m_type = EOFTOK;
    return sourceElements;
}

namespace {
struct LabelInfo {
    LabelInfo(const Identifier* ident, const JSTextPosition& start, const JSTextPosition& end)
    : m_ident(ident)
    , m_start(start)
    , m_end(end)
    {
    }
    
    const Identifier* m_ident;
    JSTextPosition m_start;
    JSTextPosition m_end;
};
}

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseExpressionOrLabelStatement(TreeBuilder& context, bool allowFunctionDeclarationAsStatement)
{
    
    /* Expression and Label statements are ambiguous at LL(1), so we have a
     * special case that looks for a colon as the next character in the input.
     */
    Vector<LabelInfo> labels;
    JSTokenLocation location;
    do {
        if (!nextTokenIsColon()) {
            // If we hit this path we're making a expression statement, which
            // by definition can't make use of continue/break so we can just
            // ignore any labels we might have accumulated.
            return parseExpressionStatement(context);
        }

        semanticFailIfTrue(isPossiblyEscapedLet(m_token) && strictMode(), ASCIILiteral("Cannot use 'let' as a label "), disallowedIdentifierLetReason());
        semanticFailIfTrue(isDisallowedIdentifierAwait(m_token), ASCIILiteral("Cannot use 'await' as a label "), disallowedIdentifierAwaitReason());
        semanticFailIfTrue(isDisallowedIdentifierYield(m_token), ASCIILiteral("Cannot use 'yield' as a label "), disallowedIdentifierYieldReason());

        const Identifier* ident = m_token.m_data.ident;
        JSTextPosition start = tokenStartPosition();
        JSTextPosition end = tokenEndPosition();
        location = tokenLocation();
        next();
        consumeOrFail(COLON, ASCIILiteral("Labels must be followed by a ':'"));
		// This is O(N^2) over the current list of consecutive labels, but I
		// have never seen more than one label in a row in the real world.
		for (size_t i = 0; i < labels.size(); i++)
			failIfTrue(ident->impl() == labels[i].m_ident->impl(), ASCIILiteral("Attempted to redeclare the label '"), ident, ASCIILiteral("'"));
		failIfTrue(getLabel(ident), ASCIILiteral("Cannot find scope for the label '"), ident, ASCIILiteral("'"));
		labels.append(LabelInfo(ident, start, end));
    } while (matchSpecIdentifier());
    bool isLoop = false;
    switch (m_token.m_type) {
    case FOR:
    case WHILE:
    case DO:
        isLoop = true;
        break;
        
    default:
        break;
    }
    const Identifier* unused = 0;
    ScopeRef labelScope = currentScope();
	for (size_t i = 0; i < labels.size(); i++)
		pushLabel(labels[i].m_ident, isLoop);
    m_immediateParentAllowsFunctionDeclarationInStatement = allowFunctionDeclarationAsStatement;
    TreeStatement statement = parseStatement(context, unused);
	for (size_t i = 0; i < labels.size(); i++)
		popLabel(labelScope);
    failIfFalse(statement, ASCIILiteral("Cannot parse statement"));
    for (size_t i = 0; i < labels.size(); i++) {
        const LabelInfo& info = labels[labels.size() - i - 1];
        statement = context.createLabelStatement(location, info.m_ident, statement, info.m_start, info.m_end);
    }
    return statement;
}

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseExpressionStatement(TreeBuilder& context)
{
    switch (m_token.m_type) {
    // https://tc39.es/ecma262/#sec-expression-statement
    // Despite the spec's requirement to fail from FUNCTION token here, Annex B.3.1
    // permits a labelled FunctionDeclaration in sloppy mode for web compatibility
    // reasons. We implement this semantics in parseStatement().
    case CLASSTOKEN:
        failWithMessage(ASCIILiteral("'class' declaration is not directly within a block statement"));
        break;
    case LET: {
        SavePoint savePoint = createSavePoint(context);
        next();
        failIfTrue(match(OPENBRACKET), "Cannot use lexical declaration in single-statement context");
        restoreSavePoint(context, savePoint);
        break;
    }
    case IDENT:
        if (UNLIKELY(*m_token.m_data.ident == m_vm->propertyNames->async && !m_token.m_data.escaped)) {
            SavePoint savePoint = createSavePoint(context);
            next();
            failIfTrue(match(FUNCTION) && !m_lexer.hasLineTerminatorBeforeToken(), ASCIILiteral("Cannot use async function declaration in single-statement context"));
            restoreSavePoint(context, savePoint);
        }
        break;
    default:
        break;
    }

    JSTextPosition start = tokenStartPosition();
    JSTokenLocation location(tokenLocation());
    TreeExpression expression = parseExpression(context);
    failIfFalse(expression, ASCIILiteral("Cannot parse expression statement"));
    if (!autoSemiColon())
		failDueToUnexpectedToken();
    return context.createExprStatement(location, expression, start, m_lastTokenEndPosition.line);
}

template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseIfStatement(TreeBuilder& context)
{
    ASSERT(match(IF));
    JSTokenLocation ifLocation(tokenLocation());
    int start = tokenLine();
    next();
    handleProductionOrFail2(OPENPAREN, ASCIILiteral("("), ASCIILiteral("start"), ASCIILiteral("'if' condition"));
    
    TreeExpression condition = parseExpression(context);
    failIfFalse(condition, ASCIILiteral("Expected an expression as the condition for an if statement"));
    int end = tokenLine();
    handleProductionOrFail2(CLOSEPAREN, ASCIILiteral(")"), ASCIILiteral("end"), ASCIILiteral("'if' condition"));
    
    const Identifier* unused = 0;
    m_immediateParentAllowsFunctionDeclarationInStatement = true;
    TreeStatement trueBlock = parseStatement(context, unused);
    failIfFalse(trueBlock, ASCIILiteral("Expected a statement as the body of an if block"));
    
    if (!match(ELSE))
        return context.createIfStatement(ifLocation, condition, trueBlock, 0, start, end);
    
    Vector<TreeExpression> exprStack;
    Vector<pair<int, int> > posStack;
    Vector<JSTokenLocation> tokenLocationStack;
    Vector<TreeStatement> statementStack;
    bool trailingElse = false;
    do {
        JSTokenLocation tempLocation = tokenLocation();
        next();
        if (!match(IF)) {
            const Identifier* unused = 0;
            m_immediateParentAllowsFunctionDeclarationInStatement = true;
            TreeStatement block = parseStatement(context, unused);
            failIfFalse(block, ASCIILiteral("Expected a statement as the body of an else block"));
            statementStack.append(block);
            trailingElse = true;
            break;
        }
        int innerStart = tokenLine();
        next();

        handleProductionOrFail2(OPENPAREN, ASCIILiteral("("), ASCIILiteral("start"), ASCIILiteral("'if' condition"));

        TreeExpression innerCondition = parseExpression(context);
        failIfFalse(innerCondition, ASCIILiteral("Expected an expression as the condition for an if statement"));
        int innerEnd = tokenLine();
        handleProductionOrFail2(CLOSEPAREN, ASCIILiteral(")"), ASCIILiteral("end"), ASCIILiteral("'if' condition"));
        const Identifier* unused = 0;
        m_immediateParentAllowsFunctionDeclarationInStatement = true;
        TreeStatement innerTrueBlock = parseStatement(context, unused);
        failIfFalse(innerTrueBlock, ASCIILiteral("Expected a statement as the body of an if block"));
        tokenLocationStack.append(tempLocation);
        exprStack.append(innerCondition);
        posStack.append(make_pair(innerStart, innerEnd));
        statementStack.append(innerTrueBlock);
    } while (match(ELSE));
    
    if (!trailingElse) {
        TreeExpression condition = exprStack.last();
        exprStack.removeLast();
        TreeStatement trueBlock = statementStack.last();
        statementStack.removeLast();
        pair<int, int> pos = posStack.last();
        posStack.removeLast();
        JSTokenLocation elseLocation = tokenLocationStack.last();
        tokenLocationStack.removeLast();
        statementStack.append(context.createIfStatement(elseLocation, condition, trueBlock, 0, pos.first, pos.second));
    }
    
    while (!exprStack.isEmpty()) {
        TreeExpression condition = exprStack.last();
        exprStack.removeLast();
        TreeStatement falseBlock = statementStack.last();
        statementStack.removeLast();
        TreeStatement trueBlock = statementStack.last();
        statementStack.removeLast();
        pair<int, int> pos = posStack.last();
        posStack.removeLast();
        JSTokenLocation elseLocation = tokenLocationStack.last();
        tokenLocationStack.removeLast();
        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), ASCIILiteral("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 = 0;
    const Identifier* localName = 0;

    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), ASCIILiteral("Expected 'as' before imported binding name"));
        next();

        failIfFalse(matchSpecIdentifier(), ASCIILiteral("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();
            failIfFalse(matchSpecIdentifier(), ASCIILiteral("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(matchSpecIdentifier());
        localNameToken = m_token;
        localName = m_token.m_data.ident;
        importedName = &m_vm->propertyNames->defaultKeyword;
        next();
        break;
    }
    }

    semanticFailIfTrue(localNameToken.m_type == AWAIT, ASCIILiteral("Cannot use 'await' as an imported binding name"));
	semanticFailIfTrue(localNameToken.m_type & KeywordTokenFlag, ASCIILiteral("Cannot use keyword as imported binding name"));
    DeclarationResultMask declarationResult = declareVariable(localName, DeclarationType::ConstDeclaration, (specifierType == ImportSpecifierType::NamespaceImport) ? DeclarationImportType::ImportedNamespace : DeclarationImportType::Imported);
	if (declarationResult != DeclarationResult::Valid) {
        failIfTrueIfStrict(declarationResult & DeclarationResult::InvalidStrictMode, ASCIILiteral("Cannot declare an imported binding named "), localName, ASCIILiteral(" in strict mode"));
        if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration)
            internalFailWithMessage(false, ASCIILiteral("Cannot declare an imported binding name twice: '"), localName, ASCIILiteral("'"));
    }

    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, ASCIILiteral("Cannot parse the module name"));
        failIfFalse(autoSemiColon(), ASCIILiteral("Expected a ';' following a targeted import declaration"));
        return context.createImportDeclaration(importLocation, specifierList, moduleName);
    }

    bool isFinishedParsingImport = false;
    if (matchSpecIdentifier()) {
        // ImportedDefaultBinding :
        // ImportedBinding
        auto specifier = parseImportClauseItem(context, ImportSpecifierType::DefaultImport);
        failIfFalse(specifier, ASCIILiteral("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, ASCIILiteral("Cannot parse the namespace import"));
            context.appendImportSpecifier(specifierList, specifier);
        } else if (match(OPENBRACE)) {
            // NamedImports :
            // { }
            // { ImportsList }
            // { ImportsList , }
            next();

            while (!match(CLOSEBRACE)) {
                failIfFalse(matchIdentifierOrKeyword(), ASCIILiteral("Expected an imported name for the import declaration"));
                auto specifier = parseImportClauseItem(context, ImportSpecifierType::NamedImport);
                failIfFalse(specifier, ASCIILiteral("Cannot parse the named import"));
                context.appendImportSpecifier(specifierList, specifier);
                if (!consume(COMMA))
                    break;
            }
            handleProductionOrFail2(CLOSEBRACE, ASCIILiteral("}"), ASCIILiteral("end"), ASCIILiteral("import list"));
        } else
            failWithMessage(ASCIILiteral("Expected namespace import or import list"));
    }

    // FromClause :
    // from ModuleSpecifier

    failIfFalse(matchContextualKeyword(m_vm->propertyNames->from), ASCIILiteral("Expected 'from' before imported module name"));
    next();

    auto moduleName = parseModuleName(context);
    failIfFalse(moduleName, ASCIILiteral("Cannot parse the module name"));
    failIfFalse(autoSemiColon(), ASCIILiteral("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<std::pair<const Identifier*, const Identifier*>>& maybeExportedLocalNames, 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(), ASCIILiteral("Expected an exported name for the export declaration"));
        exportedName = m_token.m_data.ident;
        next();
    }

    semanticFailIfFalse(exportName(*exportedName), ASCIILiteral("Cannot export a duplicate name '"), exportedName, ASCIILiteral("'"));
    maybeExportedLocalNames.append(std::make_pair(localName, exportedName));
	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 ;
        // export * as IdentifierName FromClause ;
        next();

        const Identifier* exportedName = 0;
        JSTokenLocation specifierLocation;
        if (matchContextualKeyword(m_vm->propertyNames->as)) {
            next();
            specifierLocation = JSTokenLocation(tokenLocation());
            failIfFalse(matchIdentifierOrKeyword(), "Expected an exported name for the export declaration");
            exportedName = m_token.m_data.ident;
            next();
        }

        failIfFalse(matchContextualKeyword(m_vm->propertyNames->from), ASCIILiteral("Expected 'from' before exported module name"));
        next();
        auto moduleName = parseModuleName(context);
		failIfFalse(moduleName, ASCIILiteral("Cannot parse the 'from' clause"));
		failIfFalse(autoSemiColon(), ASCIILiteral("Expected a ';' following a targeted export declaration"));

        if (exportedName) {
            semanticFailIfFalse(exportName(*exportedName), "Cannot export a duplicate name '", exportedName->impl(), "'");
            auto specifierList = context.createExportSpecifierList();
            auto localName = &m_vm->propertyNames->starNamespacePrivateName;
            auto specifier = context.createExportSpecifier(specifierLocation, *localName, *exportedName);
            context.appendExportSpecifier(specifierList, specifier);
            return context.createExportNamedDeclaration(exportLocation, specifierList, moduleName);
        }

		return context.createExportAllDeclaration(exportLocation, moduleName);
	}

    case DEFAULT: {
        // export default HoistableDeclaration[~Yield, ~Await, +Default]
        // export default ClassDeclaration[~Yield, ~Await, +Default]
        // export default [lookahead not-in { function, async [no LineTerminator here] function, class }] AssignmentExpression[+In, ~Yield, ~Await]

        next();

        TreeStatement result = 0;
        bool isFunctionOrClassDeclaration = false;
        const Identifier* localName = 0;

        bool startsWithFunction = match(FUNCTION);
        if (startsWithFunction || match(CLASSTOKEN)) {
            SavePoint savePoint = createSavePoint(context);
            isFunctionOrClassDeclaration = true;
            next();
#if ENABLE(ES6_GENERATORS)
            // ES6 Generators
            if (startsWithFunction && match(TIMES))
                next();
#endif
            if (match(IDENT))
				localName = m_token.m_data.ident;
            restoreSavePoint(context, savePoint);
        } else if (matchContextualKeyword(m_vm->propertyNames->async)) {
            // export default async function xxx() { }
            // export default async function * yyy() { }
            SavePoint savePoint = createSavePoint(context);
            next();
            if (match(FUNCTION) && !m_lexer.hasLineTerminatorBeforeToken()) {
                next();
                // Async Generators
                if (match(TIMES))
                    next();
                if (match(IDENT))
                    localName = m_token.m_data.ident;
                isFunctionOrClassDeclaration = true;
            }
            restoreSavePoint(context, savePoint);
        }

        if (!localName)
            localName = &m_vm->propertyNames->starDefaultPrivateName;

        if (isFunctionOrClassDeclaration) {
            if (startsWithFunction) {
                ASSERT(match(FUNCTION));
                DepthManager statementDepth(&m_statementDepth);
                m_statementDepth = 1;
                result = parseFunctionDeclaration(context, FunctionDeclarationType::Declaration, ExportType::NotExported, DeclarationDefaultContext::ExportDefault);
            } else if (match(CLASSTOKEN)) {
                result = parseClassDeclaration(context, ExportType::NotExported, DeclarationDefaultContext::ExportDefault);
            } else {
            	ASSERT(matchContextualKeyword(m_vm->propertyNames->async));
				next();
                DepthManager statementDepth(&m_statementDepth);
                m_statementDepth = 1;
                result = parseAsyncFunctionDeclaration(context, ExportType::NotExported, DeclarationDefaultContext::ExportDefault);
            }
        } 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, ASCIILiteral("Cannot parse expression"));

            DeclarationResultMask declarationResult = declareVariable(&m_vm->propertyNames->starDefaultPrivateName, DeclarationType::ConstDeclaration);
            if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration)
            	internalFailWithMessage(false, ASCIILiteral("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());
			failIfFalse(autoSemiColon(), ASCIILiteral("Expected a ';' following a targeted export declaration"));
		}
        failIfFalse(result, ASCIILiteral("Cannot parse the declaration"));

        semanticFailIfFalse(exportName(m_vm->propertyNames->defaultKeyword), ASCIILiteral("Only one 'default' export is allowed"));
        m_moduleScopeData->exportBinding(*localName, m_vm->propertyNames->defaultKeyword);
		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<std::pair<const Identifier*, const Identifier*>> maybeExportedLocalNames;

        bool hasKeywordForLocalBindings = false;
        while (!match(CLOSEBRACE)) {
            failIfFalse(matchIdentifierOrKeyword(), ASCIILiteral("Expected a variable name for the export declaration"));
            auto specifier = parseExportSpecifier(context, maybeExportedLocalNames, hasKeywordForLocalBindings);
            failIfFalse(specifier, ASCIILiteral("Cannot parse the named export"));
            context.appendExportSpecifier(specifierList, specifier);
            if (!consume(COMMA))
                break;
        }
        handleProductionOrFail2(CLOSEBRACE, ASCIILiteral("}"), ASCIILiteral("end"), ASCIILiteral("export list"));

        typename TreeBuilder::ModuleName moduleName = 0;
        if (matchContextualKeyword(m_vm->propertyNames->from)) {
            next();
            moduleName = parseModuleName(context);
            failIfFalse(moduleName, ASCIILiteral("Cannot parse the 'from' clause"));
        }
        failIfFalse(autoSemiColon(), ASCIILiteral("Expected a ';' following a targeted export declaration"));

        if (!moduleName) {
            semanticFailIfTrue(hasKeywordForLocalBindings, ASCIILiteral("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(int i = 0; i < maybeExportedLocalNames.size(); ++i) {
            	const auto& pair = maybeExportedLocalNames[i];
                const Identifier* localName = pair.first;
                const Identifier* exportedName = pair.second;
                m_moduleScopeData->exportBinding(*localName, *exportedName);
            }
        }

        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: {
            DepthManager statementDepth(&m_statementDepth);
            m_statementDepth = 1;
            result = parseFunctionDeclaration(context, FunctionDeclarationType::Declaration, ExportType::Exported);
            break;
        }

        case CLASSTOKEN:
            result = parseClassDeclaration(context, ExportType::Exported);
            break;

        case IDENT:
            if (*m_token.m_data.ident == m_vm->propertyNames->async && !m_token.m_data.escaped) {
                next();
				semanticFailIfFalse(match(FUNCTION) && !m_lexer.hasLineTerminatorBeforeToken(), ASCIILiteral("Expected 'function' keyword following 'async' keyword with no preceding line terminator"));
				DepthManager statementDepth(&m_statementDepth);
				m_statementDepth = 1;
				result = parseAsyncFunctionDeclaration(context, ExportType::Exported);
				break;
            }
            FALLTHROUGH;
        default:
            failWithMessage(ASCIILiteral("Expected either a declaration or a variable statement"));
            break;
        }

        failIfFalse(result, ASCIILiteral("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)
{
	propagateError();
    failIfStackOverflow();
    JSTokenLocation location(tokenLocation());
    TreeExpression node = parseAssignmentExpression(context);
    failIfFalse(node, ASCIILiteral("Cannot parse expression"));
    context.setEndOffset(node, m_lexer.currentOffset());
    if (!match(COMMA))
        return node;
    next();
    m_parserState.nonTrivialExpressionCount++;
    m_parserState.nonLHSCount++;
    TreeExpression right = parseAssignmentExpression(context);
    failIfFalse(right, ASCIILiteral("Cannot parse expression in a comma expression"));
    context.setEndOffset(right, m_lexer.currentOffset());
    typename TreeBuilder::Comma head = context.createCommaExpr(location, node);
	typename TreeBuilder::Comma tail = context.appendToCommaExpr(location, head, head, right);
    failIfFalse(head, ASCIILiteral("Out of memory"));
    while (match(COMMA)) {
        next(TreeBuilder::DontBuildStrings());
        right = parseAssignmentExpression(context);
        failIfFalse(right, ASCIILiteral("Cannot parse expression in a comma expression"));
        context.setEndOffset(right, m_lexer.currentOffset());
        tail = context.appendToCommaExpr(location, head, tail, right);
    }
    context.setEndOffset(head, m_lexer.currentOffset());
    return head;
}

template <typename LexerType>
template <typename TreeBuilder> ALWAYS_INLINE TreeExpression Parser<LexerType>::parseAssignmentExpressionOrPropagateErrorClass(TreeBuilder& context)
{
    ExpressionErrorClassifier classifier(this);
    auto assignment = parseAssignmentExpression(context, classifier);
    if (UNLIKELY(!assignment))
        classifier.propagateExpressionErrorClass();
    return assignment;
}

template <typename LexerType>
template <typename TreeBuilder> TreeExpression Parser<LexerType>::parseAssignmentExpression(TreeBuilder& context)
{
    ExpressionErrorClassifier classifier(this);
    return parseAssignmentExpression(context, classifier);
}

template <typename LexerType>
template <typename TreeBuilder> ASCIILiteral Parser<LexerType>::metaPropertyName(TreeBuilder& context, TreeExpression expr)
{
    if (context.isNewTarget(expr))
        return ASCIILiteral("new.target");
    if (context.isImportMeta(expr))
        return ASCIILiteral("import.meta");
    RELEASE_ASSERT_NOT_REACHED();
    return ASCIILiteral("error");
}

template <typename LexerType>
template <typename TreeBuilder> ALWAYS_INLINE bool Parser<LexerType>::isSimpleAssignmentTarget(TreeBuilder& context, TreeExpression expr)
{
    // Web compatibility concerns prevent us from handling a function call LHS as an early error in sloppy mode.
    // This behavior is currently unspecified, but see: https://github.com/tc39/ecma262/issues/257#issuecomment-195106880
    return context.isLocation(expr) || (!strictMode() && context.isFunctionCall(expr));
}

template <typename LexerType>
template <typename TreeBuilder> TreeExpression Parser<LexerType>::parseAssignmentExpression(TreeBuilder& context, ExpressionErrorClassifier& classifier)
{
    ASSERT(!hasError());

    failIfStackOverflow();

    if (match(YIELD) && !canUseIdentifierYield())
        return parseYieldExpression(context);

    JSTextPosition start = tokenStartPosition();
    JSTokenLocation location(tokenLocation());
    int initialAssignmentCount = m_parserState.assignmentCount;
    int initialNonLHSCount = m_parserState.nonLHSCount;
    bool maybeAssignmentPattern = match(OPENBRACE) || match(OPENBRACKET);
    bool wasOpenParen = match(OPENPAREN);
    // Do not use matchSpecIdentifier() here since it is slower than isIdentifierOrKeyword.
    // Whether spec identifier is will be validated by isArrowFunctionParameters().
    bool wasIdentifierOrKeyword = matchIdentifierOrKeyword() || (m_token.m_type == ESCAPED_KEYWORD);
	bool maybeValidArrowFunctionStart = wasOpenParen || wasIdentifierOrKeyword;
    SavePoint savePoint = createSavePoint(context);
    size_t usedVariablesSize = 0;

    if (wasOpenParen) {
        usedVariablesSize = currentScope()->currentUsedVariablesSize();
        currentScope()->pushUsedVariableSet();
    }

    TreeExpression lhs = parseConditionalExpression(context);

    // Current implementation of parseAssignmentExpression causes a weird parsing loop
    // for this example:
    //
    //      class C {
    //          static {
    //              ((x = await) => 0);
    //          }
    //      }
    //
    // which makes the 'await' error caught in parseConditionalExpression escaping from
    // parseAssignmentExpression. Therefore, we need to capture the error directly after
    // parseConditionalExpression. Besides, the usage of `await` is strictly limited in
    // class static block.
    if (UNLIKELY(!lhs && currentScope()->isStaticBlock() && match(AWAIT)))
        propagateError();

    if (maybeValidArrowFunctionStart && !match(EOFTOK)) {
        bool isArrowFunctionToken = match(ARROWFUNCTION);
        if (!lhs || isArrowFunctionToken) {
            SavePointWithError errorRestorationSavePoint = swapSavePointForError(context, savePoint);
            bool isAsyncArrow = false;
            if (UNLIKELY(classifier.indicatesPossibleAsyncArrowFunction())) {
                if (matchContextualKeyword(m_vm->propertyNames->async)) {
                	next();
                	isAsyncArrow = !m_lexer.hasLineTerminatorBeforeToken();
                }
            }
            if (isArrowFunctionParameters(context)) {
                if (wasOpenParen)
                    currentScope()->revertToPreviousUsedVariables(usedVariablesSize);
                return parseArrowFunctionExpression(context, isAsyncArrow, location);
            }
            if (isArrowFunctionToken)
                propagateError();
            restoreSavePointWithError(context, errorRestorationSavePoint);
            if (isArrowFunctionToken)
                failDueToUnexpectedToken();
        }
    }

    if (!lhs && (!maybeAssignmentPattern || !classifier.indicatesPossiblePattern()))
        propagateError();

    if (maybeAssignmentPattern && (!lhs || (context.isObjectOrArrayLiteral(lhs) && match(EQUAL)))) {
    	SavePointWithError expressionErrorLocation = swapSavePointForError(context, savePoint);
        auto pattern = tryParseDestructuringPatternExpression(context, AssignmentContext::AssignmentExpression);
        if (classifier.indicatesPossiblePattern() && (!pattern || !match(EQUAL))) {
            restoreSavePointWithError(context, expressionErrorLocation);
            return 0;
        }
        failIfFalse(pattern, ASCIILiteral("Cannot parse assignment pattern"));
        consumeOrFail(EQUAL, ASCIILiteral("Expected '=' following assignment pattern"));
        auto rhs = parseAssignmentExpression(context);
        if (!rhs)
            propagateError();
        return context.createDestructuringAssignment(location, pattern, rhs);
    }

    failIfFalse(lhs, ASCIILiteral("Cannot parse expression"));
    if (initialNonLHSCount != m_parserState.nonLHSCount){
        if (m_token.m_type >= EQUAL && m_token.m_type <= ANDEQUAL)
            semanticFail(ASCIILiteral("Left hand side of operator '"), getToken(), ASCIILiteral("' must be a reference"));

        return lhs;
    }
    
    int assignmentStack = 0;
    Operator op;
    bool hadAssignment = false;
    while (true) {
        switch (m_token.m_type) {
        case EQUAL: op = Operator::Equal; break;
        case PLUSEQUAL: op = Operator::PlusEq; break;
        case MINUSEQUAL: op = Operator::MinusEq; break;
        case MULTEQUAL: op = Operator::MultEq; break;
        case DIVEQUAL: op = Operator::DivEq; break;
        case LSHIFTEQUAL: op = Operator::LShift; break;
        case RSHIFTEQUAL: op = Operator::RShift; break;
        case URSHIFTEQUAL: op = Operator::URShift; break;
        case BITANDEQUAL: op = Operator::BitAndEq; break;
        case BITXOREQUAL: op = Operator::BitXOrEq; break;
        case BITOREQUAL: op = Operator::BitOrEq; break;
        case MODEQUAL: op = Operator::ModEq; break;
        case POWEQUAL: op = Operator::PowEq; break;
        case COALESCEEQUAL: op = Operator::CoalesceEq; break;
        case OREQUAL: op = Operator::OrEq; break;
        case ANDEQUAL: op = Operator::AndEq; break;
        default:
            goto end;
        }
        m_parserState.nonTrivialExpressionCount++;
        hadAssignment = true;
		semanticFailIfTrue(context.isMetaProperty(lhs), metaPropertyName(context, lhs), ASCIILiteral(" can't be the left hand side of an assignment expression"));
		semanticFailIfFalse(isSimpleAssignmentTarget(context, lhs), ASCIILiteral("Left side of assignment is not a reference"));
        context.assignmentStackAppend(assignmentStack, lhs, start, tokenStartPosition(), m_parserState.assignmentCount, op);
        start = tokenStartPosition();
        m_parserState.assignmentCount++;
        next(TreeBuilder::DontBuildStrings());
        if (strictMode() && m_parserState.lastIdentifier && context.isResolve(lhs)) {
        	failIfTrueIfStrict(m_vm->propertyNames->eval == *m_parserState.lastIdentifier, ASCIILiteral("Cannot modify 'eval' in strict mode"));
        	failIfTrueIfStrict(m_vm->propertyNames->arguments == *m_parserState.lastIdentifier, ASCIILiteral("Cannot modify 'arguments' in strict mode"));
            m_parserState.lastIdentifier = 0;
        }
        lhs = parseAssignmentExpression(context);
        failIfFalse(lhs, ASCIILiteral("Cannot parse the right hand side of an assignment expression"));
        if (initialNonLHSCount != m_parserState.nonLHSCount) {
            if (m_token.m_type >= EQUAL && m_token.m_type <= ANDEQUAL)
            	semanticFail(ASCIILiteral("Left hand side of operator '"), getToken(), ASCIILiteral("' must be a reference"));
            break;
        }
    }
end:
    if (hadAssignment)
        m_parserState.nonLHSCount++;

    while (assignmentStack)
        lhs = context.createAssignment(location, assignmentStack, lhs, initialAssignmentCount, m_parserState.assignmentCount, lastTokenEndPosition());
    
    return lhs;
}

template <typename LexerType>
template <class TreeBuilder> TreeExpression Parser<LexerType>::parseYieldExpression(TreeBuilder& context)
{
    // YieldExpression[In] :
    //     yield
    //     yield [no LineTerminator here] AssignmentExpression[?In, Yield]
    //     yield [no LineTerminator here] * AssignmentExpression[?In, Yield]

    // http://ecma-international.org/ecma-262/6.0/#sec-generator-function-definitions
    failIfFalse(currentScope()->isGenerator() && !currentScope()->isArrowFunctionBoundary(), ASCIILiteral("Cannot use yield expression out of generator"));

    // http://ecma-international.org/ecma-262/6.0/#sec-generator-function-definitions-static-semantics-early-errors
    failIfTrue(m_parserState.functionParsePhase == FunctionParsePhase::Parameters, ASCIILiteral("Cannot use yield expression within parameters"));

    JSTokenLocation location(tokenLocation());
    JSTextPosition divotStart = tokenStartPosition();
    ASSERT(match(YIELD));
    SavePoint savePoint = createSavePoint(context);
    next();
    if (m_lexer.hasLineTerminatorBeforeToken())
        return context.createYield(location);

    bool delegate = consume(TIMES);
    JSTextPosition argumentStart = tokenStartPosition();
    TreeExpression argument = parseAssignmentExpression(context);
    if (!argument) {
        restoreSavePoint(context, savePoint);
        next();
        return context.createYield(location);
    }
    return context.createYield(location, argument, delegate, divotStart, argumentStart, lastTokenEndPosition());
}

template <typename LexerType>
template <class TreeBuilder> TreeExpression Parser<LexerType>::parseAwaitExpression(TreeBuilder& context)
{
    ASSERT(match(AWAIT));
    ASSERT(currentScope()->isAsyncFunction() || isModuleParseMode(sourceParseMode()));
	ASSERT(isAsyncFunctionParseMode(sourceParseMode()) || isModuleParseMode(sourceParseMode()));
	ASSERT(m_parserState.functionParsePhase != FunctionParsePhase::Parameters);
    JSTokenLocation location(tokenLocation());
    JSTextPosition divotStart = tokenStartPosition();
    next();
    JSTextPosition argumentStart = tokenStartPosition();
    ExpressionErrorClassifier classifier(this);
    TreeExpression argument = parseUnaryExpression(context);
    failIfFalse(argument, ASCIILiteral("Failed to parse await expression"));
    return context.createAwait(location, argument, divotStart, argumentStart, lastTokenEndPosition());
}

template <typename LexerType>
template <class TreeBuilder> TreeExpression Parser<LexerType>::parseConditionalExpression(TreeBuilder& context)
{
    JSTokenLocation location(tokenLocation());
    TreeExpression cond = parseBinaryExpression(context);
    failIfFalse(cond, ASCIILiteral("Cannot parse expression"));
    if (!match(QUESTION))
        return cond;
    m_parserState.nonTrivialExpressionCount++;
    m_parserState.nonLHSCount++;
    next(TreeBuilder::DontBuildStrings());
    TreeExpression lhs = 0;
    {
        // this block is necessary so that we don't leave `in` enabled for the rhs
        AllowInOverride allowInOverride(this);
        lhs = parseAssignmentExpression(context);
    }
    failIfFalse(lhs, ASCIILiteral("Cannot parse left hand side of ternary operator"));
    context.setEndOffset(lhs, m_lastTokenEndPosition.offset);
	consumeOrFailWithFlags(COLON, TreeBuilder::DontBuildStrings(), ASCIILiteral("Expected ':' in ternary operator"));
    
    TreeExpression rhs = parseAssignmentExpression(context);
    failIfFalse(rhs, ASCIILiteral("Cannot parse right hand side of ternary operator"));
    context.setEndOffset(rhs, m_lastTokenEndPosition.offset);
    return context.createConditionalExpr(location, cond, lhs, rhs);
}

ALWAYS_INLINE static bool isUnaryOpExcludingUpdateOp(JSTokenType token)
{
    if (isUpdateOp(token))
        return false;
    return isUnaryOp(token);
}

template <typename LexerType>
ALWAYS_INLINE int Parser<LexerType>::isBinaryOperator(JSTokenType token)
{
    if (m_allowsIn)
        return token & (BinaryOpTokenPrecedenceMask << BinaryOpTokenAllowsInPrecedenceAdditionalShift);
    return token & BinaryOpTokenPrecedenceMask;
}

template <typename LexerType>
template <class TreeBuilder> TreeExpression Parser<LexerType>::parseBinaryExpression(TreeBuilder& context)
{
    int operandStackDepth = 0;
    int operatorStackDepth = 0;
    typename TreeBuilder::BinaryExprContext binaryExprContext(context);
    JSTokenLocation location(tokenLocation());
    bool hasLogicalOperator = false;
    bool hasCoalesceOperator = false;

    int previousOperator = 0;
    while (true) {
    	propagateError();
        JSTextPosition exprStart = tokenStartPosition();
        int initialAssignments = m_parserState.assignmentCount;
        JSTokenType leadingTokenTypeForUnaryExpression = m_token.m_type;

        TreeExpression current = 0;
        if (Options::usePrivateIn() && match(PRIVATENAME)) {
            const Identifier* ident = m_token.m_data.ident;
            ASSERT(ident);
            currentScope()->usePrivateName(*ident);
            m_seenPrivateNameUseInNonReparsingFunctionMode = true;
            next();
            semanticFailIfTrue(m_token.m_type != INTOKEN || previousOperator >= INTOKEN, ASCIILiteral("Bare private name can only be used as the left-hand side of an `in` expression"));
            current = context.createPrivateIdentifierNode(location, *ident);
        } else
            current = parseUnaryExpression(context);
        failIfFalse(current, ASCIILiteral("Cannot parse expression"));
        
        context.appendBinaryExpressionInfo(operandStackDepth, current, exprStart, lastTokenEndPosition(), lastTokenEndPosition(), initialAssignments != m_parserState.assignmentCount);
        int precedence = isBinaryOperator(m_token.m_type);
		if (!precedence)
			break;

        // 12.6 https://tc39.github.io/ecma262/#sec-exp-operator
        // ExponentiationExpresion is described as follows.
        //
        //     ExponentiationExpression[Yield]:
        //         UnaryExpression[?Yield]
        //         UpdateExpression[?Yield] ** ExponentiationExpression[?Yield]
        //
        // As we can see, the left hand side of the ExponentiationExpression is UpdateExpression, not UnaryExpression.
        // So placing UnaryExpression not included in UpdateExpression here is a syntax error.
        // This is intentional. For example, if UnaryExpression is allowed, we can have the code like `-x**y`.
        // But this is confusing: `-(x**y)` OR `(-x)**y`, which interpretation is correct?
        // To avoid this problem, ECMA262 makes unparenthesized exponentiation expression as operand of unary operators an early error.
        // More rationale: https://mail.mozilla.org/pipermail/es-discuss/2015-September/044232.html
        //
        // Here, we guarantee that the left hand side of this expression is not unary expression by checking the leading operator of the parseUnaryExpression.
        // This check just works. Let's consider the example,
        //     y <> -x ** z
        //          ^
        //          Check this.
        // If the binary operator <> has higher precedence than one of "**", this check does not work.
        // But it's OK for ** because the operator "**" has the highest operator precedence in the binary operators.
        failIfTrue(match(POW) && isUnaryOpExcludingUpdateOp(leadingTokenTypeForUnaryExpression), ASCIILiteral("Ambiguous unary expression in the left hand side of the exponentiation expression; parenthesis must be used to disambiguate the expression"));

        // Mixing ?? with || or && is currently specified as an early error.
        // Since ?? is the lowest-precedence binary operator, it suffices to check whether these ever coexist in the operator stack.
        if (match(AND) || match(OR))
            hasLogicalOperator = true;
        else if (match(COALESCE))
            hasCoalesceOperator = true;
        failIfTrue(hasLogicalOperator && hasCoalesceOperator, ASCIILiteral("Coalescing and logical operators used together in the same expression; parentheses must be used to disambiguate"));

        m_parserState.nonTrivialExpressionCount++;
        m_parserState.nonLHSCount++;
        int operatorToken = m_token.m_type;
        next(TreeBuilder::DontBuildStrings());

        while (operatorStackDepth && context.operatorStackShouldReduce(precedence)) {
            ASSERT(operandStackDepth > 1);
            
            typename TreeBuilder::BinaryOperand rhs = context.getFromOperandStack(-1);
            typename TreeBuilder::BinaryOperand lhs = context.getFromOperandStack(-2);
            context.shrinkOperandStackBy(operandStackDepth, 2);
            bool success = context.appendBinaryOperation(location, operandStackDepth, operatorStackDepth, lhs, rhs);
            failIfFalse(success, ASCIILiteral("Cannot append binary operation"));
            context.operatorStackPop(operatorStackDepth);
        }
        context.operatorStackAppend(operatorStackDepth, operatorToken, precedence);
        previousOperator = operatorToken;
    }
    while (operatorStackDepth) {
        ASSERT(operandStackDepth > 1);
        
        typename TreeBuilder::BinaryOperand rhs = context.getFromOperandStack(-1);
        typename TreeBuilder::BinaryOperand lhs = context.getFromOperandStack(-2);
        context.shrinkOperandStackBy(operandStackDepth, 2);
        bool success = context.appendBinaryOperation(location, operandStackDepth, operatorStackDepth, lhs, rhs);
        failIfFalse(success, ASCIILiteral("Cannot append binary operation"));
        context.operatorStackPop(operatorStackDepth);
    }
    return context.popOperandStack(operandStackDepth);
}

template <typename LexerType>
template <class TreeBuilder> TreeProperty Parser<LexerType>::parseProperty(TreeBuilder& context)
{
    propagateError();
    SourceParseMode parseMode = SourceParseMode::MethodMode;
    bool wasIdent = false;

    if (consume(TIMES))
        parseMode = SourceParseMode::GeneratorWrapperMethodMode;

parseProperty:
    switch (m_token.m_type) {
    case ESCAPED_KEYWORD:
    case IDENT:
		if (UNLIKELY(*m_token.m_data.ident == m_vm->propertyNames->async && !m_token.m_data.escaped)) {
	        if (parseMode == SourceParseMode::MethodMode) {
	            SavePoint savePoint = createSavePoint(context);
	            next();

	            if (match(COLON) || match(OPENPAREN) || match(COMMA) || match(CLOSEBRACE)) {
	                restoreSavePoint(context, savePoint);
	                wasIdent = true;
	                goto namedProperty;
	            }

	            failIfTrue(m_lexer.hasLineTerminatorBeforeToken(), ASCIILiteral("Expected a property name following keyword 'async'"));
	            if (Options::useAsyncIterator() && UNLIKELY(consume(TIMES)))
	                parseMode = SourceParseMode::AsyncGeneratorWrapperMethodMode;
	            else
	                parseMode = SourceParseMode::AsyncMethodMode;
	            goto parseProperty;
	        }
        }
        FALLTHROUGH;
    case YIELD:
    case AWAIT:
        wasIdent = true;
        FALLTHROUGH;
    case STRING: {
namedProperty:
        const Identifier* ident = m_token.m_data.ident;
        bool wasUnescapedIdent = wasIdent && !m_token.m_data.escaped;
        unsigned getterOrSetterStartOffset = tokenStart();
		JSToken identToken = m_token;

        if (wasUnescapedIdent && !isGeneratorMethodParseMode(parseMode) && (*ident == m_vm->propertyNames->get || *ident == m_vm->propertyNames->set))
        	nextExpectIdentifier(LexerFlags::IgnoreReservedWords);
		else
			nextExpectIdentifier(TreeBuilder::DontBuildKeywords() | LexerFlags::IgnoreReservedWords);

        if (!isGeneratorMethodParseMode(parseMode) && !isAsyncMethodParseMode(parseMode) && match(COLON)) {
            next();
            TreeExpression node = parseAssignmentExpressionOrPropagateErrorClass(context);
            failIfFalse(node, ASCIILiteral("Cannot parse expression for property declaration"));
            context.setEndOffset(node, m_lexer.currentOffset());
            InferName inferName = ident && *ident == m_vm->propertyNames->underscoreProto ? InferName::Disallowed : InferName::Allowed;
			return context.createProperty(ident, node, PropertyNode::Constant, SuperBinding::NotNeeded, inferName, ClassElementTag::No);
        }

        if (match(OPENPAREN)) {
            SetForScope<SourceParseMode> innerParseMode(m_parseMode, parseMode);
        	TreeExpression method = parsePropertyMethod(context, ident);
            propagateError();
            return context.createProperty(ident, method, PropertyNode::Constant, SuperBinding::Needed, InferName::Allowed, ClassElementTag::No);
        }
        failIfTrue(parseMode != SourceParseMode::MethodMode, ASCIILiteral("Expected a parenthesis for argument list"));

        failIfFalse(wasIdent, ASCIILiteral("Expected an identifier as property name"));

        if (match(COMMA) || match(CLOSEBRACE)) {
            semanticFailureDueToKeywordCheckingToken(identToken, ASCIILiteral("shorthand property name"));
        	JSTextPosition start = tokenStartPosition();
            JSTokenLocation location(tokenLocation());
            currentScope()->useVariable(ident, m_vm->propertyNames->eval == *ident);
            if (currentScope()->isArrowFunction())
                currentScope()->setInnerArrowFunctionUsesEval();
            TreeExpression node = context.createResolve(location, *ident, start, lastTokenEndPosition());
            return context.createProperty(ident, node, static_cast<PropertyNode::Type>(PropertyNode::Constant | PropertyNode::Shorthand), SuperBinding::NotNeeded, InferName::Allowed, ClassElementTag::No);
        }

        if (match(EQUAL)) // CoverInitializedName is exclusive to BindingPattern and AssignmentPattern
            classifyExpressionError(ErrorIndicatesPattern);

        Optional<PropertyNode::Type> type;
        if (wasUnescapedIdent) {
			if (*ident == m_vm->propertyNames->get)
				type = PropertyNode::Getter;
			else if (*ident == m_vm->propertyNames->set)
				type = PropertyNode::Setter;
        }
        if (!type)
        	failWithMessage(ASCIILiteral("Expected a ':' following the property name '"), ident, ASCIILiteral("'"));
        return parseGetterSetter(context, type.value(), getterOrSetterStartOffset, ConstructorKind::None, ClassElementTag::No);
	}
    case DOUBLE:
    case INTEGER: {
    	const Identifier& ident = m_parserArena.identifierArena().makeNumericIdentifier(const_cast<VM*>(m_vm), m_token.m_data.doubleValue);
		next();

        if (match(OPENPAREN)) {
            SetForScope<SourceParseMode> innerParseMode(m_parseMode, parseMode);
            TreeExpression method = parsePropertyMethod(context, &ident);
            propagateError();
            return context.createProperty(&ident, method, PropertyNode::Constant, SuperBinding::Needed, InferName::Allowed, ClassElementTag::No);
        }
        failIfTrue(parseMode != SourceParseMode::MethodMode, ASCIILiteral("Expected a parenthesis for argument list"));

        consumeOrFail(COLON, ASCIILiteral("Expected ':' after property name"));
        TreeExpression node = parseAssignmentExpression(context);
        failIfFalse(node, ASCIILiteral("Cannot parse expression for property declaration"));
        context.setEndOffset(node, m_lexer.currentOffset());
        return context.createProperty(&ident, node, PropertyNode::Constant, SuperBinding::NotNeeded, InferName::Allowed, ClassElementTag::No);
    }
    case BIGINT: {
        const Identifier* ident = &m_parserArena.identifierArena().makeBigIntDecimalIdentifier(const_cast<VM&>(*m_vm), *m_token.m_data.bigIntString, m_token.m_data.radix);
        next();

        if (match(OPENPAREN)) {
            SetForScope<SourceParseMode> innerParseMode(m_parseMode, parseMode);
            auto method = parsePropertyMethod(context, ident);
            propagateError();
            return context.createProperty(ident, method, PropertyNode::Constant, SuperBinding::Needed, InferName::Allowed, ClassElementTag::No);
        }
        failIfTrue(parseMode != SourceParseMode::MethodMode, "Expected a parenthesis for argument list");

        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(ident, node, PropertyNode::Constant, SuperBinding::NotNeeded, InferName::Allowed, ClassElementTag::No);
    }
    case OPENBRACKET: {
        next();
        TreeExpression propertyName = parseAssignmentExpression(context);
        failIfFalse(propertyName, ASCIILiteral("Cannot parse computed property name"));
        handleProductionOrFail(CLOSEBRACKET, ASCIILiteral("]"), ASCIILiteral("end"), ASCIILiteral("computed property name"));

        if (match(OPENPAREN)) {
            SetForScope<SourceParseMode> innerParseMode(m_parseMode, parseMode);
        	TreeExpression method = parsePropertyMethod(context, &m_vm->propertyNames->nullIdentifier);
            propagateError();
            return context.createProperty(propertyName, method, static_cast<PropertyNode::Type>(PropertyNode::Constant | PropertyNode::Computed), SuperBinding::Needed, ClassElementTag::No);
        }
        failIfTrue(parseMode != SourceParseMode::MethodMode, ASCIILiteral("Expected a parenthesis for argument list"));

        consumeOrFail(COLON, ASCIILiteral("Expected ':' after property name"));
        TreeExpression node = parseAssignmentExpression(context);
        failIfFalse(node, ASCIILiteral("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), SuperBinding::NotNeeded, ClassElementTag::No);
    }
    case DOTDOTDOT: {
        if (m_useObjectRestSpread) {
            auto spreadLocation = m_token.m_location;
            auto start = m_token.m_startPosition;
            auto divot = m_token.m_endPosition;
            next();
            TreeExpression elem = parseAssignmentExpressionOrPropagateErrorClass(context);
            failIfFalse(elem, ASCIILiteral("Cannot parse subject of a spread operation"));
            auto node = context.createObjectSpreadExpression(spreadLocation, elem, start, divot, m_lastTokenEndPosition);
            return context.createProperty(node, PropertyNode::Spread, SuperBinding::NotNeeded, ClassElementTag::No);
        }
        FALLTHROUGH;
    }
    default:
        failIfFalse(m_token.m_type & KeywordTokenFlag, ASCIILiteral("Expected a property name"));
        wasIdent = true; // Treat keyword token as an identifier
        goto namedProperty;
    }
}

template <typename LexerType>
template <class TreeBuilder> TreeExpression Parser<LexerType>::parsePropertyMethod(TreeBuilder& context, const Identifier* methodName)
{
    ASSERT(isMethodParseMode(sourceParseMode()));
    JSTokenLocation location(tokenLocation());
    unsigned methodStart = tokenStart();
    ParserFunctionInfo<TreeBuilder> methodInfo;
    methodInfo.name = methodName;
    failIfFalse((parseFunctionInfo(context, FunctionNameRequirements::Unnamed, false, ConstructorKind::None, SuperBinding::Needed, methodStart, methodInfo, FunctionDefinitionType::Method)), ASCIILiteral("Cannot parse this method"));
    return context.createMethodDefinition(location, methodInfo);
}

template <typename LexerType>
template <class TreeBuilder> TreeProperty Parser<LexerType>::parseGetterSetter(TreeBuilder& context, PropertyNode::Type type, unsigned getterOrSetterStartOffset,
	ConstructorKind constructorKind, ClassElementTag tag)
{
    const Identifier* stringPropertyName = 0;
    double numericPropertyName = 0;
    TreeExpression computedPropertyName = 0;

    JSTokenLocation location(tokenLocation());

    bool matchesPrivateName = match(PRIVATENAME);
	if (matchSpecIdentifier() || match(STRING) || (Options::usePrivateMethods() && matchesPrivateName) || m_token.m_type & KeywordTokenFlag) {
		stringPropertyName = m_token.m_data.ident;
        semanticFailIfTrue(tag == ClassElementTag::Static && *stringPropertyName == m_vm->propertyNames->prototype,
			ASCIILiteral("Cannot declare a static method named 'prototype'"));
        semanticFailIfTrue(tag == ClassElementTag::Instance && *stringPropertyName == m_vm->propertyNames->constructor,
			ASCIILiteral("Cannot declare a getter or setter named 'constructor'"));
        semanticFailIfTrue(*stringPropertyName == m_vm->propertyNames->constructorPrivateField, ASCIILiteral("Cannot declare a private accessor named '#constructor'"));

        if (match(PRIVATENAME))
            semanticFailIfTrue(tag == ClassElementTag::No, ASCIILiteral("Cannot declare a private setter or getter outside a class"));
        next();
    } else if (match(DOUBLE) || match(INTEGER)) {
        numericPropertyName = m_token.m_data.doubleValue;
        next();
    } else if (match(BIGINT)) {
        stringPropertyName = &m_parserArena.identifierArena().makeBigIntDecimalIdentifier(const_cast<VM&>(*m_vm), *m_token.m_data.bigIntString, m_token.m_data.radix);
        next();
    } else if (match(OPENBRACKET)) {
        next();
        computedPropertyName = parseAssignmentExpression(context);
        failIfFalse(computedPropertyName, ASCIILiteral("Cannot parse computed property name"));
        handleProductionOrFail(CLOSEBRACKET, ASCIILiteral("]"), ASCIILiteral("end"), ASCIILiteral("computed property name"));
    } else
        failDueToUnexpectedToken();

    ParserFunctionInfo<TreeBuilder> info;
    if (type & PropertyNode::Getter) {
        failIfFalse(match(OPENPAREN), ASCIILiteral("Expected a parameter list for getter definition"));
        SetForScope<SourceParseMode> innerParseMode(m_parseMode, SourceParseMode::GetterMode);
        failIfFalse((parseFunctionInfo(context, FunctionNameRequirements::Unnamed, false, constructorKind, SuperBinding::Needed, getterOrSetterStartOffset, info, FunctionDefinitionType::Method)), ASCIILiteral("Cannot parse getter definition"));
    } else if (type & PropertyNode::Setter) {
        failIfFalse(match(OPENPAREN), ASCIILiteral("Expected a parameter list for setter definition"));
        SetForScope<SourceParseMode> innerParseMode(m_parseMode, SourceParseMode::SetterMode);
        failIfFalse((parseFunctionInfo(context, FunctionNameRequirements::Unnamed, false, constructorKind, SuperBinding::Needed, getterOrSetterStartOffset, info, FunctionDefinitionType::Method)), ASCIILiteral("Cannot parse setter definition"));
    } else if (type & PropertyNode::PrivateSetter) {
		failIfFalse(match(OPENPAREN), ASCIILiteral("Expected a parameter list for private setter definition"));
		SetForScope<SourceParseMode> innerParseMode(m_parseMode, SourceParseMode::SetterMode);
		failIfFalse((parseFunctionInfo(context, FunctionNameRequirements::Unnamed, false, constructorKind, SuperBinding::Needed, getterOrSetterStartOffset, info, FunctionDefinitionType::Method)), ASCIILiteral("Cannot parse private setter definition"));
	} else if (type & PropertyNode::PrivateGetter) {
		failIfFalse(match(OPENPAREN), ASCIILiteral("Expected a parameter list for private getter definition"));
		SetForScope<SourceParseMode> innerParseMode(m_parseMode, SourceParseMode::GetterMode);
		failIfFalse((parseFunctionInfo(context, FunctionNameRequirements::Unnamed, false, constructorKind, SuperBinding::Needed, getterOrSetterStartOffset, info, FunctionDefinitionType::Method)), ASCIILiteral("Cannot parse private getter definition"));
	}

    if (stringPropertyName)
        return context.createGetterOrSetterProperty(location, type, stringPropertyName, info, tag);

    if (computedPropertyName)
        return context.createGetterOrSetterProperty(location, static_cast<PropertyNode::Type>(type | PropertyNode::Computed), computedPropertyName, info, tag);

    return context.createGetterOrSetterProperty(const_cast<VM*>(m_vm), m_parserArena, location, type, numericPropertyName, info, tag);
}

template <typename LexerType>
void Parser<LexerType>::recordPauseLocation(const JSTextPosition& position)
{
#if 0
    if (LIKELY(!m_debuggerParseData))
        return;

    if (position.line < 0)
        return;

    m_debuggerParseData->pausePositions.appendPause(position);
#endif
}

template <typename LexerType>
void Parser<LexerType>::recordFunctionEntryLocation(const JSTextPosition& position)
{
#if 0
    if (LIKELY(!m_debuggerParseData))
        return;

    m_debuggerParseData->pausePositions.appendEntry(position);
#endif
}

template <typename LexerType>
void Parser<LexerType>::recordFunctionLeaveLocation(const JSTextPosition& position)
{
#if 0
    if (LIKELY(!m_debuggerParseData))
        return;

    m_debuggerParseData->pausePositions.appendLeave(position);
#endif
}

template <typename LexerType>
template <class TreeBuilder> TreeExpression Parser<LexerType>::parseObjectLiteral(TreeBuilder& context)
{
    JSTokenLocation location(tokenLocation());
    consumeOrFail(OPENBRACE, ASCIILiteral("Expected opening '{' at the start of an object literal"));

    SetForScope<int> nonLHSCountScope(m_parserState.nonLHSCount);
    if (match(CLOSEBRACE)) {
        next();
        return context.createObjectLiteral(location);
    }
    
    TreeProperty property = parseProperty(context);
    failIfFalse(property, ASCIILiteral("Cannot parse object literal property"));

    bool seenProtoSetter = context.isUnderscoreProtoSetter(property);

    TreePropertyList propertyList = context.createPropertyList(location, property);
    failIfFalse(propertyList, ASCIILiteral("Out of memory"));
    TreePropertyList tail = propertyList;
    while (match(COMMA)) {
        next();
        if (match(CLOSEBRACE))
            break;
        JSTokenLocation propertyLocation(tokenLocation());
        property = parseProperty(context);
        failIfFalse(property, ASCIILiteral("Cannot parse object literal property"));
        if (context.isUnderscoreProtoSetter(property)) {
            // https://tc39.es/ecma262/#sec-__proto__-property-names-in-object-initializers
            semanticFailIfTrue(seenProtoSetter, "Attempted to redefine __proto__ property");
            seenProtoSetter = true;
        }
        tail = context.createPropertyList(propertyLocation, property, tail);
        failIfFalse(tail, ASCIILiteral("Out of memory"));
    }

    handleProductionOrFail2(CLOSEBRACE, ASCIILiteral("}"), ASCIILiteral("end"), ASCIILiteral("object literal"));

    return context.createObjectLiteral(location, propertyList);
}

template <typename LexerType>
template <class TreeBuilder> TreeExpression Parser<LexerType>::parseArrayLiteral(TreeBuilder& context)
{
    JSTokenLocation location(tokenLocation());
    consumeOrFailWithFlags(OPENBRACKET, TreeBuilder::DontBuildStrings(), ASCIILiteral("Expected an opening '[' at the beginning of an array literal"));

    SetForScope<int> nonLHSCountScope(m_parserState.nonLHSCount);
    
    int elisions = 0;
    while (match(COMMA)) {
        next(TreeBuilder::DontBuildStrings());
        elisions++;
    }
    if (match(CLOSEBRACKET)) {
        next(TreeBuilder::DontBuildStrings());
        return context.createArray(location, elisions);
    }

    TreeExpression elem;
    if (UNLIKELY(match(DOTDOTDOT))) {
    	JSTokenLocation spreadLocation = m_token.m_location;
    	JSTextPosition start = m_token.m_startPosition;
    	JSTextPosition divot = m_token.m_endPosition;
        next();
        TreeExpression spreadExpr = parseAssignmentExpressionOrPropagateErrorClass(context);
        failIfFalse(spreadExpr, ASCIILiteral("Cannot parse subject of a spread operation"));
        elem = context.createSpreadExpression(spreadLocation, spreadExpr, start, divot, m_lastTokenEndPosition);
    } else
        elem = parseAssignmentExpressionOrPropagateErrorClass(context);
    failIfFalse(elem, ASCIILiteral("Cannot parse array literal element"));
    typename TreeBuilder::ElementList elementList = context.createElementList(elisions, elem);
    failIfFalse(elementList, ASCIILiteral("Out of memory"));
    typename TreeBuilder::ElementList tail = elementList;
    elisions = 0;
    while (match(COMMA)) {
        next(TreeBuilder::DontBuildStrings());
        elisions = 0;
        
        while (match(COMMA)) {
            next();
            elisions++;
        }
        
        if (match(CLOSEBRACKET)) {
            next(TreeBuilder::DontBuildStrings());
            return context.createArray(location, elisions, elementList);
        }
        if (UNLIKELY(match(DOTDOTDOT))) {
        	JSTokenLocation spreadLocation = m_token.m_location;
        	JSTextPosition start = m_token.m_startPosition;
        	JSTextPosition divot = m_token.m_endPosition;
            next();
            TreeExpression elem = parseAssignmentExpressionOrPropagateErrorClass(context);
            failIfFalse(elem, ASCIILiteral("Cannot parse subject of a spread operation"));
            TreeExpression spread = context.createSpreadExpression(spreadLocation, elem, start, divot, m_lastTokenEndPosition);
            tail = context.createElementList(tail, elisions, spread);
            continue;
        }
        TreeExpression elem = parseAssignmentExpressionOrPropagateErrorClass(context);
        failIfFalse(elem, ASCIILiteral("Cannot parse array literal element"));
        tail = context.createElementList(tail, elisions, elem);
        failIfFalse(tail, ASCIILiteral("Out of memory"));

        failIfTrue(lowMemory(), ASCIILiteral("Out of memory"));
    }

    if (!consume(CLOSEBRACKET)) {
		failIfFalse(match(DOTDOTDOT), ASCIILiteral("Expected either a closing ']' or a ',' following an array element"));
		semanticFail(ASCIILiteral("The '...' operator should come before a target expression"));
	}

    return context.createArray(location, elementList);
}

template <typename LexerType>
template <class TreeBuilder> TreeClassExpression Parser<LexerType>::parseClassExpression(TreeBuilder& context)
{
    ASSERT(match(CLASSTOKEN));
    SetForScope<int> nonLHSCountScope(m_parserState.nonLHSCount);
    ParserClassInfo<TreeBuilder> info;
    info.className = &m_vm->propertyNames->nullIdentifier;
    return parseClass(context, FunctionNameRequirements::None, info);
}

template <typename LexerType>
template <class TreeBuilder> TreeExpression Parser<LexerType>::parseFunctionExpression(TreeBuilder& context)
{
    ASSERT(match(FUNCTION));
    SetForScope<int> nonLHSCountScope(m_parserState.nonLHSCount);
    JSTokenLocation location(tokenLocation());
    unsigned functionKeywordStart = tokenStart();
    next();
    ParserFunctionInfo<TreeBuilder> functionInfo;
    functionInfo.name = &m_vm->propertyNames->nullIdentifier;
    SourceParseMode parseMode = SourceParseMode::NormalFunctionMode;
    if (consume(TIMES))
        parseMode = SourceParseMode::GeneratorWrapperFunctionMode;
    SetForScope<SourceParseMode> setInnerParseMode(m_parseMode, parseMode);

    ConstructorKind constructorKind = currentScope()->isGlobalCode() ? m_constructorKindForTopLevelFunctionExpressions : ConstructorKind::None;
    SuperBinding expectedSuperBinding = constructorKind == ConstructorKind::Extends ? SuperBinding::Needed : SuperBinding::NotNeeded;

    failIfFalse((parseFunctionInfo(context, FunctionNameRequirements::None, false, constructorKind, expectedSuperBinding, functionKeywordStart, functionInfo, FunctionDefinitionType::Expression)), ASCIILiteral("Cannot parse function expression"));
    return context.createFunctionExpr(location, functionInfo);
}

template <typename LexerType>
template <class TreeBuilder> TreeExpression Parser<LexerType>::parseAsyncFunctionExpression(TreeBuilder& context, const JSTokenLocation& location)
{
    ASSERT(match(FUNCTION));
    next();
    SourceParseMode parseMode = SourceParseMode::AsyncFunctionMode;

    if (Options::useAsyncIterator() && consume(TIMES))
        parseMode = SourceParseMode::AsyncGeneratorWrapperFunctionMode;
    SetForScope<SourceParseMode> setInnerParseMode(m_parseMode, parseMode);

    ParserFunctionInfo<TreeBuilder> functionInfo;
    functionInfo.name = &m_vm->propertyNames->nullIdentifier;
    failIfFalse(parseFunctionInfo(context, FunctionNameRequirements::None, false, ConstructorKind::None, SuperBinding::NotNeeded, location.startOffset, functionInfo, FunctionDefinitionType::Expression), parseMode == SourceParseMode::AsyncFunctionMode ? "Cannot parse async function expression" : "Cannot parse async generator function expression");
    return context.createFunctionExpr(location, functionInfo);
}

template <typename LexerType>
template <class TreeBuilder> typename TreeBuilder::TemplateString Parser<LexerType>::parseTemplateString(TreeBuilder& context, bool isTemplateHead, typename LexerType::RawStringsBuildMode rawStringsBuildMode, bool& elementIsTail)
{
    if (isTemplateHead)
        ASSERT(match(BACKQUOTE));
    else
    	matchOrFail(CLOSEBRACE, ASCIILiteral("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.scanTemplateString(&m_token, rawStringsBuildMode);
    matchOrFail(TEMPLATE, ASCIILiteral("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)
{
    ASSERT(match(BACKQUOTE));
    SetForScope<int> nonLHSCountScope(m_parserState.nonLHSCount);
    JSTokenLocation location(tokenLocation());
    bool elementIsTail = false;

    typename TreeBuilder::TemplateString headTemplateString = parseTemplateString(context, true, rawStringsBuildMode, elementIsTail);
    failIfFalse(headTemplateString, ASCIILiteral("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), ASCIILiteral("Template literal expression cannot be empty"));
    TreeExpression expression = parseExpression(context);
    failIfFalse(expression, ASCIILiteral("Cannot parse expression in template literal"));

    typename TreeBuilder::TemplateExpressionList templateExpressionList = context.createTemplateExpressionList(expression);
    typename TreeBuilder::TemplateExpressionList templateExpressionTail = templateExpressionList;

    typename TreeBuilder::TemplateString templateString = parseTemplateString(context, false, rawStringsBuildMode, elementIsTail);
    failIfFalse(templateString, ASCIILiteral("Cannot parse template element"));
    templateStringTail = context.createTemplateStringList(templateStringTail, templateString);

    while (!elementIsTail) {
    	failIfTrue(match(CLOSEBRACE), ASCIILiteral("Template literal expression cannot be empty"));
        TreeExpression expression = parseExpression(context);
        failIfFalse(expression, ASCIILiteral("Cannot parse expression in template literal"));

        templateExpressionTail = context.createTemplateExpressionList(templateExpressionTail, expression);

        typename TreeBuilder::TemplateString templateString = parseTemplateString(context, false, rawStringsBuildMode, elementIsTail);
        failIfFalse(templateString, ASCIILiteral("Cannot parse template element"));
        templateStringTail = context.createTemplateStringList(templateStringTail, templateString);
    }

    return context.createTemplateLiteral(location, templateStringList, templateExpressionList);
}

template <class LexerType>
template <class TreeBuilder> TreeExpression Parser<LexerType>::createResolveAndUseVariable(TreeBuilder& context, const Identifier* ident, bool isEval, const JSTextPosition& start, const JSTokenLocation& location)
{
    currentScope()->useVariable(ident, isEval);
    m_parserState.lastIdentifier = ident;
    return context.createResolve(location, *ident, start, lastTokenEndPosition());
}

template <typename LexerType>
template <class TreeBuilder> TreeExpression Parser<LexerType>::parsePrimaryExpression(TreeBuilder& context)
{
    propagateError();
    failIfStackOverflow();
    switch (m_token.m_type) {
    case FUNCTION:
        return parseFunctionExpression(context);
    case CLASSTOKEN:
        return parseClassExpression(context);
    case OPENBRACE:
        return parseObjectLiteral(context);
    case OPENBRACKET:
        return parseArrayLiteral(context);
    case OPENPAREN: {
        next();
        SetForScope<int> nonLHSCountScope(m_parserState.nonLHSCount);
        TreeExpression result = parseExpression(context);
        handleProductionOrFail(CLOSEPAREN, ASCIILiteral(")"), ASCIILiteral("end"), ASCIILiteral("compound expression"));
        return result;
    }
    case THISTOKEN: {
        JSTokenLocation location(tokenLocation());
        next();
        if (currentScope()->isArrowFunction())
            currentScope()->setInnerArrowFunctionUsesThis();
        return context.createThisExpr(location);
    }
    case AWAIT:
    	semanticFailIfTrue(currentScope()->isStaticBlock(), ASCIILiteral("The 'await' keyword is disallowed in the IdentifierReference position within static block"));
		if (m_parserState.functionParsePhase == FunctionParsePhase::Parameters)
        	semanticFailIfFalse(m_parserState.allowAwait, ASCIILiteral("Cannot use 'await' within a parameter default expression"));
		else if (currentFunctionScope()->isAsyncFunctionBoundary() || (isModuleParseMode(sourceParseMode()) && Options::useTopLevelAwait()))
			return parseAwaitExpression(context);
        goto identifierExpression;
    case IDENT: {
    	semanticFailIfTrue(currentScope()->isStaticBlock() && isArgumentsIdentifier(), ASCIILiteral("Cannot use 'arguments' as an identifier in static block"));
		if (UNLIKELY(*m_token.m_data.ident == m_vm->propertyNames->async && !m_token.m_data.escaped)) {
			JSTextPosition functionStart = tokenStartPosition();
			const Identifier* ident = m_token.m_data.ident;
			JSTokenLocation location(tokenLocation());
			next();
			if (match(FUNCTION) && !m_lexer.hasLineTerminatorBeforeToken())
				return parseAsyncFunctionExpression(context, location);

			// Avoid using variable if it is an arrow function parameter
			if (UNLIKELY(match(ARROWFUNCTION)))
				return 0;

			const bool isEval = false;
			return createResolveAndUseVariable(context, ident, isEval, functionStart, location);
		}
        if (UNLIKELY(m_parserState.isParsingClassFieldInitializer))
            failIfTrue(isArgumentsIdentifier(), ASCIILiteral("Cannot reference 'arguments' in class field initializer"));
	identifierExpression:
        JSTextPosition start = tokenStartPosition();
        const Identifier* ident = m_token.m_data.ident;
        if (UNLIKELY(currentScope()->evalContextType() == EvalContextType::InstanceFieldEvalContext))
			failIfTrue(*ident == m_vm->propertyNames->arguments, ASCIILiteral("arguments is not valid in this context"));
		JSTokenLocation location(tokenLocation());
        next();

        // Avoid using variable if it is an arrow function parameter
        if (UNLIKELY(match(ARROWFUNCTION)))
            return 0;

        return createResolveAndUseVariable(context, ident, *ident == m_vm->propertyNames->eval, start, location);
    }
    case BIGINT: {
        const Identifier* ident = m_token.m_data.bigIntString;
        uint8_t radix = m_token.m_data.radix;
        JSTokenLocation location(tokenLocation());
        next();
        return context.createBigInt(location, ident, radix);
    }
    case STRING: {
        const Identifier* ident = m_token.m_data.ident;
        JSTokenLocation location(tokenLocation());
        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: {
        double d = m_token.m_data.doubleValue;
        JSTokenLocation location(tokenLocation());
        next();
        return context.createIntegerExpr(location, d);
    }
    case NULLTOKEN: {
        JSTokenLocation location(tokenLocation());
        next();
        return context.createNull(location);
    }
    case TRUETOKEN: {
        JSTokenLocation location(tokenLocation());
        next();
        return context.createBoolean(location, true);
    }
    case FALSETOKEN: {
        JSTokenLocation location(tokenLocation());
        next();
        return context.createBoolean(location, false);
    }
    case DIVEQUAL:
    case DIVIDE: {
        /* regexp */
        if (match(DIVEQUAL))
            m_token.m_type = m_lexer.scanRegExp(&m_token, '=');
        else
            m_token.m_type = m_lexer.scanRegExp(&m_token);
        matchOrFail(REGEXP, ASCIILiteral("Invalid regular expression"));

        const Identifier* pattern = m_token.m_data.pattern;
        const Identifier* flags = m_token.m_data.flags;
        JSTextPosition start = tokenStartPosition();
        JSTokenLocation location(tokenLocation());
        next();
        TreeExpression re = context.createRegExp(location, *pattern, *flags, start);
        if (!re) {
            Yarr::ErrorCode errorCode = Yarr::checkSyntax(pattern->string(), flags->string());
            regexFail(Yarr::errorMessage(errorCode));
        }
        return re;
    }
    case BACKQUOTE:
        return parseTemplateLiteral(context, LexerType::RawStringsBuildMode::DontBuildRawStrings);
    case YIELD:
        if (canUseIdentifierYield())
            goto identifierExpression;
        failDueToUnexpectedToken();
    case LET:
        if (!strictMode())
            goto identifierExpression;
        failDueToUnexpectedToken();
    case ESCAPED_KEYWORD:
        if (matchAllowedEscapedContextualKeyword())
            goto identifierExpression;
        FALLTHROUGH;
    default:
        failDueToUnexpectedToken();
    }
}

template <typename LexerType>
template <class TreeBuilder> TreeArguments Parser<LexerType>::parseArguments(TreeBuilder& context)
{
    consumeOrFailWithFlags(OPENPAREN, TreeBuilder::DontBuildStrings(), ASCIILiteral("Expected opening '(' at start of argument list"));
    JSTokenLocation location(tokenLocation());
    if (match(CLOSEPAREN)) {
        next();
        return context.createArguments();
    }
    auto argumentsStart = m_token.m_startPosition;
    auto argumentsDivot = m_token.m_endPosition;

    int initialAssignments = m_parserState.assignmentCount;
    ArgumentType argType = ArgumentType::Normal;
    TreeExpression firstArg = parseArgument(context, argType);
    failIfFalse(firstArg, ASCIILiteral("Cannot parse function argument"));
    semanticFailIfTrue(match(DOTDOTDOT), ASCIILiteral("The '...' operator should come before the target expression"));

	bool hasSpread = false;
	if (argType == ArgumentType::Spread)
		hasSpread = true;
    TreeArgumentsList argList = context.createArgumentsList(location, firstArg);
    failIfFalse(argList, ASCIILiteral("Out of memory"));

    TreeArgumentsList tail = argList;
    while (match(COMMA)) {
        JSTokenLocation argumentLocation(tokenLocation());
        next(TreeBuilder::DontBuildStrings());

        if (UNLIKELY(match(CLOSEPAREN)))
            break;

        TreeExpression arg = parseArgument(context, argType);
        propagateError();
        semanticFailIfTrue(match(DOTDOTDOT), ASCIILiteral("The '...' operator should come before the target expression"));

        if (argType == ArgumentType::Spread)
            hasSpread = true;

        tail = context.createArgumentsList(argumentLocation, tail, arg);
        failIfFalse(tail, ASCIILiteral("Out of memory"));
    }

    handleProductionOrFail2(CLOSEPAREN, ASCIILiteral(")"), ASCIILiteral("end"), ASCIILiteral("argument list"));
	if (hasSpread) {
		TreeExpression spreadArray = context.createSpreadExpression(location, context.createArray(location, context.createElementList(argList)), argumentsStart, argumentsDivot, m_lastTokenEndPosition);
		return context.createArguments(context.createArgumentsList(location, spreadArray), initialAssignments != m_parserState.assignmentCount);
	}

    return context.createArguments(argList, initialAssignments != m_parserState.assignmentCount);
}

template <typename LexerType>
template <class TreeBuilder> TreeExpression Parser<LexerType>::parseArgument(TreeBuilder& context, ArgumentType& type)
{
    if (UNLIKELY(match(DOTDOTDOT))) {
        JSTokenLocation spreadLocation(tokenLocation());
        auto start = m_token.m_startPosition;
        auto divot = m_token.m_endPosition;
        next();
        TreeExpression spreadExpr = parseAssignmentExpression(context);
        propagateError();
        auto end = m_lastTokenEndPosition;
        type = ArgumentType::Spread;
        return context.createSpreadExpression(spreadLocation, spreadExpr, start, divot, end);
    }

    type = ArgumentType::Normal;
    return parseAssignmentExpression(context);
}

template <typename ParserType>
static inline void recordCallOrApplyDepth(ParserType* parser, VM& vm, OwnPtr<typename ParserType::CallOrApplyDepthScope>& callOrApplyDepthScope, ExpressionNode* expression)
{
    if (expression->isDotAccessorNode()) {
        DotAccessorNode* dot = static_cast<DotAccessorNode*>(expression);
        bool isCallOrApply = dot->identifier() == vm.propertyNames->builtinNames().callPublicName() || dot->identifier() == vm.propertyNames->builtinNames().applyPublicName();
        if (isCallOrApply)
        	callOrApplyDepthScope = adoptPtr(new typename ParserType::CallOrApplyDepthScope(parser));
    }
}

template <typename ParserType>
static inline void recordCallOrApplyDepth(ParserType*, VM&, OwnPtr<typename ParserType::CallOrApplyDepthScope>&, SyntaxChecker::Expression)
{
}

template <typename LexerType>
template <class TreeBuilder> TreeExpression Parser<LexerType>::parseMemberExpression(TreeBuilder& context)
{
    TreeExpression base = 0;
    JSTextPosition expressionStart = tokenStartPosition();
    JSTokenLocation location = tokenLocation();
    Vector<JSTextPosition> newTokenStartPositions;
    propagateError();
    while (match(NEW)) {
        newTokenStartPositions.append(tokenStartPosition());
        next();
    }
    size_t newCount = newTokenStartPositions.size();

    bool baseIsSuper = match(SUPER);
    bool previousBaseWasSuper = false;
    bool baseIsImport = match(IMPORT);
    semanticFailIfTrue((baseIsSuper || baseIsImport) && newCount, ASCIILiteral("Cannot use new with "), getToken());

    bool baseIsNewTarget = false;
    if (newCount && match(DOT)) {
        next();
        if (matchContextualKeyword(m_vm->propertyNames->target)) {
			ScopeRef closestOrdinaryFunctionScope = closestParentOrdinaryFunctionNonLexicalScope();
			ScopeRef classScope = closestClassScopeOrTopLevelScope();
			bool isClassFieldInitializer = classScope.index() > closestOrdinaryFunctionScope.index();
			bool isFunctionEvalContextType = closestOrdinaryFunctionScope->evalContextType() == EvalContextType::FunctionEvalContext || closestOrdinaryFunctionScope->evalContextType() == EvalContextType::InstanceFieldEvalContext;
			semanticFailIfFalse(currentScope()->isFunction() || currentScope()->isStaticBlock() || isFunctionEvalContextType || isClassFieldInitializer, ASCIILiteral("new.target is only valid inside functions"));
			baseIsNewTarget = true;
			if (currentScope()->isArrowFunction()) {
				semanticFailIfFalse(!closestOrdinaryFunctionScope->isGlobalCode() || isFunctionEvalContextType || isClassFieldInitializer, ASCIILiteral("new.target is not valid inside arrow functions in global code"));
				currentScope()->setInnerArrowFunctionUsesNewTarget();
			}
            base = context.createNewTargetExpr(location);
			newCount--;
			next();
        } else {
            failIfTrue(match(IDENT), ASCIILiteral("\"new.\" can only followed with target"));
            failDueToUnexpectedToken();
        }
    }

    bool baseIsAsyncKeyword = false;
    if (baseIsSuper) {
    	ScopeRef closestOrdinaryFunctionScope = closestParentOrdinaryFunctionNonLexicalScope();
    	ScopeRef classScope = closestClassScopeOrTopLevelScope();
		bool isClassFieldInitializer = classScope.index() > closestOrdinaryFunctionScope.index();
		semanticFailIfFalse(currentScope()->isFunction() || isClassFieldInitializer || (closestOrdinaryFunctionScope->isEvalContext() && closestOrdinaryFunctionScope->expectedSuperBinding() == SuperBinding::Needed), "super is not valid in this context");
		base = context.createSuperExpr(location);
        next();
        ScopeRef functionScope = currentFunctionScope();
        if (!functionScope->setNeedsSuperBinding()) {
            // It unnecessary to check of using super during reparsing one more time. Also it can lead to syntax error
            // in case of arrow function because during reparsing we don't know whether we currently parse the arrow function
            // inside of the constructor or method.
            if (!m_lexer.isReparsingFunction()) {
                SuperBinding functionSuperBinding = !functionScope->isArrowFunction() && !closestOrdinaryFunctionScope->isEvalContext()
                    ? functionScope->expectedSuperBinding()
                    : closestOrdinaryFunctionScope->expectedSuperBinding();
                semanticFailIfTrue(functionSuperBinding == SuperBinding::NotNeeded && !isClassFieldInitializer, ASCIILiteral("super is not valid in this context"));
            }
        }
    } else if (baseIsImport) {
        next();
        JSTextPosition expressionEnd = lastTokenEndPosition();
        if (consume(DOT)) {
            if (matchContextualKeyword(m_vm->propertyNames->builtinNames().metaPublicName())) {
                semanticFailIfFalse(m_scriptMode == JSParserScriptMode::Module, ASCIILiteral("import.meta is only valid inside modules"));
                base = context.createImportMetaExpr(location, createResolveAndUseVariable(context, &m_vm->propertyNames->metaPrivateName, false, expressionStart, location));
                next();
            } else {
                failIfTrue(match(IDENT), ASCIILiteral("\"import.\" can only followed with meta"));
                failDueToUnexpectedToken();
            }
        } else {
			consumeOrFail(OPENPAREN, ASCIILiteral("import call expects exactly one argument"));
			SetForScope<int> nonLHSCountScope(m_parserState.nonLHSCount);
			TreeExpression expr = parseAssignmentExpression(context);
			failIfFalse(expr, ASCIILiteral("Cannot parse expression"));
			consumeOrFail(CLOSEPAREN, ASCIILiteral("import call expects exactly one argument"));
			base = context.createImportExpr(location, expr, expressionStart, expressionEnd, lastTokenEndPosition());
        }
    } else if (!baseIsNewTarget) {
        const bool isAsync = matchContextualKeyword(m_vm->propertyNames->async);

    	base = parsePrimaryExpression(context);
        failIfFalse(base, ASCIILiteral("Cannot parse base expression"));
        if (UNLIKELY(isAsync && context.isResolve(base) && !m_lexer.hasLineTerminatorBeforeToken())) {
            if (matchSpecIdentifier()) {
                // AsyncArrowFunction
                forceClassifyExpressionError(ErrorIndicatesAsyncArrowFunction);
                failDueToUnexpectedToken();
            }
            baseIsAsyncKeyword = true;
        }
    }

    propagateError();
    failIfFalse(base, ASCIILiteral("Cannot parse base expression"));

    do {
        TreeExpression optionalChainBase = 0;
        JSTokenLocation optionalChainLocation;
        JSTokenType type = m_token.m_type;

        if (match(QUESTIONDOT)) {
            semanticFailIfTrue(newCount, ASCIILiteral("Cannot call constructor in an optional chain"));
            semanticFailIfTrue(baseIsSuper, ASCIILiteral("Cannot use super as the base of an optional chain"));
            optionalChainBase = base;
            optionalChainLocation = tokenLocation();

            SavePoint savePoint = createSavePoint(context);
            next();
            if (match(OPENBRACKET) || match(OPENPAREN) || match(BACKQUOTE))
                type = m_token.m_type;
            else {
                type = DOT;
                restoreSavePoint(context, savePoint);
            }
        }

		while (true) {
			switch (type) {
			case OPENBRACKET: {
				m_parserState.nonTrivialExpressionCount++;
                JSTextPosition expressionDivot = tokenStartPosition();
				next();
			    SetForScope<int> nonLHSCountScope(m_parserState.nonLHSCount);
				int initialAssignments = m_parserState.assignmentCount;
				TreeExpression property = parseExpression(context);
				failIfFalse(property, ASCIILiteral("Cannot parse subscript expression"));
				base = context.createBracketAccess(location, base, property, initialAssignments != m_parserState.assignmentCount, expressionStart, expressionDivot, tokenEndPosition());

				if (UNLIKELY(baseIsSuper && currentScope()->isArrowFunction()))
					currentFunctionScope()->setInnerArrowFunctionUsesSuperProperty();

				handleProductionOrFail(CLOSEBRACKET, ASCIILiteral("]"), ASCIILiteral("end"), ASCIILiteral("subscript expression"));
				break;
			}
			case OPENPAREN: {
				if (baseIsSuper)
					failIfTrue(m_parserState.isParsingClassFieldInitializer, "super call is not valid in class field initializer context");
				m_parserState.nonTrivialExpressionCount++;
			    SetForScope<int> nonLHSCountScope(m_parserState.nonLHSCount);
				if (newCount) {
					newCount--;
					JSTextPosition expressionEnd = lastTokenEndPosition();
					TreeArguments arguments = parseArguments(context);
					failIfFalse(arguments, ASCIILiteral("Cannot parse call arguments"));
					base = context.createNewExpr(location, base, arguments, expressionStart, expressionEnd, lastTokenEndPosition());
				} else {
					size_t usedVariablesSize = currentScope()->currentUsedVariablesSize();
					JSTextPosition expressionEnd = lastTokenEndPosition();

					OwnPtr<CallOrApplyDepthScope> callOrApplyDepthScope;

					recordCallOrApplyDepth(this, *m_vm, callOrApplyDepthScope, base);

					TreeArguments arguments = parseArguments(context);

					if (baseIsAsyncKeyword && (!arguments || match(ARROWFUNCTION))) {
					  currentScope()->revertToPreviousUsedVariables(usedVariablesSize);
					  forceClassifyExpressionError(ErrorIndicatesAsyncArrowFunction);
					  failDueToUnexpectedToken();
					}

					failIfFalse(arguments, ASCIILiteral("Cannot parse call arguments"));
					if (baseIsSuper) {
						ScopeRef functionScope = currentFunctionScope();
						if (!functionScope->setHasDirectSuper()) {
							// It unnecessary to check of using super during reparsing one more time. Also it can lead to syntax error
							// in case of arrow function because during reparsing we don't know whether we currently parse the arrow function
							// inside of the constructor or method.
							if (!m_lexer.isReparsingFunction()) {
								ScopeRef closestOrdinaryFunctionScope = closestParentOrdinaryFunctionNonLexicalScope();
								semanticFailIfFalse(closestOrdinaryFunctionScope->constructorKind() == ConstructorKind::Extends || (closestOrdinaryFunctionScope->isEvalContext() && closestOrdinaryFunctionScope->derivedContextType() == DerivedContextType::DerivedConstructorContext), ASCIILiteral("super is not valid in this context"));
							}
						}
						if (currentScope()->isArrowFunction())
							functionScope->setInnerArrowFunctionUsesSuperCall();
					}

					bool isOptionalCall = optionalChainLocation.endOffset == static_cast<unsigned>(expressionEnd.offset);
					base = context.makeFunctionCallNode(location, base, previousBaseWasSuper, arguments, expressionStart,
						expressionEnd, lastTokenEndPosition(), callOrApplyDepthScope ? callOrApplyDepthScope->distanceToInnermostChild() : 0, isOptionalCall);

                    if (isOptionalCall)
                        optionalChainBase = base;
				}
				break;
			}
			case DOT: {
				m_parserState.nonTrivialExpressionCount++;
                JSTextPosition expressionDivot = tokenStartPosition();
				nextExpectIdentifier(TreeBuilder::DontBuildKeywords() | LexerFlags::IgnoreReservedWords);
				const Identifier* ident = m_token.m_data.ident;
				auto type = DotType::Name;
				if (match(PRIVATENAME)) {
					ASSERT(ident);
					failIfTrue(baseIsSuper, "Cannot access private names from super");
					if (UNLIKELY(currentScope()->evalContextType() == EvalContextType::InstanceFieldEvalContext))
						semanticFailIfFalse(currentScope()->hasPrivateName(*ident), "Cannot reference undeclared private field '", ident->impl(), "'");
					currentScope()->usePrivateName(*ident);
					m_seenPrivateNameUseInNonReparsingFunctionMode = true;
					m_parserState.lastPrivateName = ident;
					type = DotType::PrivateMember;
					m_token.m_type = IDENT;
				}
				matchOrFail(IDENT, ASCIILiteral("Expected a property name after "), optionalChainBase ? ASCIILiteral("'?.'") : ASCIILiteral("'.'"));
				base = context.createDotAccess(location, base, ident, type, expressionStart, expressionDivot, tokenEndPosition());
				if (UNLIKELY(baseIsSuper && currentScope()->isArrowFunction()))
					currentFunctionScope()->setInnerArrowFunctionUsesSuperProperty();
				next();
				break;
			}
			case BACKQUOTE: {
				semanticFailIfTrue(optionalChainBase, ASCIILiteral("Cannot use tagged templates in an optional chain"));
				semanticFailIfTrue(baseIsSuper, ASCIILiteral("Cannot use super as tag for tagged templates"));
				JSTextPosition expressionDivot = tokenStartPosition();
			    SetForScope<int> nonLHSCountScope(m_parserState.nonLHSCount);
				typename TreeBuilder::TemplateLiteral templateLiteral = parseTemplateLiteral(context, LexerType::RawStringsBuildMode::BuildRawStrings);
				failIfFalse(templateLiteral, ASCIILiteral("Cannot parse template literal"));
				base = context.createTaggedTemplate(location, base, templateLiteral, expressionStart, expressionDivot, lastTokenEndPosition());
                m_seenTaggedTemplateInNonReparsingFunctionMode = true;
				break;
			}
			default:
                goto endOfChain;
			}
			previousBaseWasSuper = baseIsSuper;
			baseIsSuper = false;
            type = m_token.m_type;
        }
endOfChain:
        if (optionalChainBase)
            base = context.createOptionalChain(location, optionalChainBase, base, !match(QUESTIONDOT));
    } while (match(QUESTIONDOT));

	semanticFailIfTrue(baseIsSuper, ASCIILiteral("super is not valid in this context"));
    while (newCount--)
        base = context.createNewExpr(location, base, expressionStart, newTokenStartPositions[newCount], lastTokenEndPosition());
    return base;
}

template <typename LexerType>
template <class TreeBuilder> TreeExpression Parser<LexerType>::parseArrowFunctionExpression(TreeBuilder& context, bool isAsync, const JSTokenLocation& location)
{
    ParserFunctionInfo<TreeBuilder> info;
    info.name = &m_vm->propertyNames->nullIdentifier;

    SetForScope<SourceParseMode> innerParseMode(m_parseMode, isAsync ? SourceParseMode::AsyncArrowFunctionMode : SourceParseMode::ArrowFunctionMode);
	failIfFalse((parseFunctionInfo(context, FunctionNameRequirements::Unnamed, true, ConstructorKind::None, SuperBinding::NotNeeded, location.startOffset, info, FunctionDefinitionType::Expression)), ASCIILiteral("Cannot parse arrow function expression"));

    return context.createArrowFunctionExpr(location, info);
}

static NEVER_INLINE ASCIILiteral operatorString(bool prefix, unsigned tok)
{
    switch (tok) {
    case MINUSMINUS:
    case AUTOMINUSMINUS:
        return prefix ? ASCIILiteral("prefix-decrement") : ASCIILiteral("decrement");

    case PLUSPLUS:
    case AUTOPLUSPLUS:
        return prefix ? ASCIILiteral("prefix-increment") : ASCIILiteral("increment");

    case EXCLAMATION:
        return ASCIILiteral("logical-not");

    case TILDE:
        return ASCIILiteral("bitwise-not");

    case TYPEOF:
        return ASCIILiteral("typeof");

    case VOIDTOKEN:
        return ASCIILiteral("void");

    case DELETETOKEN:
        return ASCIILiteral("delete");
    }
    RELEASE_ASSERT_NOT_REACHED();
    return ASCIILiteral("error");
}

template <typename LexerType>
template <class TreeBuilder> TreeExpression Parser<LexerType>::parseUnaryExpression(TreeBuilder& context)
{
    typename TreeBuilder::UnaryExprContext unaryExprContext(context);
    AllowInOverride allowInOverride(this);
    int tokenStackDepth = 0;
    bool hasPrefixUpdateOp = false;
    unsigned lastOperator = 0;

    if (UNLIKELY(match(AWAIT) && (currentFunctionScope()->isAsyncFunctionBoundary() || (isModuleParseMode(m_parseMode) && Options::useTopLevelAwait())))) {
        semanticFailIfTrue(currentScope()->isStaticBlock(), ASCIILiteral("Cannot use 'await' within static block"));
        return parseAwaitExpression(context);
    }

    JSTokenLocation location(tokenLocation());

#if 0
    int oldTokenStackDepth = context.unaryTokenStackDepth();
    makeScopeExit([&] {
        ASSERT_UNUSED(oldTokenStackDepth, oldTokenStackDepth <= context.unaryTokenStackDepth());
    });
#endif

    while (isUnaryOp(m_token.m_type)) {
		semanticFailIfTrue(hasPrefixUpdateOp, ASCIILiteral("The "), operatorString(true, lastOperator), ASCIILiteral(" operator requires a reference expression"));
		if (isUpdateOp(m_token.m_type))
			hasPrefixUpdateOp = true;
        lastOperator = m_token.m_type;
        m_parserState.nonLHSCount++;
        context.appendUnaryToken(tokenStackDepth, m_token.m_type, tokenStartPosition());
        next();
        m_parserState.nonTrivialExpressionCount++;
    }
    JSTextPosition subExprStart = tokenStartPosition();
    ASSERT(subExprStart.offset >= subExprStart.lineStartOffset);
    TreeExpression expr = parseMemberExpression(context);
    if (!expr) {
		if (lastOperator)
			failWithMessage(ASCIILiteral("Cannot parse subexpression of "), operatorString(true, lastOperator), ASCIILiteral("operator"));
		failWithMessage(ASCIILiteral("Cannot parse member expression"));
	}
    //if constexpr (std::is_same_v<TreeBuilder, ASTBuilder>)
    //    ASSERT(oldTokenStackDepth + tokenStackDepth == context.unaryTokenStackDepth());
    if (isUpdateOp(static_cast<JSTokenType>(lastOperator))) {
        semanticFailIfTrue(context.isMetaProperty(expr), metaPropertyName(context, expr), ASCIILiteral(" can't come after a prefix operator"));
        semanticFailIfFalse(isSimpleAssignmentTarget(context, expr), ASCIILiteral("Prefix "), lastOperator == PLUSPLUS || lastOperator == AUTOPLUSPLUS ? ASCIILiteral("++") : ASCIILiteral("--"), ASCIILiteral(" operator applied to value that is not a reference"));
    }
	bool isEvalOrArguments = false;
    if (strictMode()) {
        if (context.isResolve(expr))
            isEvalOrArguments = *m_parserState.lastIdentifier == m_vm->propertyNames->eval || *m_parserState.lastIdentifier == m_vm->propertyNames->arguments;
    }
    failIfTrueIfStrict(isEvalOrArguments && hasPrefixUpdateOp, ASCIILiteral("Cannot modify '"), m_parserState.lastIdentifier, ASCIILiteral("' in strict mode"));
    switch (m_token.m_type) {
    case PLUSPLUS:
    	semanticFailIfTrue(context.isMetaProperty(expr), metaPropertyName(context, expr), ASCIILiteral(" can't come before a postfix operator"));
		semanticFailIfFalse(isSimpleAssignmentTarget(context, expr), ASCIILiteral("Postfix ++ operator applied to value that is not a reference"));
		m_parserState.nonTrivialExpressionCount++;
        m_parserState.nonLHSCount++;
        expr = context.makePostfixNode(location, expr, Operator::PlusPlus, subExprStart, lastTokenEndPosition(), tokenEndPosition());
        m_parserState.assignmentCount++;
        failIfTrueIfStrict(isEvalOrArguments, ASCIILiteral("Cannot modify '"), m_parserState.lastIdentifier, ASCIILiteral("' in strict mode"));
        semanticFailIfTrue(hasPrefixUpdateOp, ASCIILiteral("The "), operatorString(false, lastOperator), ASCIILiteral(" operator requires a reference expression"));
        next();
        break;
    case MINUSMINUS:
    	semanticFailIfTrue(context.isMetaProperty(expr), metaPropertyName(context, expr), ASCIILiteral(" can't come before a postfix operator"));
		semanticFailIfFalse(isSimpleAssignmentTarget(context, expr), ASCIILiteral("Postfix -- operator applied to value that is not a reference"));
		m_parserState.nonTrivialExpressionCount++;
        m_parserState.nonLHSCount++;
        expr = context.makePostfixNode(location, expr, Operator::MinusMinus, subExprStart, lastTokenEndPosition(), tokenEndPosition());
        m_parserState.assignmentCount++;
        failIfTrueIfStrict(isEvalOrArguments, ASCIILiteral("Cannot modify '"), m_parserState.lastIdentifier, ASCIILiteral("' in strict mode"));
        semanticFailIfTrue(hasPrefixUpdateOp, ASCIILiteral("The "), operatorString(false, lastOperator), ASCIILiteral(" operator requires a reference expression"));
		next();
        break;
    default:
        break;
    }

    JSTextPosition end = lastTokenEndPosition();
    while (tokenStackDepth) {
        subExprStart = context.unaryTokenStackLastStart(tokenStackDepth);
        auto tokenType = context.unaryTokenStackLastType(tokenStackDepth);
        switch (tokenType) {
        case EXCLAMATION:
            expr = context.createLogicalNot(location, expr);
            break;
        case TILDE:
            expr = context.makeBitwiseNotNode(location, expr);
            break;
        case MINUS:
            expr = context.makeNegateNode(location, expr);
            break;
        case PLUS:
            expr = context.createUnaryPlus(location, expr);
            break;
        case PLUSPLUS:
        case AUTOPLUSPLUS:
        	ASSERT(isSimpleAssignmentTarget(context, expr));
			expr = context.makePrefixNode(location, expr, Operator::PlusPlus, subExprStart, subExprStart + 2, end);
            m_parserState.assignmentCount++;
            break;
        case MINUSMINUS:
        case AUTOMINUSMINUS:
        	ASSERT(isSimpleAssignmentTarget(context, expr));
			expr = context.makePrefixNode(location, expr, Operator::MinusMinus, subExprStart, subExprStart + 2, end);
            m_parserState.assignmentCount++;
            break;
        case TYPEOF:
            expr = context.makeTypeOfNode(location, expr);
            break;
        case VOIDTOKEN:
            expr = context.createVoid(location, expr);
            break;
        case DELETETOKEN:
            failIfTrueIfStrict(context.isResolve(expr), ASCIILiteral("Cannot delete unqualified property '"), m_parserState.lastIdentifier, ASCIILiteral("' in strict mode"));
            semanticFailIfTrue(context.isPrivateLocation(expr), "Cannot delete private field ", m_parserState.lastPrivateName->impl());
			expr = context.makeDeleteNode(location, expr, context.unaryTokenStackLastStart(tokenStackDepth), end, end);
            break;
        default:
            // If we get here something has gone horribly horribly wrong
#ifndef NDEBUG
            RELEASE_ASSERT_NOT_REACHED();
#endif
            break;
        }
        context.unaryTokenStackRemoveLast(tokenStackDepth);
    }
    return expr;
}

template <typename LexerType> void Parser<LexerType>::printUnexpectedTokenText(WTF::PrintStream& out)
{
    switch (m_token.m_type) {
    case EOFTOK:
        out.print("Unexpected end of script");
        return;
    case UNTERMINATED_IDENTIFIER_ESCAPE_ERRORTOK:
    case UNTERMINATED_IDENTIFIER_UNICODE_ESCAPE_ERRORTOK:
        out.print("Incomplete unicode escape in identifier: '", getToken(), "'");
        return;
    case UNTERMINATED_MULTILINE_COMMENT_ERRORTOK:
        out.print("Unterminated multiline comment");
        return;
    case UNTERMINATED_NUMERIC_LITERAL_ERRORTOK:
        out.print("Unterminated numeric literal '", getToken(), "'");
        return;
    case UNTERMINATED_STRING_LITERAL_ERRORTOK:
        out.print("Unterminated string literal '", getToken(), "'");
        return;
    case INVALID_IDENTIFIER_ESCAPE_ERRORTOK:
        out.print("Invalid escape in identifier: '", getToken(), "'");
        return;
    case ESCAPED_KEYWORD:
        out.print("Unexpected escaped characters in keyword token: '", getToken(), "'");
        return;
    case INVALID_IDENTIFIER_UNICODE_ESCAPE_ERRORTOK:
        out.print("Invalid unicode escape in identifier: '", getToken(), "'");
        return;
    case INVALID_NUMERIC_LITERAL_ERRORTOK:
        out.print("Invalid numeric literal: '", getToken(), "'");
        return;
    case UNTERMINATED_OCTAL_NUMBER_ERRORTOK:
        out.print("Invalid use of octal: '", getToken(), "'");
        return;
    case INVALID_STRING_LITERAL_ERRORTOK:
        out.print("Invalid string literal: '", getToken(), "'");
        return;
    case INVALID_UNICODE_ENCODING_ERRORTOK:
        out.print("Invalid unicode encoding: '", getToken(), "'");
        return;
    case INVALID_IDENTIFIER_UNICODE_ERRORTOK:
        out.print("Invalid unicode code point in identifier: '", getToken(), "'");
        return;
    case ERRORTOK:
        out.print("Unrecognized token '", getToken(), "'");
        return;
    case STRING:
        out.print("Unexpected string literal ", getToken());
        return;
    case INTEGER:
    case DOUBLE:
        out.print("Unexpected number '", getToken(), "'");
        return;

    case RESERVED_IF_STRICT:
        out.print("Unexpected use of reserved word '", getToken(), "' in strict mode");
        return;

    case RESERVED:
        out.print("Unexpected use of reserved word '", getToken(), "'");
        return;

    case INVALID_PRIVATE_NAME_ERRORTOK:
        out.print("Invalid private name '", getToken(), "'");
        return;

    case PRIVATENAME:
        out.print("Unexpected private name ", getToken());
        return;

    case AWAIT:
    case IDENT:
        out.print("Unexpected identifier '", getToken(), "'");
        return;

    default:
        break;
    }

    if (m_token.m_type & KeywordTokenFlag) {
        out.print("Unexpected keyword '", getToken(), "'");
        return;
    }

    out.print("Unexpected token '", getToken(), "'");
}

// Instantiate the two flavors of Parser we need instead of putting most of this file in Parser.h
template class Parser< Lexer<LChar> >;
template class Parser< Lexer<UChar> >;

} // namespace JSC
