1
1
package chapi.ast.rustast
2
2
3
3
import chapi.ast.antlr.RustParser
4
+ import chapi.ast.antlr.RustParser.TypePathSegmentContext
5
+ import chapi.ast.antlr.RustParser.Type_Context
4
6
import chapi.ast.antlr.RustParserBaseListener
5
7
import chapi.domain.core.*
6
8
import org.antlr.v4.runtime.ParserRuleContext
7
9
import java.io.File
8
- import java.util.concurrent.atomic.AtomicInteger
9
10
10
11
11
12
open class RustAstBaseListener (private val fileName : String ) : RustParserBaseListener() {
@@ -18,15 +19,13 @@ open class RustAstBaseListener(private val fileName: String) : RustParserBaseLis
18
19
protected open var currentFunction: CodeFunction = CodeFunction ()
19
20
protected var isEnteredImplementation: Boolean = false
20
21
protected var isEnteredIndividualFunction: Boolean = false
21
- private var isEnteredImplementationFunction: Boolean = false
22
22
23
23
protected lateinit var currentIndividualFunction: CodeFunction
24
24
25
25
private val individualFunctions = mutableListOf<CodeFunction >()
26
26
private val individualFields = mutableListOf<CodeField >()
27
27
28
- var structMap = mutableMapOf<String , CodeDataStruct >()
29
-
28
+ private var structMap = mutableMapOf<String , CodeDataStruct >()
30
29
31
30
/* *
32
31
* packageName will parse from fileName, like:
@@ -98,7 +97,7 @@ open class RustAstBaseListener(private val fileName: String) : RustParserBaseLis
98
97
}
99
98
100
99
override fun enterFunction_ (ctx : RustParser .Function_Context ? ) {
101
- if (isEnteredImplementation == false ) {
100
+ if (! isEnteredImplementation) {
102
101
val functionName = ctx!! .identifier().text
103
102
val function = CodeFunction (
104
103
Name = functionName,
@@ -141,7 +140,7 @@ open class RustAstBaseListener(private val fileName: String) : RustParserBaseLis
141
140
}
142
141
143
142
override fun enterImplementation (ctx : RustParser .ImplementationContext ? ) {
144
- val nodeName = ctx?.inherentImpl()?.type_()?.text ? : return
143
+ val nodeName = buildNodeName(ctx)
145
144
if (structMap.containsKey(nodeName)) {
146
145
currentNode = structMap[nodeName]!!
147
146
} else {
@@ -155,6 +154,28 @@ open class RustAstBaseListener(private val fileName: String) : RustParserBaseLis
155
154
isEnteredImplementation = true
156
155
}
157
156
157
+ private fun buildNodeName (ctx : RustParser .ImplementationContext ? ): String {
158
+ // keep this for better to debug
159
+ val types = when {
160
+ ctx?.inherentImpl()?.type_() != null -> listOf (ctx.inherentImpl().type_())
161
+ ctx?.traitImpl()?.type_() != null -> listOf (ctx.traitImpl().type_())
162
+ else -> emptyList()
163
+ }
164
+
165
+ val typePathSegment: List <TypePathSegmentContext >? = types.firstOrNull()
166
+ ?.typeNoBounds()
167
+ ?.traitObjectTypeOneBound()
168
+ ?.traitBound()
169
+ ?.typePath()
170
+ ?.typePathSegment()
171
+
172
+ val pathIdentSegmentContext = typePathSegment?.map {
173
+ it.pathIdentSegment()
174
+ }?.firstOrNull()
175
+
176
+ return pathIdentSegmentContext?.identifier()?.text ? : return " "
177
+ }
178
+
158
179
override fun exitImplementation (ctx : RustParser .ImplementationContext ? ) {
159
180
isEnteredImplementation = false
160
181
structMap[currentNode.NodeName ] = currentNode
0 commit comments