libgrew.ml 3.28 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 14
open Grew_parser
open Checker
open Grs
open Graph
open Rule
open HTMLer


bguillaum's avatar
bguillaum committed
15

pj2m's avatar
pj2m committed
16 17 18 19 20
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
21
exception Bug of string * (string * int) option
pj2m's avatar
pj2m committed
22 23 24

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

let empty_grs = Grs.empty

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


pj2m's avatar
pj2m committed
47
let get_available_seq grs = Grs.sequences grs
bguillaum's avatar
bguillaum committed
48

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

bguillaum's avatar
bguillaum committed
51
let load_gr file =
52 53 54
  if (Sys.file_exists file) then (
    try
      let ast = Grew_parser.parse_file_to_gr file in
bguillaum's avatar
bguillaum committed
55
      (* Checker.check_gr ast;*)
56 57 58 59 60
      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
61
    | exc -> raise (Bug (sprintf "UNCATCHED EXCEPTION: %s" (Printexc.to_string exc), None))
62 63 64 65

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

67
let rewrite ~gr ~grs ~seq = 
68
  try Grs.rewrite grs seq gr
69 70 71 72 73
  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
74 75

let display ~gr ~grs ~seq =
76
  try Grs.build_rew_display grs seq gr
pj2m's avatar
pj2m committed
77 78
  with
  | Utils.Run (msg,loc) -> raise (Run (msg,loc))
bguillaum's avatar
bguillaum committed
79
  | Utils.Bug (msg, loc) -> raise (Bug (msg,loc))
bguillaum's avatar
bguillaum committed
80 81 82 83 84 85 86 87 88 89
  | 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) 

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

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


bguillaum's avatar
bguillaum committed
120 121 122 123 124 125 126 127 128 129 130

let make_index ~title ~grs_file ~html ~grs ~output_dir ~base_names  =
  let init = Corpus_stat.empty grs in
  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

  
131

pj2m's avatar
pj2m committed
132 133
let get_css_file = Filename.concat DATA_DIR "style.css"