expression.operators.BreadthClosureFunction.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Treepat Show documentation
Show all versions of Treepat Show documentation
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.
package expression.operators
import target_tree.TargetTreeNode
fun breadthClosureFunction(
expression: VisitorFunction
): VisitorFunction {
/**
* The [hasMatch] parameter is default to false in order to return createResponseWithZeroMatches with [hasMatch]
* false when we find the first node that doesn't match after finding matches. And true if we haven't found matches
* before.
*/
fun go(targetTreeNode: TargetTreeNode?, hasMatch: Boolean = false): VisitorFunctionResponse {
val currentAnswer = expression.invoke(targetTreeNode)
if (targetTreeNode == null || !currentAnswer.hasMatch) {
return VisitorFunctionResponseFactory.createResponseWithZeroMatches(targetTreeNode, hasMatch)
}
val answer = mutableListOf(currentAnswer)
answer.addAll(currentAnswer.responses.map {
val response = go(it.lastVisitedSibling?.moveToRightSibling())
VisitorFunctionResponseFactory.createMergeResponse(it, response)
})
val allHasMatches = answer.filter { it.hasMatch }.flatMap { it.responses }
return VisitorFunctionResponse(allHasMatches, true)
}
fun go(targetTreeNode: TargetTreeNode?) = go(targetTreeNode, true)
return ::go
}