Skip to content

Commit

Permalink
Code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
phreppo committed Sep 27, 2023
1 parent 5c4a2c7 commit f7d2357
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 49 deletions.
34 changes: 17 additions & 17 deletions src/lib/Analysis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ module AttackFamilySet = struct
let simplify = map AttackFamily.simplify
end

let rec to_extended_regex = function
let rec to_ext_regex = function
| Epsilon -> ExtRe.eps
| Char c -> ExtRe.chr c
| Choice (l, r) ->
ExtRe.alternative (to_extended_regex l) (to_extended_regex r)
| Concat (l, r) -> ExtRe.concat (to_extended_regex l) (to_extended_regex r)
| Star (_, e) -> ExtRe.star (to_extended_regex e)
| Alternative (l, r) ->
ExtRe.alternative (to_ext_regex l) (to_ext_regex r)
| Concat (l, r) -> ExtRe.concat (to_ext_regex l) (to_ext_regex r)
| Star (_, e) -> ExtRe.star (to_ext_regex e)

let rec leaves e =
match next e with
Expand All @@ -123,7 +123,7 @@ let common_words_in_leaves left_leaves right_leaves =
let cartesian =
U.cartesian (RS.elements left_leaves) (RS.elements right_leaves)
|> List.map (fun el ->
(to_extended_regex (fst el), to_extended_regex (snd el)))
(to_ext_regex (fst el), to_ext_regex (snd el)))
in
List.fold_left
(fun acc elem ->
Expand All @@ -149,7 +149,7 @@ and m2_already_explored e explored pref =
occur only for kleene stars to avoid infinite loops. *)
let tail = tail e in
let head = head e in
let pref = ExtRe.concat pref (to_extended_regex head) in
let pref = ExtRe.concat pref (to_ext_regex head) in
m2_rec tail explored pref

and m2_new_expression e explored pref =
Expand All @@ -170,35 +170,35 @@ and m2_choice l r explored pref =
(ExtRe.alternative attack_left attack_right)

(** [exp_attack_families r] returns the families of exponentially attack words
for the regex *)
for the regex [r]. *)
let rec exp_attack_families r =
exp_attack_rec ExtRe.eps ExtRe.eps r |> AttackFamilySet.remove_empty

and exp_attack_rec pref suff e =
match e with
| Epsilon -> AttackFamilySet.empty
| Char _ -> AttackFamilySet.empty
| Choice (l, r) -> exp_attack_rec_choice pref suff l r
| Alternative (l, r) -> exp_attack_rec_alternative pref suff l r
| Concat (l, r) -> exp_attack_rec_concat pref suff l r
| Star (_, e') -> exp_attack_rec_star pref suff e e'

and exp_attack_rec_choice pref suff l r =
and exp_attack_rec_alternative pref suff l r =
AttackFamilySet.union
(exp_attack_rec pref suff l)
(exp_attack_rec pref suff r)

and exp_attack_rec_concat pref suff l r =
AttackFamilySet.union
(exp_attack_rec pref (ExtRe.concat (to_extended_regex r) suff) l)
(exp_attack_rec (ExtRe.concat pref (to_extended_regex l)) suff r)
(exp_attack_rec pref (ExtRe.concat (to_ext_regex r) suff) l)
(exp_attack_rec (ExtRe.concat pref (to_ext_regex l)) suff r)

and exp_attack_rec_star pref suff e e' =
let pref = ExtRe.concat pref (to_extended_regex e) in
let suff = ExtRe.concat (to_extended_regex e) suff in
let negation_suff = ExtRe.compl suff in
let pump = m2 (head e) in
let pref = ExtRe.concat pref (to_ext_regex e) in
let suff = ExtRe.concat (to_ext_regex e) suff in
let negated_suff = ExtRe.compl suff in
let pump = m2 e in
let attack_family =
AttackFamilySet.singleton { prefix = pref; pump; suffix = negation_suff }
AttackFamilySet.singleton { prefix = pref; pump; suffix = negated_suff }
in
let attack_e' = exp_attack_rec pref suff e' in
AttackFamilySet.union attack_family attack_e'
Expand Down
30 changes: 15 additions & 15 deletions src/lib/ExtRe.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(** Extended regualar expressions. *)
type t =
| Empty
| Eps
| Epsilon
| Char of Charset.t
| Concat of t list (* minimum two elements. *)
| Alternative of t list (* sorted, with minimum two elements. *)
Expand All @@ -14,9 +14,9 @@ let rec compare re1 re2 =
| Empty, Empty -> 0
| Empty, _ -> -1
| _, Empty -> 1
| Eps, Eps -> 0
| Eps, _ -> -1
| _, Eps -> 1
| Epsilon, Epsilon -> 0
| Epsilon, _ -> -1
| _, Epsilon -> 1
| Char chrs1, Char chrs2 -> Charset.compare chrs1 chrs2
| Char _, _ -> -1
| _, Char _ -> 1
Expand Down Expand Up @@ -59,7 +59,7 @@ let neg_str_repr = "\xc2\xac"
let rec to_string re =
match re with
| Empty -> empty_str_repr
| Eps -> epsilon_str_repr
| Epsilon -> epsilon_str_repr
| Char a -> Charset.to_string a
| Concat rs -> list_of_res_to_string "" rs
| Star r -> "(" ^ to_string r ^ ")*"
Expand All @@ -76,7 +76,7 @@ let pp fmt re = Format.pp_print_string fmt (to_string re)
(** {1 Smart Constructors.} *)

let empty = Empty
let eps = Eps
let eps = Epsilon
let chr c = if Charset.is_empty c then Empty else Char c

let inter re1 re2 =
Expand Down Expand Up @@ -113,16 +113,16 @@ let concat re1 re2 =
| Concat rs, Concat ts -> Concat (rs @ ts)
| Empty, _ -> Empty
| _, Empty -> Empty
| Eps, _ -> re2
| _, Eps -> re1
| Epsilon, _ -> re2
| _, Epsilon -> re1
| Concat re1', _ -> Concat (re1' @ [ re2 ])
| _, Concat re2' -> Concat (re1 :: re2')
| _ -> Concat [ re1; re2 ]

let star = function
| Star r' -> Star r'
| Eps -> Eps
| Empty -> Eps (* The star of empty is epsilon. *)
| Epsilon -> Epsilon
| Empty -> Epsilon (* The star of empty is epsilon. *)
| _ as r -> Star r

let plus re = concat re (star re)
Expand All @@ -143,7 +143,7 @@ let compl = function
(** [nullable re] is [true] iff [re] recognizes the empty word. *)
let rec nullable = function
| Empty -> false
| Eps -> true
| Epsilon -> true
| Char _ -> false
| Star _ -> true
| Concat res -> List.for_all nullable res
Expand Down Expand Up @@ -206,8 +206,8 @@ end

(** [derivative a re] is the derivative of [re] with respect to the symbols [a]. *)
let rec derivative a = function
| Eps -> Empty
| Char b -> if Charset.subset a b then Eps else Empty
| Epsilon -> Empty
| Char b -> if Charset.subset a b then Epsilon else Empty
| Empty -> Empty
| Concat [] -> failwith "internal, empty concatenation in d"
| Concat [ r ] -> derivative a r
Expand Down Expand Up @@ -236,7 +236,7 @@ let rec derivative a = function

let rec range = function
| Empty -> CharsetSet.any_char_singleton
| Eps -> CharsetSet.any_char_singleton
| Epsilon -> CharsetSet.any_char_singleton
| Char a -> CharsetSet.partition a
| Concat [] -> CharsetSet.any_char_singleton
| Concat (r :: rs) ->
Expand Down Expand Up @@ -303,7 +303,7 @@ let algebra = Aut.Algebra.of_list Utility.all_chars
let rec to_dfa (re : t) : Aut.dfa =
match re with
| Empty -> Aut.N |> Aut.automata_of_regexp algebra
| Eps -> Aut.E |> Aut.automata_of_regexp algebra
| Epsilon -> Aut.E |> Aut.automata_of_regexp algebra
| Char chars -> chars_to_dfa chars
| Concat rs -> combine Aut.concat rs
| Alternative rs -> combine Aut.join rs
Expand Down
10 changes: 5 additions & 5 deletions src/lib/ParserRe.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type body =
| Epsilon
| Char of Charset.t
| Concat of body * body
| Choice of body * body
| Alternative of body * body
| Star of body
| Backreference of int
| Utf8
Expand All @@ -21,7 +21,7 @@ type t = body * flags
let eps = Epsilon
let ch c = Char c
let from_char c = Char (Charset.singleton c)
let choice e1 e2 = Choice (e1, e2)
let choice e1 e2 = Alternative (e1, e2)
let star e1 = match e1 with Epsilon -> Epsilon | _ -> Star e1

let concat e1 e2 =
Expand Down Expand Up @@ -105,7 +105,7 @@ type semantics = Match | Fullmatch
[last_construct_blocks_universal_language "$a*"] is [false]. *)
let rec last_construct_blocks_universal_language = function
| Dollar | WordBoundary | EndOfString -> true
| Epsilon | Char _ | Choice _ | Star _ | Backreference _ | Utf8 -> false
| Epsilon | Char _ | Alternative _ | Star _ | Backreference _ | Utf8 -> false
| Concat (_, re2) -> last_construct_blocks_universal_language re2

(** [remove_some_advanced_features re] removes from [re] some advanced features,
Expand All @@ -120,7 +120,7 @@ let remove_some_advanced_features input : (body, conversion_error) result =
let* re1' = remove_advanced_features_rec false re1 in
let* re2' = remove_advanced_features_rec is_last re2 in
Ok (concat re1' re2')
| Choice (re1, re2) ->
| Alternative (re1, re2) ->
let* re1' = remove_advanced_features_rec false re1 in
let* re2' = remove_advanced_features_rec false re2 in
Ok (choice re1' re2')
Expand Down Expand Up @@ -163,7 +163,7 @@ let rec to_re_body input : (Re.t, conversion_error) result =
let* r1' = to_re_body r1 in
let* r2' = to_re_body r2 in
Ok (Re.concat r1' r2')
| Choice (r1, r2) ->
| Alternative (r1, r2) ->
let* r1' = to_re_body r1 in
let* r2' = to_re_body r2 in
Ok (Re.choice r1' r2')
Expand Down
20 changes: 10 additions & 10 deletions src/lib/Re.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type t =
| Epsilon
| Char of Charset.t
| Concat of t * t
| Choice of t * t
| Alternative of t * t
| Star of bool * t

type transition =
Expand All @@ -17,13 +17,13 @@ let rec is_finite = function
| Epsilon -> true
| Char _ -> true
| Concat (re1, re2) -> is_finite re1 && is_finite re2
| Choice (re1, re2) -> is_finite re1 && is_finite re2
| Alternative (re1, re2) -> is_finite re1 && is_finite re2
| Star (_, Epsilon) -> true (* (eps)* is finite. *)
| Star _ -> false

let eps = Epsilon
let ch c = Char c
let choice e1 e2 = Choice (e1, e2)
let choice e1 e2 = Alternative (e1, e2)

let star ?(expandible = true) e1 =
match e1 with Epsilon -> Epsilon | _ -> Star (expandible, e1)
Expand All @@ -49,19 +49,19 @@ let rec compare e1 e2 =
if comp_e1_e3 <> 0 then comp_e1_e3 else compare e2 e4
| Concat _, _ -> -1
| _, Concat _ -> 1
| Choice (e1, e2), Choice (e3, e4) ->
| Alternative (e1, e2), Alternative (e3, e4) ->
let comp_e1_e3 = compare e1 e3 in
if comp_e1_e3 <> 0 then comp_e1_e3 else compare e2 e4
| Choice _, _ -> -1
| _, Choice _ -> 1
| Alternative _, _ -> -1
| _, Alternative _ -> 1
| Star (b1, e1), Star (b2, e2) ->
if Bool.compare b1 b2 <> 0 then Bool.compare b1 b2 else compare e1 e2

let rec to_string = function
| Epsilon -> U.yellow ^ "ε" ^ U.reset
| Char c -> Charset.to_string c
| Concat (a, b) -> to_string a ^ to_string b
| Choice (a, b) ->
| Alternative (a, b) ->
U.blue ^ "(" ^ U.reset ^ to_string a ^ U.blue ^ ")|(" ^ U.reset
^ to_string b ^ U.blue ^ ")" ^ U.reset
| Star (true, a) ->
Expand All @@ -73,13 +73,13 @@ let rec refesh_stars = function
| Epsilon -> eps
| Char c -> ch c
| Concat (e1, e2) -> concat (refesh_stars e1) (refesh_stars e2)
| Choice (e1, e2) -> choice (refesh_stars e1) (refesh_stars e2)
| Alternative (e1, e2) -> choice (refesh_stars e1) (refesh_stars e2)
| Star (_, e1) -> star (refesh_stars e1)

let rec next = function
| Epsilon -> None
| Char c -> Match (c, eps)
| Choice (l, r) -> LeftOrRight (l, r)
| Alternative (l, r) -> LeftOrRight (l, r)
| Star (false, _) -> None
| Star (true, e') -> ExpandOrNot (concat e' (star ~expandible:false e'), eps)
| Concat (l, r) -> (
Expand All @@ -96,7 +96,7 @@ let rec tail e = match e with Concat (l, r) -> concat (tail l) r | _ -> eps
let rec case_insensitive = function
| Epsilon -> Epsilon
| Concat (r1, r2) -> concat (case_insensitive r1) (case_insensitive r2)
| Choice (r1, r2) -> choice (case_insensitive r1) (case_insensitive r2)
| Alternative (r1, r2) -> choice (case_insensitive r1) (case_insensitive r2)
| Star (b, r) -> star ~expandible:b (case_insensitive r)
| Char cs -> Char (Charset.case_insensitive cs)

Expand Down
4 changes: 2 additions & 2 deletions src/lib/Re.mli
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ type t =
| Epsilon
| Char of Charset.t
| Concat of t * t
| Choice of t * t
| Star of bool * t (* The bool represents if the star can be expanded. *)
| Alternative of t * t
| Star of bool * t (* bool is true if the star can be expanded. *)

val compare : t -> t -> int
val to_string : t -> string
Expand Down

0 comments on commit f7d2357

Please sign in to comment.