Skip to content

Commit

Permalink
Started to convert AST single edges
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Aug 10, 2024
1 parent b8fcce4 commit 51e7d08
Show file tree
Hide file tree
Showing 22 changed files with 263 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,20 @@ fun <PropertyType : Node, NodeType : Node, EdgeType : Edge<PropertyType>> NodeTy
return edge.unwrap()
}

/** See [EdgeSingletonList.Delegate]. */
fun <PropertyType : Node, NodeType : Node, EdgeType : Edge<PropertyType>> NodeType
.unwrappingOptional(
edgeProperty: KProperty1<NodeType, EdgeSingletonList<PropertyType, PropertyType?, EdgeType>>,
): EdgeSingletonList<PropertyType, PropertyType?, EdgeType>.Delegate<NodeType> {
edgeProperty.isAccessible = true
val edge = edgeProperty.call(this)
return edge.delegate<NodeType>()
}

/** See [EdgeSingletonList.Delegate]. */
fun <PropertyType : Node, NodeType : Node, EdgeType : Edge<PropertyType>> NodeType.unwrapping(
edgeProperty: KProperty1<NodeType, EdgeSingletonList<PropertyType, EdgeType>>,
): EdgeSingletonList<PropertyType, EdgeType>.Delegate<NodeType> {
edgeProperty: KProperty1<NodeType, EdgeSingletonList<PropertyType, PropertyType, EdgeType>>,
): EdgeSingletonList<PropertyType, PropertyType, EdgeType>.Delegate<NodeType> {
edgeProperty.isAccessible = true
val edge = edgeProperty.call(this)
return edge.delegate<NodeType>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,44 @@ open class AstEdge<T : Node> : Edge<T> {

/** Creates an [AstEdges] container starting from this node. */
fun <NodeType : Node> Node.astEdgesOf(
postAdd: ((AstEdge<NodeType>) -> Unit)? = null,
postRemove: ((AstEdge<NodeType>) -> Unit)? = null,
onAdd: ((AstEdge<NodeType>) -> Unit)? = null,
onRemove: ((AstEdge<NodeType>) -> Unit)? = null,
): AstEdges<NodeType, AstEdge<NodeType>> {
return AstEdges(this, postAdd, postRemove)
return AstEdges(this, onAdd, onRemove)
}

/** Creates an [AstEdges] container starting from this node. */
fun <NodeType : Node> Node.astOptionalEdgeOf(
onChanged: ((old: AstEdge<NodeType>?, new: AstEdge<NodeType>?) -> Unit)? = null,
): EdgeSingletonList<NodeType, NodeType?, AstEdge<NodeType>> {
return EdgeSingletonList(
thisRef = this,
init = ::AstEdge,
outgoing = true,
onChanged = onChanged,
of = null
)
}

/** Creates an [AstEdges] container starting from this node. */
fun <NodeType : Node> Node.astEdgeOf(
of: NodeType,
): EdgeSingletonList<NodeType, Edge<NodeType>> {
return EdgeSingletonList(thisRef = this, init = ::AstEdge, outgoing = true, of = of)
onChanged: ((old: AstEdge<NodeType>?, new: AstEdge<NodeType>?) -> Unit)? = null,
): EdgeSingletonList<NodeType, NodeType, AstEdge<NodeType>> {
return EdgeSingletonList(
thisRef = this,
init = ::AstEdge,
outgoing = true,
onChanged = onChanged,
of = of
)
}

/** This property edge list describes elements that are AST children of a node. */
open class AstEdges<NodeType : Node, PropertyEdgeType : Edge<NodeType>>(
thisRef: Node,
postAdd: ((PropertyEdgeType) -> Unit)? = null,
postRemove: ((PropertyEdgeType) -> Unit)? = null,
onAdd: ((PropertyEdgeType) -> Unit)? = null,
onRemove: ((PropertyEdgeType) -> Unit)? = null,
@Suppress("UNCHECKED_CAST")
init: (start: Node, end: NodeType) -> PropertyEdgeType = { start, end ->
AstEdge<NodeType>(start, end) as PropertyEdgeType
Expand All @@ -67,6 +87,6 @@ open class AstEdges<NodeType : Node, PropertyEdgeType : Edge<NodeType>>(
EdgeList<NodeType, PropertyEdgeType>(
thisRef = thisRef,
init = init,
postAdd = postAdd,
postRemove = postRemove,
onAdd = onAdd,
onRemove = onRemove,
)
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ interface EdgeCollection<
var thisRef: Node
var init: (start: Node, end: NodeType) -> EdgeType
var outgoing: Boolean
var postAdd: ((EdgeType) -> Unit)?
var postRemove: ((EdgeType) -> Unit)?
var onAdd: ((EdgeType) -> Unit)?
var onRemove: ((EdgeType) -> Unit)?

/**
* Removes all edges with the target node. The target is considered to be either the [Edge.end]
Expand Down Expand Up @@ -139,7 +139,7 @@ interface EdgeCollection<
* Note, that is an immutable list and only a snapshot. If you want a magic container that is in
* sync with this [EdgeCollection], please use [unwrap].
*/
fun toNodeCollection(outgoing: Boolean = true): Collection<NodeType>
fun toNodeCollection(): Collection<NodeType>

/**
* Returns an [UnwrappedEdgeCollection] magic container which holds a structure that provides
Expand All @@ -153,16 +153,16 @@ interface EdgeCollection<
* propagate the edge to other properties or register additional handlers, e.g. a
* [TypeObserver].
*/
fun handlePostAdd(edge: EdgeType) {
postAdd?.invoke(edge)
fun handleOnAdd(edge: EdgeType) {
onAdd?.invoke(edge)
}

/**
* This function will be executed after an edge was removed from the container. This can be used
* to unregister additional handlers, e.g. a [TypeObserver].
*/
fun handlePostRemove(edge: EdgeType) {
postRemove?.invoke(edge)
fun handleOnRemove(edge: EdgeType) {
onRemove?.invoke(edge)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ abstract class EdgeList<NodeType : Node, EdgeType : Edge<NodeType>>(
override var thisRef: Node,
override var init: (start: Node, end: NodeType) -> EdgeType,
override var outgoing: Boolean = true,
override var postAdd: ((EdgeType) -> Unit)? = null,
override var postRemove: ((EdgeType) -> Unit)? = null
override var onAdd: ((EdgeType) -> Unit)? = null,
override var onRemove: ((EdgeType) -> Unit)? = null
) : ArrayList<EdgeType>(), EdgeCollection<NodeType, EdgeType> {

override fun add(e: EdgeType): Boolean {
Expand All @@ -46,23 +46,23 @@ abstract class EdgeList<NodeType : Node, EdgeType : Edge<NodeType>>(

val ok = super<ArrayList>.add(e)
if (ok) {
handlePostAdd(e)
handleOnAdd(e)
}
return ok
}

override fun remove(o: EdgeType): Boolean {
val ok = super<ArrayList>.remove(o)
if (ok) {
handlePostRemove(o)
handleOnRemove(o)
}
return ok
}

override fun removeAll(c: Collection<EdgeType>): Boolean {
val ok = super.removeAll(c)
if (ok) {
c.forEach { handlePostRemove(it) }
c.forEach { handleOnRemove(it) }
}
return ok
}
Expand All @@ -73,23 +73,23 @@ abstract class EdgeList<NodeType : Node, EdgeType : Edge<NodeType>>(

override fun removeAt(index: Int): EdgeType {
var edge = super.removeAt(index)
handlePostRemove(edge)
handleOnRemove(edge)
return edge
}

override fun removeIf(predicate: Predicate<in EdgeType>): Boolean {
var edges = filter { predicate.test(it) }
val ok = super<ArrayList>.removeIf(predicate)
if (ok) {
edges.forEach { handlePostRemove(it) }
edges.forEach { handleOnRemove(it) }
}
return ok
}

override fun clear() {
var edges = this.toList()
super.clear()
edges.forEach { handlePostRemove(it) }
edges.forEach { handleOnRemove(it) }
}

override fun add(index: Int, element: EdgeType) {
Expand All @@ -98,15 +98,15 @@ abstract class EdgeList<NodeType : Node, EdgeType : Edge<NodeType>>(

super<ArrayList>.add(index, element)

handlePostAdd(element)
handleOnAdd(element)

// We need to re-compute all edges with an index > inserted index
for (i in index until this.size) {
this[i].index = i
}
}

override fun toNodeCollection(outgoing: Boolean): List<NodeType> {
override fun toNodeCollection(): List<NodeType> {
return internalToNodeCollection(this, outgoing, ::ArrayList)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ abstract class EdgeSet<NodeType : Node, EdgeType : Edge<NodeType>>(
override var thisRef: Node,
override var init: (start: Node, end: NodeType) -> EdgeType,
override var outgoing: Boolean = true,
override var postAdd: ((EdgeType) -> Unit)? = null,
override var postRemove: ((EdgeType) -> Unit)? = null
override var onAdd: ((EdgeType) -> Unit)? = null,
override var onRemove: ((EdgeType) -> Unit)? = null
) : HashSet<EdgeType>(), EdgeCollection<NodeType, EdgeType> {
override fun add(e: EdgeType): Boolean {
val ok = super<HashSet>.add(e)
if (ok) {
handlePostAdd(e)
handleOnAdd(e)
}
return ok
}
Expand All @@ -52,7 +52,7 @@ abstract class EdgeSet<NodeType : Node, EdgeType : Edge<NodeType>>(
var edges = filter { predicate.test(it) }
val ok = super<HashSet>.removeIf(predicate)
if (ok) {
edges.forEach { handlePostRemove(it) }
edges.forEach { handleOnRemove(it) }
}
return ok
}
Expand All @@ -61,26 +61,26 @@ abstract class EdgeSet<NodeType : Node, EdgeType : Edge<NodeType>>(
val edges = this.toSet()
val ok = super.removeAll(c)
if (ok) {
edges.forEach { handlePostRemove(it) }
edges.forEach { handleOnRemove(it) }
}
return ok
}

override fun remove(o: EdgeType): Boolean {
val ok = super<HashSet>.remove(o)
if (ok) {
handlePostRemove(o)
handleOnRemove(o)
}
return ok
}

override fun clear() {
var edges = this.toList()
super.clear()
edges.forEach { handlePostRemove(it) }
edges.forEach { handleOnRemove(it) }
}

override fun toNodeCollection(outgoing: Boolean): MutableSet<NodeType> {
override fun toNodeCollection(): MutableSet<NodeType> {
return internalToNodeCollection(this, outgoing, ::HashSet)
}

Expand Down
Loading

0 comments on commit 51e7d08

Please sign in to comment.