From e53f3a47a7cc84952c3c8be856f6c0d1661b1e3a Mon Sep 17 00:00:00 2001 From: Max Schaefer Date: Tue, 21 Nov 2023 10:27:32 +0000 Subject: [PATCH] java-parser: New entrypoint `lexAndParse` to return both tokens and CST. --- packages/java-parser/api.d.ts | 8 ++++++++ packages/java-parser/src/index.js | 14 ++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/java-parser/api.d.ts b/packages/java-parser/api.d.ts index 81c68966..6dbfaf3f 100644 --- a/packages/java-parser/api.d.ts +++ b/packages/java-parser/api.d.ts @@ -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; diff --git a/packages/java-parser/src/index.js b/packages/java-parser/src/index.js index 698f3ad1..e672ac65 100644 --- a/packages/java-parser/src/index.js +++ b/packages/java-parser/src/index.js @@ -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); @@ -25,7 +25,8 @@ function parse(inputText, entryPoint = "compilationUnit") { ); } - parser.input = lexResult.tokens; + const tokens = lexResult.tokens; + parser.input = tokens; parser.mostEnclosiveCstNodeByStartOffset = {}; parser.mostEnclosiveCstNodeByEndOffset = {}; @@ -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