Skip to content

Commit e53f3a4

Browse files
Max Schaeferclementdessoude
authored andcommitted
java-parser: New entrypoint lexAndParse to return both tokens and CST.
1 parent ae03d4a commit e53f3a4

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

packages/java-parser/api.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ export declare type CstChildrenDictionary = {
3030
[identifier: string]: CstElement[];
3131
};
3232

33+
export function lexAndParse(
34+
text: string,
35+
startProduction?: string
36+
): {
37+
tokens: IToken[];
38+
cst: CstNode;
39+
};
40+
3341
export function parse(text: string, startProduction?: string): CstNode;
3442

3543
export const BaseJavaCstVisitor: JavaCstVisitorConstructor<any, any>;

packages/java-parser/src/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const BaseJavaCstVisitor = parser.getBaseCstVisitorConstructor();
99
const BaseJavaCstVisitorWithDefaults =
1010
parser.getBaseCstVisitorConstructorWithDefaults();
1111

12-
function parse(inputText, entryPoint = "compilationUnit") {
12+
function lexAndParse(inputText, entryPoint = "compilationUnit") {
1313
// Lex
1414
const lexResult = JavaLexer.tokenize(inputText);
1515

@@ -25,7 +25,8 @@ function parse(inputText, entryPoint = "compilationUnit") {
2525
);
2626
}
2727

28-
parser.input = lexResult.tokens;
28+
const tokens = lexResult.tokens;
29+
parser.input = tokens;
2930
parser.mostEnclosiveCstNodeByStartOffset = {};
3031
parser.mostEnclosiveCstNodeByEndOffset = {};
3132

@@ -51,16 +52,21 @@ function parse(inputText, entryPoint = "compilationUnit") {
5152
}
5253

5354
attachComments(
54-
lexResult.tokens,
55+
tokens,
5556
lexResult.groups.comments,
5657
parser.mostEnclosiveCstNodeByStartOffset,
5758
parser.mostEnclosiveCstNodeByEndOffset
5859
);
5960

60-
return cst;
61+
return { cst, tokens };
62+
}
63+
64+
function parse(inputText, entryPoint = "compilationUnit") {
65+
return lexAndParse(inputText, entryPoint).cst;
6166
}
6267

6368
module.exports = {
69+
lexAndParse,
6470
parse,
6571
BaseJavaCstVisitor,
6672
BaseJavaCstVisitorWithDefaults

0 commit comments

Comments
 (0)