From 95cf4e44055ccab34cddeba340e16446afbf0e28 Mon Sep 17 00:00:00 2001 From: ygrek Date: Thu, 28 May 2009 11:11:47 +0300 Subject: [PATCH] cmdline option -name --- gen.ml | 18 +++++++++--------- gen_caml.ml | 8 ++++---- gen_cxx.ml | 6 +++--- gen_java.ml | 6 +++--- gen_xml.ml | 4 ++-- sqlgg.ml | 10 ++++++---- 6 files changed, 27 insertions(+), 25 deletions(-) diff --git a/gen.ml b/gen.ml index baf6de0..dbc46b3 100644 --- a/gen.ml +++ b/gen.ml @@ -6,7 +6,7 @@ open ExtString open Operators open Stmt -let (inc_indent,dec_indent,make_indent) = +let (inc_indent,dec_indent,make_indent) = let v = ref 0 in (fun () -> v := !v + 2), (fun () -> v := !v - 2), @@ -18,14 +18,14 @@ let indent_endline s = print_indent (); print_endline s let output fmt = kprintf indent_endline fmt let print fmt = kprintf print_endline fmt -let name_of attr index = +let name_of attr index = match attr.RA.name with | "" -> sprintf "_%u" index | s -> s let param_name_to_string id index = - match id with - | Next -> sprintf "_%u" index + match id with + | Next -> sprintf "_%u" index | Numbered x -> sprintf "_%u" x | Named s -> s @@ -53,8 +53,8 @@ module type Lang = sig type t val generate_code : t -> int -> RA.Scheme.t -> Stmt.params -> Stmt.kind -> Props.t -> unit val start : unit -> t - val start_output : t -> unit - val finish_output : t -> unit + val start_output : t -> string -> unit + val finish_output : t -> string -> unit val comment : t -> ('a,unit,string,unit) format4 -> 'a val empty_line : t -> unit end @@ -78,12 +78,12 @@ let generate_header code = S.comment code "generated by sqlgg %s" Config.version; S.empty_line code -let process stmts = +let process name stmts = let out = S.start () in generate_header out; - S.start_output out; + S.start_output out name; List.iteri (generate_code out) stmts; - S.finish_output out + S.finish_output out name end diff --git a/gen_caml.ml b/gen_caml.ml index 3f9ce5d..f47b067 100644 --- a/gen_caml.ml +++ b/gen_caml.ml @@ -100,12 +100,12 @@ let generate_code () index scheme params kind props = dec_indent (); empty_line () -let start_output () = - output "module Sqlgg (T : Sqlgg_traits.M) = struct"; +let start_output () name = + output "module %s (T : Sqlgg_traits.M) = struct" (String.capitalize name); empty_line (); inc_indent () -let finish_output () = +let finish_output () name = dec_indent (); - output "end (* module Sqlgg *)" + output "end (* module %s *)" (String.capitalize name) diff --git a/gen_cxx.ml b/gen_cxx.ml index f284e9d..7e29cd0 100644 --- a/gen_cxx.ml +++ b/gen_cxx.ml @@ -176,11 +176,11 @@ let generate_code () index schema params kind props = close_curly ""; empty_line () -let start_output () = +let start_output () name = output "#pragma once"; empty_line (); output "template "; - start_struct "sqlgg" + start_struct name -let finish_output () = end_struct "sqlgg" +let finish_output () name = end_struct name diff --git a/gen_java.ml b/gen_java.ml index 5878207..f2db4ca 100644 --- a/gen_java.ml +++ b/gen_java.ml @@ -138,10 +138,10 @@ let generate_code () index schema params kind props = G.close_curly ""; empty_line () -let start_output () = +let start_output () name = output "import java.sql.*;"; empty_line (); - start_class "demo_java_gen" + start_class name -let finish_output () = end_class "demo_java_gen" +let finish_output () name = end_class name diff --git a/gen_xml.ml b/gen_xml.ml index b524bf6..339f084 100644 --- a/gen_xml.ml +++ b/gen_xml.ml @@ -68,9 +68,9 @@ let generate_code (x,_) index scheme params kind props = let sql = get_sql props kind params in x := Node ("stmt",["name",name; "sql",sql;],[input; output]) :: !x -let start_output (x,pre) = pre := !x; x := [] +let start_output (x,pre) _ = pre := !x; x := [] -let finish_output (x,pre) = +let finish_output (x,pre) _ = List.iter (fun z -> z >> xml_to_string >> print_endline) (List.rev !pre); Node ("sqlgg",[],List.rev !x) >> xml_to_string >> print_endline; x := []; diff --git a/sqlgg.ml b/sqlgg.ml index f411313..6c9082b 100644 --- a/sqlgg.ml +++ b/sqlgg.ml @@ -9,7 +9,7 @@ module Xml_gen = Gen.Make(Gen_xml) module Java = Gen.Make(Gen_java) let generate = ref Cxx.process -(* let name = ref None *) +let name = ref "sqlgg" let set_out s = generate := @@ -20,8 +20,10 @@ let set_out s = | "java" -> Java.process | _ -> failwith (sprintf "Unknown output language: %s" s) +let set_name s = name := s + let work = - let f s = s >> Main.parse_sql >> !generate in + let f s = s >> Main.parse_sql >> !generate !name in function | "-" -> f (Std.input_all stdin) | filename -> Main.with_file filename f @@ -39,9 +41,9 @@ let main () = [ "-version", Arg.Unit show_version, " Show version"; "-gen", Arg.String set_out, "cxx|caml|java|xml Set output language"; - "-test", Arg.Unit Test.run, " Run unit tests"; -(* "-name", Arg.String set_name, "identifier Set output class name"; *) + "-name", Arg.String set_name, " Set output module name"; "-", Arg.Unit (fun () -> work "-"), " Read sql from stdin"; + "-test", Arg.Unit Test.run, " Run unit tests"; ] in Arg.parse (Arg.align args) work usage_msg -- 2.11.4.GIT