Skip to content

Commit

Permalink
refactor(c): fix parse syntax issue #24
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 6, 2024
1 parent fc84931 commit bef3dc9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion chapi-ast-c/src/main/antlr/C.g4
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
grammar C;

compilationUnit
: includeDeclaration? externalDeclaration+ EOF
: includeDeclaration? (externalDeclaration+)? EOF
;

includeDeclaration
Expand Down
21 changes: 19 additions & 2 deletions chapi-ast-c/src/main/kotlin/chapi/ast/cast/CFullIdentListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?: ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
}
}

0 comments on commit bef3dc9

Please sign in to comment.