open StdLabels exception UnknowFunction of string type 'a t = | Empty | Expr of 'a t | Literal of string | Integer of string | ValuePath of 'a | ContentPath of 'a | Nvl of 'a t list | Join of string * 'a t list | BOperator of binary_operator * 'a t * 'a t | GEquality of binary_operator * 'a t * 'a t list | Function of funct * 'a t list [@@deriving show, eq, ord] and binary_operator = | Equal | Different | Add | Minus | Division | LT | GT | LTE | GTE | And | Or [@@deriving ord] and funct = | Cmp | Trim | Ucase | If | Concat | Matches | Substring | Length | Uniq let name_of_function : funct -> string = function | Ucase -> "ucase" | Trim -> "trim" | Cmp -> "cmp" | If -> "if" | Concat -> "concat" | Matches -> "matches" | Substring -> "substring" | Length -> "length" | Uniq -> "uniq" let function_of_name : 'a t list -> string -> 'a t = fun param f -> match (String.lowercase_ascii f, param) with | "nvl", _ -> Nvl param | "join", Literal sep :: tl -> Join (sep, tl) | "join", Empty :: tl -> Join ("", tl) (* Branch function *) | "if", [ _; _; _ ] -> Function (If, param) (* String functions *) | "matches", _ -> Function (Matches, param) | "substring", _ -> Function (Substring, param) | "length", [ _ ] -> Function (Length, param) | "ucase", [ _ ] -> Function (Ucase, param) | "trim", [ _ ] -> Function (Trim, param) | "concat", _ -> Function (Concat, param) | "uniq", [ _ ] -> Function (Uniq, param) | _ -> (*Function (other, param)*) raise (UnknowFunction f) let name_of_operator = function | Equal -> "=" | Different -> "<>" | Add -> "+" | Minus -> "-" | Division -> "/" | LT -> "<" | GT -> ">" | LTE -> "<=" | GTE -> ">=" | And -> " and " | Or -> " or " let rec cmp : ('a -> 'a -> int) -> 'a t -> 'a t -> int = fun f e1 e2 -> match (e1, e2) with | Empty, Empty -> 0 | Literal l1, Literal l2 -> String.compare l1 l2 | Integer l1, Integer l2 -> String.compare l1 l2 | ValuePath p1, ValuePath p2 -> f p1 p2 | ContentPath p1, ContentPath p2 -> f p1 p2 | BOperator (n1, arg11, arg12), BOperator (n2, arg21, arg22) -> ( match compare_binary_operator n1 n2 with | 0 -> ( match cmp f arg11 arg21 with 0 -> cmp f arg12 arg22 | other -> other) | other -> other) (* Any other case *) | other1, other2 -> Stdlib.compare other1 other2 let fold_values : f:('b -> 'a -> 'b) -> init:'b -> 'a t -> 'b = fun ~f ~init expression -> let rec _f acc = function | Empty | Literal _ | Integer _ -> acc | Expr e -> _f acc e | ValuePath p -> f acc p | ContentPath p -> f acc p | Function (_, pp) | Nvl pp | Join (_, pp) -> List.fold_left ~f:(fun acc a -> _f acc a) ~init:acc pp | BOperator (_, arg1, arg2) -> _f (_f acc arg1) arg2 | GEquality (_, arg1, arg2) -> let eval1 = List.fold_left ~f:(fun acc a -> _f acc a) ~init:acc arg2 in _f eval1 arg1 in _f init expression let map : type a b. f:(a -> b) -> a t -> b t = fun ~f expression -> let rec map = function | Expr e -> Expr (map e) | Empty -> Empty | Literal s -> Literal s | Integer i -> Integer i | ValuePath p -> ValuePath (f p) | ContentPath p -> ContentPath (f p) | Function (name, pp) -> Function (name, List.map ~f:map pp) | Nvl pp -> Nvl (List.map ~f:map pp) | Join (sep, pp) -> Join (sep, List.map ~f:map pp) | BOperator (n, arg1, arg2) -> BOperator (n, map arg1, map arg2) | GEquality (n, arg1, args) -> GEquality (n, map arg1, List.map ~f:map args) in map expression