functorise some boilerplate code to start a DFA loop

This commit is contained in:
2025-07-20 09:39:17 +01:00
parent 25fb8bfc5e
commit 10b76d98c5
5 changed files with 122 additions and 89 deletions

View File

@@ -0,0 +1,61 @@
signature MAKE_DFA_LOOP =
sig
val fNext: int * int * string * string list * Word8.word * int -> int
val fPrev: int * int * string * string list * Word8.word * int -> int
val startState: Word8.word
end
functor MakeDfaLoop(M: MAKE_DFA_LOOP) =
struct
fun next (lineGap: LineGap.t, cursorIdx) =
let
val {rightStrings, idx = bufferIdx, ...} = lineGap
in
case rightStrings of
shd :: stl =>
let
(* convert absolute cursorIdx to idx relative to hd string *)
val strIdx = cursorIdx - bufferIdx
in
if strIdx < String.size shd then
(* strIdx is in this string *)
M.fNext (strIdx, cursorIdx, shd, stl, M.startState, 1)
else
(* strIdx is in tl *)
case stl of
stlhd :: stltl =>
M.fNext (strIdx, cursorIdx, stlhd, stltl, M.startState, 1)
| _ => cursorIdx
end
| [] => cursorIdx
end
fun prev (lineGap: LineGap.t, cursorIdx) =
let
val {rightStrings, leftStrings, idx = bufferIdx, ...} = lineGap
in
case rightStrings of
shd :: stl =>
let
(* convert absolute cursorIdx to idx relative to hd string *)
val strIdx = cursorIdx - bufferIdx
in
if strIdx < String.size shd then
(* strIdx is in this string *)
M.fPrev (strIdx, cursorIdx, shd, leftStrings, M.startState, 1)
else
(* strIdx is in tl *)
(case stl of
stlhd :: stltl =>
let
val strIdx = strIdx - String.size shd
val leftStrings = shd :: leftStrings
in
M.fPrev
(strIdx, cursorIdx, stlhd, leftStrings, M.startState, 1)
end
| [] => cursorIdx)
end
| [] => cursorIdx
end
end

View File

@@ -1,38 +1,23 @@
structure ViWORDDfa = structure ViWORDDfa =
struct struct
val startState = 0w0 val startState: Word8.word = 0w0
val startNonBlankState = 0w1 val startNonBlankState: Word8.word = 0w1
val startSpaceState = 0w2 val startSpaceState: Word8.word = 0w2
val nonBlankAfterSpaceState = 0w4 val nonBlankAfterSpaceState: Word8.word = 0w4
fun makeStart i = fun makeStart i =
let let val chr = Char.chr i
val chr = Char.chr i in if Char.isSpace chr then startSpaceState else startNonBlankState
in
if Char.isSpace chr then
startSpaceState
else
startNonBlankState
end end
fun makeStartNonBlankState i = fun makeStartNonBlankState i =
let let val chr = Char.chr i
val chr = Char.chr i in if Char.isSpace chr then startSpaceState else startNonBlankState
in
if Char.isSpace chr then
startSpaceState
else
startNonBlankState
end end
fun makeStartSpace i = fun makeStartSpace i =
let let val chr = Char.chr i
val chr = Char.chr i in if Char.isSpace chr then startSpaceState else nonBlankAfterSpaceState
in
if Char.isSpace chr then
startSpaceState
else
nonBlankAfterSpaceState
end end
val startTable = Vector.tabulate (255, makeStart) val startTable = Vector.tabulate (255, makeStart)
@@ -50,69 +35,54 @@ struct
Vector.sub (currentTable, charIdx) Vector.sub (currentTable, charIdx)
end end
fun loopNextWORD (idx, absIdx, str, tl, currentState, counter) = structure TraverseWORD =
if idx = String.size str then MakeDfaLoop
case tl of (struct
str :: tl => val startState = startState
loopNextWORD (0, absIdx, str, tl, currentState, counter)
| [] =>
Int.max (absIdx - 2, 0)
else
let
val chr = String.sub (str, idx)
val newState = next (currentState, chr)
in
if newState = nonBlankAfterSpaceState then
if counter = 0 then
absIdx
else
(* new loop, so reset to start state and proceed *)
loopNextWORD (idx + 1, absIdx + 1, str, tl, startState, counter - 1)
else
loopNextWORD (idx + 1, absIdx + 1, str, tl, newState, counter)
end
fun loopPrevWORD (idx, absIdx, str, tl, currentState, counter) = fun fNext (idx, absIdx, str, tl, currentState, counter) =
if idx < 0 then if idx = String.size str then
case tl of case tl of
str :: tl => str :: tl => fNext (0, absIdx, str, tl, currentState, counter)
loopPrevWORD (String.size str - 1, absIdx, str, tl, currentState, counter) | [] => Int.max (absIdx - 2, 0)
| [] => 0 else
else let
let val chr = String.sub (str, idx)
val chr = String.sub (str, idx) val newState = next (currentState, chr)
val newState = next (currentState, chr) in
in if newState = nonBlankAfterSpaceState then
if newState = nonBlankAfterSpaceState then if counter - 1 = 0 then
if counter = 0 then absIdx
absIdx else
else (* new loop, so reset to start state and proceed *)
(* reset to start state and proceed *) fNext (idx + 1, absIdx + 1, str, tl, startState, counter - 1)
loopPrevWORD (idx - 1, absIdx - 1, str, tl, startState, counter - 1) else
else fNext (idx + 1, absIdx + 1, str, tl, newState, counter)
loopPrevWORD (idx - 1, absIdx - 1, str, tl, newState, counter) end
end
fun nextWORD (lineGap: LineGap.t, cursorIdx) = fun fPrev (idx, absIdx, str, tl, currentState, counter) =
let if idx < 0 then
val {rightStrings, idx = bufferIdx, ...} = lineGap case tl of
in str :: tl =>
case rightStrings of fPrev
shd :: stl => (String.size str - 1, absIdx, str, tl, currentState, counter)
let | [] => 0
(* convert absolute cursorIdx to idx relative to hd string *) else
val strIdx = cursorIdx - bufferIdx let
in val chr = String.sub (str, idx)
if strIdx < String.size shd then val newState = next (currentState, chr)
(* strIdx is in this string *) in
loopNextWORD (strIdx, cursorIdx, shd, stl, startState, 0) if newState = nonBlankAfterSpaceState then
else if counter - 1 = 0 then
(* strIdx is in tl *) absIdx
case stl of else
stlhd :: stltl => (* reset to start state and proceed *)
loopNextWORD (strIdx, cursorIdx, stlhd, stltl, startState, 0) fPrev (idx - 1, absIdx - 1, str, tl, startState, counter - 1)
| _ => cursorIdx else
end fPrev (idx - 1, absIdx - 1, str, tl, newState, counter)
| [] => cursorIdx end
end end)
val next = TraverseWORD.next
val prev = TraverseWORD.prev
end end

View File

@@ -886,7 +886,7 @@ struct
toNextWord (lineGap, cursorIdx, helpNextWord) toNextWord (lineGap, cursorIdx, helpNextWord)
(* equivalent of vi's 'W' command *) (* equivalent of vi's 'W' command *)
val nextWORD = ViWORDDfa.nextWORD val nextWORD = ViWORDDfa.next
fun helpPrevWord (strPos, str, absIdx, strTl, lineTl) = fun helpPrevWord (strPos, str, absIdx, strTl, lineTl) =
if strPos < 0 then if strPos < 0 then

View File

@@ -22,6 +22,7 @@ ann
in in
fcore/rect.sml fcore/rect.sml
fcore/text-builder.sml fcore/text-builder.sml
fcore/cursor-dfa/make-dfa-loop.sml
fcore/cursor-dfa/vi-WORD-dfa.sml fcore/cursor-dfa/vi-WORD-dfa.sml
end end
fcore/cursor.sml fcore/cursor.sml

View File

@@ -22,6 +22,7 @@ ann
in in
fcore/rect.sml fcore/rect.sml
fcore/text-builder.sml fcore/text-builder.sml
fcore/cursor-dfa/make-dfa-loop.sml
fcore/cursor-dfa/vi-WORD-dfa.sml fcore/cursor-dfa/vi-WORD-dfa.sml
end end
fcore/cursor.sml fcore/cursor.sml