From dfc86285aefe39d72b86b55cdc0d3d5944f8f884 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 11 Jan 2020 18:18:49 +0500 Subject: [PATCH] #22: Forward full attribute data to compilers --- .../io/kaitai/struct/ClassCompiler.scala | 4 +-- .../struct/languages/CSharpCompiler.scala | 8 +++--- .../kaitai/struct/languages/CppCompiler.scala | 10 +++---- .../kaitai/struct/languages/GoCompiler.scala | 6 ++-- .../struct/languages/JavaCompiler.scala | 8 +++--- .../struct/languages/JavaScriptCompiler.scala | 5 ++-- .../kaitai/struct/languages/LuaCompiler.scala | 5 ++-- .../kaitai/struct/languages/PHPCompiler.scala | 28 +++++++++++-------- .../struct/languages/PerlCompiler.scala | 11 ++++---- .../struct/languages/PythonCompiler.scala | 5 ++-- .../struct/languages/RubyCompiler.scala | 9 +++--- .../struct/languages/RustCompiler.scala | 10 +++---- .../components/LanguageCompiler.scala | 6 ++-- 13 files changed, 63 insertions(+), 52 deletions(-) diff --git a/shared/src/main/scala/io/kaitai/struct/ClassCompiler.scala b/shared/src/main/scala/io/kaitai/struct/ClassCompiler.scala index f4d3826f2..46fb67659 100644 --- a/shared/src/main/scala/io/kaitai/struct/ClassCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/ClassCompiler.scala @@ -195,7 +195,7 @@ class ClassCompiler( } else { attr.isNullable } - lang.attributeDeclaration(attr.id, attr.dataTypeComposite, isNullable) + lang.attributeDeclaration(attr, isNullable) } } @@ -214,7 +214,7 @@ class ClassCompiler( } else { attr.isNullable } - lang.attributeReader(attr.id, attr.dataTypeComposite, isNullable) + lang.attributeReader(attr, isNullable) } } diff --git a/shared/src/main/scala/io/kaitai/struct/languages/CSharpCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/CSharpCompiler.scala index c356c0265..85be3b05c 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/CSharpCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/CSharpCompiler.scala @@ -152,12 +152,12 @@ class CSharpCompiler(val typeProvider: ClassTypeProvider, config: RuntimeConfig) override def readFooter(): Unit = fileFooter("") - override def attributeDeclaration(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = { - out.puts(s"private ${kaitaiType2NativeTypeNullable(attrType, isNullable)} ${privateMemberName(attrName)};") + override def attributeDeclaration(attr: MemberSpec, isNullable: Boolean): Unit = { + out.puts(s"private ${kaitaiType2NativeTypeNullable(attr.dataTypeComposite, isNullable)} ${privateMemberName(attr.id)};") } - override def attributeReader(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = { - out.puts(s"public ${kaitaiType2NativeTypeNullable(attrType, isNullable)} ${publicMemberName(attrName)} { get { return ${privateMemberName(attrName)}; } }") + override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = { + out.puts(s"public ${kaitaiType2NativeTypeNullable(attr.dataTypeComposite, isNullable)} ${publicMemberName(attr.id)} { get { return ${privateMemberName(attr.id)}; } }") } override def universalDoc(doc: DocSpec): Unit = { diff --git a/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala index fc32492f4..4db6c433c 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala @@ -297,10 +297,10 @@ class CppCompiler( ensureMode(PublicAccess) } - override def attributeDeclaration(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = { + override def attributeDeclaration(attr: MemberSpec, isNullable: Boolean): Unit = { ensureMode(PrivateAccess) - outHdr.puts(s"${kaitaiType2NativeType(attrType)} ${privateMemberName(attrName)};") - declareNullFlag(attrName, isNullable) + outHdr.puts(s"${kaitaiType2NativeType(attr.dataTypeComposite)} ${privateMemberName(attr.id)};") + declareNullFlag(attr.id, isNullable) } def ensureMode(newMode: AccessMode): Unit = { @@ -316,9 +316,9 @@ class CppCompiler( } } - override def attributeReader(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = { + override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = { ensureMode(PublicAccess) - outHdr.puts(s"${kaitaiType2NativeType(attrType.asNonOwning)} ${publicMemberName(attrName)}() const { return ${nonOwningPointer(attrName, attrType)}; }") + outHdr.puts(s"${kaitaiType2NativeType(attr.dataTypeComposite.asNonOwning)} ${publicMemberName(attr.id)}() const { return ${nonOwningPointer(attr.id, attr.dataTypeComposite)}; }") } override def universalDoc(doc: DocSpec): Unit = { diff --git a/shared/src/main/scala/io/kaitai/struct/languages/GoCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/GoCompiler.scala index b00d41722..6413627b2 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/GoCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/GoCompiler.scala @@ -136,12 +136,12 @@ class GoCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) universalFooter } - override def attributeDeclaration(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = { - out.puts(s"${idToStr(attrName)} ${kaitaiType2NativeType(attrType)}") + override def attributeDeclaration(attr: MemberSpec, isNullable: Boolean): Unit = { + out.puts(s"${idToStr(attr.id)} ${kaitaiType2NativeType(attr.dataTypeComposite)}") translator.returnRes = None } - override def attributeReader(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = {} + override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = {} override def universalDoc(doc: DocSpec): Unit = { out.puts diff --git a/shared/src/main/scala/io/kaitai/struct/languages/JavaCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/JavaCompiler.scala index 3cea44586..917d096f4 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/JavaCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/JavaCompiler.scala @@ -197,12 +197,12 @@ class JavaCompiler(val typeProvider: ClassTypeProvider, config: RuntimeConfig) override def readFooter(): Unit = universalFooter - override def attributeDeclaration(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = { - out.puts(s"private ${kaitaiType2JavaType(attrType, isNullable)} ${idToStr(attrName)};") + override def attributeDeclaration(attr: MemberSpec, isNullable: Boolean): Unit = { + out.puts(s"private ${kaitaiType2JavaType(attr.dataTypeComposite, isNullable)} ${idToStr(attr.id)};") } - override def attributeReader(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = { - out.puts(s"public ${kaitaiType2JavaType(attrType, isNullable)} ${idToStr(attrName)}() { return ${idToStr(attrName)}; }") + override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = { + out.puts(s"public ${kaitaiType2JavaType(attr.dataTypeComposite, isNullable)} ${idToStr(attr.id)}() { return ${idToStr(attr.id)}; }") } override def universalDoc(doc: DocSpec): Unit = { diff --git a/shared/src/main/scala/io/kaitai/struct/languages/JavaScriptCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/JavaScriptCompiler.scala index 3a916f420..07a08cc2d 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/JavaScriptCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/JavaScriptCompiler.scala @@ -150,9 +150,9 @@ class JavaScriptCompiler(val typeProvider: ClassTypeProvider, config: RuntimeCon out.puts("}") } - override def attributeDeclaration(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = {} + override def attributeDeclaration(attr: MemberSpec, isNullable: Boolean): Unit = {} - override def attributeReader(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = {} + override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = {} override def universalDoc(doc: DocSpec): Unit = { // JSDoc docstring style: http://usejsdoc.org/about-getting-started.html @@ -487,6 +487,7 @@ class JavaScriptCompiler(val typeProvider: ClassTypeProvider, config: RuntimeCon // + override def instanceDeclaration(instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {} override def instanceHeader(className: List[String], instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = { out.puts(s"Object.defineProperty(${type2class(className.last)}.prototype, '${publicMemberName(instName)}', {") out.inc diff --git a/shared/src/main/scala/io/kaitai/struct/languages/LuaCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/LuaCompiler.scala index 6c538404a..94caf4a94 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/LuaCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/LuaCompiler.scala @@ -132,9 +132,9 @@ class LuaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) out.puts } - override def attributeDeclaration(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = + override def attributeDeclaration(attr: MemberSpec, isNullable: Boolean): Unit = {} - override def attributeReader(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = + override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = {} override def attrParseHybrid(leProc: () => Unit, beProc: () => Unit): Unit = { @@ -251,6 +251,7 @@ class LuaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) override def alignToByte(io: String): Unit = out.puts(s"$io:align_to_byte()") + override def instanceDeclaration(instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {} override def instanceHeader(className: List[String], instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = { out.puts(s"${types2class(className)}.property.${publicMemberName(instName)} = {}") out.puts(s"function ${types2class(className)}.property.${publicMemberName(instName)}:get()") diff --git a/shared/src/main/scala/io/kaitai/struct/languages/PHPCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/PHPCompiler.scala index 07f420292..0e6f4b4e9 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/PHPCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/PHPCompiler.scala @@ -142,21 +142,15 @@ class PHPCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) override def readFooter(): Unit = universalFooter - override def attributeDeclaration(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = { - attrName match { - case ParentIdentifier | RootIdentifier | IoIdentifier => - // just ignore it for now - case _ => - out.puts(s"protected $$_m_${idToStr(attrName)};") - } - } + override def attributeDeclaration(attr: MemberSpec, isNullable: Boolean): Unit + = declaration(attr.id) - override def attributeReader(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = { - attrName match { + override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = { + attr.id match { case ParentIdentifier | RootIdentifier => // just ignore it for now case _ => - out.puts(s"public function ${publicMemberName(attrName)}() { return ${privateMemberName(attrName)}; }") + out.puts(s"public function ${publicMemberName(attr.id)}() { return ${privateMemberName(attr.id)}; }") } } @@ -383,6 +377,9 @@ class PHPCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) override def switchEnd(): Unit = universalFooter + override def instanceDeclaration(instName: InstanceIdentifier, instSpec: InstanceSpec): Unit + = declaration(instName) + override def instanceHeader(className: List[String], instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = { out.puts(s"public function ${idToStr(instName)}() {") out.inc @@ -465,6 +462,15 @@ class PHPCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) } override def ksErrorName(err: KSError): String = PHPCompiler.ksErrorName(err) + + def declaration(attrName: Identifier): Unit = { + attrName match { + case ParentIdentifier | RootIdentifier | IoIdentifier => + // just ignore it for now + case _ => + out.puts(s"protected $$_m_${idToStr(attrName)};") + } + } } object PHPCompiler extends LanguageCompilerStatic diff --git a/shared/src/main/scala/io/kaitai/struct/languages/PerlCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/PerlCompiler.scala index 687eb6630..48d87a752 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/PerlCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/PerlCompiler.scala @@ -132,19 +132,19 @@ class PerlCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) override def readFooter(): Unit = universalFooter - override def attributeDeclaration(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = {} + override def attributeDeclaration(attr: MemberSpec, isNullable: Boolean): Unit = {} - override def attributeReader(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = { - attrName match { + override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = { + attr.id match { case RootIdentifier | ParentIdentifier => // ignore, they are already defined in KaitaiStruct class case _ => out.puts - out.puts(s"sub ${publicMemberName(attrName)} {") + out.puts(s"sub ${publicMemberName(attr.id)} {") out.inc out.puts("my ($self) = @_;") - out.puts(s"return ${privateMemberName(attrName)};") + out.puts(s"return ${privateMemberName(attr.id)};") universalFooter } @@ -356,6 +356,7 @@ class PerlCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) def onComparisonExpr(condition: Ast.expr) = Ast.expr.Compare(Ast.expr.Name(Ast.identifier("_on")), Ast.cmpop.Eq, condition) + override def instanceDeclaration(instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {} override def instanceHeader(className: List[String], instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = { out.puts out.puts(s"sub ${instName.name} {") diff --git a/shared/src/main/scala/io/kaitai/struct/languages/PythonCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/PythonCompiler.scala index cebccf72f..668701f7a 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/PythonCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/PythonCompiler.scala @@ -135,9 +135,9 @@ class PythonCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) override def readFooter() = universalFooter - override def attributeDeclaration(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = {} + override def attributeDeclaration(attr: MemberSpec, isNullable: Boolean): Unit = {} - override def attributeReader(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = {} + override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = {} override def universalDoc(doc: DocSpec): Unit = { val docStr = doc.summary match { @@ -419,6 +419,7 @@ class PythonCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) override def switchEnd(): Unit = {} + override def instanceDeclaration(instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {} override def instanceHeader(className: String, instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = { out.puts("@property") out.puts(s"def ${publicMemberName(instName)}(self):") diff --git a/shared/src/main/scala/io/kaitai/struct/languages/RubyCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/RubyCompiler.scala index 5b48413e9..54882aba7 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/RubyCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/RubyCompiler.scala @@ -137,14 +137,14 @@ class RubyCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) universalFooter } - override def attributeDeclaration(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = {} + override def attributeDeclaration(attr: MemberSpec, isNullable: Boolean): Unit = {} - override def attributeReader(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = { - attrName match { + override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = { + attr.id match { case RootIdentifier | ParentIdentifier => // ignore, they are already added in Kaitai::Struct::Struct case _ => - out.puts(s"attr_reader :${publicMemberName(attrName)}") + out.puts(s"attr_reader :${publicMemberName(attr.id)}") } } @@ -398,6 +398,7 @@ class RubyCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) override def switchEnd(): Unit = out.puts("end") + override def instanceDeclaration(instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = {} override def instanceHeader(className: String, instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = { out.puts(s"def ${instName.name}") out.inc diff --git a/shared/src/main/scala/io/kaitai/struct/languages/RustCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/RustCompiler.scala index f9599a935..847c658a1 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/RustCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/RustCompiler.scala @@ -134,18 +134,18 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) out.puts("}") } - override def attributeDeclaration(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = { - attrName match { + override def attributeDeclaration(attr: MemberSpec, isNullable: Boolean): Unit = { + attr.id match { case ParentIdentifier | RootIdentifier | IoIdentifier => // just ignore it for now case IoIdentifier => - out.puts(s" stream: ${kaitaiType2NativeType(attrType)},") + out.puts(s" stream: ${kaitaiType2NativeType(attr.dataTypeComposite)},") case _ => - out.puts(s" pub ${idToStr(attrName)}: ${kaitaiType2NativeType(attrType)},") + out.puts(s" pub ${idToStr(attr.id)}: ${kaitaiType2NativeType(attr.dataTypeComposite)},") } } - override def attributeReader(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = { + override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = { } diff --git a/shared/src/main/scala/io/kaitai/struct/languages/components/LanguageCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/components/LanguageCompiler.scala index fb3797aaa..d0646e145 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/components/LanguageCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/components/LanguageCompiler.scala @@ -86,8 +86,8 @@ abstract class LanguageCompiler( def readHeader(endian: Option[FixedEndian], isEmpty: Boolean): Unit def readFooter(): Unit - def attributeDeclaration(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit - def attributeReader(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit + def attributeDeclaration(attr: MemberSpec, isNullable: Boolean): Unit + def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit def attributeDoc(id: Identifier, doc: DocSpec): Unit = {} def attrParse(attr: AttrLikeSpec, id: Identifier, defEndian: Option[Endianness]): Unit @@ -124,7 +124,7 @@ abstract class LanguageCompiler( def instanceDeclHeader(className: List[String]): Unit = {} def instanceClear(instName: InstanceIdentifier): Unit = {} def instanceSetCalculated(instName: InstanceIdentifier): Unit = {} - def instanceDeclaration(instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = attributeDeclaration(instName, instSpec.dataTypeComposite, instSpec.isNullable) + def instanceDeclaration(instName: InstanceIdentifier, instSpec: InstanceSpec): Unit def instanceHeader(className: List[String], instName: InstanceIdentifier, instSpec: InstanceSpec): Unit def instanceFooter: Unit def instanceCheckCacheAndReturn(instName: InstanceIdentifier, dataType: DataType): Unit