Skip to content

Commit 7a7cf83

Browse files
support designated initializer
1 parent c53ff4b commit 7a7cf83

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

cppparser/src/parser.y

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ using namespace cppast;
227227
%token <str> tknLShift tknRShift tknLShiftEq tknRShiftEq tknCmpEq tknNotEq tknLessEq tknGreaterEq
228228
%token <str> tkn3WayCmp tknAnd tknOr tknInc tknDec tknArrow tknArrowStar
229229
%token <str> tknLT tknGT // We will need the position of these operators in stream when used for declaring template instance.
230-
%token <str> '+' '-' '*' '/' '%' '^' '&' '|' '~' '!' '=' ',' '(' ')' '[' ']' ';'
230+
%token <str> '+' '-' '*' '/' '%' '^' '&' '|' '~' '!' '=' ',' '(' ')' '[' ']' ';' '.'
231231
%token <str> tknNew tknDelete
232232
%token <str> tknConst tknConstExpr // For templatearg parsing it is made as str type.
233233
%token <str> tknVoid // For the cases when void is used as function parameter.
@@ -249,7 +249,7 @@ using namespace cppast;
249249

250250
%type <str> strlit
251251
%type <str> optapidecor apidecor apidecortokensq
252-
%type <str> identifier optidentifier numbertype typeidentifier varidentifier optname id name operfuncname funcname
252+
%type <str> identifier optidentifier numbertype typeidentifier varidentifier optname id name designatedname operfuncname funcname
253253
%type <str> templidentifier templqualifiedid
254254
%type <str> doccommentstr optdoccommentstr
255255
%type <str> rshift
@@ -278,7 +278,7 @@ using namespace cppast;
278278
%type <templateParamList> templatespecifier templateparamlist
279279
%type <templateParam> templateparam
280280
%type <docCommentObj> doccomment
281-
%type <cppExprObj> expr exprstmt optexpr lambdacapture captureallbyref captureallbyval exprorlist optexprorlist
281+
%type <cppExprObj> expr exprstmt optexpr lambdacapture captureallbyref captureallbyval exprorlist optexprorlist desinatedinitialization
282282
%type <exprList> exprlist optexprlist
283283
%type <cppExprObj> objcarg objcarglist
284284
%type <cppLambda> lambda
@@ -748,6 +748,10 @@ name
748748
: tknName [ZZLOG; $$ = $1;] {}
749749
;
750750

751+
designatedname
752+
: '.' name [ZZLOG;] {$$ = mergeCppToken($1, $2);}
753+
;
754+
751755
id
752756
: tknID [ZZLOG; $$ = $1; ] {}
753757
;
@@ -2073,11 +2077,20 @@ exprlist
20732077
$$ = new std::vector<std::unique_ptr<const cppast::CppExpression>>;
20742078
$$->emplace_back($1);
20752079
}
2080+
| desinatedinitialization [ZZLOG;] {
2081+
$$ = new std::vector<std::unique_ptr<const cppast::CppExpression>>;
2082+
$$->emplace_back($1);
2083+
}
20762084
| exprlist optdoccommentstr ',' optdoccommentstr expr %prec COMMA [ZZLOG;] { $1->emplace_back($5); $$ = $1; }
2085+
| exprlist optdoccommentstr ',' optdoccommentstr desinatedinitialization %prec COMMA [ZZLOG;] { $1->emplace_back($5); $$ = $1; }
20772086
| doccommentstr exprlist [ZZLOG;] { $$ = $2; }
20782087
| exprlist doccommentstr [ZZLOG;] { $$ = $1; }
20792088
;
20802089

2090+
desinatedinitialization
2091+
: designatedname '=' expr [ZZLOG;] { $$ = BinomialExpr(cppast::CppBinaryOperator::ASSIGN, NameExpr($1), $3); }
2092+
;
2093+
20812094
optexprlist
20822095
: [ZZLOG;] { $$ = nullptr; }
20832096
| exprlist [ZZLOG;] { $$ = $1; }

cppparser/test/unit/initializer-list-test.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,27 @@ TEST_CASE_METHOD(InitializerListTest, "docstr in middle", "[initializerlist]")
3636
cppast::CppConstVarEPtr var = members[0];
3737
REQUIRE(var);
3838
}
39+
40+
TEST_CASE_METHOD(InitializerListTest, "designated initializer", "[initializerlist]")
41+
{
42+
#if TEST_CASE_SNIPPET_STARTS_FROM_NEXT_LINE
43+
struct Point
44+
{
45+
int x;
46+
int y;
47+
};
48+
Point p = {.x = 4, .y = 0};
49+
#endif
50+
auto testSnippet = getTestSnippetParseStream(__LINE__ - 2);
51+
52+
cppparser::CppParser parser;
53+
const auto ast = parser.parseStream(testSnippet.data(), testSnippet.size());
54+
REQUIRE(ast != nullptr);
55+
56+
const auto members = GetAllOwnedEntities(*ast);
57+
REQUIRE(members.size() == 2);
58+
cppast::CppConstVarEPtr var = members[1];
59+
REQUIRE(var);
60+
cppast::CppConstInitializerListExprEPtr assignExpr = var->assignValue();
61+
REQUIRE(assignExpr);
62+
}

0 commit comments

Comments
 (0)