From 349c6e97fbbe39fe80a2dc73aba62dc6b3bdec1e Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Tue, 11 Feb 2020 15:33:41 +0800 Subject: [PATCH] feat: add basic func paramters support --- .../chapi/ast/pythonast/PythonAstBaseListener.kt | 14 +++++++++++++- .../chapi/ast/pythonast/PythonFullIdentListener.kt | 5 +++++ .../ast/pythonast/PythonFullIdentListenerTest.kt | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) 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 1f3d52b9..56f00b49 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 @@ -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 { + var parameters: Array = arrayOf() + for (defParameter in ctx.typedargslist().def_parameters()) { + val parameter = CodeProperty( + TypeType = "", + TypeValue = defParameter.text + ) + parameters += parameter + } + return parameters + } } 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 b0dd48f0..18e32e72 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,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() @@ -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?) { diff --git a/chapi-ast-python/src/test/kotlin/chapi/ast/pythonast/PythonFullIdentListenerTest.kt b/chapi-ast-python/src/test/kotlin/chapi/ast/pythonast/PythonFullIdentListenerTest.kt index 75f138be..e57bfc92 100644 --- a/chapi-ast-python/src/test/kotlin/chapi/ast/pythonast/PythonFullIdentListenerTest.kt +++ b/chapi-ast-python/src/test/kotlin/chapi/ast/pythonast/PythonFullIdentListenerTest.kt @@ -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") } }