Skip to content

Commit 3fc4af9

Browse files
committed
Document DataTypes
1 parent 6942bd2 commit 3fc4af9

File tree

1 file changed

+116
-1
lines changed

1 file changed

+116
-1
lines changed

shared/src/main/scala/io/kaitai/struct/datatype/DataType.scala

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,69 @@ object DataType {
3131
def apiCall(defEndian: Option[FixedEndian]): String
3232
}
3333

34+
/** A generic number type. */
3435
abstract sealed class NumericType extends DataType
36+
/** A generic boolean type. */
3537
abstract sealed class BooleanType extends DataType
3638

39+
/** A generic integer type. */
3740
abstract sealed class IntType extends NumericType
41+
/** An integer type that occupies undecided number of bytes in the stream. */
3842
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+
*/
3950
case class Int1Type(signed: Boolean) extends IntType with ReadableType {
4051
override def apiCall(defEndian: Option[FixedEndian]): String = if (signed) "s1" else "u1"
4152
}
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+
*/
4262
case class IntMultiType(signed: Boolean, width: IntWidth, endian: Option[FixedEndian]) extends IntType with ReadableType {
4363
override def apiCall(defEndian: Option[FixedEndian]): String = {
4464
val ch1 = if (signed) 's' else 'u'
4565
val finalEnd = endian.orElse(defEndian)
4666
s"$ch1${width.width}${finalEnd.map(_.toSuffix).getOrElse("")}"
4767
}
4868
}
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+
*/
4974
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+
*/
5083
case class BitsType(width: Int, bitEndian: BitEndianness) extends IntType
5184

85+
/** A generic floating-point number type. */
5286
abstract sealed class FloatType extends NumericType
87+
/** A floating-point number type that occupies undecided number of bytes in the stream. */
5388
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+
*/
5497
case class FloatMultiType(width: IntWidth, endian: Option[FixedEndian]) extends FloatType with ReadableType {
5598
override def apiCall(defEndian: Option[FixedEndian]): String = {
5699
val finalEnd = endian.orElse(defEndian)
@@ -62,7 +105,13 @@ object DataType {
62105
def process: Option[ProcessExpr]
63106
}
64107

108+
/** A generic raw bytes type. */
65109
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+
*/
66115
case object BorrowedBytesType extends BytesType {
67116
override def process = None
68117
}
@@ -87,10 +136,27 @@ object DataType {
87136
override val process: Option[ProcessExpr]
88137
) extends BytesType
89138

139+
/** A generic string type. */
90140
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+
*/
91146
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+
*/
92153
case class StrFromBytesType(bytes: BytesType, encoding: String) extends StrType
93154

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+
*/
94160
case object CalcBooleanType extends BooleanType
95161

96162
/**
@@ -132,6 +198,7 @@ object DataType {
132198
cs.isTopLevel || cs.meta.isOpaque
133199
}
134200
}
201+
/** User type which isn't restricted in size (i.e. without `size`, `terminator`, or `size-eos`). */
135202
case class OwnedUserType(
136203
_name: List[String],
137204
_forcedParent: Option[Ast.expr],
@@ -144,6 +211,7 @@ object DataType {
144211
r
145212
}
146213
}
214+
/** User type which is restricted in size either via `size`, `terminator`, or `size-eos`. */
147215
case class OwnedUserTypeFromBytes(
148216
_name: List[String],
149217
_forcedParent: Option[Ast.expr],
@@ -175,30 +243,75 @@ object DataType {
175243
override def isOwning = false
176244
}
177245

246+
/**
247+
* A generic collection type.
248+
*
249+
* @param elType Type of elements in the collection
250+
*/
178251
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+
*/
180258
case class OwnedSliceType(_elType: DataType) extends ArrayType(_elType) {
181259
override def isOwning: Boolean = true
182260
override def asNonOwning: BorrowedSliceType = BorrowedSliceType(elType)
183261
}
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+
*/
184269
case class BorrowedSliceType(_elType: DataType) extends ArrayType(_elType) {
185270
override def isOwning: Boolean = false
186271
}
187272

188273
val USER_TYPE_NO_PARENT = Ast.expr.Bool(false)
189274

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+
*/
190281
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+
*/
191287
case object OwnedKaitaiStructType extends StructType {
192288
def isOwning = true
193289
override def asNonOwning: DataType = BorrowedKaitaiStructType
194290
}
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+
*/
195297
case object BorrowedKaitaiStructType extends StructType {
196298
def isOwning = false
197299
}
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+
*/
198305
case object OwnedKaitaiStreamType extends ComplexDataType {
199306
def isOwning = true
200307
override def asNonOwning: DataType = BorrowedKaitaiStreamType
201308
}
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+
*/
202315
case object BorrowedKaitaiStreamType extends ComplexDataType {
203316
def isOwning = false
204317
}
@@ -381,6 +494,8 @@ object DataType {
381494
case _ => (dt, List())
382495
}
383496
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
384499
if (arg.size.isEmpty && !arg.sizeEos && arg.terminator.isEmpty) {
385500
if (arg.process.isDefined)
386501
throw new YAMLParseException(s"user type '$dt': need 'size' / 'size-eos' / 'terminator' if 'process' is used", path)

0 commit comments

Comments
 (0)