Skip to content

Commit d9ed41c

Browse files
committed
Document DataTypes
1 parent c254d37 commit d9ed41c

File tree

1 file changed

+150
-1
lines changed

1 file changed

+150
-1
lines changed

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

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

35+
/** A generic number type. */
3536
abstract sealed class NumericType extends DataType
37+
/** A generic boolean type. */
3638
abstract sealed class BooleanType extends DataType
3739

40+
/** A generic integer type. */
3841
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+
*/
3958
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+
*/
4075
case class Int1Type(signed: Boolean) extends IntType with ReadableType {
4176
override def apiCall(defEndian: Option[FixedEndian]): String = if (signed) "s1" else "u1"
4277
}
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+
*/
4387
case class IntMultiType(signed: Boolean, width: IntWidth, endian: Option[FixedEndian]) extends IntType with ReadableType {
4488
override def apiCall(defEndian: Option[FixedEndian]): String = {
4589
val ch1 = if (signed) 's' else 'u'
4690
val finalEnd = endian.orElse(defEndian)
4791
s"$ch1${width.width}${finalEnd.map(_.toSuffix).getOrElse("")}"
4892
}
4993
}
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+
*/
5099
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+
*/
51108
case class BitsType(width: Int, bitEndian: BitEndianness) extends IntType
52109

110+
/** A generic floating-point number type. */
53111
abstract sealed class FloatType extends NumericType
112+
/** A floating-point number type that occupies undecided number of bytes in the stream. */
54113
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+
*/
55122
case class FloatMultiType(width: IntWidth, endian: Option[FixedEndian]) extends FloatType with ReadableType {
56123
override def apiCall(defEndian: Option[FixedEndian]): String = {
57124
val finalEnd = endian.orElse(defEndian)
@@ -63,7 +130,13 @@ object DataType {
63130
def process: Option[ProcessExpr]
64131
}
65132

133+
/** A generic raw bytes type. */
66134
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+
*/
67140
case object CalcBytesType extends BytesType {
68141
override def process = None
69142
}
@@ -88,10 +161,27 @@ object DataType {
88161
override val process: Option[ProcessExpr]
89162
) extends BytesType
90163

164+
/** A generic string type. */
91165
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+
*/
92171
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+
*/
93178
case class StrFromBytesType(bytes: BytesType, var encoding: String) extends StrType
94179

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

97187
/**
@@ -149,6 +239,7 @@ object DataType {
149239
cs.isTopLevel || cs.meta.isOpaque
150240
}
151241
}
242+
/** User type which isn't restricted in size (i.e. without `size`, `terminator`, or `size-eos`). */
152243
case class UserTypeInstream(
153244
_name: List[String],
154245
_forcedParent: Option[Ast.expr],
@@ -161,6 +252,7 @@ object DataType {
161252
r
162253
}
163254
}
255+
/** User type which is restricted in size either via `size`, `terminator`, or `size-eos`. */
164256
case class UserTypeFromBytes(
165257
_name: List[String],
166258
_forcedParent: Option[Ast.expr],
@@ -175,6 +267,12 @@ object DataType {
175267
r
176268
}
177269
}
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+
*/
178276
case class CalcUserType(
179277
_name: List[String],
180278
_forcedParent: Option[Ast.expr],
@@ -183,6 +281,10 @@ object DataType {
183281
) extends UserType(_name, _forcedParent, _args) {
184282
override def isOwning = false
185283
}
284+
/**
285+
* Reference to the user type which restricted in size either via `size`,
286+
* `terminator`, or `size-eos`.
287+
*/
186288
case class CalcUserTypeFromBytes(
187289
_name: List[String],
188290
_forcedParent: Option[Ast.expr],
@@ -194,30 +296,75 @@ object DataType {
194296
override def isOwning = false
195297
}
196298

299+
/**
300+
* A generic collection type.
301+
*
302+
* @param elType Type of elements in the collection
303+
*/
197304
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+
*/
199311
case class ArrayTypeInStream(_elType: DataType) extends ArrayType(_elType) {
200312
override def isOwning: Boolean = true
201313
override def asNonOwning(isOwningInExpr: Boolean = false): CalcArrayType = CalcArrayType(elType, isOwningInExpr)
202314
}
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+
*/
203322
case class CalcArrayType(_elType: DataType, override val isOwningInExpr: Boolean = false) extends ArrayType(_elType) {
204323
override def isOwning: Boolean = false
205324
}
206325

207326
val USER_TYPE_NO_PARENT = Ast.expr.Bool(false)
208327

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+
*/
209334
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+
*/
210340
case object KaitaiStructType extends StructType {
211341
def isOwning = true
212342
override def asNonOwning(isOwningInExpr: Boolean = false): DataType = CalcKaitaiStructType(isOwningInExpr)
213343
}
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+
*/
214350
case class CalcKaitaiStructType(override val isOwningInExpr: Boolean = false) extends StructType {
215351
def isOwning = false
216352
}
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+
*/
217358
case object OwnedKaitaiStreamType extends ComplexDataType {
218359
def isOwning = true
219360
override def asNonOwning(isOwningInExpr: Boolean = false): DataType = KaitaiStreamType
220361
}
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+
*/
221368
case object KaitaiStreamType extends ComplexDataType {
222369
def isOwning = false
223370
}
@@ -400,6 +547,8 @@ object DataType {
400547
StrFromBytesType(bat, enc)
401548
case _ =>
402549
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
403552
if (arg.size.isEmpty && !arg.sizeEos && arg.terminator.isEmpty) {
404553
if (arg.process.isDefined)
405554
throw KSYParseError(s"user type '$dt': need 'size' / 'size-eos' / 'terminator' if 'process' is used", path).toException

0 commit comments

Comments
 (0)