Skip to content

Commit

Permalink
#22: Forward full attribute data to compilers
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Jan 11, 2020
1 parent c76750e commit dfc8628
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 52 deletions.
4 changes: 2 additions & 2 deletions shared/src/main/scala/io/kaitai/struct/ClassCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class ClassCompiler(
} else {
attr.isNullable
}
lang.attributeDeclaration(attr.id, attr.dataTypeComposite, isNullable)
lang.attributeDeclaration(attr, isNullable)
}
}

Expand All @@ -214,7 +214,7 @@ class ClassCompiler(
} else {
attr.isNullable
}
lang.attributeReader(attr.id, attr.dataTypeComposite, isNullable)
lang.attributeReader(attr, isNullable)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -487,6 +487,7 @@ class JavaScriptCompiler(val typeProvider: ClassTypeProvider, config: RuntimeCon

//</editor-fold>

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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()")
Expand Down
28 changes: 17 additions & 11 deletions shared/src/main/scala/io/kaitai/struct/languages/PHPCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)}; }")
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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} {")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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):")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)}")
}
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dfc8628

Please sign in to comment.