You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.9 KiB
Kotlin

package me.eater.hefbrug.dsl.scope
import me.eater.hefbrug.definition.DefinitionKey
import me.eater.hefbrug.definition.DefinitionSkeleton
import me.eater.hefbrug.dsl.context.AssignContext
import me.eater.hefbrug.dsl.context.CollectionContext
import me.eater.hefbrug.dsl.context.ContextInterface
import me.eater.hefbrug.dsl.context.TargetedContext
import me.eater.hefbrug.dsl.context.extension_util.RuntimeRegister
import me.eater.hefbrug.logging.Logging
import me.eater.hefbrug.module.Module
import me.eater.hefbrug.node.Node
import me.eater.hefbrug.selector.SelectorInterface
import java.util.*
class SelectorScope(
override val contextUUID: UUID
) : ContextInterface, Logging {
private val selectors: MutableSet<Pair<SelectorInterface, suspend TargetedContext.() -> Unit>> = mutableSetOf()
private val modules: MutableMap<String, Module> = mutableMapOf()
private val assigners: MutableSet<AssignContext.() -> Unit> = mutableSetOf()
fun getModule(id: String): Module =
modules.getOrPut(id, { Module(id, contextUUID) })
fun addSelector(selector: SelectorInterface, block: suspend TargetedContext.() -> Unit) =
selectors.add(selector to block)
fun addAssigner(block: AssignContext.() -> Unit) {
assigners.add(block)
}
suspend fun collect(node: Node): Map<DefinitionKey, DefinitionSkeleton<*, *>> {
val assign = AssignContext(node)
assigners.forEach { it(assign) }
info("Collecting definitions for $node")
val rootScope = RootScope()
val collection = CollectionContext(UUID.randomUUID(), this, rootScope)
for ((selector, block) in selectors) {
if (!selector.matches(node)) {
continue
}
block(collection)
}
RuntimeRegister.remove(collection.runtimeUUID)
info("Collected ${rootScope.definitions.size} for $node")
return rootScope.definitions
}
}