Skip to content

Commit

Permalink
feat(thrift): enhance ThriftFullIdentListener with struct parsing and…
Browse files Browse the repository at this point in the history
… position tracking

The commit updates the ThriftFullIdentListener by adding the capability to parse Thrift struct definitions and tracking their positions within the source file. It includes the construction of a CodeDataStruct to hold the struct information and a CodeField for individual field details. Additionally, the buildPosition method's visibility has been adjusted for both Thrift and Protobuf listeners.
  • Loading branch information
phodal committed Oct 27, 2024
1 parent 4d63a51 commit f240aba
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ProtobufFullIdentListener(var fileName: String) : Protobuf3BaseListener()
codeContainer.PackageName = packageName
}

fun buildPosition(ctx: ParserRuleContext): CodePosition {
private fun buildPosition(ctx: ParserRuleContext): CodePosition {
val position = CodePosition()
position.StartLine = ctx.start.line
position.StartLinePosition = ctx.start.charPositionInLine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package chapi.ast.thrift

import chapi.ast.antlr.ThriftBaseListener
import chapi.ast.antlr.ThriftParser
import chapi.domain.core.CodeContainer
import chapi.domain.core.*
import org.antlr.v4.runtime.ParserRuleContext

class ThriftFullIdentListener(fileName: String) : ThriftBaseListener() {
private var codeContainer: CodeContainer = CodeContainer(FullName = fileName)
Expand All @@ -13,6 +14,47 @@ class ThriftFullIdentListener(fileName: String) : ThriftBaseListener() {
codeContainer.PackageName = namespace
}

private fun buildPosition(ctx: ParserRuleContext): CodePosition {
val position = CodePosition()
position.StartLine = ctx.start.line
position.StartLinePosition = ctx.start.charPositionInLine
position.StopLine = ctx.stop.line
position.StopLinePosition = ctx.stop.charPositionInLine
return position
}

override fun enterStruct_(ctx: ThriftParser.Struct_Context?) {
val codeDataStruct = constructStructDef(ctx)
codeContainer.DataStructures += codeDataStruct

}

private fun constructStructDef(ctx: ThriftParser.Struct_Context?): CodeDataStruct {
val codeDataStruct = CodeDataStruct(
NodeName = ctx!!.IDENTIFIER().text,
Module = codeContainer.PackageName,
Position = buildPosition(ctx),
FilePath = codeContainer.FullName,
Package = codeContainer.PackageName,
Type = DataStructType.STRUCT,
)

ctx.field().forEach {
codeDataStruct.Fields += constructField(it)
}

return codeDataStruct
}

private fun constructField(child: ThriftParser.FieldContext): CodeField {
return CodeField(
TypeType = child.field_type().text,
TypeKey = child.IDENTIFIER().text,
TypeValue = child.field_id()?.text ?: "",
Modifiers = listOf(child.field_req()?.text ?: ""),
)
}

fun getNodeInfo(): CodeContainer {
return codeContainer
}
Expand Down

0 comments on commit f240aba

Please sign in to comment.