Skip to content

Commit

Permalink
Merge branch 'master' into future/16
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Jul 4, 2023
2 parents 63d4aec + afe2f3c commit ff6cbe2
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 39 deletions.
2 changes: 2 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
val clikt_version = extra["clikt_version"]
val gson_version = extra["gson_version"]

dependencies {
implementation("com.github.salomonbrys.kotson:kotson:2.5.0")
implementation("org.redundent:kotlin-xml-builder:1.9.0")
implementation("com.github.doyaaaaaken:kotlin-csv-jvm:1.8.0")
implementation("com.google.code.gson:gson:$gson_version")

implementation("com.github.ajalt.clikt:clikt:$clikt_version")
api("io.reactivex.rxjava3:rxjava:3.1.1")
Expand Down
28 changes: 0 additions & 28 deletions core/src/main/kotlin/com/strumenta/kolasu/codegen/PrinterOutput.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.strumenta.kolasu.codegen

import com.strumenta.kolasu.model.Node
import com.strumenta.kolasu.model.PossiblyNamed
import com.strumenta.kolasu.model.Range
import com.strumenta.kolasu.model.ReferenceByName
import com.strumenta.kolasu.model.START_POINT
import com.strumenta.kolasu.model.TextFileDestination
import java.lang.IllegalStateException
import kotlin.reflect.KClass
import kotlin.reflect.full.superclasses

Expand Down Expand Up @@ -168,17 +165,6 @@ class PrinterOutput(
}
}

fun <T : PossiblyNamed> printRefsList(elements: List<ReferenceByName<T>>, separator: String = ", ") {
var i = 0
while (i < elements.size) {
if (i != 0) {
print(separator)
}
print(elements[i].name)
i += 1
}
}

fun <T : Node> printList(
prefix: String,
elements: List<T>,
Expand All @@ -193,20 +179,6 @@ class PrinterOutput(
}
}

fun <T : PossiblyNamed> printRefsList(
prefix: String,
elements: List<ReferenceByName<T>>,
postfix: String,
printEvenIfEmpty: Boolean = false,
separator: String = ", "
) {
if (elements.isNotEmpty() || printEvenIfEmpty) {
print(prefix)
printRefsList(elements, separator)
print(postfix)
}
}

fun printOneOf(vararg alternatives: Node?) {
val notNull = alternatives.filterNotNull()
if (notNull.size != 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ class JsonGenerator {
withDestinationIds: IdentityHashMap<Node, String>? = null
):
JsonElement {
val nodeType = node.nodeType
val jsonObject = jsonObject(
JSON_TYPE_KEY to if (shortClassNames) node.javaClass.simpleName else node.javaClass.canonicalName,
JSON_TYPE_KEY to if (shortClassNames) nodeType.substring(nodeType.lastIndexOf('.') + 1) else nodeType,
JSON_RANGE_KEY to node.range?.toJson()
)
if (withIds != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import kotlin.reflect.full.isSubtypeOf
data class Scope(
var parent: Scope? = null,
val symbolTable: SymbolTable = mutableMapOf(),
val ignoreCase: Boolean = false,
val ignoreCase: Boolean = false
) {
fun define(symbol: PossiblyNamed) {
this.symbolTable.computeIfAbsent(symbol.name.toSymbolTableKey()) { mutableListOf() }.add(symbol)
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/kotlin/com/strumenta/kolasu/testing/Testing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ fun assertASTsAreEqual(
assertEquals(
expectedPropValue.name,
actualPropValue.name,
"$context, comparing reference name of ${expectedProperty.name} of ${expected.nodeType}",
"$context, comparing reference name of ${expectedProperty.name} of ${expected.nodeType}"
)
assertEquals(
expectedPropValue.referred?.toString(),
actualPropValue.referred?.toString(),
"$context, comparing reference pointer ${expectedProperty.name} of ${expected.nodeType}",
"$context, comparing reference pointer ${expectedProperty.name} of ${expected.nodeType}"
)
} else {
TODO()
Expand All @@ -171,13 +171,13 @@ fun assertASTsAreEqual(
assertEquals(
expectedPropValue?.toString(),
actualPropValue?.toString(),
"$context, comparing property ${expectedProperty.name} of ${expected.nodeType}",
"$context, comparing property ${expectedProperty.name} of ${expected.nodeType}"
)
} else {
assertEquals(
expectedPropValue,
actualPropValue,
"$context, comparing property ${expectedProperty.name} of ${expected.nodeType}",
"$context, comparing property ${expectedProperty.name} of ${expected.nodeType}"
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion emf/src/main/kotlin/com/strumenta/kolasu/emf/Metamodel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ val KClass<*>.eClassifierName: String

val Class<*>.eClassifierName: String
get() = if (this.enclosingClass != null) {
"${this.enclosingClass.simpleName}${this.simpleName}"
"${this.enclosingClass.simpleName}.${this.simpleName}"
} else {
this.simpleName
}
Expand Down
23 changes: 20 additions & 3 deletions emf/src/main/kotlin/com/strumenta/kolasu/emf/MetamodelBuilder.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.strumenta.kolasu.emf

import com.strumenta.kolasu.model.PropertyTypeDescription
import com.strumenta.kolasu.model.isANode
import com.strumenta.kolasu.model.processProperties
import org.eclipse.emf.ecore.EClass
import org.eclipse.emf.ecore.EClassifier
Expand All @@ -25,12 +26,23 @@ import kotlin.reflect.full.withNullability

private val KClass<*>.packageName: String?
get() {
val qname = this.qualifiedName ?: throw IllegalStateException("The class has no qualified name: $this")
val isInternal = this.java.name.contains('$')
val qname = if (isInternal) {
this.java.name.split("$").first()
} else {
this.qualifiedName ?: throw IllegalStateException("The class has no qualified name: $this")
}
return if (qname == this.simpleName) {
null
} else {
require(qname.endsWith(".${this.simpleName}"))
qname.removeSuffix(".${this.simpleName}")
if (isInternal) {
val last = qname.split(".").last()
require(qname.endsWith(".$last"))
qname.removeSuffix(".$last")
} else {
require(qname.endsWith(".${this.simpleName}"))
qname.removeSuffix(".${this.simpleName}")
}
}
}

Expand Down Expand Up @@ -372,6 +384,11 @@ class MetamodelBuilder(packageName: String, nsURI: String, nsPrefix: String, res
queue.add(it)
}
}
kClass.nestedClasses.forEach {
if (it.isANode()) {
queue.add(it)
}
}
}
while (queue.isNotEmpty()) {
provideClass(queue.removeFirst())
Expand Down
24 changes: 23 additions & 1 deletion emf/src/test/kotlin/com/strumenta/kolasu/emf/MetamodelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ class MetamodelTest {
)
metamodelBuilder.provideClass(NodeWithReference::class)
val ePackage = metamodelBuilder.generate()
println(ePackage.saveAsJsonObject().toString())
assertEquals("com.strumenta.kolasu.emf", ePackage.name)
assertEquals(1, ePackage.eClassifiers.size)

Expand All @@ -166,4 +165,27 @@ class MetamodelTest {
val pointers = nodeWithReference.eStructuralFeatures.find { it.name == "pointers" } as EReference
assertEquals(true, pointers.isContainment)
}

@Test
fun internalClasses() {
val metamodelBuilder = MetamodelBuilder(
"com.strumenta.kolasu.emf",
"https://strumenta.com/simplemm",
"simplemm"
)
metamodelBuilder.provideClass(MyClassWithInternalClasses::class)
val ePackage = metamodelBuilder.generate()
assertEquals("com.strumenta.kolasu.emf", ePackage.name)
assertEquals(2, ePackage.eClassifiers.size)
val MyClassWithInternalClasses = ePackage.eClassifiers[0]
val Internal = ePackage.eClassifiers[1]
assertEquals("MyClassWithInternalClasses", MyClassWithInternalClasses.name)
assertEquals("MyClassWithInternalClasses.Internal", Internal.name)
}
}

class MyClassWithInternalClasses : Node() {
class Internal : Node()

class InternalWhichIsNotANode
}

0 comments on commit ff6cbe2

Please sign in to comment.