@@ -32,26 +32,93 @@ object DataType {
32
32
def apiCall (defEndian : Option [FixedEndian ]): String
33
33
}
34
34
35
+ /** A generic number type. */
35
36
abstract sealed class NumericType extends DataType
37
+ /** A generic boolean type. */
36
38
abstract sealed class BooleanType extends DataType
37
39
40
+ /** A generic integer type. */
38
41
abstract sealed class IntType extends NumericType
42
+ /**
43
+ * An integer type that occupies undecided number of bytes in the stream.
44
+ *
45
+ * If it possible to determine, the more narrow `Int1Type` will be inferred
46
+ * for an expression instead of this type.
47
+ *
48
+ * Represents a type of the following constructions:
49
+ *
50
+ * - `sizeof<>` and `bitsizeof<>` built-in operators
51
+ * - `_sizeof` special property of fields and self object
52
+ * - `size` and `pos` properties of streams
53
+ * - `to_i` result when converting other types (strings, floats, enums, and booleans) to integers
54
+ * - `length` and `size` properties of arrays
55
+ * - `length` property of strings
56
+ * - `_index` context variable
57
+ */
39
58
case object CalcIntType extends IntType
59
+ /**
60
+ * An integer type that fit into the byte and therefore can represent element
61
+ * of the byte array.
62
+ *
63
+ * Parameters have this type when it's declared with `type: u1` or `type: s1`.
64
+ *
65
+ * Represents a type of the following constructions:
66
+ *
67
+ * - `Int1Type(true)` -- a constant in the [0..127] range
68
+ * - `Int1Type(false)` -- a constant in the [128..255] range
69
+ * - element type of byte arrays
70
+ * - `first`, `last`, `min`, and `max` properties of byte arrays
71
+ * - result of the `[]` operator of byte arrays
72
+ *
73
+ * @param signed Determines if most significant bit of number contains sign
74
+ */
40
75
case class Int1Type (signed : Boolean ) extends IntType with ReadableType {
41
76
override def apiCall (defEndian : Option [FixedEndian ]): String = if (signed) " s1" else " u1"
42
77
}
78
+ /**
79
+ * An integer type that occupies some predefined size in bytes in the stream.
80
+ *
81
+ * Parameters have this type when it's declared with `type: u<X>` or `type: s<X>`.
82
+ *
83
+ * @param signed Determines if most significant bit of number contains sign
84
+ * @param width Size of type in bytes
85
+ * @param endian Byte order used to represent the number
86
+ */
43
87
case class IntMultiType (signed : Boolean , width : IntWidth , endian : Option [FixedEndian ]) extends IntType with ReadableType {
44
88
override def apiCall (defEndian : Option [FixedEndian ]): String = {
45
89
val ch1 = if (signed) 's' else 'u'
46
90
val finalEnd = endian.orElse(defEndian)
47
91
s " $ch1${width.width}${finalEnd.map(_.toSuffix).getOrElse(" " )}"
48
92
}
49
93
}
94
+ /**
95
+ * A boolean type that occupies one bit in the stream.
96
+ *
97
+ * Parameters have this type when it's declared with `type: b1`.
98
+ */
50
99
case class BitsType1 (bitEndian : BitEndianness ) extends BooleanType
100
+ /**
101
+ * An integer number type that occupies some predefined size in _bits_ in the stream.
102
+ *
103
+ * Parameters have this type when it's declared with `type: bX`.
104
+ *
105
+ * @param width Size of type in bits
106
+ * @param bitEndian Bit order inside of byte used to represent the number
107
+ */
51
108
case class BitsType (width : Int , bitEndian : BitEndianness ) extends IntType
52
109
110
+ /** A generic floating-point number type. */
53
111
abstract sealed class FloatType extends NumericType
112
+ /** A floating-point number type that occupies undecided number of bytes in the stream. */
54
113
case object CalcFloatType extends FloatType
114
+ /**
115
+ * A floating-point number type that occupies some predefined size in bytes in the stream.
116
+ *
117
+ * Parameters have this type when it's declared with `type: fX`.
118
+ *
119
+ * @param width Size of type in bytes
120
+ * @param endian Byte order used to represent the number
121
+ */
55
122
case class FloatMultiType (width : IntWidth , endian : Option [FixedEndian ]) extends FloatType with ReadableType {
56
123
override def apiCall (defEndian : Option [FixedEndian ]): String = {
57
124
val finalEnd = endian.orElse(defEndian)
@@ -63,7 +130,13 @@ object DataType {
63
130
def process : Option [ProcessExpr ]
64
131
}
65
132
133
+ /** A generic raw bytes type. */
66
134
abstract sealed class BytesType extends DataType with Processing
135
+ /**
136
+ * A raw bytes type that occupies undecided number of bytes in the stream.
137
+ *
138
+ * Parameters have this type when it's declared with `type: bytes`.
139
+ */
67
140
case object CalcBytesType extends BytesType {
68
141
override def process = None
69
142
}
@@ -88,10 +161,27 @@ object DataType {
88
161
override val process : Option [ProcessExpr ]
89
162
) extends BytesType
90
163
164
+ /** A generic string type. */
91
165
abstract sealed class StrType extends DataType
166
+ /**
167
+ * A pure string type that occupies undecided number of bytes in the stream.
168
+ *
169
+ * Parameters have this type when it's declared with `type: str`.
170
+ */
92
171
case object CalcStrType extends StrType
172
+ /**
173
+ * A type that have the `str` and `strz` built-in Kaitai types.
174
+ *
175
+ * @param bytes An underlying bytes of the string
176
+ * @param encoding An encoding used to convert byte array into string
177
+ */
93
178
case class StrFromBytesType (bytes : BytesType , var encoding : String ) extends StrType
94
179
180
+ /**
181
+ * A boolean type that occupies undecided number of bytes in the stream.
182
+ *
183
+ * Parameters have this type when it's declared with `type: bool`.
184
+ */
95
185
case object CalcBooleanType extends BooleanType
96
186
97
187
/**
@@ -149,6 +239,7 @@ object DataType {
149
239
cs.isTopLevel || cs.meta.isOpaque
150
240
}
151
241
}
242
+ /** User type which isn't restricted in size (i.e. without `size`, `terminator`, or `size-eos`). */
152
243
case class UserTypeInstream (
153
244
_name : List [String ],
154
245
_forcedParent : Option [Ast .expr],
@@ -161,6 +252,7 @@ object DataType {
161
252
r
162
253
}
163
254
}
255
+ /** User type which is restricted in size either via `size`, `terminator`, or `size-eos`. */
164
256
case class UserTypeFromBytes (
165
257
_name : List [String ],
166
258
_forcedParent : Option [Ast .expr],
@@ -175,6 +267,12 @@ object DataType {
175
267
r
176
268
}
177
269
}
270
+ /**
271
+ * Reference to the user type which isn't restricted in size (i.e. without
272
+ * `size`, `terminator`, or `size-eos`).
273
+ *
274
+ * Parameters have this type when it's declared with any non-built-in type.
275
+ */
178
276
case class CalcUserType (
179
277
_name : List [String ],
180
278
_forcedParent : Option [Ast .expr],
@@ -183,6 +281,10 @@ object DataType {
183
281
) extends UserType (_name, _forcedParent, _args) {
184
282
override def isOwning = false
185
283
}
284
+ /**
285
+ * Reference to the user type which restricted in size either via `size`,
286
+ * `terminator`, or `size-eos`.
287
+ */
186
288
case class CalcUserTypeFromBytes (
187
289
_name : List [String ],
188
290
_forcedParent : Option [Ast .expr],
@@ -194,30 +296,75 @@ object DataType {
194
296
override def isOwning = false
195
297
}
196
298
299
+ /**
300
+ * A generic collection type.
301
+ *
302
+ * @param elType Type of elements in the collection
303
+ */
197
304
abstract sealed class ArrayType (val elType : DataType ) extends ComplexDataType
198
-
305
+ /**
306
+ * An owned slice of array type. This type is used for holding data in attributes
307
+ * with `repeat` key. Number of elements in that type is unknown
308
+ *
309
+ * @param _elType Type of elements in the slice
310
+ */
199
311
case class ArrayTypeInStream (_elType : DataType ) extends ArrayType (_elType) {
200
312
override def isOwning : Boolean = true
201
313
override def asNonOwning (isOwningInExpr : Boolean = false ): CalcArrayType = CalcArrayType (elType, isOwningInExpr)
202
314
}
315
+ /**
316
+ * A borrowed slice of an array. This type is used when array is passed as a
317
+ * parameter to the user type (parameter have type `bytes` or haven't any
318
+ * explicitly defined type). Number of elements in that type is unknown
319
+ *
320
+ * @param _elType Type of elements in the slice
321
+ */
203
322
case class CalcArrayType (_elType : DataType , override val isOwningInExpr : Boolean = false ) extends ArrayType (_elType) {
204
323
override def isOwning : Boolean = false
205
324
}
206
325
207
326
val USER_TYPE_NO_PARENT = Ast .expr.Bool (false )
208
327
328
+ /**
329
+ * A very generic type that can hold any other type. Used when type of expression
330
+ * is completely unknown to the compiler.
331
+ *
332
+ * Parameters have this type when it's declared with `type: any`.
333
+ */
209
334
case object AnyType extends DataType
335
+
336
+ /**
337
+ * A type that can hold any Kaitai generated type. Can be used as common ancestor
338
+ * of `switch-on` types, when all alternative types is owned.
339
+ */
210
340
case object KaitaiStructType extends StructType {
211
341
def isOwning = true
212
342
override def asNonOwning (isOwningInExpr : Boolean = false ): DataType = CalcKaitaiStructType (isOwningInExpr)
213
343
}
344
+ /**
345
+ * A type that can hold any Kaitai generated type. Can be used as common ancestor
346
+ * of `switch-on` types, when at least one of the alternative types is borrowed.
347
+ *
348
+ * Parameters have this type when it's declared with `type: struct`.
349
+ */
214
350
case class CalcKaitaiStructType (override val isOwningInExpr : Boolean = false ) extends StructType {
215
351
def isOwning = false
216
352
}
353
+ /**
354
+ * A type that hold and own Kaitai stream object. This type is used when a new IO object
355
+ * is allocated (i.e. when new sub-stream is created for types with `size`, `terminator`,
356
+ * or `size-eos: true` attributes).
357
+ */
217
358
case object OwnedKaitaiStreamType extends ComplexDataType {
218
359
def isOwning = true
219
360
override def asNonOwning (isOwningInExpr : Boolean = false ): DataType = KaitaiStreamType
220
361
}
362
+ /**
363
+ * A type that hold and borrow Kaitai stream object. This type is used
364
+ * when an IO object is passed as parameter to the user type.
365
+ *
366
+ * Parameters have this type when it's declared with `type: io`.
367
+ */
221
368
case object KaitaiStreamType extends ComplexDataType {
222
369
def isOwning = false
223
370
}
@@ -400,6 +547,8 @@ object DataType {
400
547
StrFromBytesType (bat, enc)
401
548
case _ =>
402
549
val typeWithArgs = Expressions .parseTypeRef(dt)
550
+ // if `size`, `terminator` and `size-eos: true` isn't defined,
551
+ // user type uses parent stream, otherwise creates an own stream
403
552
if (arg.size.isEmpty && ! arg.sizeEos && arg.terminator.isEmpty) {
404
553
if (arg.process.isDefined)
405
554
throw KSYParseError (s " user type ' $dt': need 'size' / 'size-eos' / 'terminator' if 'process' is used " , path).toException
0 commit comments