Skip to content

Commit

Permalink
feat(parser): support pattern matching guards
Browse files Browse the repository at this point in the history
  • Loading branch information
clementdessoude committed Nov 26, 2022
1 parent 2e0e0da commit bfdc38e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
34 changes: 28 additions & 6 deletions packages/java-parser/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ export abstract class JavaCstVisitor<IN, OUT> implements ICstVisitor<IN, OUT> {
arrayAccessSuffix(ctx: ArrayAccessSuffixCtx, param?: IN): OUT;
methodReferenceSuffix(ctx: MethodReferenceSuffixCtx, param?: IN): OUT;
pattern(ctx: PatternCtx, param?: IN): OUT;
primaryPattern(ctx: PrimaryPatternCtx, param?: IN): OUT;
typePattern(ctx: TypePatternCtx, param?: IN): OUT;
identifyNewExpressionType(ctx: IdentifyNewExpressionTypeCtx, param?: IN): OUT;
isLambdaExpression(ctx: IsLambdaExpressionCtx, param?: IN): OUT;
Expand Down Expand Up @@ -625,6 +626,8 @@ export abstract class JavaCstVisitorWithDefaults<IN, OUT>
switchBlock(ctx: SwitchBlockCtx, param?: IN): OUT;
switchBlockStatementGroup(ctx: SwitchBlockStatementGroupCtx, param?: IN): OUT;
switchLabel(ctx: SwitchLabelCtx, param?: IN): OUT;
caseOrDefaultLabel(ctx: CaseOrDefaultLabelCtx, param?: IN): OUT;
caseLabelElement(ctx: CaseLabelElementCtx, param?: IN): OUT;
switchRule(ctx: SwitchRuleCtx, param?: IN): OUT;
caseConstant(ctx: CaseConstantCtx, param?: IN): OUT;
whileStatement(ctx: WhileStatementCtx, param?: IN): OUT;
Expand Down Expand Up @@ -728,6 +731,7 @@ export abstract class JavaCstVisitorWithDefaults<IN, OUT>
arrayAccessSuffix(ctx: ArrayAccessSuffixCtx, param?: IN): OUT;
methodReferenceSuffix(ctx: MethodReferenceSuffixCtx, param?: IN): OUT;
pattern(ctx: PatternCtx, param?: IN): OUT;
primaryPattern(ctx: PrimaryPatternCtx, param?: IN): OUT;
typePattern(ctx: TypePatternCtx, param?: IN): OUT;
identifyNewExpressionType(ctx: IdentifyNewExpressionTypeCtx, param?: IN): OUT;
isLambdaExpression(ctx: IsLambdaExpressionCtx, param?: IN): OUT;
Expand Down Expand Up @@ -1848,6 +1852,8 @@ export type IdentifyClassBodyDeclarationTypeCtx = {
Volatile?: IToken[];
Synchronized?: IToken[];
Native?: IToken[];
Sealed?: IToken[];
NonSealed?: IToken[];
Strictfp?: IToken[];
unannType: UnannTypeCstNode[];
};
Expand Down Expand Up @@ -2370,11 +2376,13 @@ export type IdentifyInterfaceBodyDeclarationTypeCtx = {
Public?: IToken[];
Protected?: IToken[];
Private?: IToken[];
Abstract?: IToken[];
Static?: IToken[];
Sealed?: IToken[];
NonSealed?: IToken[];
Strictfp?: IToken[];
Final?: IToken[];
Abstract?: IToken[];
Default?: IToken[];
Strictfp?: IToken[];
unannType: UnannTypeCstNode[];
};

Expand Down Expand Up @@ -2621,7 +2629,7 @@ export interface SwitchBlockStatementGroupCstNode extends CstNode {
export type SwitchBlockStatementGroupCtx = {
switchLabel: SwitchLabelCstNode[];
Colon: IToken[];
blockStatements: BlockStatementsCstNode[];
blockStatements?: BlockStatementsCstNode[];
};

export interface SwitchLabelCstNode extends CstNode {
Expand Down Expand Up @@ -2652,10 +2660,10 @@ export interface CaseLabelElementCstNode extends CstNode {
}

export type CaseLabelElementCtx = {
caseConstant?: CaseConstantCstNode[];
pattern?: PatternCstNode[];
Null?: IToken[];
Default?: IToken[];
pattern?: PatternCstNode[];
caseConstant?: CaseConstantCstNode[];
};

export interface SwitchRuleCstNode extends CstNode {
Expand Down Expand Up @@ -3489,7 +3497,21 @@ export interface PatternCstNode extends CstNode {
}

export type PatternCtx = {
typePattern: TypePatternCstNode[];
primaryPattern: PrimaryPatternCstNode[];
AndAnd?: IToken[];
binaryExpression?: BinaryExpressionCstNode[];
};

export interface PrimaryPatternCstNode extends CstNode {
name: "primaryPattern";
children: PrimaryPatternCtx;
}

export type PrimaryPatternCtx = {
LBrace?: IToken[];
pattern?: PatternCstNode[];
RBrace?: IToken[];
typePattern?: TypePatternCstNode[];
};

export interface TypePatternCstNode extends CstNode {
Expand Down
21 changes: 20 additions & 1 deletion packages/java-parser/src/productions/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,26 @@ function defineRules($, t) {
});

$.RULE("pattern", () => {
$.SUBRULE($.typePattern);
$.SUBRULE($.primaryPattern);
$.OPTION(() => {
$.CONSUME(t.AndAnd);
$.SUBRULE($.binaryExpression);
});
});

$.RULE("primaryPattern", () => {
$.OR([
{
ALT: () => {
$.CONSUME(t.LBrace);
$.SUBRULE($.pattern);
$.CONSUME(t.RBrace);
}
},
{
ALT: () => $.SUBRULE($.typePattern)
}
]);
});

$.RULE("typePattern", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,21 @@ describe("Pattern matching", () => {
`;
expect(() => javaParser.parse(input, "methodDeclaration")).to.not.throw();
});

it("should support pattern matching guards", () => {
const input = `
package com.vimat.model;
public record Buyer(String name, double bestPrice, double joker) {
public boolean hasBestOffer(Buyer other) {
return switch (other) {
case null -> true;
case Buyer b && this.bestPrice > b.bestPrice -> true;
default -> false;
};
}
}
`;
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw();
});
});
1 change: 1 addition & 0 deletions packages/prettier-plugin-java/src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ module.exports = {
{ value: "arrayAccessSuffix" },
{ value: "methodReferenceSuffix" },
{ value: "pattern" },
{ value: "primaryPattern" },
{ value: "typePattern" },
{ value: "identifyNewExpressionType" },
{ value: "isLambdaExpression" },
Expand Down

0 comments on commit bfdc38e

Please sign in to comment.