From 1c107b0d72c50cc91c9fd18ae07cec44d2123e9f Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Thu, 2 Oct 2025 05:00:23 +0100 Subject: [PATCH] add function to get path to a particular position, for the sake of finding followPpos of a particular node --- fcore/search-list/nfa.sml | 44 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/fcore/search-list/nfa.sml b/fcore/search-list/nfa.sml index 92695f7..f40d391 100644 --- a/fcore/search-list/nfa.sml +++ b/fcore/search-list/nfa.sml @@ -225,6 +225,28 @@ struct | ONE_OR_MORE regex => firstpos (regex, acc) | GROUP regex => firstpos (regex, acc) + fun firstposWithChar (tree, acc) = + case tree of + CHAR_LITERAL {position, char} => + {position = position, char = Char.ord char} :: acc + | WILDCARD position => {position = position, char = ~1} :: acc + + | CONCAT {l, r, ...} => + if isNullable l then + let val acc = firstposWithChar (l, acc) + in firstposWithChar (r, acc) + end + else + firstposWithChar (l, acc) + | ALTERNATION {l, r, ...} => + let val acc = firstposWithChar (l, acc) + in firstposWithChar (r, acc) + end + | ZERO_OR_ONE regex => firstposWithChar (regex, acc) + | ZERO_OR_MORE regex => firstposWithChar (regex, acc) + | ONE_OR_MORE regex => firstposWithChar (regex, acc) + | GROUP regex => firstposWithChar (regex, acc) + fun lastpos (tree, acc) = case tree of CHAR_LITERAL {position, ...} => position :: acc @@ -254,6 +276,28 @@ struct | ZERO_OR_ONE r => firstpos (r, []) | ONE_OR_MORE r => firstpos (r, []) | _ => [] + + (* for help finding followpos of a particular node. + * Get list of concat and loop nodes to pos. + * Direct ancestor is at front of list, and furthest ancestor + * is at end of list. *) + fun getConcatAndLoopsToPos (tree, pos, acc) = + case tree of + CONCAT {l, r, leftMaxState, rightMaxState} => + if pos <= leftMaxState then + getConcatAndLoopsToPos (l, pos, tree :: acc) + else + getConcatAndLoopsToPos (r, pos, tree :: acc) + | ZERO_OR_ONE r => getConcatAndLoopsToPos (r, pos, tree :: acc) + | ZERO_OR_MORE r => getConcatAndLoopsToPos (r, pos, tree :: acc) + | ONE_OR_MORE r => getConcatAndLoopsToPos (r, pos, tree :: acc) + + | ALTERNATION {l, r, leftMaxState, rightMaxState} => + if pos <= leftMaxState then getConcatAndLoopsToPos (l, pos, acc) + else getConcatAndLoopsToPos (r, pos, acc) + | CHAR_LITERAL _ => acc + | WILDCARD _ => acc + | GROUP r => getConcatAndLoopsToPos (r, pos, acc) end fun parse str =