Skip to content

Commit

Permalink
perf: leverage Chevrotain Allstar to reduce backtracking
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkiesel committed Nov 26, 2023
1 parent 3591437 commit 38afbfe
Show file tree
Hide file tree
Showing 17 changed files with 132 additions and 1,234 deletions.
256 changes: 0 additions & 256 deletions packages/java-parser/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,7 @@ export abstract class JavaCstVisitor<IN, OUT> implements ICstVisitor<IN, OUT> {
ctx: CompactConstructorDeclarationCtx,
param?: IN
): OUT;
isClassDeclaration(ctx: IsClassDeclarationCtx, param?: IN): OUT;
identifyClassBodyDeclarationType(
ctx: IdentifyClassBodyDeclarationTypeCtx,
param?: IN
): OUT;
isDims(ctx: IsDimsCtx, param?: IN): OUT;
isCompactConstructorDeclaration(
ctx: IsCompactConstructorDeclarationCtx,
param?: IN
): OUT;
compilationUnit(ctx: CompilationUnitCtx, param?: IN): OUT;
ordinaryCompilationUnit(ctx: OrdinaryCompilationUnitCtx, param?: IN): OUT;
modularCompilationUnit(ctx: ModularCompilationUnitCtx, param?: IN): OUT;
Expand All @@ -195,7 +186,6 @@ export abstract class JavaCstVisitor<IN, OUT> implements ICstVisitor<IN, OUT> {
usesModuleDirective(ctx: UsesModuleDirectiveCtx, param?: IN): OUT;
providesModuleDirective(ctx: ProvidesModuleDirectiveCtx, param?: IN): OUT;
requiresModifier(ctx: RequiresModifierCtx, param?: IN): OUT;
isModuleCompilationUnit(ctx: IsModuleCompilationUnitCtx, param?: IN): OUT;
interfaceDeclaration(ctx: InterfaceDeclarationCtx, param?: IN): OUT;
normalInterfaceDeclaration(
ctx: NormalInterfaceDeclarationCtx,
Expand Down Expand Up @@ -240,18 +230,6 @@ export abstract class JavaCstVisitor<IN, OUT> implements ICstVisitor<IN, OUT> {
param?: IN
): OUT;
elementValueList(ctx: ElementValueListCtx, param?: IN): OUT;
identifyInterfaceBodyDeclarationType(
ctx: IdentifyInterfaceBodyDeclarationTypeCtx,
param?: IN
): OUT;
identifyAnnotationBodyDeclarationType(
ctx: IdentifyAnnotationBodyDeclarationTypeCtx,
param?: IN
): OUT;
isSimpleElementValueAnnotation(
ctx: IsSimpleElementValueAnnotationCtx,
param?: IN
): OUT;
arrayInitializer(ctx: ArrayInitializerCtx, param?: IN): OUT;
variableInitializerList(ctx: VariableInitializerListCtx, param?: IN): OUT;
block(ctx: BlockCtx, param?: IN): OUT;
Expand Down Expand Up @@ -305,12 +283,6 @@ export abstract class JavaCstVisitor<IN, OUT> implements ICstVisitor<IN, OUT> {
resource(ctx: ResourceCtx, param?: IN): OUT;
yieldStatement(ctx: YieldStatementCtx, param?: IN): OUT;
variableAccess(ctx: VariableAccessCtx, param?: IN): OUT;
isBasicForStatement(ctx: IsBasicForStatementCtx, param?: IN): OUT;
isLocalVariableDeclaration(
ctx: IsLocalVariableDeclarationCtx,
param?: IN
): OUT;
isClassicSwitchLabel(ctx: IsClassicSwitchLabelCtx, param?: IN): OUT;
expression(ctx: ExpressionCtx, param?: IN): OUT;
lambdaExpression(ctx: LambdaExpressionCtx, param?: IN): OUT;
lambdaParameters(ctx: LambdaParametersCtx, param?: IN): OUT;
Expand Down Expand Up @@ -386,14 +358,6 @@ export abstract class JavaCstVisitor<IN, OUT> implements ICstVisitor<IN, OUT> {
componentPattern(ctx: ComponentPatternCtx, param?: IN): OUT;
unnamedPattern(ctx: UnnamedPatternCtx, param?: IN): OUT;
guard(ctx: GuardCtx, param?: IN): OUT;
identifyNewExpressionType(ctx: IdentifyNewExpressionTypeCtx, param?: IN): OUT;
isLambdaExpression(ctx: IsLambdaExpressionCtx, param?: IN): OUT;
isCastExpression(ctx: IsCastExpressionCtx, param?: IN): OUT;
isPrimitiveCastExpression(ctx: IsPrimitiveCastExpressionCtx, param?: IN): OUT;
isReferenceTypeCastExpression(
ctx: IsReferenceTypeCastExpressionCtx,
param?: IN
): OUT;
isRefTypeInMethodRef(ctx: IsRefTypeInMethodRefCtx, param?: IN): OUT;
}

Expand Down Expand Up @@ -527,16 +491,7 @@ export abstract class JavaCstVisitorWithDefaults<IN, OUT>
ctx: CompactConstructorDeclarationCtx,
param?: IN
): OUT;
isClassDeclaration(ctx: IsClassDeclarationCtx, param?: IN): OUT;
identifyClassBodyDeclarationType(
ctx: IdentifyClassBodyDeclarationTypeCtx,
param?: IN
): OUT;
isDims(ctx: IsDimsCtx, param?: IN): OUT;
isCompactConstructorDeclaration(
ctx: IsCompactConstructorDeclarationCtx,
param?: IN
): OUT;
compilationUnit(ctx: CompilationUnitCtx, param?: IN): OUT;
ordinaryCompilationUnit(ctx: OrdinaryCompilationUnitCtx, param?: IN): OUT;
modularCompilationUnit(ctx: ModularCompilationUnitCtx, param?: IN): OUT;
Expand All @@ -552,7 +507,6 @@ export abstract class JavaCstVisitorWithDefaults<IN, OUT>
usesModuleDirective(ctx: UsesModuleDirectiveCtx, param?: IN): OUT;
providesModuleDirective(ctx: ProvidesModuleDirectiveCtx, param?: IN): OUT;
requiresModifier(ctx: RequiresModifierCtx, param?: IN): OUT;
isModuleCompilationUnit(ctx: IsModuleCompilationUnitCtx, param?: IN): OUT;
interfaceDeclaration(ctx: InterfaceDeclarationCtx, param?: IN): OUT;
normalInterfaceDeclaration(
ctx: NormalInterfaceDeclarationCtx,
Expand Down Expand Up @@ -597,18 +551,6 @@ export abstract class JavaCstVisitorWithDefaults<IN, OUT>
param?: IN
): OUT;
elementValueList(ctx: ElementValueListCtx, param?: IN): OUT;
identifyInterfaceBodyDeclarationType(
ctx: IdentifyInterfaceBodyDeclarationTypeCtx,
param?: IN
): OUT;
identifyAnnotationBodyDeclarationType(
ctx: IdentifyAnnotationBodyDeclarationTypeCtx,
param?: IN
): OUT;
isSimpleElementValueAnnotation(
ctx: IsSimpleElementValueAnnotationCtx,
param?: IN
): OUT;
arrayInitializer(ctx: ArrayInitializerCtx, param?: IN): OUT;
variableInitializerList(ctx: VariableInitializerListCtx, param?: IN): OUT;
block(ctx: BlockCtx, param?: IN): OUT;
Expand Down Expand Up @@ -662,12 +604,6 @@ export abstract class JavaCstVisitorWithDefaults<IN, OUT>
resource(ctx: ResourceCtx, param?: IN): OUT;
yieldStatement(ctx: YieldStatementCtx, param?: IN): OUT;
variableAccess(ctx: VariableAccessCtx, param?: IN): OUT;
isBasicForStatement(ctx: IsBasicForStatementCtx, param?: IN): OUT;
isLocalVariableDeclaration(
ctx: IsLocalVariableDeclarationCtx,
param?: IN
): OUT;
isClassicSwitchLabel(ctx: IsClassicSwitchLabelCtx, param?: IN): OUT;
expression(ctx: ExpressionCtx, param?: IN): OUT;
lambdaExpression(ctx: LambdaExpressionCtx, param?: IN): OUT;
lambdaParameters(ctx: LambdaParametersCtx, param?: IN): OUT;
Expand Down Expand Up @@ -743,14 +679,6 @@ export abstract class JavaCstVisitorWithDefaults<IN, OUT>
componentPattern(ctx: ComponentPatternCtx, param?: IN): OUT;
unnamedPattern(ctx: UnnamedPatternCtx, param?: IN): OUT;
guard(ctx: GuardCtx, param?: IN): OUT;
identifyNewExpressionType(ctx: IdentifyNewExpressionTypeCtx, param?: IN): OUT;
isLambdaExpression(ctx: IsLambdaExpressionCtx, param?: IN): OUT;
isCastExpression(ctx: IsCastExpressionCtx, param?: IN): OUT;
isPrimitiveCastExpression(ctx: IsPrimitiveCastExpressionCtx, param?: IN): OUT;
isReferenceTypeCastExpression(
ctx: IsReferenceTypeCastExpressionCtx,
param?: IN
): OUT;
isRefTypeInMethodRef(ctx: IsRefTypeInMethodRefCtx, param?: IN): OUT;
}

Expand Down Expand Up @@ -1836,39 +1764,6 @@ export type CompactConstructorDeclarationCtx = {
constructorBody: ConstructorBodyCstNode[];
};

export interface IsClassDeclarationCstNode extends CstNode {
name: "isClassDeclaration";
children: IsClassDeclarationCtx;
}

export type IsClassDeclarationCtx = {
Semicolon?: IToken[];
classModifier?: ClassModifierCstNode[];
};

export interface IdentifyClassBodyDeclarationTypeCstNode extends CstNode {
name: "identifyClassBodyDeclarationType";
children: IdentifyClassBodyDeclarationTypeCtx;
}

export type IdentifyClassBodyDeclarationTypeCtx = {
annotation?: AnnotationCstNode[];
Public?: IToken[];
Protected?: IToken[];
Private?: IToken[];
Abstract?: IToken[];
Static?: IToken[];
Final?: IToken[];
Transient?: IToken[];
Volatile?: IToken[];
Synchronized?: IToken[];
Native?: IToken[];
Sealed?: IToken[];
NonSealed?: IToken[];
Strictfp?: IToken[];
unannType: UnannTypeCstNode[];
};

export interface IsDimsCstNode extends CstNode {
name: "isDims";
children: IsDimsCtx;
Expand All @@ -1883,20 +1778,6 @@ export type IsDimsCtx = {
RBrace?: IToken[];
};

export interface IsCompactConstructorDeclarationCstNode extends CstNode {
name: "isCompactConstructorDeclaration";
children: IsCompactConstructorDeclarationCtx;
}

export type IsCompactConstructorDeclarationCtx = {
annotation?: AnnotationCstNode[];
Public?: IToken[];
Protected?: IToken[];
Private?: IToken[];
simpleTypeName: SimpleTypeNameCstNode[];
LCurly: IToken[];
};

export interface CompilationUnitCstNode extends CstNode {
name: "compilationUnit";
children: CompilationUnitCtx;
Expand Down Expand Up @@ -2090,17 +1971,6 @@ export type RequiresModifierCtx = {
Static?: IToken[];
};

export interface IsModuleCompilationUnitCstNode extends CstNode {
name: "isModuleCompilationUnit";
children: IsModuleCompilationUnitCtx;
}

export type IsModuleCompilationUnitCtx = {
packageDeclaration?: PackageDeclarationCstNode[];
importDeclaration?: ImportDeclarationCstNode[];
annotation?: AnnotationCstNode[];
};

export interface InterfaceDeclarationCstNode extends CstNode {
name: "interfaceDeclaration";
children: InterfaceDeclarationCtx;
Expand Down Expand Up @@ -2379,52 +2249,6 @@ export type ElementValueListCtx = {
Comma?: IToken[];
};

export interface IdentifyInterfaceBodyDeclarationTypeCstNode extends CstNode {
name: "identifyInterfaceBodyDeclarationType";
children: IdentifyInterfaceBodyDeclarationTypeCtx;
}

export type IdentifyInterfaceBodyDeclarationTypeCtx = {
annotation?: AnnotationCstNode[];
Public?: IToken[];
Protected?: IToken[];
Private?: IToken[];
Abstract?: IToken[];
Static?: IToken[];
Sealed?: IToken[];
NonSealed?: IToken[];
Strictfp?: IToken[];
Final?: IToken[];
Default?: IToken[];
unannType: UnannTypeCstNode[];
};

export interface IdentifyAnnotationBodyDeclarationTypeCstNode extends CstNode {
name: "identifyAnnotationBodyDeclarationType";
children: IdentifyAnnotationBodyDeclarationTypeCtx;
}

export type IdentifyAnnotationBodyDeclarationTypeCtx = {
annotation?: AnnotationCstNode[];
Public?: IToken[];
Protected?: IToken[];
Private?: IToken[];
Abstract?: IToken[];
Static?: IToken[];
Final?: IToken[];
Strictfp?: IToken[];
unannType: UnannTypeCstNode[];
};

export interface IsSimpleElementValueAnnotationCstNode extends CstNode {
name: "isSimpleElementValueAnnotation";
children: IsSimpleElementValueAnnotationCtx;
}

export type IsSimpleElementValueAnnotationCtx = {
annotation: AnnotationCstNode[];
};

export interface ArrayInitializerCstNode extends CstNode {
name: "arrayInitializer";
children: ArrayInitializerCtx;
Expand Down Expand Up @@ -2972,39 +2796,6 @@ export type VariableAccessCtx = {
primary: PrimaryCstNode[];
};

export interface IsBasicForStatementCstNode extends CstNode {
name: "isBasicForStatement";
children: IsBasicForStatementCtx;
}

export type IsBasicForStatementCtx = {
For: IToken[];
LBrace: IToken[];
forInit?: ForInitCstNode[];
Semicolon: IToken[];
};

export interface IsLocalVariableDeclarationCstNode extends CstNode {
name: "isLocalVariableDeclaration";
children: IsLocalVariableDeclarationCtx;
}

export type IsLocalVariableDeclarationCtx = {
variableModifier?: VariableModifierCstNode[];
localVariableType: LocalVariableTypeCstNode[];
variableDeclaratorId: VariableDeclaratorIdCstNode[];
};

export interface IsClassicSwitchLabelCstNode extends CstNode {
name: "isClassicSwitchLabel";
children: IsClassicSwitchLabelCtx;
}

export type IsClassicSwitchLabelCtx = {
switchLabel: SwitchLabelCstNode[];
Colon: IToken[];
};

export interface ExpressionCstNode extends CstNode {
name: "expression";
children: ExpressionCtx;
Expand Down Expand Up @@ -3543,53 +3334,6 @@ export type GuardCtx = {
expression: ExpressionCstNode[];
};

export interface IdentifyNewExpressionTypeCstNode extends CstNode {
name: "identifyNewExpressionType";
children: IdentifyNewExpressionTypeCtx;
}

export type IdentifyNewExpressionTypeCtx = {
New: IToken[];
classOrInterfaceTypeToInstantiate: ClassOrInterfaceTypeToInstantiateCstNode[];
};

export interface IsLambdaExpressionCstNode extends CstNode {
name: "isLambdaExpression";
children: IsLambdaExpressionCtx;
}

export type IsLambdaExpressionCtx = {};

export interface IsCastExpressionCstNode extends CstNode {
name: "isCastExpression";
children: IsCastExpressionCtx;
}

export type IsCastExpressionCtx = {};

export interface IsPrimitiveCastExpressionCstNode extends CstNode {
name: "isPrimitiveCastExpression";
children: IsPrimitiveCastExpressionCtx;
}

export type IsPrimitiveCastExpressionCtx = {
LBrace: IToken[];
primitiveType: PrimitiveTypeCstNode[];
RBrace: IToken[];
};

export interface IsReferenceTypeCastExpressionCstNode extends CstNode {
name: "isReferenceTypeCastExpression";
children: IsReferenceTypeCastExpressionCtx;
}

export type IsReferenceTypeCastExpressionCtx = {
LBrace: IToken[];
referenceType: ReferenceTypeCstNode[];
additionalBound?: AdditionalBoundCstNode[];
RBrace: IToken[];
};

export interface IsRefTypeInMethodRefCstNode extends CstNode {
name: "isRefTypeInMethodRef";
children: IsRefTypeInMethodRefCtx;
Expand Down
1 change: 1 addition & 0 deletions packages/java-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"types": "./api.d.ts",
"dependencies": {
"chevrotain": "11.0.3",
"chevrotain-allstar": "0.3.1",
"lodash": "4.17.21"
},
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion packages/java-parser/src/parser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CstParser, isRecognitionException } from "chevrotain";
import { LLStarLookaheadStrategy } from "chevrotain-allstar";
import { allTokens, tokens as t } from "./tokens.js";
import * as lexicalStructure from "./productions/lexical-structure.js";
import * as typesValuesVariables from "./productions/types-values-and-variables.js";
Expand Down Expand Up @@ -38,7 +39,9 @@ import { shouldNotFormat } from "./comments.js";
export default class JavaParser extends CstParser {
constructor() {
super(allTokens, {
maxLookahead: 1,
lookaheadStrategy: new LLStarLookaheadStrategy({
logging: getSkipValidations() ? () => {} : undefined
}),
nodeLocationTracking: "full",
// traceInitPerf: 2,
skipValidations: getSkipValidations()
Expand Down
Loading

0 comments on commit 38afbfe

Please sign in to comment.