Skip to content

Commit

Permalink
feat: <python> add basic func paramters support
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Feb 11, 2020
1 parent c5b420c commit 349c6e9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package chapi.ast.pythonast

import chapi.ast.antlr.PythonParser
import chapi.ast.antlr.PythonParserBaseListener
import domain.core.CodeProperty

open class PythonAstBaseListener : PythonParserBaseListener() {

fun buildParameters(ctx: PythonParser.FuncdefContext): Array<CodeProperty> {
var parameters: Array<CodeProperty> = arrayOf()
for (defParameter in ctx.typedargslist().def_parameters()) {
val parameter = CodeProperty(
TypeType = "",
TypeValue = defParameter.text
)
parameters += parameter
}
return parameters
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import chapi.ast.antlr.PythonParser
import domain.core.CodeDataStruct
import domain.core.CodeFile
import domain.core.CodeFunction
import domain.core.CodeProperty

class PythonFullIdentListener(var fileName: String) : PythonAstBaseListener() {
private var currentFunction: CodeFunction = CodeFunction()
Expand Down Expand Up @@ -43,6 +44,10 @@ class PythonFullIdentListener(var fileName: String) : PythonAstBaseListener() {
if (ctx.ASYNC() != null) {
currentFunction.Modifiers += ctx.ASYNC().text
}

if (ctx.typedargslist() != null) {
currentFunction.Parameters = this.buildParameters(ctx)
}
}

override fun exitFuncdef(ctx: PythonParser.FuncdefContext?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ async def show(str):
val codeFile = PythonAnalyser().analysis(code, "")
assertEquals(codeFile.DataStructures[0].NodeName, "default")
assertEquals(codeFile.DataStructures[0].Functions[0].Name, "show")
}

@Test
internal fun shouldIdentFuncParameters() {
val code = """
async def show(str):
print(str)
"""

val codeFile = PythonAnalyser().analysis(code, "")
assertEquals(codeFile.DataStructures[0].NodeName, "default")
assertEquals(codeFile.DataStructures[0].Functions[0].Modifiers[0], "async")
assertEquals(codeFile.DataStructures[0].Functions[0].Parameters.size, 1)
assertEquals(codeFile.DataStructures[0].Functions[0].Parameters[0].TypeValue, "str")
}
}

0 comments on commit 349c6e9

Please # to comment.