java - Parsing opcodes with Kaitai Struct -


doing first steps in kaitai struct, i've been trying bson parser excercise. .ksy code parses bson element looks that:

  element:     seq:       - id: el_type         type: u1         enum: bson_type       - id: el_name         type: strz         encoding: utf-8         if: el_type != bson_type::end_of_document       - id: el_string         type: bson_string         if: el_type == bson_type::string       - id: el_document         type: bson_document         if: el_type == bson_type::document       - id: el_boolean         type: u1         if: el_type == bson_type::boolean       - id: el_int32         type: s4         if: el_type == bson_type::int32       - id: el_int64         type: s4         if: el_type == bson_type::int64 enums:   bson_type:     0: end_of_document     1: double     2: string     3: document     8: boolean     0x10: int32     0x12: int64 

as might have noticed, there's lots of repetition. 1 have go duplicate if block every time 1 wants additional element type. worse, have duplicate stuff 3 times in every such field, i.e.:

  - id: el_string                    # <= string!     type: bson_string                # <= string!     if: el_type == bson_type::string # <= string! 

my target language java. before kaitai, i've tried preon, , there had clauses such as:

@choices(prefixsize = 8, alternatives = {     @choice(condition = "prefix==0x01", type = floatnamedelement.class),     @choice(condition = "prefix==0x02", type = utf8namedelement.class) } private namedelement elements; 

there automatically these 2 elements based on value of "prefix". possible in kaitai?

well, you're right, , feature requested 3 or 4 times ;) i've filed issue that.

i can't agree on preon's implementation, though, seems limited me. can have single "prefix", integer, , must preceed point of choice.

i want implement little more generic switch style statement, that:

  - id: value     switch: code     cases:       string:         type: bson_string       document:         type: bson_document       boolean:         type: u1       int32:         type: s4       int64:         type: s8 

what think of that?

note chances won't "proper" oop object hierarchy, though, you're getting preon. because preon's classes made hand , can common superclass , inherit both floatnamedelement , utf8namedelement it, can't think of method in current ks model now.


Comments