Skip to content

Commit b8ef134

Browse files
committed
Parser: support multiple trailing declarators after class body (} a, b, *c;)
1 parent 8ec892c commit b8ef134

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

src/java/Parser.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -863,21 +863,23 @@ else if (checkOp(">>")) {
863863
matchPunct(";");
864864

865865
TypeDef typeDef = new TypeDef(kind, name, templateParams, baseClasses, members, start.line(), start.col(), leadingComments);
866-
// Trailing declarator: "class Foo { ... } *ptr;" or "} instance;"
866+
// Trailing declarators: "class Foo { ... } *ptr;" or "} a, b, *c;"
867867
if (!matchPunct(";") && !isAtEnd() && !checkPunct("}")) {
868-
int ptrDepth = 0;
869-
while (matchOp("*") || matchPunct("*")) ptrDepth++;
870-
if (peek().type() == CppLexerTokenType.IDENTIFIER) {
868+
List<TopLevelItem> result = new ArrayList<>();
869+
result.add(typeDef);
870+
do {
871+
int ptrDepth = 0;
872+
while (matchOp("*") || matchPunct("*")) ptrDepth++;
873+
if (peek().type() != CppLexerTokenType.IDENTIFIER) break;
871874
CppLexerToken varTok = advance();
872875
Expr init = null;
873876
if (matchPunct("=")) init = parseExpr();
874-
expectPunct(";");
875877
TypeRef varType = new NamedType(name, List.of(), ptrDepth, false, false, false);
876-
VariableDecl varDecl = new VariableDecl(varType, varTok.text(), List.of(), init,
877-
false, false, varTok.line(), varTok.col(), List.of());
878-
return List.of(typeDef, varDecl);
879-
}
880-
matchPunct(";");
878+
result.add(new VariableDecl(varType, varTok.text(), List.of(), init,
879+
false, false, varTok.line(), varTok.col(), List.of()));
880+
} while (matchPunct(","));
881+
expectPunct(";");
882+
return result;
881883
}
882884
return List.of(typeDef);
883885
}

0 commit comments

Comments
 (0)