Skip to content

Commit

Permalink
feat(cpp): add support for identifying C++ structures #24
Browse files Browse the repository at this point in the history
This commit adds support for identifying C++ structures in the `CPPBasicIdentListener` class. It includes a new test case that analyzes a C++ code snippet and verifies the identification of a structure. The identified structure is then added to the `CodeContainer` object. Additionally, the `CodeDataStruct` class is updated to include a new `DataStructType` enum value for C++ unions.
  • Loading branch information
phodal committed Jan 30, 2024
1 parent f0eed87 commit 15c8f0e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ class CPPBasicIdentListener(fileName: String) : CPP14ParserBaseListener() {
override fun enterClassSpecifier(ctx: CPP14Parser.ClassSpecifierContext?) {
ctx?.classHead()?.let {
currentNode = CodeDataStruct()

it.classKey()?.let { keyContext ->
currentNode?.Type = when (keyContext.text) {
"class" -> DataStructType.CLASS
"struct" -> DataStructType.STRUCT
"union" -> DataStructType.UNION
else -> DataStructType.CLASS
}
}

currentNode?.NodeName = it.classHeadName()?.className()?.text ?: ""

val extends = it.baseClause()?.baseSpecifierList()?.baseSpecifier()?.map { baseSpecifier ->
Expand Down Expand Up @@ -88,6 +98,8 @@ class CPPBasicIdentListener(fileName: String) : CPP14ParserBaseListener() {
}
}



fun getNodeInfo(): CodeContainer {
if (defaultNode.Functions.isNotEmpty()) {
codeContainer.DataStructures += defaultNode
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package chapi.ast.cppast

import chapi.domain.core.DataStructType
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

Expand Down Expand Up @@ -150,4 +151,20 @@ void display(char c, int n) {
assertEquals(container.DataStructures[0].Fields[1].TypeType, "int")
assertEquals(container.DataStructures[0].Fields[1].TypeKey, "No")
}

@Test
internal fun shouldIdentifyCppStructure() {
val code = """
struct Person
{
char name[50];
int age;
float salary;
};
""".trimIndent()

val container = CPPAnalyser().analysis(code, "helloworld.cpp")
assertEquals(container.DataStructures.size, 1)
assertEquals(container.DataStructures[0].Type, DataStructType.STRUCT)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ enum class DataStructType(val structType: String) {
INNER_STRUCTURES("InnerStructures"),
CREATOR_CLASS("CreatorClass"),
ABSTRACT_CLASS("AbstractClass"),
/// add 2.2.6 for cpp
UNION("Union"),

// for scala, Rust
TRAIT("Trait"),
Expand Down
7 changes: 4 additions & 3 deletions chapi-domain/src/main/kotlin/chapi/domain/core/CodeField.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package chapi.domain.core
import kotlinx.serialization.Serializable

/**
* Represents a field in a Java class.
*
* In Java, a field is defined as follows:
*
* for Java, it will be the field
* ```java
* private String helloText = "hello, world";
* // || || ||
* // <TypeType> <TypeKey> <TypeValue>
* ```
*
*/
@Serializable
data class CodeField(
Expand All @@ -19,7 +20,7 @@ data class CodeField(
var TypeKey: String = "",
var Annotations: List<CodeAnnotation> = listOf(),
var Modifiers: List<String> = listOf(),
// for TypeScript and JavaScript only, examples: `export default sample = createHello() `
/// for TypeScript and JavaScript only, examples: `export default sample = createHello() `
var Calls: List<CodeCall> = listOf(),
/// import 2.2.3 for Toml
@Since("2.2.3")
Expand Down

0 comments on commit 15c8f0e

Please # to comment.