Skip to content

Commit bd7e979

Browse files
committed
Document DataTypes
1 parent 519f065 commit bd7e979

File tree

1 file changed

+144
-1
lines changed

1 file changed

+144
-1
lines changed

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

Lines changed: 144 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,93 @@ object DataType {
3535
def apiCall(defEndian: Option[FixedEndian]): String
3636
}
3737

38+
/** A generic number type. */
3839
abstract sealed class NumericType extends DataType
40+
/** A generic boolean type. */
3941
abstract sealed class BooleanType extends DataType
4042

43+
/** A generic integer type. */
4144
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+
*/
4261
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+
*/
4378
case class Int1Type(signed: Boolean) extends IntType with ReadableType {
4479
override def apiCall(defEndian: Option[FixedEndian]): String = if (signed) "s1" else "u1"
4580
}
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+
*/
4690
case class IntMultiType(signed: Boolean, width: IntWidth, endian: Option[FixedEndian]) extends IntType with ReadableType {
4791
override def apiCall(defEndian: Option[FixedEndian]): String = {
4892
val ch1 = if (signed) 's' else 'u'
4993
val finalEnd = endian.orElse(defEndian)
5094
s"$ch1${width.width}${finalEnd.map(_.toSuffix).getOrElse("")}"
5195
}
5296
}
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+
*/
53102
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+
*/
54111
case class BitsType(width: Int, bitEndian: BitEndianness) extends IntType
55112

113+
/** A generic floating-point number type. */
56114
abstract sealed class FloatType extends NumericType
115+
/** A floating-point number type that occupies undecided number of bytes in the stream. */
57116
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+
*/
58125
case class FloatMultiType(width: IntWidth, endian: Option[FixedEndian]) extends FloatType with ReadableType {
59126
override def apiCall(defEndian: Option[FixedEndian]): String = {
60127
val finalEnd = endian.orElse(defEndian)
@@ -66,7 +133,13 @@ object DataType {
66133
def process: Option[ProcessExpr]
67134
}
68135

136+
/** A generic raw bytes type. */
69137
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+
*/
70143
case object CalcBytesType extends BytesType {
71144
override def process = None
72145
}
@@ -91,7 +164,13 @@ object DataType {
91164
override val process: Option[ProcessExpr]
92165
) extends BytesType
93166

167+
/** A generic string type. */
94168
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+
*/
95174
case object CalcStrType extends StrType
96175
/**
97176
* A type that have the `str` and `strz` built-in Kaitai types.
@@ -112,6 +191,11 @@ object DataType {
112191
isEncodingDerived: Boolean,
113192
) extends StrType
114193

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

117201
/**
@@ -178,6 +262,7 @@ object DataType {
178262
cs.meta.isOpaque
179263
}
180264
}
265+
/** User type which isn't restricted in size (i.e. without `size`, `terminator`, or `size-eos`). */
181266
case class UserTypeInstream(
182267
_name: List[String],
183268
_forcedParent: Option[Ast.expr],
@@ -190,6 +275,7 @@ object DataType {
190275
r
191276
}
192277
}
278+
/** User type which is restricted in size either via `size`, `terminator`, or `size-eos`. */
193279
case class UserTypeFromBytes(
194280
_name: List[String],
195281
_forcedParent: Option[Ast.expr],
@@ -204,6 +290,12 @@ object DataType {
204290
r
205291
}
206292
}
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+
*/
207299
case class CalcUserType(
208300
_name: List[String],
209301
_forcedParent: Option[Ast.expr],
@@ -212,6 +304,10 @@ object DataType {
212304
) extends UserType(_name, _forcedParent, _args) {
213305
override def isOwning = false
214306
}
307+
/**
308+
* Reference to the user type which restricted in size either via `size`,
309+
* `terminator`, or `size-eos`.
310+
*/
215311
case class CalcUserTypeFromBytes(
216312
_name: List[String],
217313
_forcedParent: Option[Ast.expr],
@@ -223,31 +319,76 @@ object DataType {
223319
override def isOwning = false
224320
}
225321

322+
/**
323+
* A generic collection type.
324+
*
325+
* @param elType Type of elements in the collection
326+
*/
226327
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+
*/
228334
case class ArrayTypeInStream(_elType: DataType) extends ArrayType(_elType) {
229335
override def isOwning: Boolean = true
230336
override def asNonOwning(isOwningInExpr: Boolean = false): CalcArrayType = CalcArrayType(elType, isOwningInExpr)
231337
}
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+
*/
232345
case class CalcArrayType(_elType: DataType, override val isOwningInExpr: Boolean = false) extends ArrayType(_elType) {
233346
override def isOwning: Boolean = false
234347
}
235348

236349
/** Represents `_parent: false` expression which means that type explicitly has no parent. */
237350
val USER_TYPE_NO_PARENT = Ast.expr.Bool(false)
238351

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+
*/
239358
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+
*/
240364
case object KaitaiStructType extends StructType {
241365
def isOwning = true
242366
override def asNonOwning(isOwningInExpr: Boolean = false): DataType = CalcKaitaiStructType(isOwningInExpr)
243367
}
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+
*/
244374
case class CalcKaitaiStructType(override val isOwningInExpr: Boolean = false) extends StructType {
245375
def isOwning = false
246376
}
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+
*/
247382
case object OwnedKaitaiStreamType extends ComplexDataType {
248383
def isOwning = true
249384
override def asNonOwning(isOwningInExpr: Boolean = false): DataType = KaitaiStreamType
250385
}
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+
*/
251392
case object KaitaiStreamType extends ComplexDataType {
252393
def isOwning = false
253394
}
@@ -459,6 +600,8 @@ object DataType {
459600
StrFromBytesType(bat, enc, arg.encoding.isEmpty)
460601
case _ =>
461602
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
462605
if (arg.size.isEmpty && !arg.sizeEos && arg.terminator.isEmpty) {
463606
if (arg.process.isDefined)
464607
throw KSYParseError(s"user type '$dt': need 'size' / 'size-eos' / 'terminator' if 'process' is used", path).toException

0 commit comments

Comments
 (0)