expression.operators.TreepatFunction.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
fun treepatFunction(expression: VisitorFunction): VisitorFunction = { targetTreeNode ->
var currentNode = targetTreeNode
val answers = mutableListOf()
while (currentNode != null) {
val response = expression.invoke(currentNode)
answers.add(
VisitorFunctionResponse(
response.responses.filter { it.matches.isNotEmpty() },
response.hasMatch
)
)
currentNode = currentNode.nextLeftmostPreorderNode()
}
val allHasMatches = answers.filter { it.hasMatch }.filter { it.responses.isNotEmpty() }.map { it.responses.last() }
if (allHasMatches.isNotEmpty()) {
VisitorFunctionResponse(allHasMatches, true)
} else {
VisitorFunctionResponseFactory.createResponseWithZeroMatches(targetTreeNode)
}
}