Skip to content

Commit

Permalink
feat: add method annotation support
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Mar 3, 2022
1 parent a06b126 commit 4bd7306
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ open class CSharpAnalyser {

ParseTreeWalker().walk(listener, context)

val nodeInfo = listener.getNodeInfo()
return nodeInfo
return listener.getNodeInfo()
}

open fun parse(str: String): CSharpParser {
val fromString = CharStreams.fromString(str)
val lexer = CSharpLexer (fromString)
val tokenStream = CommonTokenStream(lexer)
return CSharpParser(tokenStream)
}
private fun parse(str: String): CSharpParser =
CharStreams.fromString(str)
.let(::CSharpLexer)
.let(::CommonTokenStream)
.let(::CSharpParser)
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package chapi.ast.csharpast

import chapi.ast.antlr.CSharpParser
import chapi.ast.antlr.CSharpParser.Class_typeContext
import chapi.ast.antlr.CSharpParser.Type_declarationContext
import chapi.domain.core.*
import chapi.infra.Stack

class CSharpFullIdentListener(val fileName: String) : CSharpAstListener() {
private var currentStruct: CodeDataStruct = CodeDataStruct();
private var codeContainer: CodeContainer = CodeContainer(FullName = fileName)
private var currentContainer: CodeContainer = codeContainer
private var containerStack: Stack<CodeContainer> = Stack<CodeContainer>()
Expand Down Expand Up @@ -67,12 +69,10 @@ class CSharpFullIdentListener(val fileName: String) : CSharpAstListener() {
val lastContainer = containerStack.elements.last()
lastContainer.Containers += currentContainer
containerStack.elements[containerStack.elements.size - 1] = lastContainer

containerStack.push(currentContainer)
} else {
codeContainer.Containers += currentContainer
containerStack.push(currentContainer)
println(containerStack.elements.size)
}
}

Expand All @@ -88,8 +88,8 @@ class CSharpFullIdentListener(val fileName: String) : CSharpAstListener() {

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

Expand All @@ -101,7 +101,50 @@ class CSharpFullIdentListener(val fileName: String) : CSharpAstListener() {
}
}

currentContainer.DataStructures += codeDataStruct
currentStruct = codeDataStruct;
}

override fun exitClass_definition(ctx: CSharpParser.Class_definitionContext?) {
currentContainer.DataStructures += currentStruct
}

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_()

val field = createField(typeValue, typeContext)

currentStruct.Fields += field
}
}
}

private fun createField(typeValue: String, typeContext: CSharpParser.Type_Context): CodeField {
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()
if (nsOrType != null) {
field.Modifiers = arrayOf(nsOrType.identifier()[0].text)
if (nsOrType.type_argument_list() != null) {
nsOrType.type_argument_list()
.asSequence()
.flatMap { it.type_().asSequence() }
.forEach { field.TypeType = it.text }
}
}
}
}

return field
}

private fun parseAnnotations(attributes: CSharpParser.AttributesContext?): Array<CodeAnnotation> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,53 @@ namespace Chapi {

assertEquals(2, structs[0].Functions.size)
}

@Test
fun shouldIdentProperty() {
val code = """
using System;
namespace Chapi {
public class Chapi {
public Chapi() {}
public Chapi(long id) {}
public long ChapiId { get; set; }
}
}
"""
val codeContainer = CSharpAnalyser().analysis(code, "ChapiController.cs")
val structs = codeContainer.Containers[0].DataStructures
assertEquals(structs.size, 1)

assertEquals(2, structs[0].Functions.size)
assertEquals(1, structs[0].Fields.size)
assertEquals("long", structs[0].Fields[0].TypeType)
assertEquals("ChapiId", structs[0].Fields[0].TypeValue)
}

@Test
fun shouldIdentPropertyInList() {
val code = """
using System;
namespace Chapi {
public class Chapi {
public Chapi() {}
public Chapi(long id) {}
public List<DeviceParameter> Parameters { get; set; }
}
}
"""
val codeContainer = CSharpAnalyser().analysis(code, "ChapiController.cs")
val structs = codeContainer.Containers[0].DataStructures
assertEquals(structs.size, 1)

assertEquals(2, structs[0].Functions.size)
assertEquals(1, structs[0].Fields.size)
assertEquals("List", structs[0].Fields[0].Modifiers[0])
assertEquals("DeviceParameter", structs[0].Fields[0].TypeType)
assertEquals("Parameters", structs[0].Fields[0].TypeValue)
}
}

0 comments on commit 4bd7306

Please # to comment.