Skip to content

Commit

Permalink
refactor: update csharp code sample
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 6, 2024
1 parent 5db890b commit 8097334
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
package chapi.ast.csharpast

import chapi.ast.antlr.CSharpParser
import chapi.ast.antlr.CSharpParser.Class_typeContext
import chapi.ast.antlr.CSharpParser.Primary_expressionContext
import chapi.ast.antlr.CSharpParser.Type_declarationContext
import chapi.domain.core.*
import chapi.infra.Stack
import org.antlr.v4.runtime.ParserRuleContext
import org.antlr.v4.runtime.tree.TerminalNodeImpl

class CSharpFullIdentListener(fileName: String) : CSharpAstListener(fileName) {

private fun handleClassMember(memberCtx: CSharpParser.Class_member_declarationContext?) {
val memberDeclaration = memberCtx!!.common_member_declaration() ?: return
val firstChild = memberDeclaration.getChild(0) ?: return

var returnType = "";
when (firstChild::class.java.simpleName) {
"TerminalNodeImpl" -> {
when (firstChild) {
is TerminalNodeImpl -> {
returnType = firstChild.text
}
}
Expand Down Expand Up @@ -123,28 +119,31 @@ class CSharpFullIdentListener(fileName: String) : CSharpAstListener(fileName) {
if (ctx == null) return

var commonMemberForClass: CSharpParser.Common_member_declarationContext? = null
if (ctx.parent.javaClass.simpleName == "Common_member_declarationContext") {
commonMemberForClass = ctx.parent as CSharpParser.Common_member_declarationContext
} else if (ctx.parent.parent.javaClass.simpleName == "Common_member_declarationContext") {
commonMemberForClass = ctx.parent.parent as CSharpParser.Common_member_declarationContext
when (val parent = ctx.parent) {
is CSharpParser.Common_member_declarationContext -> {
commonMemberForClass = parent
}

is CSharpParser.Class_member_declarationContext -> {
commonMemberForClass = parent.parent as CSharpParser.Common_member_declarationContext
}
}

if (commonMemberForClass == null) return

if (commonMemberForClass.parent.javaClass.simpleName == "Class_member_declarationContext") {
if (commonMemberForClass.parent is CSharpParser.Class_member_declarationContext) {
handleClassMember(commonMemberForClass.parent as CSharpParser.Class_member_declarationContext?)
}
}

override fun enterProperty_declaration(ctx: CSharpParser.Property_declarationContext?) {
val memberName = ctx!!.member_name()
when (ctx.parent.javaClass.simpleName) {
"Typed_member_declarationContext" -> {
val typedMember = ctx.parent as CSharpParser.Typed_member_declarationContext
val typeValue = memberName.text
val typeContext = typedMember.type_()
when (val parent = ctx?.parent) {
is CSharpParser.Typed_member_declarationContext -> {
val typeValue = ctx.member_name().text
val typeContext = parent.type_()

val field = createField(typeValue, typeContext)

currentStruct.Fields += field
}
}
Expand All @@ -154,11 +153,9 @@ class CSharpFullIdentListener(fileName: String) : CSharpAstListener(fileName) {
val field = CodeField(TypeValue = typeValue)
field.TypeType = typeContext.text

val child = typeContext.base_type().getChild(0)
when (child.javaClass.simpleName) {
"Class_typeContext" -> {
val clazzType = child as Class_typeContext
val nsOrType = clazzType.namespace_or_type_name()
when (val child = typeContext.base_type().getChild(0)) {
is CSharpParser.Class_typeContext -> {
val nsOrType = child.namespace_or_type_name()
if (nsOrType != null) {
field.Modifiers = listOf(nsOrType.identifier()[0].text)
if (nsOrType.type_argument_list() != null) {
Expand All @@ -180,30 +177,38 @@ class CSharpFullIdentListener(fileName: String) : CSharpAstListener(fileName) {

// call from method invocation parent will be easy to search for method call
override fun enterMethod_invocation(ctx: CSharpParser.Method_invocationContext?) {
if (ctx == null) {
return
}
val primaryExpr = ctx.parent as Primary_expressionContext
val parent = ctx?.parent ?: return
val primaryExpr = when (parent) {
is CSharpParser.Primary_expressionContext -> {
ctx.parent as CSharpParser.Primary_expressionContext
}

else -> {
null
}
} ?: return

var ident = ""
var member = ""
var params: List<CodeProperty> = listOf()
primaryExpr.children.forEach {
when (it.javaClass.simpleName) {
when (it) {
// todo: merge to primary expression
"SimpleNameExpressionContext" -> {
ident = (it as CSharpParser.SimpleNameExpressionContext).identifier().text
is CSharpParser.SimpleNameExpressionContext -> {
ident = it.identifier().text
}
"Member_accessContext" -> {
member = (it as CSharpParser.Member_accessContext).identifier().text

is CSharpParser.Member_accessContext -> {
member = it.identifier().text
}
"Method_invocationContext" -> {

is CSharpParser.Method_invocationContext -> {
// todo: parse parameters
val argumentList = (it as CSharpParser.Method_invocationContext).argument_list()
if (argumentList != null) {
it.argument_list()?.let { argumentList ->
params = parseParameters(argumentList)
}
}

else -> {
println(it.javaClass.simpleName)
}
Expand All @@ -222,9 +227,4 @@ class CSharpFullIdentListener(fileName: String) : CSharpAstListener(fileName) {
)
}
}

// //for debug only
// override fun enterEveryRule(ctx: ParserRuleContext?) {
// println(ctx!!.javaClass.simpleName)
// }
}
27 changes: 12 additions & 15 deletions chapi-ast-go/src/main/kotlin/chapi/ast/goast/GoFullIdentListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,20 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() {
private var lastBlock = CodeFunction(Type = FunctionType.Block, Name = "chapi_block")

override fun enterBlock(ctx: GoParser.BlockContext?) {
if (ctx?.parent is GoParser.StatementContext) {
lastBlock = CodeFunction(Type = FunctionType.Block, Name = "chapi_block" + "${blockStack.count()}")
blockStack.push(lastBlock)
}
if (ctx?.parent !is GoParser.StatementContext) return

lastBlock = CodeFunction(Type = FunctionType.Block, Name = "chapi_block" + "${blockStack.count()}")
blockStack.push(lastBlock)
}

override fun exitBlock(ctx: GoParser.BlockContext?) {
if (ctx?.parent is GoParser.StatementContext) {
val popBlock = blockStack.pop()!!
if (blockStack.count() > 0) {
blockStack.peek()!!.InnerFunctions += popBlock
} else {
currentFunction.InnerFunctions += popBlock
}
if (ctx?.parent !is GoParser.StatementContext) return

val popBlock = blockStack.pop()!!
if (blockStack.count() > 0) {
blockStack.peek()!!.InnerFunctions += popBlock
} else {
currentFunction.InnerFunctions += popBlock
}
}

Expand Down Expand Up @@ -110,9 +110,7 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() {
val typeValue = it.identifierList()?.text ?: ""
val typeType = it.type_()?.text ?: ""

val pair = processingStringType(typeValue, typeType)

return Pair(pair.first, pair.second)
return processingStringType(typeValue, typeType)
}

private fun processingStringType(typeValue: String, typeType: String): Pair<String, String> {
Expand Down Expand Up @@ -287,7 +285,6 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() {
}

else -> {
println("${child.javaClass} not implemented -> ${child.text}")
CodeCall(NodeName = child.text)
}
}
Expand Down

0 comments on commit 8097334

Please sign in to comment.