2025-09-28 22:01:44 +01:00
|
|
|
structure Nfa =
|
|
|
|
|
struct
|
2025-09-29 00:46:05 +01:00
|
|
|
datatype state = VALID of int | INVALID | UNTESTED
|
|
|
|
|
|
2025-09-28 22:01:44 +01:00
|
|
|
datatype regex =
|
2025-09-29 00:46:05 +01:00
|
|
|
CHAR_LITERAL of char * state
|
|
|
|
|
| CONCAT of (regex * state) list * state
|
|
|
|
|
| ALTERNATION of (regex * state) list * state
|
|
|
|
|
| ZERO_OR_ONE of regex * state
|
|
|
|
|
| ZERO_OR_MORE of regex * state
|
|
|
|
|
| ONE_OR_MORE of regex * state
|
|
|
|
|
| GROUP of regex * state
|
|
|
|
|
|
2025-09-29 08:33:10 +01:00
|
|
|
structure NfaMatch =
|
|
|
|
|
struct
|
|
|
|
|
(* test to see if NFA matches.
|
|
|
|
|
* Algorithm: Walk down to the leaves/subnodes of the regex tree
|
|
|
|
|
* and check if the current chr turns the leaf valid.
|
|
|
|
|
*
|
|
|
|
|
* When we hit a concatenation node, we check if the list's hd
|
|
|
|
|
* matches the same char.
|
|
|
|
|
* If it is valid and we have reached the last list element,
|
|
|
|
|
* then the whole concatenation is valid and we mark it as such.
|
|
|
|
|
* If it is valid while we still have other nodes to test,
|
|
|
|
|
* we filter the hd and the next loop with the next chr
|
|
|
|
|
* checks the node's tl.
|
|
|
|
|
* If it isn't valid, then we mark the whole concatenation as invalid.
|
|
|
|
|
*
|
|
|
|
|
* When we hit an alternation node, we check each list element at once
|
|
|
|
|
* to see if any of them are valid.
|
|
|
|
|
* We filter out the nodes in the alternation list that turn out to be
|
|
|
|
|
* invalid.
|
|
|
|
|
* At the end of the alternation loop, we check if all nodes are valid
|
|
|
|
|
* and then mark the alternation as valid if so.
|
|
|
|
|
* This helps us to implement "maximal munch",
|
|
|
|
|
* retrieving the maximum match instead of any other.
|
|
|
|
|
* *)
|
|
|
|
|
local
|
|
|
|
|
fun loop (tl, maxValid) =
|
|
|
|
|
case tl of
|
|
|
|
|
(_, VALID curValid) :: tl => loop (tl, Int.max (maxValid, curValid))
|
|
|
|
|
| (_, UNTESTED) :: _ => UNTESTED
|
|
|
|
|
| (_, INVALID) :: _ =>
|
|
|
|
|
raise Fail
|
|
|
|
|
"nfa.sml 24: \
|
|
|
|
|
\should not have INVALID state in acc"
|
|
|
|
|
| [] => VALID maxValid
|
|
|
|
|
in
|
|
|
|
|
fun getAlternationState acc =
|
|
|
|
|
case acc of
|
|
|
|
|
(_, VALID maxValid) :: tl => loop (tl, maxValid)
|
|
|
|
|
| (_, UNTESTED) :: _ => UNTESTED
|
|
|
|
|
| (_, INVALID) :: _ =>
|
|
|
|
|
raise Fail
|
|
|
|
|
"nfa.sml 26: \
|
|
|
|
|
\should not have INVALID state in acc"
|
|
|
|
|
| [] => UNTESTED
|
|
|
|
|
end
|
2025-09-29 01:45:28 +01:00
|
|
|
|
2025-09-29 12:18:45 +01:00
|
|
|
fun rebuildConcat (lst, chr, idx) =
|
|
|
|
|
case lst of
|
|
|
|
|
[(hd, _)] =>
|
|
|
|
|
let
|
|
|
|
|
val (hd: regex, state: state) = rebuild (hd, chr, idx)
|
|
|
|
|
val result = [(hd, state)]
|
|
|
|
|
val concat = CONCAT (result, state)
|
|
|
|
|
in
|
|
|
|
|
(concat, state)
|
|
|
|
|
end
|
|
|
|
|
| (hd, _) :: tl =>
|
|
|
|
|
let
|
|
|
|
|
val (hd, state) = rebuild (hd, chr, idx)
|
|
|
|
|
in
|
|
|
|
|
case state of
|
|
|
|
|
UNTESTED =>
|
|
|
|
|
let val concat = CONCAT ((hd, state) :: tl, UNTESTED)
|
|
|
|
|
in (concat, UNTESTED)
|
|
|
|
|
end
|
|
|
|
|
| INVALID =>
|
|
|
|
|
let val concat = CONCAT ([], INVALID)
|
|
|
|
|
in (concat, INVALID)
|
|
|
|
|
end
|
|
|
|
|
| VALID _ =>
|
|
|
|
|
let val concat = CONCAT (tl, UNTESTED)
|
|
|
|
|
in (concat, UNTESTED)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
| [] =>
|
|
|
|
|
(* should never occur *)
|
|
|
|
|
raise Fail
|
|
|
|
|
"nfa.sml, rebuildConcat 45: \
|
|
|
|
|
\should never try to rebuild empty concat list"
|
2025-09-29 08:33:10 +01:00
|
|
|
|
2025-09-29 12:18:45 +01:00
|
|
|
and rebuildAlternation (lst, chr, idx, acc) =
|
|
|
|
|
case lst of
|
|
|
|
|
[(hd, _)] =>
|
|
|
|
|
let
|
|
|
|
|
val (hd, state) = rebuild (hd, chr, idx)
|
|
|
|
|
val acc =
|
|
|
|
|
case state of
|
|
|
|
|
VALID _ => (hd, state) :: acc
|
|
|
|
|
| UNTESTED => (hd, state) :: acc
|
|
|
|
|
| INVALID => acc
|
|
|
|
|
val state = getAlternationState acc
|
|
|
|
|
in
|
|
|
|
|
(ALTERNATION (acc, state), state)
|
|
|
|
|
end
|
|
|
|
|
| (hd, _) :: tl =>
|
|
|
|
|
let
|
|
|
|
|
val (hd, state) = rebuild (hd, chr, idx)
|
|
|
|
|
val acc =
|
|
|
|
|
case state of
|
|
|
|
|
VALID _ => (hd, state) :: acc
|
|
|
|
|
| UNTESTED => (hd, state) :: acc
|
|
|
|
|
| INVALID => acc
|
|
|
|
|
in
|
|
|
|
|
rebuildAlternation (tl, chr, idx, acc)
|
|
|
|
|
end
|
|
|
|
|
| [] => (ALTERNATION ([], INVALID), INVALID)
|
2025-09-29 08:33:10 +01:00
|
|
|
|
2025-09-29 12:18:45 +01:00
|
|
|
and rebuild (nfa, chr, idx) =
|
|
|
|
|
case nfa of
|
|
|
|
|
CHAR_LITERAL (lit, UNTESTED) =>
|
|
|
|
|
if chr = lit then (CHAR_LITERAL (lit, VALID idx), VALID idx)
|
|
|
|
|
else (CHAR_LITERAL (lit, INVALID), INVALID)
|
|
|
|
|
| CHAR_LITERAL (lit, state) => (nfa, state)
|
2025-09-29 08:33:10 +01:00
|
|
|
|
2025-09-29 12:18:45 +01:00
|
|
|
| CONCAT (lst, UNTESTED) => rebuildConcat (lst, chr, idx)
|
|
|
|
|
| CONCAT (_, state) => (nfa, state)
|
2025-09-29 08:33:10 +01:00
|
|
|
|
2025-09-29 12:18:45 +01:00
|
|
|
| ALTERNATION (lst, UNTESTED) => rebuildAlternation (lst, chr, idx, [])
|
|
|
|
|
| ALTERNATION (_, state) => (nfa, state)
|
2025-09-29 08:33:10 +01:00
|
|
|
|
2025-09-29 12:18:45 +01:00
|
|
|
| _ => raise Fail "nfa.sml 69: not char literal or concat or alternation"
|
2025-09-29 08:33:10 +01:00
|
|
|
|
2025-09-29 12:18:45 +01:00
|
|
|
(* get all matches in string.
|
|
|
|
|
* Todo:
|
|
|
|
|
* - Append {start: int, finish: int} into PersistentVector instead
|
|
|
|
|
* - Search through gap buffer instead of string
|
|
|
|
|
* *)
|
|
|
|
|
local
|
2025-09-29 10:28:03 +01:00
|
|
|
fun loop (pos, str, nfa, origNfa, startPos, acc) =
|
2025-09-29 08:33:10 +01:00
|
|
|
if pos = String.size str then
|
2025-09-29 14:02:07 +01:00
|
|
|
acc
|
2025-09-29 08:33:10 +01:00
|
|
|
else
|
2025-09-29 00:46:05 +01:00
|
|
|
let
|
2025-09-29 08:33:10 +01:00
|
|
|
val chr = String.sub (str, pos)
|
|
|
|
|
val (nfa, state) = rebuild (nfa, chr, pos)
|
2025-09-29 00:46:05 +01:00
|
|
|
in
|
|
|
|
|
case state of
|
2025-09-29 10:28:03 +01:00
|
|
|
VALID finishIdx =>
|
2025-09-29 21:07:02 +01:00
|
|
|
let
|
|
|
|
|
val acc = PersistentVector.append (startPos, finishIdx, acc)
|
|
|
|
|
in
|
|
|
|
|
loop
|
|
|
|
|
(finishIdx + 1, str, origNfa, origNfa, finishIdx + 1, acc)
|
2025-09-29 10:28:03 +01:00
|
|
|
end
|
2025-09-29 00:46:05 +01:00
|
|
|
| INVALID =>
|
2025-09-29 08:33:10 +01:00
|
|
|
(* backtrack to another position in the string
|
|
|
|
|
* to see if the NFA matches that portion of the string *)
|
2025-09-29 21:07:02 +01:00
|
|
|
let val pos = startPos + 1
|
|
|
|
|
in loop (pos, str, origNfa, origNfa, pos, acc)
|
|
|
|
|
end
|
2025-09-29 10:28:03 +01:00
|
|
|
| UNTESTED => loop (pos + 1, str, nfa, origNfa, startPos, acc)
|
2025-09-29 00:46:05 +01:00
|
|
|
end
|
2025-09-29 08:33:10 +01:00
|
|
|
in
|
2025-09-29 10:28:03 +01:00
|
|
|
fun getMatches (str, nfa) =
|
|
|
|
|
loop (0, str, nfa, nfa, 0, PersistentVector.empty)
|
2025-09-29 08:33:10 +01:00
|
|
|
end
|
2025-09-29 12:18:45 +01:00
|
|
|
|
|
|
|
|
local
|
|
|
|
|
fun backtrackRange
|
|
|
|
|
(hd, tl, prevStrings, origNfa, acc, absIdx, startIdx, finishIdx) =
|
|
|
|
|
case prevStrings of
|
|
|
|
|
prevHd :: prevTl =>
|
|
|
|
|
let
|
|
|
|
|
val prevIdx = absIdx - String.size prevHd
|
|
|
|
|
val tl = hd :: tl
|
|
|
|
|
in
|
2025-09-29 13:13:14 +01:00
|
|
|
if startIdx < prevIdx then
|
2025-09-29 12:18:45 +01:00
|
|
|
(* keep backtracking *)
|
|
|
|
|
backtrackRange
|
|
|
|
|
( prevHd
|
|
|
|
|
, tl
|
|
|
|
|
, prevTl
|
|
|
|
|
, origNfa
|
|
|
|
|
, acc
|
|
|
|
|
, prevIdx
|
|
|
|
|
, startIdx
|
|
|
|
|
, finishIdx
|
|
|
|
|
)
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val strIdx = startIdx - prevIdx + 1
|
|
|
|
|
val absIdx = absIdx + strIdx
|
|
|
|
|
in
|
|
|
|
|
loop
|
|
|
|
|
( strIdx
|
|
|
|
|
, prevHd
|
|
|
|
|
, tl
|
|
|
|
|
, prevTl
|
|
|
|
|
, origNfa
|
|
|
|
|
, origNfa
|
|
|
|
|
, acc
|
|
|
|
|
, absIdx
|
|
|
|
|
, absIdx
|
|
|
|
|
, finishIdx
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
| [] => raise Fail "nfa.sml 188: should not backtrack to empty list"
|
|
|
|
|
|
|
|
|
|
and loop
|
|
|
|
|
( strIdx
|
|
|
|
|
, hd
|
|
|
|
|
, tl
|
|
|
|
|
, prevStrings
|
|
|
|
|
, nfa
|
|
|
|
|
, origNfa
|
|
|
|
|
, acc
|
|
|
|
|
, absIdx
|
|
|
|
|
, startIdx
|
|
|
|
|
, finishIdx
|
|
|
|
|
) =
|
|
|
|
|
if strIdx = String.size hd then
|
|
|
|
|
case tl of
|
|
|
|
|
newHd :: newTl =>
|
|
|
|
|
loop
|
|
|
|
|
( 0
|
|
|
|
|
, newHd
|
|
|
|
|
, newTl
|
|
|
|
|
, hd :: prevStrings
|
|
|
|
|
, nfa
|
|
|
|
|
, origNfa
|
|
|
|
|
, acc
|
|
|
|
|
, absIdx
|
|
|
|
|
, startIdx
|
|
|
|
|
, finishIdx
|
|
|
|
|
)
|
2025-09-29 14:02:07 +01:00
|
|
|
| [] => acc
|
2025-09-29 12:18:45 +01:00
|
|
|
else if absIdx > finishIdx then
|
2025-09-29 14:02:07 +01:00
|
|
|
acc
|
2025-09-29 12:18:45 +01:00
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val chr = String.sub (hd, strIdx)
|
|
|
|
|
val (nfa, state) = rebuild (nfa, chr, absIdx)
|
|
|
|
|
in
|
|
|
|
|
case state of
|
|
|
|
|
UNTESTED =>
|
|
|
|
|
loop
|
2025-09-29 13:13:14 +01:00
|
|
|
( strIdx + 1
|
2025-09-29 12:18:45 +01:00
|
|
|
, hd
|
|
|
|
|
, tl
|
|
|
|
|
, prevStrings
|
|
|
|
|
, nfa
|
|
|
|
|
, origNfa
|
|
|
|
|
, acc
|
|
|
|
|
, absIdx + 1
|
|
|
|
|
, startIdx
|
|
|
|
|
, finishIdx
|
|
|
|
|
)
|
2025-09-29 21:21:06 +01:00
|
|
|
| VALID foundIdx =>
|
2025-09-29 12:18:45 +01:00
|
|
|
let
|
2025-09-29 21:21:06 +01:00
|
|
|
val acc = PersistentVector.append (startIdx, foundIdx, acc)
|
2025-09-29 12:18:45 +01:00
|
|
|
in
|
|
|
|
|
loop
|
2025-09-29 21:29:03 +01:00
|
|
|
( strIdx + 1
|
2025-09-29 12:18:45 +01:00
|
|
|
, hd
|
|
|
|
|
, tl
|
|
|
|
|
, prevStrings
|
|
|
|
|
, origNfa
|
|
|
|
|
, origNfa
|
|
|
|
|
, acc
|
2025-09-29 21:21:06 +01:00
|
|
|
, foundIdx + 1
|
|
|
|
|
, foundIdx + 1
|
2025-09-29 12:18:45 +01:00
|
|
|
, finishIdx
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
| INVALID =>
|
|
|
|
|
let
|
|
|
|
|
val prevIdx = absIdx - strIdx
|
|
|
|
|
in
|
2025-09-29 13:13:14 +01:00
|
|
|
if startIdx < prevIdx then
|
2025-09-29 12:18:45 +01:00
|
|
|
backtrackRange
|
|
|
|
|
( hd
|
|
|
|
|
, tl
|
|
|
|
|
, prevStrings
|
|
|
|
|
, origNfa
|
|
|
|
|
, acc
|
|
|
|
|
, prevIdx
|
|
|
|
|
, startIdx
|
|
|
|
|
, finishIdx
|
|
|
|
|
)
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val strIdx = startIdx - prevIdx + 1
|
2025-09-29 13:13:14 +01:00
|
|
|
val absIdx = prevIdx + strIdx
|
2025-09-29 12:18:45 +01:00
|
|
|
in
|
|
|
|
|
loop
|
|
|
|
|
( strIdx
|
|
|
|
|
, hd
|
|
|
|
|
, tl
|
|
|
|
|
, prevStrings
|
|
|
|
|
, origNfa
|
|
|
|
|
, origNfa
|
|
|
|
|
, acc
|
|
|
|
|
, absIdx
|
2025-09-29 21:29:03 +01:00
|
|
|
, startIdx + 1
|
2025-09-29 12:18:45 +01:00
|
|
|
, finishIdx
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
in
|
|
|
|
|
(* Prerequisite: move buffer to 'start' parameter before calling *)
|
|
|
|
|
fun getMatchesInRange (startIdx, finishIdx, buffer: LineGap.t, nfa) =
|
|
|
|
|
let
|
|
|
|
|
val {rightStrings, idx = bufferIdx, ...} = buffer
|
|
|
|
|
val strIdx = startIdx - bufferIdx
|
|
|
|
|
in
|
|
|
|
|
case rightStrings of
|
|
|
|
|
hd :: tl =>
|
|
|
|
|
if strIdx < String.size hd then
|
|
|
|
|
(* strIdx is in this string *)
|
|
|
|
|
loop
|
|
|
|
|
( strIdx
|
|
|
|
|
, hd
|
|
|
|
|
, tl
|
|
|
|
|
, []
|
|
|
|
|
, nfa
|
|
|
|
|
, nfa
|
|
|
|
|
, PersistentVector.empty
|
|
|
|
|
, startIdx
|
|
|
|
|
, startIdx
|
|
|
|
|
, finishIdx
|
|
|
|
|
)
|
|
|
|
|
else
|
|
|
|
|
(* strIdx is in tl *)
|
|
|
|
|
(case tl of
|
|
|
|
|
stlhd :: stltl =>
|
|
|
|
|
let
|
|
|
|
|
val strIdx = strIdx - String.size hd
|
|
|
|
|
in
|
|
|
|
|
loop
|
|
|
|
|
( strIdx
|
|
|
|
|
, stlhd
|
|
|
|
|
, stltl
|
|
|
|
|
, []
|
|
|
|
|
, nfa
|
|
|
|
|
, nfa
|
|
|
|
|
, PersistentVector.empty
|
|
|
|
|
, startIdx
|
|
|
|
|
, startIdx
|
|
|
|
|
, finishIdx
|
|
|
|
|
)
|
|
|
|
|
end
|
2025-09-29 14:02:07 +01:00
|
|
|
| [] => PersistentVector.empty)
|
|
|
|
|
| [] => PersistentVector.empty
|
2025-09-29 12:18:45 +01:00
|
|
|
end
|
|
|
|
|
end
|
2025-09-29 08:33:10 +01:00
|
|
|
end
|
2025-09-29 00:46:05 +01:00
|
|
|
|
2025-09-29 08:33:10 +01:00
|
|
|
structure ParseNfa =
|
|
|
|
|
struct
|
2025-09-29 13:34:55 +01:00
|
|
|
(* parsing through precedence climbing algorithm. *)
|
2025-09-29 00:46:05 +01:00
|
|
|
|
2025-09-29 08:33:10 +01:00
|
|
|
val groupLevel = 1
|
|
|
|
|
val postfixLevel = 2
|
|
|
|
|
val concatLevel = 3
|
|
|
|
|
val altLevel = 4
|
2025-09-29 01:06:15 +01:00
|
|
|
|
2025-09-29 08:33:10 +01:00
|
|
|
local
|
|
|
|
|
fun loop (pos, str, openParens, closeParens) =
|
|
|
|
|
if pos = String.size str then
|
2025-09-29 13:34:55 +01:00
|
|
|
NONE
|
2025-09-29 08:33:10 +01:00
|
|
|
else
|
|
|
|
|
case String.sub (str, pos) of
|
|
|
|
|
#"(" => loop (pos + 1, str, openParens + 1, closeParens)
|
|
|
|
|
| #")" =>
|
2025-09-29 13:34:55 +01:00
|
|
|
if closeParens + 1 = openParens then SOME pos
|
2025-09-29 08:33:10 +01:00
|
|
|
else loop (pos + 1, str, openParens, closeParens + 1)
|
|
|
|
|
| _ => loop (pos + 1, str, openParens, closeParens)
|
|
|
|
|
in
|
|
|
|
|
fun getRightParenIdx (pos, str) = loop (pos, str, 1, 0)
|
|
|
|
|
end
|
2025-09-29 00:46:05 +01:00
|
|
|
|
2025-09-29 13:34:55 +01:00
|
|
|
fun climb (pos, str, lhs, level) : (int * regex) option =
|
2025-09-29 00:46:05 +01:00
|
|
|
if pos = String.size str then
|
2025-09-29 13:34:55 +01:00
|
|
|
SOME (pos, lhs)
|
2025-09-29 00:46:05 +01:00
|
|
|
else
|
2025-09-29 08:33:10 +01:00
|
|
|
case String.sub (str, pos) of
|
2025-09-29 13:34:55 +01:00
|
|
|
#")" => SOME (pos + 1, lhs)
|
2025-09-29 08:33:10 +01:00
|
|
|
| #"(" =>
|
|
|
|
|
if level < groupLevel then
|
2025-09-29 13:34:55 +01:00
|
|
|
SOME (pos, lhs)
|
2025-09-29 08:33:10 +01:00
|
|
|
else
|
2025-09-29 13:34:55 +01:00
|
|
|
(case getRightParenIdx (pos + 1, str) of
|
|
|
|
|
SOME groupEndIdx =>
|
|
|
|
|
let
|
|
|
|
|
val substr = String.substring
|
|
|
|
|
(str, pos + 1, groupEndIdx - pos - 1)
|
|
|
|
|
in
|
|
|
|
|
(case parse substr of
|
|
|
|
|
SOME rhs =>
|
|
|
|
|
let
|
|
|
|
|
val rhs = GROUP (rhs, UNTESTED)
|
|
|
|
|
val result = CONCAT
|
|
|
|
|
([(lhs, UNTESTED), (rhs, UNTESTED)], UNTESTED)
|
|
|
|
|
in
|
|
|
|
|
climb (groupEndIdx + 1, str, result, groupLevel)
|
|
|
|
|
end
|
|
|
|
|
| NONE => NONE)
|
|
|
|
|
end
|
|
|
|
|
| NONE => NONE)
|
2025-09-29 08:33:10 +01:00
|
|
|
| #"|" =>
|
|
|
|
|
if level < altLevel then
|
2025-09-29 13:34:55 +01:00
|
|
|
SOME (pos, lhs)
|
|
|
|
|
else if pos + 1 < String.size str then
|
2025-09-29 08:33:10 +01:00
|
|
|
let
|
|
|
|
|
val chr = String.sub (str, pos + 1)
|
|
|
|
|
val chr = CHAR_LITERAL (chr, UNTESTED)
|
|
|
|
|
in
|
2025-09-29 13:34:55 +01:00
|
|
|
case climb (pos + 2, str, chr, altLevel) of
|
|
|
|
|
SOME (pos, rhs) =>
|
|
|
|
|
let
|
|
|
|
|
val result =
|
|
|
|
|
case rhs of
|
|
|
|
|
ALTERNATION (lst, state) =>
|
|
|
|
|
ALTERNATION ((lhs, UNTESTED) :: lst, UNTESTED)
|
|
|
|
|
| _ =>
|
|
|
|
|
ALTERNATION
|
|
|
|
|
([(lhs, UNTESTED), (rhs, UNTESTED)], UNTESTED)
|
|
|
|
|
in
|
|
|
|
|
SOME (pos, result)
|
|
|
|
|
end
|
|
|
|
|
| NONE => NONE
|
2025-09-29 08:33:10 +01:00
|
|
|
end
|
2025-09-29 13:34:55 +01:00
|
|
|
else
|
|
|
|
|
NONE
|
2025-09-29 08:33:10 +01:00
|
|
|
| #"?" =>
|
|
|
|
|
if level < postfixLevel then
|
2025-09-29 13:34:55 +01:00
|
|
|
SOME (pos, lhs)
|
2025-09-29 08:33:10 +01:00
|
|
|
else
|
|
|
|
|
let val lhs = ZERO_OR_ONE (lhs, UNTESTED)
|
|
|
|
|
in climb (pos + 1, str, lhs, postfixLevel)
|
|
|
|
|
end
|
|
|
|
|
| #"*" =>
|
|
|
|
|
if level < postfixLevel then
|
2025-09-29 13:34:55 +01:00
|
|
|
SOME (pos, lhs)
|
2025-09-29 08:33:10 +01:00
|
|
|
else
|
|
|
|
|
let val lhs = ZERO_OR_MORE (lhs, UNTESTED)
|
|
|
|
|
in climb (pos + 1, str, lhs, postfixLevel)
|
|
|
|
|
end
|
|
|
|
|
| #"+" =>
|
|
|
|
|
if level < postfixLevel then
|
2025-09-29 13:34:55 +01:00
|
|
|
SOME (pos, lhs)
|
2025-09-29 08:33:10 +01:00
|
|
|
else
|
|
|
|
|
let val lhs = ONE_OR_MORE (lhs, UNTESTED)
|
|
|
|
|
in climb (pos + 1, str, lhs, postfixLevel)
|
|
|
|
|
end
|
|
|
|
|
| chr =>
|
|
|
|
|
if level < concatLevel then
|
2025-09-29 13:34:55 +01:00
|
|
|
SOME (pos, lhs)
|
2025-09-29 08:33:10 +01:00
|
|
|
else
|
2025-09-29 13:34:55 +01:00
|
|
|
case
|
|
|
|
|
climb (pos + 1, str, CHAR_LITERAL (chr, UNTESTED), concatLevel)
|
|
|
|
|
of
|
|
|
|
|
SOME (pos, rhs) =>
|
|
|
|
|
let
|
|
|
|
|
val result =
|
|
|
|
|
case rhs of
|
|
|
|
|
CONCAT (lst, _) =>
|
|
|
|
|
CONCAT ((lhs, UNTESTED) :: lst, UNTESTED)
|
|
|
|
|
| _ =>
|
|
|
|
|
CONCAT ([(lhs, UNTESTED), (rhs, UNTESTED)], UNTESTED)
|
|
|
|
|
in
|
|
|
|
|
SOME (pos, result)
|
|
|
|
|
end
|
|
|
|
|
| NONE => NONE
|
2025-09-29 00:46:05 +01:00
|
|
|
|
2025-09-29 08:33:10 +01:00
|
|
|
and loop (pos, str, ast) =
|
2025-09-28 22:01:44 +01:00
|
|
|
if pos = String.size str then
|
2025-09-29 13:34:55 +01:00
|
|
|
SOME ast
|
2025-09-28 22:01:44 +01:00
|
|
|
else
|
2025-09-29 13:34:55 +01:00
|
|
|
case climb (pos, str, ast, altLevel) of
|
|
|
|
|
SOME (pos, ast) => loop (pos, str, ast)
|
|
|
|
|
| NONE => NONE
|
2025-09-28 22:01:44 +01:00
|
|
|
|
2025-09-29 08:33:10 +01:00
|
|
|
and parse str =
|
2025-09-29 13:34:55 +01:00
|
|
|
if String.size str > 0 then
|
|
|
|
|
let
|
|
|
|
|
val chr = String.sub (str, 0)
|
|
|
|
|
val chr = CHAR_LITERAL (chr, UNTESTED)
|
|
|
|
|
in
|
|
|
|
|
loop (1, str, chr)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
NONE
|
2025-09-29 08:33:10 +01:00
|
|
|
end
|
2025-09-28 22:01:44 +01:00
|
|
|
|
2025-09-29 08:33:10 +01:00
|
|
|
val parse = ParseNfa.parse
|
2025-09-29 10:28:03 +01:00
|
|
|
val getMatches = NfaMatch.getMatches
|
2025-09-29 13:13:14 +01:00
|
|
|
val getMatchesInRange = NfaMatch.getMatchesInRange
|
2025-09-28 22:01:44 +01:00
|
|
|
end
|