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 12, 2023
1 parent 7ebb6a0 commit 1c82352
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 1,235 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 @@ -162,16 +162,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 @@ -187,7 +178,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 @@ -232,18 +222,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 @@ -300,12 +278,6 @@ export abstract class JavaCstVisitor<IN, OUT> implements ICstVisitor<IN, OUT> {
resourceInit(ctx: ResourceInitCtx, 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 @@ -379,14 +351,6 @@ export abstract class JavaCstVisitor<IN, OUT> implements ICstVisitor<IN, OUT> {
recordPattern(ctx: RecordPatternCtx, param?: IN): OUT;
patternList(ctx: PatternListCtx, 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 @@ -520,16 +484,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 @@ -545,7 +500,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 @@ -590,18 +544,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 @@ -658,12 +600,6 @@ export abstract class JavaCstVisitorWithDefaults<IN, OUT>
resourceInit(ctx: ResourceInitCtx, 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 @@ -737,14 +673,6 @@ export abstract class JavaCstVisitorWithDefaults<IN, OUT>
recordPattern(ctx: RecordPatternCtx, param?: IN): OUT;
patternList(ctx: PatternListCtx, 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 @@ -1829,39 +1757,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 @@ -1876,20 +1771,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 @@ -2083,17 +1964,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 @@ -2372,52 +2242,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 @@ -2998,39 +2822,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 @@ -3549,53 +3340,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 1c82352

Please sign in to comment.