在 Kaitai Struct 中引用外部文件中的枚举

Referencing enums in external file in Kaitai Struct

我想要一个 Kaitai Struct ksy 文件,它引用了不同外部文件中的一些枚举和类型。在外部文件中,我希望只有子类型和枚举定义。

这是引用外部类型的测试文件(test.ksy):

meta:
  id: test
  imports:
    - rowing
  endian: be

seq:
  - id: system_id
    type: u1
  - id: data
    type:
      switch-on: system_id
      cases:
        rowing_messages::position: rowing::rowing_position_message

这是包含外部类型的文件(rowing.ksy):

meta:
  id: rowing
  endian: be

enums:
  rowing_messages:
    0x10: position
    0x12: meter_per_stroke

types:
  rowing_position_message:
    seq:
      - id: id
        type: u1
      - id: timestamp
        type: u4

编译器抱怨:

test: /seq/1/type/cases/EnumByLabel(identifier(rowing_messages),identifier(position)): unable to find enum 'rowing_messages', searching from test

根据我的测试,我似乎可以引用带有前缀 rowing:: 的外部 rowing_position_message 类型,但我不能对枚举做同样的事情。如果我像 rowing::rowing_messages::position 那样做,编译器会抱怨:

/seq/1/cases/rowing::rowing_messages::position: parsing expression 'rowing::rowing_messages::position' failed on 1:24, expected "or" | CharsWhile(Set( , n)) | "\\n" | End

提前感谢您的任何想法。

前置 rowing:: 是这里的正确行为,因为除此之外,编译器无法引用在外部 class 中声明的枚举。

此外,您需要通过添加

system_id 声明为枚举,而不仅仅是整数
enum: 'rowing::rowing_messages'

此问题已在相对现代的编译器中得到解决(即 >0.8,任何现代 0.9 不稳定快照都应该有效),所以这应该有效:

  - id: system_id
    type: u1
    enum: 'rowing::rowing_messages'
  - id: data
    type:
      switch-on: system_id
      cases:
        'rowing::rowing_messages::position': 'rowing::rowing_position_message'

对于 .to_i 枚举的另一个可能选项(理论上,您宁愿在实践中不使用它),请在 https://github.com/kaitai-io/kaitai_struct/issues/643.[=16= 中查看更深入的解释]