com.github.treepat.expression.operators.SiblingFunction.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 com.github.treepat.expression.operators
fun siblingFunction(
firstSibling: VisitorFunction,
secondSibling: VisitorFunction
): VisitorFunction = { targetTreeNode ->
val firstAnswer = firstSibling.invoke(targetTreeNode)
var answers = emptyList()
if (firstAnswer.hasMatch) {
answers = firstAnswer.responses.map {
var nextNode = targetTreeNode
if (it.matches.isNotEmpty()) {
nextNode = it.lastVisitedSibling?.moveToRightSibling()
}
val response = secondSibling.invoke(nextNode)
VisitorFunctionResponseFactory.createMergeResponse(it, response)
}
}
VisitorFunctionResponseFactory.createResponse(answers, targetTreeNode)
}