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 cef11f73..5f3f9d24 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 @@ -2,12 +2,11 @@ package chapi.ast.pythonast import chapi.ast.antlr.PythonParser import chapi.ast.antlr.PythonParserBaseListener -import chapi.domain.core.AnnotationKeyValue -import chapi.domain.core.CodeAnnotation -import chapi.domain.core.CodeProperty +import chapi.domain.core.* import org.antlr.v4.runtime.tree.ParseTree open class PythonAstBaseListener : PythonParserBaseListener() { + var currentFunction: CodeFunction = CodeFunction() private var localVars = mutableMapOf() fun buildParameters(listCtx: PythonParser.TypedargslistContext?): Array { @@ -110,7 +109,32 @@ open class PythonAstBaseListener : PythonParserBaseListener() { } private fun buildTestContext(testContext: PythonParser.TestContext) { -// println(testContext.getChild(0).getChild(0)::class.java.simpleName) -// println(testContext.getChild(0).text) + val exprCtx = testContext.getChild(0).getChild(0).getChild(0) + val expr = exprCtx::class.java.simpleName + when(expr) { + "ExprContext" -> { + val exprCtx = exprCtx as PythonParser.ExprContext + val exprChild = exprCtx.getChild(0)::class.java.simpleName + when (exprChild) { + "AtomContext" -> { + buildAtomExpr(exprCtx) + } + } + } + } + } + + private fun buildAtomExpr(exprCtx: PythonParser.ExprContext) { + var funcCalls : Array = arrayOf() + val localVarName = exprCtx.atom().text + for (trailerContext in exprCtx.trailer()) { + val caller = trailerContext.text + funcCalls += CodeCall( + NodeName = localVarName, + FunctionName = caller + ) + } + + currentFunction.FunctionCalls = funcCalls } } 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 b40351d2..8f03197b 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 @@ -4,7 +4,6 @@ import chapi.ast.antlr.PythonParser import chapi.domain.core.* class PythonFullIdentListener(var fileName: String) : PythonAstBaseListener() { - private var currentFunction: CodeFunction = CodeFunction() private var hasEnterClass = false private var codeContainer: CodeContainer = CodeContainer(FullName = fileName) diff --git a/chapi-ast-python/src/test/kotlin/chapi/ast/pythonast/PythonAnalyserTest.kt b/chapi-ast-python/src/test/kotlin/chapi/ast/pythonast/PythonAnalyserTest.kt index 6a541caf..c8b1cc37 100644 --- a/chapi-ast-python/src/test/kotlin/chapi/ast/pythonast/PythonAnalyserTest.kt +++ b/chapi-ast-python/src/test/kotlin/chapi/ast/pythonast/PythonAnalyserTest.kt @@ -1,16 +1,21 @@ package chapi.ast.pythonast import org.junit.jupiter.api.Test +import kotlin.test.assertEquals internal class PythonAnalyserTest { @Test - internal fun shouldIdentBuilderPattern() { + internal fun shouldIdentBuilderPatternSize() { val code = """ def build(): p = Person() p.setName("Hunter").setAge(24).setSSN("111-22-3333") """ val codeFile = PythonAnalyser().analysis(code, "") - println(codeFile) + val firstFunc = codeFile.DataStructures[0].Functions[0] + assertEquals(firstFunc.FunctionCalls.size, 3) +// assertEquals(firstFunc.FunctionCalls[0].FunctionName, "setName") +// assertEquals(firstFunc.FunctionCalls[1].FunctionName, "setAge") +// assertEquals(firstFunc.FunctionCalls[2].FunctionName, "setSSN") } }