fix bugs in vi-l-dfa (we want to stop looping when the counter is ~1, because the starting character will likely be final/a not-newline, and we want to loop at least once; also, we want to special case 'twoNewlineState' as a final case which causes us to go backwards by 1 instead of treating 'oneNewlineState' as a final/special case). We also modify other code to use the new vi-l implementation in the program.

This commit is contained in:
2025-09-17 03:52:31 +01:00
parent 5174a5b0a4
commit 9ddb5f68d7
6 changed files with 40 additions and 68 deletions

View File

@@ -18,24 +18,41 @@ struct
val tables = #[startTable, notNewlineTable, oneNewlineTable, twoNewLineTable]
fun next (currentState, chr) =
let val table = Vector.sub (tables, Word8.toInt currentState)
in Vector.sub (table, Char.ord chr)
end
structure ViL =
MakeNextDfaLoop
(struct
val startState = startState
structure Folder =
MakeCharFolderNext
(struct
val startState = startState
val tables = tables
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
if newState = twoNewlineState then
if counter - 1 = ~1 then
absIdx - 1
else
loop (idx + 1, absIdx + 1, str, tl, startState, counter - 1)
else if newState = notNewlineState then
if counter - 1 = ~1 then
absIdx
else
loop (idx + 1, absIdx + 1, str, tl, startState, counter - 1)
else
loop (idx + 1, absIdx + 1, str, tl, newState, counter)
end
fun finish x = x
fun isFinal currentState =
currentState = notNewlineState
orelse currentState = oneNewlineState
end)
val fStart = Folder.foldNext
val fStart = loop
end)
val next = ViL.next