ocamlformat: new break-infix rule

This commit is contained in:
Emile Rolley 2022-08-03 17:07:35 +02:00
parent d85812109c
commit ba620fca28
20 changed files with 153 additions and 64 deletions

View File

@ -9,3 +9,5 @@ cases-exp-indent=2
indicate-multiline-delimiters=no
parens-tuple=multi-line-only
space-around-lists=false
break-infix-before-func
break-infix= fit-or-vertical

View File

@ -24,7 +24,8 @@ module Nj = Ninja_utils
let files_or_folders =
Arg.(
non_empty & pos_right 0 file []
non_empty
& pos_right 0 file []
& info [] ~docv:"FILE(S)" ~doc:"File(s) or folder(s) to process")
let command =
@ -38,7 +39,8 @@ let debug =
let reset_test_outputs =
Arg.(
value & flag
value
& flag
& info ["r"; "reset"]
~doc:
"Used with the `test` command, resets the test output to whatever is \
@ -88,8 +90,16 @@ let catala_opts =
let clerk_t f =
Term.(
const f $ files_or_folders $ command $ catalac $ catala_opts $ makeflags
$ debug $ scope $ reset_test_outputs $ ninja_output)
const f
$ files_or_folders
$ command
$ catalac
$ catala_opts
$ makeflags
$ debug
$ scope
$ reset_test_outputs
$ ninja_output)
let version = "0.5.0"
@ -336,7 +346,8 @@ let collect_all_ninja_build
let expected_output_file =
expected_output.output_dir
^ Filename.basename expected_output.tested_filename
^ "." ^ expected_output.id
^ "."
^ expected_output.id
in
let vars =
[
@ -392,7 +403,8 @@ let add_root_test_build
(all_file_names : string list)
(all_test_builds : string) : ninja =
let file_names_str =
List.hd all_file_names ^ ""
List.hd all_file_names
^ ""
^ List.fold_left
(fun acc name -> acc ^ "; " ^ name)
"" (List.tl all_file_names)
@ -440,9 +452,11 @@ let get_catala_files_in_folder (dir : string) : string list =
false
in
if f_is_dir then
readdir_sort f |> Array.to_list
readdir_sort f
|> Array.to_list
|> List.map (Filename.concat f)
|> List.append fs |> loop result
|> List.append fs
|> loop result
else loop (f :: result) fs
| [] -> result
in

View File

@ -463,14 +463,17 @@ let rec free_vars_expr (e : 'm marked_expr) : VarSet.t =
| EInj (e1, _, _, _) ->
free_vars_expr e1
| EApp (e1, es) | EMatch (e1, es, _) ->
e1 :: es |> List.map free_vars_expr
e1 :: es
|> List.map free_vars_expr
|> List.fold_left VarSet.union VarSet.empty
| EDefault (es, ejust, econs) ->
ejust :: econs :: es |> List.map free_vars_expr
ejust :: econs :: es
|> List.map free_vars_expr
|> List.fold_left VarSet.union VarSet.empty
| EOp _ | ELit _ -> VarSet.empty
| EIfThenElse (e1, e2, e3) ->
[e1; e2; e3] |> List.map free_vars_expr
[e1; e2; e3]
|> List.map free_vars_expr
|> List.fold_left VarSet.union VarSet.empty
| EAbs (binder, _) ->
let vs, body = Bindlib.unmbind binder in
@ -672,7 +675,8 @@ let rec equal_exprs (e1 : 'm marked_expr) (e2 : 'm marked_expr) : bool =
| EAssert e1, EAssert e2 -> equal_exprs e1 e2
| EOp op1, EOp op2 -> equal_ops op1 op2
| EDefault (exc1, def1, cons1), EDefault (exc2, def2, cons2) ->
equal_exprs def1 def2 && equal_exprs cons1 cons2
equal_exprs def1 def2
&& equal_exprs cons1 cons2
&& equal_exprs_list exc1 exc2
| EIfThenElse (if1, then1, else1), EIfThenElse (if2, then2, else2) ->
equal_exprs if1 if2 && equal_exprs then1 then2 && equal_exprs else1 else2

View File

@ -198,7 +198,8 @@ let rec evaluate_operator
A.ELit
(LBool
(try
en1 = en2 && i1 = i2
en1 = en2
&& i1 = i2
&&
match evaluate_operator ctx op pos [e1; e2] with
| A.ELit (LBool b) -> b

View File

@ -132,7 +132,8 @@ let check_for_cycle (scope : Ast.scope) (g : ScopeDependencies.t) : unit =
Marked.get_mark var_info );
( Some
("Used here in the definition of another cycle variable "
^ succ_str ^ ":"),
^ succ_str
^ ":"),
edge_pos );
])
scc)
@ -468,12 +469,15 @@ let check_for_exception_cycle (g : ExceptionsDependencies.t) : unit =
in
[
( Some
("Cyclic exception for definition of variable \"" ^ var_str
^ "\", declared here:"),
("Cyclic exception for definition of variable \""
^ var_str
^ "\", declared here:"),
Marked.get_mark var_info );
( Some
("Used here in the definition of another cyclic exception \
for defining \"" ^ var_str ^ "\":"),
for defining \""
^ var_str
^ "\":"),
List.hd edge_pos );
])
scc)

View File

@ -688,7 +688,8 @@ let translate_program (pgrm : Ast.program) : Scopelang.Ast.program =
in
( Marked.unmark
(Ast.ScopeVar.get_info scope_var)
^ "_" ^ state_name,
^ "_"
^ state_name,
state_pos )) ))
states))
ctx.scope_var_mapping;

View File

@ -101,7 +101,8 @@ let driver source_file (options : Cli.options) : int =
let output_file, with_output = get_output ~ext:".d" () in
Cli.debug_print "Writing list of dependencies to %s..."
(Option.value ~default:"stdout" output_file);
with_output @@ fun oc ->
with_output
@@ fun oc ->
Printf.fprintf oc "%s:\\\n%s\n%s:"
(String.concat "\\\n"
(Option.value ~default:"stdout" output_file
@ -165,7 +166,8 @@ let driver source_file (options : Cli.options) : int =
match backend with
| `Scopelang ->
let _output_file, with_output = get_output_format () in
with_output @@ fun fmt ->
with_output
@@ fun fmt ->
if Option.is_some options.ex_scope then
Format.fprintf fmt "%a\n"
(Scopelang.Print.format_scope ~debug:options.debug)
@ -191,7 +193,8 @@ let driver source_file (options : Cli.options) : int =
match backend with
| `Dcalc ->
let _output_file, with_output = get_output_format () in
with_output @@ fun fmt ->
with_output
@@ fun fmt ->
if Option.is_some options.ex_scope then
Format.fprintf fmt "%a\n"
(Dcalc.Print.format_scope ~debug:options.debug prgm.decl_ctx)
@ -295,7 +298,8 @@ let driver source_file (options : Cli.options) : int =
match backend with
| `Lcalc ->
let _output_file, with_output = get_output_format () in
with_output @@ fun fmt ->
with_output
@@ fun fmt ->
if Option.is_some options.ex_scope then
Format.fprintf fmt "%a\n"
(Lcalc.Print.format_scope ~debug:options.debug prgm.decl_ctx)
@ -326,7 +330,8 @@ let driver source_file (options : Cli.options) : int =
let output_file, with_output =
get_output_format ~ext:".ml" ()
in
with_output @@ fun fmt ->
with_output
@@ fun fmt ->
Cli.debug_print "Compiling program into OCaml...";
Cli.debug_print "Writing to %s..."
(Option.value ~default:"stdout" output_file);
@ -344,7 +349,8 @@ let driver source_file (options : Cli.options) : int =
match backend with
| `Scalc ->
let _output_file, with_output = get_output_format () in
with_output @@ fun fmt ->
with_output
@@ fun fmt ->
if Option.is_some options.ex_scope then
Format.fprintf fmt "%a\n"
(Scalc.Print.format_scope ~debug:options.debug
@ -367,7 +373,8 @@ let driver source_file (options : Cli.options) : int =
Cli.debug_print "Compiling program into Python...";
Cli.debug_print "Writing to %s..."
(Option.value ~default:"stdout" output_file);
with_output @@ fun fmt ->
with_output
@@ fun fmt ->
Scalc.To_python.format_program fmt prgm type_ordering
| `Plugin (Plugin.Lcalc _) -> assert false
| `Plugin (Plugin.Scalc p) ->

View File

@ -222,7 +222,8 @@ let option_enum_config : (D.EnumConstructor.t * D.typ Marked.pos) list =
let make_none m =
let mark = Marked.mark m in
let tunit = D.TLit D.TUnit, D.mark_pos m in
Bindlib.box @@ mark
Bindlib.box
@@ mark
@@ EInj
( Marked.mark
(D.map_mark (fun pos -> pos) (fun _ -> tunit) m)

View File

@ -148,7 +148,10 @@ let avoid_keywords (s : string) : string =
let format_struct_name (fmt : Format.formatter) (v : Dcalc.Ast.StructName.t) :
unit =
Format.asprintf "%a" Dcalc.Ast.StructName.format_t v
|> to_ascii |> to_snake_case |> avoid_keywords |> Format.fprintf fmt "%s"
|> to_ascii
|> to_snake_case
|> avoid_keywords
|> Format.fprintf fmt "%s"
let format_to_module_name
(fmt : Format.formatter)
@ -156,9 +159,13 @@ let format_to_module_name
(match name with
| `Ename v -> Format.asprintf "%a" D.EnumName.format_t v
| `Sname v -> Format.asprintf "%a" D.StructName.format_t v)
|> to_ascii |> to_snake_case |> avoid_keywords |> String.split_on_char '_'
|> to_ascii
|> to_snake_case
|> avoid_keywords
|> String.split_on_char '_'
|> List.map String.capitalize_ascii
|> String.concat "" |> Format.fprintf fmt "%s"
|> String.concat ""
|> Format.fprintf fmt "%s"
let format_struct_field_name
(fmt : Format.formatter)

View File

@ -160,7 +160,8 @@ let pygmentize_code (c : string Marked.pos) (language : C.backend_lang) : string
(** {1 Weaving} *)
let sanitize_html_href str =
str |> Ubase.from_utf8
str
|> Ubase.from_utf8
|> R.substitute ~rex:(R.regexp "[' '°]") ~subst:(function _ -> "%20")
let rec law_structure_to_html

View File

@ -182,7 +182,8 @@ let check_exceeding_lines
(start_line : int)
(filename : string)
(content : string) =
content |> String.split_on_char '\n'
content
|> String.split_on_char '\n'
|> List.iteri (fun i s ->
if String.length s > max_len then (
Cli.warning_print "The line %s in %s is exceeding %s characters:"

View File

@ -42,7 +42,10 @@ module To_jsoo = struct
(v : Dcalc.Ast.StructFieldName.t) : unit =
let s =
Format.asprintf "%a" Dcalc.Ast.StructFieldName.format_t v
|> to_ascii |> to_snake_case |> avoid_keywords |> to_camel_case
|> to_ascii
|> to_snake_case
|> avoid_keywords
|> to_camel_case
in
Format.fprintf fmt "%s" s
@ -128,10 +131,14 @@ module To_jsoo = struct
let format_var_camel_case (fmt : Format.formatter) (v : 'm var) : unit =
let lowercase_name =
Bindlib.name_of v |> to_ascii |> to_snake_case
Bindlib.name_of v
|> to_ascii
|> to_snake_case
|> Re.Pcre.substitute ~rex:(Re.Pcre.regexp "\\.") ~subst:(fun _ ->
"_dot_")
|> to_ascii |> avoid_keywords |> to_camel_case
|> to_ascii
|> avoid_keywords
|> to_camel_case
in
if
List.mem lowercase_name ["handle_default"; "handle_default_opt"]

View File

@ -40,7 +40,10 @@ module To_json = struct
(v : Dcalc.Ast.StructFieldName.t) : unit =
let s =
Format.asprintf "%a" Dcalc.Ast.StructFieldName.format_t v
|> to_ascii |> to_snake_case |> avoid_keywords |> to_camel_case
|> to_ascii
|> to_snake_case
|> avoid_keywords
|> to_camel_case
in
Format.fprintf fmt "%s" s

View File

@ -26,7 +26,7 @@ let extension = ".py"
let apply ~source_file ~output_file ~scope prgm type_ordering =
ignore source_file;
ignore scope;
Utils.File.with_formatter_of_opt_file output_file @@ fun fmt ->
Scalc.To_python.format_program fmt prgm type_ordering
Utils.File.with_formatter_of_opt_file output_file
@@ fun fmt -> Scalc.To_python.format_program fmt prgm type_ordering
let () = Driver.Plugin.register_scalc ~name ~extension apply

View File

@ -193,9 +193,13 @@ let rec format_typ (fmt : Format.formatter) (typ : Dcalc.Ast.typ Marked.pos) :
| TAny -> Format.fprintf fmt "Any"
let format_name_cleaned (fmt : Format.formatter) (s : string) : unit =
s |> to_ascii |> to_snake_case
s
|> to_ascii
|> to_snake_case
|> Re.Pcre.substitute ~rex:(Re.Pcre.regexp "\\.") ~subst:(fun _ -> "_dot_")
|> to_ascii |> avoid_keywords |> Format.fprintf fmt "%s"
|> to_ascii
|> avoid_keywords
|> Format.fprintf fmt "%s"
module StringMap = Map.Make (String)
module IntMap = Map.Make (Int)

View File

@ -103,7 +103,8 @@ let check_for_cycle_in_scope (g : SDependencies.t) : unit =
Marked.get_mark var_info );
( Some
("Used here in the definition of another cycle variable "
^ succ_str ^ ":"),
^ succ_str
^ ":"),
edge_pos );
])
scc)
@ -254,7 +255,8 @@ let check_type_cycles (structs : Ast.struct_ctx) (enums : Ast.enum_ctx) :
Marked.get_mark var_info );
( Some
("Used here in the definition of another cycle type "
^ succ_str ^ ":"),
^ succ_str
^ ":"),
edge_pos );
])
scc)

View File

@ -464,7 +464,8 @@ let translate_rule
let a_name =
Marked.map_under_mark
(fun str ->
str ^ "."
str
^ "."
^ Marked.unmark (Ast.ScopeVar.get_info (Marked.unmark subs_var)))
(Ast.SubScopeName.get_info (Marked.unmark subs_index))
in

View File

@ -109,7 +109,8 @@ let color =
let unstyled =
Arg.(
value & flag
value
& flag
& info ["unstyled"; "u"]
~doc:
"Removes styling (colors, etc.) from terminal output. Equivalent to \
@ -120,7 +121,8 @@ let optimize =
let trace_opt =
Arg.(
value & flag
value
& flag
& info ["trace"; "t"]
~doc:
"Displays a trace of the interpreter's computation or generates \
@ -128,25 +130,29 @@ let trace_opt =
let avoid_exceptions =
Arg.(
value & flag
value
& flag
& info ["avoid_exceptions"]
~doc:"Compiles the default calculus without exceptions")
let closure_conversion =
Arg.(
value & flag
value
& flag
& info ["closure_conversion"]
~doc:"Performs closure conversion on the lambda calculus")
let wrap_weaved_output =
Arg.(
value & flag
value
& flag
& info ["wrap"; "w"]
~doc:"Wraps literate programming output with a minimal preamble.")
let print_only_law =
Arg.(
value & flag
value
& flag
& info ["print_only_law"]
~doc:
"In literate programming output, skip all code and metadata sections \
@ -189,7 +195,8 @@ let max_prec_digits_opt =
let disable_counterexamples_opt =
Arg.(
value & flag
value
& flag
& info
["disable_counterexamples"]
~doc:
@ -268,10 +275,23 @@ let options =
}
in
Term.(
const make $ debug $ color $ unstyled $ wrap_weaved_output
$ avoid_exceptions $ closure_conversion $ backend $ plugins_dirs $ language
$ max_prec_digits_opt $ trace_opt $ disable_counterexamples_opt $ optimize
$ ex_scope $ output $ print_only_law)
const make
$ debug
$ color
$ unstyled
$ wrap_weaved_output
$ avoid_exceptions
$ closure_conversion
$ backend
$ plugins_dirs
$ language
$ max_prec_digits_opt
$ trace_opt
$ disable_counterexamples_opt
$ optimize
$ ex_scope
$ output
$ print_only_law)
let catala_t f = Term.(const f $ file $ options)
@ -432,7 +452,9 @@ let concat_with_line_depending_prefix_and_suffix
let out, _ =
List.fold_left
(fun (acc, i) s ->
( (acc ^ prefix i ^ s
( (acc
^ prefix i
^ s
^ if i = List.length ss - 1 then "" else suffix i),
i + 1 ))
((prefix 0 ^ hd ^ if 0 = List.length ss - 1 then "" else suffix 0), 1)

View File

@ -91,12 +91,15 @@ let event_manager : event_manager Js.t =
Js.wrap_meth_callback (fun () ->
Js.array
(Array.of_list
(R_ocaml.retrieve_log () |> R_ocaml.EventParser.parse_raw_events
(R_ocaml.retrieve_log ()
|> R_ocaml.EventParser.parse_raw_events
|> List.map (fun event ->
object%js
val mutable data =
event |> R_ocaml.yojson_of_event
|> Yojson.Safe.to_string |> Js.string
event
|> R_ocaml.yojson_of_event
|> Yojson.Safe.to_string
|> Js.string
end))))
method retrieveRawEvents =
@ -130,7 +133,8 @@ let event_manager : event_manager Js.t =
| EndCall _ | BeginCall _ | DecisionTaken _ ->
R_ocaml.unembeddable ())
|> R_ocaml.yojson_of_runtime_value
|> Yojson.Safe.to_string |> Js.string
|> Yojson.Safe.to_string
|> Js.string
val mutable sourcePosition =
match evt with

View File

@ -436,16 +436,19 @@ module EventParser = struct
{ pos = Some pos; name; value; fun_calls = Some fun_calls } )
| event :: _ ->
failwith
("Invalid function call ([ " ^ String.concat ", " infos
^ " ]): expected variable definition (function output), found: "
^ raw_event_to_string event ^ "["
("Invalid function call ([ "
^ String.concat ", " infos
^ " ]): expected variable definition (function output), found: "
^ raw_event_to_string event
^ "["
^ (nb_raw_events - List.length rest + 1 |> string_of_int)
^ "]")
| [] ->
failwith
("Invalid function call ([ " ^ String.concat ", " infos
^ " ]): expected variable definition (function output), found: \
end of tokens")
("Invalid function call ([ "
^ String.concat ", " infos
^ " ]): expected variable definition (function output), found: \
end of tokens")
in
parse_events { ctx with events = var_comp :: ctx.events; rest }