Merge branch 'wild-args-reasonligo' into 'dev'

ReasonLIGO: Add support for _ as an argument

See merge request ligolang/ligo!527
This commit is contained in:
Sander 2020-04-02 11:57:32 +00:00
commit cbf2c7680e
11 changed files with 1150 additions and 1110 deletions

View File

@ -68,6 +68,18 @@ module Errors =
("location",
fun () -> Format.asprintf "%a" Location.pp_lift @@ expression_loc)]
in error ~data title message
let invalid_wild (expr: AST.expr) =
let title () = "" in
let message () =
"It looks like you are using a wild pattern where it cannot be used."
in
let expression_loc = AST.expr_to_region expr in
let data = [
("location",
fun () -> Format.asprintf "%a" Location.pp_lift @@ expression_loc)]
in error ~data title message
end
let parse (module IO : IO) parser =
@ -127,6 +139,8 @@ let parse (module IO : IO) parser =
| exception SyntaxError.Error (SyntaxError.WrongFunctionArguments expr) ->
Trace.fail @@ Errors.wrong_function_arguments expr
| exception SyntaxError.Error (SyntaxError.InvalidWild expr) ->
Trace.fail @@ Errors.wrong_function_arguments expr
let parse_file (source: string) =
let module IO =

View File

@ -40,6 +40,13 @@ let rec curry hd = function
in TFun {value; region}
| [] -> hd
let wild_error e =
match e with
| EVar { value = "_"; _} as e ->
let open! SyntaxError in
raise (Error (InvalidWild e))
| _ -> ()
(* END HEADER *)
%}
@ -262,24 +269,30 @@ let_declaration:
let_binding:
"<ident>" type_annotation? "=" expr {
wild_error $4;
Scoping.check_reserved_name $1;
{binders = PVar $1, []; lhs_type=$2; eq=$3; let_rhs=$4}
}
| "_" type_annotation? "=" expr {
wild_error $4;
{binders = PWild $1, []; lhs_type=$2; eq=$3; let_rhs=$4}
}
| unit type_annotation? "=" expr {
wild_error $4;
{binders = PUnit $1, []; lhs_type=$2; eq=$3; let_rhs=$4}
}
| record_pattern type_annotation? "=" expr {
wild_error $4;
Scoping.check_pattern (PRecord $1);
{binders = PRecord $1, []; lhs_type=$2; eq=$3; let_rhs=$4}
}
| par(closed_irrefutable) type_annotation? "=" expr {
wild_error $4;
Scoping.check_pattern $1.value.inside;
{binders = PPar $1, []; lhs_type=$2; eq=$3; let_rhs=$4}
}
| tuple(sub_irrefutable) type_annotation? "=" expr {
wild_error $4;
Utils.nsepseq_iter Scoping.check_pattern $1;
let hd, tl = $1 in
let start = pattern_to_region hd in
@ -408,7 +421,9 @@ expr:
base_cond__open(expr) | switch_expr(base_cond) { $1 }
base_cond__open(x):
base_expr(x) | conditional(expr_with_let_expr) { $1 }
base_expr(x) | conditional(expr_with_let_expr) {
wild_error $1;
$1 }
base_cond:
base_cond__open(base_cond) { $1 }
@ -445,8 +460,12 @@ fun_expr:
let rec arg_to_pattern = function
EVar v ->
Scoping.check_reserved_name v;
PVar v
if v.value = "_" then
PWild v.region
else (
Scoping.check_reserved_name v;
PVar v
)
| EAnnot {region; value = {inside = EVar v, colon, typ; _}} ->
Scoping.check_reserved_name v;
let value = {pattern = PVar v; colon; type_expr = typ}
@ -779,6 +798,7 @@ common_expr:
| "<bytes>" { EBytes $1 }
| "<ident>" | module_field { EVar $1 }
| projection { EProj $1 }
| "_" { EVar {value = "_"; region = $1} }
| update_record { EUpdate $1 }
| "<string>" { EString (String $1) }
| unit { EUnit $1 }

View File

@ -48,7 +48,12 @@ let parse parser : ('a, string Region.reg) Stdlib.result =
in Stdlib.Error Region.{value=error; region}
(* Scoping errors *)
| SyntaxError.Error (SyntaxError.InvalidWild expr) ->
let msg = "It looks like you are using a wild pattern where it cannot be used.\n"
and region = AST.expr_to_region expr in
let error = Unit.short_error ~offsets:IO.options#offsets
IO.options#mode msg region
in Stdlib.Error Region.{value=error; region}
| Scoping.Error (Scoping.Reserved_name name) ->
let token =
Lexer.Token.mk_ident name.Region.value name.Region.region in

View File

@ -1,4 +1,5 @@
type error =
| WrongFunctionArguments of AST.expr
| InvalidWild of AST.expr
exception Error of error

View File

@ -1,4 +1,5 @@
type error =
| WrongFunctionArguments of AST.expr
| InvalidWild of AST.expr
exception Error of error

File diff suppressed because it is too large Load Diff

View File

@ -5,6 +5,6 @@ type storage = big_map (int, bar);
type return = (list (operation), storage);
let main = ((ignore, store): (unit, storage)): return => {
let main = ((_, store): (unit, storage)): return => {
([]: list(operation), store)
};

View File

@ -3,7 +3,7 @@ type storage = big_map (nat, big_map (int, string));
type return = (list (operation), storage);
let main = ((ignore, store): (unit, storage)): return => {
let main = ((_, store): (unit, storage)): return => {
([]: list(operation), store)
};

View File

@ -10,6 +10,6 @@ type storage = big_map(nat, foo);
type return = (list (operation), storage);
let main = ((ignore, store): (unit, storage)): return => {
let main = ((_, store): (unit, storage)): return => {
([]: list(operation), store)
};

View File

@ -3,7 +3,7 @@ type storage = map (int, big_map (nat, big_map (int, string)));
type return = (list (operation), storage);
let main = ((ignore, store): (unit, storage)): return => {
let main = ((_, store): (unit, storage)): return => {
([]: list(operation), store)
};

View File

@ -15,7 +15,7 @@ let arguments = (b: int, c: int) => { b + c; };
let arguments_type_def = (b: fun_type) => b (5, 3);
let arguments_test = (ignore: int) => arguments_type_def (arguments);
let arguments_test = (_: int) => arguments_type_def (arguments);
type tuple_type = ((int, int)) => int;
@ -23,7 +23,7 @@ let tuple = ((a, b): (int, int)) => { a + b; };
let tuple_type_def = (b: tuple_type) => b ((5, 3));
let tuple_test = (ignore: int) => tuple_type_def (tuple);
let tuple_test = (_: int) => tuple_type_def (tuple);
/* inline */
@ -32,12 +32,12 @@ let arguments_inline = (b: int, c: int) => { b + c; };
let arguments_type_def_inline = (b: (int, int) => int) => b (5, 3);
let arguments_test_inline = (ignore: int) =>
let arguments_test_inline = (_: int) =>
arguments_type_def_inline (arguments_inline);
let tuple_inline = ((a, b): (int, int)) => { a + b; };
let tuple_type_def_inline = (b: ((int, int)) => int) => b ((5, 3));
let tuple_test_inline = (ignore: int) =>
let tuple_test_inline = (_: int) =>
tuple_type_def_inline(tuple_inline);