2018-06-29 16:08:08 +04:00
|
|
|
(*****************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Open Source License *)
|
|
|
|
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.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. *)
|
|
|
|
(* *)
|
|
|
|
(*****************************************************************************)
|
2017-09-29 18:20:10 +04:00
|
|
|
|
2018-05-16 20:46:55 +04:00
|
|
|
type annot = string list
|
|
|
|
|
2017-09-29 18:20:10 +04:00
|
|
|
type ('l, 'p) node =
|
2018-03-28 22:42:10 +04:00
|
|
|
| Int of 'l * Z.t
|
2017-09-29 18:20:10 +04:00
|
|
|
| String of 'l * string
|
2018-06-13 16:41:36 +04:00
|
|
|
| Bytes of 'l * MBytes.t
|
2018-05-16 20:46:55 +04:00
|
|
|
| Prim of 'l * 'p * ('l, 'p) node list * annot
|
2018-05-17 21:37:25 +04:00
|
|
|
| Seq of 'l * ('l, 'p) node list
|
2017-09-29 18:20:10 +04:00
|
|
|
|
|
|
|
type canonical_location = int
|
|
|
|
|
|
|
|
type 'p canonical = Canonical of (canonical_location, 'p) node
|
|
|
|
|
|
|
|
let canonical_location_encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
def
|
2018-05-29 16:57:59 +04:00
|
|
|
"micheline.location"
|
2017-09-29 18:20:10 +04:00
|
|
|
~title:
|
|
|
|
"Canonical location in a Micheline expression"
|
|
|
|
~description:
|
|
|
|
"The location of a node in a Micheline expression tree \
|
|
|
|
in prefix order, with zero being the root and adding one \
|
|
|
|
for every basic node, sequence and primitive application." @@
|
|
|
|
int31
|
|
|
|
|
|
|
|
let location = function
|
2017-11-13 19:34:00 +04:00
|
|
|
| Int (loc, _) -> loc
|
|
|
|
| String (loc, _) -> loc
|
2018-06-13 16:41:36 +04:00
|
|
|
| Bytes (loc, _) -> loc
|
2018-05-17 21:37:25 +04:00
|
|
|
| Seq (loc, _) -> loc
|
2017-11-13 19:34:00 +04:00
|
|
|
| Prim (loc, _, _, _) -> loc
|
2017-09-29 18:20:10 +04:00
|
|
|
|
2018-05-17 21:37:25 +04:00
|
|
|
let annotations = function
|
|
|
|
| Int (_, _) -> []
|
|
|
|
| String (_, _) -> []
|
2018-06-13 16:41:36 +04:00
|
|
|
| Bytes (_, _) -> []
|
2018-05-17 21:37:25 +04:00
|
|
|
| Seq (_, _) -> []
|
|
|
|
| Prim (_, _, _, annots) -> annots
|
2017-09-29 18:20:10 +04:00
|
|
|
|
|
|
|
let root (Canonical expr) = expr
|
|
|
|
|
|
|
|
let strip_locations root =
|
|
|
|
let id = let id = ref (-1) in fun () -> incr id ; !id in
|
|
|
|
let rec strip_locations l =
|
|
|
|
let id = id () in
|
|
|
|
match l with
|
|
|
|
| Int (_, v) ->
|
|
|
|
Int (id, v)
|
|
|
|
| String (_, v) ->
|
|
|
|
String (id, v)
|
2018-06-13 16:41:36 +04:00
|
|
|
| Bytes (_, v) ->
|
|
|
|
Bytes (id, v)
|
2018-05-17 21:37:25 +04:00
|
|
|
| Seq (_, seq) ->
|
|
|
|
Seq (id, List.map strip_locations seq)
|
|
|
|
| Prim (_, name, seq, annots) ->
|
|
|
|
Prim (id, name, List.map strip_locations seq, annots) in
|
2017-09-29 18:20:10 +04:00
|
|
|
Canonical (strip_locations root)
|
|
|
|
|
|
|
|
let extract_locations root =
|
|
|
|
let id = let id = ref (-1) in fun () -> incr id ; !id in
|
|
|
|
let loc_table = ref [] in
|
|
|
|
let rec strip_locations l =
|
|
|
|
let id = id () in
|
|
|
|
match l with
|
|
|
|
| Int (loc, v) ->
|
|
|
|
loc_table := (id, loc) :: !loc_table ;
|
|
|
|
Int (id, v)
|
|
|
|
| String (loc, v) ->
|
|
|
|
loc_table := (id, loc) :: !loc_table ;
|
|
|
|
String (id, v)
|
2018-06-13 16:41:36 +04:00
|
|
|
| Bytes (loc, v) ->
|
|
|
|
loc_table := (id, loc) :: !loc_table ;
|
|
|
|
Bytes (id, v)
|
2018-05-17 21:37:25 +04:00
|
|
|
| Seq (loc, seq) ->
|
2017-09-29 18:20:10 +04:00
|
|
|
loc_table := (id, loc) :: !loc_table ;
|
2018-05-17 21:37:25 +04:00
|
|
|
Seq (id, List.map strip_locations seq)
|
|
|
|
| Prim (loc, name, seq, annots) ->
|
2017-09-29 18:20:10 +04:00
|
|
|
loc_table := (id, loc) :: !loc_table ;
|
2018-05-17 21:37:25 +04:00
|
|
|
Prim (id, name, List.map strip_locations seq, annots) in
|
2017-09-29 18:20:10 +04:00
|
|
|
let stripped = strip_locations root in
|
|
|
|
Canonical stripped, List.rev !loc_table
|
|
|
|
|
|
|
|
let inject_locations lookup (Canonical root) =
|
|
|
|
let rec inject_locations l =
|
|
|
|
match l with
|
|
|
|
| Int (loc, v) ->
|
|
|
|
Int (lookup loc, v)
|
|
|
|
| String (loc, v) ->
|
|
|
|
String (lookup loc, v)
|
2018-06-13 16:41:36 +04:00
|
|
|
| Bytes (loc, v) ->
|
|
|
|
Bytes (lookup loc, v)
|
2018-05-17 21:37:25 +04:00
|
|
|
| Seq (loc, seq) ->
|
|
|
|
Seq (lookup loc, List.map inject_locations seq)
|
|
|
|
| Prim (loc, name, seq, annots) ->
|
|
|
|
Prim (lookup loc, name, List.map inject_locations seq, annots) in
|
2017-09-29 18:20:10 +04:00
|
|
|
inject_locations root
|
|
|
|
|
|
|
|
let map f (Canonical expr) =
|
|
|
|
let rec map_node f = function
|
2018-06-13 16:41:36 +04:00
|
|
|
| Int _ | String _ | Bytes _ as node -> node
|
2018-05-17 21:37:25 +04:00
|
|
|
| Seq (loc, seq) ->
|
|
|
|
Seq (loc, List.map (map_node f) seq)
|
|
|
|
| Prim (loc, name, seq, annots) ->
|
|
|
|
Prim (loc, f name, List.map (map_node f) seq, annots) in
|
2017-09-29 18:20:10 +04:00
|
|
|
Canonical (map_node f expr)
|
|
|
|
|
|
|
|
let rec map_node fl fp = function
|
|
|
|
| Int (loc, v) ->
|
2017-11-13 19:34:00 +04:00
|
|
|
Int (fl loc, v)
|
2017-09-29 18:20:10 +04:00
|
|
|
| String (loc, v) ->
|
2017-11-13 19:34:00 +04:00
|
|
|
String (fl loc, v)
|
2018-06-13 16:41:36 +04:00
|
|
|
| Bytes (loc, v) ->
|
|
|
|
Bytes (fl loc, v)
|
2018-05-17 21:37:25 +04:00
|
|
|
| Seq (loc, seq) ->
|
|
|
|
Seq (fl loc, List.map (map_node fl fp) seq)
|
|
|
|
| Prim (loc, name, seq, annots) ->
|
|
|
|
Prim (fl loc, fp name, List.map (map_node fl fp) seq, annots)
|
2017-09-29 18:20:10 +04:00
|
|
|
|
2018-03-14 14:36:09 +04:00
|
|
|
let canonical_encoding ~variant prim_encoding =
|
2017-09-29 18:20:10 +04:00
|
|
|
let open Data_encoding in
|
|
|
|
let int_encoding =
|
2018-03-28 22:42:10 +04:00
|
|
|
obj1 (req "int" z) in
|
2017-09-29 18:20:10 +04:00
|
|
|
let string_encoding =
|
|
|
|
obj1 (req "string" string) in
|
2018-06-13 16:41:36 +04:00
|
|
|
let bytes_encoding =
|
|
|
|
obj1 (req "bytes" bytes) in
|
2017-12-07 21:36:42 +04:00
|
|
|
let int_encoding tag =
|
|
|
|
case tag int_encoding
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Int"
|
2017-12-07 21:36:42 +04:00
|
|
|
(function Int (_, v) -> Some v | _ -> None)
|
|
|
|
(fun v -> Int (0, v)) in
|
|
|
|
let string_encoding tag =
|
|
|
|
case tag string_encoding
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"String"
|
2017-12-07 21:36:42 +04:00
|
|
|
(function String (_, v) -> Some v | _ -> None)
|
|
|
|
(fun v -> String (0, v)) in
|
2018-06-13 16:41:36 +04:00
|
|
|
let bytes_encoding tag =
|
|
|
|
case tag bytes_encoding
|
|
|
|
~title:"Bytes"
|
|
|
|
(function Bytes (_, v) -> Some v | _ -> None)
|
|
|
|
(fun v -> Bytes (0, v)) in
|
2017-12-07 21:36:42 +04:00
|
|
|
let seq_encoding tag expr_encoding =
|
|
|
|
case tag (list expr_encoding)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Sequence"
|
2018-05-17 21:37:25 +04:00
|
|
|
(function Seq (_, v) -> Some v | _ -> None)
|
|
|
|
(fun args -> Seq (0, args)) in
|
2018-06-12 17:35:35 +04:00
|
|
|
let annots_encoding =
|
|
|
|
let split s =
|
2018-07-13 17:50:48 +04:00
|
|
|
if s = "" then []
|
|
|
|
else
|
|
|
|
let annots = String.split_on_char ' ' s in
|
|
|
|
List.iter (fun a ->
|
|
|
|
if String.length a > 255 then failwith "Oversized annotation"
|
|
|
|
) annots;
|
|
|
|
if String.concat " " annots <> s then
|
|
|
|
failwith "Invalid annotation string, \
|
|
|
|
must be a sequence of valid annotations with spaces" ;
|
|
|
|
annots in
|
2018-06-12 17:35:35 +04:00
|
|
|
splitted
|
|
|
|
~json:(list (Bounded.string 255))
|
|
|
|
~binary:(conv (String.concat " ") split string) in
|
2017-12-07 21:36:42 +04:00
|
|
|
let application_encoding tag expr_encoding =
|
|
|
|
case tag
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Generic prim (any number of args with or without annot)"
|
2017-12-07 21:36:42 +04:00
|
|
|
(obj3 (req "prim" prim_encoding)
|
2018-05-17 21:37:25 +04:00
|
|
|
(dft "args" (list expr_encoding) [])
|
2018-06-12 17:35:35 +04:00
|
|
|
(dft "annots" annots_encoding []))
|
2018-05-17 21:37:25 +04:00
|
|
|
(function Prim (_, prim, args, annots) -> Some (prim, args, annots)
|
2017-12-07 21:36:42 +04:00
|
|
|
| _ -> None)
|
2018-05-17 21:37:25 +04:00
|
|
|
(fun (prim, args, annots) -> Prim (0, prim, args, annots)) in
|
2018-03-14 14:36:09 +04:00
|
|
|
let node_encoding = mu ("micheline." ^ variant ^ ".expression") (fun expr_encoding ->
|
2017-12-07 21:36:42 +04:00
|
|
|
splitted
|
|
|
|
~json:(union ~tag_size:`Uint8
|
|
|
|
[ int_encoding Json_only;
|
|
|
|
string_encoding Json_only ;
|
2018-06-14 15:53:52 +04:00
|
|
|
bytes_encoding Json_only ;
|
2017-12-07 21:36:42 +04:00
|
|
|
seq_encoding Json_only expr_encoding ;
|
|
|
|
application_encoding Json_only expr_encoding ])
|
|
|
|
~binary:(union ~tag_size:`Uint8
|
|
|
|
[ int_encoding (Tag 0) ;
|
|
|
|
string_encoding (Tag 1) ;
|
|
|
|
seq_encoding (Tag 2) expr_encoding ;
|
|
|
|
(* No args, no annot *)
|
|
|
|
case (Tag 3)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Prim (no args, annot)"
|
2017-12-07 21:36:42 +04:00
|
|
|
(obj1 (req "prim" prim_encoding))
|
2018-05-17 21:37:25 +04:00
|
|
|
(function Prim (_, v, [], []) -> Some v
|
2017-12-07 21:36:42 +04:00
|
|
|
| _ -> None)
|
2018-05-17 21:37:25 +04:00
|
|
|
(fun v -> Prim (0, v, [], [])) ;
|
|
|
|
(* No args, with annots *)
|
2017-12-07 21:36:42 +04:00
|
|
|
case (Tag 4)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Prim (no args + annot)"
|
2017-12-07 21:36:42 +04:00
|
|
|
(obj2 (req "prim" prim_encoding)
|
2018-06-12 17:35:35 +04:00
|
|
|
(req "annots" annots_encoding))
|
2017-12-07 21:36:42 +04:00
|
|
|
(function
|
2018-05-17 21:37:25 +04:00
|
|
|
| Prim (_, v, [], annots) -> Some (v, annots)
|
2017-12-07 21:36:42 +04:00
|
|
|
| _ -> None)
|
2018-05-17 21:37:25 +04:00
|
|
|
(function (prim, annots) -> Prim (0, prim, [], annots)) ;
|
2017-12-07 21:36:42 +04:00
|
|
|
(* Single arg, no annot *)
|
|
|
|
case (Tag 5)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Prim (1 arg, no annot)"
|
2017-12-07 21:36:42 +04:00
|
|
|
(obj2 (req "prim" prim_encoding)
|
|
|
|
(req "arg" expr_encoding))
|
|
|
|
(function
|
2018-05-17 21:37:25 +04:00
|
|
|
| Prim (_, v, [ arg ], []) -> Some (v, arg)
|
2017-12-07 21:36:42 +04:00
|
|
|
| _ -> None)
|
2018-05-17 21:37:25 +04:00
|
|
|
(function (prim, arg) -> Prim (0, prim, [ arg ], [])) ;
|
2017-12-07 21:36:42 +04:00
|
|
|
(* Single arg, with annot *)
|
|
|
|
case (Tag 6)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Prim (1 arg + annot)"
|
2017-12-07 21:36:42 +04:00
|
|
|
(obj3 (req "prim" prim_encoding)
|
|
|
|
(req "arg" expr_encoding)
|
2018-06-12 17:35:35 +04:00
|
|
|
(req "annots" annots_encoding))
|
2017-12-07 21:36:42 +04:00
|
|
|
(function
|
2018-05-17 21:37:25 +04:00
|
|
|
| Prim (_, prim, [ arg ], annots) -> Some (prim, arg, annots)
|
2017-12-07 21:36:42 +04:00
|
|
|
| _ -> None)
|
2018-05-17 21:37:25 +04:00
|
|
|
(fun (prim, arg, annots) -> Prim (0, prim, [ arg ], annots)) ;
|
2017-12-07 21:36:42 +04:00
|
|
|
(* Two args, no annot *)
|
|
|
|
case (Tag 7)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Prim (2 args, no annot)"
|
2017-12-07 21:36:42 +04:00
|
|
|
(obj3 (req "prim" prim_encoding)
|
|
|
|
(req "arg1" expr_encoding)
|
|
|
|
(req "arg2" expr_encoding))
|
|
|
|
(function
|
2018-05-17 21:37:25 +04:00
|
|
|
| Prim (_, prim, [ arg1 ; arg2 ], []) -> Some (prim, arg1, arg2)
|
2017-12-07 21:36:42 +04:00
|
|
|
| _ -> None)
|
2018-05-17 21:37:25 +04:00
|
|
|
(fun (prim, arg1, arg2) -> Prim (0, prim, [ arg1 ; arg2 ], [])) ;
|
|
|
|
(* Two args, with annots *)
|
2017-12-07 21:36:42 +04:00
|
|
|
case (Tag 8)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Prim (2 args + annot)"
|
2017-12-07 21:36:42 +04:00
|
|
|
(obj4 (req "prim" prim_encoding)
|
|
|
|
(req "arg1" expr_encoding)
|
|
|
|
(req "arg2" expr_encoding)
|
2018-06-12 17:35:35 +04:00
|
|
|
(req "annots" annots_encoding))
|
2017-12-07 21:36:42 +04:00
|
|
|
(function
|
2018-05-17 21:37:25 +04:00
|
|
|
| Prim (_, prim, [ arg1 ; arg2 ], annots) -> Some (prim, arg1, arg2, annots)
|
2017-12-07 21:36:42 +04:00
|
|
|
| _ -> None)
|
2018-05-17 21:37:25 +04:00
|
|
|
(fun (prim, arg1, arg2, annots) -> Prim (0, prim, [ arg1 ; arg2 ], annots)) ;
|
2017-12-07 21:36:42 +04:00
|
|
|
(* General case *)
|
2018-06-13 16:41:36 +04:00
|
|
|
application_encoding (Tag 9) expr_encoding ;
|
|
|
|
bytes_encoding (Tag 10) ]))
|
2017-12-07 21:36:42 +04:00
|
|
|
in
|
2017-09-29 18:20:10 +04:00
|
|
|
conv
|
|
|
|
(function Canonical node -> node)
|
|
|
|
(fun node -> strip_locations node)
|
|
|
|
node_encoding
|
|
|
|
|
2018-03-14 14:36:09 +04:00
|
|
|
let table_encoding ~variant location_encoding prim_encoding =
|
2017-09-29 18:20:10 +04:00
|
|
|
let open Data_encoding in
|
|
|
|
conv
|
|
|
|
(fun node ->
|
|
|
|
let canon, assoc = extract_locations node in
|
|
|
|
let _, table = List.split assoc in
|
|
|
|
(canon, table))
|
|
|
|
(fun (canon, table) ->
|
|
|
|
let table = Array.of_list table in
|
|
|
|
inject_locations (fun i -> table.(i)) canon)
|
|
|
|
(obj2
|
2018-03-14 14:36:09 +04:00
|
|
|
(req "expression" (canonical_encoding ~variant prim_encoding))
|
2017-09-29 18:20:10 +04:00
|
|
|
(req "locations" (list location_encoding)))
|
|
|
|
|
2018-03-14 14:36:09 +04:00
|
|
|
let erased_encoding ~variant default_location prim_encoding =
|
2017-09-29 18:20:10 +04:00
|
|
|
let open Data_encoding in
|
|
|
|
conv
|
|
|
|
(fun node -> strip_locations node)
|
|
|
|
(fun canon -> inject_locations (fun _ -> default_location) canon)
|
2018-03-14 14:36:09 +04:00
|
|
|
(canonical_encoding ~variant prim_encoding)
|