Project

General

Profile

Download (1.84 KB) Statistics
| Branch: | Tag: | Revision:
1 bad7b67a Christophe Garion
open Basetypes
2 5a71ed25 Christophe Garion
open Cmdliner
3 bad7b67a Christophe Garion
open Datatype
4
open Parser_json
5 57ecad58 Christophe Garion
open Sys
6 bad7b67a Christophe Garion
7
module ParseExt =
8
struct
9 eb70bae5 Christophe Garion
  let parse_condition _ = Condition.tru
10
  let parse_action    _ = Action.nil
11 bad7b67a Christophe Garion
  let parse_event json  = Some Yojson.Basic.(json |> to_string)
12
end
13
14
module Parse = Parser (ParseExt)
15
16 5a71ed25 Christophe Garion
(* setup for logging *)
17
let setup_log style_renderer level =
18
  Fmt_tty.setup_std_outputs ?style_renderer ();
19
  Logs.set_level level;
20
  Logs.set_reporter (Logs_fmt.reporter ());
21
  ()
22
23
(* function representing the program to execute *)
24
let json_parse _ file pp =
25
  let prog = Parse.parse_prog (Yojson.Basic.from_file file) in
26
  if pp then
27
    SF.pp_prog Format.std_formatter prog
28
29
(* term representing argument for file *)
30
let file =
31
  let doc = "The file to parse." in
32
  let env = Arg.env_var "JSON_FILE" ~doc in
33
  let doc = "The file to parse." in
34
  Arg.(required & pos 0 (some string) None & info [] ~env ~docv:"FILE" ~doc)
35
36
(* term representing argument for flag for pretty printing the program *)
37
let pp =
38
  let doc = "Pretty print the resulting program" in
39
  Arg.(value & flag & info ["pp"; "pretty-print"] ~docv:"PP" ~doc)
40
41
(* term for argument for logging *)
42
let setup_log_arg =
43
  let env = Arg.env_var "TOOL_VERBOSITY" in
44
  Term.(const setup_log $ Fmt_cli.style_renderer () $ Logs_cli.level ~env ())
45
46
(* term representing the program to execute *)
47
let json_parse_t = Term.(const json_parse $ setup_log_arg $ file $ pp)
48
49
(* term info for manpages etc. *)
50
let info =
51
  let doc = "parse a JSON file representing a Stateflow model" in
52
  let man = [
53
    `S Manpage.s_bugs;
54
    `P "Report bug to Github issues tracking." ]
55
  in
56
  Term.info "json-parser-example" ~doc ~exits:Term.default_exits ~man
57
58
let main () =
59 bad7b67a Christophe Garion
  begin
60 57ecad58 Christophe Garion
    let json = Yojson.Basic.from_file Sys.argv.(1) in
61
    SF.pp_prog Format.std_formatter (Parse.parse_prog json);
62 bad7b67a Christophe Garion
  end
63
64 5a71ed25 Christophe Garion
(* program *)
65
let _ = Term.exit @@ Term.eval (json_parse_t, info)