Fix connection close.
[wrappi.git] / generator-macros / pa_wrap.ml
index c8e8af8..bfe28fb 100644 (file)
@@ -18,6 +18,7 @@
 
 (* For general information about camlp4, see:
  * http://brion.inria.fr/gallium/index.php/Camlp4
+ *
  * For information about quotations, see:
  * http://brion.inria.fr/gallium/index.php/Quotation
  *)
@@ -28,15 +29,98 @@ open Ast
 
 open Wrappi_types
 
-let add_entry_point _loc name parameters return_type code =
-  (* XXX *)
-  <:str_item< >>
+(* Convert a plain list into a list AST. *)
+let rec expr_of_list _loc = function
+  | [] -> <:expr< [] >>
+  | h::t -> let t = expr_of_list _loc t in <:expr< $h$ :: $t$ >>
+
+(* Convert an optional type into an option AST. *)
+let expr_of_option _loc = function
+  | None -> <:expr< None >>
+  | Some x -> <:expr< Some $x$ >>
+
+(* Convert a _loc to an AST. *)
+let expr_of_loc _loc loc =
+  let file_name,
+    start_line, start_bol, start_off,
+    stop_line, stop_bol, stop_off,
+    ghost = Loc.to_tuple loc in
+  <:expr< Camlp4.PreCast.Loc.of_tuple
+    ($str:file_name$,
+     $`int:start_line$, $`int:start_bol$, $`int:start_off$,
+     $`int:stop_line$, $`int:stop_bol$, $`int:stop_off$,
+     $`bool:ghost$) >>
+
+let add_entry_point _loc local name parameters rtype code includes =
+  let loc = expr_of_loc _loc _loc in
+
+  let local =
+    match local with None -> <:expr< false >> | _ -> <:expr< true >> in
+
+  let parameters = List.map (
+    fun (name, t) -> <:expr< ($str:name$, $t$, None) >>
+  ) parameters in
+  let parameters = expr_of_list _loc parameters in
+
+  let code = expr_of_option _loc code in
+
+  let includes = match includes with None -> <:expr< [] >> | Some xs -> xs in
+
+  <:str_item<
+    let ep = { Wrappi_types.ep_loc = $loc$;
+               ep_local = $local$;
+               ep_name = $str:name$;
+               ep_ftype = ($rtype$, $parameters$, []);
+               ep_code = $code$;
+               ep_includes = $includes$ } in
+    Wrappi_accumulator.add_entry_point ep
+  >>
+
+let add_typedef _loc name t =
+  let loc = expr_of_loc _loc _loc in
+
+  <:str_item<
+    let td = { Wrappi_types.td_loc = $loc$;
+               td_name = $str:name$;
+               td_type = $t$ } in
+    Wrappi_accumulator.add_typedef td
+  >>
+
+let add_enum _loc name identifiers =
+  let loc = expr_of_loc _loc _loc in
+
+  <:str_item<
+    let en = { Wrappi_types.en_loc = $loc$;
+               en_name = $str:name$;
+               en_identifiers = Array.of_list $identifiers$ } in
+    Wrappi_accumulator.add_enum en
+  >>
+
+let add_struct _loc name fields =
+  let loc = expr_of_loc _loc _loc in
+
+  let fields = List.map (
+    fun (name, t) -> <:expr< ($str:name$, $t$) >>
+  ) fields in
+  let fields = expr_of_list _loc fields in
+
+  <:str_item<
+    let sd = { Wrappi_types.sd_loc = $loc$;
+               sd_name = $str:name$;
+               sd_fields = Array.of_list $fields$ } in
+    Wrappi_accumulator.add_struct sd
+  >>
 
 let () =
   (* Quotation expander for C code. *)
   let c_quotation_expander _loc _ code =
+    let loc = expr_of_loc _loc _loc in
+
     (* XXX Expand %- or $- expressions in code. *)
-    ExStr (_loc, code)
+    (* XXX Escape >> in code. *)
+
+    <:expr< { Wrappi_types.cc_loc = $loc$;
+              cc_code = $str:code$ } >>
   in
   Quotation.add "c" Quotation.DynAst.expr_tag c_quotation_expander;
 
@@ -50,27 +134,61 @@ EXTEND Gram
   GLOBAL: str_item;
 
   (* A parameter or return type. *)
-  any_type: [
-    [ "int32" -> TInt32 ]
-  | [ "int64" -> TInt64 ]
-  | [ t = LIDENT -> Type t ]
+  ptype: [
+    [ "bool" -> <:expr< Wrappi_types.TBool >> ]
+  | [ "buffer" -> <:expr< Wrappi_types.TBuffer >> ]
+  | [ "enum"; name = LIDENT -> <:expr< Wrappi_types.TEnum $str:name$ >> ]
+  | [ "file" -> <:expr< Wrappi_types.TFile >> ]
+  | [ "hash"; "("; t = ptype; ")" -> <:expr< Wrappi_types.THash $t$ >> ]
+  | [ "int" -> <:expr< Wrappi_types.TInt >> ]
+  | [ "int32" -> <:expr< Wrappi_types.TInt32 >> ]
+  | [ "int64" -> <:expr< Wrappi_types.TInt64 >> ]
+  | [ "list"; "("; t = ptype; ")" -> <:expr< Wrappi_types.TList $t$ >> ]
+  | [ "nullable"; "("; t = ptype; ")" -> <:expr< Wrappi_types.TNullable $t$ >> ]
+  | [ "string" -> <:expr< Wrappi_types.TString >> ]
+  | [ "struct"; name = LIDENT -> <:expr< Wrappi_types.TStruct $str:name$ >> ]
+  | [ "uint32" -> <:expr< Wrappi_types.TUInt32 >> ]
+  | [ "uint64" -> <:expr< Wrappi_types.TUInt64 >> ]
+  | [ "union"; name = LIDENT -> <:expr< Wrappi_types.TUnion $str:name$ >> ]
+  | [ name = LIDENT -> <:expr< Wrappi_types.TTypedef $str:name$ >> ]
   ];
 
   (* A return type. *)
-  return_type: [
-    [ "err" -> RErr ]
-  | [ t = any_type -> Return t ]
+  rtype: [
+    [ "void" -> <:expr< Wrappi_types.RVoid >> ]
+  | [ "static_string" -> <:expr< Wrappi_types.RStaticString >> ]
+  | [ t = ptype -> <:expr< Wrappi_types.Return $t$ >> ]
   ];
 
-  (* A single function parameter. *)
-  parameter: [[ t = any_type; name = LIDENT -> (t, name) ]];
+  (* A single function parameter.  XXX Preconditions. *)
+  parameter: [[ t = ptype; name = LIDENT -> (name, t) ]];
+
+  (* A single struct field.  XXX Preconditions. *)
+  struct_field: [[ t = ptype; name = LIDENT -> (name, t) ]];
 
   str_item: LEVEL "top" [
     [ "entry_point";
-      return_type = return_type; name = LIDENT;
+      local = OPT "local";
+      rtype = rtype; name = LIDENT;
       "("; parameters = LIST0 parameter SEP ","; ")";
-      code = OPT [ code = expr -> code ] ->
-      add_entry_point _loc name parameters return_type code
+      code = OPT [ code = expr -> code ];
+      includes = OPT [ "includes"; includes = expr -> includes ]
+      ->
+      add_entry_point _loc local name parameters rtype code includes
+    ]
+
+  | [ "enum"; name = LIDENT; identifiers = expr ->
+      add_enum _loc name identifiers
+    ]
+
+  | [ "struct"; name = LIDENT; "{";
+      fields = LIST0 struct_field SEP ";";
+      "}" ->
+      add_struct _loc name fields
+    ]
+
+  | [ "typedef"; t = ptype; name = LIDENT ->
+      add_typedef _loc name t
     ]
   ];