@@ -35,26 +35,93 @@ object DataType {
35
35
def apiCall (defEndian : Option [FixedEndian ]): String
36
36
}
37
37
38
+ /** A generic number type. */
38
39
abstract sealed class NumericType extends DataType
40
+ /** A generic boolean type. */
39
41
abstract sealed class BooleanType extends DataType
40
42
43
+ /** A generic integer type. */
41
44
abstract sealed class IntType extends NumericType
45
+ /**
46
+ * An integer type that occupies undecided number of bytes in the stream.
47
+ *
48
+ * If it possible to determine, the more narrow `Int1Type` will be inferred
49
+ * for an expression instead of this type.
50
+ *
51
+ * Represents a type of the following constructions:
52
+ *
53
+ * - `sizeof<>` and `bitsizeof<>` built-in operators
54
+ * - `_sizeof` special property of fields and self object
55
+ * - `size` and `pos` properties of streams
56
+ * - `to_i` result when converting other types (strings, floats, enums, and booleans) to integers
57
+ * - `length` and `size` properties of arrays
58
+ * - `length` property of strings
59
+ * - `_index` context variable
60
+ */
42
61
case object CalcIntType extends IntType
62
+ /**
63
+ * An integer type that fit into the byte and therefore can represent element
64
+ * of the byte array.
65
+ *
66
+ * Parameters have this type when it's declared with `type: u1` or `type: s1`.
67
+ *
68
+ * Represents a type of the following constructions:
69
+ *
70
+ * - `Int1Type(true)` -- a constant in the [0..127] range
71
+ * - `Int1Type(false)` -- a constant in the [128..255] range
72
+ * - element type of byte arrays
73
+ * - `first`, `last`, `min`, and `max` properties of byte arrays
74
+ * - result of the `[]` operator of byte arrays
75
+ *
76
+ * @param signed Determines if most significant bit of number contains sign
77
+ */
43
78
case class Int1Type (signed : Boolean ) extends IntType with ReadableType {
44
79
override def apiCall (defEndian : Option [FixedEndian ]): String = if (signed) " s1" else " u1"
45
80
}
81
+ /**
82
+ * An integer type that occupies some predefined size in bytes in the stream.
83
+ *
84
+ * Parameters have this type when it's declared with `type: u<X>` or `type: s<X>`.
85
+ *
86
+ * @param signed Determines if most significant bit of number contains sign
87
+ * @param width Size of type in bytes
88
+ * @param endian Byte order used to represent the number
89
+ */
46
90
case class IntMultiType (signed : Boolean , width : IntWidth , endian : Option [FixedEndian ]) extends IntType with ReadableType {
47
91
override def apiCall (defEndian : Option [FixedEndian ]): String = {
48
92
val ch1 = if (signed) 's' else 'u'
49
93
val finalEnd = endian.orElse(defEndian)
50
94
s " $ch1${width.width}${finalEnd.map(_.toSuffix).getOrElse(" " )}"
51
95
}
52
96
}
97
+ /**
98
+ * A boolean type that occupies one bit in the stream.
99
+ *
100
+ * Parameters have this type when it's declared with `type: b1`.
101
+ */
53
102
case class BitsType1 (bitEndian : BitEndianness ) extends BooleanType
103
+ /**
104
+ * An integer number type that occupies some predefined size in _bits_ in the stream.
105
+ *
106
+ * Parameters have this type when it's declared with `type: bX`.
107
+ *
108
+ * @param width Size of type in bits
109
+ * @param bitEndian Bit order inside of byte used to represent the number
110
+ */
54
111
case class BitsType (width : Int , bitEndian : BitEndianness ) extends IntType
55
112
113
+ /** A generic floating-point number type. */
56
114
abstract sealed class FloatType extends NumericType
115
+ /** A floating-point number type that occupies undecided number of bytes in the stream. */
57
116
case object CalcFloatType extends FloatType
117
+ /**
118
+ * A floating-point number type that occupies some predefined size in bytes in the stream.
119
+ *
120
+ * Parameters have this type when it's declared with `type: fX`.
121
+ *
122
+ * @param width Size of type in bytes
123
+ * @param endian Byte order used to represent the number
124
+ */
58
125
case class FloatMultiType (width : IntWidth , endian : Option [FixedEndian ]) extends FloatType with ReadableType {
59
126
override def apiCall (defEndian : Option [FixedEndian ]): String = {
60
127
val finalEnd = endian.orElse(defEndian)
@@ -66,7 +133,13 @@ object DataType {
66
133
def process : Option [ProcessExpr ]
67
134
}
68
135
136
+ /** A generic raw bytes type. */
69
137
abstract sealed class BytesType extends DataType with Processing
138
+ /**
139
+ * A raw bytes type that occupies undecided number of bytes in the stream.
140
+ *
141
+ * Parameters have this type when it's declared with `type: bytes`.
142
+ */
70
143
case object CalcBytesType extends BytesType {
71
144
override def process = None
72
145
}
@@ -91,7 +164,13 @@ object DataType {
91
164
override val process : Option [ProcessExpr ]
92
165
) extends BytesType
93
166
167
+ /** A generic string type. */
94
168
abstract sealed class StrType extends DataType
169
+ /**
170
+ * A pure string type that occupies undecided number of bytes in the stream.
171
+ *
172
+ * Parameters have this type when it's declared with `type: str`.
173
+ */
95
174
case object CalcStrType extends StrType
96
175
/**
97
176
* A type that have the `str` and `strz` built-in Kaitai types.
@@ -112,6 +191,11 @@ object DataType {
112
191
isEncodingDerived : Boolean ,
113
192
) extends StrType
114
193
194
+ /**
195
+ * A boolean type that occupies undecided number of bytes in the stream.
196
+ *
197
+ * Parameters have this type when it's declared with `type: bool`.
198
+ */
115
199
case object CalcBooleanType extends BooleanType
116
200
117
201
/**
@@ -178,6 +262,7 @@ object DataType {
178
262
cs.meta.isOpaque
179
263
}
180
264
}
265
+ /** User type which isn't restricted in size (i.e. without `size`, `terminator`, or `size-eos`). */
181
266
case class UserTypeInstream (
182
267
_name : List [String ],
183
268
_forcedParent : Option [Ast .expr],
@@ -190,6 +275,7 @@ object DataType {
190
275
r
191
276
}
192
277
}
278
+ /** User type which is restricted in size either via `size`, `terminator`, or `size-eos`. */
193
279
case class UserTypeFromBytes (
194
280
_name : List [String ],
195
281
_forcedParent : Option [Ast .expr],
@@ -204,6 +290,12 @@ object DataType {
204
290
r
205
291
}
206
292
}
293
+ /**
294
+ * Reference to the user type which isn't restricted in size (i.e. without
295
+ * `size`, `terminator`, or `size-eos`).
296
+ *
297
+ * Parameters have this type when it's declared with any non-built-in type.
298
+ */
207
299
case class CalcUserType (
208
300
_name : List [String ],
209
301
_forcedParent : Option [Ast .expr],
@@ -212,6 +304,10 @@ object DataType {
212
304
) extends UserType (_name, _forcedParent, _args) {
213
305
override def isOwning = false
214
306
}
307
+ /**
308
+ * Reference to the user type which restricted in size either via `size`,
309
+ * `terminator`, or `size-eos`.
310
+ */
215
311
case class CalcUserTypeFromBytes (
216
312
_name : List [String ],
217
313
_forcedParent : Option [Ast .expr],
@@ -223,31 +319,76 @@ object DataType {
223
319
override def isOwning = false
224
320
}
225
321
322
+ /**
323
+ * A generic collection type.
324
+ *
325
+ * @param elType Type of elements in the collection
326
+ */
226
327
abstract sealed class ArrayType (val elType : DataType ) extends ComplexDataType
227
-
328
+ /**
329
+ * An owned slice of array type. This type is used for holding data in attributes
330
+ * with `repeat` key. Number of elements in that type is unknown
331
+ *
332
+ * @param _elType Type of elements in the slice
333
+ */
228
334
case class ArrayTypeInStream (_elType : DataType ) extends ArrayType (_elType) {
229
335
override def isOwning : Boolean = true
230
336
override def asNonOwning (isOwningInExpr : Boolean = false ): CalcArrayType = CalcArrayType (elType, isOwningInExpr)
231
337
}
338
+ /**
339
+ * A borrowed slice of an array. This type is used when array is passed as a
340
+ * parameter to the user type (parameter have type `bytes` or haven't any
341
+ * explicitly defined type). Number of elements in that type is unknown
342
+ *
343
+ * @param _elType Type of elements in the slice
344
+ */
232
345
case class CalcArrayType (_elType : DataType , override val isOwningInExpr : Boolean = false ) extends ArrayType (_elType) {
233
346
override def isOwning : Boolean = false
234
347
}
235
348
236
349
/** Represents `_parent: false` expression which means that type explicitly has no parent. */
237
350
val USER_TYPE_NO_PARENT = Ast .expr.Bool (false )
238
351
352
+ /**
353
+ * A very generic type that can hold any other type. Used when type of expression
354
+ * is completely unknown to the compiler.
355
+ *
356
+ * Parameters have this type when it's declared with `type: any`.
357
+ */
239
358
case object AnyType extends DataType
359
+
360
+ /**
361
+ * A type that can hold any Kaitai generated type. Can be used as common ancestor
362
+ * of `switch-on` types, when all alternative types is owned.
363
+ */
240
364
case object KaitaiStructType extends StructType {
241
365
def isOwning = true
242
366
override def asNonOwning (isOwningInExpr : Boolean = false ): DataType = CalcKaitaiStructType (isOwningInExpr)
243
367
}
368
+ /**
369
+ * A type that can hold any Kaitai generated type. Can be used as common ancestor
370
+ * of `switch-on` types, when at least one of the alternative types is borrowed.
371
+ *
372
+ * Parameters have this type when it's declared with `type: struct`.
373
+ */
244
374
case class CalcKaitaiStructType (override val isOwningInExpr : Boolean = false ) extends StructType {
245
375
def isOwning = false
246
376
}
377
+ /**
378
+ * A type that hold and own Kaitai stream object. This type is used when a new IO object
379
+ * is allocated (i.e. when new sub-stream is created for types with `size`, `terminator`,
380
+ * or `size-eos: true` attributes).
381
+ */
247
382
case object OwnedKaitaiStreamType extends ComplexDataType {
248
383
def isOwning = true
249
384
override def asNonOwning (isOwningInExpr : Boolean = false ): DataType = KaitaiStreamType
250
385
}
386
+ /**
387
+ * A type that hold and borrow Kaitai stream object. This type is used
388
+ * when an IO object is passed as parameter to the user type.
389
+ *
390
+ * Parameters have this type when it's declared with `type: io`.
391
+ */
251
392
case object KaitaiStreamType extends ComplexDataType {
252
393
def isOwning = false
253
394
}
@@ -459,6 +600,8 @@ object DataType {
459
600
StrFromBytesType (bat, enc, arg.encoding.isEmpty)
460
601
case _ =>
461
602
val typeWithArgs = Expressions .parseTypeRef(dt)
603
+ // if `size`, `terminator` and `size-eos: true` isn't defined,
604
+ // user type uses parent stream, otherwise creates an own stream
462
605
if (arg.size.isEmpty && ! arg.sizeEos && arg.terminator.isEmpty) {
463
606
if (arg.process.isDefined)
464
607
throw KSYParseError (s " user type ' $dt': need 'size' / 'size-eos' / 'terminator' if 'process' is used " , path).toException
0 commit comments