Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

feat: add ScalaParser with trait/class/object/method extraction

riley02273b56b1a868b1
diff --git a/memory-layer/src/main/scala/ix/memory/ingestion/parsers/ScalaParser.scala b/memory-layer/src/main/scala/ix/memory/ingestion/parsers/ScalaParser.scala
new file mode 100644
index 00000000..c6b1e685
--- /dev/null
+++ b/memory-layer/src/main/scala/ix/memory/ingestion/parsers/ScalaParser.scala
@@ -0,0 +1,220 @@
+package ix.memory.ingestion.parsers
+
+import ix.memory.ingestion.{ParseResult, ParsedEntity, ParsedRelationship}
+import ix.memory.model.NodeKind
+import io.circe.Json
+
+import scala.util.matching.Regex
+
+/**
+ * Regex-based Scala source parser that extracts structural entities and relationships.
+ *
+ * Extracts: traits, classes, case classes, objects, methods (def), and imports.
+ * Uses brace counting for block boundary detection.
+ */
+class ScalaParser {
+
+ // Patterns for Scala constructs
+ private val TraitPattern: Regex = """^\s*(?:sealed\s+|abstract\s+|private\s+|protected\s+)*trait\s+(\w+)""".r
+ private val ClassPattern: Regex = """^\s*(?:sealed\s+|abstract\s+|private\s+|protected\s+)*class\s+(\w+)""".r
+ private val CaseClassPattern: Regex = """^\s*(?:sealed\s+|abstract\s+|private\s+|protected\s+)*case\s+class\s+(\w+)""".r
+ private val ObjectPattern: Regex = """^\s*(?:sealed\s+|abstract\s+|private\s+|protected\s+)*(?:case\s+)?object\s+(\w+)""".r
+ private val DefPattern: Regex = """^\s*(?:override\s+)?(?:private(?:\[\w+\])?\s+|protected(?:\[\w+\])?\s+)?(?:final\s+)?def\s+(\w+)""".r
+ private val ImportPattern: Regex = """^\s*import\s+(.+)$""".r
+
+ def parse(fileName: String, source: String): ParseResult = {
+ val lines = source.split("\n", -1)
+
+ val fileEntity = ParsedEntity(
+ name = fileName,
+ kind = NodeKind.File,
+ attrs = Map("language" -> Json.fromString("scala")),
+ lineStart = 1,
+ lineEnd = lines.length
+ )
+
+ var entities = Vector(fileEntity)
+ var relationships = Vector.empty[ParsedRelationship]
+
+ // Track type ranges for containment: (name, startLine, endLine)
+ var typeRanges = Vector.empty[(String, Int, Int)]
+
+ for ((line, idx) <- lines.zipWithIndex) {
+ val lineNum = idx + 1
+ val trimmed = line.trim
+
+ // Skip comment lines
+ if (trimmed.startsWith("//") || trimmed.startsWith("*") || trimmed.startsWith("/*")) {
+ // no-op
+ } else {
+
+ // Case class must be checked before class (since "case class" contains "class")
+ val handledAsType = CaseClassPattern.findFirstMatchIn(line) match {
+ case Some(m) =>
+ val name = m.group(1)
+ val endLine = findBraceBlockEnd(lines, idx).getOrElse(lineNum)
+ entities = entities :+ ParsedEntity(
+ name = name,
+ kind = NodeKind.Class,
+ attrs = Map("scala_kind" -> Json.fromString("case_class"), "language" -> Json.fromString("scala")),
+ lineStart = lineNum,
+ lineEnd = endLine
+ )
+ relationships = relationships :+ ParsedRelationship(fileName, name, "DEFINES")
+ typeRanges = typeRanges :+ (name, lineNum, endLine)
+ true
+
+ case None =>
+ // Check for trait
+ TraitPattern.findFirstMatchIn(line) match {
+ case Some(m) =>
+ val name = m.group(1)
+ val endLine = findBraceBlockEnd(lines, idx).getOrElse(lineNum)
+ entities = entities :+ ParsedEntity(
+ name = name,
+ kind = NodeKind.Class,
+ attrs = Map("scala_kind" -> Json.fromString("trait"), "language" -> Json.fromString("scala")),
+ lineStart = lineNum,
+ lineEnd = endLine
+ )
+ relationships = relationships :+ ParsedRelationship(fileName, name, "DEFINES")
+ typeRanges = typeRanges :+ (name, lineNum, endLine)
+ true
+
+ case None =>
+ // Check for object (before class, since object keyword won't match class pattern)
+ ObjectPattern.findFirstMatchIn(line) match {
+ case Some(m) =>
+ val name = m.group(1)
+ val endLine = findBraceBlockEnd(lines, idx).getOrElse(lineNum)
+ entities = entities :+ ParsedEntity(
+ name = name,
+ kind = NodeKind.Class,
+ attrs = Map("scala_kind" -> Json.fromString("object"), "language" -> Json.fromString("scala")),
+ lineStart = lineNum,
+ lineEnd = endLine
+ )
+ relationships = relationships :+ ParsedRelationship(fileName, name, "DEFINES")
+ typeRanges = typeRanges :+ (name, lineNum, endLine)
+ true
+
+ case None =>
+ // Check for class
+ ClassPattern.findFirstMatchIn(line) match {
+ case Some(m) =>
+ val name = m.group(1)
+ val endLine = findBraceBlockEnd(lines, idx).getOrElse(lineNum)
+ entities = entities :+ ParsedEntity(
+ name = name,
+ kind = NodeKind.Class,
+ attrs = Map("scala_kind" -> Json.fromString("class"), "language" -> Json.fromString("scala")),
+ lineStart = lineNum,
+ lineEnd = endLine
+ )
+ relationships = relationships :+ ParsedRelationship(fileName, name, "DEFINES")
+ typeRanges = typeRanges :+ (name, lineNum, endLine)
+ true
+ case None => false
+ }
+ }
+ }
+ }
+
+ // Check for def (only if we didn't match a type on this line)
+ if (!handledAsType) {
+ DefPattern.findFirstMatchIn(line).foreach { m =>
+ val funcName = m.group(1)
+ val endLine = findBraceBlockEnd(lines, idx).getOrElse(lineNum)
+
+ // Build summary from the first line of the signature, truncated to 120 chars
+ val sigLine = trimmed.take(120)
+ val summary = sigLine
+
+ entities = entities :+ ParsedEntity(
+ name = funcName,
+ kind = NodeKind.Function,
+ attrs = Map(
+ "language" -> Json.fromString("scala"),
+ "summary" -> Json.fromString(summary)
+ ),
+ lineStart = lineNum,
+ lineEnd = endLine
+ )
+
+ // Determine enclosing type
+ val enclosing = typeRanges.find { case (_, start, end) =>
+ lineNum > start && lineNum <= end
+ }
+ enclosing match {
+ case Some((typeName, _, _)) =>
+ relationships = relationships :+ ParsedRelationship(typeName, funcName, "DEFINES")
+ case None =>
+ relationships = relationships :+ ParsedRelationship(fileName, funcName, "DEFINES")
+ }
+ }
+ }
+
+ // Check for imports
+ ImportPattern.findFirstMatchIn(line).foreach { m =>
+ val importPath = m.group(1).trim
+ // Parse import path: "ix.memory.model.{Claim, GraphNode}" -> module "ix.memory.model"
+ // "cats.effect.IO" -> module "cats.effect.IO"
+ val moduleName = if (importPath.contains("{")) {
+ // Grouped import: take the prefix before the braces
+ importPath.substring(0, importPath.indexOf('{')).stripSuffix(".").trim
+ } else {
+ importPath
+ }
+
+ // Avoid duplicate module entities
+ if (!entities.exists(e => e.name == moduleName && e.kind == NodeKind.Module)) {
+ entities = entities :+ ParsedEntity(
+ name = moduleName,
+ kind = NodeKind.Module,
+ attrs = Map.empty,
+ lineStart = lineNum,
+ lineEnd = lineNum
+ )
+ }
+ relationships = relationships :+ ParsedRelationship(fileName, moduleName, "IMPORTS")
+ }
+
+ } // end else (non-comment)
+ }
+
+ ParseResult(entities, relationships)
+ }
+
+ /**
+ * Find the end of a brace-delimited block starting at `startIdx`.
+ * Counts opening and closing braces. Returns None if no opening brace is found
+ * (e.g. abstract method in a trait with no body).
+ */
+ private def findBraceBlockEnd(lines: Array[String], startIdx: Int): Option[Int] = {
+ var braceCount = 0
+ var foundOpen = false
+ var i = startIdx
+
+ while (i < lines.length) {
+ val line = lines(i)
+ for (ch <- line) {
+ ch match {
+ case '{' =>
+ braceCount += 1
+ foundOpen = true
+ case '}' =>
+ braceCount -= 1
+ if (foundOpen && braceCount == 0) {
+ return Some(i + 1) // 1-indexed
+ }
+ case _ => // skip
+ }
+ }
+ // If we found an opening brace and closed it, we'd have returned above
+ // If line ends and no brace found yet, keep going
+ i += 1
+ }
+
+ if (foundOpen) Some(lines.length) else None
+ }
+}
diff --git a/memory-layer/src/test/scala/ix/memory/ingestion/parsers/ScalaParserSpec.scala b/memory-layer/src/test/scala/ix/memory/ingestion/parsers/ScalaParserSpec.scala
new file mode 100644
index 00000000..93c1e863
--- /dev/null
+++ b/memory-layer/src/test/scala/ix/memory/ingestion/parsers/ScalaParserSpec.scala
@@ -0,0 +1,129 @@
+package ix.memory.ingestion.parsers
+
+import org.scalatest.flatspec.AnyFlatSpec
+import org.scalatest.matchers.should.Matchers
+import ix.memory.model.NodeKind
+import io.circe.Json
+
+class ScalaParserSpec extends AnyFlatSpec with Matchers {
+ val parser = new ScalaParser
+
+ val sampleCode: String =
+ """package ix.memory.context
+ |
+ |import cats.effect.IO
+ |import ix.memory.model.{Claim, GraphNode}
+ |
+ |trait ConfidenceScorer {
+ | def score(claim: Claim, ctx: ScoringContext): ScoredClaim
+ |}
+ |
+ |class ConfidenceScorerImpl extends ConfidenceScorer {
+ | override def score(claim: Claim, ctx: ScoringContext): ScoredClaim = {
+ | val base = computeBase(claim)
+ | ScoredClaim(claim, base)
+ | }
+ |
+ | private def computeBase(claim: Claim): Double = {
+ | claim.provenance.sourceType match {
+ | case SourceType.Code => 0.9
+ | case _ => 0.5
+ | }
+ | }
+ |}
+ |
+ |object ConfidenceScorerImpl {
+ | def apply(): ConfidenceScorerImpl = new ConfidenceScorerImpl
+ |}
+ |""".stripMargin
+
+ "ScalaParser" should "create a File entity" in {
+ val result = parser.parse("ConfidenceScorer.scala", sampleCode)
+ val file = result.entities.find(_.kind == NodeKind.File)
+ file shouldBe defined
+ file.get.name shouldBe "ConfidenceScorer.scala"
+ file.get.attrs.get("language") shouldBe Some(Json.fromString("scala"))
+ }
+
+ it should "extract trait definitions" in {
+ val result = parser.parse("ConfidenceScorer.scala", sampleCode)
+ val traits = result.entities.filter(e =>
+ e.kind == NodeKind.Class &&
+ e.attrs.get("scala_kind").contains(Json.fromString("trait"))
+ )
+ traits.map(_.name) should contain("ConfidenceScorer")
+ }
+
+ it should "extract class definitions" in {
+ val result = parser.parse("ConfidenceScorer.scala", sampleCode)
+ val classes = result.entities.filter(e =>
+ e.kind == NodeKind.Class &&
+ e.attrs.get("scala_kind").contains(Json.fromString("class"))
+ )
+ classes.map(_.name) should contain("ConfidenceScorerImpl")
+ }
+
+ it should "extract object definitions" in {
+ val result = parser.parse("ConfidenceScorer.scala", sampleCode)
+ val objects = result.entities.filter(e =>
+ e.kind == NodeKind.Class &&
+ e.attrs.get("scala_kind").contains(Json.fromString("object"))
+ )
+ objects.map(_.name) should contain("ConfidenceScorerImpl")
+ }
+
+ it should "extract method definitions with line ranges" in {
+ val result = parser.parse("ConfidenceScorer.scala", sampleCode)
+ val methods = result.entities.filter(_.kind == NodeKind.Function)
+ methods.map(_.name) should contain allOf ("score", "computeBase", "apply")
+ val computeBase = methods.find(_.name == "computeBase").get
+ computeBase.lineStart should be > 0
+ computeBase.lineEnd should be >= computeBase.lineStart
+ }
+
+ it should "extract import statements" in {
+ val result = parser.parse("ConfidenceScorer.scala", sampleCode)
+ val imports = result.entities.filter(_.kind == NodeKind.Module)
+ imports.map(_.name) should contain allOf ("cats.effect.IO", "ix.memory.model")
+ }
+
+ it should "create DEFINES relationships" in {
+ val result = parser.parse("ConfidenceScorer.scala", sampleCode)
+ val defines = result.relationships.filter(_.predicate == "DEFINES")
+ defines.map(r => (r.srcName, r.dstName)) should contain allOf (
+ ("ConfidenceScorer.scala", "ConfidenceScorer"),
+ ("ConfidenceScorer.scala", "ConfidenceScorerImpl"),
+ ("ConfidenceScorerImpl", "computeBase")
+ )
+ }
+
+ it should "create IMPORTS relationships" in {
+ val result = parser.parse("ConfidenceScorer.scala", sampleCode)
+ val imports = result.relationships.filter(_.predicate == "IMPORTS")
+ imports should not be empty
+ }
+
+ it should "store method signature as summary" in {
+ val result = parser.parse("ConfidenceScorer.scala", sampleCode)
+ val method = result.entities.find(_.name == "computeBase").get
+ val summary = method.attrs.get("summary").flatMap(_.asString)
+ summary shouldBe defined
+ summary.get should include("computeBase")
+ }
+
+ it should "not store raw file content" in {
+ val result = parser.parse("ConfidenceScorer.scala", sampleCode)
+ val file = result.entities.find(_.kind == NodeKind.File).get
+ file.attrs.get("content") shouldBe None
+ }
+
+ it should "handle case classes" in {
+ val code = """case class Foo(bar: String, baz: Int)""".stripMargin
+ val result = parser.parse("Foo.scala", code)
+ val classes = result.entities.filter(e =>
+ e.kind == NodeKind.Class &&
+ e.attrs.get("scala_kind").contains(Json.fromString("case_class"))
+ )
+ classes.map(_.name) should contain("Foo")
+ }
+}