package tezos-protocol-alpha

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file script_ir_annot.ml

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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com>     *)
(* Copyright (c) 2019-2022 Nomadic Labs, <contact@nomadic-labs.com>          *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Alpha_context
open Micheline
open Script_tc_errors

type var_annot = Var_annot

type type_annot = Type_annot

type field_annot = Field_annot of Non_empty_string.t [@@ocaml.unboxed]

let error_unexpected_annot loc annot =
  let open Result_syntax in
  match annot with
  | [] -> return_unit
  | _ :: _ -> tzfail (Unexpected_annotation loc)

(* Check that the predicate p holds on all s.[k] for k >= i *)
let string_iter p s i =
  let open Result_syntax in
  let len = String.length s in
  let rec aux i =
    if Compare.Int.(i >= len) then return_unit
    else
      let* () = p s.[i] in
      aux (i + 1)
  in
  aux i

let is_allowed_char = function
  | 'a' .. 'z' | 'A' .. 'Z' | '_' | '.' | '%' | '@' | '0' .. '9' -> true
  | _ -> false

(* Valid annotation characters as defined by the allowed_annot_char function from lib_micheline/micheline_parser *)
let check_char loc c =
  let open Result_syntax in
  if is_allowed_char c then return_unit else tzfail (Unexpected_annotation loc)

(* This constant is defined in lib_micheline/micheline_parser which is not available in the environment. *)
let max_annot_length = 255

type annot_opt =
  | Field_annot_opt of Non_empty_string.t option
  | Type_annot_opt of type_annot option
  | Var_annot_opt of var_annot option

let at = Non_empty_string.of_string_exn "@"

let parse_annot loc s =
  let open Result_syntax in
  (* allow empty annotations as wildcards but otherwise only accept
     annotations that start with [a-zA-Z_] *)
  let sub_or_wildcard wrap s =
    match Non_empty_string.of_string s with
    | None -> return @@ wrap None
    | Some s -> (
        match (s :> string).[0] with
        | 'a' .. 'z' | 'A' .. 'Z' | '_' | '0' .. '9' ->
            (* check that all characters are valid*)
            let* () = string_iter (check_char loc) (s :> string) 1 in
            return @@ wrap (Some s)
        | _ -> tzfail (Unexpected_annotation loc))
  in
  let len = String.length s in
  if Compare.Int.(len = 0 || len > max_annot_length) then
    tzfail (Unexpected_annotation loc)
  else
    let rest = String.sub s 1 (len - 1) in
    match s.[0] with
    | ':' ->
        sub_or_wildcard
          (fun a ->
            Type_annot_opt
              (Option.map (fun (_ : Non_empty_string.t) -> Type_annot) a))
          rest
    | '@' ->
        sub_or_wildcard
          (fun a ->
            Var_annot_opt
              (Option.map (fun (_ : Non_empty_string.t) -> Var_annot) a))
          rest
    | '%' -> sub_or_wildcard (fun a -> Field_annot_opt a) rest
    | _ -> tzfail (Unexpected_annotation loc)

let parse_annots loc ?(allow_special_var = false) ?(allow_special_field = false)
    l =
  let open Result_syntax in
  List.map_e
    (function
      | "@%" when allow_special_var -> return @@ Var_annot_opt (Some Var_annot)
      | "@%%" when allow_special_var -> return @@ Var_annot_opt (Some Var_annot)
      | "%@" when allow_special_field -> return @@ Field_annot_opt (Some at)
      | s -> parse_annot loc s)
    l

let opt_field_of_field_opt = function
  | None -> None
  | Some a -> Some (Field_annot a)

let classify_annot loc l :
    (var_annot option list * type_annot option list * field_annot option list)
    tzresult =
  let open Result_syntax in
  try
    let _, rv, _, rt, _, rf =
      List.fold_left
        (fun (in_v, rv, in_t, rt, in_f, rf) a ->
          match (a, in_v, rv, in_t, rt, in_f, rf) with
          | Var_annot_opt a, true, _, _, _, _, _
          | Var_annot_opt a, false, [], _, _, _, _ ->
              (true, a :: rv, false, rt, false, rf)
          | Type_annot_opt a, _, _, true, _, _, _
          | Type_annot_opt a, _, _, false, [], _, _ ->
              (false, rv, true, a :: rt, false, rf)
          | Field_annot_opt a, _, _, _, _, true, _
          | Field_annot_opt a, _, _, _, _, false, [] ->
              (false, rv, false, rt, true, opt_field_of_field_opt a :: rf)
          | _ -> raise Exit)
        (false, [], false, [], false, [])
        l
    in
    return (List.rev rv, List.rev rt, List.rev rf)
  with Exit -> tzfail (Ungrouped_annotations loc)

let get_one_annot loc =
  let open Result_syntax in
  function
  | [] -> return_none
  | [a] -> return a
  | _ -> tzfail (Unexpected_annotation loc)

let get_two_annot loc =
  let open Result_syntax in
  function
  | [] -> return (None, None)
  | [a] -> return (a, None)
  | [a; b] -> return (a, b)
  | _ -> tzfail (Unexpected_annotation loc)

let check_type_annot loc annot =
  let open Result_syntax in
  let* vars, types, fields =
    let* annots = parse_annots loc annot in
    classify_annot loc annots
  in
  let* () = error_unexpected_annot loc vars in
  let* () = error_unexpected_annot loc fields in
  let+ (_a : type_annot option) = get_one_annot loc types in
  ()

let parse_field_annot :
    Script.location -> string -> Non_empty_string.t option tzresult =
  let open Result_syntax in
  fun loc annot ->
    if Compare.Int.(String.length annot <= 0) || Compare.Char.(annot.[0] <> '%')
    then return_none
    else
      let+ annot_opt = parse_annot loc annot in
      match annot_opt with Field_annot_opt annot_opt -> annot_opt | _ -> None

let is_field_annot loc a =
  let open Result_syntax in
  let+ result = parse_field_annot loc a in
  Option.is_some result

let extract_field_annot :
    Script.node -> (Script.node * Non_empty_string.t option) tzresult =
  let open Result_syntax in
  function
  | Prim (loc, prim, args, annot) as expr ->
      let rec extract_first acc = function
        | [] -> return (expr, None)
        | s :: rest -> (
            let* str_opt = parse_field_annot loc s in
            match str_opt with
            | None -> extract_first (s :: acc) rest
            | Some _ as some_field_annot ->
                let annot = List.rev_append acc rest in
                return (Prim (loc, prim, args, annot), some_field_annot))
      in
      extract_first [] annot
  | expr -> return (expr, None)

let has_field_annot node =
  let open Result_syntax in
  let+ _node, result = extract_field_annot node in
  Option.is_some result

let remove_field_annot node =
  let open Result_syntax in
  let+ node, _a = extract_field_annot node in
  node

let extract_entrypoint_annot node =
  let open Result_syntax in
  let+ node, field_annot_opt = extract_field_annot node in
  ( node,
    Option.bind field_annot_opt (fun field_annot ->
        Entrypoint.of_annot_lax_opt field_annot) )

let check_var_annot loc annot =
  let open Result_syntax in
  let* vars, types, fields =
    let* annots = parse_annots loc annot in
    classify_annot loc annots
  in
  let* () = error_unexpected_annot loc types in
  let* () = error_unexpected_annot loc fields in
  let+ (_a : var_annot option) = get_one_annot loc vars in
  ()

let check_constr_annot loc annot =
  let open Result_syntax in
  let* vars, types, fields =
    let* annots = parse_annots ~allow_special_field:true loc annot in
    classify_annot loc annots
  in
  let* (_v : var_annot option) = get_one_annot loc vars in
  let* (_t : type_annot option) = get_one_annot loc types in
  let+ _f1, _f2 = get_two_annot loc fields in
  ()

let check_two_var_annot loc annot =
  let open Result_syntax in
  let* vars, types, fields =
    let* annots = parse_annots loc annot in
    classify_annot loc annots
  in
  let* () = error_unexpected_annot loc types in
  let* () = error_unexpected_annot loc fields in
  let+ _a1, _a2 = get_two_annot loc vars in
  ()

let check_destr_annot loc annot =
  let open Result_syntax in
  let* vars, types, fields =
    let* annots = parse_annots loc ~allow_special_var:true annot in
    classify_annot loc annots
  in
  let* () = error_unexpected_annot loc types in
  let* (_v : var_annot option) = get_one_annot loc vars in
  let+ (_f : field_annot option) = get_one_annot loc fields in
  ()

let check_unpair_annot loc annot =
  let open Result_syntax in
  let* vars, types, fields =
    let* annots = parse_annots loc ~allow_special_var:true annot in
    classify_annot loc annots
  in
  let* () = error_unexpected_annot loc types in
  let* _vcar, _vcdr = get_two_annot loc vars in
  let+ _f1, _f2 = get_two_annot loc fields in
  ()

let parse_entrypoint_annot loc annot =
  let open Result_syntax in
  let* vars, types, fields =
    let* annots = parse_annots loc annot in
    classify_annot loc annots
  in
  let* () = error_unexpected_annot loc types in
  let* f = get_one_annot loc fields in
  let+ (_v : var_annot option) = get_one_annot loc vars in
  f

let parse_entrypoint_annot_strict loc annot =
  let open Result_syntax in
  let* entrypoint_annot = parse_entrypoint_annot loc annot in
  match entrypoint_annot with
  | None -> Ok Entrypoint.default
  | Some (Field_annot a) -> Entrypoint.of_annot_strict ~loc a

let parse_entrypoint_annot_lax loc annot =
  let open Result_syntax in
  let* entrypoint_annot = parse_entrypoint_annot loc annot in
  match entrypoint_annot with
  | None -> Ok Entrypoint.default
  | Some (Field_annot annot) -> Entrypoint.of_annot_lax annot

let check_var_type_annot loc annot =
  let open Result_syntax in
  let* vars, types, fields =
    let* annots = parse_annots loc annot in
    classify_annot loc annots
  in
  let* () = error_unexpected_annot loc fields in
  let* (_v : var_annot option) = get_one_annot loc vars in
  let+ (_t : type_annot option) = get_one_annot loc types in
  ()
OCaml

Innovation. Community. Security.