From 67fa0167d248d7830a2bc55bba6a226f963d7879 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Thu, 4 Jan 2024 21:03:34 +0800 Subject: [PATCH] fix: add lost code annotation for Java & Python --- .../chapi/ast/javaast/JavaAstListener.kt | 1 + .../ast/pythonast/PythonAstBaseListener.kt | 14 +++++++- .../ast/pythonast/PythonFullIdentListener.kt | 35 +++++++++---------- .../typescriptast/TypeScriptAstListener.kt | 1 + 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/chapi-ast-java/src/main/kotlin/chapi/ast/javaast/JavaAstListener.kt b/chapi-ast-java/src/main/kotlin/chapi/ast/javaast/JavaAstListener.kt index 112c4191..c6283ba9 100644 --- a/chapi-ast-java/src/main/kotlin/chapi/ast/javaast/JavaAstListener.kt +++ b/chapi-ast-java/src/main/kotlin/chapi/ast/javaast/JavaAstListener.kt @@ -32,6 +32,7 @@ open class JavaAstListener : JavaParserBaseListener() { } } + codeAnnotation.Position = buildPosition(ctx) return codeAnnotation } diff --git a/chapi-ast-python/src/main/kotlin/chapi/ast/pythonast/PythonAstBaseListener.kt b/chapi-ast-python/src/main/kotlin/chapi/ast/pythonast/PythonAstBaseListener.kt index 683cbfcd..1998522d 100644 --- a/chapi-ast-python/src/main/kotlin/chapi/ast/pythonast/PythonAstBaseListener.kt +++ b/chapi-ast-python/src/main/kotlin/chapi/ast/pythonast/PythonAstBaseListener.kt @@ -3,6 +3,7 @@ package chapi.ast.pythonast import chapi.ast.antlr.PythonParser import chapi.ast.antlr.PythonParserBaseListener import chapi.domain.core.* +import org.antlr.v4.runtime.ParserRuleContext import org.antlr.v4.runtime.tree.ParseTree open class PythonAstBaseListener : PythonParserBaseListener() { @@ -67,9 +68,19 @@ open class PythonAstBaseListener : PythonParserBaseListener() { codeAnnotation.KeyValues = this.buildArgList(node.arglist()) } + codeAnnotation.Position = buildPosition(node) return codeAnnotation } + fun buildPosition(ctx: ParserRuleContext): CodePosition { + return CodePosition( + StartLine = ctx.start.line, + StartLinePosition = ctx.start.charPositionInLine, + StopLine = ctx.stop.line, + StopLinePosition = ctx.stop.charPositionInLine + ) + } + private fun buildArgList(arglistCtx: PythonParser.ArglistContext?): List { var arguments: List = listOf() for (argCtx in arglistCtx!!.argument()) { @@ -165,7 +176,8 @@ open class PythonAstBaseListener : PythonParserBaseListener() { funcCalls += CodeCall( NodeName = nodeName, - FunctionName = caller + FunctionName = caller, + Position = buildPosition(trailerContext) ) } diff --git a/chapi-ast-python/src/main/kotlin/chapi/ast/pythonast/PythonFullIdentListener.kt b/chapi-ast-python/src/main/kotlin/chapi/ast/pythonast/PythonFullIdentListener.kt index df950589..6d885b63 100644 --- a/chapi-ast-python/src/main/kotlin/chapi/ast/pythonast/PythonFullIdentListener.kt +++ b/chapi-ast-python/src/main/kotlin/chapi/ast/pythonast/PythonFullIdentListener.kt @@ -14,9 +14,7 @@ class PythonFullIdentListener(var fileName: String) : PythonAstBaseListener() { val dotNames = ctx!!.dotted_as_names().dotted_as_name() val firstNameCtx = dotNames[0] - var codeImport = CodeImport( - Source = firstNameCtx.dotted_name().text - ) + val codeImport = CodeImport(Source = firstNameCtx.dotted_name().text) if (firstNameCtx.name() != null) { codeImport.UsageName += firstNameCtx.name().text } @@ -30,25 +28,23 @@ class PythonFullIdentListener(var fileName: String) : PythonAstBaseListener() { override fun enterFrom_stmt(ctx: PythonParser.From_stmtContext?) { var sourceName = "" - if (ctx!!.dotted_name() != null) { + if (ctx?.dotted_name() != null) { if (ctx.import_dot_ellipsis().size > 0) { sourceName = ctx.getChild(1).text } sourceName += ctx.dotted_name().text } else { - sourceName = ctx.getChild(1).text + sourceName = ctx?.getChild(1)?.text ?: "" } - val codeImport = CodeImport( - Source = sourceName - ) + val codeImport = CodeImport(Source = sourceName) - for (importAsNameCtx in ctx.import_as_names().import_as_name()) { - val usageName = importAsNameCtx.name()[0].text + ctx?.import_as_names()?.import_as_name()?.forEach { importAsNamecontext -> + val usageName = importAsNamecontext.name()[0].text codeImport.UsageName += usageName - if (importAsNameCtx.AS() != null) { - codeImport.AsName = importAsNameCtx.name()[1].text + importAsNamecontext.AS()?.let { + codeImport.AsName = importAsNamecontext.name()[1].text } } @@ -60,7 +56,8 @@ class PythonFullIdentListener(var fileName: String) : PythonAstBaseListener() { currentNode = CodeDataStruct( NodeName = ctx!!.name().text, Type = DataStructType.CLASS, - FilePath = codeContainer.FullName + FilePath = codeContainer.FullName, + Position = buildPosition(ctx) ) if (ctx.arglist() != null) { @@ -84,7 +81,8 @@ class PythonFullIdentListener(var fileName: String) : PythonAstBaseListener() { override fun enterFuncdef(ctx: PythonParser.FuncdefContext?) { val funcName = ctx!!.name().text currentFunction = CodeFunction( - Name = funcName + Name = funcName, + Position = buildPosition(ctx) ) if (ctx.ASYNC() != null) { @@ -113,13 +111,12 @@ class PythonFullIdentListener(var fileName: String) : PythonAstBaseListener() { override fun enterSimple_stmt(ctx: PythonParser.Simple_stmtContext?) { for (smallStmtCtx in ctx!!.small_stmt()) { - val stmtType = smallStmtCtx::class.java.simpleName - when (stmtType) { - "Expr_stmtContext" -> { - this.buildExprStmt(smallStmtCtx as PythonParser.Expr_stmtContext) + when(smallStmtCtx) { + is PythonParser.Expr_stmtContext -> { + this.buildExprStmt(smallStmtCtx) } else -> { - println("enterSimple_stmt ->$stmtType") + println("enterSimple_stmt ->${smallStmtCtx::class.java.simpleName}") } } } diff --git a/chapi-ast-typescript/src/main/kotlin/chapi/ast/typescriptast/TypeScriptAstListener.kt b/chapi-ast-typescript/src/main/kotlin/chapi/ast/typescriptast/TypeScriptAstListener.kt index 25b03b1d..949d7988 100644 --- a/chapi-ast-typescript/src/main/kotlin/chapi/ast/typescriptast/TypeScriptAstListener.kt +++ b/chapi-ast-typescript/src/main/kotlin/chapi/ast/typescriptast/TypeScriptAstListener.kt @@ -144,6 +144,7 @@ open class TypeScriptAstListener : TypeScriptParserBaseListener() { val member = callExpression.decoratorMemberExpression() annotation.Name = member.Identifier().text } + return annotation } }