Skip to content

Commit 901db85

Browse files
committed
fix: limit TypePattern's VariableDeclaratorList to a single VariableDeclarator
1 parent 841de42 commit 901db85

File tree

5 files changed

+12
-43
lines changed

5 files changed

+12
-43
lines changed

packages/java-parser/api.d.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,6 @@ export abstract class JavaCstVisitor<IN, OUT> implements ICstVisitor<IN, OUT> {
171171
param?: IN
172172
): OUT;
173173
isDims(ctx: IsDimsCtx, param?: IN): OUT;
174-
isFollowingVariableDeclarator(
175-
ctx: IsFollowingVariableDeclaratorCtx,
176-
param?: IN
177-
): OUT;
178174
compilationUnit(ctx: CompilationUnitCtx, param?: IN): OUT;
179175
ordinaryCompilationUnit(ctx: OrdinaryCompilationUnitCtx, param?: IN): OUT;
180176
modularCompilationUnit(ctx: ModularCompilationUnitCtx, param?: IN): OUT;
@@ -503,10 +499,6 @@ export abstract class JavaCstVisitorWithDefaults<IN, OUT>
503499
param?: IN
504500
): OUT;
505501
isDims(ctx: IsDimsCtx, param?: IN): OUT;
506-
isFollowingVariableDeclarator(
507-
ctx: IsFollowingVariableDeclaratorCtx,
508-
param?: IN
509-
): OUT;
510502
compilationUnit(ctx: CompilationUnitCtx, param?: IN): OUT;
511503
ordinaryCompilationUnit(ctx: OrdinaryCompilationUnitCtx, param?: IN): OUT;
512504
modularCompilationUnit(ctx: ModularCompilationUnitCtx, param?: IN): OUT;
@@ -1802,16 +1794,6 @@ export type IsDimsCtx = {
18021794
RBrace?: IToken[];
18031795
};
18041796

1805-
export interface IsFollowingVariableDeclaratorCstNode extends CstNode {
1806-
name: "isFollowingVariableDeclarator";
1807-
children: IsFollowingVariableDeclaratorCtx;
1808-
}
1809-
1810-
export type IsFollowingVariableDeclaratorCtx = {
1811-
Comma: IToken[];
1812-
variableDeclarator: VariableDeclaratorCstNode[];
1813-
};
1814-
18151797
export interface CompilationUnitCstNode extends CstNode {
18161798
name: "compilationUnit";
18171799
children: CompilationUnitCtx;

packages/java-parser/src/productions/blocks-and-statements.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ export function defineRules($, t) {
4141
});
4242

4343
// https://docs.oracle.com/javase/specs/jls/se22/html/jls-14.html#jls-LocalVariableDeclaration
44-
$.RULE("localVariableDeclaration", () => {
44+
$.RULE("localVariableDeclaration", singleDeclarator => {
4545
$.MANY(() => {
4646
$.SUBRULE($.variableModifier);
4747
});
4848
$.SUBRULE($.localVariableType);
49-
$.SUBRULE($.variableDeclaratorList);
49+
$.SUBRULE($.variableDeclaratorList, { ARGS: [singleDeclarator] });
5050
});
5151

5252
// https://docs.oracle.com/javase/specs/jls/se22/html/jls-14.html#jls-LocalVariableType

packages/java-parser/src/productions/classes.js

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,12 @@ export function defineRules($, t) {
154154
});
155155

156156
// https://docs.oracle.com/javase/specs/jls/se22/html/jls-8.html#jls-VariableDeclaratorList
157-
$.RULE("variableDeclaratorList", () => {
157+
$.RULE("variableDeclaratorList", singleDeclarator => {
158158
$.SUBRULE($.variableDeclarator);
159159
$.MANY({
160-
// required to distinguish from patternList
161-
GATE: () => this.BACKTRACK_LOOKAHEAD($.isFollowingVariableDeclarator),
160+
// TypePattern has a semantic requirement that its VariableDeclaratorList
161+
// consists of a single VariableDeclarator
162+
GATE: () => !singleDeclarator,
162163
DEF: () => {
163164
$.CONSUME(t.Comma);
164165
$.SUBRULE2($.variableDeclarator);
@@ -729,23 +730,4 @@ export function defineRules($, t) {
729730
tokenMatcher(this.LA(1), t.LSquare) && tokenMatcher(this.LA(2), t.RSquare)
730731
);
731732
});
732-
733-
/*
734-
* The following sequence can either be a variable declarator or a component pattern if next token is a comma
735-
* This check if the following sequence is **not** a component pattern sequence
736-
*/
737-
$.RULE("isFollowingVariableDeclarator", () => {
738-
const hasDims =
739-
tokenMatcher(this.LA(3), t.LSquare) &&
740-
tokenMatcher(this.LA(4), t.RSquare);
741-
const offset = hasDims ? 2 : 0;
742-
if (
743-
tokenMatcher(this.LA(offset + 3), t.Identifier) ||
744-
tokenMatcher(this.LA(offset + 3), t.Underscore)
745-
) {
746-
return false;
747-
}
748-
749-
return !tokenMatcher(this.LA(3), t.LBrace);
750-
});
751733
}

packages/java-parser/src/productions/expressions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ export function defineRules($, t) {
595595

596596
// https://docs.oracle.com/javase/specs/jls/se22/html/jls-14.html#jls-TypePattern
597597
$.RULE("typePattern", () => {
598-
$.SUBRULE($.localVariableDeclaration);
598+
$.SUBRULE($.localVariableDeclaration, { ARGS: [true] });
599599
});
600600

601601
// https://docs.oracle.com/javase/specs/jls/se22/html/jls-14.html#jls-RecordPattern

packages/java-parser/test/pattern-matching/pattern-matching-spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,9 @@ describe("Pattern matching", () => {
8484
javaParser.parse(input, "componentPatternList")
8585
).to.not.throw();
8686
});
87+
88+
it("should parse switch label with multiple qualified case patterns", () => {
89+
const input = "case b.B _, c.C _";
90+
expect(() => javaParser.parse(input, "switchLabel")).to.not.throw();
91+
});
8792
});

0 commit comments

Comments
 (0)