Merge branch 'rinderknecht@reasonligo_parsing' into 'dev'

Fixed the parsing of type t = list ((u,v));

See merge request ligolang/ligo!473
This commit is contained in:
Sander 2020-03-24 14:17:04 +00:00
commit 2182054c1c
4 changed files with 412 additions and 413 deletions

View File

@ -24,23 +24,21 @@ type 'a sequence_or_record =
let (<@) f g x = f (g x) let (<@) f g x = f (g x)
(** (*
Covert nsepseq to a chain of TFun's. Convert a nsepseq to a chain of TFun's.
Necessary to handle cases like: Necessary to handle cases like:
`type foo = (int, int) => int;` [type foo = (int, int) => int;]
*) *)
let rec nsepseq_to_curry hd rest =
match hd, rest with let rec curry hd = function
| hd, (sep, item) :: rest -> (sep, item)::rest ->
let start = type_expr_to_region hd in let stop = nsepseq_to_region type_expr_to_region (hd, rest)
let stop = nsepseq_to_region type_expr_to_region (hd, rest) in and start = type_expr_to_region hd in
let region = cover start stop in let region = cover start stop
TFun { and value = hd, sep, curry item rest
value = hd, sep, (nsepseq_to_curry item rest); in TFun {value; region}
region | [] -> hd
}
| hd, [] -> hd
(* END HEADER *) (* END HEADER *)
%} %}
@ -58,6 +56,7 @@ let rec nsepseq_to_curry hd rest =
can be reduced to [expr -> Ident], but also to can be reduced to [expr -> Ident], but also to
[field_assignment -> Ident]. [field_assignment -> Ident].
*) *)
%nonassoc Ident %nonassoc Ident
%nonassoc COLON %nonassoc COLON
@ -175,42 +174,32 @@ type_decl:
in {region; value} } in {region; value} }
type_expr: type_expr:
cartesian | sum_type | record_type { $1 } fun_type | sum_type | record_type { $1 }
type_expr_func: fun_type:
"=>" cartesian { type_name "=>" fun_type {
$1, $2 let region = cover $1.region (type_expr_to_region $3)
in TFun {region; value = TVar $1, $2, $3}
} }
| "(" fun_type ")" "=>" fun_type {
let region = cover $1 (type_expr_to_region $5)
in TFun {region; value = $2,$4,$5}
}
| "(" tuple(fun_type) ")" "=>" fun_type {
let hd, rest = $2 in curry hd (rest @ [($4,$5)])
}
| "(" tuple(fun_type) ")" {
TProd {region = cover $1 $3; value = $2}
}
| core_type { $1 }
cartesian: type_args:
core_type { $1 } tuple(fun_type) { $1 }
| type_name type_expr_func { | fun_type { $1, [] }
let (arrow, c) = $2 in
let value = TVar $1, arrow, c in
let region = cover $1.region (type_expr_to_region c) in
TFun { region; value }
}
| "(" cartesian ")" type_expr_func {
let (arrow, c) = $4 in
let value = $2, arrow, c in
let region = cover $1 (type_expr_to_region c) in
TFun { region; value }
}
| "(" cartesian "," nsepseq(cartesian,",") ")" type_expr_func? {
match $6 with
| Some (arrow, c) ->
let (hd, rest) = Utils.nsepseq_cons $2 $3 $4 in
let rest = rest @ [(arrow, c)] in
nsepseq_to_curry hd rest
| None ->
let value = Utils.nsepseq_cons $2 $3 $4 in
let region = cover $1 $5 in
TProd {region; value}
}
core_type: core_type:
type_name { TVar $1 } type_name { TVar $1 }
| par(cartesian) { TPar $1 } | par(fun_type) { TPar $1 }
| module_name "." type_name { | module_name "." type_name {
let module_name = $1.value in let module_name = $1.value in
let type_name = $3.value in let type_name = $3.value in
@ -218,12 +207,9 @@ core_type:
let region = cover $1.region $3.region let region = cover $1.region $3.region
in TVar {region; value} in TVar {region; value}
} }
| type_name par(nsepseq(core_type,",") { $1 }) { | type_name par(type_args) {
let constr, arg = $1, $2 in let region = cover $1.region $2.region
let start = constr.region in TApp {region; value = $1,$2} }
and stop = arg.region in
let region = cover start stop
in TApp {region; value = constr,arg} }
sum_type: sum_type:
ioption("|") nsepseq(variant,"|") { ioption("|") nsepseq(variant,"|") {
@ -233,7 +219,7 @@ sum_type:
variant: variant:
"<constr>" { {$1 with value={constr=$1; arg=None}} } "<constr>" { {$1 with value={constr=$1; arg=None}} }
| "<constr>" "(" cartesian ")" { | "<constr>" "(" fun_type ")" {
let region = cover $1.region $4 let region = cover $1.region $4
and value = {constr=$1; arg = Some (ghost,$3)} and value = {constr=$1; arg = Some (ghost,$3)}
in {region; value} } in {region; value} }
@ -274,9 +260,6 @@ let_declaration:
let region = cover $2 stop let region = cover $2 stop
in {region; value} } in {region; value} }
es6_func:
"=>" expr { $1,$2 }
let_binding: let_binding:
"<ident>" type_annotation? "=" expr { "<ident>" type_annotation? "=" expr {
Scoping.check_reserved_name $1; Scoping.check_reserved_name $1;
@ -452,13 +435,12 @@ type_expr_simple:
type_annotation_simple: type_annotation_simple:
":" type_expr_simple { $1,$2 } ":" type_expr_simple { $1,$2 }
fun_expr: fun_expr:
disj_expr_level es6_func { disj_expr_level "=>" expr {
let arrow, body = $2 in let arrow, body = $2, $3
let kwd_fun = ghost in and kwd_fun = ghost in
let start = expr_to_region $1 in let start = expr_to_region $1
let stop = expr_to_region body in and stop = expr_to_region body in
let region = cover start stop in let region = cover start stop in
let rec arg_to_pattern = function let rec arg_to_pattern = function
@ -525,8 +507,8 @@ fun_expr:
match type_expr with match type_expr with
| TProd {value; _} -> | TProd {value; _} ->
let (hd, rest) = value in let (hd, rest) = value in
let rest = rest @ [(arrow, expr_to_type body)] in let rest = rest @ [(arrow, expr_to_type body)]
nsepseq_to_curry hd rest in curry hd rest
| e -> | e ->
TFun { TFun {
value = e, arrow, expr_to_type body; value = e, arrow, expr_to_type body;

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,3 @@
type z = list((int, int));
let o: z = [(2,4), (4, 6)];

View File

@ -2316,6 +2316,10 @@ let no_semicolon_religo () : unit result =
in in
ok () ok ()
let tuple_list_religo () : unit result =
let%bind _ = retype_file "./contracts/tuple_list.religo" in
ok ()
let loop_bugs_ligo () : unit result = let loop_bugs_ligo () : unit result =
let%bind program = type_file "./contracts/loop_bugs.ligo" in let%bind program = type_file "./contracts/loop_bugs.ligo" in
let input = e_unit () in let input = e_unit () in
@ -2502,4 +2506,5 @@ let main = test_suite "Integration (End to End)" [
test "tuple type (religo)" tuple_type_religo ; test "tuple type (religo)" tuple_type_religo ;
test "no semicolon (religo)" no_semicolon_religo ; test "no semicolon (religo)" no_semicolon_religo ;
test "loop_bugs (ligo)" loop_bugs_ligo ; test "loop_bugs (ligo)" loop_bugs_ligo ;
test "tuple_list (religo)" tuple_list_religo ;
] ]