Add structs.
[wrappi.git] / generator-macros / pa_wrap.ml
index a31497a..bfe28fb 100644 (file)
@@ -96,6 +96,21 @@ let add_enum _loc name identifiers =
     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 =
@@ -148,6 +163,9 @@ EXTEND Gram
   (* 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";
       local = OPT "local";
@@ -163,6 +181,12 @@ EXTEND Gram
       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
     ]