fix regex parsing by not considering grouping parens as an operator

This commit is contained in:
2025-10-01 13:48:18 +01:00
parent 6a98cddebe
commit 169dcb5bf2

View File

@@ -17,10 +17,9 @@ struct
datatype action = datatype action =
TRY_NEXT_NODE_WITHOUT_CONSUMING_CHR TRY_NEXT_NODE_WITHOUT_CONSUMING_CHR
val groupLevel = 1 val postfixLevel = 1
val postfixLevel = 2 val concatLevel = 2
val concatLevel = 3 val altLevel = 3
val altLevel = 4
local local
fun loop (pos, str, openParens, closeParens) = fun loop (pos, str, openParens, closeParens) =
@@ -37,40 +36,40 @@ struct
fun getRightParenIdx (pos, str) = loop (pos, str, 1, 0) fun getRightParenIdx (pos, str) = loop (pos, str, 1, 0)
end end
fun climb (pos, str, lhs, level, stateNum) : (int * regex * int) option = fun computeAtom (pos, str, stateNum) =
if pos = String.size str then if pos = String.size str then
SOME (pos, lhs, stateNum) NONE
else else
case String.sub (str, pos) of case String.sub (str, pos) of
#")" => SOME (pos + 1, lhs, stateNum) #"(" =>
| #"(" =>
if level < groupLevel then
SOME (pos, lhs, stateNum)
else
(case getRightParenIdx (pos + 1, str) of (case getRightParenIdx (pos + 1, str) of
SOME groupEndIdx => SOME groupEndIdx =>
let let
val substr = String.substring val substr = String.substring
(str, pos + 1, groupEndIdx - pos - 1) (str, pos + 1, groupEndIdx - pos - 1)
in in
(case parse (substr, stateNum) of case parse (substr, stateNum) of
SOME (rhs, stateNum) => SOME (rhs, stateNum) =>
let SOME (groupEndIdx + 1, rhs, stateNum)
val rhs = GROUP rhs | NONE => NONE
val result = CONCAT (lhs, rhs)
in
climb
( groupEndIdx + 1
, str
, result
, groupLevel
, stateNum
)
end end
| NONE => NONE) | NONE => NONE)
| #")" => NONE
| #"?" => NONE
| #"*" => NONE
| #"+" => NONE
| #"." => SOME (pos + 1, WILDCARD (stateNum + 1), stateNum + 1)
| chr =>
let val chr = CHAR_LITERAL {char = chr, position = stateNum + 1}
in SOME (pos + 1, chr, stateNum + 1)
end end
| NONE => NONE)
| #"|" => and climb (pos, str, lhs, level, stateNum) : (int * regex * int) option =
if pos = String.size str then
SOME (pos, lhs, stateNum)
else
case String.sub (str, pos) of
#"|" =>
if level < altLevel then if level < altLevel then
SOME (pos, lhs, stateNum) SOME (pos, lhs, stateNum)
else if pos + 1 < String.size str then else if pos + 1 < String.size str then
@@ -112,20 +111,15 @@ struct
if level < concatLevel then if level < concatLevel then
SOME (pos, lhs, stateNum) SOME (pos, lhs, stateNum)
else else
let case computeAtom (pos, str, stateNum) of
val currentState = SOME (nextPos, curAtom, stateNum) =>
if chr = #"." then WILDCARD (stateNum + 1) (case climb (nextPos, str, curAtom, concatLevel, stateNum + 1) of
else CHAR_LITERAL {char = chr, position = stateNum + 1}
in
case
climb (pos + 1, str, currentState, concatLevel, stateNum + 1)
of
SOME (pos, rhs, stateNum) => SOME (pos, rhs, stateNum) =>
let val result = CONCAT (lhs, rhs) let val result = CONCAT (lhs, rhs)
in SOME (pos, result, stateNum) in SOME (pos, result, stateNum)
end end
| NONE => NONE)
| NONE => NONE | NONE => NONE
end
and loop (pos, str, ast, stateNum) = and loop (pos, str, ast, stateNum) =
if pos = String.size str then if pos = String.size str then
@@ -140,12 +134,9 @@ struct
(* todo: we currently assume that the first char is always a CHAR_LITERAL (* todo: we currently assume that the first char is always a CHAR_LITERAL
* but we should actually check what character the chr is * but we should actually check what character the chr is
* before deciding it represents one variant or another *) * before deciding it represents one variant or another *)
let case computeAtom (0, str, stateNum) of
val chr = String.sub (str, 0) SOME (nextPos, lhs, stateNum) => loop (nextPos, str, lhs, stateNum)
val chr = CHAR_LITERAL {char = chr, position = stateNum + 1} | NONE => NONE
in
loop (1, str, chr, stateNum + 1)
end
else else
NONE NONE
end end