2025-09-17 11:02:11 +01:00
|
|
|
structure ViHlDfa =
|
2025-09-17 02:31:38 +01:00
|
|
|
struct
|
|
|
|
|
val startState: Word8.word = 0w0
|
|
|
|
|
val notNewlineState: Word8.word = 0w1
|
|
|
|
|
val oneNewlineState: Word8.word = 0w2
|
2025-09-17 02:35:32 +01:00
|
|
|
val twoNewlineState: Word8.word = 0w3
|
2025-09-17 02:31:38 +01:00
|
|
|
|
|
|
|
|
fun makeStart i =
|
|
|
|
|
if Char.chr i = #"\n" then oneNewlineState else notNewlineState
|
|
|
|
|
|
2025-09-17 02:35:32 +01:00
|
|
|
fun makeOneNewline i =
|
2025-09-19 02:40:14 +01:00
|
|
|
if Char.chr i = #"\n" then twoNewlineState else notNewlineState
|
2025-09-17 02:31:38 +01:00
|
|
|
|
|
|
|
|
val startTable = Vector.tabulate (255, makeStart)
|
|
|
|
|
val notNewlineTable = startTable
|
|
|
|
|
val oneNewlineTable = Vector.tabulate (255, makeOneNewline)
|
2025-09-17 02:35:32 +01:00
|
|
|
val twoNewLineTable = startTable
|
2025-09-17 02:31:38 +01:00
|
|
|
|
2025-09-19 02:40:14 +01:00
|
|
|
val tables = #[startTable, notNewlineTable, oneNewlineTable, twoNewLineTable]
|
2025-09-17 02:31:38 +01:00
|
|
|
|
2025-09-17 03:52:31 +01:00
|
|
|
fun next (currentState, chr) =
|
|
|
|
|
let val table = Vector.sub (tables, Word8.toInt currentState)
|
|
|
|
|
in Vector.sub (table, Char.ord chr)
|
|
|
|
|
end
|
|
|
|
|
|
2025-09-17 02:31:38 +01:00
|
|
|
structure ViL =
|
2025-09-19 02:40:14 +01:00
|
|
|
MakeNextDfaLoopPlus1
|
2025-09-17 02:31:38 +01:00
|
|
|
(struct
|
|
|
|
|
val startState = startState
|
|
|
|
|
|
2025-09-17 03:52:31 +01:00
|
|
|
fun loop (idx, absIdx, str, tl, currentState, counter) =
|
|
|
|
|
if idx = String.size str then
|
|
|
|
|
case tl of
|
|
|
|
|
str :: tl => loop (0, absIdx, str, tl, currentState, counter)
|
|
|
|
|
| [] => absIdx
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val chr = String.sub (str, idx)
|
|
|
|
|
val newState = next (currentState, chr)
|
|
|
|
|
in
|
2025-09-19 02:40:14 +01:00
|
|
|
if newState = twoNewlineState then
|
|
|
|
|
if counter - 1 = 0 then
|
2025-09-17 03:52:31 +01:00
|
|
|
absIdx - 1
|
|
|
|
|
else
|
2025-09-19 03:04:10 +01:00
|
|
|
loop
|
|
|
|
|
( idx + 1
|
|
|
|
|
, absIdx + 1
|
|
|
|
|
, str
|
|
|
|
|
, tl
|
|
|
|
|
, oneNewlineState
|
|
|
|
|
, counter - 1
|
|
|
|
|
)
|
2025-09-19 02:40:14 +01:00
|
|
|
else if newState = notNewlineState then
|
|
|
|
|
if counter - 1 = 0 then
|
2025-09-17 03:52:31 +01:00
|
|
|
absIdx
|
|
|
|
|
else
|
|
|
|
|
loop (idx + 1, absIdx + 1, str, tl, startState, counter - 1)
|
|
|
|
|
else
|
|
|
|
|
loop (idx + 1, absIdx + 1, str, tl, newState, counter)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
val fStart = loop
|
2025-09-17 02:31:38 +01:00
|
|
|
end)
|
|
|
|
|
|
2025-09-17 10:45:37 +01:00
|
|
|
structure ViH =
|
|
|
|
|
MakePrevDfaLoop
|
|
|
|
|
(struct
|
|
|
|
|
val startState = startState
|
|
|
|
|
|
|
|
|
|
fun loop (idx, absIdx, str, tl, currentState, counter) =
|
|
|
|
|
if idx < 0 then
|
|
|
|
|
case tl of
|
|
|
|
|
str :: tl =>
|
|
|
|
|
loop
|
|
|
|
|
(String.size str - 1, absIdx, str, tl, currentState, counter)
|
2025-09-17 11:02:11 +01:00
|
|
|
| [] => 0
|
2025-09-17 10:45:37 +01:00
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val chr = String.sub (str, idx)
|
|
|
|
|
val newState = next (currentState, chr)
|
|
|
|
|
in
|
|
|
|
|
if newState = twoNewlineState then
|
2025-09-19 03:04:10 +01:00
|
|
|
if counter - 1 = 0 then
|
|
|
|
|
absIdx
|
|
|
|
|
else
|
|
|
|
|
loop
|
|
|
|
|
( idx - 1
|
|
|
|
|
, absIdx - 1
|
|
|
|
|
, str
|
|
|
|
|
, tl
|
|
|
|
|
, oneNewlineState
|
|
|
|
|
, counter - 1
|
|
|
|
|
)
|
2025-09-17 10:45:37 +01:00
|
|
|
else if newState = notNewlineState then
|
2025-09-19 02:40:14 +01:00
|
|
|
if counter - 1 = 0 then
|
2025-09-17 10:45:37 +01:00
|
|
|
absIdx
|
|
|
|
|
else
|
|
|
|
|
loop (idx - 1, absIdx - 1, str, tl, startState, counter - 1)
|
|
|
|
|
else
|
|
|
|
|
loop (idx - 1, absIdx - 1, str, tl, newState, counter)
|
|
|
|
|
end
|
|
|
|
|
|
2025-09-19 02:40:14 +01:00
|
|
|
fun fStart (idx, absIdx, str, tl, _, counter) =
|
|
|
|
|
if idx < 0 then
|
|
|
|
|
case tl of
|
|
|
|
|
str :: tl =>
|
|
|
|
|
loop
|
|
|
|
|
(String.size str - 1, absIdx, str, tl, startState, counter)
|
|
|
|
|
| [] => 0
|
|
|
|
|
else if String.sub (str, idx) = #"\n" then
|
|
|
|
|
loop (idx - 1, absIdx - 1, str, tl, oneNewlineState, counter)
|
|
|
|
|
else
|
|
|
|
|
loop (idx - 1, absIdx - 1, str, tl, startState, counter)
|
2025-09-17 10:45:37 +01:00
|
|
|
end)
|
|
|
|
|
|
2025-09-17 11:02:11 +01:00
|
|
|
val l = ViL.next
|
|
|
|
|
val h = ViH.prev
|
2025-09-17 02:31:38 +01:00
|
|
|
end
|