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.04k stars 199 forks source link

Absolute paths in `enum` and `type` frequently handled incorrectly #1135

Open Mingun opened 1 month ago

Mingun commented 1 month ago

Reference to enum that includes absolute path (like ::path::to::enum) will be parsed to DataType.EnumType(List(,path,to,enum)) (note the first empty string in EnumType.name)

Creation of EnumType instance: https://github.com/kaitai-io/kaitai_struct_compiler/blob/542b24124a95f5308df625d667f8776dd023c696/shared/src/main/scala/io/kaitai/struct/datatype/DataType.scala#L486-L497

The same will happen with the CalcUserType.name when type is defined in a parameter with absolute path:

params:
  - id: param1
    type: ::absolute::path::to::type
  - id: param2
    type: u1
    enum: ::absolute::path::to::enum

https://github.com/kaitai-io/kaitai_struct_compiler/blob/542b24124a95f5308df625d667f8776dd023c696/shared/src/main/scala/io/kaitai/struct/datatype/DataType.scala#L549

In many cases in the generators such paths will produce uncompilable code, for example, in Java and C# the generated type name will begin with dot: .Absolute.Path.To.Type and .Absolute.Path.To.Enum: Language Path
Java https://github.com/kaitai-io/kaitai_struct_compiler/blob/542b24124a95f5308df625d667f8776dd023c696/shared/src/main/scala/io/kaitai/struct/languages/JavaCompiler.scala#L920-L921
C# https://github.com/kaitai-io/kaitai_struct_compiler/blob/542b24124a95f5308df625d667f8776dd023c696/shared/src/main/scala/io/kaitai/struct/languages/CSharpCompiler.scala#L690-L691

I think, that sign of absolute path should be present in types explicitly instead of implicitly as empty first component of a name.