在 Kaitai Struct 中迈出第一步,我一直在尝试将 BSON 解析器作为练习。我解析 BSON 元素的 .ksy 代码现在看起来像这样:
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
您可能已经注意到,有很多重复。if
每当一个人想要做额外的元素类型时,就需要去重复块。更糟糕的是,您基本上必须在每个此类字段中复制 3 次内容,即:
- id: el_string # <= string!
type: bson_string # <= string!
if: el_type == bson_type::string # <= string!
我的目标语言是Java。在 Kaitai 之前,我只尝试过 Preon,我们有这样的子句:
@Choices(prefixSize = 8, alternatives = {
@Choice(condition = "prefix==0x01", type = FloatNamedElement.class),
@Choice(condition = "prefix==0x02", type = UTF8NamedElement.class)
}
private NamedElement elements;
在那里,您会根据“前缀”的值自动获取这两个元素。在开泰可以吗?