Commit 1fccffb6 authored by Alain Mebsout's avatar Alain Mebsout Committed by Benjamin Canou

Michelson: keep field annotations in SET_CADR, MAP_CADR macros

parent 040fa2a0
......@@ -2149,7 +2149,7 @@ Syntax
Primitive applications can receive one or many annotations.
An annotation is a sequence of characters that matches the regular
expression ``[@:%](|[@%]|[_a-ZA-Z][_0-9a-zA-Z\.]*)``. They come after
expression ``[@:%](|@|%|%%|[_a-ZA-Z][_0-9a-zA-Z\.]*)``. They come after
the primitive name and before its potential arguments.
::
......@@ -2310,24 +2310,32 @@ type (which can be changed). For instance the annotated typing rule for
Special Annotations
~~~~~~~~~~~~~~~~~~~
The special variable annotation ``@%`` can be used on instructions
The special variable annotations ``@%%`` can be used on instructions
``CAR`` and ``CDR``. It means to use the accessed field name (if any) as
a name for the value on the stack.
a name for the value on the stack. The following typing rule
demonstrates their use for instruction ``CAR``.
::
CAR @%
:: (pair ('a %fst) ('b %snd)) : 'S -> @fst 'a : 'S
:: @p (pair ('a %fst) ('b %snd)) : 'S -> @fst 'a : 'S
CAR @%%
:: @p (pair ('a %fst) ('b %snd)) : 'S -> @p.fst 'a : 'S
The special variable annotation ``%@`` can be used on instructions
``PAIR``, ``SOME``, ``LEFT``, ``RIGHT``. It means to use the variable
name annotation in the stack as a field name for the constructed
element. An example with ``PAIR`` follows,
element. Two examples with ``PAIR`` follows, notice the special
treatment of annotations with `.`.
::
PAIR %@ %@
:: @x 'a : @y 'b : 'S -> (pair ('a %x) ('b %y)) : 'S
PAIR %@ %@
:: @p.x 'a : @p.y 'b : 'S -> @p (pair ('a %x) ('b %y)) : 'S
:: @p.x 'a : @q.y 'b : 'S -> (pair ('a %x) ('b %y)) : 'S
XI - JSON syntax
---------------
......
......@@ -172,7 +172,7 @@ Overrides `michelson-print-errors' and `michelson-highlight-errors'"
(defconst michelson-font-lock-defaults
(list
(list
'("\\<[@]\\(\\|%\\|[A-Za-z-_][A-Za-z-_0-9\.]*\\)\\>" . michelson-face-var-annotation)
'("\\<[@]\\(\\|%\\|%%\\|[A-Za-z-_][A-Za-z-_0-9\.]*\\)\\>" . michelson-face-var-annotation)
'("\\<[%:]\\(\\|@\\|[A-Za-z-_][A-Za-z-_0-9\.]*\\)\\>" . michelson-face-type-annotation)
'("\\<[0-9]+\\>" . michelson-face-constant)
'("\\<[A-Z][a-z_0-9]+\\>" . michelson-face-constant)
......@@ -501,7 +501,7 @@ If `DO-NOT-OVERWRITE' is non-nil, the existing contents of the buffer are mainta
(lexical-let*
((pp-no-trailing-newline
(lambda (sexp)
(let* ((str (pp-to-string sexp))
(let* ((str (replace-regexp-in-string "\\\\\." "." (pp-to-string sexp)))
(len (length str)))
(if (equal "\n" (substring str (- len 1) len))
(substring str 0 (- len 1))
......
parameter (option key_hash) ;
storage (pair
(pair %mgr1 (address %addr) (option key_hash))
(pair %mgr2 (address %addr) (option key_hash))) ;
(pair %mgr1 (address %addr) (option %key key_hash))
(pair %mgr2 (address %addr) (option %key key_hash))) ;
code { # Update the storage
DUP ; CDAAR %addr @%; SOURCE ; PAIR %@ %@; UNPAIR;
IFCMPEQ
{ UNPAIR ; SWAP ; SET_CADR }
{ UNPAIR ; SWAP ; SET_CADR %key @changed_mgr1_key }
{ DUP ; CDDAR ; SOURCE ;
IFCMPEQ
{ UNPAIR ; SWAP ; SET_CDDR }
{ UNPAIR ; SWAP ; SET_CDDR %key }
{ FAIL } } ;
# Now compare the proposals
DUP ; CADR ;
......
......@@ -131,8 +131,14 @@ let parse_annots loc ?(allow_special_var = false) ?(allow_special_field = false)
else match s.[1] with
| 'a' .. 'z' | 'A' .. 'Z' | '_' ->
ok @@ wrap (Some (String.sub s 1 (len - 1))) :: acc
| ('%' | '@' as c) when Compare.Int.(len = 2) && List.mem c specials ->
ok @@ wrap (Some (String.sub s 1 (len - 1))) :: acc
| '@' when Compare.Int.(len = 2) && List.mem '@' specials ->
ok @@ wrap (Some "@") :: acc
| '%' when List.mem '%' specials ->
if Compare.Int.(len = 2)
then ok @@ wrap (Some "%") :: acc
else if Compare.Int.(len = 3) && Compare.Char.(s.[2] = '%')
then ok @@ wrap (Some "%%") :: acc
else error (Unexpected_annotation loc)
| _ -> error (Unexpected_annotation loc) in
List.fold_left (fun acc s ->
match acc with
......@@ -300,6 +306,27 @@ let parse_var_annot
| Some a -> a
| None -> None
let split_last_dot = function
| None -> None, None
| Some `Field_annot s ->
try
let i = String.rindex s '.' in
let s1 = String.sub s 0 i in
let s2 = String.sub s (i + 1) (String.length s - i - 1) in
let f =
if Compare.String.equal s2 "car" || Compare.String.equal s2 "cdr"
then None
else Some (`Field_annot s2) in
Some (`Var_annot s1), f
with Not_found -> None, Some (`Field_annot s)
let common_prefix v1 v2 =
match v1, v2 with
| Some (`Var_annot s1), Some (`Var_annot s2) when Compare.String.equal s1 s2 -> v1
| Some _, None -> v1
| None, Some _ -> v2
| _, _ -> None
let parse_constr_annot
: int ->
?if_special_first:field_annot option ->
......@@ -313,15 +340,20 @@ let parse_constr_annot
get_one_annot loc types >>? fun t ->
get_two_annot loc fields >>? fun (f1, f2) ->
begin match if_special_first, f1 with
| Some special_var, Some `Field_annot "@" -> ok special_var
| Some special_var, Some `Field_annot "@" ->
ok (split_last_dot special_var)
| None, Some `Field_annot "@" -> error (Unexpected_annotation loc)
| _, _ -> ok f1
end >>? fun f1 ->
| _, _ -> ok (v, f1)
end >>? fun (v1, f1) ->
begin match if_special_second, f2 with
| Some special_var, Some `Field_annot "@" -> ok special_var
| Some special_var, Some `Field_annot "@" ->
ok (split_last_dot special_var)
| None, Some `Field_annot "@" -> error (Unexpected_annotation loc)
| _, _ -> ok f2
end >|? fun f2 ->
| _, _ -> ok (v, f2)
end >|? fun (v2, f2) ->
let v = match v with
| None -> common_prefix v1 v2
| Some _ -> v in
(v, t, f1, f2)
let parse_two_var_annot
......@@ -333,19 +365,24 @@ let parse_two_var_annot
error_unexpected_annot loc fields >>? fun () ->
get_two_annot loc vars
let parse_var_field_annot
: int -> ?if_special_var:var_annot option -> string list ->
(var_annot option * field_annot option) tzresult
= fun loc ?if_special_var annot ->
let parse_destr_annot
: int -> string list -> default_accessor:field_annot option ->
field_name:field_annot option ->
pair_annot:var_annot option -> value_annot:var_annot option ->
(var_annot option * field_annot option) tzresult
= fun loc annot ~default_accessor ~field_name ~pair_annot ~value_annot ->
parse_annots loc ~allow_special_var:true annot >>?
classify_annot loc >>? fun (vars, types, fields) ->
error_unexpected_annot loc types >>? fun () ->
get_one_annot loc vars >>? fun v ->
get_one_annot loc fields >>? fun f ->
match if_special_var, v with
| Some special_var, Some `Var_annot "%" -> ok (special_var, f)
| None, Some `Var_annot "%" -> error (Unexpected_annotation loc)
| _, _ -> ok (v, f)
get_one_annot loc fields >|? fun f ->
let default = gen_access_annot pair_annot field_name ~default:default_accessor in
let v = match v with
| Some `Var_annot "%" -> field_to_var_annot field_name
| Some `Var_annot "%%" -> default
| Some _ -> v
| None -> value_annot in
(v, f)
let parse_var_type_annot
: int -> string list -> (var_annot option * type_annot option) tzresult
......
......@@ -129,8 +129,12 @@ val parse_constr_annot :
val parse_two_var_annot :
int -> string list -> (var_annot option * var_annot option) tzresult
val parse_var_field_annot :
int -> ?if_special_var:var_annot option -> string list ->
val parse_destr_annot :
int -> string list ->
default_accessor:field_annot option ->
field_name:field_annot option ->
pair_annot:var_annot option ->
value_annot:var_annot option ->
(var_annot option * field_annot option) tzresult
val parse_var_type_annot :
......
......@@ -1077,8 +1077,8 @@ let parse_constr_annot loc ?if_special_first ?if_special_second annot =
Lwt.return (parse_constr_annot loc ?if_special_first ?if_special_second annot)
let parse_two_var_annot loc annot =
Lwt.return (parse_two_var_annot loc annot)
let parse_var_field_annot loc ?if_special_var annot =
Lwt.return (parse_var_field_annot loc ?if_special_var annot)
let parse_destr_annot loc annot ~default_accessor ~field_name ~pair_annot ~value_annot =
Lwt.return (parse_destr_annot loc annot ~default_accessor ~field_name ~pair_annot ~value_annot)
let parse_var_type_annot loc annot =
Lwt.return (parse_var_type_annot loc annot)
......@@ -1526,22 +1526,22 @@ and parse_instr
(Item_t (Pair_t((a, l_field, fst_annot), (b, r_field, snd_annot), ty_name), rest, annot))
| Prim (loc, I_CAR, [], annot),
Item_t (Pair_t ((a, expected_field_annot, a_annot), _, _), rest, pair_annot) ->
parse_var_field_annot loc annot
~if_special_var:(field_to_var_annot expected_field_annot)
parse_destr_annot loc annot
~pair_annot
~value_annot:a_annot
~field_name:expected_field_annot
~default_accessor:default_car_annot
>>=? fun (annot, field_annot) ->
let annot = default_annot annot ~default:a_annot in
let annot = default_annot annot
~default:(gen_access_annot pair_annot expected_field_annot ~default:default_car_annot) in
Lwt.return @@ check_correct_field field_annot expected_field_annot >>=? fun () ->
typed ctxt loc Car (Item_t (a, rest, annot))
| Prim (loc, I_CDR, [], annot),
Item_t (Pair_t (_, (b, expected_field_annot, b_annot), _), rest, pair_annot) ->
parse_var_field_annot loc annot
~if_special_var:(field_to_var_annot expected_field_annot)
parse_destr_annot loc annot
~pair_annot
~value_annot:b_annot
~field_name:expected_field_annot
~default_accessor:default_cdr_annot
>>=? fun (annot, field_annot) ->
let annot = default_annot annot ~default:b_annot in
let annot = default_annot annot
~default:(gen_access_annot pair_annot expected_field_annot ~default:default_cdr_annot) in
Lwt.return @@ check_correct_field field_annot expected_field_annot >>=? fun () ->
typed ctxt loc Cdr (Item_t (b, rest, annot))
(* unions *)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment