Skip to content

Commit

Permalink
Make InvalidIdentifier exception subclass of ExpressionError
Browse files Browse the repository at this point in the history
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.<init>(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]   ...
  • Loading branch information
Mingun committed Oct 5, 2024
1 parent 542b241 commit a2b5e81
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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

/**
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
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

/**
* Base class for all expression-related errors, not localized to a certain path
* 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
Expand Down Expand Up @@ -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)))
}
Expand Down

0 comments on commit a2b5e81

Please sign in to comment.