Skip to content

Commit

Permalink
support designated initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
SatyaRanjanDas-tomtom committed Oct 21, 2024
1 parent c53ff4b commit 7a7cf83
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
19 changes: 16 additions & 3 deletions cppparser/src/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ using namespace cppast;
%token <str> tknLShift tknRShift tknLShiftEq tknRShiftEq tknCmpEq tknNotEq tknLessEq tknGreaterEq
%token <str> tkn3WayCmp tknAnd tknOr tknInc tknDec tknArrow tknArrowStar
%token <str> tknLT tknGT // We will need the position of these operators in stream when used for declaring template instance.
%token <str> '+' '-' '*' '/' '%' '^' '&' '|' '~' '!' '=' ',' '(' ')' '[' ']' ';'
%token <str> '+' '-' '*' '/' '%' '^' '&' '|' '~' '!' '=' ',' '(' ')' '[' ']' ';' '.'
%token <str> tknNew tknDelete
%token <str> tknConst tknConstExpr // For templatearg parsing it is made as str type.
%token <str> tknVoid // For the cases when void is used as function parameter.
Expand All @@ -249,7 +249,7 @@ using namespace cppast;

%type <str> strlit
%type <str> optapidecor apidecor apidecortokensq
%type <str> identifier optidentifier numbertype typeidentifier varidentifier optname id name operfuncname funcname
%type <str> identifier optidentifier numbertype typeidentifier varidentifier optname id name designatedname operfuncname funcname
%type <str> templidentifier templqualifiedid
%type <str> doccommentstr optdoccommentstr
%type <str> rshift
Expand Down Expand Up @@ -278,7 +278,7 @@ using namespace cppast;
%type <templateParamList> templatespecifier templateparamlist
%type <templateParam> templateparam
%type <docCommentObj> doccomment
%type <cppExprObj> expr exprstmt optexpr lambdacapture captureallbyref captureallbyval exprorlist optexprorlist
%type <cppExprObj> expr exprstmt optexpr lambdacapture captureallbyref captureallbyval exprorlist optexprorlist desinatedinitialization
%type <exprList> exprlist optexprlist
%type <cppExprObj> objcarg objcarglist
%type <cppLambda> lambda
Expand Down Expand Up @@ -748,6 +748,10 @@ name
: tknName [ZZLOG; $$ = $1;] {}
;

designatedname
: '.' name [ZZLOG;] {$$ = mergeCppToken($1, $2);}
;

id
: tknID [ZZLOG; $$ = $1; ] {}
;
Expand Down Expand Up @@ -2073,11 +2077,20 @@ exprlist
$$ = new std::vector<std::unique_ptr<const cppast::CppExpression>>;
$$->emplace_back($1);
}
| desinatedinitialization [ZZLOG;] {
$$ = new std::vector<std::unique_ptr<const cppast::CppExpression>>;
$$->emplace_back($1);
}
| exprlist optdoccommentstr ',' optdoccommentstr expr %prec COMMA [ZZLOG;] { $1->emplace_back($5); $$ = $1; }
| exprlist optdoccommentstr ',' optdoccommentstr desinatedinitialization %prec COMMA [ZZLOG;] { $1->emplace_back($5); $$ = $1; }
| doccommentstr exprlist [ZZLOG;] { $$ = $2; }
| exprlist doccommentstr [ZZLOG;] { $$ = $1; }
;

desinatedinitialization
: designatedname '=' expr [ZZLOG;] { $$ = BinomialExpr(cppast::CppBinaryOperator::ASSIGN, NameExpr($1), $3); }
;

optexprlist
: [ZZLOG;] { $$ = nullptr; }
| exprlist [ZZLOG;] { $$ = $1; }
Expand Down
24 changes: 24 additions & 0 deletions cppparser/test/unit/initializer-list-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,27 @@ TEST_CASE_METHOD(InitializerListTest, "docstr in middle", "[initializerlist]")
cppast::CppConstVarEPtr var = members[0];
REQUIRE(var);
}

TEST_CASE_METHOD(InitializerListTest, "designated initializer", "[initializerlist]")
{
#if TEST_CASE_SNIPPET_STARTS_FROM_NEXT_LINE
struct Point
{
int x;
int y;
};
Point p = {.x = 4, .y = 0};
#endif
auto testSnippet = getTestSnippetParseStream(__LINE__ - 2);

cppparser::CppParser parser;
const auto ast = parser.parseStream(testSnippet.data(), testSnippet.size());
REQUIRE(ast != nullptr);

const auto members = GetAllOwnedEntities(*ast);
REQUIRE(members.size() == 2);
cppast::CppConstVarEPtr var = members[1];
REQUIRE(var);
cppast::CppConstInitializerListExprEPtr assignExpr = var->assignValue();
REQUIRE(assignExpr);
}

0 comments on commit 7a7cf83

Please sign in to comment.