Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hhugo committed Nov 26, 2024
1 parent 375f001 commit 7226c7d
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions tools/printast/printast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@ open Ocamlformat_lib

let ocaml_version = Ocaml_version.sys_version

let extended_ast ppf syntax ~input_name content =
let extended_ast ppf syntax ~input_name ~metaocaml content =
let open Extended_ast in
let (Any kind) = of_syntax syntax in
Parse.ast kind ~ocaml_version ~preserve_beginend:true ~input_name content
Parse.ast kind ~ocaml_version ~metaocaml ~preserve_beginend:true
~input_name content
|> Printast.ast kind ppf

let std_ast ppf syntax ~input_name content =
let std_ast ppf syntax ~input_name ~metaocaml content =
let open Std_ast in
let (Any kind) = of_syntax syntax in
Parse.ast kind ~ocaml_version ~input_name content |> Printast.ast kind ppf
Parse.ast kind ~ocaml_version ~metaocaml ~input_name content
|> Printast.ast kind ppf

let get_arg () =
let std = ref false and input = ref None in
let opts = [("-std", Arg.Set std, "Use the standard parser")] in
let usage = "printast [-std] <file>" in
let std = ref false and input = ref None and metaocaml = ref false in
let opts =
[ ("-std", Arg.Set std, "Use the standard parser")
; ("-metaocaml", Arg.Set metaocaml, "Enable metaocaml syntax") ]
in
let usage = "printast [-std] [-metaocaml] <file>" in
Arg.parse opts (fun inp -> input := Some inp) usage ;
let input =
match !input with
Expand All @@ -26,13 +31,14 @@ let get_arg () =
Printf.eprintf "Not enough argument\n" ;
exit 2
and parse_and_print = if !std then std_ast else extended_ast in
(parse_and_print, input)
(parse_and_print, input, !metaocaml)

let () =
let parse_and_print, inputf = get_arg () in
let parse_and_print, inputf, metaocaml = get_arg () in
let syntax =
Option.value ~default:Syntax.Use_file (Syntax.of_fname inputf)
in
Printf.printf "Reading %S\n" inputf ;
let content = In_channel.read_all inputf in
parse_and_print Format.std_formatter syntax ~input_name:inputf content
parse_and_print Format.std_formatter syntax ~input_name:inputf ~metaocaml
content

0 comments on commit 7226c7d

Please sign in to comment.