From: Richard W.M. Jones Date: Mon, 16 Jun 2008 20:29:24 +0000 (+0000) Subject: Added computed offset field X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=4f8971025e9431049a97c260fa586fe64bde22d2;p=ocaml-bitstring.git Added computed offset field --- diff --git a/bitmatch_persistent.ml b/bitmatch_persistent.ml index 8a2cbdf..41ea0c9 100644 --- a/bitmatch_persistent.ml +++ b/bitmatch_persistent.ml @@ -38,6 +38,7 @@ type 'a field = { signed : bool; (* true if signed, false if unsigned *) t : field_type; (* type *) _loc : Loc.t; (* location in source code *) + offset : expr option; (* offset expression *) (* Turn the field into a string. This used to be a function, * but that would prevent this structure from being marshalled. @@ -158,6 +159,7 @@ let create_pattern_field _loc = t = Int; _loc = _loc; printer = PattPrinter; + offset = None; } let set_lident_patt field id = @@ -184,6 +186,11 @@ let set_type_int field = { field with t = Int } let set_type_string field = { field with t = String } let set_type_bitstring field = { field with t = Bitstring } let set_location field loc = { field with _loc = loc } +let set_offset_int field i = + let _loc = field._loc in + { field with offset = Some <:expr< $`int:i$ >> } +let set_offset field expr = { field with offset = Some expr } +let set_no_offset field = { field with offset = None } let create_constructor_field _loc = { @@ -194,6 +201,7 @@ let create_constructor_field _loc = t = Int; _loc = _loc; printer = ExprPrinter; + offset = None; } let set_lident_expr field id = @@ -216,3 +224,4 @@ let get_endian field = field.endian let get_signed field = field.signed let get_type field = field.t let get_location field = field._loc +let get_offset field = field.offset diff --git a/bitmatch_persistent.mli b/bitmatch_persistent.mli index d2057c6..9da0f93 100644 --- a/bitmatch_persistent.mli +++ b/bitmatch_persistent.mli @@ -329,7 +329,7 @@ val create_pattern_field : loc_t -> patt field The pattern is unbound, the type is set to [int], bit length to [32], endianness to [BigEndian], signedness to unsigned ([false]), - and source code location to the [_loc] parameter. + source code location to the [_loc] parameter, and no offset expression. To create a complete field you need to call the [set_*] functions. For example, to create [{ len : 8 : int }] @@ -422,6 +422,23 @@ val set_location : 'a field -> loc_t -> 'a field (** Sets the source code location of a field. This is used when pa_bitmatch displays error messages. *) +val set_offset_int : 'a field -> int -> 'a field +(** Set the offset expression for a field to the given number. + + The effect is that the field [{ _ : 8 : offset(160) }] could + be created by calling [set_offset_int field 160]. *) + +val set_offset : 'a field -> expr -> 'a field +(** Set the offset expression for a field to the given expression. + + The effect is that the field [{ _ : 8 : offset(160) }] could + be created by calling [set_offset_int field <:expr< 160 >>]. *) + +val set_no_offset : 'a field -> 'a field +(** Remove the offset expression from a field. The field will + follow the previous field, or if it is the first field will + be at offset zero. *) + (** {3 Create constructor fields} These fields are used in constructors ([BITSTRING]). *) @@ -485,3 +502,6 @@ val get_type : 'a field -> field_type val get_location : 'a field -> loc_t (** Get the source code location of a field. *) + +val get_offset : 'a field -> expr option +(** Get the offset expression of a field, or [None] if there is none. *)