Skip to content

Commit

Permalink
Use LionWeb Kotlin methods for defining languages
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Jan 17, 2025
1 parent c1761e1 commit 7955002
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 112 deletions.
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ gson_version=2.10.1
lionwebJavaVersion=0.2.18
kspVersion=1.0.11
lionwebGenGradlePluginID=com.strumenta.kolasu.lionwebgen
lionwebRepositoryCommitID=7c6dfa1d6decbb7f6c3783caba9b2d78e5de644f
# We should get this from lionweb-kotlin, somehow
lionwebRepositoryCommitID=cbf8e0c67ecadcc3cdf892497e523304a15ff508
SONATYPE_CONNECT_TIMEOUT_SECONDS=180
SONATYPE_CLOSE_TIMEOUT_SECONDS=900
Original file line number Diff line number Diff line change
@@ -1,39 +1,17 @@
package com.strumenta.kolasu.lionweb

import com.strumenta.kolasu.model.Multiplicity
import io.lionweb.lioncore.java.language.Classifier
import io.lionweb.lioncore.java.language.Concept
import io.lionweb.lioncore.java.language.Containment
import io.lionweb.lioncore.java.language.DataType
import io.lionweb.lioncore.java.language.IKeyed
import io.lionweb.lioncore.java.language.Interface
import io.lionweb.lioncore.java.language.Language
import io.lionweb.lioncore.java.language.PrimitiveType
import io.lionweb.lioncore.java.language.Property
import io.lionweb.lioncore.java.language.Reference
import io.lionweb.lioncore.java.model.Node
import io.lionweb.lioncore.java.model.impl.DynamicNode
import kotlin.random.Random

// TODO: move this to LionWeb Kotlin, once that project is created

/**
* Create a LionWeb Language with the given name.
*/
fun lwLanguage(name: String): Language {
val cleanedName = name.lowercase().lwIDCleanedVersion()
return Language(name, "language-$cleanedName-id", "language-$cleanedName-key", "1")
}

fun Language.addConcept(name: String): Concept {
val concept =
Concept(
this,
name,
this.idForContainedElement(name),
this.keyForContainedElement(name)
)
this.addElement(concept)
return concept
}
// TODO: use equivalent methods from LionWeb Kotlin

fun Language.addInterface(name: String): Interface {
val intf =
Expand All @@ -47,48 +25,6 @@ fun Language.addInterface(name: String): Interface {
return intf
}

fun Language.addPrimitiveType(name: String): PrimitiveType {
val primitiveType =
PrimitiveType(
this,
name,
this.idForContainedElement(name)
)
primitiveType.key = this.keyForContainedElement(name)
this.addElement(primitiveType)
return primitiveType
}

fun Concept.addContainment(
name: String,
containedConcept: Concept,
multiplicity: Multiplicity = Multiplicity.SINGULAR
): Containment {
val containment =
Containment().apply {
this.name = name
this.id = "${this@addContainment.id!!.removeSuffix("-id")}-$name-id"
this.key = "${this@addContainment.key!!.removeSuffix("-key")}-$name-key"
this.type = containedConcept
this.setOptional(
when (multiplicity) {
Multiplicity.SINGULAR -> false
Multiplicity.MANY -> true
Multiplicity.OPTIONAL -> true
}
)
this.setMultiple(
when (multiplicity) {
Multiplicity.SINGULAR -> false
Multiplicity.MANY -> true
Multiplicity.OPTIONAL -> false
}
)
}
this.addFeature(containment)
return containment
}

fun Concept.addProperty(
name: String,
type: DataType<*>,
Expand Down Expand Up @@ -119,24 +55,6 @@ val Multiplicity.optional
val Multiplicity.multiple
get() = this == Multiplicity.MANY

fun Concept.addReference(
name: String,
type: Classifier<*>,
multiplicity: Multiplicity = Multiplicity.SINGULAR
): Reference {
val reference =
Reference().apply {
this.name = name
this.id = "${this@addReference.id!!.removeSuffix("-id")}-$name-id"
this.key = "${this@addReference.key!!.removeSuffix("-key")}-$name-key"
this.type = type
this.setOptional(multiplicity.optional)
this.setMultiple(multiplicity.multiple)
}
this.addFeature(reference)
return reference
}

/**
* Create a Dynamic Node with the given Concept and a random node ID.
*/
Expand All @@ -150,18 +68,18 @@ fun String.lwIDCleanedVersion(): String {
.replace("/", "_")
}

private fun Language.idPrefixForContainedElements(): String {
private fun Node.idPrefixForContainedElements(): String {
return this.id!!.removePrefix("language-").removeSuffix("-id")
}

private fun Language.keyPrefixForContainedElements(): String {
private fun IKeyed<*>.keyPrefixForContainedElements(): String {
return this.key!!.removePrefix("language-").removeSuffix("-key")
}

fun Language.idForContainedElement(containedElementName: String): String {
fun Node.idForContainedElement(containedElementName: String): String {
return "${this.idPrefixForContainedElements()}-${containedElementName.lwIDCleanedVersion()}-id"
}

fun Language.keyForContainedElement(containedElementName: String): String {
fun IKeyed<*>.keyForContainedElement(containedElementName: String): String {
return "${this.keyPrefixForContainedElements()}-${containedElementName.lwIDCleanedVersion()}-key"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.strumenta.kolasu.lionweb

import com.strumenta.kolasu.model.Multiplicity
import com.strumenta.kolasu.model.Node
import com.strumenta.kolasu.model.Point
import com.strumenta.kolasu.model.Position
Expand All @@ -22,7 +21,12 @@ import io.lionweb.lioncore.java.self.LionCore
import io.lionweb.lioncore.java.serialization.PrimitiveValuesSerialization.PrimitiveDeserializer
import io.lionweb.lioncore.java.serialization.PrimitiveValuesSerialization.PrimitiveSerializer
import io.lionweb.lioncore.kotlin.MetamodelRegistry
import io.lionweb.lioncore.kotlin.Multiplicity
import io.lionweb.lioncore.kotlin.createConcept
import io.lionweb.lioncore.kotlin.createContainment
import io.lionweb.lioncore.kotlin.createPrimitiveType
import io.lionweb.lioncore.kotlin.createProperty
import io.lionweb.lioncore.kotlin.createReference
import com.strumenta.kolasu.model.BehaviorDeclaration as KBehaviorDeclaration
import com.strumenta.kolasu.model.CommonElement as KCommonElement
import com.strumenta.kolasu.model.Documentation as KDocumentation
Expand Down Expand Up @@ -52,14 +56,14 @@ object StarLasuLWLanguage : Language("com.strumenta.StarLasu") {
id = "com-strumenta-StarLasu"
key = "com_strumenta_starlasu"
version = "1"
addPrimitiveType("Char")
addPrimitiveType("Point")
val position = addPrimitiveType("Position")
val astNode = addConcept("ASTNode").apply {
addProperty(Node::position.name, position, Multiplicity.OPTIONAL)
createPrimitiveType("Char")
createPrimitiveType("Point")
val position = createPrimitiveType("Position")
val astNode = createConcept("ASTNode").apply {
createProperty(Node::position.name, position, Multiplicity.OPTIONAL)
}
astNode.addReference("originalNode", astNode, Multiplicity.OPTIONAL)
astNode.addReference("transpiledNodes", astNode, Multiplicity.MANY)
astNode.createReference("originalNode", astNode, Multiplicity.OPTIONAL)
astNode.createReference("transpiledNodes", astNode, Multiplicity.ZERO_TO_MANY)

addPlaceholderNodeAnnotation(astNode)

Expand All @@ -74,19 +78,19 @@ object StarLasuLWLanguage : Language("com.strumenta.StarLasu") {
addInterface(KStatement::class.simpleName!!).apply { addExtendedInterface(CommonElement) }
addInterface(KTypeAnnotation::class.simpleName!!).apply { addExtendedInterface(CommonElement) }

Issue = addConcept(KIssue::class.simpleName!!).apply {
Issue = createConcept(KIssue::class.simpleName!!).apply {
addProperty(KIssue::type.name, addEnumerationFromClass(this@StarLasuLWLanguage, IssueType::class))
addProperty(KIssue::message.name, LionCoreBuiltins.getString())
createProperty(KIssue::message.name, LionCoreBuiltins.getString())
addProperty(KIssue::severity.name, addEnumerationFromClass(this@StarLasuLWLanguage, IssueSeverity::class))
addProperty(KIssue::position.name, position, Multiplicity.OPTIONAL)
createProperty(KIssue::position.name, position, Multiplicity.OPTIONAL)
}

createPrimitiveType(TokensList::class)
ParsingResult = addConcept(KParsingResult::class.simpleName!!).apply {
addContainment(KParsingResult<*>::issues.name, Issue, Multiplicity.MANY)
addContainment(KParsingResult<*>::root.name, ASTNode, Multiplicity.OPTIONAL)
addProperty(KParsingResult<*>::code.name, LionCoreBuiltins.getString(), Multiplicity.OPTIONAL)
addProperty(
ParsingResult = createConcept(KParsingResult::class.simpleName!!).apply {
createContainment(KParsingResult<*>::issues.name, Issue, Multiplicity.ZERO_TO_MANY)
createContainment(KParsingResult<*>::root.name, ASTNode, Multiplicity.OPTIONAL)
createProperty(KParsingResult<*>::code.name, LionCoreBuiltins.getString(), Multiplicity.OPTIONAL)
createProperty(
ParsingResultWithTokens<*>::tokens.name,
MetamodelRegistry.getPrimitiveType(TokensList::class)!!,
Multiplicity.OPTIONAL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ package com.strumenta.kolasu.lionwebclient

import com.strumenta.kolasu.ids.NodeIdProvider
import com.strumenta.kolasu.language.KolasuLanguage
import com.strumenta.kolasu.lionweb.addConcept
import com.strumenta.kolasu.lionweb.addContainment
import com.strumenta.kolasu.lionweb.lwLanguage
import com.strumenta.kolasu.model.ASTRoot
import com.strumenta.kolasu.model.Multiplicity
import com.strumenta.kolasu.model.Named
import com.strumenta.kolasu.model.Node
import com.strumenta.kolasu.model.ReferenceByName
Expand All @@ -17,11 +13,15 @@ import com.strumenta.kolasu.semantics.symbol.provider.declarative.symbolFor
import com.strumenta.kolasu.semantics.symbol.repository.SymbolRepository
import io.lionweb.lioncore.java.language.LionCoreBuiltins
import io.lionweb.lioncore.java.model.impl.DynamicNode
import io.lionweb.lioncore.kotlin.Multiplicity
import io.lionweb.lioncore.kotlin.createConcept
import io.lionweb.lioncore.kotlin.createContainment
import io.lionweb.lioncore.kotlin.lwLanguage

val todoAccountLanguage =
lwLanguage("todoAccountLanguage").apply {
addConcept("TodoAccount").apply {
addContainment("projects", LionCoreBuiltins.getNode(), Multiplicity.MANY)
createConcept("TodoAccount").apply {
createContainment("projects", LionCoreBuiltins.getNode(), Multiplicity.ZERO_TO_MANY)
}
}

Expand Down

0 comments on commit 7955002

Please sign in to comment.