libgrew.ml 3.5 KB
Newer Older
pj2m's avatar
pj2m committed
1 2
include Grew_types

bguillaum's avatar
bguillaum committed
3
open Printf
pj2m's avatar
pj2m committed
4 5
open Log

6
open Utils
pj2m's avatar
pj2m committed
7 8 9 10 11 12 13
open Grew_parser
open Grs
open Graph
open Rule
open HTMLer


bguillaum's avatar
bguillaum committed
14

pj2m's avatar
pj2m committed
15 16 17 18 19
exception Parsing_err of string
exception File_dont_exists of string

exception Build of string * (string * int) option
exception Run of string * (string * int) option
bguillaum's avatar
bguillaum committed
20
exception Bug of string * (string * int) option
pj2m's avatar
pj2m committed
21 22 23

type grs = Grs.t
type gr = Instance.t
bguillaum's avatar
bguillaum committed
24
type rew_history = Rewrite_history.t
pj2m's avatar
pj2m committed
25

bguillaum's avatar
bguillaum committed
26 27
let is_empty = Rewrite_history.is_empty

pj2m's avatar
pj2m committed
28 29
let empty_grs = Grs.empty

bguillaum's avatar
bguillaum committed
30 31 32 33
let load_grs ?doc_output_dir file =
  if not (Sys.file_exists file)
  then raise (File_dont_exists file)
  else
34 35 36
    try
      let ast = Grew_parser.parse_file_to_grs file in
      (* Checker.check_grs ast; *)
bguillaum's avatar
bguillaum committed
37 38 39 40 41
      (match doc_output_dir with
      | None -> ()
      | Some dir -> HTMLer.proceed dir ast);
      Grs.build ast
    with
42 43
    | Grew_parser.Parse_error msg -> raise (Parsing_err msg)
    | Utils.Build (msg,loc) -> raise (Build (msg,loc))
44
    | Utils.Bug (msg, loc) -> raise (Bug (msg,loc))
bguillaum's avatar
bguillaum committed
45
    | exc -> raise (Bug (sprintf "UNCATCHED EXCEPTION: %s" (Printexc.to_string exc), None))
bguillaum's avatar
bguillaum committed
46 47


48
let get_sequence_names grs = Grs.sequence_names grs
bguillaum's avatar
bguillaum committed
49

pj2m's avatar
pj2m committed
50 51
let empty_gr = Instance.empty

bguillaum's avatar
bguillaum committed
52
let load_gr file =
53 54 55
  if (Sys.file_exists file) then (
    try
      let ast = Grew_parser.parse_file_to_gr file in
bguillaum's avatar
bguillaum committed
56
      (* Checker.check_gr ast;*)
57 58 59 60 61
      Instance.build ast
    with
    | Grew_parser.Parse_error msg -> raise (Parsing_err msg)
    | Utils.Build (msg,loc) -> raise (Build (msg,loc))
    | Utils.Bug (msg, loc) -> raise (Bug (msg,loc))
bguillaum's avatar
bguillaum committed
62
    | exc -> raise (Bug (sprintf "UNCATCHED EXCEPTION: %s" (Printexc.to_string exc), None))
63 64 65 66

   ) else (
    raise (File_dont_exists file)
   )
pj2m's avatar
pj2m committed
67

68
let rewrite ~gr ~grs ~seq = 
69
  try Grs.rewrite grs seq gr
70 71 72 73 74
  with
  | Utils.Run (msg,loc) -> raise (Run (msg,loc))
  | Utils.Bug (msg, loc) -> raise (Bug (msg,loc))
  | exc -> raise (Bug (sprintf "UNCATCHED EXCEPTION: %s" (Printexc.to_string exc), None))

bguillaum's avatar
bguillaum committed
75 76

let display ~gr ~grs ~seq =
77
  try Grs.build_rew_display grs seq gr
pj2m's avatar
pj2m committed
78 79
  with
  | Utils.Run (msg,loc) -> raise (Run (msg,loc))
bguillaum's avatar
bguillaum committed
80
  | Utils.Bug (msg, loc) -> raise (Bug (msg,loc))
bguillaum's avatar
bguillaum committed
81 82 83 84 85
  | exc -> raise (Bug (sprintf "UNCATCHED EXCEPTION: %s" (Printexc.to_string exc), None))


let write_stat filename rew_hist = Gr_stat.save filename (Gr_stat.from_rew_history rew_hist) 

bguillaum's avatar
bguillaum committed
86 87 88 89 90
let save_index ~dirname ~base_names =
  let out_ch = open_out (Filename.concat dirname "index") in
  List.iter (fun f -> fprintf out_ch "%s\n" f) base_names;
  close_out out_ch

bguillaum's avatar
bguillaum committed
91 92 93 94 95
let write_html 
    ?(no_init=false) ?main_feat 
    ~header
    rew_hist
    output_base =
96
IFDEF DEP2PICT THEN
97
  ignore (
bguillaum's avatar
bguillaum committed
98 99 100 101 102 103
  Rewrite_history.save_html 
    ?main_feat 
    ~init_graph: (not no_init)
    ~header
    output_base rew_hist
    )
104 105 106
ELSE
    Log.critical "[write_html] The \"libcaml-grew\" library is compiled without Dep2pict"
ENDIF
107 108 109 110 111 112

let error_html 
    ?(no_init=false) ?main_feat 
    ~header
    msg ?init
    output_base =
113
IFDEF DEP2PICT THEN
114 115 116 117 118 119 120
  ignore (
  Rewrite_history.error_html 
    ?main_feat 
    ~init_graph: (not no_init)
    ~header
    output_base msg init
    )
121 122 123
ELSE
    Log.critical "[_html] The \"libcaml-grew\" library is compiled without Dep2pict"
ENDIF
124 125


bguillaum's avatar
bguillaum committed
126

bguillaum's avatar
bguillaum committed
127 128
let make_index ~title ~grs_file ~html ~grs ~seq ~output_dir ~base_names  =
  let init = Corpus_stat.empty grs seq in
bguillaum's avatar
bguillaum committed
129 130 131 132 133 134 135 136
  let corpus_stat =
    List.fold_left
      (fun acc base_name -> 
        Corpus_stat.add_gr_stat base_name (Gr_stat.load (Filename.concat output_dir (base_name^".stat"))) acc
      ) init base_names in
  Corpus_stat.save_html title grs_file html output_dir corpus_stat

  
137

pj2m's avatar
pj2m committed
138 139
let get_css_file = Filename.concat DATA_DIR "style.css"