Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions cppparser/src/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ class CppTemplateArg;
cppast::CppRefType refType;
unsigned int attr;
Optional<cppast::CppAccessType> objAccessType;
cppast::CppExpression* attribSpecifier;
cppast::CppCallArgs* attribSpecifiers;
cppast::CppIfBlock* ifBlock;
cppast::CppWhileBlock* whileBlock;
Expand Down Expand Up @@ -313,8 +312,7 @@ class CppTemplateArg;
%type <accessSpecifier> entityaccessspecifier
%type <identifierList> identifierlist
%type <funcThrowSpec> functhrowspec optfuncthrowspec
%type <attribSpecifier> attribspecifier
%type <attribSpecifiers> attribspecifiers optattribspecifiers
%type <attribSpecifiers> attribs optattribs attribspecifier attribspecifiers optattribspecifiers
%type <hashDefine> define
%type <hashUndef> undef
%type <hashInclude> include
Expand Down Expand Up @@ -1703,8 +1701,26 @@ classdefnstmt
: classdefn ';' [ZZVALID;] { $$ = $1; }
;

attribs
: expr {
$$ = new std::vector<std::unique_ptr<cppast::CppExpression>>;
$$->push_back(Ptr($1));
}
| attribs ',' expr {
$$ = $1;
$$->push_back(Ptr($3));
}
;

optattribs
: {
$$ = new std::vector<std::unique_ptr<cppast::CppExpression>>;
}
| attribs { $$ = $1; }
;

attribspecifier
: '[' '[' expr ']' ']' {
: '[' '[' optattribs ']' ']' {
$$ = $3;
}
;
Expand All @@ -1719,11 +1735,11 @@ optattribspecifiers
attribspecifiers
: attribspecifier {
$$ = new std::vector<std::unique_ptr<cppast::CppExpression>>;
$$->push_back(Ptr($1));
$$->insert($$->end(), std::make_move_iterator($1->begin()), std::make_move_iterator($1->end()));
}
| attribspecifiers attribspecifier {
$$ = $1;
$$->push_back(Ptr($2));
$$->insert($$->end(), std::make_move_iterator($2->begin()), std::make_move_iterator($2->end()));
}
;

Expand Down
46 changes: 45 additions & 1 deletion cppparser/test/unit/attribute-specifier-sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ TEST_CASE_METHOD(CppAtributeTest, "Attribute specifier sequence")

[[xnet::HttpPost]] [[xnet::Route("/register")]] std::string CreateAccount(
[[xnet::FromBody]] [[xnet::EnsureValid]] SignUpRequest request);

[[xnet::HttpGet, xnet::Route("/entities"), xnet::Route("/entity")]] std::string GetEntities();

[[]] [[]] void UnannotatedHelper();
};
#endif
auto testSnippet = getTestSnippetParseStream(__LINE__ - 2);
Expand Down Expand Up @@ -72,7 +76,7 @@ TEST_CASE_METHOD(CppAtributeTest, "Attribute specifier sequence")
CHECK((*classAttrib1Arg) == cppast::CppStringLiteralExpr("\"/plakmp\""));

const auto classMembers = GetAllOwnedEntities(*classDefn);
REQUIRE(classMembers.size() == 4);
REQUIRE(classMembers.size() == 6);

const cppast::CppConstFunctionEPtr methodGetPlakMpPlayers = classMembers[1];
REQUIRE(methodGetPlakMpPlayers);
Expand All @@ -95,4 +99,44 @@ TEST_CASE_METHOD(CppAtributeTest, "Attribute specifier sequence")
cppast::CppConstStringLiteralExprEPtr methodAttrib1Arg = &(methodAttrib1->arg(0));
REQUIRE(methodAttrib1Arg);
CHECK((*methodAttrib1Arg) == cppast::CppStringLiteralExpr("\"/players\""));

const cppast::CppConstFunctionEPtr methodGetEntities = classMembers[4];
REQUIRE(methodGetEntities);
const auto* returnTypeGetEntities = methodGetEntities->returnType();
REQUIRE(returnTypeGetEntities);

const auto attribSeqGetEntities = GetAllAttributeSpecifiers(*returnTypeGetEntities);
REQUIRE(attribSeqGetEntities.size() == 3);

cppast::CppConstNameExprEPtr method2Attrib0 = attribSeqGetEntities.at(0);
REQUIRE(method2Attrib0);
CHECK((*method2Attrib0) == cppast::CppNameExpr("xnet::HttpGet"));

cppast::CppConstFunctionCallExprEPtr method2Attrib1 = attribSeqGetEntities.at(1);
REQUIRE(method2Attrib1);
cppast::CppConstNameExprEPtr method2Attrib1Func = &(method2Attrib1->function());
REQUIRE(method2Attrib1Func);
CHECK((*method2Attrib1Func) == cppast::CppNameExpr("xnet::Route"));
REQUIRE(method2Attrib1->numArgs() == 1);
cppast::CppConstStringLiteralExprEPtr method2Attrib1Arg = &(method2Attrib1->arg(0));
REQUIRE(method2Attrib1Arg);
CHECK((*method2Attrib1Arg) == cppast::CppStringLiteralExpr("\"/entities\""));

cppast::CppConstFunctionCallExprEPtr method2Attrib2 = attribSeqGetEntities.at(2);
REQUIRE(method2Attrib2);
cppast::CppConstNameExprEPtr method2Attrib2Func = &(method2Attrib2->function());
REQUIRE(method2Attrib2Func);
CHECK((*method2Attrib2Func) == cppast::CppNameExpr("xnet::Route"));
REQUIRE(method2Attrib2->numArgs() == 1);
cppast::CppConstStringLiteralExprEPtr method2Attrib2Arg = &(method2Attrib2->arg(0));
REQUIRE(method2Attrib2Arg);
CHECK((*method2Attrib2Arg) == cppast::CppStringLiteralExpr("\"/entity\""));

const cppast::CppConstFunctionEPtr methodUnannotatedHelper = classMembers[5];
REQUIRE(methodUnannotatedHelper);
const auto* returnTypeUnannotatedHelper = methodUnannotatedHelper->returnType();
REQUIRE(returnTypeUnannotatedHelper);

const auto attribSeqUnannotatedHelper = GetAllAttributeSpecifiers(*returnTypeUnannotatedHelper);
REQUIRE(attribSeqUnannotatedHelper.size() == 0);
}