Fixed parsing and source pretty-printing of recursive functions.

This commit is contained in:
Christian Rinderknecht 2020-02-21 12:30:32 +01:00 committed by Pierre-Emmanuel Wulfman
parent 68b6e6d3b0
commit b51818bc4e
3 changed files with 20 additions and 47 deletions

View File

@ -364,6 +364,7 @@ let keywords = [
(fun reg -> Or reg);
(fun reg -> Patch reg);
(fun reg -> Record reg);
(fun reg -> Recursive reg);
(fun reg -> Remove reg);
(fun reg -> Set reg);
(fun reg -> Skip reg);

View File

@ -263,47 +263,13 @@ fun_expr:
(* Function declarations *)
open_fun_decl:
"function" fun_name parameters ":" type_expr "is"
block "with" expr {
Scoping.check_reserved_name $2;
let stop = expr_to_region $9 in
let region = cover $1 stop
and value = {kwd_recursive= None;
kwd_function = $1;
fun_name = $2;
param = $3;
colon = $4;
ret_type = $5;
kwd_is = $6;
block_with = Some ($7, $8);
return = $9;
terminator = None;
attributes = None}
in {region; value}
}
| "function" fun_name parameters ":" type_expr "is" expr {
Scoping.check_reserved_name $2;
let stop = expr_to_region $7 in
let region = cover $1 stop
and value = {kwd_recursive= None;
kwd_function = $1;
fun_name = $2;
param = $3;
colon = $4;
ret_type = $5;
kwd_is = $6;
block_with = None;
return = $7;
terminator = None;
attributes = None}
in {region; value} }
| "recursive" "function" fun_name parameters ":" type_expr "is"
"recursive"? "function" fun_name parameters ":" type_expr "is"
block "with" expr {
Scoping.check_reserved_name $3;
let stop = expr_to_region $10 in
let region = cover $2 stop
and value = {kwd_recursive= Some($1);
kwd_function = $2;
and value = {kwd_recursive= $1;
kwd_function = $2;
fun_name = $3;
param = $4;
colon = $5;
@ -315,11 +281,11 @@ open_fun_decl:
attributes = None}
in {region; value}
}
| "recursive" "function" fun_name parameters ":" type_expr "is" expr {
| "recursive"? "function" fun_name parameters ":" type_expr "is" expr {
Scoping.check_reserved_name $3;
let stop = expr_to_region $8 in
let region = cover $2 stop
and value = {kwd_recursive= Some($1);
and value = {kwd_recursive= $1;
kwd_function = $2;
fun_name = $3;
param = $4;

View File

@ -609,8 +609,8 @@ and print_field_path_assign state {value; _} =
print_nsepseq state "field_path" print_var field_path;
print_token state equal "=";
print_expr state field_expr
and print_update_expr state {value; _} =
and print_update_expr state {value; _} =
let {record; kwd_with; updates} = value in
print_path state record;
print_token state kwd_with "with";
@ -859,20 +859,26 @@ and pp_declaration state = function
and pp_attr_decl state = pp_ne_injection pp_string state
and pp_fun_decl state decl =
let arity = 5 in
let arity, start =
match decl.kwd_recursive with
None -> 5,0
| Some _ ->
let state = state#pad 6 0 in
let () = pp_node state "recursive"
in 6,1 in
let () =
let state = state#pad arity 0 in
let state = state#pad arity start in
pp_ident state decl.fun_name in
let () =
let state = state#pad arity 1 in
let state = state#pad arity (start + 1) in
pp_node state "<parameters>";
pp_parameters state decl.param in
let () =
let state = state#pad arity 2 in
let state = state#pad arity (start + 2) in
pp_node state "<return type>";
pp_type_expr (state#pad 1 0) decl.ret_type in
let () =
let state = state#pad arity 3 in
let state = state#pad arity (start + 3) in
pp_node state "<body>";
let statements =
match decl.block_with with
@ -880,7 +886,7 @@ and pp_fun_decl state decl =
| None -> Instr (Skip Region.ghost), [] in
pp_statements state statements in
let () =
let state = state#pad arity 4 in
let state = state#pad arity (start + 4) in
pp_node state "<return>";
pp_expr (state#pad 1 0) decl.return
in ()