Skip to content

Commit

Permalink
#22: Forward attribute indexes to language compilers
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Jan 13, 2020
1 parent dfc8628 commit 33d99ef
Show file tree
Hide file tree
Showing 15 changed files with 41 additions and 41 deletions.
26 changes: 13 additions & 13 deletions shared/src/main/scala/io/kaitai/struct/ClassCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ class ClassCompiler(
compileInstances(curClass)

// Attributes declarations and readers
val allAttrs: List[MemberSpec] =
curClass.seq ++
curClass.params ++
List(
val allAttrs: List[(MemberSpec, Int)] =
curClass.seq.zipWithIndex ++
curClass.params.zipWithIndex ++
(List(
AttrSpec(List(), RootIdentifier, CalcUserType(topClassName, None)),
AttrSpec(List(), ParentIdentifier, curClass.parentType)
) ++
ExtraAttrs.forClassSpec(curClass, lang)
ExtraAttrs.forClassSpec(curClass, lang)).zipWithIndex
compileAttrDeclarations(allAttrs)
compileAttrReaders(allAttrs)

Expand Down Expand Up @@ -184,28 +184,28 @@ class ClassCompiler(
}

/**
* Iterates over a given list of attributes and generates attribute
* Iterates over a given list of attributes and their indexes and generates attribute
* declarations for each of them.
* @param attrs attribute list to traverse
*/
def compileAttrDeclarations(attrs: List[MemberSpec]): Unit = {
attrs.foreach { (attr) =>
def compileAttrDeclarations(attrs: List[(MemberSpec, Int)]): Unit = {
attrs.foreach { case (attr, i) =>
val isNullable = if (lang.switchBytesOnlyAsRaw) {
attr.isNullableSwitchRaw
} else {
attr.isNullable
}
lang.attributeDeclaration(attr, isNullable)
lang.attributeDeclaration(attr, i, isNullable)
}
}

/**
* Iterates over a given list of attributes and generates attribute
* Iterates over a given list of attributes and their indexes and generates attribute
* readers (AKA getters) for each of them.
* @param attrs attribute list to traverse
*/
def compileAttrReaders(attrs: List[MemberSpec]): Unit = {
attrs.foreach { (attr) =>
def compileAttrReaders(attrs: List[(MemberSpec, Int)]): Unit = {
attrs.foreach { case (attr, i) =>
// FIXME: Python should have some form of attribute docs too
if (!attr.doc.isEmpty && !lang.innerDocstrings)
lang.attributeDoc(attr.id, attr.doc)
Expand All @@ -214,7 +214,7 @@ class ClassCompiler(
} else {
attr.isNullable
}
lang.attributeReader(attr, isNullable)
lang.attributeReader(attr, i, isNullable)
}
}

Expand Down
4 changes: 2 additions & 2 deletions shared/src/main/scala/io/kaitai/struct/GoClassCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class GoClassCompiler(

// Basic struct declaration
lang.classHeader(curClass.name)
compileAttrDeclarations(curClass.seq ++ extraAttrs)
compileAttrDeclarations(curClass.seq.zipWithIndex ++ extraAttrs.zipWithIndex)
curClass.instances.foreach { case (instName, instSpec) =>
compileInstanceDeclaration(instName, instSpec)
}
Expand All @@ -41,7 +41,7 @@ class GoClassCompiler(

compileInstances(curClass)

compileAttrReaders(curClass.seq ++ extraAttrs)
compileAttrReaders(curClass.seq.zipWithIndex ++ extraAttrs.zipWithIndex)

// Recursive types
compileSubclasses(curClass)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class RustClassCompiler(
// Basic struct declaration
lang.classHeader(curClass.name)

compileAttrDeclarations(curClass.seq ++ extraAttrs)
compileAttrDeclarations(curClass.seq.zipWithIndex ++ extraAttrs.zipWithIndex)
curClass.instances.foreach { case (instName, instSpec) =>
compileInstanceDeclaration(instName, instSpec)
}
Expand All @@ -40,7 +40,7 @@ class RustClassCompiler(

compileInstances(curClass)

compileAttrReaders(curClass.seq ++ extraAttrs)
compileAttrReaders(curClass.seq.zipWithIndex ++ extraAttrs.zipWithIndex)
lang.classFooter(curClass.name)

compileEnums(curClass)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ class CSharpCompiler(val typeProvider: ClassTypeProvider, config: RuntimeConfig)

override def readFooter(): Unit = fileFooter("")

override def attributeDeclaration(attr: MemberSpec, isNullable: Boolean): Unit = {
override def attributeDeclaration(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = {
out.puts(s"private ${kaitaiType2NativeTypeNullable(attr.dataTypeComposite, isNullable)} ${privateMemberName(attr.id)};")
}

override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = {
override def attributeReader(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = {
out.puts(s"public ${kaitaiType2NativeTypeNullable(attr.dataTypeComposite, isNullable)} ${publicMemberName(attr.id)} { get { return ${privateMemberName(attr.id)}; } }")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class CppCompiler(
ensureMode(PublicAccess)
}

override def attributeDeclaration(attr: MemberSpec, isNullable: Boolean): Unit = {
override def attributeDeclaration(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = {
ensureMode(PrivateAccess)
outHdr.puts(s"${kaitaiType2NativeType(attr.dataTypeComposite)} ${privateMemberName(attr.id)};")
declareNullFlag(attr.id, isNullable)
Expand All @@ -316,7 +316,7 @@ class CppCompiler(
}
}

override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = {
override def attributeReader(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = {
ensureMode(PublicAccess)
outHdr.puts(s"${kaitaiType2NativeType(attr.dataTypeComposite.asNonOwning)} ${publicMemberName(attr.id)}() const { return ${nonOwningPointer(attr.id, attr.dataTypeComposite)}; }")
}
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(attr: MemberSpec, isNullable: Boolean): Unit = {
override def attributeDeclaration(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = {
out.puts(s"${idToStr(attr.id)} ${kaitaiType2NativeType(attr.dataTypeComposite)}")
translator.returnRes = None
}

override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = {}
override def attributeReader(attr: MemberSpec, index: Int, 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,11 +197,11 @@ class JavaCompiler(val typeProvider: ClassTypeProvider, config: RuntimeConfig)

override def readFooter(): Unit = universalFooter

override def attributeDeclaration(attr: MemberSpec, isNullable: Boolean): Unit = {
override def attributeDeclaration(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = {
out.puts(s"private ${kaitaiType2JavaType(attr.dataTypeComposite, isNullable)} ${idToStr(attr.id)};")
}

override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = {
override def attributeReader(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = {
out.puts(s"public ${kaitaiType2JavaType(attr.dataTypeComposite, isNullable)} ${idToStr(attr.id)}() { return ${idToStr(attr.id)}; }")
}

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(attr: MemberSpec, isNullable: Boolean): Unit = {}
override def attributeDeclaration(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = {}

override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = {}
override def attributeReader(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = {}

override def universalDoc(doc: DocSpec): Unit = {
// JSDoc docstring style: http://usejsdoc.org/about-getting-started.html
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(attr: MemberSpec, isNullable: Boolean): Unit =
override def attributeDeclaration(attr: MemberSpec, index: Int, isNullable: Boolean): Unit =
{}
override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit =
override def attributeReader(attr: MemberSpec, index: Int, isNullable: Boolean): Unit =
{}

override def attrParseHybrid(leProc: () => Unit, beProc: () => Unit): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ class PHPCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

override def readFooter(): Unit = universalFooter

override def attributeDeclaration(attr: MemberSpec, isNullable: Boolean): Unit
override def attributeDeclaration(attr: MemberSpec, index: Int, isNullable: Boolean): Unit
= declaration(attr.id)

override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = {
override def attributeReader(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = {
attr.id match {
case ParentIdentifier | RootIdentifier =>
// just ignore it for now
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ class PerlCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

override def readFooter(): Unit = universalFooter

override def attributeDeclaration(attr: MemberSpec, isNullable: Boolean): Unit = {}
override def attributeDeclaration(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = {}

override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = {
override def attributeReader(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = {
attr.id match {
case RootIdentifier | ParentIdentifier =>
// ignore, they are already defined in KaitaiStruct class
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(attr: MemberSpec, isNullable: Boolean): Unit = {}
override def attributeDeclaration(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = {}

override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = {}
override def attributeReader(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = {}

override def universalDoc(doc: DocSpec): Unit = {
val docStr = doc.summary match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ class RubyCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
universalFooter
}

override def attributeDeclaration(attr: MemberSpec, isNullable: Boolean): Unit = {}
override def attributeDeclaration(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = {}

override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = {
override def attributeReader(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = {
attr.id match {
case RootIdentifier | ParentIdentifier =>
// ignore, they are already added in Kaitai::Struct::Struct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
out.puts("}")
}

override def attributeDeclaration(attr: MemberSpec, isNullable: Boolean): Unit = {
override def attributeDeclaration(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = {
attr.id match {
case ParentIdentifier | RootIdentifier | IoIdentifier =>
// just ignore it for now
Expand All @@ -145,7 +145,7 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
}
}

override def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit = {
override def attributeReader(attr: MemberSpec, index: Int, 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(attr: MemberSpec, isNullable: Boolean): Unit
def attributeReader(attr: MemberSpec, isNullable: Boolean): Unit
def attributeDeclaration(attr: MemberSpec, index: Int, isNullable: Boolean): Unit
def attributeReader(attr: MemberSpec, index: Int, isNullable: Boolean): Unit
def attributeDoc(id: Identifier, doc: DocSpec): Unit = {}

def attrParse(attr: AttrLikeSpec, id: Identifier, defEndian: Option[Endianness]): Unit
Expand Down

0 comments on commit 33d99ef

Please sign in to comment.