Skip to content

Commit

Permalink
feat: <python> add basic builder call size suport
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Feb 24, 2020
1 parent 904fa19 commit bd1a23b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>()

fun buildParameters(listCtx: PythonParser.TypedargslistContext?): Array<CodeProperty> {
Expand Down Expand Up @@ -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<CodeCall> = arrayOf()
val localVarName = exprCtx.atom().text
for (trailerContext in exprCtx.trailer()) {
val caller = trailerContext.text
funcCalls += CodeCall(
NodeName = localVarName,
FunctionName = caller
)
}

currentFunction.FunctionCalls = funcCalls
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

0 comments on commit bd1a23b

Please # to comment.