2025-07-20 09:12:23 +01:00
|
|
|
structure ViWORDDfa =
|
|
|
|
|
struct
|
2025-07-20 09:39:17 +01:00
|
|
|
val startState: Word8.word = 0w0
|
|
|
|
|
val startNonBlankState: Word8.word = 0w1
|
|
|
|
|
val startSpaceState: Word8.word = 0w2
|
2025-07-20 13:09:21 +01:00
|
|
|
val nonBlankAfterSpaceState: Word8.word = 0w3
|
|
|
|
|
val spaceAfterNonBlankState = 0w4
|
2025-07-20 09:12:23 +01:00
|
|
|
|
|
|
|
|
fun makeStart i =
|
2025-07-20 09:39:17 +01:00
|
|
|
let val chr = Char.chr i
|
|
|
|
|
in if Char.isSpace chr then startSpaceState else startNonBlankState
|
2025-07-20 09:12:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun makeStartNonBlankState i =
|
2025-07-20 09:39:17 +01:00
|
|
|
let val chr = Char.chr i
|
2025-07-20 13:09:21 +01:00
|
|
|
in if Char.isSpace chr then spaceAfterNonBlankState else startNonBlankState
|
2025-07-20 09:12:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun makeStartSpace i =
|
2025-07-20 09:39:17 +01:00
|
|
|
let val chr = Char.chr i
|
|
|
|
|
in if Char.isSpace chr then startSpaceState else nonBlankAfterSpaceState
|
2025-07-20 09:12:23 +01:00
|
|
|
end
|
|
|
|
|
|
2025-07-20 13:09:21 +01:00
|
|
|
fun makeNonBlankAfterSpace i =
|
|
|
|
|
let
|
|
|
|
|
val chr = Char.chr i
|
|
|
|
|
in
|
|
|
|
|
if Char.isSpace chr then spaceAfterNonBlankState
|
|
|
|
|
else nonBlankAfterSpaceState
|
|
|
|
|
end
|
|
|
|
|
|
2025-07-20 09:12:23 +01:00
|
|
|
val startTable = Vector.tabulate (255, makeStart)
|
|
|
|
|
val startNonBlankTable = Vector.tabulate (255, makeStartNonBlankState)
|
|
|
|
|
val startSpaceTable = Vector.tabulate (255, makeStartSpace)
|
2025-07-20 13:09:21 +01:00
|
|
|
val nonBlankAfterSpaceTable = Vector.tabulate (255, makeNonBlankAfterSpace)
|
|
|
|
|
val spaceAfterNonBlankTable = nonBlankAfterSpaceTable
|
2025-07-20 09:12:23 +01:00
|
|
|
|
2025-07-20 13:09:21 +01:00
|
|
|
val tables =
|
|
|
|
|
#[ startTable
|
|
|
|
|
, startNonBlankTable
|
|
|
|
|
, startSpaceTable
|
|
|
|
|
, nonBlankAfterSpaceTable
|
|
|
|
|
, spaceAfterNonBlankTable
|
|
|
|
|
]
|
2025-07-20 09:12:23 +01:00
|
|
|
|
|
|
|
|
fun next (currentState, currentChar) =
|
|
|
|
|
let
|
|
|
|
|
val currentState = Word8.toInt currentState
|
|
|
|
|
val currentTable = Vector.sub (tables, currentState)
|
|
|
|
|
val charIdx = Char.ord currentChar
|
|
|
|
|
in
|
|
|
|
|
Vector.sub (currentTable, charIdx)
|
|
|
|
|
end
|
|
|
|
|
|
2025-07-20 09:56:32 +01:00
|
|
|
structure StartOfNextWORD =
|
|
|
|
|
MakeNextDfaLoop
|
2025-07-20 09:39:17 +01:00
|
|
|
(struct
|
|
|
|
|
val startState = startState
|
2025-07-20 09:12:23 +01:00
|
|
|
|
2025-07-20 09:56:32 +01:00
|
|
|
fun fStart (idx, absIdx, str, tl, currentState, counter) =
|
2025-07-20 09:39:17 +01:00
|
|
|
if idx = String.size str then
|
|
|
|
|
case tl of
|
2025-07-20 09:56:32 +01:00
|
|
|
str :: tl => fStart (0, absIdx, str, tl, currentState, counter)
|
2025-07-20 09:39:17 +01:00
|
|
|
| [] => 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 - 1 = 0 then
|
|
|
|
|
absIdx
|
|
|
|
|
else
|
|
|
|
|
(* new loop, so reset to start state and proceed *)
|
2025-07-20 09:56:32 +01:00
|
|
|
fStart
|
|
|
|
|
(idx + 1, absIdx + 1, str, tl, startState, counter - 1)
|
2025-07-20 09:39:17 +01:00
|
|
|
else
|
2025-07-20 09:56:32 +01:00
|
|
|
fStart (idx + 1, absIdx + 1, str, tl, newState, counter)
|
2025-07-20 09:39:17 +01:00
|
|
|
end
|
2025-07-20 09:56:32 +01:00
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
structure EndOfPrevWORD =
|
|
|
|
|
MakePrevDfaLoop
|
|
|
|
|
(struct
|
|
|
|
|
val startState = startState
|
2025-07-20 09:12:23 +01:00
|
|
|
|
2025-07-20 09:56:32 +01:00
|
|
|
fun fStart (idx, absIdx, str, tl, currentState, counter) =
|
2025-07-20 09:39:17 +01:00
|
|
|
if idx < 0 then
|
|
|
|
|
case tl of
|
|
|
|
|
str :: tl =>
|
2025-07-20 09:56:32 +01:00
|
|
|
fStart
|
2025-07-20 09:39:17 +01:00
|
|
|
(String.size str - 1, absIdx, str, tl, currentState, counter)
|
|
|
|
|
| [] => 0
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val chr = String.sub (str, idx)
|
|
|
|
|
val newState = next (currentState, chr)
|
|
|
|
|
in
|
|
|
|
|
if newState = nonBlankAfterSpaceState then
|
|
|
|
|
if counter - 1 = 0 then
|
|
|
|
|
absIdx
|
|
|
|
|
else
|
|
|
|
|
(* reset to start state and proceed *)
|
2025-07-20 09:56:32 +01:00
|
|
|
fStart
|
|
|
|
|
(idx - 1, absIdx - 1, str, tl, startState, counter - 1)
|
2025-07-20 09:39:17 +01:00
|
|
|
else
|
2025-07-20 09:56:32 +01:00
|
|
|
fStart (idx - 1, absIdx - 1, str, tl, newState, counter)
|
2025-07-20 09:39:17 +01:00
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
|
2025-07-20 13:39:48 +01:00
|
|
|
structure StartOfCurrentWORD =
|
|
|
|
|
MakePrevDfaLoopMinus1
|
|
|
|
|
(struct
|
|
|
|
|
val startState = startState
|
2025-07-20 13:09:21 +01:00
|
|
|
|
2025-07-20 13:39:48 +01:00
|
|
|
fun fStart (idx, absIdx, str, tl, currentState, counter) =
|
|
|
|
|
if idx < 0 then
|
|
|
|
|
case tl of
|
|
|
|
|
str :: tl =>
|
|
|
|
|
fStart
|
|
|
|
|
(String.size str - 1, absIdx, str, tl, currentState, counter)
|
|
|
|
|
| [] => 0
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val chr = String.sub (str, idx)
|
|
|
|
|
val newState = next (currentState, chr)
|
|
|
|
|
in
|
|
|
|
|
if newState = spaceAfterNonBlankState then
|
|
|
|
|
if counter - 1 = 0 then
|
|
|
|
|
absIdx + 1
|
|
|
|
|
else
|
|
|
|
|
fStart
|
|
|
|
|
(idx - 1, absIdx - 1, str, tl, startState, counter - 1)
|
|
|
|
|
else
|
|
|
|
|
fStart (idx - 1, absIdx - 1, str, tl, newState, counter)
|
|
|
|
|
end
|
|
|
|
|
end)
|
2025-07-20 13:28:40 +01:00
|
|
|
|
2025-07-20 14:00:59 +01:00
|
|
|
structure EndOfCurrentWORD =
|
|
|
|
|
MakeNextDfaLoopPlus1
|
|
|
|
|
(struct
|
|
|
|
|
val startState = startState
|
|
|
|
|
|
|
|
|
|
fun fStart (idx, absIdx, str, tl, currentState, counter) =
|
|
|
|
|
if idx = String.size str then
|
|
|
|
|
case tl of
|
|
|
|
|
str :: tl => fStart (0, absIdx, str, tl, currentState, counter)
|
|
|
|
|
| [] => Int.max (0, absIdx - 2)
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val chr = String.sub (str, idx)
|
|
|
|
|
val newState = next (currentState, chr)
|
|
|
|
|
in
|
|
|
|
|
if newState = spaceAfterNonBlankState then
|
|
|
|
|
if counter - 1 = 0 then
|
|
|
|
|
Int.max (0, absIdx - 1)
|
|
|
|
|
else
|
|
|
|
|
fStart
|
|
|
|
|
(idx + 1, absIdx + 1, str, tl, startState, counter - 1)
|
|
|
|
|
else
|
|
|
|
|
fStart (idx + 1, absIdx + 1, str, tl, newState, counter)
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
(* W *)
|
2025-07-20 13:39:48 +01:00
|
|
|
val startOfNextWORD = StartOfNextWORD.next
|
2025-07-20 14:00:59 +01:00
|
|
|
(* gE *)
|
2025-07-20 13:39:48 +01:00
|
|
|
val endOfPrevWORD = EndOfPrevWORD.prev
|
2025-07-20 14:00:59 +01:00
|
|
|
(* B *)
|
2025-07-20 13:39:48 +01:00
|
|
|
val startOfCurrentWORD = StartOfCurrentWORD.prev
|
2025-07-20 14:00:59 +01:00
|
|
|
(* E *)
|
|
|
|
|
val endOfCurrentWORD = EndOfCurrentWORD.next
|
2025-07-20 09:12:23 +01:00
|
|
|
end
|