-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
48 additions
and
6 deletions.
There are no files selected for viewing
31 changes: 25 additions & 6 deletions
31
chapi-ast-rust/src/main/kotlin/chapi/ast/rustast/RustFullIdentListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,38 @@ | ||
package chapi.ast.rustast | ||
|
||
import chapi.ast.antlr.RustParser | ||
import chapi.domain.core.* | ||
|
||
open class RustFullIdentListener(fileName: String) : RustAstBaseListener() { | ||
private var currentDataStruct = CodeDataStruct() | ||
private val defaultDataStruct = CodeDataStruct() | ||
private var currentFunction = CodeFunction() | ||
private var structMap = mutableMapOf<String, CodeDataStruct>() | ||
private var codeContainer: CodeContainer = CodeContainer(FullName = fileName) | ||
|
||
private var defaultNode = CodeDataStruct() | ||
private var structMap = mutableMapOf<String, CodeDataStruct>() | ||
private var localVars = mutableMapOf<String, String>() | ||
|
||
private var currentFunction = CodeFunction(IsConstructor = false) | ||
|
||
override fun enterStructStruct(ctx: RustParser.StructStructContext?) { | ||
val structName = ctx!!.identifier().text | ||
|
||
val codeStruct = CodeDataStruct( | ||
NodeName = structName, | ||
Package = codeContainer.PackageName | ||
) | ||
|
||
structMap[structName] = codeStruct | ||
defaultNode = codeStruct | ||
} | ||
|
||
fun getNodeInfo(): CodeContainer { | ||
if (defaultDataStruct.Functions.isNotEmpty()) { | ||
codeContainer.DataStructures += defaultDataStruct | ||
for (entry in structMap) { | ||
codeContainer.DataStructures += entry.value | ||
} | ||
|
||
if (defaultNode.Functions.isNotEmpty()) { | ||
codeContainer.DataStructures += defaultNode | ||
} | ||
|
||
return codeContainer | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
chapi-ast-rust/src/test/kotlin/chapi/ast/rustast/RustFullIdentListenerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package chapi.ast.rustast | ||
|
||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertEquals | ||
|
||
|
||
class RustFullIdentListenerTest { | ||
@Test | ||
fun structDef() { | ||
val str = """ | ||
struct Point { | ||
x: i32, | ||
y: i32, | ||
} | ||
""".trimIndent() | ||
|
||
val codeContainer = RustAnalyser().analysis(str, "test.rs") | ||
assertEquals(1, codeContainer.DataStructures.size) | ||
assertEquals("Point", codeContainer.DataStructures[0].NodeName) | ||
} | ||
} |