2025-10-07 14:05:45 +01:00
|
|
|
signature DFA_GEN_PARAMS =
|
|
|
|
|
sig
|
|
|
|
|
val endMarker: char
|
|
|
|
|
val charIsEqual: char * char -> bool
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
signature DFA_GEN =
|
|
|
|
|
sig
|
|
|
|
|
type dfa = int vector vector
|
|
|
|
|
type dfa_state = int
|
|
|
|
|
|
|
|
|
|
val fromString: string -> dfa
|
|
|
|
|
|
|
|
|
|
val nextState: dfa * dfa_state * char -> dfa_state
|
|
|
|
|
val isFinal: dfa * dfa_state -> bool
|
|
|
|
|
val isDead: dfa_state -> bool
|
2025-10-09 05:34:32 +01:00
|
|
|
|
|
|
|
|
val matchString: dfa * string -> (int * int) list
|
2025-10-07 14:05:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
functor MakeDfaGen(Fn: DFA_GEN_PARAMS): DFA_GEN =
|
2025-09-28 22:01:44 +01:00
|
|
|
struct
|
2025-10-07 14:05:45 +01:00
|
|
|
datatype parse_tree =
|
2025-10-11 11:32:30 +01:00
|
|
|
CHAR_LITERAL of {char: char, position: int}
|
|
|
|
|
| WILDCARD of int
|
|
|
|
|
| IS_ANY_CHARACTER of {chars: char vector, position: int}
|
|
|
|
|
| NOT_ANY_CHARACTER of {chars: char vector, position: int}
|
2025-10-07 14:05:45 +01:00
|
|
|
| CONCAT of
|
2025-10-11 11:32:30 +01:00
|
|
|
{l: parse_tree, r: parse_tree, leftMaxState: int, rightMaxState: int}
|
2025-10-07 14:05:45 +01:00
|
|
|
| ALTERNATION of
|
2025-10-11 11:32:30 +01:00
|
|
|
{l: parse_tree, r: parse_tree, leftMaxState: int, rightMaxState: int}
|
2025-10-07 14:05:45 +01:00
|
|
|
| ZERO_OR_ONE of parse_tree
|
|
|
|
|
| ZERO_OR_MORE of parse_tree
|
|
|
|
|
| ONE_OR_MORE of parse_tree
|
|
|
|
|
| GROUP of parse_tree
|
2025-10-07 08:57:01 +01:00
|
|
|
|
2025-10-10 01:43:24 +01:00
|
|
|
fun isNullable tree =
|
|
|
|
|
case tree of
|
|
|
|
|
CHAR_LITERAL _ => false
|
|
|
|
|
| WILDCARD _ => false
|
|
|
|
|
| IS_ANY_CHARACTER _ => false
|
|
|
|
|
| NOT_ANY_CHARACTER _ => false
|
|
|
|
|
|
|
|
|
|
| CONCAT {l, r, ...} => isNullable l andalso isNullable r
|
|
|
|
|
| ALTERNATION {l, r, ...} => isNullable l orelse isNullable r
|
|
|
|
|
|
|
|
|
|
| ZERO_OR_ONE _ => true
|
|
|
|
|
| ZERO_OR_MORE _ => true
|
|
|
|
|
| ONE_OR_MORE regex => isNullable regex
|
|
|
|
|
|
|
|
|
|
| GROUP regex => isNullable regex
|
|
|
|
|
|
|
|
|
|
fun firstpos (tree, acc) =
|
|
|
|
|
case tree of
|
|
|
|
|
CHAR_LITERAL {position, ...} => position :: acc
|
|
|
|
|
| IS_ANY_CHARACTER {position, ...} => position :: acc
|
|
|
|
|
| NOT_ANY_CHARACTER {position, ...} => position :: acc
|
2025-10-11 11:32:30 +01:00
|
|
|
| WILDCARD i => i :: acc
|
|
|
|
|
|
|
|
|
|
| CONCAT {l, r, ...} =>
|
|
|
|
|
if isNullable l then
|
|
|
|
|
let val acc = firstpos (l, acc)
|
|
|
|
|
in firstpos (r, acc)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
firstpos (l, acc)
|
|
|
|
|
| ALTERNATION {l, r, ...} =>
|
|
|
|
|
let val acc = firstpos (l, acc)
|
|
|
|
|
in firstpos (r, acc)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
| ZERO_OR_ONE regex => firstpos (regex, acc)
|
|
|
|
|
| ZERO_OR_MORE regex => firstpos (regex, acc)
|
|
|
|
|
| ONE_OR_MORE regex => firstpos (regex, acc)
|
|
|
|
|
| GROUP regex => firstpos (regex, acc)
|
2025-10-10 01:43:24 +01:00
|
|
|
|
|
|
|
|
fun lastpos (tree, acc) =
|
|
|
|
|
case tree of
|
|
|
|
|
CHAR_LITERAL {position, ...} => position :: acc
|
|
|
|
|
| IS_ANY_CHARACTER {position, ...} => position :: acc
|
|
|
|
|
| NOT_ANY_CHARACTER {position, ...} => position :: acc
|
2025-10-11 11:32:30 +01:00
|
|
|
| WILDCARD i => i :: acc
|
|
|
|
|
|
|
|
|
|
| CONCAT {l, r, ...} =>
|
|
|
|
|
if isNullable r then
|
|
|
|
|
let val acc = lastpos (l, acc)
|
|
|
|
|
in lastpos (r, acc)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
lastpos (r, acc)
|
|
|
|
|
| ALTERNATION {l, r, ...} =>
|
|
|
|
|
let val acc = lastpos (l, acc)
|
|
|
|
|
in lastpos (r, acc)
|
|
|
|
|
end
|
2025-10-10 01:43:24 +01:00
|
|
|
|
2025-10-11 11:32:30 +01:00
|
|
|
| ZERO_OR_ONE regex => lastpos (regex, acc)
|
|
|
|
|
| ZERO_OR_MORE regex => lastpos (regex, acc)
|
|
|
|
|
| ONE_OR_MORE regex => lastpos (regex, acc)
|
|
|
|
|
| GROUP regex => lastpos (regex, acc)
|
2025-10-10 01:43:24 +01:00
|
|
|
|
2025-10-02 04:34:16 +01:00
|
|
|
structure Set =
|
|
|
|
|
struct
|
2025-10-02 13:54:59 +01:00
|
|
|
datatype 'a set = BRANCH of 'a set * int * 'a * 'a set | LEAF
|
2025-10-02 04:34:16 +01:00
|
|
|
|
2025-10-10 11:54:34 +01:00
|
|
|
fun isEmpty set =
|
|
|
|
|
case set of
|
|
|
|
|
BRANCH _ => false
|
|
|
|
|
| LEAF => true
|
|
|
|
|
|
2025-10-02 13:54:59 +01:00
|
|
|
fun insertOrReplace (newKey, newVal, tree) =
|
2025-10-02 04:34:16 +01:00
|
|
|
case tree of
|
2025-10-02 13:54:59 +01:00
|
|
|
BRANCH (l, curKey, curVal, r) =>
|
2025-10-02 04:34:16 +01:00
|
|
|
if newKey > curKey then
|
2025-10-02 13:54:59 +01:00
|
|
|
let val r = insertOrReplace (newKey, newVal, r)
|
|
|
|
|
in BRANCH (l, curKey, curVal, r)
|
2025-10-02 04:34:16 +01:00
|
|
|
end
|
|
|
|
|
else if newKey < curKey then
|
2025-10-02 13:54:59 +01:00
|
|
|
let val l = insertOrReplace (newKey, newVal, l)
|
|
|
|
|
in BRANCH (l, curKey, curVal, r)
|
2025-10-02 04:34:16 +01:00
|
|
|
end
|
|
|
|
|
else
|
2025-10-02 13:54:59 +01:00
|
|
|
BRANCH (l, newKey, newVal, r)
|
|
|
|
|
| LEAF => BRANCH (LEAF, newKey, newVal, LEAF)
|
2025-10-02 04:34:16 +01:00
|
|
|
|
2025-10-10 03:29:52 +01:00
|
|
|
fun addFromList (lst, tree) =
|
|
|
|
|
case lst of
|
|
|
|
|
[] => tree
|
2025-10-10 04:59:32 +01:00
|
|
|
| k :: tl =>
|
|
|
|
|
let val tree = insertOrReplace (k, (), tree)
|
2025-10-10 03:49:09 +01:00
|
|
|
in addFromList (tl, tree)
|
2025-10-10 03:29:52 +01:00
|
|
|
end
|
|
|
|
|
|
2025-10-02 13:54:59 +01:00
|
|
|
fun getOrDefault (findKey, tree, default) =
|
2025-10-02 04:34:16 +01:00
|
|
|
case tree of
|
2025-10-02 13:54:59 +01:00
|
|
|
BRANCH (l, curKey, curVal, r) =>
|
|
|
|
|
if findKey > curKey then getOrDefault (findKey, r, default)
|
|
|
|
|
else if findKey < curKey then getOrDefault (findKey, l, default)
|
|
|
|
|
else curVal
|
|
|
|
|
| LEAF => default
|
|
|
|
|
|
|
|
|
|
fun helpToList (tree, acc) =
|
|
|
|
|
case tree of
|
|
|
|
|
BRANCH (l, curKey, curVal, r) =>
|
|
|
|
|
let
|
|
|
|
|
val acc = helpToList (r, acc)
|
|
|
|
|
val acc = (curKey, curVal) :: acc
|
|
|
|
|
in
|
|
|
|
|
helpToList (l, acc)
|
|
|
|
|
end
|
|
|
|
|
| LEAF => acc
|
|
|
|
|
|
|
|
|
|
fun toList tree = helpToList (tree, [])
|
|
|
|
|
|
|
|
|
|
fun helpKeysToList (tree, acc) =
|
|
|
|
|
case tree of
|
|
|
|
|
BRANCH (l, curKey, _, r) =>
|
|
|
|
|
let
|
|
|
|
|
val acc = helpKeysToList (r, acc)
|
|
|
|
|
val acc = curKey :: acc
|
|
|
|
|
in
|
|
|
|
|
helpKeysToList (l, acc)
|
|
|
|
|
end
|
|
|
|
|
| LEAF => acc
|
|
|
|
|
|
|
|
|
|
fun keysToList tree = helpKeysToList (tree, [])
|
|
|
|
|
|
2025-10-11 11:32:30 +01:00
|
|
|
fun helpValuesToList (tree, acc) =
|
|
|
|
|
case tree of
|
|
|
|
|
BRANCH (l, _, v, r) =>
|
|
|
|
|
let
|
|
|
|
|
val acc = helpValuesToList (r, acc)
|
|
|
|
|
val acc = v :: acc
|
|
|
|
|
in
|
|
|
|
|
helpValuesToList (l, acc)
|
|
|
|
|
end
|
|
|
|
|
| LEAF => acc
|
|
|
|
|
|
|
|
|
|
fun valuesToList tree = helpValuesToList (tree, [])
|
|
|
|
|
|
2025-10-02 13:54:59 +01:00
|
|
|
fun map (f, tree) =
|
|
|
|
|
case tree of
|
|
|
|
|
BRANCH (l, key, value, r) =>
|
|
|
|
|
let
|
|
|
|
|
val r = map (f, r)
|
|
|
|
|
val l = map (f, l)
|
|
|
|
|
val value = f value
|
|
|
|
|
in
|
|
|
|
|
BRANCH (l, key, value, r)
|
|
|
|
|
end
|
|
|
|
|
| LEAF => LEAF
|
2025-10-03 05:17:13 +01:00
|
|
|
|
|
|
|
|
fun foldl (f, tree, acc) =
|
|
|
|
|
case tree of
|
|
|
|
|
BRANCH (l, k, v, r) =>
|
|
|
|
|
let
|
|
|
|
|
val acc = foldl (f, l, acc)
|
|
|
|
|
val acc = f (v, acc)
|
|
|
|
|
in
|
|
|
|
|
foldl (f, r, acc)
|
|
|
|
|
end
|
|
|
|
|
| LEAF => acc
|
2025-10-11 11:32:30 +01:00
|
|
|
|
|
|
|
|
fun foldr (f, tree, acc) =
|
|
|
|
|
case tree of
|
|
|
|
|
BRANCH (l, k, v, r) =>
|
|
|
|
|
let
|
|
|
|
|
val acc = foldr (f, r, acc)
|
|
|
|
|
val acc = f (v, acc)
|
|
|
|
|
in
|
|
|
|
|
foldr (f, l, acc)
|
|
|
|
|
end
|
|
|
|
|
| LEAF => acc
|
2025-10-02 04:34:16 +01:00
|
|
|
end
|
|
|
|
|
|
2025-10-03 07:29:28 +01:00
|
|
|
structure ParseDfa =
|
2025-09-29 08:33:10 +01:00
|
|
|
struct
|
2025-09-29 13:34:55 +01:00
|
|
|
(* parsing through precedence climbing algorithm. *)
|
2025-10-01 13:48:18 +01:00
|
|
|
val postfixLevel = 1
|
|
|
|
|
val concatLevel = 2
|
|
|
|
|
val altLevel = 3
|
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-10-06 21:44:57 +01:00
|
|
|
(* assumes previous char is a backslash *)
|
|
|
|
|
fun isValidEscapeSequence chr =
|
|
|
|
|
case chr of
|
|
|
|
|
(* regex metacharacters *)
|
2025-10-06 21:58:50 +01:00
|
|
|
#"(" => (true, chr)
|
|
|
|
|
| #")" => (true, chr)
|
|
|
|
|
| #"[" => (true, chr)
|
|
|
|
|
| #"]" => (true, chr)
|
|
|
|
|
| #"+" => (true, chr)
|
|
|
|
|
| #"*" => (true, chr)
|
|
|
|
|
| #"|" => (true, chr)
|
|
|
|
|
| #"?" => (true, chr)
|
2025-10-07 09:31:24 +01:00
|
|
|
| #"." => (true, chr)
|
2025-10-07 09:48:10 +01:00
|
|
|
| #"-" => (true, chr)
|
2025-10-06 21:44:57 +01:00
|
|
|
(* standard escape sequences *)
|
2025-10-06 21:58:50 +01:00
|
|
|
| #"a" => (true, #"\a")
|
|
|
|
|
| #"b" => (true, #"\b")
|
|
|
|
|
| #"t" => (true, #"\t")
|
|
|
|
|
| #"n" => (true, #"\n")
|
|
|
|
|
| #"v" => (true, #"\v")
|
|
|
|
|
| #"f" => (true, #"\f")
|
|
|
|
|
| #"r" => (true, #"\r")
|
|
|
|
|
| #"\\" => (true, chr)
|
|
|
|
|
| _ => (false, chr)
|
2025-10-06 21:44:57 +01:00
|
|
|
|
2025-10-07 09:48:10 +01:00
|
|
|
fun getCharsBetween (lowChr, highChr, acc) =
|
|
|
|
|
if lowChr = highChr then
|
|
|
|
|
highChr :: acc
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val acc = lowChr :: acc
|
|
|
|
|
val lowChr = Char.succ lowChr
|
|
|
|
|
in
|
|
|
|
|
getCharsBetween (lowChr, highChr, acc)
|
|
|
|
|
end
|
|
|
|
|
|
2025-10-07 09:31:24 +01:00
|
|
|
fun getCharsInBrackets (pos, str, acc) =
|
|
|
|
|
if pos = String.size str then
|
|
|
|
|
NONE
|
|
|
|
|
else
|
|
|
|
|
case String.sub (str, pos) of
|
|
|
|
|
#"\\" =>
|
|
|
|
|
(* escape sequences *)
|
|
|
|
|
if pos + 1 = String.size str then
|
|
|
|
|
NONE
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val chr = String.sub (str, pos + 1)
|
|
|
|
|
val (isValid, chr) = isValidEscapeSequence chr
|
|
|
|
|
in
|
2025-10-07 12:28:14 +01:00
|
|
|
if isValid then
|
|
|
|
|
(* Edge case:
|
|
|
|
|
* We have to check if there is a char range like a-z,
|
|
|
|
|
* and if there is,
|
|
|
|
|
* we have to check if the second char in the range
|
|
|
|
|
* is another escaped-character *)
|
|
|
|
|
if
|
|
|
|
|
pos + 2 < String.size str
|
|
|
|
|
andalso String.sub (str, pos + 2) = #"-"
|
|
|
|
|
andalso pos + 3 < String.size str
|
|
|
|
|
then
|
|
|
|
|
(* we do have a character range,
|
|
|
|
|
* which may possibly be escaped *)
|
|
|
|
|
case String.sub (str, pos + 3) of
|
|
|
|
|
#"(" => NONE
|
|
|
|
|
| #")" => NONE
|
|
|
|
|
| #"[" => NONE
|
|
|
|
|
| #"]" => NONE
|
|
|
|
|
| #"+" => NONE
|
|
|
|
|
| #"*" => NONE
|
|
|
|
|
| #"|" => NONE
|
|
|
|
|
| #"?" => NONE
|
|
|
|
|
| #"." => NONE
|
|
|
|
|
| #"-" => NONE
|
|
|
|
|
| #"\\" =>
|
|
|
|
|
if pos + 4 < String.size str then
|
|
|
|
|
let
|
|
|
|
|
val chr2 = String.sub (str, pos + 4)
|
|
|
|
|
val (isValid, chr2) = isValidEscapeSequence chr2
|
|
|
|
|
val acc =
|
|
|
|
|
if chr < chr2 then
|
|
|
|
|
getCharsBetween (chr, chr2, acc)
|
|
|
|
|
else
|
|
|
|
|
getCharsBetween (chr2, chr, acc)
|
|
|
|
|
in
|
|
|
|
|
getCharsInBrackets (pos + 5, str, acc)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
NONE
|
|
|
|
|
| chr2 =>
|
|
|
|
|
let
|
|
|
|
|
val acc =
|
|
|
|
|
if chr < chr2 then getCharsBetween (chr, chr2, acc)
|
|
|
|
|
else getCharsBetween (chr2, chr, acc)
|
|
|
|
|
in
|
|
|
|
|
getCharsInBrackets (pos + 4, str, acc)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
(* no character range we have to check *)
|
|
|
|
|
getCharsInBrackets (pos + 2, str, chr :: acc)
|
|
|
|
|
else
|
|
|
|
|
NONE
|
2025-10-07 09:31:24 +01:00
|
|
|
end
|
|
|
|
|
| #"]" =>
|
|
|
|
|
let val chars = Vector.fromList acc
|
|
|
|
|
in SOME (pos + 1, chars)
|
|
|
|
|
end
|
2025-10-07 09:48:10 +01:00
|
|
|
| chr =>
|
|
|
|
|
if
|
|
|
|
|
pos + 1 < String.size str andalso String.sub (str, pos + 1) = #"-"
|
|
|
|
|
andalso pos + 2 < String.size str
|
|
|
|
|
then
|
2025-10-07 12:13:41 +01:00
|
|
|
(* handle character ranges like a-z.
|
|
|
|
|
* There are edge cases regarding
|
|
|
|
|
* the second character in the range.
|
|
|
|
|
* We have to check that any unescaped metacharacters
|
|
|
|
|
* return an invalid parse state.
|
|
|
|
|
* We also have to unescape any escape sequences.
|
|
|
|
|
* *)
|
|
|
|
|
case String.sub (str, pos + 2) of
|
|
|
|
|
#"\\" =>
|
|
|
|
|
(* second char contains an escape sequence *)
|
|
|
|
|
if pos + 3 < String.size str then
|
|
|
|
|
let
|
|
|
|
|
val chr2 = String.sub (str, pos + 3)
|
|
|
|
|
val (isValid, chr2) = isValidEscapeSequence chr2
|
|
|
|
|
val acc =
|
|
|
|
|
if chr < chr2 then getCharsBetween (chr, chr2, acc)
|
|
|
|
|
else getCharsBetween (chr2, chr, acc)
|
|
|
|
|
in
|
|
|
|
|
if isValid then getCharsInBrackets (pos + 4, str, acc)
|
|
|
|
|
else NONE
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
NONE
|
|
|
|
|
| #"(" => NONE
|
|
|
|
|
| #")" => NONE
|
|
|
|
|
| #"[" => NONE
|
|
|
|
|
| #"]" => NONE
|
|
|
|
|
| #"+" => NONE
|
|
|
|
|
| #"*" => NONE
|
|
|
|
|
| #"|" => NONE
|
|
|
|
|
| #"?" => NONE
|
|
|
|
|
| #"." => NONE
|
|
|
|
|
| #"-" => NONE
|
|
|
|
|
| chr2 =>
|
|
|
|
|
(* valid char range *)
|
|
|
|
|
let
|
|
|
|
|
val acc =
|
|
|
|
|
if chr < chr2 then getCharsBetween (chr, chr2, acc)
|
|
|
|
|
else getCharsBetween (chr2, chr, acc)
|
|
|
|
|
in
|
|
|
|
|
getCharsInBrackets (pos + 3, str, acc)
|
|
|
|
|
end
|
2025-10-07 09:48:10 +01:00
|
|
|
else
|
|
|
|
|
getCharsInBrackets (pos + 1, str, chr :: acc)
|
2025-10-07 09:31:24 +01:00
|
|
|
|
|
|
|
|
fun parseCharacterClass (pos, str, stateNum) =
|
|
|
|
|
case getCharsInBrackets (pos, str, []) of
|
|
|
|
|
SOME (pos, chars) =>
|
|
|
|
|
let
|
2025-10-11 11:32:30 +01:00
|
|
|
val node = IS_ANY_CHARACTER {chars = chars, position = stateNum + 1}
|
2025-10-07 09:31:24 +01:00
|
|
|
in
|
|
|
|
|
SOME (pos, node, stateNum + 1)
|
|
|
|
|
end
|
|
|
|
|
| NONE => NONE
|
|
|
|
|
|
|
|
|
|
fun parseNegateCharacterClass (pos, str, stateNum) =
|
|
|
|
|
case getCharsInBrackets (pos, str, []) of
|
|
|
|
|
SOME (pos, chars) =>
|
|
|
|
|
let
|
|
|
|
|
val node =
|
2025-10-11 11:32:30 +01:00
|
|
|
NOT_ANY_CHARACTER {chars = chars, position = stateNum + 1}
|
2025-10-07 09:31:24 +01:00
|
|
|
in
|
|
|
|
|
SOME (pos, node, stateNum + 1)
|
|
|
|
|
end
|
|
|
|
|
| NONE => NONE
|
|
|
|
|
|
2025-10-01 13:48:18 +01:00
|
|
|
fun computeAtom (pos, str, stateNum) =
|
|
|
|
|
if pos = String.size str then
|
|
|
|
|
NONE
|
|
|
|
|
else
|
|
|
|
|
case String.sub (str, pos) of
|
|
|
|
|
#"(" =>
|
|
|
|
|
(case getRightParenIdx (pos + 1, str) of
|
|
|
|
|
SOME groupEndIdx =>
|
|
|
|
|
let
|
|
|
|
|
val substr = String.substring
|
|
|
|
|
(str, pos + 1, groupEndIdx - pos - 1)
|
|
|
|
|
in
|
|
|
|
|
case parse (substr, stateNum) of
|
|
|
|
|
SOME (rhs, stateNum) =>
|
|
|
|
|
SOME (groupEndIdx + 1, rhs, stateNum)
|
|
|
|
|
| NONE => NONE
|
|
|
|
|
end
|
|
|
|
|
| NONE => NONE)
|
2025-10-06 21:44:57 +01:00
|
|
|
| #"\\" =>
|
|
|
|
|
(* escape sequences *)
|
|
|
|
|
if pos + 1 = String.size str then
|
|
|
|
|
NONE
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val chr = String.sub (str, pos + 1)
|
2025-10-06 21:58:50 +01:00
|
|
|
val (isValid, chr) = isValidEscapeSequence chr
|
2025-10-06 21:44:57 +01:00
|
|
|
in
|
2025-10-07 14:36:35 +01:00
|
|
|
if Fn.charIsEqual (chr, Fn.endMarker) then
|
|
|
|
|
NONE
|
|
|
|
|
else if isValid then
|
2025-10-06 21:44:57 +01:00
|
|
|
let
|
2025-10-11 11:32:30 +01:00
|
|
|
val chr = CHAR_LITERAL {char = chr, position = stateNum + 1}
|
2025-10-06 21:44:57 +01:00
|
|
|
in
|
|
|
|
|
SOME (pos + 2, chr, stateNum + 1)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
NONE
|
|
|
|
|
end
|
2025-10-10 03:49:09 +01:00
|
|
|
| #"." =>
|
2025-10-11 11:32:30 +01:00
|
|
|
let val w = WILDCARD (stateNum + 1)
|
2025-10-10 03:49:09 +01:00
|
|
|
in SOME (pos + 1, w, stateNum + 1)
|
|
|
|
|
end
|
2025-10-07 09:31:24 +01:00
|
|
|
| #"[" =>
|
|
|
|
|
if pos + 1 = String.size str then
|
|
|
|
|
NONE
|
|
|
|
|
else if String.sub (str, pos + 1) = #"^" then
|
|
|
|
|
parseNegateCharacterClass (pos + 2, str, stateNum)
|
|
|
|
|
else
|
|
|
|
|
parseCharacterClass (pos + 1, str, stateNum)
|
2025-10-09 06:22:21 +01:00
|
|
|
| #")" => NONE
|
|
|
|
|
| #"]" => NONE
|
|
|
|
|
| #"+" => NONE
|
|
|
|
|
| #"*" => NONE
|
|
|
|
|
| #"|" => NONE
|
|
|
|
|
| #"?" => NONE
|
|
|
|
|
| #"-" => NONE
|
2025-10-01 13:48:18 +01:00
|
|
|
| chr =>
|
2025-10-07 14:36:35 +01:00
|
|
|
if Fn.charIsEqual (chr, Fn.endMarker) then
|
|
|
|
|
NONE
|
|
|
|
|
else
|
2025-10-11 11:32:30 +01:00
|
|
|
let val chr = CHAR_LITERAL {char = chr, position = stateNum + 1}
|
|
|
|
|
in SOME (pos + 1, chr, stateNum + 1)
|
2025-10-07 14:36:35 +01:00
|
|
|
end
|
2025-10-01 13:48:18 +01:00
|
|
|
|
2025-10-07 14:05:45 +01:00
|
|
|
and climb (pos, str, lhs, level, stateNum) : (int * parse_tree * int) option =
|
2025-09-29 00:46:05 +01:00
|
|
|
if pos = String.size str then
|
2025-10-01 11:23:41 +01:00
|
|
|
SOME (pos, lhs, stateNum)
|
2025-09-29 00:46:05 +01:00
|
|
|
else
|
2025-09-29 08:33:10 +01:00
|
|
|
case String.sub (str, pos) of
|
2025-10-01 13:48:18 +01:00
|
|
|
#"|" =>
|
2025-09-29 08:33:10 +01:00
|
|
|
if level < altLevel then
|
2025-10-01 11:23:41 +01:00
|
|
|
SOME (pos, lhs, stateNum)
|
2025-09-29 13:34:55 +01:00
|
|
|
else if pos + 1 < String.size str then
|
2025-09-29 08:33:10 +01:00
|
|
|
let
|
|
|
|
|
val chr = String.sub (str, pos + 1)
|
2025-10-11 11:32:30 +01:00
|
|
|
val chr = CHAR_LITERAL {char = chr, position = stateNum + 1}
|
2025-09-29 08:33:10 +01:00
|
|
|
in
|
2025-10-01 11:23:41 +01:00
|
|
|
case climb (pos + 2, str, chr, altLevel, stateNum + 1) of
|
2025-10-02 04:34:16 +01:00
|
|
|
SOME (pos, rhs, rightStateNum) =>
|
|
|
|
|
let
|
|
|
|
|
val result = ALTERNATION
|
|
|
|
|
{ l = lhs
|
|
|
|
|
, r = rhs
|
|
|
|
|
, leftMaxState = stateNum
|
|
|
|
|
, rightMaxState = rightStateNum
|
|
|
|
|
}
|
|
|
|
|
in
|
|
|
|
|
SOME (pos, result, rightStateNum)
|
2025-09-29 13:34:55 +01:00
|
|
|
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-10-01 11:23:41 +01:00
|
|
|
SOME (pos, lhs, stateNum)
|
2025-09-29 08:33:10 +01:00
|
|
|
else
|
2025-10-01 10:48:45 +01:00
|
|
|
let val lhs = ZERO_OR_ONE lhs
|
2025-10-01 11:23:41 +01:00
|
|
|
in climb (pos + 1, str, lhs, postfixLevel, stateNum)
|
2025-09-29 08:33:10 +01:00
|
|
|
end
|
|
|
|
|
| #"*" =>
|
|
|
|
|
if level < postfixLevel then
|
2025-10-01 11:23:41 +01:00
|
|
|
SOME (pos, lhs, stateNum)
|
2025-09-29 08:33:10 +01:00
|
|
|
else
|
2025-10-01 10:48:45 +01:00
|
|
|
let val lhs = ZERO_OR_MORE lhs
|
2025-10-01 11:23:41 +01:00
|
|
|
in climb (pos + 1, str, lhs, postfixLevel, stateNum)
|
2025-09-29 08:33:10 +01:00
|
|
|
end
|
|
|
|
|
| #"+" =>
|
|
|
|
|
if level < postfixLevel then
|
2025-10-01 11:23:41 +01:00
|
|
|
SOME (pos, lhs, stateNum)
|
2025-09-29 08:33:10 +01:00
|
|
|
else
|
2025-10-01 10:48:45 +01:00
|
|
|
let val lhs = ONE_OR_MORE lhs
|
2025-10-01 11:23:41 +01:00
|
|
|
in climb (pos + 1, str, lhs, postfixLevel, stateNum)
|
2025-09-29 08:33:10 +01:00
|
|
|
end
|
|
|
|
|
| chr =>
|
|
|
|
|
if level < concatLevel then
|
2025-10-01 11:23:41 +01:00
|
|
|
SOME (pos, lhs, stateNum)
|
2025-09-29 08:33:10 +01:00
|
|
|
else
|
2025-10-01 13:48:18 +01:00
|
|
|
case computeAtom (pos, str, stateNum) of
|
2025-10-02 04:34:16 +01:00
|
|
|
SOME (nextPos, curAtom, atomStateNum) =>
|
|
|
|
|
(case climb (nextPos, str, curAtom, concatLevel, atomStateNum) of
|
|
|
|
|
SOME (pos, rhs, rightStateNum) =>
|
|
|
|
|
let
|
|
|
|
|
val result = CONCAT
|
|
|
|
|
{ l = lhs
|
|
|
|
|
, r = rhs
|
|
|
|
|
, leftMaxState = stateNum
|
|
|
|
|
, rightMaxState = rightStateNum
|
|
|
|
|
}
|
|
|
|
|
in
|
|
|
|
|
SOME (pos, result, rightStateNum)
|
2025-10-01 13:48:18 +01:00
|
|
|
end
|
|
|
|
|
| NONE => NONE)
|
|
|
|
|
| NONE => NONE
|
2025-09-29 00:46:05 +01:00
|
|
|
|
2025-10-01 11:23:41 +01:00
|
|
|
and loop (pos, str, ast, stateNum) =
|
2025-09-28 22:01:44 +01:00
|
|
|
if pos = String.size str then
|
2025-10-01 11:23:41 +01:00
|
|
|
SOME (ast, stateNum)
|
2025-09-28 22:01:44 +01:00
|
|
|
else
|
2025-10-01 11:23:41 +01:00
|
|
|
case climb (pos, str, ast, altLevel, stateNum) of
|
|
|
|
|
SOME (pos, ast, stateNum) => loop (pos, str, ast, stateNum)
|
2025-09-29 13:34:55 +01:00
|
|
|
| NONE => NONE
|
2025-09-28 22:01:44 +01:00
|
|
|
|
2025-10-01 11:23:41 +01:00
|
|
|
and parse (str, stateNum) =
|
2025-09-29 13:34:55 +01:00
|
|
|
if String.size str > 0 then
|
2025-10-01 13:48:18 +01:00
|
|
|
case computeAtom (0, str, stateNum) of
|
|
|
|
|
SOME (nextPos, lhs, stateNum) => loop (nextPos, str, lhs, stateNum)
|
|
|
|
|
| NONE => NONE
|
2025-09-29 13:34:55 +01:00
|
|
|
else
|
|
|
|
|
NONE
|
2025-09-29 08:33:10 +01:00
|
|
|
end
|
2025-09-28 22:01:44 +01:00
|
|
|
|
2025-10-01 11:52:45 +01:00
|
|
|
structure ToDfa =
|
|
|
|
|
struct
|
2025-10-11 11:32:30 +01:00
|
|
|
type dstate_element = {marked: bool, transitions: int list}
|
|
|
|
|
type dstate_vec = dstate_element vector
|
2025-10-02 13:54:59 +01:00
|
|
|
|
2025-10-07 08:51:46 +01:00
|
|
|
fun chrExistsInVec (idx, vec, curChr) =
|
|
|
|
|
if idx = Vector.length vec then
|
|
|
|
|
false
|
|
|
|
|
else
|
2025-10-07 14:05:45 +01:00
|
|
|
let
|
|
|
|
|
val idxChr = Vector.sub (vec, idx)
|
|
|
|
|
in
|
|
|
|
|
Fn.charIsEqual (idxChr, curChr)
|
|
|
|
|
orelse chrExistsInVec (idx + 1, vec, curChr)
|
|
|
|
|
end
|
2025-10-07 08:51:46 +01:00
|
|
|
|
2025-10-10 11:54:34 +01:00
|
|
|
fun addKeysToFollowSet (lst, addSet, followSet) =
|
|
|
|
|
case lst of
|
|
|
|
|
hd :: tl =>
|
|
|
|
|
let
|
|
|
|
|
val currentFollows = Set.getOrDefault (hd, followSet, [])
|
|
|
|
|
val updatedFollows = Set.addFromList (currentFollows, addSet)
|
|
|
|
|
val updatedFollows: int list = Set.keysToList updatedFollows
|
|
|
|
|
val followSet = Set.insertOrReplace (hd, updatedFollows, followSet)
|
|
|
|
|
in
|
|
|
|
|
addKeysToFollowSet (tl, addSet, followSet)
|
|
|
|
|
end
|
|
|
|
|
| [] => followSet
|
|
|
|
|
|
|
|
|
|
fun addToFollowSet (tree, followSet) =
|
|
|
|
|
case tree of
|
|
|
|
|
WILDCARD _ => followSet
|
2025-10-12 00:22:14 +01:00
|
|
|
| CHAR_LITERAL {char, position} =>
|
|
|
|
|
(* we add the endMarker and its position to the followSet *)
|
|
|
|
|
if char = Fn.endMarker then
|
|
|
|
|
Set.insertOrReplace (position, [Char.ord Fn.endMarker], followSet)
|
|
|
|
|
else
|
|
|
|
|
followSet
|
2025-10-10 11:54:34 +01:00
|
|
|
| IS_ANY_CHARACTER _ => followSet
|
|
|
|
|
| NOT_ANY_CHARACTER _ => followSet
|
|
|
|
|
| CONCAT {l, r, ...} =>
|
|
|
|
|
let
|
|
|
|
|
val followSet = addToFollowSet (l, followSet)
|
|
|
|
|
val followSet = addToFollowSet (r, followSet)
|
|
|
|
|
|
2025-10-12 00:22:14 +01:00
|
|
|
val lpOfLeft = lastpos (l, [])
|
|
|
|
|
val fpOfRight = firstpos (r, [])
|
|
|
|
|
val fpOfRight = Set.addFromList (fpOfRight, Set.LEAF)
|
2025-10-10 11:54:34 +01:00
|
|
|
in
|
2025-10-12 00:22:14 +01:00
|
|
|
addKeysToFollowSet (lpOfLeft, fpOfRight, followSet)
|
2025-10-10 11:54:34 +01:00
|
|
|
end
|
|
|
|
|
| ALTERNATION {l, r, ...} =>
|
|
|
|
|
let val followSet = addToFollowSet (l, followSet)
|
|
|
|
|
in addToFollowSet (r, followSet)
|
|
|
|
|
end
|
|
|
|
|
| ZERO_OR_MORE child =>
|
|
|
|
|
let
|
2025-10-12 00:22:14 +01:00
|
|
|
val followSet = addToFollowSet (child, followSet)
|
2025-10-10 11:54:34 +01:00
|
|
|
val fp = firstpos (child, [])
|
|
|
|
|
val fp = Set.addFromList (fp, Set.LEAF)
|
2025-10-12 00:22:14 +01:00
|
|
|
val lp = lastpos (child, [])
|
2025-10-10 11:54:34 +01:00
|
|
|
in
|
|
|
|
|
addKeysToFollowSet (lp, fp, followSet)
|
|
|
|
|
end
|
|
|
|
|
| ONE_OR_MORE child =>
|
|
|
|
|
let
|
2025-10-12 00:32:55 +01:00
|
|
|
val followSet = addToFollowSet (child, followSet)
|
2025-10-10 11:54:34 +01:00
|
|
|
val lp = lastpos (child, [])
|
|
|
|
|
val fp = firstpos (child, [])
|
|
|
|
|
val fp = Set.addFromList (fp, Set.LEAF)
|
|
|
|
|
in
|
|
|
|
|
addKeysToFollowSet (lp, fp, followSet)
|
|
|
|
|
end
|
|
|
|
|
| ZERO_OR_ONE child => addToFollowSet (child, followSet)
|
|
|
|
|
| GROUP child => addToFollowSet (child, followSet)
|
|
|
|
|
|
2025-10-05 15:31:11 +01:00
|
|
|
fun appendIfNew (pos, dstates, newStates) =
|
2025-10-03 05:54:48 +01:00
|
|
|
if pos = Vector.length dstates then
|
2025-10-06 08:21:04 +01:00
|
|
|
let
|
|
|
|
|
val record = {transitions = newStates, marked = false}
|
|
|
|
|
val dstates = Vector.concat [dstates, Vector.fromList [record]]
|
|
|
|
|
in
|
|
|
|
|
(pos, dstates)
|
2025-10-05 20:27:48 +01:00
|
|
|
end
|
2025-10-03 05:54:48 +01:00
|
|
|
else
|
|
|
|
|
let
|
2025-10-05 20:27:48 +01:00
|
|
|
val {transitions: int list, marked = _} = Vector.sub (dstates, pos)
|
2025-10-03 05:54:48 +01:00
|
|
|
in
|
2025-10-06 08:21:04 +01:00
|
|
|
if transitions = newStates then (pos, dstates)
|
2025-10-05 15:31:11 +01:00
|
|
|
else appendIfNew (pos + 1, dstates, newStates)
|
2025-10-03 05:54:48 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun getUnmarkedTransitionsIfExists (pos, dstates) =
|
|
|
|
|
if pos = Vector.length dstates then
|
|
|
|
|
NONE
|
|
|
|
|
else
|
|
|
|
|
let
|
2025-10-11 11:32:30 +01:00
|
|
|
val record: dstate_element = Vector.sub (dstates, pos)
|
2025-10-03 05:54:48 +01:00
|
|
|
in
|
|
|
|
|
if #marked record then
|
|
|
|
|
getUnmarkedTransitionsIfExists (pos + 1, dstates)
|
2025-10-03 05:17:13 +01:00
|
|
|
else
|
2025-10-03 05:54:48 +01:00
|
|
|
SOME (pos, #transitions record)
|
|
|
|
|
end
|
2025-10-03 05:17:13 +01:00
|
|
|
|
2025-10-10 11:54:34 +01:00
|
|
|
fun isCharMatch (regex, pos, curChr) =
|
|
|
|
|
case regex of
|
|
|
|
|
CHAR_LITERAL {char, ...} => Fn.charIsEqual (char, curChr)
|
2025-10-12 00:22:14 +01:00
|
|
|
| WILDCARD _ => true
|
2025-10-10 11:54:34 +01:00
|
|
|
| IS_ANY_CHARACTER {chars, ...} => chrExistsInVec (0, chars, curChr)
|
|
|
|
|
| NOT_ANY_CHARACTER {chars, ...} =>
|
|
|
|
|
let val charIsValid = chrExistsInVec (0, chars, curChr)
|
2025-10-12 00:22:14 +01:00
|
|
|
in not charIsValid
|
2025-10-10 11:54:34 +01:00
|
|
|
end
|
|
|
|
|
| ALTERNATION {l, r, leftMaxState, ...} =>
|
|
|
|
|
if pos > leftMaxState then isCharMatch (r, pos, curChr)
|
|
|
|
|
else isCharMatch (l, pos, curChr)
|
|
|
|
|
| CONCAT {l, r, leftMaxState, ...} =>
|
|
|
|
|
if pos > leftMaxState then isCharMatch (r, pos, curChr)
|
|
|
|
|
else isCharMatch (l, pos, curChr)
|
|
|
|
|
| ZERO_OR_ONE child => isCharMatch (child, pos, curChr)
|
|
|
|
|
| ZERO_OR_MORE child => isCharMatch (child, pos, curChr)
|
|
|
|
|
| ONE_OR_MORE child => isCharMatch (child, pos, curChr)
|
|
|
|
|
| GROUP child => isCharMatch (child, pos, curChr)
|
|
|
|
|
|
2025-10-11 13:23:44 +01:00
|
|
|
fun positionsThatCorrespondToChar
|
|
|
|
|
(char, curStates, regex, acc, followSet, hasAnyMatch) =
|
2025-10-10 11:54:34 +01:00
|
|
|
case curStates of
|
2025-10-11 11:32:30 +01:00
|
|
|
[] => List.concat (Set.valuesToList acc)
|
2025-10-10 11:54:34 +01:00
|
|
|
| pos :: tl =>
|
2025-10-11 11:32:30 +01:00
|
|
|
if isCharMatch (regex, pos, Char.chr char) then
|
|
|
|
|
let
|
|
|
|
|
(* get union of new and previous follows *)
|
|
|
|
|
val prevFollows = Set.getOrDefault (char, acc, [])
|
|
|
|
|
val newFollows = Set.getOrDefault (pos, followSet, [])
|
|
|
|
|
|
|
|
|
|
val tempSet = Set.addFromList (prevFollows, Set.LEAF)
|
|
|
|
|
val tempSet = Set.addFromList (newFollows, tempSet)
|
|
|
|
|
val allFollowList = Set.keysToList tempSet
|
|
|
|
|
|
|
|
|
|
(* store union of new and previous follows so far *)
|
|
|
|
|
val acc = Set.insertOrReplace (char, allFollowList, acc)
|
|
|
|
|
in
|
2025-10-11 13:23:44 +01:00
|
|
|
positionsThatCorrespondToChar
|
|
|
|
|
(char, tl, regex, acc, followSet, true)
|
2025-10-11 11:32:30 +01:00
|
|
|
end
|
|
|
|
|
else
|
2025-10-11 13:23:44 +01:00
|
|
|
positionsThatCorrespondToChar
|
|
|
|
|
(char, tl, regex, acc, followSet, hasAnyMatch)
|
2025-10-11 11:32:30 +01:00
|
|
|
|
|
|
|
|
structure Dtran =
|
|
|
|
|
struct
|
|
|
|
|
(* vector, with idx corresponding to state in dstate,
|
|
|
|
|
* an int key which corresponds to char's ascii code,
|
|
|
|
|
* and an int value corresponding to state we will transition to *)
|
|
|
|
|
type t = int Set.set vector
|
|
|
|
|
|
|
|
|
|
fun insert (dStateIdx, char, toStateIdx, dtran: t) =
|
|
|
|
|
if dStateIdx = Vector.length dtran then
|
2025-10-10 11:54:34 +01:00
|
|
|
let
|
2025-10-11 11:32:30 +01:00
|
|
|
val el = Set.insertOrReplace (char, toStateIdx, Set.LEAF)
|
|
|
|
|
val el = Vector.fromList [el]
|
2025-10-10 11:54:34 +01:00
|
|
|
in
|
2025-10-11 11:32:30 +01:00
|
|
|
Vector.concat [dtran, el]
|
2025-10-10 11:54:34 +01:00
|
|
|
end
|
2025-10-14 02:24:45 +01:00
|
|
|
else if dStateIdx < Vector.length dtran then
|
2025-10-11 11:32:30 +01:00
|
|
|
let
|
|
|
|
|
val el = Vector.sub (dtran, dStateIdx)
|
|
|
|
|
val el = Set.insertOrReplace (char, toStateIdx, el)
|
|
|
|
|
in
|
|
|
|
|
Vector.update (dtran, dStateIdx, el)
|
|
|
|
|
end
|
2025-10-14 02:24:45 +01:00
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val appendLength = dStateIdx - Vector.length dtran
|
|
|
|
|
val appendVecs = Vector.tabulate (appendLength, fn _ => Set.LEAF)
|
|
|
|
|
val dtran = Vector.concat [dtran, appendVecs]
|
|
|
|
|
in
|
|
|
|
|
insert (dStateIdx, char, toStateIdx, dtran)
|
|
|
|
|
end
|
2025-10-11 11:32:30 +01:00
|
|
|
end
|
2025-10-06 09:06:04 +01:00
|
|
|
|
2025-10-05 20:19:26 +01:00
|
|
|
fun convertChar
|
2025-10-06 08:21:04 +01:00
|
|
|
( char
|
|
|
|
|
, regex
|
|
|
|
|
, dstates
|
2025-10-11 11:32:30 +01:00
|
|
|
, dtran: Dtran.t
|
|
|
|
|
, unmarkedState
|
|
|
|
|
, unmarkedIdx
|
2025-10-10 11:54:34 +01:00
|
|
|
, followSet
|
2025-10-11 13:39:28 +01:00
|
|
|
, prevDstateLength
|
2025-10-06 08:21:04 +01:00
|
|
|
) =
|
2025-10-05 15:31:11 +01:00
|
|
|
if char < 0 then
|
2025-10-12 00:22:14 +01:00
|
|
|
(dstates, dtran)
|
2025-10-05 15:31:11 +01:00
|
|
|
else
|
|
|
|
|
let
|
2025-10-11 11:32:30 +01:00
|
|
|
val u = positionsThatCorrespondToChar
|
2025-10-11 13:23:44 +01:00
|
|
|
(char, unmarkedState, regex, Set.LEAF, followSet, false)
|
2025-10-06 07:44:46 +01:00
|
|
|
in
|
2025-10-11 11:32:30 +01:00
|
|
|
case u of
|
2025-10-06 07:44:46 +01:00
|
|
|
[] =>
|
|
|
|
|
convertChar
|
2025-10-06 08:21:04 +01:00
|
|
|
( char - 1
|
|
|
|
|
, regex
|
|
|
|
|
, dstates
|
|
|
|
|
, dtran
|
2025-10-11 11:32:30 +01:00
|
|
|
, unmarkedState
|
|
|
|
|
, unmarkedIdx
|
2025-10-10 11:54:34 +01:00
|
|
|
, followSet
|
2025-10-11 13:39:28 +01:00
|
|
|
, prevDstateLength
|
2025-10-06 08:21:04 +01:00
|
|
|
)
|
2025-10-06 07:44:46 +01:00
|
|
|
| _ =>
|
2025-10-05 20:19:26 +01:00
|
|
|
let
|
2025-10-11 11:32:30 +01:00
|
|
|
(* dtran is idx -> char -> state_list map *)
|
|
|
|
|
val (uIdx, dstates) = appendIfNew (0, dstates, u)
|
|
|
|
|
val dtran = Dtran.insert (unmarkedIdx, char, uIdx, dtran)
|
2025-10-05 20:19:26 +01:00
|
|
|
in
|
2025-10-06 07:44:46 +01:00
|
|
|
convertChar
|
2025-10-06 08:21:04 +01:00
|
|
|
( char - 1
|
|
|
|
|
, regex
|
|
|
|
|
, dstates
|
|
|
|
|
, dtran
|
2025-10-11 11:32:30 +01:00
|
|
|
, unmarkedState
|
|
|
|
|
, unmarkedIdx
|
2025-10-10 11:54:34 +01:00
|
|
|
, followSet
|
2025-10-11 13:39:28 +01:00
|
|
|
, prevDstateLength
|
2025-10-06 08:21:04 +01:00
|
|
|
)
|
2025-10-05 20:19:26 +01:00
|
|
|
end
|
2025-10-05 15:31:11 +01:00
|
|
|
end
|
|
|
|
|
|
2025-10-10 11:54:34 +01:00
|
|
|
fun convertLoop (regex, dstates, dtran, followSet) =
|
2025-10-03 05:54:48 +01:00
|
|
|
case getUnmarkedTransitionsIfExists (0, dstates) of
|
|
|
|
|
SOME (unmarkedIdx, unamarkedTransition) =>
|
2025-10-03 05:17:13 +01:00
|
|
|
let
|
2025-10-03 05:54:48 +01:00
|
|
|
(* mark transition *)
|
|
|
|
|
val dstates =
|
|
|
|
|
let
|
|
|
|
|
val newMark = {marked = true, transitions = unamarkedTransition}
|
|
|
|
|
in
|
|
|
|
|
Vector.update (dstates, unmarkedIdx, newMark)
|
|
|
|
|
end
|
2025-10-03 05:17:13 +01:00
|
|
|
|
2025-10-06 07:44:46 +01:00
|
|
|
val (dstates, dtran) = convertChar
|
2025-10-06 08:21:04 +01:00
|
|
|
( 255
|
|
|
|
|
, regex
|
|
|
|
|
, dstates
|
|
|
|
|
, dtran
|
|
|
|
|
, unamarkedTransition
|
|
|
|
|
, unmarkedIdx
|
2025-10-10 11:54:34 +01:00
|
|
|
, followSet
|
2025-10-11 13:39:28 +01:00
|
|
|
, Vector.length dstates
|
2025-10-06 08:21:04 +01:00
|
|
|
)
|
2025-10-03 05:17:13 +01:00
|
|
|
in
|
2025-10-10 11:54:34 +01:00
|
|
|
convertLoop (regex, dstates, dtran, followSet)
|
2025-10-03 05:17:13 +01:00
|
|
|
end
|
2025-10-06 09:06:04 +01:00
|
|
|
| NONE =>
|
2025-10-11 13:23:44 +01:00
|
|
|
Vector.map
|
|
|
|
|
(fn set =>
|
|
|
|
|
Vector.tabulate (256, fn i => Set.getOrDefault (i, set, ~1)))
|
|
|
|
|
dtran
|
2025-10-03 05:17:13 +01:00
|
|
|
|
2025-10-10 11:54:34 +01:00
|
|
|
fun convert regex =
|
|
|
|
|
let
|
|
|
|
|
val followSet = addToFollowSet (regex, Set.LEAF)
|
2025-10-10 04:44:18 +01:00
|
|
|
|
2025-10-10 11:54:34 +01:00
|
|
|
(* get firstpos, sorted *)
|
|
|
|
|
val first = firstpos (regex, [])
|
|
|
|
|
val first = Set.addFromList (first, Set.LEAF)
|
|
|
|
|
val first = Set.keysToList first
|
2025-10-10 04:00:34 +01:00
|
|
|
|
2025-10-03 05:54:48 +01:00
|
|
|
val dstates = Vector.fromList [{transitions = first, marked = false}]
|
2025-10-02 13:54:59 +01:00
|
|
|
in
|
2025-10-11 13:23:44 +01:00
|
|
|
convertLoop (regex, dstates, Vector.fromList [Set.LEAF], followSet)
|
2025-10-02 13:54:59 +01:00
|
|
|
end
|
2025-10-01 11:52:45 +01:00
|
|
|
end
|
|
|
|
|
|
2025-10-03 07:29:28 +01:00
|
|
|
fun fromString str =
|
2025-10-06 11:49:10 +01:00
|
|
|
case ParseDfa.parse (str, 0) of
|
|
|
|
|
SOME (ast, numStates) =>
|
|
|
|
|
let
|
2025-10-07 08:57:01 +01:00
|
|
|
val endMarker =
|
2025-10-11 11:32:30 +01:00
|
|
|
CHAR_LITERAL {char = Fn.endMarker, position = numStates + 1}
|
2025-10-06 11:49:10 +01:00
|
|
|
val ast = CONCAT
|
|
|
|
|
{ l = ast
|
|
|
|
|
, leftMaxState = numStates
|
|
|
|
|
, r = endMarker
|
|
|
|
|
, rightMaxState = numStates + 1
|
|
|
|
|
}
|
|
|
|
|
in
|
2025-10-10 11:54:34 +01:00
|
|
|
ToDfa.convert ast
|
2025-10-06 11:49:10 +01:00
|
|
|
end
|
2025-10-06 09:06:04 +01:00
|
|
|
| NONE => Vector.fromList []
|
2025-10-02 13:54:59 +01:00
|
|
|
|
2025-10-06 09:06:04 +01:00
|
|
|
type dfa = int vector vector
|
2025-10-07 14:05:45 +01:00
|
|
|
type dfa_state = int
|
2025-10-06 09:06:04 +01:00
|
|
|
|
2025-10-07 14:05:45 +01:00
|
|
|
fun nextState (dfa: dfa, curState: dfa_state, chr) =
|
2025-10-06 09:06:04 +01:00
|
|
|
let val curTable = Vector.sub (dfa, curState)
|
|
|
|
|
in Vector.sub (curTable, Char.ord chr)
|
|
|
|
|
end
|
|
|
|
|
|
2025-10-07 14:05:45 +01:00
|
|
|
fun isFinal (dfa: dfa, curState: dfa_state) =
|
2025-10-06 09:55:05 +01:00
|
|
|
curState <> ~1
|
|
|
|
|
andalso
|
2025-10-07 14:30:23 +01:00
|
|
|
let
|
|
|
|
|
val curTable = Vector.sub (dfa, curState)
|
|
|
|
|
val endMarkerCode = Char.ord Fn.endMarker
|
|
|
|
|
in
|
|
|
|
|
Vector.sub (curTable, endMarkerCode) <> ~1
|
2025-10-06 09:06:04 +01:00
|
|
|
end
|
2025-10-06 09:55:05 +01:00
|
|
|
|
2025-10-07 14:05:45 +01:00
|
|
|
fun isDead (curState: dfa_state) = curState = ~1
|
2025-10-09 05:34:32 +01:00
|
|
|
|
|
|
|
|
fun helpMatchString (strPos, str, dfa, curState, startPos, prevFinalPos, acc) =
|
|
|
|
|
if strPos = String.size str then
|
|
|
|
|
let
|
|
|
|
|
val acc =
|
|
|
|
|
if prevFinalPos = ~1 then acc else (startPos, prevFinalPos) :: acc
|
|
|
|
|
in
|
|
|
|
|
List.rev acc
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val chr = String.sub (str, strPos)
|
|
|
|
|
val newState = nextState (dfa, curState, chr)
|
|
|
|
|
val prevFinalPos =
|
|
|
|
|
if isFinal (dfa, newState) then strPos else prevFinalPos
|
|
|
|
|
in
|
|
|
|
|
if isDead newState then
|
|
|
|
|
if prevFinalPos = ~1 then
|
|
|
|
|
(* restart from startPos *)
|
|
|
|
|
helpMatchString (startPos + 1, str, dfa, 0, startPos + 1, ~1, acc)
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val acc = (startPos, prevFinalPos) :: acc
|
|
|
|
|
in
|
|
|
|
|
helpMatchString
|
|
|
|
|
(prevFinalPos + 1, str, dfa, 0, prevFinalPos + 1, ~1, acc)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
helpMatchString
|
|
|
|
|
(strPos + 1, str, dfa, newState, startPos, prevFinalPos, acc)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun matchString (dfa, string) =
|
|
|
|
|
if Vector.length dfa = 0 then []
|
|
|
|
|
else helpMatchString (0, string, dfa, 0, 0, ~1, [])
|
2025-10-06 09:06:04 +01:00
|
|
|
end
|
2025-10-07 14:05:45 +01:00
|
|
|
|
|
|
|
|
structure CaseInsensitiveDfa =
|
|
|
|
|
MakeDfaGen
|
|
|
|
|
(struct
|
|
|
|
|
val endMarker = #"\^@"
|
2025-10-07 14:30:23 +01:00
|
|
|
fun charIsEqual (a: char, b: char) = Char.toLower a = Char.toLower b
|
2025-10-07 14:05:45 +01:00
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
structure CaseSensitiveDfa =
|
|
|
|
|
MakeDfaGen
|
|
|
|
|
(struct
|
|
|
|
|
val endMarker = #"\^@"
|
2025-10-07 14:30:23 +01:00
|
|
|
fun charIsEqual (a: char, b: char) = a = b
|
2025-10-07 14:05:45 +01:00
|
|
|
end)
|