@@ -31,26 +31,69 @@ object DataType {
31
31
def apiCall (defEndian : Option [FixedEndian ]): String
32
32
}
33
33
34
+ /** A generic number type. */
34
35
abstract sealed class NumericType extends DataType
36
+ /** A generic boolean type. */
35
37
abstract sealed class BooleanType extends DataType
36
38
39
+ /** A generic integer type. */
37
40
abstract sealed class IntType extends NumericType
41
+ /** An integer type that occupies undecided number of bytes in the stream. */
38
42
case object CalcIntType extends IntType
43
+ /**
44
+ * An integer type that fit into the byte and therefore can represent element of byte array.
45
+ *
46
+ * Parameters have this type when it's declared with `type: u1` or `type: s1`.
47
+ *
48
+ * @param signed Determines if most significant bit of number contains sign
49
+ */
39
50
case class Int1Type (signed : Boolean ) extends IntType with ReadableType {
40
51
override def apiCall (defEndian : Option [FixedEndian ]): String = if (signed) " s1" else " u1"
41
52
}
53
+ /**
54
+ * An integer type that occupies some predefined size in bytes in the stream.
55
+ *
56
+ * Parameters have this type when it's declared with `type: u<X>` or `type: s<X>`.
57
+ *
58
+ * @param signed Determines if most significant bit of number contains sign
59
+ * @param width Size of type in bytes
60
+ * @param endian Byte order used to represent the number
61
+ */
42
62
case class IntMultiType (signed : Boolean , width : IntWidth , endian : Option [FixedEndian ]) extends IntType with ReadableType {
43
63
override def apiCall (defEndian : Option [FixedEndian ]): String = {
44
64
val ch1 = if (signed) 's' else 'u'
45
65
val finalEnd = endian.orElse(defEndian)
46
66
s " $ch1${width.width}${finalEnd.map(_.toSuffix).getOrElse(" " )}"
47
67
}
48
68
}
69
+ /**
70
+ * A boolean type that occupies one bit in the stream.
71
+ *
72
+ * Parameters have this type when it's declared with `type: b1`.
73
+ */
49
74
case class BitsType1 (bitEndian : BitEndianness ) extends BooleanType
75
+ /**
76
+ * An integer number type that occupies some predefined size in _bits_ in the stream.
77
+ *
78
+ * Parameters have this type when it's declared with `type: b<X>`.
79
+ *
80
+ * @param width Size of type in bits
81
+ * @param bitEndian Bit order inside of byte used to represent the number
82
+ */
50
83
case class BitsType (width : Int , bitEndian : BitEndianness ) extends IntType
51
84
85
+ /** A generic floating-point number type. */
52
86
abstract sealed class FloatType extends NumericType
87
+ /** A floating-point number type that occupies undecided number of bytes in the stream. */
53
88
case object CalcFloatType extends FloatType
89
+ /**
90
+ * A floating-point number type that occupies some predefined size in bytes in the stream.
91
+ *
92
+ * Parameters have this type when it's declared with `type: f<X>`.
93
+ *
94
+ * @param width Size of type in bytes
95
+ * @param endian Byte order used to represent the number
96
+ */
54
97
case class FloatMultiType (width : IntWidth , endian : Option [FixedEndian ]) extends FloatType with ReadableType {
55
98
override def apiCall (defEndian : Option [FixedEndian ]): String = {
56
99
val finalEnd = endian.orElse(defEndian)
@@ -62,7 +105,13 @@ object DataType {
62
105
def process : Option [ProcessExpr ]
63
106
}
64
107
108
+ /** A generic raw bytes type. */
65
109
abstract sealed class BytesType extends DataType with Processing
110
+ /**
111
+ * A raw bytes type that occupies undecided number of bytes in the stream.
112
+ *
113
+ * Parameters have this type when it's declared with `type: bytes`.
114
+ */
66
115
case object BorrowedBytesType extends BytesType {
67
116
override def process = None
68
117
}
@@ -87,10 +136,27 @@ object DataType {
87
136
override val process : Option [ProcessExpr ]
88
137
) extends BytesType
89
138
139
+ /** A generic string type. */
90
140
abstract sealed class StrType extends DataType
141
+ /**
142
+ * A pure string type that occupies undecided number of bytes in the stream.
143
+ *
144
+ * Parameters have this type when it's declared with `type: str`.
145
+ */
91
146
case object CalcStrType extends StrType
147
+ /**
148
+ * A type that have the `str` and `strz` built-in Kaitai types.
149
+ *
150
+ * @param bytes An underlying bytes of the string
151
+ * @param encoding An encoding used to convert byte array into string
152
+ */
92
153
case class StrFromBytesType (bytes : BytesType , encoding : String ) extends StrType
93
154
155
+ /**
156
+ * A boolean type that occupies undecided number of bytes in the stream.
157
+ *
158
+ * Parameters have this type when it's declared with `type: bool`.
159
+ */
94
160
case object CalcBooleanType extends BooleanType
95
161
96
162
/**
@@ -132,6 +198,7 @@ object DataType {
132
198
cs.isTopLevel || cs.meta.isOpaque
133
199
}
134
200
}
201
+ /** User type which isn't restricted in size (i.e. without `size`, `terminator`, or `size-eos`). */
135
202
case class OwnedUserType (
136
203
_name : List [String ],
137
204
_forcedParent : Option [Ast .expr],
@@ -144,6 +211,7 @@ object DataType {
144
211
r
145
212
}
146
213
}
214
+ /** User type which is restricted in size either via `size`, `terminator`, or `size-eos`. */
147
215
case class OwnedUserTypeFromBytes (
148
216
_name : List [String ],
149
217
_forcedParent : Option [Ast .expr],
@@ -175,30 +243,75 @@ object DataType {
175
243
override def isOwning = false
176
244
}
177
245
246
+ /**
247
+ * A generic collection type.
248
+ *
249
+ * @param elType Type of elements in the collection
250
+ */
178
251
abstract sealed class ArrayType (val elType : DataType ) extends ComplexDataType
179
-
252
+ /**
253
+ * An owned slice of array type. This type is used for holding data in attributes
254
+ * with `repeat` key. Number of elements in that type is unknown
255
+ *
256
+ * @param _elType Type of elements in the slice
257
+ */
180
258
case class OwnedSliceType (_elType : DataType ) extends ArrayType (_elType) {
181
259
override def isOwning : Boolean = true
182
260
override def asNonOwning : BorrowedSliceType = BorrowedSliceType (elType)
183
261
}
262
+ /**
263
+ * A borrowed slice of an array. This type is used when array is passed as a
264
+ * parameter to the user type (parameter have type `bytes` or haven't any
265
+ * explicitly defined type). Number of elements in that type is unknown
266
+ *
267
+ * @param _elType Type of elements in the slice
268
+ */
184
269
case class BorrowedSliceType (_elType : DataType ) extends ArrayType (_elType) {
185
270
override def isOwning : Boolean = false
186
271
}
187
272
188
273
val USER_TYPE_NO_PARENT = Ast .expr.Bool (false )
189
274
275
+ /**
276
+ * A very generic type that can hold any other type. Used when type of expression
277
+ * is completely unknown to the compiler.
278
+ *
279
+ * Parameters have this type when it's declared with `type: any`.
280
+ */
190
281
case object AnyType extends DataType
282
+
283
+ /**
284
+ * A type that can hold any Kaitai generated type. Can be used as common ancestor
285
+ * of `switch-on` types, when all alternative types is owned.
286
+ */
191
287
case object OwnedKaitaiStructType extends StructType {
192
288
def isOwning = true
193
289
override def asNonOwning : DataType = BorrowedKaitaiStructType
194
290
}
291
+ /**
292
+ * A type that can hold any Kaitai generated type. Can be used as common ancestor
293
+ * of `switch-on` types, when at least one of the alternative types is borrowed.
294
+ *
295
+ * Parameters have this type when it's declared with `type: struct`.
296
+ */
195
297
case object BorrowedKaitaiStructType extends StructType {
196
298
def isOwning = false
197
299
}
300
+ /**
301
+ * A type that hold and own Kaitai stream object. This type is used when a new IO object
302
+ * is allocated (i.e. when new sub-stream is created for types with `size`, `terminator`,
303
+ * or `size-eos: true` attributes).
304
+ */
198
305
case object OwnedKaitaiStreamType extends ComplexDataType {
199
306
def isOwning = true
200
307
override def asNonOwning : DataType = BorrowedKaitaiStreamType
201
308
}
309
+ /**
310
+ * A type that hold and borrow Kaitai stream object. This type is used
311
+ * when an IO object is passed as parameter to the user type.
312
+ *
313
+ * Parameters have this type when it's declared with `type: io`.
314
+ */
202
315
case object BorrowedKaitaiStreamType extends ComplexDataType {
203
316
def isOwning = false
204
317
}
@@ -381,6 +494,8 @@ object DataType {
381
494
case _ => (dt, List ())
382
495
}
383
496
val dtl = classNameToList(arglessType)
497
+ // if `size`, `terminator` and `size-eos: true` isn't defined,
498
+ // user type uses parent stream, otherwise creates own stream
384
499
if (arg.size.isEmpty && ! arg.sizeEos && arg.terminator.isEmpty) {
385
500
if (arg.process.isDefined)
386
501
throw new YAMLParseException (s " user type ' $dt': need 'size' / 'size-eos' / 'terminator' if 'process' is used " , path)
0 commit comments