wip
[sqlgg.git] / sql2cpp.ml
blobb3cf27141292c711d05ad76ed1116b2168e33a08
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 let work filename =
14 match
15 (try Some (Std.input_file filename) with exn -> None)
16 with
17 | None -> Error.log "cannot open file : %s" filename
18 | Some s ->
19 let lexbuf = Lexing.from_string s in
20 let rec lines l =
21 match (try Sql_lexer.ruleStatement Props.empty lexbuf with exn -> None) with
22 | Some x -> lines (x::l)
23 | None -> l
25 let all = lines [] >> List.rev in
26 (* parse in direct order *)
27 let parse1 (stmt,props) =
28 try
29 print_endline stmt;
30 let (s,ps) = Parser.parse_stmt stmt in
31 RA.Scheme.print s;
32 print_endline (Show.show<Stmt.Raw.params>(ps))
33 with
34 | exn ->
35 begin
36 print_endline (Printexc.to_string exn)
37 end
39 List.iter parse1 all
41 let stmts =
42 List.map
43 (fun (str,props) -> match P.parse_string str with
44 | Some ((k,n,placeholders) as stmt) ->
45 (*Error.logs (Show.show<Stmt.Raw.parsed> stmt); *)
46 Some (k,n,placeholders,props,str)
47 | None -> Error.log "Failed to parse : %s" str; None)
48 all
49 >> List.filter_valid
51 Gen.process (Stmt.resolve stmts)
54 (* match P.parse_file filename with
55 | None -> print_endline "none"
56 | Some stmts ->
57 Error.logs (Show.show<Stmt.t list> stmts);
58 Gen.process stmts*)
60 let show_help () =
61 Error.log "SQL to C++ Code Generator Version %s (%s)" Version.version (Version.revision >> S.explode >> L.take 8 >> S.implode);
62 Error.log "";
63 Error.log " Usage: %s file_with_statements.sql" (Filename.basename Sys.executable_name);
64 Error.log "";
65 Error.log " Parse given file (treating content as SQL statements) and emit corresponding code to stdout"
67 let main () =
68 match Array.to_list Sys.argv with
69 | _::"-test"::_ -> Test.run ()
70 | _::[file] -> work file
71 | _ -> show_help ()
73 let _ = Printexc.print main ()