ligo/src/passes/1-parser/pascaligo/ParserLog.ml

743 lines
24 KiB
OCaml
Raw Normal View History

2019-05-13 00:56:22 +04:00
[@@@warning "-42"]
open Utils
open AST
open! Region
(* Printing the tokens with their source regions *)
let sprintf = Printf.sprintf
2019-05-13 00:56:22 +04:00
let offsets = ref true
let mode = ref `Point
2019-05-13 00:56:22 +04:00
let compact (region: Region.t) =
region#compact ~offsets:!offsets !mode
let print_nsepseq :
Buffer.t -> string -> (Buffer.t -> 'a -> unit) ->
('a, Region.t) nsepseq -> unit =
fun buffer sep print (head, tail) ->
2019-05-13 00:56:22 +04:00
let print_aux (sep_reg, item) =
let sep_line = sprintf "%s: %s\n" (compact sep_reg) sep in
Buffer.add_string buffer sep_line;
print buffer item
in print buffer head; List.iter print_aux tail
2019-05-13 00:56:22 +04:00
let print_sepseq :
Buffer.t -> string -> (Buffer.t -> 'a -> unit) ->
('a, Region.t) sepseq -> unit =
fun buffer sep print -> function
2019-05-13 00:56:22 +04:00
None -> ()
| Some seq -> print_nsepseq buffer sep print seq
let print_token buffer region lexeme =
let line = sprintf "%s: %s\n"(compact region) lexeme
in Buffer.add_string buffer line
2019-05-13 00:56:22 +04:00
let print_var buffer {region; value=lexeme} =
let line = sprintf "%s: Ident \"%s\"\n"
(compact region) lexeme
in Buffer.add_string buffer line
2019-05-13 00:56:22 +04:00
let print_constr buffer {region; value=lexeme} =
let line = sprintf "%s: Constr \"%s\"\n"
(compact region) lexeme
in Buffer.add_string buffer line
2019-05-13 00:56:22 +04:00
let print_string buffer {region; value=lexeme} =
let line = sprintf "%s: String %s\n"
(compact region) lexeme
in Buffer.add_string buffer line
2019-05-13 00:56:22 +04:00
let print_bytes buffer {region; value = lexeme, abstract} =
let line = sprintf "%s: Bytes (\"%s\", \"0x%s\")\n"
(compact region) lexeme
(Hex.to_string abstract)
in Buffer.add_string buffer line
2019-05-13 00:56:22 +04:00
let print_int buffer {region; value = lexeme, abstract} =
let line = sprintf "%s: Int (\"%s\", %s)\n"
(compact region) lexeme
(Z.to_string abstract)
in Buffer.add_string buffer line
2019-05-13 00:56:22 +04:00
(* Main printing function *)
let rec print_tokens buffer ast =
2019-05-13 00:56:22 +04:00
let {decl; eof} = ast in
Utils.nseq_iter (print_decl buffer) decl;
print_token buffer eof "EOF"
2019-05-13 00:56:22 +04:00
and print_decl buffer = function
TypeDecl decl -> print_type_decl buffer decl
| ConstDecl decl -> print_const_decl buffer decl
| LambdaDecl decl -> print_lambda_decl buffer decl
2019-05-13 00:56:22 +04:00
and print_const_decl buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {kwd_const; name; colon; const_type;
equal; init; terminator} = value in
print_token buffer kwd_const "const";
print_var buffer name;
print_token buffer colon ":";
print_type_expr buffer const_type;
print_token buffer equal "=";
print_expr buffer init;
print_terminator buffer terminator
and print_type_decl buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {kwd_type; name; kwd_is;
type_expr; terminator} = value in
print_token buffer kwd_type "type";
print_var buffer name;
print_token buffer kwd_is "is";
print_type_expr buffer type_expr;
print_terminator buffer terminator
and print_type_expr buffer = function
TProd cartesian -> print_cartesian buffer cartesian
| TSum sum_type -> print_sum_type buffer sum_type
| TRecord record_type -> print_record_type buffer record_type
| TApp type_app -> print_type_app buffer type_app
| TFun type_fun -> print_type_fun buffer type_fun
| TPar par_type -> print_par_type buffer par_type
| TAlias type_alias -> print_var buffer type_alias
and print_cartesian buffer {value; _} =
print_nsepseq buffer "*" print_type_expr value
and print_variant buffer {value; _} =
let {constr; args} = value in
print_constr buffer constr;
match args with
None -> ()
| Some (kwd_of, product) ->
print_token buffer kwd_of "of";
print_cartesian buffer product
2019-05-13 00:56:22 +04:00
and print_sum_type buffer {value; _} =
print_nsepseq buffer "|" print_variant value
2019-05-13 00:56:22 +04:00
and print_record_type buffer record_type =
print_injection buffer "record" print_field_decl record_type
2019-05-13 00:56:22 +04:00
and print_type_app buffer {value; _} =
2019-05-13 00:56:22 +04:00
let type_name, type_tuple = value in
print_var buffer type_name;
print_type_tuple buffer type_tuple
2019-05-13 00:56:22 +04:00
and print_type_fun buffer {value; _} =
2019-05-13 00:56:22 +04:00
let type_expr_a, arrow, type_expr_b = value in
print_type_expr buffer type_expr_a;
print_token buffer arrow "->";
print_type_expr buffer type_expr_b
2019-05-13 00:56:22 +04:00
and print_par_type buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {lpar; inside; rpar} = value in
print_token buffer lpar "(";
print_type_expr buffer inside;
print_token buffer rpar ")"
2019-05-13 00:56:22 +04:00
and print_field_decl buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {field_name; colon; field_type} = value in
print_var buffer field_name;
print_token buffer colon ":";
print_type_expr buffer field_type
2019-05-13 00:56:22 +04:00
and print_type_tuple buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {lpar; inside; rpar} = value in
print_token buffer lpar "(";
print_nsepseq buffer "," print_type_expr inside;
print_token buffer rpar ")"
2019-05-13 00:56:22 +04:00
and print_lambda_decl buffer = function
FunDecl fun_decl -> print_fun_decl buffer fun_decl
| ProcDecl proc_decl -> print_proc_decl buffer proc_decl
2019-05-13 00:56:22 +04:00
and print_fun_decl buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {kwd_function; name; param; colon;
ret_type; kwd_is; local_decls;
block; kwd_with; return; terminator} = value in
print_token buffer kwd_function "function";
print_var buffer name;
print_parameters buffer param;
print_token buffer colon ":";
print_type_expr buffer ret_type;
print_token buffer kwd_is "is";
print_local_decls buffer local_decls;
print_block buffer block;
print_token buffer kwd_with "with";
print_expr buffer return;
print_terminator buffer terminator
and print_proc_decl buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {kwd_procedure; name; param; kwd_is;
local_decls; block; terminator} = value in
print_token buffer kwd_procedure "procedure";
print_var buffer name;
print_parameters buffer param;
print_token buffer kwd_is "is";
print_local_decls buffer local_decls;
print_block buffer block;
print_terminator buffer terminator
and print_parameters buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {lpar; inside; rpar} = value in
print_token buffer lpar "(";
print_nsepseq buffer ";" print_param_decl inside;
print_token buffer rpar ")"
2019-05-13 00:56:22 +04:00
and print_param_decl buffer = function
ParamConst param_const -> print_param_const buffer param_const
| ParamVar param_var -> print_param_var buffer param_var
2019-05-13 00:56:22 +04:00
and print_param_const buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {kwd_const; var; colon; param_type} = value in
print_token buffer kwd_const "const";
print_var buffer var;
print_token buffer colon ":";
print_type_expr buffer param_type
2019-05-13 00:56:22 +04:00
and print_param_var buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {kwd_var; var; colon; param_type} = value in
print_token buffer kwd_var "var";
print_var buffer var;
print_token buffer colon ":";
print_type_expr buffer param_type
2019-05-13 00:56:22 +04:00
and print_block buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {opening; statements; terminator; closing} = value in
print_block_opening buffer opening;
print_statements buffer statements;
print_terminator buffer terminator;
print_block_closing buffer closing
and print_block_opening buffer = function
Block (kwd_block, lbrace) ->
print_token buffer kwd_block "block";
print_token buffer lbrace "{"
| Begin kwd_begin ->
print_token buffer kwd_begin "begin"
and print_block_closing buffer = function
Block rbrace -> print_token buffer rbrace "}"
| End kwd_end -> print_token buffer kwd_end "end"
and print_local_decls buffer sequence =
List.iter (print_local_decl buffer) sequence
and print_local_decl buffer = function
LocalFun decl -> print_fun_decl buffer decl
| LocalProc decl -> print_proc_decl buffer decl
| LocalData decl -> print_data_decl buffer decl
and print_data_decl buffer = function
LocalConst decl -> print_const_decl buffer decl
| LocalVar decl -> print_var_decl buffer decl
and print_var_decl buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {kwd_var; name; colon; var_type;
assign; init; terminator} = value in
print_token buffer kwd_var "var";
print_var buffer name;
print_token buffer colon ":";
print_type_expr buffer var_type;
print_token buffer assign ":=";
print_expr buffer init;
print_terminator buffer terminator
and print_statements buffer sequence =
print_nsepseq buffer ";" print_statement sequence
and print_statement buffer = function
Instr instr -> print_instruction buffer instr
| Data data -> print_data_decl buffer data
and print_instruction buffer = function
Single instr -> print_single_instr buffer instr
| Block block -> print_block buffer block
and print_single_instr buffer = function
Cond {value; _} -> print_conditional buffer value
| CaseInstr {value; _} -> print_case_instr buffer value
| Assign assign -> print_assignment buffer assign
| Loop loop -> print_loop buffer loop
| ProcCall fun_call -> print_fun_call buffer fun_call
| Skip kwd_skip -> print_token buffer kwd_skip "skip"
| RecordPatch {value; _} -> print_record_patch buffer value
| MapPatch {value; _} -> print_map_patch buffer value
| SetPatch {value; _} -> print_set_patch buffer value
| MapRemove {value; _} -> print_map_remove buffer value
| SetRemove {value; _} -> print_set_remove buffer value
and print_conditional buffer node =
2019-05-13 00:56:22 +04:00
let {kwd_if; test; kwd_then; ifso; terminator;
kwd_else; ifnot} = node in
print_token buffer kwd_if "if";
print_expr buffer test;
print_token buffer kwd_then "then";
print_if_clause buffer ifso;
print_terminator buffer terminator;
print_token buffer kwd_else "else";
print_if_clause buffer ifnot
and print_if_clause buffer = function
ClauseInstr instr -> print_instruction buffer instr
2019-05-13 00:56:22 +04:00
| ClauseBlock {value; _} ->
let {lbrace; inside; rbrace} = value in
let statements, terminator = inside in
print_token buffer lbrace "{";
print_statements buffer statements;
print_terminator buffer terminator;
print_token buffer rbrace "}"
2019-05-13 00:56:22 +04:00
and print_case_instr buffer (node : instruction case) =
2019-05-13 00:56:22 +04:00
let {kwd_case; expr; opening;
lead_vbar; cases; closing} = node in
print_token buffer kwd_case "case";
print_expr buffer expr;
print_opening buffer "of" opening;
print_token_opt buffer lead_vbar "|";
print_cases_instr buffer cases;
print_closing buffer closing
and print_token_opt buffer = function
2019-05-13 00:56:22 +04:00
None -> fun _ -> ()
| Some region -> print_token buffer region
2019-05-13 00:56:22 +04:00
and print_cases_instr buffer {value; _} =
print_nsepseq buffer "|" print_case_clause_instr value
2019-05-13 00:56:22 +04:00
and print_case_clause_instr buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {pattern; arrow; rhs} = value in
print_pattern buffer pattern;
print_token buffer arrow "->";
print_instruction buffer rhs
2019-05-13 00:56:22 +04:00
and print_assignment buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {lhs; assign; rhs} = value in
print_lhs buffer lhs;
print_token buffer assign ":=";
print_rhs buffer rhs
2019-05-13 00:56:22 +04:00
and print_rhs buffer e = print_expr buffer e
2019-05-13 00:56:22 +04:00
and print_lhs buffer = function
Path path -> print_path buffer path
| MapPath {value; _} -> print_map_lookup buffer value
2019-05-13 00:56:22 +04:00
and print_loop buffer = function
While {value; _} -> print_while_loop buffer value
| For for_loop -> print_for_loop buffer for_loop
2019-05-13 00:56:22 +04:00
and print_while_loop buffer value =
2019-05-13 00:56:22 +04:00
let {kwd_while; cond; block} = value in
print_token buffer kwd_while "while";
print_expr buffer cond;
print_block buffer block
2019-05-13 00:56:22 +04:00
and print_for_loop buffer = function
ForInt for_int -> print_for_int buffer for_int
| ForCollect for_collect -> print_for_collect buffer for_collect
2019-05-13 00:56:22 +04:00
and print_for_int buffer ({value; _} : for_int reg) =
2019-05-13 00:56:22 +04:00
let {kwd_for; assign; down; kwd_to;
bound; step; block} = value in
print_token buffer kwd_for "for";
print_var_assign buffer assign;
print_down buffer down;
print_token buffer kwd_to "to";
print_expr buffer bound;
print_step buffer step;
print_block buffer block
and print_var_assign buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {name; assign; expr} = value in
print_var buffer name;
print_token buffer assign ":=";
print_expr buffer expr
2019-05-13 00:56:22 +04:00
and print_down buffer = function
Some kwd_down -> print_token buffer kwd_down "down"
| None -> ()
2019-05-13 00:56:22 +04:00
and print_step buffer = function
2019-05-13 00:56:22 +04:00
Some (kwd_step, expr) ->
print_token buffer kwd_step "step";
print_expr buffer expr
2019-05-13 00:56:22 +04:00
| None -> ()
and print_for_collect buffer ({value; _} : for_collect reg) =
2019-05-13 00:56:22 +04:00
let {kwd_for; var; bind_to; kwd_in; expr; block} = value in
print_token buffer kwd_for "for";
print_var buffer var;
print_bind_to buffer bind_to;
print_token buffer kwd_in "in";
print_expr buffer expr;
print_block buffer block
and print_bind_to buffer = function
2019-05-13 00:56:22 +04:00
Some (arrow, variable) ->
print_token buffer arrow "->";
print_var buffer variable
2019-05-13 00:56:22 +04:00
| None -> ()
and print_expr buffer = function
ECase {value;_} -> print_case_expr buffer value
| EAnnot {value;_} -> print_annot_expr buffer value
| ELogic e -> print_logic_expr buffer e
| EArith e -> print_arith_expr buffer e
| EString e -> print_string_expr buffer e
| EList e -> print_list_expr buffer e
| ESet e -> print_set_expr buffer e
| EConstr e -> print_constr_expr buffer e
| ERecord e -> print_record_expr buffer e
| EProj e -> print_projection buffer e
| EMap e -> print_map_expr buffer e
| EVar v -> print_var buffer v
| ECall e -> print_fun_call buffer e
| EBytes b -> print_bytes buffer b
| EUnit r -> print_token buffer r "Unit"
| ETuple e -> print_tuple_expr buffer e
| EPar e -> print_par_expr buffer e
and print_annot_expr buffer (expr , type_expr) =
print_expr buffer expr;
print_type_expr buffer type_expr
and print_case_expr buffer (node : expr case) =
2019-05-13 00:56:22 +04:00
let {kwd_case; expr; opening;
lead_vbar; cases; closing} = node in
print_token buffer kwd_case "case";
print_expr buffer expr;
print_opening buffer "of" opening;
print_token_opt buffer lead_vbar "|";
print_cases_expr buffer cases;
print_closing buffer closing
2019-05-13 00:56:22 +04:00
and print_cases_expr buffer {value; _} =
print_nsepseq buffer "|" print_case_clause_expr value
2019-05-13 00:56:22 +04:00
and print_case_clause_expr buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {pattern; arrow; rhs} = value in
print_pattern buffer pattern;
print_token buffer arrow "->";
print_expr buffer rhs
2019-05-13 00:56:22 +04:00
and print_map_expr buffer = function
MapLookUp {value; _} -> print_map_lookup buffer value
| MapInj inj -> print_injection buffer "map" print_binding inj
2019-05-13 00:56:22 +04:00
and print_set_expr buffer = function
SetInj inj -> print_injection buffer "set" print_expr inj
| SetMem mem -> print_set_membership buffer mem
2019-05-13 00:56:22 +04:00
and print_set_membership buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {set; kwd_contains; element} = value in
print_expr buffer set;
print_token buffer kwd_contains "contains";
print_expr buffer element
2019-05-13 00:56:22 +04:00
and print_map_lookup buffer {path; index} =
2019-05-13 00:56:22 +04:00
let {lbracket; inside; rbracket} = index.value in
print_path buffer path;
print_token buffer lbracket "[";
print_expr buffer inside;
print_token buffer rbracket "]"
2019-05-13 00:56:22 +04:00
and print_path buffer = function
Name var -> print_var buffer var
| Path path -> print_projection buffer path
2019-05-13 00:56:22 +04:00
and print_logic_expr buffer = function
BoolExpr e -> print_bool_expr buffer e
| CompExpr e -> print_comp_expr buffer e
2019-05-13 00:56:22 +04:00
and print_bool_expr buffer = function
2019-05-13 00:56:22 +04:00
Or {value = {arg1; op; arg2}; _} ->
print_expr buffer arg1;
print_token buffer op "||";
print_expr buffer arg2
2019-05-13 00:56:22 +04:00
| And {value = {arg1; op; arg2}; _} ->
print_expr buffer arg1;
print_token buffer op "&&";
print_expr buffer arg2
2019-05-13 00:56:22 +04:00
| Not {value = {op; arg}; _} ->
print_token buffer op "not";
print_expr buffer arg
| False region ->
print_token buffer region "False"
| True region ->
print_token buffer region "True"
and print_comp_expr buffer = function
2019-05-13 00:56:22 +04:00
Lt {value = {arg1; op; arg2}; _} ->
print_expr buffer arg1;
print_token buffer op "<";
print_expr buffer arg2
2019-05-13 00:56:22 +04:00
| Leq {value = {arg1; op; arg2}; _} ->
print_expr buffer arg1;
print_token buffer op "<=";
print_expr buffer arg2
2019-05-13 00:56:22 +04:00
| Gt {value = {arg1; op; arg2}; _} ->
print_expr buffer arg1;
print_token buffer op ">";
print_expr buffer arg2
2019-05-13 00:56:22 +04:00
| Geq {value = {arg1; op; arg2}; _} ->
print_expr buffer arg1;
print_token buffer op ">=";
print_expr buffer arg2
2019-05-13 00:56:22 +04:00
| Equal {value = {arg1; op; arg2}; _} ->
print_expr buffer arg1;
print_token buffer op "=";
print_expr buffer arg2
2019-05-13 00:56:22 +04:00
| Neq {value = {arg1; op; arg2}; _} ->
print_expr buffer arg1;
print_token buffer op "=/=";
print_expr buffer arg2
2019-05-13 00:56:22 +04:00
and print_arith_expr buffer = function
2019-05-13 00:56:22 +04:00
Add {value = {arg1; op; arg2}; _} ->
print_expr buffer arg1;
print_token buffer op "+";
print_expr buffer arg2
2019-05-13 00:56:22 +04:00
| Sub {value = {arg1; op; arg2}; _} ->
print_expr buffer arg1;
print_token buffer op "-";
print_expr buffer arg2
2019-05-13 00:56:22 +04:00
| Mult {value = {arg1; op; arg2}; _} ->
print_expr buffer arg1;
print_token buffer op "*";
print_expr buffer arg2
2019-05-13 00:56:22 +04:00
| Div {value = {arg1; op; arg2}; _} ->
print_expr buffer arg1;
print_token buffer op "/";
print_expr buffer arg2
2019-05-13 00:56:22 +04:00
| Mod {value = {arg1; op; arg2}; _} ->
print_expr buffer arg1;
print_token buffer op "mod";
print_expr buffer arg2
2019-05-13 00:56:22 +04:00
| Neg {value = {op; arg}; _} ->
print_token buffer op "-";
print_expr buffer arg
2019-05-13 00:56:22 +04:00
| Int i
| Nat i
| Mtz i -> print_int buffer i
2019-05-13 00:56:22 +04:00
and print_string_expr buffer = function
2019-05-13 00:56:22 +04:00
Cat {value = {arg1; op; arg2}; _} ->
print_expr buffer arg1;
print_token buffer op "^";
print_expr buffer arg2
| String s ->
print_string buffer s
2019-05-13 00:56:22 +04:00
and print_list_expr buffer = function
2019-05-13 00:56:22 +04:00
Cons {value = {arg1; op; arg2}; _} ->
print_expr buffer arg1;
print_token buffer op "#";
print_expr buffer arg2
| List e -> print_injection buffer "list" print_expr e
| Nil e -> print_nil buffer e
2019-05-13 00:56:22 +04:00
and print_constr_expr buffer = function
SomeApp e -> print_some_app buffer e
| NoneExpr e -> print_none_expr buffer e
| ConstrApp e -> print_constr_app buffer e
2019-05-13 00:56:22 +04:00
and print_record_expr buffer e =
print_injection buffer "record" print_field_assign e
2019-05-13 00:56:22 +04:00
and print_field_assign buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {field_name; equal; field_expr} = value in
print_var buffer field_name;
print_token buffer equal "=";
print_expr buffer field_expr
2019-05-13 00:56:22 +04:00
and print_projection buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {struct_name; selector; field_path} = value in
print_var buffer struct_name;
print_token buffer selector ".";
print_field_path buffer field_path
2019-05-13 00:56:22 +04:00
and print_field_path buffer sequence =
print_nsepseq buffer "." print_selection sequence
2019-05-13 00:56:22 +04:00
and print_selection buffer = function
FieldName name -> print_var buffer name
| Component int -> print_int buffer int
2019-05-13 00:56:22 +04:00
and print_record_patch buffer node =
2019-05-13 00:56:22 +04:00
let {kwd_patch; path; kwd_with; record_inj} = node in
print_token buffer kwd_patch "patch";
print_path buffer path;
print_token buffer kwd_with "with";
print_record_expr buffer record_inj
2019-05-13 00:56:22 +04:00
and print_set_patch buffer node =
2019-05-13 00:56:22 +04:00
let {kwd_patch; path; kwd_with; set_inj} = node in
print_token buffer kwd_patch "patch";
print_path buffer path;
print_token buffer kwd_with "with";
print_injection buffer "set" print_expr set_inj
2019-05-13 00:56:22 +04:00
and print_map_patch buffer node =
2019-05-13 00:56:22 +04:00
let {kwd_patch; path; kwd_with; map_inj} = node in
print_token buffer kwd_patch "patch";
print_path buffer path;
print_token buffer kwd_with "with";
print_injection buffer "map" print_binding map_inj
2019-05-13 00:56:22 +04:00
and print_map_remove buffer node =
2019-05-13 00:56:22 +04:00
let {kwd_remove; key; kwd_from; kwd_map; map} = node in
print_token buffer kwd_remove "remove";
print_expr buffer key;
print_token buffer kwd_from "from";
print_token buffer kwd_map "map";
print_path buffer map
2019-05-13 00:56:22 +04:00
and print_set_remove buffer node =
2019-05-13 00:56:22 +04:00
let {kwd_remove; element; kwd_from; kwd_set; set} = node in
print_token buffer kwd_remove "remove";
print_expr buffer element;
print_token buffer kwd_from "from";
print_token buffer kwd_set "set";
print_path buffer set
2019-05-13 00:56:22 +04:00
and print_injection :
'a.Buffer.t -> string -> (Buffer.t -> 'a -> unit) ->
'a injection reg -> unit =
fun buffer kwd print {value; _} ->
2019-05-13 00:56:22 +04:00
let {opening; elements; terminator; closing} = value in
print_opening buffer kwd opening;
print_sepseq buffer ";" print elements;
print_terminator buffer terminator;
print_closing buffer closing
and print_opening buffer lexeme = function
Kwd kwd ->
print_token buffer kwd lexeme
2019-05-13 00:56:22 +04:00
| KwdBracket (kwd, lbracket) ->
print_token buffer kwd lexeme;
print_token buffer lbracket "{"
2019-05-13 00:56:22 +04:00
and print_closing buffer = function
RBracket rbracket -> print_token buffer rbracket "}"
| End kwd_end -> print_token buffer kwd_end "end"
2019-05-13 00:56:22 +04:00
and print_binding buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {source; arrow; image} = value in
print_expr buffer source;
print_token buffer arrow "->";
print_expr buffer image
2019-05-13 00:56:22 +04:00
and print_tuple_expr buffer = function
TupleInj inj -> print_tuple_inj buffer inj
2019-05-13 00:56:22 +04:00
and print_tuple_inj buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {lpar; inside; rpar} = value in
print_token buffer lpar "(";
print_nsepseq buffer "," print_expr inside;
print_token buffer rpar ")"
2019-05-13 00:56:22 +04:00
and print_nil buffer value = print_token buffer value "nil"
2019-05-13 00:56:22 +04:00
and print_none_expr buffer value = print_token buffer value "None"
2019-05-13 00:56:22 +04:00
and print_fun_call buffer {value; _} =
2019-05-13 00:56:22 +04:00
let fun_name, arguments = value in
print_var buffer fun_name;
print_tuple_inj buffer arguments
2019-05-13 00:56:22 +04:00
and print_constr_app buffer {value; _} =
2019-05-13 00:56:22 +04:00
let constr, arguments = value in
print_constr buffer constr;
match arguments with
None -> ()
| Some args -> print_tuple_inj buffer args
2019-05-13 00:56:22 +04:00
and print_some_app buffer {value; _} =
2019-05-13 00:56:22 +04:00
let c_Some, arguments = value in
print_token buffer c_Some "Some";
print_tuple_inj buffer arguments
2019-05-13 00:56:22 +04:00
and print_par_expr buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {lpar; inside; rpar} = value in
print_token buffer lpar "(";
print_expr buffer inside;
print_token buffer rpar ")"
and print_pattern buffer = function
PCons {value; _} -> print_nsepseq buffer "#" print_pattern value
| PVar var -> print_var buffer var
| PWild wild -> print_token buffer wild "_"
| PInt i -> print_int buffer i
| PBytes b -> print_bytes buffer b
| PString s -> print_string buffer s
| PUnit region -> print_token buffer region "Unit"
| PFalse region -> print_token buffer region "False"
| PTrue region -> print_token buffer region "True"
| PNone region -> print_token buffer region "None"
| PSome psome -> print_psome buffer psome
| PList pattern -> print_list_pattern buffer pattern
| PTuple ptuple -> print_ptuple buffer ptuple
| PConstr pattern -> print_constr_pattern buffer pattern
and print_constr_pattern buffer {value; _} =
2019-05-13 00:56:22 +04:00
let (constr, args) = value in
print_constr buffer constr;
match args with
None -> ()
| Some tuple -> print_ptuple buffer tuple
2019-05-13 00:56:22 +04:00
and print_psome buffer {value; _} =
2019-05-13 00:56:22 +04:00
let c_Some, patterns = value in
print_token buffer c_Some "Some";
print_patterns buffer patterns
2019-05-13 00:56:22 +04:00
and print_patterns buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {lpar; inside; rpar} = value in
print_token buffer lpar "(";
print_pattern buffer inside;
print_token buffer rpar ")"
and print_list_pattern buffer = function
Sugar sugar ->
print_injection buffer "list" print_pattern sugar
| PNil kwd_nil ->
print_token buffer kwd_nil "nil"
| Raw raw ->
print_raw buffer raw
and print_raw buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {lpar; inside; rpar} = value in
let head, cons, tail = inside in
print_token buffer lpar "(";
print_pattern buffer head;
print_token buffer cons "#";
print_pattern buffer tail;
print_token buffer rpar ")"
2019-05-13 00:56:22 +04:00
and print_ptuple buffer {value; _} =
2019-05-13 00:56:22 +04:00
let {lpar; inside; rpar} = value in
print_token buffer lpar "(";
print_nsepseq buffer "," print_pattern inside;
print_token buffer rpar ")"
2019-05-13 00:56:22 +04:00
and print_terminator buffer = function
Some semi -> print_token buffer semi ";"
2019-05-13 00:56:22 +04:00
| None -> ()
(* Conversion to string *)
let to_string printer node =
let buffer = Buffer.create 131 in
let () = printer buffer node
in Buffer.contents buffer
let tokens_to_string = to_string print_tokens
let path_to_string = to_string print_path
let pattern_to_string = to_string print_pattern
let instruction_to_string = to_string print_instruction