From a2b5e8134e03ab04d19f8dedf2e71e1c40842da7 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 5 Oct 2024 22:51:03 +0500 Subject: [PATCH] Make InvalidIdentifier exception subclass of ExpressionError Fixes test which instead of reporting error shows this exception: [info] - expr_bad_id_inst_value *** FAILED *** [info] io.kaitai.struct.format.InvalidIdentifier: invalid ID: 'BAD', expected /^[a-z][a-z0-9_]*$/ [info] at io.kaitai.struct.format.Identifier$.checkIdentifier(Identifier.scala:52) [info] at io.kaitai.struct.format.InstanceIdentifier.(Identifier.scala:113) [info] at io.kaitai.struct.ClassTypeProvider.resolveMember(ClassTypeProvider.scala:83) [info] at io.kaitai.struct.ClassTypeProvider.determineType(ClassTypeProvider.scala:46) [info] at io.kaitai.struct.ClassTypeProvider.determineType(ClassTypeProvider.scala:20) [info] at io.kaitai.struct.translators.TypeDetector.detectTypeRaw(TypeDetector.scala:61) [info] at io.kaitai.struct.translators.TypeDetector.detectType(TypeDetector.scala:25) [info] at io.kaitai.struct.precompile.DeriveValueInstanceTypes.$anonfun$deriveValueType$2(DeriveValueInstanceTypes.scala:53) [info] at io.kaitai.struct.precompile.DeriveValueInstanceTypes.$anonfun$deriveValueType$2$adapted(DeriveValueInstanceTypes.scala:46) [info] at scala.collection.immutable.RedBlackTree$.foreach(RedBlackTree.scala:291) [info] ... --- .../src/main/scala/io/kaitai/struct/ClassTypeProvider.scala | 2 +- .../src/main/scala/io/kaitai/struct/format/AttrSpec.scala | 1 + .../src/main/scala/io/kaitai/struct/format/Identifier.scala | 5 +---- .../src/main/scala/io/kaitai/struct/format/ParseUtils.scala | 1 + .../main/scala/io/kaitai/struct/precompile/Exceptions.scala | 6 +++++- .../scala/io/kaitai/struct/precompile/TypeValidator.scala | 4 ---- 6 files changed, 9 insertions(+), 10 deletions(-) diff --git a/shared/src/main/scala/io/kaitai/struct/ClassTypeProvider.scala b/shared/src/main/scala/io/kaitai/struct/ClassTypeProvider.scala index 0dee55827..310d69482 100644 --- a/shared/src/main/scala/io/kaitai/struct/ClassTypeProvider.scala +++ b/shared/src/main/scala/io/kaitai/struct/ClassTypeProvider.scala @@ -68,7 +68,7 @@ class ClassTypeProvider(classSpecs: ClassSpecs, var topClass: ClassSpec) extends * @param inClass type specification to search member in * @param attrName name of a member to search for * @return member spec if found, or throws an exception - * @throws format.InvalidIdentifier if attribute name is not a valid name for a member + * @throws precompile.InvalidIdentifier if attribute name is not a valid name for a member * @throws precompile.FieldNotFoundError if attribute with such name is not found */ def resolveMember(inClass: ClassSpec, attrName: String): MemberSpec = { diff --git a/shared/src/main/scala/io/kaitai/struct/format/AttrSpec.scala b/shared/src/main/scala/io/kaitai/struct/format/AttrSpec.scala index d9e595dca..12a760121 100644 --- a/shared/src/main/scala/io/kaitai/struct/format/AttrSpec.scala +++ b/shared/src/main/scala/io/kaitai/struct/format/AttrSpec.scala @@ -6,6 +6,7 @@ import io.kaitai.struct.datatype.DataType import io.kaitai.struct.datatype.DataType._ import io.kaitai.struct.exprlang.Ast.expr import io.kaitai.struct.exprlang.{Ast, Expressions} +import io.kaitai.struct.precompile.InvalidIdentifier import io.kaitai.struct.problems.KSYParseError import scala.collection.immutable.SortedMap diff --git a/shared/src/main/scala/io/kaitai/struct/format/Identifier.scala b/shared/src/main/scala/io/kaitai/struct/format/Identifier.scala index 2912eb9e9..8f7855ac9 100644 --- a/shared/src/main/scala/io/kaitai/struct/format/Identifier.scala +++ b/shared/src/main/scala/io/kaitai/struct/format/Identifier.scala @@ -1,6 +1,7 @@ package io.kaitai.struct.format import io.kaitai.struct.exprlang.Ast +import io.kaitai.struct.precompile.InvalidIdentifier import io.kaitai.struct.problems.KSYParseError /** @@ -37,10 +38,6 @@ case class NamedIdentifier(name: String) extends Identifier { override def humanReadable: String = name } -case class InvalidIdentifier(id: String) extends RuntimeException( - s"invalid ID: '$id', expected /${Identifier.ReIdentifier.toString}/" -) - object Identifier { val ReIdentifier = "^[a-z][a-z0-9_]*$".r diff --git a/shared/src/main/scala/io/kaitai/struct/format/ParseUtils.scala b/shared/src/main/scala/io/kaitai/struct/format/ParseUtils.scala index 1481cddcb..a9fc6b8f7 100644 --- a/shared/src/main/scala/io/kaitai/struct/format/ParseUtils.scala +++ b/shared/src/main/scala/io/kaitai/struct/format/ParseUtils.scala @@ -2,6 +2,7 @@ package io.kaitai.struct.format import io.kaitai.struct.Utils import io.kaitai.struct.exprlang.{Ast, Expressions} +import io.kaitai.struct.precompile.InvalidIdentifier import io.kaitai.struct.problems.KSYParseError object ParseUtils { diff --git a/shared/src/main/scala/io/kaitai/struct/precompile/Exceptions.scala b/shared/src/main/scala/io/kaitai/struct/precompile/Exceptions.scala index 1ac6035bb..bc5dfb540 100644 --- a/shared/src/main/scala/io/kaitai/struct/precompile/Exceptions.scala +++ b/shared/src/main/scala/io/kaitai/struct/precompile/Exceptions.scala @@ -1,7 +1,7 @@ package io.kaitai.struct.precompile import io.kaitai.struct.datatype.DataType -import io.kaitai.struct.format.ClassSpec +import io.kaitai.struct.format.{ClassSpec, Identifier} import io.kaitai.struct.translators.MethodArgType /** @@ -9,6 +9,10 @@ import io.kaitai.struct.translators.MethodArgType * in source file. */ sealed abstract class ExpressionError(msg: String) extends RuntimeException(msg) + +class InvalidIdentifier(id: String) + extends ExpressionError(s"invalid ID: '$id', expected /${Identifier.ReIdentifier.toString}/") + class TypeMismatchError(msg: String) extends ExpressionError(msg) class TypeUndecidedError(msg: String) extends ExpressionError(msg) class WrongMethodCall(val dataType: MethodArgType, val methodName: String, val expectedSigs: Iterable[String], val actualSig: String) diff --git a/shared/src/main/scala/io/kaitai/struct/precompile/TypeValidator.scala b/shared/src/main/scala/io/kaitai/struct/precompile/TypeValidator.scala index d80741484..11fbd9909 100644 --- a/shared/src/main/scala/io/kaitai/struct/precompile/TypeValidator.scala +++ b/shared/src/main/scala/io/kaitai/struct/precompile/TypeValidator.scala @@ -239,8 +239,6 @@ class TypeValidator(specs: ClassSpecs) extends PrecompileStep { detector.validate(expr) None } catch { - case err: InvalidIdentifier => - Some(ErrorInInput(err, path ++ List(pathKey))) case err: ExpressionError => Some(ErrorInInput(err, path ++ List(pathKey))) } @@ -285,8 +283,6 @@ class TypeValidator(specs: ClassSpecs) extends PrecompileStep { detector.validate(expr) None } catch { - case err: InvalidIdentifier => - Some(ErrorInInput(err, path ++ List(pathKey))) case err: ExpressionError => Some(ErrorInInput(err, path ++ List(pathKey))) }