From 2f157070908bee52e63e32f65d618e1200818843 Mon Sep 17 00:00:00 2001 From: Mingun Date: Tue, 24 Nov 2020 11:10:04 +0500 Subject: [PATCH] Rename EnumById & Co to EnumCast and document them --- .../io/kaitai/struct/GraphvizClassCompiler.scala | 4 ++-- .../main/scala/io/kaitai/struct/exprlang/Ast.scala | 8 +++++++- .../io/kaitai/struct/format/InstanceSpec.scala | 4 ++-- .../components/EveryReadIsExpression.scala | 2 +- .../struct/languages/components/GoReads.scala | 2 +- .../kaitai/struct/translators/BaseTranslator.scala | 14 +++++++++++--- .../struct/translators/CSharpTranslator.scala | 4 ++-- .../kaitai/struct/translators/CppTranslator.scala | 4 ++-- .../struct/translators/ExpressionValidator.scala | 4 ++-- .../kaitai/struct/translators/GoTranslator.scala | 8 ++++---- .../struct/translators/JavaScriptTranslator.scala | 4 ++-- .../kaitai/struct/translators/JavaTranslator.scala | 4 ++-- .../kaitai/struct/translators/LuaTranslator.scala | 4 ++-- .../kaitai/struct/translators/NimTranslator.scala | 2 +- .../kaitai/struct/translators/PHPTranslator.scala | 4 ++-- .../kaitai/struct/translators/PerlTranslator.scala | 4 ++-- .../struct/translators/PythonTranslator.scala | 4 ++-- .../kaitai/struct/translators/RubyTranslator.scala | 4 ++-- .../kaitai/struct/translators/RustTranslator.scala | 4 ++-- .../kaitai/struct/translators/TypeDetector.scala | 2 +- 20 files changed, 52 insertions(+), 38 deletions(-) diff --git a/shared/src/main/scala/io/kaitai/struct/GraphvizClassCompiler.scala b/shared/src/main/scala/io/kaitai/struct/GraphvizClassCompiler.scala index 0b17f49b6..f3c0f5240 100644 --- a/shared/src/main/scala/io/kaitai/struct/GraphvizClassCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/GraphvizClassCompiler.scala @@ -296,8 +296,8 @@ class GraphvizClassCompiler(classSpecs: ClassSpecs, topClass: ClassSpec) extends List() case _: Ast.expr.EnumVariant => List() - case Ast.expr.EnumById(_, id, _) => - affectedVars(id) + case Ast.expr.EnumCast(_, value, _) => + affectedVars(value) case Ast.expr.Attribute(value, attr) => val targetClass = translator.detectType(value) targetClass match { diff --git a/shared/src/main/scala/io/kaitai/struct/exprlang/Ast.scala b/shared/src/main/scala/io/kaitai/struct/exprlang/Ast.scala index e80c79eee..ce6795c52 100644 --- a/shared/src/main/scala/io/kaitai/struct/exprlang/Ast.scala +++ b/shared/src/main/scala/io/kaitai/struct/exprlang/Ast.scala @@ -84,7 +84,13 @@ object Ast { * KSY file. */ case class EnumVariant(enumName: identifier, variant: identifier, inType: typeId = EmptyTypeId) extends expr - case class EnumById(enumName: identifier, id: expr, inType: typeId = EmptyTypeId) extends expr + /** + * Transformation of the `value` expression into the `enumName` type, + * defined in the type `inType`. Unlike other nodes this node never + * parsed from the expression language, because at parse time any + * identifier can represent an enum and actual resolution performed later + */ + case class EnumCast(enumName: identifier, value: expr, inType: typeId = EmptyTypeId) extends expr case class Attribute(value: expr, attr: identifier) extends expr case class CastToType(value: expr, typeName: typeId) extends expr diff --git a/shared/src/main/scala/io/kaitai/struct/format/InstanceSpec.scala b/shared/src/main/scala/io/kaitai/struct/format/InstanceSpec.scala index 4a66307ed..467019991 100644 --- a/shared/src/main/scala/io/kaitai/struct/format/InstanceSpec.scala +++ b/shared/src/main/scala/io/kaitai/struct/format/InstanceSpec.scala @@ -56,12 +56,12 @@ object InstanceSpec { // value instance ParseUtils.ensureLegalKeys(srcMap, LEGAL_KEYS_VALUE_INST, path, Some("value instance")) - // Wrap everything in EnumById if "enum" is used + // Wrap everything in EnumCast if "enum" is used val value2 = ParseUtils.getOptValueStr(srcMap, "enum", path) match { case None => value case Some(enumName) => - Ast.expr.EnumById(Ast.identifier(enumName), value) + Ast.expr.EnumCast(Ast.identifier(enumName), value) } val ifExpr = ParseUtils.getOptValueExpression(srcMap, "if", path) diff --git a/shared/src/main/scala/io/kaitai/struct/languages/components/EveryReadIsExpression.scala b/shared/src/main/scala/io/kaitai/struct/languages/components/EveryReadIsExpression.scala index 9db2e5c44..cd15ff14f 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/components/EveryReadIsExpression.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/components/EveryReadIsExpression.scala @@ -55,7 +55,7 @@ trait EveryReadIsExpression val expr = translator.bytesToStr(parseExprBytes(t.bytes, io), t.encoding) handleAssignment(id, expr, rep, isRaw) case t: EnumType => - val expr = translator.doEnumById(t.enumSpec.get, parseExpr(t.basedOn, t.basedOn, io, defEndian)) + val expr = translator.doEnumCast(t.enumSpec.get, parseExpr(t.basedOn, t.basedOn, io, defEndian)) handleAssignment(id, expr, rep, isRaw) case _ => val expr = parseExpr(dataType, assignType, io, defEndian) diff --git a/shared/src/main/scala/io/kaitai/struct/languages/components/GoReads.scala b/shared/src/main/scala/io/kaitai/struct/languages/components/GoReads.scala index 600d0cb28..f4fb24273 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/components/GoReads.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/components/GoReads.scala @@ -71,7 +71,7 @@ trait GoReads extends CommonReads with ObjectOrientedLanguage with GoSwitchOps { case t: EnumType => val r1 = translator.outVarCheckRes(parseExpr(t.basedOn, io, defEndian)) val enumSpec = t.enumSpec.get - val expr = translator.trEnumById(enumSpec.name, translator.resToStr(r1)) + val expr = translator.trEnumCast(enumSpec.name, translator.resToStr(r1)) handleAssignment(id, expr, rep, isRaw) case _: BitsType1 => val expr = parseExpr(dataType, io, defEndian) diff --git a/shared/src/main/scala/io/kaitai/struct/translators/BaseTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/BaseTranslator.scala index d04a293a7..ce9bdf46c 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/BaseTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/BaseTranslator.scala @@ -59,9 +59,9 @@ abstract class BaseTranslator(val provider: TypeProvider) doInterpolatedStringLiteral(s) case Ast.expr.Bool(n) => doBoolLiteral(n) - case Ast.expr.EnumById(enumType, id, inType) => + case Ast.expr.EnumCast(enumType, value, inType) => val enumSpec = provider.resolveEnum(inType, enumType.name) - doEnumById(enumSpec, translate(id)) + doEnumCast(enumSpec, translate(value)) case Ast.expr.EnumVariant(enumType, variant, inType) => val enumSpec = provider.resolveEnum(inType, enumType.name) doEnumVariant(enumSpec, variant.name) @@ -195,7 +195,15 @@ abstract class BaseTranslator(val provider: TypeProvider) * @return String in the target language with reference to the enum variant */ def doEnumVariant(enumSpec: EnumSpec, variant: String): String - def doEnumById(enumSpec: EnumSpec, id: String): String + /** + * Translates cast of expression to the enumeration type. + * + * @param enumSpec An enum definition + * @param value Translated expression which should have enum type + * + * @return String in the target language transformation of the expression result into enumeration type + */ + def doEnumCast(enumSpec: EnumSpec, value: String): String // Predefined methods of various types def strConcat(left: Ast.expr, right: Ast.expr, extPrec: Int) = diff --git a/shared/src/main/scala/io/kaitai/struct/translators/CSharpTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/CSharpTranslator.scala index fe6fc828c..b53da85ee 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/CSharpTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/CSharpTranslator.scala @@ -63,8 +63,8 @@ class CSharpTranslator(provider: TypeProvider, importList: ImportList) extends B override def doEnumVariant(enumSpec: EnumSpec, variant: String): String = s"${enumClass(enumSpec.name)}.${Utils.upperCamelCase(variant)}" - override def doEnumById(enumSpec: EnumSpec, id: String): String = - s"((${enumClass(enumSpec.name)}) $id)" + override def doEnumCast(enumSpec: EnumSpec, value: String): String = + s"((${enumClass(enumSpec.name)}) $value)" def enumClass(enumTypeAbs: List[String]): String = { val enumTypeRel = Utils.relClass(enumTypeAbs, provider.nowClass.name) diff --git a/shared/src/main/scala/io/kaitai/struct/translators/CppTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/CppTranslator.scala index 755a020a2..6e524874c 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/CppTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/CppTranslator.scala @@ -150,8 +150,8 @@ class CppTranslator(provider: TypeProvider, importListSrc: CppImportList, import CppCompiler.types2class(enumSpec.name.dropRight(1)) + "::" + Utils.upperUnderscoreCase(enumSpec.name.last + "_" + variant) } - override def doEnumById(enumSpec: EnumSpec, id: String): String = - s"static_cast<${CppCompiler.types2class(enumSpec.name)}>($id)" + override def doEnumCast(enumSpec: EnumSpec, value: String): String = + s"static_cast<${CppCompiler.types2class(enumSpec.name)}>($value)" override def doStrCompareOp(left: Ast.expr, op: Ast.cmpop, right: Ast.expr) = { if (op == Ast.cmpop.Eq) { diff --git a/shared/src/main/scala/io/kaitai/struct/translators/ExpressionValidator.scala b/shared/src/main/scala/io/kaitai/struct/translators/ExpressionValidator.scala index 50c6f43f4..8a4ed4608 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/ExpressionValidator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/ExpressionValidator.scala @@ -31,9 +31,9 @@ class ExpressionValidator(val provider: TypeProvider) _: Ast.expr.FloatNum | _: Ast.expr.Str | _: Ast.expr.Bool => // all simple literals are good and valid - case Ast.expr.EnumById(enumType, id, inType) => + case Ast.expr.EnumCast(enumType, value, inType) => provider.resolveEnum(inType, enumType.name) - validate(id) + validate(value) case Ast.expr.EnumVariant(enumType, variant, inType) => val enumSpec = provider.resolveEnum(inType, enumType.name) if (!enumSpec.map.values.exists(_.name == variant.name)) { diff --git a/shared/src/main/scala/io/kaitai/struct/translators/GoTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/GoTranslator.scala index 3de3c7649..630581a3f 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/GoTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/GoTranslator.scala @@ -47,9 +47,9 @@ class GoTranslator(out: StringLanguageOutputWriter, provider: TypeProvider, impo trInterpolatedStringLiteral(s) case Ast.expr.Bool(n) => trBoolLiteral(n) - case Ast.expr.EnumById(enumType, id, inType) => + case Ast.expr.EnumCast(enumType, value, inType) => val enumSpec = provider.resolveEnum(inType, enumType.name) - trEnumById(enumSpec.name, translate(id)) + trEnumCast(enumSpec.name, translate(value)) case Ast.expr.EnumVariant(enumType, variant, inType) => val enumSpec = provider.resolveEnum(inType, enumType.name) trEnumVariant(enumSpec.name, variant.name) @@ -255,8 +255,8 @@ class GoTranslator(out: StringLanguageOutputWriter, provider: TypeProvider, impo def trEnumVariant(enumTypeAbs: List[String], variant: String) = ResultString(GoCompiler.enumToStr(enumTypeAbs, variant)) - def trEnumById(enumTypeAbs: List[String], id: String) = - ResultString(s"${types2class(enumTypeAbs)}($id)") + def trEnumCast(enumTypeAbs: List[String], value: String) = + ResultString(s"${types2class(enumTypeAbs)}($value)") override def doBytesCompareOp(left: Ast.expr, op: Ast.cmpop, right: Ast.expr): String = { op match { diff --git a/shared/src/main/scala/io/kaitai/struct/translators/JavaScriptTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/JavaScriptTranslator.scala index 62859cfec..89663fe42 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/JavaScriptTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/JavaScriptTranslator.scala @@ -64,9 +64,9 @@ class JavaScriptTranslator(provider: TypeProvider, importList: ImportList) exten } s"${JavaScriptCompiler.types2class(enumSpec.name, isExternal)}.${Utils.upperUnderscoreCase(variant)}" } - override def doEnumById(enumSpec: EnumSpec, id: String): String = + override def doEnumCast(enumSpec: EnumSpec, value: String): String = // Just an integer, without any casts / resolutions - one would have to look up constants manually - id + value override def doBytesCompareOp(left: Ast.expr, op: Ast.cmpop, right: Ast.expr): String = s"(${JavaScriptCompiler.kstreamName}.byteArrayCompare(${translate(left)}, ${translate(right)}) ${cmpOp(op)} 0)" diff --git a/shared/src/main/scala/io/kaitai/struct/translators/JavaTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/JavaTranslator.scala index 320ce4fd0..2a72a7aa0 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/JavaTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/JavaTranslator.scala @@ -68,8 +68,8 @@ class JavaTranslator(provider: TypeProvider, importList: ImportList) extends Bas override def doEnumVariant(enumSpec: EnumSpec, variant: String): String = s"${enumClass(enumSpec.name)}.${Utils.upperUnderscoreCase(variant)}" - override def doEnumById(enumSpec: EnumSpec, id: String): String = - s"${enumClass(enumSpec.name)}.byId($id)" + override def doEnumCast(enumSpec: EnumSpec, value: String): String = + s"${enumClass(enumSpec.name)}.byId($value)" def enumClass(enumTypeAbs: List[String]): String = { val enumTypeRel = Utils.relClass(enumTypeAbs, provider.nowClass.name) diff --git a/shared/src/main/scala/io/kaitai/struct/translators/LuaTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/LuaTranslator.scala index 7bcbe857b..c5fadd6a9 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/LuaTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/LuaTranslator.scala @@ -84,8 +84,8 @@ class LuaTranslator(provider: TypeProvider, importList: ImportList) extends Base override def doEnumVariant(enumSpec: EnumSpec, variant: String): String = s"${LuaCompiler.types2class(enumSpec.name)}.$variant" - override def doEnumById(enumSpec: EnumSpec, id: String): String = - s"${LuaCompiler.types2class(enumSpec.name)}($id)" + override def doEnumCast(enumSpec: EnumSpec, value: String): String = + s"${LuaCompiler.types2class(enumSpec.name)}($value)" override def strConcat(left: Ast.expr, right: Ast.expr, extPrec: Int): String = s"${translate(left)} .. ${translate(right)}" diff --git a/shared/src/main/scala/io/kaitai/struct/translators/NimTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/NimTranslator.scala index 939e48313..e8c2a14e9 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/NimTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/NimTranslator.scala @@ -14,7 +14,7 @@ class NimTranslator(provider: TypeProvider, importList: ImportList) extends Base override def bytesToStr(bytesExpr: String, encoding: String): String = { s"""encode($bytesExpr, ${doStringLiteral(encoding)})""" } - override def doEnumById(enumSpec: EnumSpec, id: String): String = s"${namespaced(enumSpec.name)}($id)" + override def doEnumCast(enumSpec: EnumSpec, value: String): String = s"${namespaced(enumSpec.name)}($value)" // override def doEnumVariant(enumSpec: EnumSpec, variant: String): String = s"${namespaced(enumSpec.name)}($variant)" override def doEnumVariant(enumSpec: EnumSpec, variant: String): String = s"${enumSpec.name.head}.$variant" override def doName(s: String): String = diff --git a/shared/src/main/scala/io/kaitai/struct/translators/PHPTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/PHPTranslator.scala index e1aaf41a3..0f6526026 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/PHPTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/PHPTranslator.scala @@ -74,9 +74,9 @@ class PHPTranslator(provider: TypeProvider, config: RuntimeConfig) extends BaseT val enumClass = types2classAbs(enumSpec.name) s"$enumClass::${Utils.upperUnderscoreCase(variant)}" } - override def doEnumById(enumSpec: EnumSpec, id: String): String = + override def doEnumCast(enumSpec: EnumSpec, value: String): String = // Just an integer, without any casts / resolutions - one would have to look up constants manually - id + value override def arraySubscript(container: expr, idx: expr): String = s"${translate(container)}[${translate(idx)}]" diff --git a/shared/src/main/scala/io/kaitai/struct/translators/PerlTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/PerlTranslator.scala index dd6e9d2bd..25926cd49 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/PerlTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/PerlTranslator.scala @@ -86,9 +86,9 @@ class PerlTranslator(provider: TypeProvider, importList: ImportList) extends Bas val enumName = Utils.upperUnderscoreCase(enumSpec.name.last) s"$$$enumClassWithScope${enumName}_${Utils.upperUnderscoreCase(variant)}" } - override def doEnumById(enumSpec: EnumSpec, id: String): String = + override def doEnumCast(enumSpec: EnumSpec, value: String): String = // Just an integer, without any casts / resolutions - one would have to look up constants manually - id + value def enumClass(enumTypeAbs: List[String]): String = { val enumTypeRel = Utils.relClass(enumTypeAbs, provider.nowClass.name) diff --git a/shared/src/main/scala/io/kaitai/struct/translators/PythonTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/PythonTranslator.scala index c0f6eceba..201140c12 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/PythonTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/PythonTranslator.scala @@ -63,8 +63,8 @@ class PythonTranslator(provider: TypeProvider, importList: ImportList, config: R } s"${PythonCompiler.types2class(enumSpec.name, isExternal)}.$variant" } - override def doEnumById(enumSpec: EnumSpec, id: String): String = - s"${PythonCompiler.kstreamName}.resolve_enum(${PythonCompiler.types2class(enumSpec.name, enumSpec.isExternal(provider.nowClass))}, $id)" + override def doEnumCast(enumSpec: EnumSpec, value: String): String = + s"${PythonCompiler.kstreamName}.resolve_enum(${PythonCompiler.types2class(enumSpec.name, enumSpec.isExternal(provider.nowClass))}, $value)" override def booleanOp(op: Ast.boolop) = op match { case Ast.boolop.Or => "or" diff --git a/shared/src/main/scala/io/kaitai/struct/translators/RubyTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/RubyTranslator.scala index fcb5e4daf..d43a8ded1 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/RubyTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/RubyTranslator.scala @@ -42,8 +42,8 @@ class RubyTranslator(provider: TypeProvider) extends BaseTranslator(provider) override def doEnumVariant(enumSpec: EnumSpec, variant: String): String = RubyCompiler.enumValue(enumSpec.name.last, variant) - override def doEnumById(enumSpec: EnumSpec, id: String): String = - s"${RubyCompiler.kstreamName}::resolve_enum(${enumDirectMap(enumSpec.name)}, $id)" + override def doEnumCast(enumSpec: EnumSpec, value: String): String = + s"${RubyCompiler.kstreamName}::resolve_enum(${enumDirectMap(enumSpec.name)}, $value)" def enumDirectMap(enumTypeAndName: List[String]): String = { val enumTypeAbs = enumTypeAndName.dropRight(1) diff --git a/shared/src/main/scala/io/kaitai/struct/translators/RustTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/RustTranslator.scala index 1f9049ba7..9c8874809 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/RustTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/RustTranslator.scala @@ -41,9 +41,9 @@ class RustTranslator(provider: TypeProvider, config: RuntimeConfig) extends Base val enumClass = types2classAbs(enumSpec.name) s"$enumClass::${Utils.upperUnderscoreCase(variant)}" } - override def doEnumById(enumSpec: EnumSpec, id: String): String = + override def doEnumCast(enumSpec: EnumSpec, value: String): String = // Just an integer, without any casts / resolutions - one would have to look up constants manually - id + value override def arraySubscript(container: expr, idx: expr): String = s"${translate(container)}[${translate(idx)}]" diff --git a/shared/src/main/scala/io/kaitai/struct/translators/TypeDetector.scala b/shared/src/main/scala/io/kaitai/struct/translators/TypeDetector.scala index f711cbbdb..927227e0d 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/TypeDetector.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/TypeDetector.scala @@ -54,7 +54,7 @@ class TypeDetector(provider: TypeProvider) { val t = EnumType(inType.names.toList :+ enumType.name, CalcIntType) t.enumSpec = Some(provider.resolveEnum(inType, enumType.name)) t - case Ast.expr.EnumById(enumType, _, inType) => + case Ast.expr.EnumCast(enumType, _, inType) => val t = EnumType(List(enumType.name), CalcIntType) t.enumSpec = Some(provider.resolveEnum(inType, enumType.name)) t