Skip to content

Commit

Permalink
feat: <C#> add class function name support
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Feb 14, 2020
1 parent 0e010e7 commit 94bcf52
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package chapi.ast.csharpast

import chapi.ast.antlr.CSharpParser
import domain.core.CodeContainer
import domain.core.CodeDataStruct
import domain.core.CodeImport
import domain.core.CodePackage
import domain.core.*
import domain.infra.Stack

class CSharpFullIdentListener(val fileName: String) : CSharpAstListener() {
Expand Down Expand Up @@ -78,9 +75,42 @@ class CSharpFullIdentListener(val fileName: String) : CSharpAstListener() {
NodeName = className
)

val classMemberDeclarations = ctx.class_body().class_member_declarations()
if (classMemberDeclarations != null) {
for (classMemberDeclarationcontext in classMemberDeclarations.class_member_declaration()) {
this.handleClassMember(classMemberDeclarationcontext, codeDataStruct)
}
}

currentContainer.DataStructures += codeDataStruct
}

private fun handleClassMember(
memberCtx: CSharpParser.Class_member_declarationContext?,
codeDataStruct: CodeDataStruct
) {
val memberDeclaration = memberCtx!!.common_member_declaration()
if (memberDeclaration != null) {
val memberType = memberDeclaration.getChild(0)::class.java.simpleName
when(memberType) {
"TerminalNodeImpl" -> {
val methodDeclaration = memberDeclaration.method_declaration()
val methodName = methodDeclaration.method_member_name()

val codeFunction = CodeFunction(
Package = codeDataStruct.Package,
Name = methodName.text
)

codeDataStruct.Functions += codeFunction
}
else -> {
println(memberType)
}
}
}
}

fun getNodeInfo(): CodeContainer {
return codeContainer
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ namespace HelloWorldApp {
}
"""
val codeContainer = CSharpAnalyser().analysis(code, "hello.cs")
println(codeContainer.toString())
assertEquals(codeContainer.Containers[0].DataStructures.size, 1)
assertEquals(codeContainer.Containers[0].DataStructures[0].NodeName, "Geeks")
}
Expand All @@ -112,10 +111,29 @@ namespace HelloWorldApp {
}
"""
val codeContainer = CSharpAnalyser().analysis(code, "hello.cs")
println(codeContainer.toString())
assertEquals(codeContainer.Containers[0].DataStructures.size, 1)
assertEquals(codeContainer.Containers[0].DataStructures[0].NodeName, "Geeks")
assertEquals(codeContainer.Containers[0].Containers[0].DataStructures.size, 1)
assertEquals(codeContainer.Containers[0].Containers[0].DataStructures[0].NodeName, "Geeks2")
}

@Test
fun shouldIdentNameSpaceClassMethodSupport() {
val code = """
using System;
namespace HelloWorldApp {
class Geeks {
static void Main(string[] args) {
Console.WriteLine("Hello World!");
Console.ReadKey();
}
}
}
"""
val codeContainer = CSharpAnalyser().analysis(code, "hello.cs")
val codeDataStruct = codeContainer.Containers[0].DataStructures[0]
assertEquals(codeDataStruct.Functions.size, 1)
assertEquals(codeDataStruct.Functions[0].Name, "Main")
}
}

0 comments on commit 94bcf52

Please sign in to comment.