diff --git a/chapi-ast-c/src/main/antlr/C.g4 b/chapi-ast-c/src/main/antlr/C.g4 index 5381fb27..843c69c8 100644 --- a/chapi-ast-c/src/main/antlr/C.g4 +++ b/chapi-ast-c/src/main/antlr/C.g4 @@ -34,7 +34,7 @@ grammar C; compilationUnit - : includeDeclaration? externalDeclaration+ EOF + : includeDeclaration? (externalDeclaration+)? EOF ; includeDeclaration diff --git a/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CFullIdentListener.kt b/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CFullIdentListener.kt index f456595e..a71d8737 100644 --- a/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CFullIdentListener.kt +++ b/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CFullIdentListener.kt @@ -56,8 +56,25 @@ open class CFullIdentListener(fileName: String) : CAstBaseListener() { currentDataStruct = it } - ctx?.structDeclarationList()?.structDeclaration()?.map { - it.specifierQualifierList()?.let { qualifierList -> + ctx?.structDeclarationList()?.structDeclaration()?.forEach { structDeclCtx -> + /// for forward struct declaration + structDeclCtx.structDeclaratorList()?.let { + val type = structDeclCtx.specifierQualifierList()?.typeSpecifier()?.let { + val specifier = it.structOrUnionSpecifier() + specifier?.structOrUnion()?.text + " " + specifier?.Identifier()?.text + } + val value = structDeclCtx.specifierQualifierList()?.specifierQualifierList()?.text ?: "" + + val field = CodeField( + TypeType = type ?: "", + TypeValue = value + ) + + currentDataStruct.Fields += field + return@forEach + } + + structDeclCtx.specifierQualifierList()?.let { qualifierList -> val field = CodeField( TypeType = qualifierList.typeSpecifier().text, TypeValue = qualifierList.specifierQualifierList()?.text ?: "" diff --git a/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt b/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt index e8fe584a..d5d680d4 100644 --- a/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt +++ b/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt @@ -191,7 +191,7 @@ typedef struct { assertEquals(elementDs.Fields.size, 2) assertEquals(elementDs.Fields[0].TypeType, "int") assertEquals(elementDs.Fields[0].TypeValue, "value") -// assertEquals(elementDs.Fields[1].TypeType, "struct element*") + assertEquals(elementDs.Fields[1].TypeType, "struct element") assertEquals(elementDs.Fields[1].TypeValue, "") } }