Skip to content

Commit

Permalink
refactor: update for antlr parameters syntax #24
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 6, 2024
1 parent 853ef64 commit 28f11f5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 44 deletions.
6 changes: 2 additions & 4 deletions chapi-ast-c/src/main/antlr/C.g4
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,11 @@ typeQualifierList
;

parameterTypeList
: parameterList
| parameterList ',' '...'
: parameterList (',' '...')?
;

parameterList
: parameterDeclaration
| parameterList ',' parameterDeclaration
: parameterDeclaration (',' parameterDeclaration)*
;

parameterDeclaration
Expand Down
11 changes: 2 additions & 9 deletions chapi-ast-c/src/main/kotlin/chapi/ast/cast/CAstBaseListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,17 @@ import chapi.ast.antlr.CParser
import chapi.domain.core.*
import org.antlr.v4.runtime.ParserRuleContext

open class CAstBaseListener: CBaseListener() {
open class CAstBaseListener : CBaseListener() {
fun buildPosition(ctx: ParserRuleContext?): CodePosition {
if (ctx == null) {
return CodePosition()
}

return CodePosition(
StartLine = ctx.start.line,
StartLinePosition= ctx.start.charPositionInLine,
StartLinePosition = ctx.start.charPositionInLine,
StopLine = ctx.stop.line,
StopLinePosition = ctx.stop.charPositionInLine
)
}

fun buildParameters(ctx: CParser.ParameterListContext, parameters: MutableList<CParser.ParameterDeclarationContext>) {
if (ctx.parameterList() != null) {
buildParameters(ctx.parameterList(), parameters)
}
parameters.add(ctx.parameterDeclaration())
}
}
57 changes: 26 additions & 31 deletions chapi-ast-c/src/main/kotlin/chapi/ast/cast/CFullIdentListener.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package chapi.ast.cast

import chapi.ast.antlr.CBaseListener
import chapi.ast.antlr.CParser
import chapi.domain.core.*

Expand Down Expand Up @@ -74,15 +73,10 @@ open class CFullIdentListener(fileName: String) : CAstBaseListener() {
}
}

private fun handleParamDirectDeclCtx(ctx: CParser.DirectDeclaratorContext) {
currentFunction.Parameters = listOf()
val directDeclarator = ctx as CParser.ParameterDirectDeclaratorContext
private fun handleParamDirectDeclCtx(ctx: CParser.ParameterDirectDeclaratorContext) {
parseDirectDeclarator(ctx.directDeclarator())

val parameterTypeList = directDeclarator.parameterTypeList().parameterList()
val parameters: MutableList<CParser.ParameterDeclarationContext> = ArrayList()
this.buildParameters(parameterTypeList, parameters)

val parameters = ctx.parameterTypeList().parameterList().parameterDeclaration()
for (parameter in parameters) {
var type: String? = null
var name: String? = null
Expand All @@ -95,6 +89,7 @@ open class CFullIdentListener(fileName: String) : CAstBaseListener() {
if (declarator.Identifier().text != null) {
name = declarator.Identifier().text
}

if (type != null && name != null) {
val codeParameter = CodeProperty(
TypeValue = name,
Expand All @@ -105,18 +100,16 @@ open class CFullIdentListener(fileName: String) : CAstBaseListener() {
}
}

private fun handleFuncPointerDirectDeclCtx(ctx: CParser.DirectDeclaratorContext) {
currentFunction.Parameters = listOf()
var name: String? = null
var type: String? = null
val directDeclarator = ctx as CParser.FunctionPointerDirectDeclaratorContext
if (directDeclarator.typeSpecifier() != null) {
type = directDeclarator.typeSpecifier().text
}
val directDeclaratorType = directDeclarator.directDeclarator()::class.java.simpleName
if (directDeclaratorType == "IdentifierDirectDeclaratorContext") {
name = (directDeclarator.directDeclarator() as CParser.IdentifierDirectDeclaratorContext).Identifier().text
private fun handleFuncPointerDirectDeclCtx(ctx: CParser.FunctionPointerDirectDeclaratorContext) {
val type = ctx.typeSpecifier()?.text
val name = when (val directDeclaratorType = ctx.directDeclarator()) {
is CParser.IdentifierDirectDeclaratorContext -> {
directDeclaratorType.Identifier().text
}

else -> null
}

if (name != null && type != null) {
currentFunction.Parameters += CodeProperty(
TypeValue = name,
Expand All @@ -139,19 +132,21 @@ open class CFullIdentListener(fileName: String) : CAstBaseListener() {
}

parseDirectDeclarator(ctx?.declarator()?.directDeclarator())
if (currentFunction.Parameters.isNotEmpty()) {
val firstParameter = currentFunction.Parameters[0]
if (firstParameter.TypeType.endsWith('*')) {
val pointerIndex = firstParameter.TypeType.length - 1
val baseType = firstParameter.TypeType.substring(0, pointerIndex)
structMap.getOrPut(baseType) {
CodeDataStruct(NodeName = baseType)
}.let {
it.Functions += currentFunction
}
} else {
defaultDataStruct.Functions += currentFunction

if (currentFunction.Parameters.isEmpty()) return

// handle for pointer
val firstParameter = currentFunction.Parameters[0]
if (firstParameter.TypeType.endsWith('*')) {
val pointerIndex = firstParameter.TypeType.length - 1
val baseType = firstParameter.TypeType.substring(0, pointerIndex)
structMap.getOrPut(baseType) {
CodeDataStruct(NodeName = baseType)
}.let {
it.Functions += currentFunction
}
} else {
defaultDataStruct.Functions += currentFunction
}
}

Expand Down

0 comments on commit 28f11f5

Please sign in to comment.