todo -1
[sqlgg.git] / main.ml
blob80655f318cb6cdfb17c7864ed9b8cf1838624b3a
1 (*
2 Main
3 *)
5 open Printf
6 open Operators
7 open ListMore
8 open ExtString
9 open Apply
11 module L = List
12 module S = String
14 let repeat f x k =
15 let rec loop () =
16 match f x with
17 | Some z -> k z
18 | None -> ()
20 loop ()
22 let parse_one (stmt,props) =
23 try
24 (* print_endline stmt; *)
25 Some ((Parser.parse_stmt stmt), Props.set props "sql" stmt)
26 with
27 | exn ->
28 begin
29 Error.log "==> %s" stmt;
30 None
31 end
33 let show_one ((s,p),props) =
34 RA.Scheme.print s;
35 print_endline (Stmt.params_to_string p)
37 let get_statements ch =
38 let lexbuf = Lexing.from_channel ch in
39 let f () = try Sql_lexer.ruleStatement Props.empty lexbuf with exn -> None in
40 let rec next () =
41 match f () with
42 | None -> raise Enum.No_more_elements
43 | Some s ->
44 begin match parse_one s with
45 | None -> next ()
46 | Some stmt -> stmt
47 end
49 Enum.from next
51 let with_file filename f =
52 match catch Std.input_file filename with
53 | None -> Error.log "cannot open file : %s" filename
54 | Some s -> f s
56 let with_channel filename f =
57 match catch open_in filename with
58 | None -> Error.log "cannot open file : %s" filename
59 | Some ch -> Std.finally (fun () -> close_in_noerr ch) f ch