gmanager.ml 11.2 KB
Newer Older
MARCHE Claude's avatar
MARCHE Claude committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
(**************************************************************************)
(*                                                                        *)
(*  Copyright (C) 2010-                                                   *)
(*    Francois Bobot                                                      *)
(*    Jean-Christophe Filliatre                                           *)
(*    Johannes Kanig                                                      *)
(*    Andrei Paskevich                                                    *)
(*                                                                        *)
(*  This software is free software; you can redistribute it and/or        *)
(*  modify it under the terms of the GNU Library General Public           *)
(*  License version 2.1, with the special exception on linking            *)
(*  described in file LICENSE.                                            *)
(*                                                                        *)
(*  This software is distributed in the hope that it will be useful,      *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *)
(*                                                                        *)
(**************************************************************************)

let pname = "[Why-rustprover]"

let () = ignore (GtkMain.Main.init ())

open Format
open Why
open Whyconf

(******************************)
(* loading user configuration *)
(******************************)

let config = 
  try 
    Whyconf.read_config None
  with 
      Not_found -> 
        eprintf "%s No config file found.@." pname;
        exit 1
    | Whyconf.Error e ->
        eprintf "%s Error while reading config file: %a@." pname 
          Whyconf.report e;
        exit 1

let () = eprintf "%s Load path is: %a@." pname
  (Pp.print_list Pp.comma Pp.string) config.loadpath

let timelimit = 
  match config.timelimit with
    | None -> 2
    | Some n -> n


(* TODO: put that in config file *)
let window_width = 1024
let window_height = 768
let font_name = "Monospace 10"

let spec = []
let usage_str = "why-rustprover [options] <file>.why"
let file = ref None
let set_file f = match !file with
  | Some _ -> 
      raise (Arg.Bad "only one file")
  | None -> 
      if not (Filename.check_suffix f ".why") then 
	raise (Arg.Bad ("don't know what to do with " ^ f));
      if not (Sys.file_exists f) then begin
	Format.eprintf "%s %s: no such file@." pname f; 
        exit 1
      end;
      file := Some f

let () = Arg.parse spec set_file usage_str

let fname = match !file with
  | None -> 
      Arg.usage spec usage_str; 
      exit 1
  | Some f -> 
      f

let lang =
  let load_path = 
    List.fold_right Filename.concat 
      [Filename.dirname Sys.argv.(0); ".."; "share"] "lang" 
  in
  let languages_manager = 
    GSourceView2.source_language_manager ~default:true 
  in
  languages_manager#set_search_path 
    (load_path :: languages_manager#search_path);
  match languages_manager#language "why" with
    | None -> Format.eprintf "pas trouv@;"; None
    | Some _ as l -> l

let source_text = 
  let ic = open_in fname in
  let size = in_channel_length ic in
  let buf = String.create size in
  really_input ic buf 0 size;
  close_in ic;
  buf

let env = Why.Env.create_env (Why.Typing.retrieve config.loadpath)


(***********************)
(* Parsing input file  *)
(***********************)
   
let rec report fmt = function
  | Lexer.Error e ->
      fprintf fmt "lexical error: %a" Lexer.report e;
  | Loc.Located (loc, e) ->
      fprintf fmt "%a%a" Loc.report_position loc report e
  | Parsing.Parse_error ->
      fprintf fmt "syntax error"
  | Denv.Error e ->
      Denv.report fmt e
  | Typing.Error e ->
      Typing.report fmt e
  | Decl.UnknownIdent i ->
      fprintf fmt "anomaly: unknown ident '%s'" i.Ident.id_string
  | Driver.Error e ->
      Driver.report fmt e
  | Config.Dynlink.Error e ->
      fprintf fmt "Dynlink : %s" (Config.Dynlink.error_message e)
  | e -> fprintf fmt "anomaly: %s" (Printexc.to_string e)


let theories : Theory.theory Theory.Mnm.t =
  try
    let cin = open_in fname in
    let m = Env.read_channel env fname cin in
    close_in cin;
    eprintf "Parsing/Typing Ok@.";
    m
  with e -> 
    eprintf "%a@." report e;
    exit 1

(********************)
(* opening database *)
(********************)

let () = Db.init_base (fname ^ ".db")

let get_driver name = 
  let pi = Util.Mstr.find name config.provers in
  Why.Driver.load_driver env pi.Whyconf.driver

type prover_data =
    { prover : Db.prover;
      command : string;
      driver : Why.Driver.driver;
    }

let provers_data =
  printf "===============================@\nProvers: ";
  let l = 
    Util.Mstr.fold
    (fun id conf acc ->
       let name = conf.Whyconf.name in
       printf " %s, " name;
       { prover = Db.get_prover name;
         command = conf.Whyconf.command;
         driver = get_driver id; } :: acc
    ) config.provers []
  in
  printf "@\n===============================@.";
  l 
   
let () = 
  printf "previously known goals:@\n";
  List.iter (fun s -> printf "%s@\n" (Db.goal_task_checksum s)) (Db.root_goals ());
  printf "@."


(***************************)
(* Process input theories  *)
(* add corresponding tasks *)
(***************************)

let add_task (tname : string) (task : Why.Task.task) acc =
  let name = (Why.Task.task_goal task).Why.Decl.pr_name.Why.Ident.id_string in
  eprintf "doing task: tname=%s, name=%s@." tname name;
  Db.add_or_replace_task ~tname ~name task :: acc

let do_theory tname th glist =
  let tasks = Why.Task.split_theory th None in
  List.fold_right (add_task tname) tasks glist


(****************)
(* goals widget *)
(****************)

module Ide_goals = struct

  let cols = new GTree.column_list
MARCHE Claude's avatar
MARCHE Claude committed
201
  let name_column = cols#add Gobject.Data.string
MARCHE Claude's avatar
MARCHE Claude committed
202
  let id_column = cols#add Gobject.Data.caml
MARCHE Claude's avatar
MARCHE Claude committed
203 204 205

  let renderer = GTree.cell_renderer_text [`XALIGN 0.] 
  let vcolumn = 
MARCHE Claude's avatar
MARCHE Claude committed
206 207
    GTree.view_column ~title:"Theories/Goals" 
      ~renderer:(renderer, ["text", name_column]) () 
MARCHE Claude's avatar
MARCHE Claude committed
208 209 210 211 212 213 214 215 216 217 218

  let () = 
    vcolumn#set_resizable true;
    vcolumn#set_max_width 400

  let create ~packing () =
    let model = GTree.tree_store cols in
    let view = GTree.view ~model ~packing () in
    let _ = view#selection#set_mode `SINGLE in
    let _ = view#set_rules_hint true in
    ignore (view#append_column vcolumn);
MARCHE Claude's avatar
MARCHE Claude committed
219
    model,view
MARCHE Claude's avatar
MARCHE Claude committed
220 221 222

  let clear model = model#clear ()

MARCHE Claude's avatar
MARCHE Claude committed
223 224 225 226
  let task_table = Ident.Hid.create 17

  let get_task = Ident.Hid.find task_table

MARCHE Claude's avatar
MARCHE Claude committed
227
  let add_goals (model : GTree.tree_store) th =
MARCHE Claude's avatar
MARCHE Claude committed
228 229 230 231 232 233
    let tasks = Task.split_theory th None in
    List.iter
      (fun t->
         let name = (Task.task_goal t).Decl.pr_name in
         Ident.Hid.add task_table name t)
      tasks;
MARCHE Claude's avatar
MARCHE Claude committed
234
    let rec fill parent ns =
MARCHE Claude's avatar
MARCHE Claude committed
235
      let add_s s symb = 
MARCHE Claude's avatar
MARCHE Claude committed
236
	let row = model#append ~parent () in
MARCHE Claude's avatar
MARCHE Claude committed
237 238
	model#set ~row ~column:name_column s;
	model#set ~row ~column:id_column symb.Decl.pr_name;
MARCHE Claude's avatar
MARCHE Claude committed
239
      in
MARCHE Claude's avatar
MARCHE Claude committed
240
      Theory.Mnm.iter add_s ns.Theory.ns_pr;
MARCHE Claude's avatar
MARCHE Claude committed
241 242
    in
    let row = model#append () in
MARCHE Claude's avatar
MARCHE Claude committed
243
    model#set ~row ~column:name_column th.Theory.th_name.Ident.id_string;
MARCHE Claude's avatar
MARCHE Claude committed
244
    model#set ~row ~column:id_column th.Theory.th_name;
MARCHE Claude's avatar
MARCHE Claude committed
245 246 247 248
    fill row th.Theory.th_export

end

MARCHE Claude's avatar
MARCHE Claude committed
249
(****************)
MARCHE Claude's avatar
MARCHE Claude committed
250
(* windows, etc *)
MARCHE Claude's avatar
MARCHE Claude committed
251 252 253 254 255 256 257 258 259 260
(****************)

let move_to_line (v : GText.view) line = 
  if line <= v#buffer#line_count && line <> 0 then begin
    let it = v#buffer#get_iter (`LINE line) in 
    let mark = `MARK (v#buffer#create_mark it) in
    v#scroll_to_mark ~use_align:true ~yalign:0.5 mark
  end

(* to be run when a row in the tree view is selected *)
MARCHE Claude's avatar
MARCHE Claude committed
261
let select_goals (goals_view:GTree.tree_store) (task_view:GSourceView2.source_view) 
MARCHE Claude's avatar
MARCHE Claude committed
262 263 264 265
   (_source_view:GSourceView2.source_view) selected_rows = 
  List.iter
    (fun p ->
       let row = goals_view#get_iter p in
MARCHE Claude's avatar
MARCHE Claude committed
266 267 268 269 270 271 272 273
       let id = goals_view#get ~row ~column:Ide_goals.id_column in
       Format.eprintf "You clicked on %s@." id.Ident.id_string;
       let t = Ide_goals.get_task id in
       let task_text = Pp.string_of Pretty.print_task t in
       task_view#source_buffer#set_text task_text;
       task_view#scroll_to_mark `INSERT

(*
MARCHE Claude's avatar
MARCHE Claude committed
274 275 276 277
       match origin with
         | Ident.User (_loc,_) -> ()
             move_to_line source_view#as_view loc.Lexing.pos_lnum
         | _ -> ()
MARCHE Claude's avatar
MARCHE Claude committed
278
*)
MARCHE Claude's avatar
MARCHE Claude committed
279 280 281 282 283 284
    )
    selected_rows

let alt_ergo_on_all_goals () =
  begin
  end
MARCHE Claude's avatar
MARCHE Claude committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

let main () =
  let w = GWindow.window 
    ~allow_grow:true ~allow_shrink:true
    ~width:window_width ~height:window_height 
    ~title:"why-ide" ()
  in
  let _ = w#connect#destroy ~callback:(fun () -> exit 0) in
  let vbox = GPack.vbox ~homogeneous:false ~packing:w#add () in

  (* Menu *)
  let menubar = GMenu.menu_bar ~packing:vbox#pack () in
  let factory = new GMenu.factory menubar in
  let accel_group = factory#accel_group in
  let file_menu = factory#add_submenu "_File" in
  let file_factory = new GMenu.factory file_menu ~accel_group in
  let _ = 
    file_factory#add_image_item ~key:GdkKeysyms._Q ~label:"_Quit" 
      ~callback:(fun () -> exit 0) () 
  in
MARCHE Claude's avatar
MARCHE Claude committed
305 306 307 308 309 310
  let tools_menu = factory#add_submenu "_Tools" in
  let tools_factory = new GMenu.factory tools_menu ~accel_group in
  let _ = 
    tools_factory#add_image_item ~label:"Alt-Ergo on all goals" 
      ~callback:alt_ergo_on_all_goals () 
  in
MARCHE Claude's avatar
MARCHE Claude committed
311 312 313 314 315 316 317 318 319 320 321

  (* horizontal paned *)
  let hp = GPack.paned `HORIZONTAL  ~border_width:3 ~packing:vbox#add () in

  (* left tree of namespace *)
  let scrollview = 
    GBin.scrolled_window ~hpolicy:`AUTOMATIC ~vpolicy:`AUTOMATIC 
    ~width:(window_width / 3) ~packing:hp#add () 
  in
  let _ = scrollview#set_shadow_type `ETCHED_OUT in

MARCHE Claude's avatar
MARCHE Claude committed
322 323
  let goals_model,goals_view = Ide_goals.create ~packing:scrollview#add () in
  Theory.Mnm.iter (fun _ th -> Ide_goals.add_goals goals_model th) theories;
MARCHE Claude's avatar
MARCHE Claude committed
324

MARCHE Claude's avatar
MARCHE Claude committed
325 326 327 328
  (* horizontal paned *)
  let vp = GPack.paned `VERTICAL  ~border_width:3 ~packing:hp#add () in

  (* goal text view *)
MARCHE Claude's avatar
MARCHE Claude committed
329
  let scrolled_task_view = GBin.scrolled_window
MARCHE Claude's avatar
MARCHE Claude committed
330 331 332
    ~hpolicy: `AUTOMATIC ~vpolicy: `AUTOMATIC
    ~packing:vp#add ()
  in
MARCHE Claude's avatar
MARCHE Claude committed
333
  let task_view =
MARCHE Claude's avatar
MARCHE Claude committed
334 335 336
    GSourceView2.source_view
      ~editable:false
      ~show_line_numbers:true
MARCHE Claude's avatar
MARCHE Claude committed
337
      ~packing:scrolled_task_view#add ~height:500 ~width:650
MARCHE Claude's avatar
MARCHE Claude committed
338 339
      ()
  in
MARCHE Claude's avatar
MARCHE Claude committed
340 341
  task_view#source_buffer#set_language lang;
  task_view#set_highlight_current_line true;
MARCHE Claude's avatar
MARCHE Claude committed
342
  
MARCHE Claude's avatar
MARCHE Claude committed
343
  (* source view *)
MARCHE Claude's avatar
MARCHE Claude committed
344
  let scrolled_source_view = GBin.scrolled_window
MARCHE Claude's avatar
MARCHE Claude committed
345
    ~hpolicy: `AUTOMATIC ~vpolicy: `AUTOMATIC
MARCHE Claude's avatar
MARCHE Claude committed
346
    ~packing:vp#add ()
MARCHE Claude's avatar
MARCHE Claude committed
347 348 349 350 351 352 353 354
  in
  let source_view =
    GSourceView2.source_view
      ~auto_indent:true
      ~insert_spaces_instead_of_tabs:true ~tab_width:2
      ~show_line_numbers:true
      ~right_margin_position:80 ~show_right_margin:true
      (* ~smart_home_end:true *)
MARCHE Claude's avatar
MARCHE Claude committed
355
      ~packing:scrolled_source_view#add ~height:500 ~width:650
MARCHE Claude's avatar
MARCHE Claude committed
356 357
      ()
  in
MARCHE Claude's avatar
MARCHE Claude committed
358
(*
MARCHE Claude's avatar
MARCHE Claude committed
359
  source_view#misc#modify_font_by_name font_name;
MARCHE Claude's avatar
MARCHE Claude committed
360
*)
MARCHE Claude's avatar
MARCHE Claude committed
361 362 363
  source_view#source_buffer#set_language lang;
  source_view#set_highlight_current_line true;
  source_view#source_buffer#set_text source_text;
MARCHE Claude's avatar
MARCHE Claude committed
364 365 366 367 368 369

(* Bind event: row selection on tree view on the left *) 
  let _ =
    goals_view#selection#connect#after#changed ~callback:
      begin fun () ->
	let list = goals_view#selection#get_selected_rows in
MARCHE Claude's avatar
MARCHE Claude committed
370
        select_goals goals_model task_view source_view list
MARCHE Claude's avatar
MARCHE Claude committed
371 372 373
      end
  in

MARCHE Claude's avatar
MARCHE Claude committed
374 375 376 377 378 379 380 381 382
  w#add_accel_group accel_group;
  w#show ()

let () =
  main ();
  GtkThread.main ()

(*
Local Variables: 
MARCHE Claude's avatar
MARCHE Claude committed
383
compile-command: "unset LANG; make -C ../.. bin/why-rustprover.byte"
MARCHE Claude's avatar
MARCHE Claude committed
384 385
End: 
*)