Skip to content

Commit

Permalink
Complete and test serialization and deserialization to LionWeb of Pla…
Browse files Browse the repository at this point in the history
…ceholder node annotations
  • Loading branch information
ftomassetti committed Sep 14, 2024
1 parent 2df2e32 commit 1ced2f2
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ class LionWebModelConverter(
setOriginalNode(lwNode, targetID)
}
setPlaceholderNodeType(instance, origin.javaClass.kotlin)
instance.setPropertyValue(
StarLasuLWLanguage.PlaceholderNodeMessageProperty,
origin.message
)
lwNode.addAnnotation(instance)
} else {
throw Exception(
Expand Down Expand Up @@ -399,7 +403,7 @@ class LionWebModelConverter(
throw RuntimeException("Issue instantiating $kClass from LionWeb node $lwNode", e)
}
}
val placeholderNodes = mutableListOf<KNode>()
val placeholderNodes = mutableMapOf<KNode, (KNode) -> Unit>()
lwTree.thisAndAllDescendants().forEach { lwNode ->
val kNode = nodesMapping.byB(lwNode)!!
if (kNode is KNode) {
Expand All @@ -419,7 +423,34 @@ class LionWebModelConverter(
it.classifier == StarLasuLWLanguage.PlaceholderNode
}
if (placeholderNodeAnnotation != null) {
placeholderNodes.add(kNode)
val placeholderType = (
placeholderNodeAnnotation.getPropertyValue(
StarLasuLWLanguage.PlaceholderNodeTypeProperty
) as EnumerationValue
).enumerationLiteral
val placeholderMessage = placeholderNodeAnnotation.getPropertyValue(
StarLasuLWLanguage.PlaceholderNodeMessageProperty
) as String
when (placeholderType.name) {
"MissingASTTransformation" -> {
placeholderNodes[kNode] = { kNode ->
kNode.origin = MissingASTTransformation(
origin = kNode.origin,
transformationSource = kNode.origin as? KNode,
expectedType = null
)
}
}
"FailingASTTransformation" -> {
placeholderNodes[kNode] = { kNode ->
kNode.origin = FailingASTTransformation(
origin = kNode.origin,
message = placeholderMessage
)
}
}
else -> TODO()
}
}
val transpiledNodes = lwNode.getReferenceValues(StarLasuLWLanguage.ASTNodeTranspiledNodes)
if (transpiledNodes.isNotEmpty()) {
Expand All @@ -429,12 +460,10 @@ 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
)
// We want to handle the origin for placeholder nodes AFTER references, to override the origins
// set during the population of references
placeholderNodes.entries.forEach { entry ->
entry.value.invoke(entry.key)
}
return nodesMapping.byB(lwTree)!!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ object StarLasuLWLanguage : Language("com.strumenta.StarLasu") {
this.setOptional(false)
}
placeholderNodeAnnotation.addFeature(type)
val message = Property().apply {
this.name = "message"
this.id = "${placeholderNodeAnnotation.id!!.removeSuffix("-id")}-$name-id"
this.key = "${placeholderNodeAnnotation.key!!.removeSuffix("-key")}-$name-key"
this.type = LionCoreBuiltins.getString()
this.setOptional(false)
}
placeholderNodeAnnotation.addFeature(message)
addElement(placeholderNodeAnnotation)
}

Expand Down Expand Up @@ -177,6 +185,9 @@ object StarLasuLWLanguage : Language("com.strumenta.StarLasu") {
val PlaceholderNodeTypeProperty: Property
get() = PlaceholderNode.getPropertyByName("type")!!

val PlaceholderNodeMessageProperty: Property
get() = PlaceholderNode.getPropertyByName("message")!!

val PlaceholderNodeType: Enumeration
get() = StarLasuLWLanguage.getEnumerationByName("PlaceholderNodeType")!!

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import com.strumenta.kolasu.parsing.KolasuToken
import com.strumenta.kolasu.parsing.ParsingResult
import com.strumenta.kolasu.parsing.TokenCategory
import com.strumenta.kolasu.testing.assertASTsAreEqual
import com.strumenta.kolasu.transformation.FailingASTTransformation
import com.strumenta.kolasu.transformation.MissingASTTransformation
import com.strumenta.kolasu.transformation.dummyInstance
import com.strumenta.kolasu.validation.Issue
import com.strumenta.kolasu.validation.IssueSeverity
import com.strumenta.kolasu.validation.IssueType
Expand Down Expand Up @@ -974,6 +976,51 @@ class LionWebModelConverterTest {
reimported.tokens.map { it.position }
)
}

@Test
fun serializeAndDeserializeNodeWithMissingTransformation() {
val myOriginalSource = LionWebSource("Some_dummy_original_source")
val myMigratedSource = LionWebSource("Some_dummy_migrated_source")
val myOriginalNode = NodeWithEnum::class.dummyInstance()
val myTransformedNode = NodeWithEnum::class.dummyInstance()
myOriginalNode.source = myOriginalSource
myTransformedNode.source = myMigratedSource
myTransformedNode.origin = MissingASTTransformation(myOriginalNode)

val converter = LionWebModelConverter()
converter.exportLanguageToLionWeb(
KolasuLanguage("myLanguage").apply {
addClass(NodeWithEnum::class)
}
)
converter.exportModelToLionWeb(myOriginalNode)
val lwNode = converter.exportModelToLionWeb(myTransformedNode)
val reimportedNode = converter.importModelFromLionWeb(lwNode) as Node
assert(reimportedNode.origin is MissingASTTransformation)
}

@Test
fun serializeAndDeserializeNodeWithFailingTransformation() {
val myOriginalSource = LionWebSource("Some_dummy_original_source")
val myMigratedSource = LionWebSource("Some_dummy_migrated_source")
val myOriginalNode = NodeWithEnum::class.dummyInstance()
val myTransformedNode = NodeWithEnum::class.dummyInstance()
myOriginalNode.source = myOriginalSource
myTransformedNode.source = myMigratedSource
myTransformedNode.origin = FailingASTTransformation(myOriginalNode, "failing because...")

val converter = LionWebModelConverter()
converter.exportLanguageToLionWeb(
KolasuLanguage("myLanguage").apply {
addClass(NodeWithEnum::class)
}
)
converter.exportModelToLionWeb(myOriginalNode)
val lwNode = converter.exportModelToLionWeb(myTransformedNode)
val reimportedNode = converter.importModelFromLionWeb(lwNode) as Node
assert(reimportedNode.origin is FailingASTTransformation)
assertEquals("failing because...", (reimportedNode.origin as FailingASTTransformation).message)
}
}

@ASTRoot(canBeNotRoot = true)
Expand Down

0 comments on commit 1ced2f2

Please sign in to comment.