diff --git a/chapi-ast-cpp/src/main/kotlin/chapi/ast/cppast/CPPFullIdentListener.kt b/chapi-ast-cpp/src/main/kotlin/chapi/ast/cppast/CPPFullIdentListener.kt index 314e96d5..086e4fdd 100644 --- a/chapi-ast-cpp/src/main/kotlin/chapi/ast/cppast/CPPFullIdentListener.kt +++ b/chapi-ast-cpp/src/main/kotlin/chapi/ast/cppast/CPPFullIdentListener.kt @@ -1,12 +1,41 @@ package chapi.ast.cppast import chapi.ast.antlr.CPPBaseListener +import chapi.ast.antlr.CPPParser import chapi.domain.core.CodeContainer +import chapi.domain.core.CodeDataStruct +import chapi.domain.core.CodeFunction class CPPFullIdentListener(fileName: String) : CPPBaseListener() { private var codeContainer: CodeContainer = CodeContainer(FullName = fileName) + private var defaultNode = CodeDataStruct() + + override fun enterFunctiondefinition(ctx: CPPParser.FunctiondefinitionContext?) { + val context = ctx!! + val firstPtrDecl = context.declarator().ptrdeclarator() + if (firstPtrDecl != null) { + if (firstPtrDecl.noptrdeclarator() != null) { + tryFunctionBuild(firstPtrDecl) + } + } + } + + private fun tryFunctionBuild(firstPtrDecl: CPPParser.PtrdeclaratorContext) { + val parametersandqualifiers = firstPtrDecl.noptrdeclarator().parametersandqualifiers() + if (parametersandqualifiers != null) { + val codeFunction = CodeFunction() + + val functionName = firstPtrDecl.noptrdeclarator().noptrdeclarator().text + codeFunction.Name = functionName + defaultNode.Functions += codeFunction + } + } fun getNodeInfo(): CodeContainer { + if (defaultNode.Functions.isNotEmpty()) { + codeContainer.DataStructures += defaultNode + } + return codeContainer } } diff --git a/chapi-ast-cpp/src/test/kotlin/chapi/ast/cppast/CPPFullIdentListenerTest.kt b/chapi-ast-cpp/src/test/kotlin/chapi/ast/cppast/CPPFullIdentListenerTest.kt index 9930e355..7a7a1259 100644 --- a/chapi-ast-cpp/src/test/kotlin/chapi/ast/cppast/CPPFullIdentListenerTest.kt +++ b/chapi-ast-cpp/src/test/kotlin/chapi/ast/cppast/CPPFullIdentListenerTest.kt @@ -6,18 +6,17 @@ import kotlin.test.assertEquals internal class CPPFullIdentListenerTest { @Test internal fun shouldAnalysisHelloWorld() { -val code = """ -#include -using namespace std; -int main() { - cout << "Hello, World!"; - return 0; -} -""" - - val fileName = "helloworld.cpp" - val container = CPPAnalyser().analysis(code, fileName) - + val code = this::class.java.getResource("/grammar/helloworld.cpp").readText() + val container = CPPAnalyser().analysis(code, "helloworld.cpp") assertEquals(container.FullName, "helloworld.cpp") } + + @Test + internal fun shouldIdentFunctionName() { + val code = this::class.java.getResource("/grammar/helloworld.cpp").readText() + val container = CPPAnalyser().analysis(code, "helloworld.cpp") + assertEquals(container.DataStructures.size, 1) + assertEquals(container.DataStructures[0].Functions.size, 1) + assertEquals(container.DataStructures[0].Functions[0].Name, "main") + } }