예제 #1
0
/*
 “GRAMMAR OF A CLASS DECLARATION
 
 ‌ class-declaration → attributes opt class class-name generic-parameter-clause opt type-inheritance-clause opt class-body
 ‌ class-name → identifier
 ‌ class-body → {declarations opt}”
 
*/
DeclarationPtr Parser::parseClass(const std::vector<AttributePtr>& attrs)
{
    Token token;
    expect(Keyword::Class, token);
    ClassDefPtr ret = nodeFactory->createClass(token.state);
    ret->setAttributes(attrs);
    expect_identifier(token);
    TypeIdentifierPtr typeId = nodeFactory->createTypeIdentifier(token.state);
    typeId->setName(token.token);
    ret->setIdentifier(typeId);
    if(predicate(L"<"))
    {
        GenericParametersDefPtr params = parseGenericParametersDef();
        ret->setGenericParametersDef(params);
    }
    if(match(L":"))
    {
        do
        {
            TypeIdentifierPtr protocol = parseTypeIdentifier();
            ret->addParent(protocol);
        }while(match(L","));
    }
    
    Flags f(this);
    f += UNDER_CLASS;
    
    expect(L"{");
    while(!predicate(L"}"))
    {
        DeclarationPtr decl = parseDeclaration();
        ret->addDeclaration(decl);
    }
    expect(L"}");
    return ret;
}