kaitai-io / kaitai_struct

Kaitai Struct: declarative language to generate binary data parsers in C++ / C# / Go / Java / JavaScript / Lua / Nim / Perl / PHP / Python / Ruby
https://kaitai.io
4.02k stars 197 forks source link

Parent type inference works incorrectly when _parent is overridden with "parent:" #459

Open KOLANICH opened 6 years ago

KOLANICH commented 6 years ago
meta:
  id: cntk_snippet
  file-extension: model
  endian: le
  encoding: UTF-16le
  title: a piece of Microsoft CNTK format to test KS
  license: MIT
seq:
  - id: network
    type: section_hardcode("CN")
types:
  begin_marker_hardcode:
    params:
      - id: name
        type: str
    seq:
      - id: begin
        contents: ["B", 0]
      - id: marker
        contents: [name]
  end_marker:
    params:
      - id: name
        type: str
    seq:
      - id: end
        contents: ["E", 0]
      - id: marker
        contents: [name]
  section_hardcode:
    params:
      - id: name
        type: str
    seq:
      - id: begin_marker
        type: begin_marker_hardcode(name)
      - id: contents
        type: section_contents(begin_marker.name)
      - id: end_marker
        type: end_marker(begin_marker.name)
  section_contents:
    params:
      - id: section_name
        type: str
    seq:
      - id: contents
        parent: _parent._parent
        #<computation_network> -> node_count
        #<computation_network> (self._parent._parent) -> node_list <section_hardcode> (self._parent) -> contents <section_contents> (self) -> contents <node_list> -> nodes
        type:
          switch-on: section_name
          cases:
            "'CN'": computation_network
            "'NodeList'": node_list
    types:
      computation_network:
        seq:
          - id: node_count
            type: u8
          - id: node_list
            type: section_hardcode("NodeList")
      node_list:
        seq:
          - id: nodes
            type: u1
            repeat: expr
            #repeat-expr: "_parent.as<computation_network>.node_count"
            repeat-expr: "_parent.node_count" # /types/section_contents/types/node_list/seq/0/repeat-expr: unable to access 'node_count' in cntk_snippet::section_contents context
GreyCat commented 6 years ago

I'm not the mind-reader, and "I see no good way to implement it" is pretty vague. What's the problem, exactly?

Judging from the original title, I might guess that you're having problems with _parent type inferring. By any chance, parent type enforcing would help?

KOLANICH commented 6 years ago

Thank you, haven't known about this feature, I'll try that.

KOLANICH commented 6 years ago

I have tried this feature. 1 there is still prob,ems with tyoe inference 2 they can be worked around with as<>, and the generated code works fine.