Permalink
Browse files

add string_to_<cenum> functions to match the <cenum>_to_string versions.

This helps with command-line parsing of cenums
  • Loading branch information...
avsm committed Sep 29, 2012
1 parent fcc8195 commit bbee1f0980275f6678815b73fca6a67ab00522f4
Showing with 14 additions and 2 deletions.
  1. +5 −0 CHANGES
  2. +1 −0 unix/lib_test/enum.ml
  3. +8 −2 unix/syntax/pa_cstruct.ml
View
@@ -1,3 +1,8 @@
+===== 0.5.1 (28-Sep-2012) =====
+
+* Add string_to_<cenum> function to match the <cenum>_to_string,
+ primarily to help with command-line parsing of enum arguments.
+
===== 0.5.0 (20-Sep-2012) =====
* Add a signature generator for cstruct and cenum to permit
View
@@ -51,4 +51,5 @@ let _ =
assert(foo32_to_int THREE32 = 1l);
assert(int_to_foo32 3l = Some (TWO32));
assert(int_to_foo32 1l = Some (THREE32));
+ assert(string_to_foo16 "ONE16" = Some ONE16);
print_endline (foo8_to_string ONE8)
View
@@ -241,14 +241,18 @@ let output_enum _loc name fields width =
) fields) in
let printers = mcOr_of_list (List.map (fun (f,_) ->
<:match_case< $uid:f$ -> $str:f$ >>) fields) in
+ let parsers = mcOr_of_list (List.map (fun (f,_) ->
+ <:match_case< $str:f$ -> Some $uid:f$ >>) fields) in
let getter x = sprintf "int_to_%s" x in
let setter x = sprintf "%s_to_int" x in
let printer x = sprintf "%s_to_string" x in
+ let parse x = sprintf "string_to_%s" x in
<:str_item<
type $lid:name$ = [ $decls$ ] ;
value $lid:getter name$ x = match x with [ $getters$ ] ;
value $lid:setter name$ x = match x with [ $setters$ ] ;
- value $lid:printer name$ x= match x with [ $printers$ ] ;
+ value $lid:printer name$ x = match x with [ $printers$ ] ;
+ value $lid:parse name$ x = match x with [ $parsers$ | _ -> None ] ;
>>
let output_enum_sig _loc name fields width =
@@ -264,13 +268,15 @@ let output_enum_sig _loc name fields width =
let getter x = sprintf "int_to_%s" x in
let setter x = sprintf "%s_to_int" x in
let printer x = sprintf "%s_to_string" x in
+ let parse x = sprintf "string_to_%s" x in
let ctyo = <:ctyp< option $lid:name$ >> in
let cty = <:ctyp< $lid:name$ >> in
<:sig_item<
type $lid:name$ = [ $decls$ ] ;
value $lid:getter name$ : $oty$ -> $ctyo$ ;
value $lid:setter name$ : $cty$ -> $oty$ ;
- value $lid:printer name$ : $cty$ -> string
+ value $lid:printer name$ : $cty$ -> string ;
+ value $lid:parse name$ : string -> option $cty$ ;
>>
EXTEND Gram

0 comments on commit bbee1f0

Please sign in to comment.