Skip to content

Commit

Permalink
feat: <CPP> add basic function name support
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Feb 19, 2020
1 parent 13dcdd2 commit ef643c5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ import kotlin.test.assertEquals
internal class CPPFullIdentListenerTest {
@Test
internal fun shouldAnalysisHelloWorld() {
val code = """
#include <iostream>
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")
}
}

0 comments on commit ef643c5

Please # to comment.