From d75b1a18ff1818a62d160b3b6f459b5dbf12ef32 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Sun, 28 Sep 2025 22:23:48 +0100 Subject: [PATCH] flatten repeated concatenations and alternations into a single list when possible --- fcore/search-list/nfa.sml | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/fcore/search-list/nfa.sml b/fcore/search-list/nfa.sml index 282d0f8..a3b34a0 100644 --- a/fcore/search-list/nfa.sml +++ b/fcore/search-list/nfa.sml @@ -1,9 +1,9 @@ structure Nfa = struct datatype regex = - CONCAT of regex * regex - | CHAR_LITERAL of char - | ALTERNATION of regex * regex + CHAR_LITERAL of char + | CONCAT of regex list + | ALTERNATION of regex list | ZERO_OR_ONE of regex | ZERO_OR_MORE of regex | ONE_OR_MORE of regex @@ -45,7 +45,7 @@ struct (str, pos + 1, groupEndIdx - pos - 1) val rhs = climb substr val rhs = GROUP rhs - val result = CONCAT (lhs, rhs) + val result = CONCAT [lhs, rhs] in helpClimb (groupEndIdx + 1, str, result, groupLevel) end @@ -57,7 +57,10 @@ struct val chr = String.sub (str, pos + 1) val chr = CHAR_LITERAL chr val (pos, rhs) = helpClimb (pos + 2, str, chr, altLevel) - val result = ALTERNATION (lhs, rhs) + val result = + case rhs of + ALTERNATION lst => ALTERNATION (lhs :: lst) + | _ => ALTERNATION [lhs, rhs] in (pos, result) end @@ -89,7 +92,10 @@ struct let val chr = CHAR_LITERAL chr val (pos, rhs) = helpClimb (pos + 1, str, chr, concatLevel) - val result = CONCAT (lhs, rhs) + val result = + case rhs of + CONCAT lst => CONCAT (lhs :: lst) + | _ => CONCAT [lhs, rhs] in (pos, result) end