Skip to content

Commit

Permalink
feat: 🎸 allow passing created gumtree node in DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanonymous-GitHub committed Jul 30, 2024
1 parent 50822c3 commit 5c300b7
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions core/src/main/kotlin/tw/xcc/gumtree/helper/GumTreeCreatorHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,41 @@ internal annotation class GumTreeMarker
internal class GumTreeBuilder(grammarName: String, label: String, pos: Int, length: Int) {
private val root by lazy { GumTree(TreeType(grammarName), label, pos, length) }

fun child(
inline fun child(
grammarName: String,
label: String = "",
pos: Int = -1,
length: Int = -1,
block: GumTreeBuilder.() -> Unit
) {
): GumTree {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}

val child = GumTreeBuilder(grammarName, label, pos, length)
child.block()
root.addChild(child.build())

val childTree = child.build()
root.addChild(childTree)
return childTree
}

fun child(
grammarName: String,
label: String = "",
pos: Int = -1,
length: Int = -1
) {
root.addChild(GumTree(TreeType(grammarName), label, pos, length))
): GumTree {
val childTree = GumTree(TreeType(grammarName), label, pos, length)
root.addChild(childTree)
return childTree
}

internal fun build(): GumTree = root
}

@OptIn(ExperimentalContracts::class)
internal fun gumTree(
internal inline fun gumTree(
grammarName: String,
label: String = "",
pos: Int = -1,
Expand Down

0 comments on commit 5c300b7

Please sign in to comment.