compute compound scheme correctly
[sqlgg.git] / sql2cpp.ml
blob17db3497d5e0fdc3874f202738457c9e897bbb0c
1 (*
2 $Id$
3 *)
5 open Printf
6 open Operators
7 open ListMore
8 open ExtString
10 module L = List
11 module S = String
13 module T_SQL_parser =
14 struct
15 type token = Sql_parser.token
16 type result = RA.Scheme.t * Stmt.Raw.params
17 let rule = Sql_lexer.parse_rule
18 let input = Sql_parser.input
19 end
21 module P = Parser_utils.Make (T_SQL_parser)
23 let work filename =
24 match
25 (try Some (Std.input_file filename) with exn -> None)
26 with
27 | None -> Error.log "cannot open file : %s" filename
28 | Some s ->
29 let lexbuf = Lexing.from_string s in
30 let rec lines l =
31 match (try Sql_lexer.ruleStatement Props.empty lexbuf with exn -> None) with
32 | Some x -> lines (x::l)
33 | None -> l
35 let all = lines [] >> List.rev in
36 (* parse in direct order *)
37 let parse1 (stmt,props) =
38 try
39 print_endline stmt;
40 let (s,ps) = P.parse_buf_exn (Lexing.from_string stmt) in
41 RA.Scheme.print s;
42 print_endline (Show.show<Stmt.Raw.params>(ps))
43 with
44 | exn ->
45 begin
46 print_endline (Printexc.to_string exn)
47 end
49 List.iter parse1 all
51 let stmts =
52 List.map
53 (fun (str,props) -> match P.parse_string str with
54 | Some ((k,n,placeholders) as stmt) ->
55 (*Error.logs (Show.show<Stmt.Raw.parsed> stmt); *)
56 Some (k,n,placeholders,props,str)
57 | None -> Error.log "Failed to parse : %s" str; None)
58 all
59 >> List.filter_valid
61 Gen.process (Stmt.resolve stmts)
64 (* match P.parse_file filename with
65 | None -> print_endline "none"
66 | Some stmts ->
67 Error.logs (Show.show<Stmt.t list> stmts);
68 Gen.process stmts*)
70 let show_help () =
71 Error.log "SQL to C++ Code Generator Version %s (%s)" Version.version (Version.revision >> S.explode >> L.take 8 >> S.implode);
72 Error.log "";
73 Error.log " Usage: %s file_with_statements.sql" (Filename.basename Sys.executable_name);
74 Error.log "";
75 Error.log " Parse given file (treating content as SQL statements) and emit corresponding code to stdout"
77 let main () =
78 match Array.to_list Sys.argv with
79 | _::[file] -> work file
80 | _ -> show_help ()
82 let _ = Printexc.print main ()