Skip to content

Commit 495e860

Browse files
committed
feat(rust): make parse first function call works
1 parent 37b6c42 commit 495e860

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Languages Stages (Welcome to PR your usage languages)
1818
|---------------|------|--------|----|--------|-------|----|----|-------|-----|------|
1919
| http api decl || 🆕 | 🆕 ||| 🆕 | 🆕 | | | |
2020
| syntax parse |||||| 🆕 | 🆕 || 🆕 | 🆕 |
21-
| function call || 🆕 | 🆕 | 🆕 || | | | | |
21+
| function call || 🆕 | 🆕 | 🆕 || | | | | 🆕 |
2222
| arch/package || | ||| | || | |
2323
| real world || | | 🆕 || | | | | |
2424

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
package chapi.ast.rustast
22

3+
import chapi.ast.antlr.RustParser
4+
import chapi.domain.core.CodeCall
5+
import chapi.domain.core.CodeProperty
6+
37

48
open class RustFullIdentListener(val fileName: String) : RustAstBaseListener(fileName) {
9+
override fun enterCallExpression(ctx: RustParser.CallExpressionContext?) {
10+
val function = if (isEnteredIndividualFunction) {
11+
currentIndividualFunction
12+
} else {
13+
currentFunction
14+
}
15+
16+
val functionName = ctx?.expression()?.text
17+
val split = functionName?.split("::")
18+
function.FunctionCalls += CodeCall(
19+
Package = split?.dropLast(1)?.joinToString("::") ?: "",
20+
NodeName = split?.dropLast(1)?.joinToString("::") ?: "",
21+
FunctionName = split?.last() ?: "",
22+
Parameters = ctx?.callParams()?.expression()?.map {
23+
CodeProperty(
24+
TypeValue = it.text,
25+
TypeType = it.text
26+
)
27+
} ?: listOf(),
28+
)
29+
}
530
}

chapi-ast-rust/src/test/kotlin/chapi/ast/rustast/RustFullIdentListenerTest.kt

+19
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,23 @@ class RustFullIdentListenerTest {
188188
assertEquals("crate::Embedding", codeDataStruct.Fields[2].TypeType)
189189
assertEquals("crate::Document", codeDataStruct.Fields[3].TypeType)
190190
}
191+
192+
@Test
193+
fun should_analysis_first_function_call() {
194+
val code = """
195+
use crate::Point;
196+
197+
fn main() {
198+
let p = Point::new(1, 2);
199+
}
200+
""".trimIndent()
201+
202+
val codeContainer = RustAnalyser().analysis(code, "test.rs")
203+
val codeDataStruct = codeContainer.DataStructures[0]
204+
assertEquals(1, codeDataStruct.Functions.size)
205+
val functionCalls = codeDataStruct.Functions[0].FunctionCalls
206+
assertEquals(1, functionCalls.size)
207+
assertEquals("Point", functionCalls[0].NodeName)
208+
assertEquals("new", functionCalls[0].FunctionName)
209+
}
191210
}

0 commit comments

Comments
 (0)