add function to extend an existing match, and add tests for it

This commit is contained in:
2026-02-07 00:29:58 +00:00
parent b02b2f53da
commit 0ea0d44da3
2 changed files with 119 additions and 2 deletions

View File

@@ -807,7 +807,7 @@ struct
val matchAfterFinish = nextMatch (finish, tree, 1)
in
if matchAfterFinish <= finish then
(* no match after the 'finish', so we just append *)
(* no match after the 'finish', so we can just append to 'tree' *)
append (start, finish, tree)
else
let
@@ -825,6 +825,31 @@ struct
end
end
fun extendExistingMatch (start, newFinish, tree) =
let
val matchAfterFinish = nextMatch (newFinish, tree, 1)
val left = splitLeft (start, tree)
val left = append (start, newFinish, left)
in
if matchAfterFinish <= newFinish then
(* no match after newFinish, so we can return 'left'
* which has the newFinish appended *)
left
else
let
val right = splitRight (newFinish, tree)
val leftFinish = getFinishIdx left
val rightStartRelative = getStartIdx right
val rightStartAbsolute = rightStartRelative + leftFinish
val difference = rightStartAbsolute - matchAfterFinish
val right = decrementBy (difference, right)
in
merge (left, right)
end
end
(* functions only for testing *)
fun childrenHaveSameDepth (pos, nodes, expectedDepth) =
if pos = Vector.length nodes then