From cef8467ff6e157a2f3176e7613b8c1a65f035147 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Mon, 4 Aug 2025 03:23:27 +0100 Subject: [PATCH] begin functorising some char iterators which use if-expressions instead of DFAs --- fcore/cursor-dfa/make-dfa-loop.sml | 45 ++++++++++++++++++++++++++++++ fcore/cursor.sml | 38 ++----------------------- 2 files changed, 48 insertions(+), 35 deletions(-) diff --git a/fcore/cursor-dfa/make-dfa-loop.sml b/fcore/cursor-dfa/make-dfa-loop.sml index 00837e8..ce5bfc9 100644 --- a/fcore/cursor-dfa/make-dfa-loop.sml +++ b/fcore/cursor-dfa/make-dfa-loop.sml @@ -187,3 +187,48 @@ struct foldPrev (idx - 1, absIdx - 1, str, tl, newState, counter) end end + +signature MAKE_IF_CHAR_FOLDER = +sig + val fStart: int * string * int vector * int * string list * int vector list + -> int +end + +functor MakeIfCharFolderPrev(Fn: MAKE_IF_CHAR_FOLDER) = +struct + fun foldPrev (lineGap: LineGap.t, cursorIdx) = + let + val + {rightStrings, idx = bufferIdx, rightLines, leftStrings, leftLines, ...} = + lineGap + in + case (rightStrings, rightLines) of + (strHd :: strTl, lnHd :: lnTl) => + let + (* convert absolute cursorIdx to idx relative to hd string *) + val strIdx = cursorIdx - bufferIdx + in + if strIdx < String.size strHd then + (* strIdx is in this string *) + Fn.fStart (strIdx, strHd, lnHd, cursorIdx, leftStrings, leftLines) + else + (* strIdx must be in the strTl *) + (case (strTl, lnTl) of + (nestStrHd :: _, nestLnHd :: _) => + let + val strIdx = strIdx - String.size strHd + in + Fn.fStart + ( strIdx + , nestStrHd + , nestLnHd + , cursorIdx + , strHd :: leftStrings + , lnHd :: leftLines + ) + end + | (_, _) => cursorIdx) + end + | (_, _) => (* nowhere to go, so return cursorIdx *) cursorIdx + end +end diff --git a/fcore/cursor.sml b/fcore/cursor.sml index 82fd42b..38ba38a 100644 --- a/fcore/cursor.sml +++ b/fcore/cursor.sml @@ -36,41 +36,9 @@ struct else helpVi0 (absIdx - strPos, stl, ltl) - fun vi0 (lineGap: LineGap.t, cursorIdx) = - let - val - {rightStrings, idx = bufferIdx, rightLines, leftStrings, leftLines, ...} = - lineGap - in - case (rightStrings, rightLines) of - (strHd :: strTl, lnHd :: lnTl) => - let - (* convert absolute cursorIdx to idx relative to hd string *) - val strIdx = cursorIdx - bufferIdx - in - if strIdx < String.size strHd then - (* strIdx is in this string *) - startVi0 (strIdx, strHd, lnHd, cursorIdx, leftStrings, leftLines) - else - (* strIdx must be in the strTl *) - (case (strTl, lnTl) of - (nestStrHd :: _, nestLnHd :: _) => - let - val strIdx = strIdx - String.size strHd - in - startVi0 - ( strIdx - , nestStrHd - , nestLnHd - , cursorIdx - , strHd :: leftStrings - , lnHd :: leftLines - ) - end - | (_, _) => cursorIdx) - end - | (_, _) => (* nowhere to go, so return cursorIdx *) cursorIdx - end + structure Vi0 = MakeIfCharFolderPrev (struct val fStart = startVi0 end) + + val vi0 = Vi0.foldPrev fun helpViDlr (strPos, str, absIdx, strTl, lineTl) = if strPos = String.size str then