Skip to content

Commit 0b57fc3

Browse files
committed
Support formatting sail code via stdin
1 parent 8a1836c commit 0b57fc3

File tree

1 file changed

+44
-25
lines changed

1 file changed

+44
-25
lines changed

src/bin/sail.ml

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -578,32 +578,51 @@ let run_sail_format (config : Yojson.Safe.t option) =
578578
| None -> Format_sail.default_config
579579
end in
580580
let module Formatter = Format_sail.Make (Config) in
581-
let parsed_files = List.map (fun f -> (f, Initial_check.parse_file f)) !opt_free_arguments in
582-
List.iter
583-
(fun (f, (comments, parse_ast)) ->
584-
let source = file_to_string f in
585-
if is_format_file f && not (is_skipped_file f) then (
586-
let formatted = Formatter.format_defs ~debug:!opt_format_debug f source comments parse_ast in
587-
begin
588-
match !opt_format_backup with
589-
| Some suffix ->
590-
let out_chan = open_out (f ^ "." ^ suffix) in
591-
output_string out_chan source;
592-
close_out out_chan
593-
| None -> ()
594-
end;
595-
match !opt_format_emit with
596-
| "file" ->
597-
let file_info = Util.open_output_with_check f in
598-
output_string file_info.channel formatted;
599-
Util.close_output_with_check file_info
600-
| "stdout" ->
601-
output_string stdout formatted;
602-
flush stdout
603-
| _ -> raise (Failure "unknown format_emit option")
581+
if List.is_empty !opt_free_arguments then (
582+
let read_stdin_lines () =
583+
let rec read_loop acc =
584+
try
585+
let line = input_line stdin in
586+
read_loop (line :: acc)
587+
with End_of_file -> List.rev acc
588+
in
589+
read_loop []
590+
in
591+
let source = String.concat "\n" (read_stdin_lines ()) in
592+
let f = "stdin" in
593+
let comments, parse_ast = Initial_check.parse_file_from_string ~filename:f ~contents:source in
594+
let formatted = Formatter.format_defs f source comments parse_ast in
595+
output_string stdout formatted;
596+
flush stdout
597+
)
598+
else (
599+
let parsed_files = List.map (fun f -> (f, Initial_check.parse_file f)) !opt_free_arguments in
600+
List.iter
601+
(fun (f, (comments, parse_ast)) ->
602+
let source = file_to_string f in
603+
if is_format_file f && not (is_skipped_file f) then (
604+
let formatted = Formatter.format_defs ~debug:true f source comments parse_ast in
605+
begin
606+
match !opt_format_backup with
607+
| Some suffix ->
608+
let out_chan = open_out (f ^ "." ^ suffix) in
609+
output_string out_chan source;
610+
close_out out_chan
611+
| None -> ()
612+
end;
613+
match !opt_format_emit with
614+
| "file" ->
615+
let file_info = Util.open_output_with_check f in
616+
output_string file_info.channel formatted;
617+
Util.close_output_with_check file_info
618+
| "stdout" ->
619+
output_string stdout formatted;
620+
flush stdout
621+
| _ -> raise (Failure "unknown format_emit option")
622+
)
604623
)
605-
)
606-
parsed_files
624+
parsed_files
625+
)
607626

608627
let feature_check () =
609628
match !opt_have_feature with None -> () | Some symbol -> if Preprocess.have_symbol symbol then exit 0 else exit 2

0 commit comments

Comments
 (0)