Skip to content

Commit

Permalink
add string_to_<cenum> functions to match the <cenum>_to_string versions.
Browse files Browse the repository at this point in the history
This helps with command-line parsing of cenums
  • Loading branch information
avsm committed Sep 29, 2012
1 parent fcc8195 commit bbee1f0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGES
@@ -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) ===== ===== 0.5.0 (20-Sep-2012) =====


* Add a signature generator for cstruct and cenum to permit * Add a signature generator for cstruct and cenum to permit
Expand Down
1 change: 1 addition & 0 deletions unix/lib_test/enum.ml
Expand Up @@ -51,4 +51,5 @@ let _ =
assert(foo32_to_int THREE32 = 1l); assert(foo32_to_int THREE32 = 1l);
assert(int_to_foo32 3l = Some (TWO32)); assert(int_to_foo32 3l = Some (TWO32));
assert(int_to_foo32 1l = Some (THREE32)); assert(int_to_foo32 1l = Some (THREE32));
assert(string_to_foo16 "ONE16" = Some ONE16);
print_endline (foo8_to_string ONE8) print_endline (foo8_to_string ONE8)
10 changes: 8 additions & 2 deletions unix/syntax/pa_cstruct.ml
Expand Up @@ -241,14 +241,18 @@ let output_enum _loc name fields width =
) fields) in ) fields) in
let printers = mcOr_of_list (List.map (fun (f,_) -> let printers = mcOr_of_list (List.map (fun (f,_) ->
<:match_case< $uid:f$ -> $str:f$ >>) fields) in <: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 getter x = sprintf "int_to_%s" x in
let setter x = sprintf "%s_to_int" x in let setter x = sprintf "%s_to_int" x in
let printer x = sprintf "%s_to_string" x in let printer x = sprintf "%s_to_string" x in
let parse x = sprintf "string_to_%s" x in
<:str_item< <:str_item<
type $lid:name$ = [ $decls$ ] ; type $lid:name$ = [ $decls$ ] ;
value $lid:getter name$ x = match x with [ $getters$ ] ; value $lid:getter name$ x = match x with [ $getters$ ] ;
value $lid:setter name$ x = match x with [ $setters$ ] ; 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 = let output_enum_sig _loc name fields width =
Expand All @@ -264,13 +268,15 @@ let output_enum_sig _loc name fields width =
let getter x = sprintf "int_to_%s" x in let getter x = sprintf "int_to_%s" x in
let setter x = sprintf "%s_to_int" x in let setter x = sprintf "%s_to_int" x in
let printer x = sprintf "%s_to_string" 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 ctyo = <:ctyp< option $lid:name$ >> in
let cty = <:ctyp< $lid:name$ >> in let cty = <:ctyp< $lid:name$ >> in
<:sig_item< <:sig_item<
type $lid:name$ = [ $decls$ ] ; type $lid:name$ = [ $decls$ ] ;
value $lid:getter name$ : $oty$ -> $ctyo$ ; value $lid:getter name$ : $oty$ -> $ctyo$ ;
value $lid:setter name$ : $cty$ -> $oty$ ; 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 EXTEND Gram
Expand Down

0 comments on commit bbee1f0

Please sign in to comment.