Skip to content

Commit

Permalink
feat: make first parser for java code
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 6, 2023
1 parent 811a47f commit f07c22e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cc.unitmesh.pick.picker

import chapi.domain.core.CodeContainer
import chapi.domain.core.CodeDataStruct
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import org.archguard.scanner.analyser.count.FileJob
Expand Down Expand Up @@ -28,6 +30,7 @@ class PickJob(
var minified: Boolean = false,
var generated: Boolean = false,
var endPoint: Int = 0,
var container: CodeContainer? = null,
) {
companion object {
fun from(fileJob: FileJob): PickJob {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cc.unitmesh.pick.worker

import cc.unitmesh.pick.picker.PickJob
import chapi.ast.antlr.JavaParser
import chapi.ast.javaast.JavaAnalyser
import kotlinx.coroutines.coroutineScope
import org.archguard.scanner.analyser.count.FileJob

Expand All @@ -19,25 +21,31 @@ import org.archguard.scanner.analyser.count.FileJob
* - by Vertical (with History Change):
*/
class JavaLangWorker : LangWorker() {
val jobs: MutableList<PickJob> = mutableListOf()
val packageTree: MutableMap<String, PickJob> = mutableMapOf()
private val jobs: MutableList<PickJob> = mutableListOf()
private val packageTree: MutableMap<String, PickJob> = mutableMapOf()

val packageRegex = Regex("package\\s+([a-zA-Z0-9_\\.]+);")
val extLength = ".java".length
private val packageRegex = Regex("package\\s+([a-zA-Z0-9_\\.]+);")
private val extLength = ".java".length

override fun addJob(job: PickJob) {
this.jobs.add(job)
val packageMatch = packageRegex.find(job.content.decodeToString())
val code = job.content.decodeToString()
val packageMatch = packageRegex.find(code)
if (packageMatch != null) {
val packageName = packageMatch.groupValues[1]
val className = job.filename.substring(0, job.filename.length - extLength)
val fullClassName = "$packageName.$className"
packageTree[fullClassName] = job
}

job.container = JavaAnalyser().analysis(code, job.location)
}

override suspend fun start() = coroutineScope {
override suspend fun start(): Unit = coroutineScope {
// 1. read directory to a collection of files for FileJob
jobs.map {
println(it.container)
}

// 2. check package information from line 1?

Expand All @@ -60,5 +68,4 @@ class JavaLangWorker : LangWorker() {
// TODO: split methods with comments
return listOf()
}

}

0 comments on commit f07c22e

Please sign in to comment.