Skip to content

Commit

Permalink
fix: add lost code annotation for Java & Python
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 4, 2024
1 parent 1f9ea6b commit 67fa016
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ open class JavaAstListener : JavaParserBaseListener() {
}
}

codeAnnotation.Position = buildPosition(ctx)
return codeAnnotation
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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<AnnotationKeyValue> {
var arguments: List<AnnotationKeyValue> = listOf()
for (argCtx in arglistCtx!!.argument()) {
Expand Down Expand Up @@ -165,7 +176,8 @@ open class PythonAstBaseListener : PythonParserBaseListener() {

funcCalls += CodeCall(
NodeName = nodeName,
FunctionName = caller
FunctionName = caller,
Position = buildPosition(trailerContext)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
}

Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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}")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ open class TypeScriptAstListener : TypeScriptParserBaseListener() {
val member = callExpression.decoratorMemberExpression()
annotation.Name = member.Identifier().text
}

return annotation
}
}

0 comments on commit 67fa016

Please sign in to comment.