+template <typename LexerType>
+template <class TreeBuilder> TreeClassExpression Parser<LexerType>::parseClass(TreeBuilder& context, FunctionRequirements requirements, ParserClassInfo<TreeBuilder>& info)
+{
+ ASSERT(match(CLASSTOKEN));
+ JSTokenLocation location(tokenLocation());
+ next();
+
+ AutoPopScopeRef classScope(this, pushScope());
+ classScope->setStrictMode();
+
+ const Identifier* className = nullptr;
+ if (match(IDENT)) {
+ className = m_token.m_data.ident;
+ info.className = className;
+ next();
+ failIfFalse(classScope->declareVariable(className), "'", className->impl(), "' is not a valid class name");
+ } else if (requirements == FunctionNeedsName) {
+ if (match(OPENBRACE))
+ semanticFail("Class statements must have a name");
+ semanticFailureDueToKeyword("class name");
+ failDueToUnexpectedToken();
+ } else
+ className = &m_vm->propertyNames->nullIdentifier;
+ ASSERT(className);
+
+ TreeExpression parentClass = 0;
+ if (consume(EXTENDS)) {
+ parentClass = parseMemberExpression(context);
+ failIfFalse(parentClass, "Cannot parse the parent class name");
+ }
+ const ConstructorKind constructorKind = parentClass ? ConstructorKind::Derived : ConstructorKind::Base;
+
+ consumeOrFail(OPENBRACE, "Expected opening '{' at the start of a class body");
+
+ TreeExpression constructor = 0;
+ TreePropertyList staticMethods = 0;
+ TreePropertyList instanceMethods = 0;
+ TreePropertyList instanceMethodsTail = 0;
+ TreePropertyList staticMethodsTail = 0;
+ while (!match(CLOSEBRACE)) {
+ if (match(SEMICOLON)) {
+ next();
+ continue;
+ }
+
+ JSTokenLocation methodLocation(tokenLocation());
+ unsigned methodStart = tokenStart();
+
+ // For backwards compatibility, "static" is a non-reserved keyword in non-strict mode.
+ bool isStaticMethod = match(RESERVED_IF_STRICT) && *m_token.m_data.ident == m_vm->propertyNames->staticKeyword;
+ if (isStaticMethod)
+ next();
+
+ // FIXME: Figure out a way to share more code with parseProperty.
+ const CommonIdentifiers& propertyNames = *m_vm->propertyNames;
+ const Identifier* ident = nullptr;
+ bool isGetter = false;
+ bool isSetter = false;
+ switch (m_token.m_type) {
+ case STRING:
+ ident = m_token.m_data.ident;
+ ASSERT(ident);
+ next();
+ break;
+ case IDENT:
+ ident = m_token.m_data.ident;
+ isGetter = *ident == propertyNames.get;
+ isSetter = *ident == propertyNames.set;
+ ASSERT(ident);
+ break;
+ case DOUBLE:
+ case INTEGER:
+ ident = &m_parserArena.identifierArena().makeNumericIdentifier(const_cast<VM*>(m_vm), m_token.m_data.doubleValue);
+ ASSERT(ident);
+ next();
+ break;
+ default:
+ failDueToUnexpectedToken();
+ }
+
+ TreeProperty property;
+ const bool alwaysStrictInsideClass = true;
+ if (isGetter || isSetter) {
+ nextExpectIdentifier(LexerFlagsIgnoreReservedWords);
+ property = parseGetterSetter(context, alwaysStrictInsideClass, isGetter ? PropertyNode::Getter : PropertyNode::Setter, methodStart,
+ ConstructorKind::None, SuperBinding::Needed);
+ failIfFalse(property, "Cannot parse this method");
+ } else {
+ ParserFunctionInfo<TreeBuilder> methodInfo;
+ bool isConstructor = !isStaticMethod && *ident == propertyNames.constructor;
+ failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, isStaticMethod ? FunctionMode : MethodMode, false, isConstructor ? constructorKind : ConstructorKind::None, SuperBinding::Needed, methodStart, methodInfo, StandardFunctionParseType)), "Cannot parse this method");
+ failIfFalse(ident && declareVariable(ident), "Cannot declare a method named '", methodInfo.name->impl(), "'");
+ methodInfo.name = isConstructor ? className : ident;
+
+ TreeExpression method = context.createFunctionExpr(methodLocation, methodInfo);
+ if (isConstructor) {
+ semanticFailIfTrue(constructor, "Cannot declare multiple constructors in a single class");
+ constructor = method;
+ continue;
+ }
+
+ // FIXME: Syntax error when super() is called
+ semanticFailIfTrue(isStaticMethod && methodInfo.name && *methodInfo.name == propertyNames.prototype,
+ "Cannot declare a static method named 'prototype'");
+ property = context.createProperty(methodInfo.name, method, PropertyNode::Constant, PropertyNode::Unknown, alwaysStrictInsideClass, SuperBinding::Needed);
+ }
+
+ TreePropertyList& tail = isStaticMethod ? staticMethodsTail : instanceMethodsTail;
+ if (tail)
+ tail = context.createPropertyList(methodLocation, property, tail);
+ else {
+ tail = context.createPropertyList(methodLocation, property);
+ if (isStaticMethod)
+ staticMethods = tail;
+ else
+ instanceMethods = tail;
+ }
+ }
+
+ failIfFalse(popScope(classScope, TreeBuilder::NeedsFreeVariableInfo), "Parser error");
+ consumeOrFail(CLOSEBRACE, "Expected a closing '}' after a class body");
+
+ return context.createClassExpr(location, *className, constructor, parentClass, instanceMethods, staticMethods);
+}
+#endif
+