Skip to content

Commit

Permalink
java-parser: New entrypoint lexAndParse to return both tokens and CST.
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Schaefer authored and clementdessoude committed Nov 26, 2023
1 parent ae03d4a commit e53f3a4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 8 additions & 0 deletions packages/java-parser/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ export declare type CstChildrenDictionary = {
[identifier: string]: CstElement[];
};

export function lexAndParse(
text: string,
startProduction?: string
): {
tokens: IToken[];
cst: CstNode;
};

export function parse(text: string, startProduction?: string): CstNode;

export const BaseJavaCstVisitor: JavaCstVisitorConstructor<any, any>;
Expand Down
14 changes: 10 additions & 4 deletions packages/java-parser/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const BaseJavaCstVisitor = parser.getBaseCstVisitorConstructor();
const BaseJavaCstVisitorWithDefaults =
parser.getBaseCstVisitorConstructorWithDefaults();

function parse(inputText, entryPoint = "compilationUnit") {
function lexAndParse(inputText, entryPoint = "compilationUnit") {
// Lex
const lexResult = JavaLexer.tokenize(inputText);

Expand All @@ -25,7 +25,8 @@ function parse(inputText, entryPoint = "compilationUnit") {
);
}

parser.input = lexResult.tokens;
const tokens = lexResult.tokens;
parser.input = tokens;
parser.mostEnclosiveCstNodeByStartOffset = {};
parser.mostEnclosiveCstNodeByEndOffset = {};

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

attachComments(
lexResult.tokens,
tokens,
lexResult.groups.comments,
parser.mostEnclosiveCstNodeByStartOffset,
parser.mostEnclosiveCstNodeByEndOffset
);

return cst;
return { cst, tokens };
}

function parse(inputText, entryPoint = "compilationUnit") {
return lexAndParse(inputText, entryPoint).cst;
}

module.exports = {
lexAndParse,
parse,
BaseJavaCstVisitor,
BaseJavaCstVisitorWithDefaults
Expand Down

0 comments on commit e53f3a4

Please sign in to comment.