2017-09-29 18:20:10 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2018-02-06 00:17:03 +04:00
|
|
|
(* Copyright (c) 2014 - 2018. *)
|
2017-09-29 18:20:10 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
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-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-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 (_, _) -> []
|
|
|
|
| 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-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-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-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
|
|
|
|
| Int _ | String _ 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-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
|
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
|
|
|
|
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-03-31 06:58:11 +04:00
|
|
|
let byte_string = Bounded.string 255 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) [])
|
|
|
|
(dft "annots" (list byte_string) []))
|
|
|
|
(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 ;
|
|
|
|
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-05-17 21:37:25 +04:00
|
|
|
(req "annots" (list byte_string)))
|
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-05-17 21:37:25 +04:00
|
|
|
(req "annots" (list byte_string)))
|
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-05-17 21:37:25 +04:00
|
|
|
(req "annots" (list byte_string)))
|
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 *)
|
|
|
|
application_encoding (Tag 9) expr_encoding ]))
|
|
|
|
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)
|