Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Sep 14, 2024
1 parent 3cc6bc1 commit 26b4b7e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import com.strumenta.kolasu.model.Node
import com.strumenta.kolasu.model.Position
import com.strumenta.kolasu.model.START_POINT
import com.strumenta.kolasu.model.TextFileDestination
import com.strumenta.kolasu.transformation.FailingASTTransformation
import com.strumenta.kolasu.transformation.MissingASTTransformation
import com.strumenta.kolasu.transformation.PlaceholderASTTransformation
import kotlin.reflect.KClass
import kotlin.reflect.full.superclasses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.strumenta.kolasu.transformation
import com.strumenta.kolasu.model.*
import com.strumenta.kolasu.validation.Issue
import com.strumenta.kolasu.validation.IssueSeverity
import org.antlr.v4.runtime.tree.ParseTree
import java.lang.reflect.ParameterizedType
import kotlin.random.Random
import org.antlr.v4.runtime.tree.ParseTree
import kotlin.reflect.KClass
import kotlin.reflect.KMutableProperty1
import kotlin.reflect.KParameter
Expand Down Expand Up @@ -248,12 +248,21 @@ sealed class PlaceholderASTTransformation(val origin: Origin?, val message: Stri
get() = origin?.sourceText
}


class MissingASTTransformation(origin: Origin?, val transformationSource: Any?, val expectedType: KClass<out Node>? = null,
message: String = "Translation of a node is not yet implemented: ${if (transformationSource is Node) transformationSource.simpleNodeType else transformationSource}${if (expectedType != null) " into $expectedType" else ""}")
: PlaceholderASTTransformation(origin, message) {
constructor(transformationSource: Node, expectedType: KClass<out Node>? = null) : this(transformationSource, transformationSource, expectedType)
}
class MissingASTTransformation(
origin: Origin?,
val transformationSource: Any?,
val expectedType: KClass<out Node>? = null,
message: String = "Translation of a node is not yet implemented: " +
"${if (transformationSource is Node) transformationSource.simpleNodeType else transformationSource}" +
"${if (expectedType != null) " into $expectedType" else ""}"
) :
PlaceholderASTTransformation(origin, message) {
constructor(transformationSource: Node, expectedType: KClass<out Node>? = null) : this(
transformationSource,
transformationSource,
expectedType
)
}

class FailingASTTransformation(origin: Origin?, message: String) : PlaceholderASTTransformation(origin, message)

Expand Down Expand Up @@ -469,29 +478,38 @@ open class ASTTransformer(
crossinline factory: S.(ASTTransformer) -> T?
): NodeFactory<S, T> = registerNodeFactory(S::class) { source, transformer, _ -> source.factory(transformer) }

inline fun <S : Any, reified T : Node> registerNodeFactory(kclass: KClass<S>, crossinline factory: (S) -> T?): NodeFactory<S, T> =
registerNodeFactory(kclass) { input, _, _ -> try {
factory(input)
} catch (t: NotImplementedError) {
if (faultTollerant) {
val node = T::class.dummyInstance()
node.origin = FailingASTTransformation(asOrigin(input),
"Failed to transform $input into $kclass because the implementation is not complete (${t.message}")
node
} else {
throw RuntimeException("Failed to transform $input into $kclass", t)
}
}catch (e: Exception) {
if (faultTollerant) {
val node = T::class.dummyInstance()
node.origin = FailingASTTransformation(asOrigin(input),
"Failed to transform $input into $kclass because of an error (${e.message})")
node
} else {
throw RuntimeException("Failed to transform $input into $kclass", e)
inline fun <S : Any, reified T : Node> registerNodeFactory(
kclass: KClass<S>,
crossinline factory: (S) -> T?
): NodeFactory<S, T> =
registerNodeFactory(kclass) { input, _, _ ->
try {
factory(input)
} catch (t: NotImplementedError) {
if (faultTollerant) {
val node = T::class.dummyInstance()
node.origin = FailingASTTransformation(
asOrigin(input),
"Failed to transform $input into $kclass because the implementation is not complete " +
"(${t.message}"
)
node
} else {
throw RuntimeException("Failed to transform $input into $kclass", t)
}
} catch (e: Exception) {
if (faultTollerant) {
val node = T::class.dummyInstance()
node.origin = FailingASTTransformation(
asOrigin(input),
"Failed to transform $input into $kclass because of an error (${e.message})"
)
node
} else {
throw RuntimeException("Failed to transform $input into $kclass", e)
}
}
}
}

fun <S : Any, T : Node> registerMultipleNodeFactory(kclass: KClass<S>, factory: (S) -> List<T>): NodeFactory<S, T> =
registerMultipleNodeFactory(kclass) { input, _, _ -> factory(input) }
Expand Down Expand Up @@ -638,13 +656,15 @@ open class ASTTransformer(
}
}

private fun <T:Any> KClass<T>.isDirectlyOrIndirectlyInstantiable(): Boolean {
private fun <T : Any> KClass<T>.isDirectlyOrIndirectlyInstantiable(): Boolean {
return if (this.isSealed) {
this.sealedSubclasses.any { it.isDirectlyOrIndirectlyInstantiable() }
} else !this.isAbstract && !this.java.isInterface
} else {
!this.isAbstract && !this.java.isInterface
}
}

private fun <T:Any> KClass<T>.toInstantiableType() : KClass<out T> {
private fun <T : Any> KClass<T>.toInstantiableType(): KClass<out T> {
return when {
this.isSealed -> {
val subclasses = this.sealedSubclasses.filter { it.isDirectlyOrIndirectlyInstantiable() }
Expand All @@ -657,8 +677,8 @@ private fun <T:Any> KClass<T>.toInstantiableType() : KClass<out T> {
subclasses.first().toInstantiableType()
} else {
// Some constructs are recursive (think of the ArrayType)
// We either find complex logic to find the ones that aren't or just pick one randomly. Eventually we will build
// a tree
// We either find complex logic to find the ones that aren't or just pick one randomly.
// Eventually we will build a tree
val r = Random.Default
subclasses[r.nextInt(subclasses.size)].toInstantiableType()
}
Expand All @@ -678,7 +698,7 @@ private fun <T:Any> KClass<T>.toInstantiableType() : KClass<out T> {
}
}

fun <T:Node> KClass<T>.dummyInstance() : T {
fun <T : Node> KClass<T>.dummyInstance(): T {
val kClassToInstantiate = this.toInstantiableType()
val emptyConstructor = kClassToInstantiate.constructors.find { it.parameters.isEmpty() }
if (emptyConstructor != null) {
Expand All @@ -691,7 +711,8 @@ fun <T:Node> KClass<T>.dummyInstance() : T {
val value = when {
param.type.isMarkedNullable -> null
mt is ParameterizedType && mt.rawType == List::class.java -> mutableListOf<Any>()
(param.type.classifier as KClass<*>).isSubclassOf(Node::class) -> (param.type.classifier as KClass<out Node>).dummyInstance()
(param.type.classifier as KClass<*>).isSubclassOf(Node::class) ->
(param.type.classifier as KClass<out Node>).dummyInstance()
param.type == String::class.createType() -> "DUMMY"
param.type.classifier == ReferenceByName::class -> ReferenceByName<PossiblyNamed>("UNKNOWN")
else -> TODO()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.strumenta.kolasu.codegen

import com.strumenta.kolasu.model.Node
import com.strumenta.kolasu.transformation.MissingASTTransformation
import com.strumenta.kolasu.transformation.PlaceholderASTTransformation

class KotlinPrinter : ASTCodeGenerator<KCompilationUnit>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.strumenta.kolasu.parsing.FirstStageParsingResult
import com.strumenta.kolasu.parsing.KolasuToken
import com.strumenta.kolasu.parsing.ParsingResult
import com.strumenta.kolasu.transformation.MissingASTTransformation
import com.strumenta.kolasu.transformation.PlaceholderASTTransformation
import com.strumenta.kolasu.traversing.walk
import com.strumenta.kolasu.validation.Issue
import com.strumenta.kolasu.validation.IssueSeverity
Expand Down Expand Up @@ -59,7 +60,6 @@ import kotlin.reflect.KMutableProperty
import kotlin.reflect.KParameter
import kotlin.reflect.full.primaryConstructor
import io.lionweb.lioncore.java.language.Feature as LWFeature
import com.strumenta.kolasu.transformation.PlaceholderASTTransformation

interface PrimitiveValueSerialization<E> {
fun serialize(value: E): String
Expand Down Expand Up @@ -169,7 +169,9 @@ class LionWebModelConverter(
if (!nodesMapping.containsA(kNode)) {
val nodeID = myIDManager.nodeId(kNode)
if (!CommonChecks.isValidID(nodeID)) {
throw RuntimeException("We generated an invalid Node ID, using $myIDManager in $kNode. Node ID: $nodeID")
throw RuntimeException(
"We generated an invalid Node ID, using $myIDManager in $kNode. Node ID: $nodeID"
)
}
val lwNode = DynamicNode(nodeID, findConcept(kNode))
associateNodes(kNode, lwNode)
Expand Down Expand Up @@ -403,9 +405,13 @@ class LionWebModelConverter(
}
}
referencesPostponer.populateReferences(nodesMapping, externalNodeResolver)
placeholderNodes.forEach { it.origin = MissingASTTransformation(origin = it.origin,
transformationSource = it.origin as com.strumenta.kolasu.model.Node,
expectedType = null) }
placeholderNodes.forEach {
it.origin = MissingASTTransformation(
origin = it.origin,
transformationSource = it.origin as com.strumenta.kolasu.model.Node,
expectedType = null
)
}
return nodesMapping.byB(lwTree)!!
}

Expand Down

0 comments on commit 26b4b7e

Please sign in to comment.