Skip to content

Commit 6f587d7

Browse files
jtkieselclementdessoude
authored andcommitted
perf: leverage Chevrotain Allstar to reduce backtracking
1 parent 982a4c4 commit 6f587d7

17 files changed

+132
-1234
lines changed

packages/java-parser/api.d.ts

Lines changed: 0 additions & 256 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,7 @@ export abstract class JavaCstVisitor<IN, OUT> implements ICstVisitor<IN, OUT> {
170170
ctx: CompactConstructorDeclarationCtx,
171171
param?: IN
172172
): OUT;
173-
isClassDeclaration(ctx: IsClassDeclarationCtx, param?: IN): OUT;
174-
identifyClassBodyDeclarationType(
175-
ctx: IdentifyClassBodyDeclarationTypeCtx,
176-
param?: IN
177-
): OUT;
178173
isDims(ctx: IsDimsCtx, param?: IN): OUT;
179-
isCompactConstructorDeclaration(
180-
ctx: IsCompactConstructorDeclarationCtx,
181-
param?: IN
182-
): OUT;
183174
compilationUnit(ctx: CompilationUnitCtx, param?: IN): OUT;
184175
ordinaryCompilationUnit(ctx: OrdinaryCompilationUnitCtx, param?: IN): OUT;
185176
modularCompilationUnit(ctx: ModularCompilationUnitCtx, param?: IN): OUT;
@@ -195,7 +186,6 @@ export abstract class JavaCstVisitor<IN, OUT> implements ICstVisitor<IN, OUT> {
195186
usesModuleDirective(ctx: UsesModuleDirectiveCtx, param?: IN): OUT;
196187
providesModuleDirective(ctx: ProvidesModuleDirectiveCtx, param?: IN): OUT;
197188
requiresModifier(ctx: RequiresModifierCtx, param?: IN): OUT;
198-
isModuleCompilationUnit(ctx: IsModuleCompilationUnitCtx, param?: IN): OUT;
199189
interfaceDeclaration(ctx: InterfaceDeclarationCtx, param?: IN): OUT;
200190
normalInterfaceDeclaration(
201191
ctx: NormalInterfaceDeclarationCtx,
@@ -240,18 +230,6 @@ export abstract class JavaCstVisitor<IN, OUT> implements ICstVisitor<IN, OUT> {
240230
param?: IN
241231
): OUT;
242232
elementValueList(ctx: ElementValueListCtx, param?: IN): OUT;
243-
identifyInterfaceBodyDeclarationType(
244-
ctx: IdentifyInterfaceBodyDeclarationTypeCtx,
245-
param?: IN
246-
): OUT;
247-
identifyAnnotationBodyDeclarationType(
248-
ctx: IdentifyAnnotationBodyDeclarationTypeCtx,
249-
param?: IN
250-
): OUT;
251-
isSimpleElementValueAnnotation(
252-
ctx: IsSimpleElementValueAnnotationCtx,
253-
param?: IN
254-
): OUT;
255233
arrayInitializer(ctx: ArrayInitializerCtx, param?: IN): OUT;
256234
variableInitializerList(ctx: VariableInitializerListCtx, param?: IN): OUT;
257235
block(ctx: BlockCtx, param?: IN): OUT;
@@ -305,12 +283,6 @@ export abstract class JavaCstVisitor<IN, OUT> implements ICstVisitor<IN, OUT> {
305283
resource(ctx: ResourceCtx, param?: IN): OUT;
306284
yieldStatement(ctx: YieldStatementCtx, param?: IN): OUT;
307285
variableAccess(ctx: VariableAccessCtx, param?: IN): OUT;
308-
isBasicForStatement(ctx: IsBasicForStatementCtx, param?: IN): OUT;
309-
isLocalVariableDeclaration(
310-
ctx: IsLocalVariableDeclarationCtx,
311-
param?: IN
312-
): OUT;
313-
isClassicSwitchLabel(ctx: IsClassicSwitchLabelCtx, param?: IN): OUT;
314286
expression(ctx: ExpressionCtx, param?: IN): OUT;
315287
lambdaExpression(ctx: LambdaExpressionCtx, param?: IN): OUT;
316288
lambdaParameters(ctx: LambdaParametersCtx, param?: IN): OUT;
@@ -386,14 +358,6 @@ export abstract class JavaCstVisitor<IN, OUT> implements ICstVisitor<IN, OUT> {
386358
componentPattern(ctx: ComponentPatternCtx, param?: IN): OUT;
387359
unnamedPattern(ctx: UnnamedPatternCtx, param?: IN): OUT;
388360
guard(ctx: GuardCtx, param?: IN): OUT;
389-
identifyNewExpressionType(ctx: IdentifyNewExpressionTypeCtx, param?: IN): OUT;
390-
isLambdaExpression(ctx: IsLambdaExpressionCtx, param?: IN): OUT;
391-
isCastExpression(ctx: IsCastExpressionCtx, param?: IN): OUT;
392-
isPrimitiveCastExpression(ctx: IsPrimitiveCastExpressionCtx, param?: IN): OUT;
393-
isReferenceTypeCastExpression(
394-
ctx: IsReferenceTypeCastExpressionCtx,
395-
param?: IN
396-
): OUT;
397361
isRefTypeInMethodRef(ctx: IsRefTypeInMethodRefCtx, param?: IN): OUT;
398362
}
399363

@@ -527,16 +491,7 @@ export abstract class JavaCstVisitorWithDefaults<IN, OUT>
527491
ctx: CompactConstructorDeclarationCtx,
528492
param?: IN
529493
): OUT;
530-
isClassDeclaration(ctx: IsClassDeclarationCtx, param?: IN): OUT;
531-
identifyClassBodyDeclarationType(
532-
ctx: IdentifyClassBodyDeclarationTypeCtx,
533-
param?: IN
534-
): OUT;
535494
isDims(ctx: IsDimsCtx, param?: IN): OUT;
536-
isCompactConstructorDeclaration(
537-
ctx: IsCompactConstructorDeclarationCtx,
538-
param?: IN
539-
): OUT;
540495
compilationUnit(ctx: CompilationUnitCtx, param?: IN): OUT;
541496
ordinaryCompilationUnit(ctx: OrdinaryCompilationUnitCtx, param?: IN): OUT;
542497
modularCompilationUnit(ctx: ModularCompilationUnitCtx, param?: IN): OUT;
@@ -552,7 +507,6 @@ export abstract class JavaCstVisitorWithDefaults<IN, OUT>
552507
usesModuleDirective(ctx: UsesModuleDirectiveCtx, param?: IN): OUT;
553508
providesModuleDirective(ctx: ProvidesModuleDirectiveCtx, param?: IN): OUT;
554509
requiresModifier(ctx: RequiresModifierCtx, param?: IN): OUT;
555-
isModuleCompilationUnit(ctx: IsModuleCompilationUnitCtx, param?: IN): OUT;
556510
interfaceDeclaration(ctx: InterfaceDeclarationCtx, param?: IN): OUT;
557511
normalInterfaceDeclaration(
558512
ctx: NormalInterfaceDeclarationCtx,
@@ -597,18 +551,6 @@ export abstract class JavaCstVisitorWithDefaults<IN, OUT>
597551
param?: IN
598552
): OUT;
599553
elementValueList(ctx: ElementValueListCtx, param?: IN): OUT;
600-
identifyInterfaceBodyDeclarationType(
601-
ctx: IdentifyInterfaceBodyDeclarationTypeCtx,
602-
param?: IN
603-
): OUT;
604-
identifyAnnotationBodyDeclarationType(
605-
ctx: IdentifyAnnotationBodyDeclarationTypeCtx,
606-
param?: IN
607-
): OUT;
608-
isSimpleElementValueAnnotation(
609-
ctx: IsSimpleElementValueAnnotationCtx,
610-
param?: IN
611-
): OUT;
612554
arrayInitializer(ctx: ArrayInitializerCtx, param?: IN): OUT;
613555
variableInitializerList(ctx: VariableInitializerListCtx, param?: IN): OUT;
614556
block(ctx: BlockCtx, param?: IN): OUT;
@@ -662,12 +604,6 @@ export abstract class JavaCstVisitorWithDefaults<IN, OUT>
662604
resource(ctx: ResourceCtx, param?: IN): OUT;
663605
yieldStatement(ctx: YieldStatementCtx, param?: IN): OUT;
664606
variableAccess(ctx: VariableAccessCtx, param?: IN): OUT;
665-
isBasicForStatement(ctx: IsBasicForStatementCtx, param?: IN): OUT;
666-
isLocalVariableDeclaration(
667-
ctx: IsLocalVariableDeclarationCtx,
668-
param?: IN
669-
): OUT;
670-
isClassicSwitchLabel(ctx: IsClassicSwitchLabelCtx, param?: IN): OUT;
671607
expression(ctx: ExpressionCtx, param?: IN): OUT;
672608
lambdaExpression(ctx: LambdaExpressionCtx, param?: IN): OUT;
673609
lambdaParameters(ctx: LambdaParametersCtx, param?: IN): OUT;
@@ -743,14 +679,6 @@ export abstract class JavaCstVisitorWithDefaults<IN, OUT>
743679
componentPattern(ctx: ComponentPatternCtx, param?: IN): OUT;
744680
unnamedPattern(ctx: UnnamedPatternCtx, param?: IN): OUT;
745681
guard(ctx: GuardCtx, param?: IN): OUT;
746-
identifyNewExpressionType(ctx: IdentifyNewExpressionTypeCtx, param?: IN): OUT;
747-
isLambdaExpression(ctx: IsLambdaExpressionCtx, param?: IN): OUT;
748-
isCastExpression(ctx: IsCastExpressionCtx, param?: IN): OUT;
749-
isPrimitiveCastExpression(ctx: IsPrimitiveCastExpressionCtx, param?: IN): OUT;
750-
isReferenceTypeCastExpression(
751-
ctx: IsReferenceTypeCastExpressionCtx,
752-
param?: IN
753-
): OUT;
754682
isRefTypeInMethodRef(ctx: IsRefTypeInMethodRefCtx, param?: IN): OUT;
755683
}
756684

@@ -1836,39 +1764,6 @@ export type CompactConstructorDeclarationCtx = {
18361764
constructorBody: ConstructorBodyCstNode[];
18371765
};
18381766

1839-
export interface IsClassDeclarationCstNode extends CstNode {
1840-
name: "isClassDeclaration";
1841-
children: IsClassDeclarationCtx;
1842-
}
1843-
1844-
export type IsClassDeclarationCtx = {
1845-
Semicolon?: IToken[];
1846-
classModifier?: ClassModifierCstNode[];
1847-
};
1848-
1849-
export interface IdentifyClassBodyDeclarationTypeCstNode extends CstNode {
1850-
name: "identifyClassBodyDeclarationType";
1851-
children: IdentifyClassBodyDeclarationTypeCtx;
1852-
}
1853-
1854-
export type IdentifyClassBodyDeclarationTypeCtx = {
1855-
annotation?: AnnotationCstNode[];
1856-
Public?: IToken[];
1857-
Protected?: IToken[];
1858-
Private?: IToken[];
1859-
Abstract?: IToken[];
1860-
Static?: IToken[];
1861-
Final?: IToken[];
1862-
Transient?: IToken[];
1863-
Volatile?: IToken[];
1864-
Synchronized?: IToken[];
1865-
Native?: IToken[];
1866-
Sealed?: IToken[];
1867-
NonSealed?: IToken[];
1868-
Strictfp?: IToken[];
1869-
unannType: UnannTypeCstNode[];
1870-
};
1871-
18721767
export interface IsDimsCstNode extends CstNode {
18731768
name: "isDims";
18741769
children: IsDimsCtx;
@@ -1883,20 +1778,6 @@ export type IsDimsCtx = {
18831778
RBrace?: IToken[];
18841779
};
18851780

1886-
export interface IsCompactConstructorDeclarationCstNode extends CstNode {
1887-
name: "isCompactConstructorDeclaration";
1888-
children: IsCompactConstructorDeclarationCtx;
1889-
}
1890-
1891-
export type IsCompactConstructorDeclarationCtx = {
1892-
annotation?: AnnotationCstNode[];
1893-
Public?: IToken[];
1894-
Protected?: IToken[];
1895-
Private?: IToken[];
1896-
simpleTypeName: SimpleTypeNameCstNode[];
1897-
LCurly: IToken[];
1898-
};
1899-
19001781
export interface CompilationUnitCstNode extends CstNode {
19011782
name: "compilationUnit";
19021783
children: CompilationUnitCtx;
@@ -2090,17 +1971,6 @@ export type RequiresModifierCtx = {
20901971
Static?: IToken[];
20911972
};
20921973

2093-
export interface IsModuleCompilationUnitCstNode extends CstNode {
2094-
name: "isModuleCompilationUnit";
2095-
children: IsModuleCompilationUnitCtx;
2096-
}
2097-
2098-
export type IsModuleCompilationUnitCtx = {
2099-
packageDeclaration?: PackageDeclarationCstNode[];
2100-
importDeclaration?: ImportDeclarationCstNode[];
2101-
annotation?: AnnotationCstNode[];
2102-
};
2103-
21041974
export interface InterfaceDeclarationCstNode extends CstNode {
21051975
name: "interfaceDeclaration";
21061976
children: InterfaceDeclarationCtx;
@@ -2379,52 +2249,6 @@ export type ElementValueListCtx = {
23792249
Comma?: IToken[];
23802250
};
23812251

2382-
export interface IdentifyInterfaceBodyDeclarationTypeCstNode extends CstNode {
2383-
name: "identifyInterfaceBodyDeclarationType";
2384-
children: IdentifyInterfaceBodyDeclarationTypeCtx;
2385-
}
2386-
2387-
export type IdentifyInterfaceBodyDeclarationTypeCtx = {
2388-
annotation?: AnnotationCstNode[];
2389-
Public?: IToken[];
2390-
Protected?: IToken[];
2391-
Private?: IToken[];
2392-
Abstract?: IToken[];
2393-
Static?: IToken[];
2394-
Sealed?: IToken[];
2395-
NonSealed?: IToken[];
2396-
Strictfp?: IToken[];
2397-
Final?: IToken[];
2398-
Default?: IToken[];
2399-
unannType: UnannTypeCstNode[];
2400-
};
2401-
2402-
export interface IdentifyAnnotationBodyDeclarationTypeCstNode extends CstNode {
2403-
name: "identifyAnnotationBodyDeclarationType";
2404-
children: IdentifyAnnotationBodyDeclarationTypeCtx;
2405-
}
2406-
2407-
export type IdentifyAnnotationBodyDeclarationTypeCtx = {
2408-
annotation?: AnnotationCstNode[];
2409-
Public?: IToken[];
2410-
Protected?: IToken[];
2411-
Private?: IToken[];
2412-
Abstract?: IToken[];
2413-
Static?: IToken[];
2414-
Final?: IToken[];
2415-
Strictfp?: IToken[];
2416-
unannType: UnannTypeCstNode[];
2417-
};
2418-
2419-
export interface IsSimpleElementValueAnnotationCstNode extends CstNode {
2420-
name: "isSimpleElementValueAnnotation";
2421-
children: IsSimpleElementValueAnnotationCtx;
2422-
}
2423-
2424-
export type IsSimpleElementValueAnnotationCtx = {
2425-
annotation: AnnotationCstNode[];
2426-
};
2427-
24282252
export interface ArrayInitializerCstNode extends CstNode {
24292253
name: "arrayInitializer";
24302254
children: ArrayInitializerCtx;
@@ -2972,39 +2796,6 @@ export type VariableAccessCtx = {
29722796
primary: PrimaryCstNode[];
29732797
};
29742798

2975-
export interface IsBasicForStatementCstNode extends CstNode {
2976-
name: "isBasicForStatement";
2977-
children: IsBasicForStatementCtx;
2978-
}
2979-
2980-
export type IsBasicForStatementCtx = {
2981-
For: IToken[];
2982-
LBrace: IToken[];
2983-
forInit?: ForInitCstNode[];
2984-
Semicolon: IToken[];
2985-
};
2986-
2987-
export interface IsLocalVariableDeclarationCstNode extends CstNode {
2988-
name: "isLocalVariableDeclaration";
2989-
children: IsLocalVariableDeclarationCtx;
2990-
}
2991-
2992-
export type IsLocalVariableDeclarationCtx = {
2993-
variableModifier?: VariableModifierCstNode[];
2994-
localVariableType: LocalVariableTypeCstNode[];
2995-
variableDeclaratorId: VariableDeclaratorIdCstNode[];
2996-
};
2997-
2998-
export interface IsClassicSwitchLabelCstNode extends CstNode {
2999-
name: "isClassicSwitchLabel";
3000-
children: IsClassicSwitchLabelCtx;
3001-
}
3002-
3003-
export type IsClassicSwitchLabelCtx = {
3004-
switchLabel: SwitchLabelCstNode[];
3005-
Colon: IToken[];
3006-
};
3007-
30082799
export interface ExpressionCstNode extends CstNode {
30092800
name: "expression";
30102801
children: ExpressionCtx;
@@ -3543,53 +3334,6 @@ export type GuardCtx = {
35433334
expression: ExpressionCstNode[];
35443335
};
35453336

3546-
export interface IdentifyNewExpressionTypeCstNode extends CstNode {
3547-
name: "identifyNewExpressionType";
3548-
children: IdentifyNewExpressionTypeCtx;
3549-
}
3550-
3551-
export type IdentifyNewExpressionTypeCtx = {
3552-
New: IToken[];
3553-
classOrInterfaceTypeToInstantiate: ClassOrInterfaceTypeToInstantiateCstNode[];
3554-
};
3555-
3556-
export interface IsLambdaExpressionCstNode extends CstNode {
3557-
name: "isLambdaExpression";
3558-
children: IsLambdaExpressionCtx;
3559-
}
3560-
3561-
export type IsLambdaExpressionCtx = {};
3562-
3563-
export interface IsCastExpressionCstNode extends CstNode {
3564-
name: "isCastExpression";
3565-
children: IsCastExpressionCtx;
3566-
}
3567-
3568-
export type IsCastExpressionCtx = {};
3569-
3570-
export interface IsPrimitiveCastExpressionCstNode extends CstNode {
3571-
name: "isPrimitiveCastExpression";
3572-
children: IsPrimitiveCastExpressionCtx;
3573-
}
3574-
3575-
export type IsPrimitiveCastExpressionCtx = {
3576-
LBrace: IToken[];
3577-
primitiveType: PrimitiveTypeCstNode[];
3578-
RBrace: IToken[];
3579-
};
3580-
3581-
export interface IsReferenceTypeCastExpressionCstNode extends CstNode {
3582-
name: "isReferenceTypeCastExpression";
3583-
children: IsReferenceTypeCastExpressionCtx;
3584-
}
3585-
3586-
export type IsReferenceTypeCastExpressionCtx = {
3587-
LBrace: IToken[];
3588-
referenceType: ReferenceTypeCstNode[];
3589-
additionalBound?: AdditionalBoundCstNode[];
3590-
RBrace: IToken[];
3591-
};
3592-
35933337
export interface IsRefTypeInMethodRefCstNode extends CstNode {
35943338
name: "isRefTypeInMethodRef";
35953339
children: IsRefTypeInMethodRefCtx;

packages/java-parser/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"types": "./api.d.ts",
1010
"dependencies": {
1111
"chevrotain": "11.0.3",
12+
"chevrotain-allstar": "0.3.1",
1213
"lodash": "4.17.21"
1314
},
1415
"scripts": {

packages/java-parser/src/parser.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CstParser, isRecognitionException } from "chevrotain";
2+
import { LLStarLookaheadStrategy } from "chevrotain-allstar";
23
import { allTokens, tokens as t } from "./tokens.js";
34
import * as lexicalStructure from "./productions/lexical-structure.js";
45
import * as typesValuesVariables from "./productions/types-values-and-variables.js";
@@ -38,7 +39,9 @@ import { shouldNotFormat } from "./comments.js";
3839
export default class JavaParser extends CstParser {
3940
constructor() {
4041
super(allTokens, {
41-
maxLookahead: 1,
42+
lookaheadStrategy: new LLStarLookaheadStrategy({
43+
logging: getSkipValidations() ? () => {} : undefined
44+
}),
4245
nodeLocationTracking: "full",
4346
// traceInitPerf: 2,
4447
skipValidations: getSkipValidations()

0 commit comments

Comments
 (0)