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

com.github.treepat.expression.operators.DepthClosureFunction.kt Maven / Gradle / Ivy

Go to download

Treepat is a language to recognise patterns in trees in a similar way as regular expressions recognize patterns in strings. Treepat includes analogous operators to regex union, concatenation, and closure, which are extended to the notion of trees.

There is a newer version: 2.0.0
Show newest version
package com.github.treepat.expression.operators

import com.github.treepat.target_tree.TargetTreeNode

fun depthClosureFunction(
    expression: VisitorFunction
): VisitorFunction {
    fun go(targetTreeNode: TargetTreeNode?): VisitorFunctionResponse {
        val currentAnswer = expression.invoke(targetTreeNode)
        if (targetTreeNode == null || !currentAnswer.hasMatch) {
            return VisitorFunctionResponseFactory.createResponseWithZeroMatches(targetTreeNode)
        }
        val answer = mutableListOf()
        answer.addAll(currentAnswer.responses.map {
            val response = go(it.depthTerm)
            VisitorFunctionResponseFactory.createMergeResponse(it, response, hasMatch = true)
        })
        val response = VisitorFunctionResponseFactory.createResponse(answer, targetTreeNode)
        val depthClosureFixed =
            response.responses.map { it.copy(lastVisitedSibling = targetTreeNode, depthTerm = null) }
        return VisitorFunctionResponse(depthClosureFixed, response.hasMatch)
    }
    return ::go
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy