All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.piacenti.dslmaker.structures.derivationgraph.DerivationNode.kt Maven / Gradle / Ivy

Go to download

Kotlin multiplatform library to facilitate creation of DSLs with ANTLR or a simple built in parser

There is a newer version: 1.1.55
Show newest version
package piacenti.dslmaker.structures.derivationgraph

import piacenti.dslmaker.abstraction.ProductionStep
import piacenti.dslmaker.interfaces.TreePrinter

/**
 * @param 
 * @author Piacenti
 */
@Suppress("MemberVisibilityCanBePrivate")
class DerivationNode(var step: ProductionStep, var isRootNode: Boolean = false) : TreePrinter {

    var parent: DerivationNode? = null
    override var children: MutableList = mutableListOf()

    var isLeaf = false

    override fun treeNodeIdentifierString(): String? {
        return null
    }

    override fun treeNodeText(): String {
        return toString()
    }
    @Suppress("MemberVisibilityCanBePrivate")
    fun fixTreeLinks() {
        traverseTree { parent, child ->
            child.parent = parent
            true
        }
    }
    @Suppress("MemberVisibilityCanBePrivate")
    fun traverseTree(action: (parent: DerivationNode, node: DerivationNode) -> Boolean) {
        var root = this
        while (root.parent != null) {
            root = root.parent!!
        }
        traverseDown(root, action)
    }

    @Suppress("MemberVisibilityCanBePrivate")
    fun traverseDown(node: DerivationNode = this, action: (parent: DerivationNode, node: DerivationNode) -> Boolean) {
        node.children.forEach {
            if (action(node, it))
                traverseDown(it, action)
        }
    }
    override fun toString(): String {
        return "$step\nleaf:$isLeaf\n root:$isRootNode"
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy