summaryrefslogtreecommitdiff
path: root/lib/model/dsl.ml
blob: 0f873f31e70402e50918bae62a31edbd5edba238 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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