Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve text of some errors #311

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,14 @@ object DataType {
case class CalcKaitaiStructType(override val isOwningInExpr: Boolean = false) extends StructType {
def isOwning = false
}
case object OwnedKaitaiStreamType extends ComplexDataType {

/** Base class for streams from which types can be read or written. */
abstract sealed class StreamType extends ComplexDataType
case object OwnedKaitaiStreamType extends StreamType {
def isOwning = true
override def asNonOwning(isOwningInExpr: Boolean = false): DataType = KaitaiStreamType
}
case object KaitaiStreamType extends ComplexDataType {
case object KaitaiStreamType extends StreamType {
def isOwning = false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ object CSharpCompiler extends LanguageCompilerStatic

case AnyType => "object"
case KaitaiStructType | CalcKaitaiStructType(_) => kstructName
case KaitaiStreamType | OwnedKaitaiStreamType => kstreamName
case _: StreamType => kstreamName

case t: UserType => types2class(t.name)
case EnumType(name, _) => types2class(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ object GoCompiler extends LanguageCompilerStatic

case AnyType => "interface{}"
case KaitaiStructType | CalcKaitaiStructType(_) => kstructName
case KaitaiStreamType | OwnedKaitaiStreamType => s"*$kstreamName"
case _: StreamType => s"*$kstreamName"

case t: UserType => "*" + types2class(t.classSpec match {
case Some(cs) => cs.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ object JavaCompiler extends LanguageCompilerStatic
case _: BytesType => "byte[]"

case AnyType => "Object"
case KaitaiStreamType | OwnedKaitaiStreamType => kstreamName
case _: StreamType => kstreamName
case KaitaiStructType | CalcKaitaiStructType(_) => kstructName

case t: UserType => types2class(t.name)
Expand Down Expand Up @@ -914,7 +914,7 @@ object JavaCompiler extends LanguageCompilerStatic
case _: BytesType => "byte[]"

case AnyType => "Object"
case KaitaiStreamType | OwnedKaitaiStreamType => kstreamName
case _: StreamType => kstreamName
case KaitaiStructType | CalcKaitaiStructType(_) => kstructName

case t: UserType => types2class(t.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ object NimCompiler extends LanguageCompilerStatic
case _: BytesType => "seq[byte]"

case KaitaiStructType | CalcKaitaiStructType(_) => "KaitaiStruct"
case KaitaiStreamType | OwnedKaitaiStreamType => "KaitaiStream"
case _: StreamType => "KaitaiStream"

case t: UserType => namespaced(t.classSpec match {
case Some(cs) => cs.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ class PHPCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
case _: ArrayType => "array"

case KaitaiStructType | CalcKaitaiStructType(_) => kstructName
case KaitaiStreamType | OwnedKaitaiStreamType => kstreamName
case _: StreamType => kstreamName
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ object MethodArgType {
case object ArrayArg extends MethodArgType {
override def toString = "array"
}
case object StreamArg extends MethodArgType {
override def toString = "io stream"
}

def byDataType(dataType: DataType): Option[MethodArgType] = {
dataType match {
Expand All @@ -35,6 +38,7 @@ object MethodArgType {
case _: BooleanType => Some(BooleanArg)
case _: BytesType => Some(BytesArg)
case _: ArrayType => Some(ArrayArg)
case _: StreamType => Some(StreamArg)
case _ => None
}
}
Expand Down Expand Up @@ -133,6 +137,12 @@ abstract trait CommonMethods[T] extends TypeDetector {
MethodSig0("min", AnyType, arrayMin),
MethodSig0("max", AnyType, arrayMax),
),

StreamArg -> List(
MethodSig0("eof", CalcBooleanType, kaitaiStreamEof),
MethodSig0("pos", CalcIntType, kaitaiStreamPos),
MethodSig0("size", CalcIntType, kaitaiStreamSize),
),
)

/**
Expand Down Expand Up @@ -170,12 +180,6 @@ abstract trait CommonMethods[T] extends TypeDetector {
}
case ut: UserType =>
userTypeField(ut, value, attr.name)
case KaitaiStreamType | OwnedKaitaiStreamType =>
attr.name match {
case "size" => kaitaiStreamSize(value)
case "eof" => kaitaiStreamEof(value)
case "pos" => kaitaiStreamPos(value)
}
case et: EnumType =>
attr.name match {
case "to_i" => enumToInt(value, et)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.kaitai.struct.datatype.DataType
import io.kaitai.struct.datatype.DataType._
import io.kaitai.struct.exprlang.Ast
import io.kaitai.struct.format.Identifier
import io.kaitai.struct.precompile.{MethodNotFoundError, TypeMismatchError, TypeUndecidedError}
import io.kaitai.struct.precompile.{MethodNotFoundError, MethodNotFoundErrorWithArg, TypeMismatchError, TypeUndecidedError}

/**
* Basic class the implements type inferring functionality for Ast.expr
Expand Down Expand Up @@ -172,24 +172,28 @@ class TypeDetector(provider: TypeProvider) {
attr.name match {
case "length" | "size" => CalcIntType
case "first" | "last" | "min" | "max" => Int1Type(false)
case _ => throw new MethodNotFoundError(attr.name, valType)
// MethodArgType.byDataType returns Some(...) in that case
case _ => throw new MethodNotFoundErrorWithArg(attr.name, MethodArgType.byDataType(valType).get)
}
case _: StrType =>
attr.name match {
case "length" => CalcIntType
case "reverse" => CalcStrType
case "to_i" => CalcIntType
case _ => throw new MethodNotFoundError(attr.name, valType)
// MethodArgType.byDataType returns Some(...) in that case
case _ => throw new MethodNotFoundErrorWithArg(attr.name, MethodArgType.byDataType(valType).get)
}
case _: IntType =>
attr.name match {
case "to_s" => CalcStrType
case _ => throw new MethodNotFoundError(attr.name, valType)
// MethodArgType.byDataType returns Some(...) in that case
case _ => throw new MethodNotFoundErrorWithArg(attr.name, MethodArgType.byDataType(valType).get)
}
case _: FloatType =>
attr.name match {
case "to_i" => CalcIntType
case _ => throw new MethodNotFoundError(attr.name, valType)
// MethodArgType.byDataType returns Some(...) in that case
case _ => throw new MethodNotFoundErrorWithArg(attr.name, MethodArgType.byDataType(valType).get)
}
case ArrayTypeInStream(_) | CalcArrayType(_, _) =>
val inType = valType match {
Expand All @@ -211,14 +215,16 @@ class TypeDetector(provider: TypeProvider) {
attr.name match {
case "first" | "last" | "min" | "max" => inType
case "size" => CalcIntType
case _ => throw new MethodNotFoundError(attr.name, valType)
// MethodArgType.byDataType returns Some(...) in that case
case _ => throw new MethodNotFoundErrorWithArg(attr.name, MethodArgType.byDataType(valType).get)
}
case KaitaiStreamType | OwnedKaitaiStreamType =>
case _: StreamType =>
attr.name match {
case "size" => CalcIntType
case "pos" => CalcIntType
case "eof" => CalcBooleanType
case _ => throw new MethodNotFoundError(attr.name, valType)
// MethodArgType.byDataType returns Some(...) in that case
case _ => throw new MethodNotFoundErrorWithArg(attr.name, MethodArgType.byDataType(valType).get)
}
case et: EnumType =>
attr.name match {
Expand All @@ -228,7 +234,8 @@ class TypeDetector(provider: TypeProvider) {
case _: BooleanType =>
attr.name match {
case "to_i" => CalcIntType
case _ => throw new MethodNotFoundError(attr.name, valType)
// MethodArgType.byDataType returns Some(...) in that case
case _ => throw new MethodNotFoundErrorWithArg(attr.name, MethodArgType.byDataType(valType).get)
}
case _ =>
throw new MethodNotFoundError(attr.name, valType)
Expand All @@ -253,7 +260,10 @@ class TypeDetector(provider: TypeProvider) {
case (_: StrType, "to_i") => CalcIntType
case (_: BytesType, "to_s") => CalcStrType
case _ =>
throw new MethodNotFoundError(methodName.name, objType)
MethodArgType.byDataType(objType) match {
case Some(argType) => throw new MethodNotFoundErrorWithArg(methodName.name, argType)
case None => throw new MethodNotFoundError(methodName.name, objType)
}
}
}
}
Expand Down
Loading