Skip to content

Commit

Permalink
Rename Identifier constants: ITERATOR -> THIS, ITERATOR2 -> THIS_RAW
Browse files Browse the repository at this point in the history
The new names better reflects purpose and that fact, that variable `_` (ITERATOR)
used not only as reference to the last parsed object in loops, but also as current
object in other contexts
  • Loading branch information
Mingun committed Jul 16, 2024
1 parent efae9a6 commit a216e8e
Showing 25 changed files with 50 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ class ClassTypeProvider(classSpecs: ClassSpecs, var topClass: ClassSpec) extends
inClass.parentClass.toDataType
case Identifier.IO =>
KaitaiStreamType
case Identifier.ITERATOR =>
case Identifier.THIS =>
currentIteratorType
case Identifier.SWITCH_ON =>
currentSwitchType
Original file line number Diff line number Diff line change
@@ -87,8 +87,8 @@ object Identifier {
val ROOT = "_root"
val PARENT = "_parent"
val IO = "_io"
val ITERATOR = "_"
val ITERATOR2 = "_buf"
val THIS = "_"
val THIS_RAW = "_buf"
val INDEX = "_index"
val SWITCH_ON = "_on"
val IS_LE = "_is_le"
Original file line number Diff line number Diff line change
@@ -226,7 +226,7 @@ class CSharpCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
val ioName = s"io_$privateVarName"

val args = rep match {
case RepeatUntil(_) => translator.doName(Identifier.ITERATOR2)
case RepeatUntil(_) => translator.doName(Identifier.THIS_RAW)
case _ => getRawIdExpr(varName, rep)
}

@@ -322,9 +322,9 @@ class CSharpCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

override def handleAssignmentRepeatUntil(id: Identifier, expr: String, isRaw: Boolean): Unit = {
val (typeDecl, tempVar) = if (isRaw) {
("byte[] ", translator.doName(Identifier.ITERATOR2))
("byte[] ", translator.doName(Identifier.THIS_RAW))
} else {
("", translator.doName(Identifier.ITERATOR))
("", translator.doName(Identifier.THIS))
}
out.puts(s"$typeDecl$tempVar = $expr;")
out.puts(s"${privateMemberName(id)}.Add($tempVar);")
Original file line number Diff line number Diff line change
@@ -483,7 +483,7 @@ class CppCompiler(
val ioId = IoStorageIdentifier(id)

val args = rep match {
case RepeatUntil(_) => translator.doName(Identifier.ITERATOR2)
case RepeatUntil(_) => translator.doName(Identifier.THIS_RAW)
case _ => getRawIdExpr(id, rep)
}

@@ -619,9 +619,9 @@ class CppCompiler(

override def handleAssignmentRepeatUntil(id: Identifier, expr: String, isRaw: Boolean): Unit = {
val (typeDecl, tempVar) = if (isRaw) {
("std::string ", translator.doName(Identifier.ITERATOR2))
("std::string ", translator.doName(Identifier.THIS_RAW))
} else {
("", translator.doName(Identifier.ITERATOR))
("", translator.doName(Identifier.THIS))
}

val (wrappedTempVar, rawPtrExpr) = if (config.cppConfig.pointers == UniqueAndRawPointers) {
Original file line number Diff line number Diff line change
@@ -244,7 +244,7 @@ class GoCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
val ioName = idToStr(IoStorageIdentifier(varName))

val args = rep match {
case RepeatUntil(_) => translator.specialName(Identifier.ITERATOR2)
case RepeatUntil(_) => translator.specialName(Identifier.THIS_RAW)
case _ => getRawIdExpr(varName, rep)
}

@@ -341,7 +341,7 @@ class GoCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

override def handleAssignmentRepeatUntil(id: Identifier, r: TranslatorResult, isRaw: Boolean): Unit = {
val expr = translator.resToStr(r)
val tempVar = translator.specialName(if (isRaw) Identifier.ITERATOR2 else Identifier.ITERATOR)
val tempVar = translator.specialName(if (isRaw) Identifier.THIS_RAW else Identifier.THIS)
out.puts(s"$tempVar := $expr")
out.puts(s"${privateMemberName(id)} = append(${privateMemberName(id)}, $tempVar)")
}
Original file line number Diff line number Diff line change
@@ -272,7 +272,7 @@ class JavaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
val ioName = idToStr(IoStorageIdentifier(varName))

val args = rep match {
case RepeatUntil(_) => translator.doName(Identifier.ITERATOR2)
case RepeatUntil(_) => translator.doName(Identifier.THIS_RAW)
case _ => getRawIdExpr(varName, rep)
}

@@ -400,9 +400,9 @@ class JavaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

override def handleAssignmentRepeatUntil(id: Identifier, expr: String, isRaw: Boolean): Unit = {
val (typeDecl, tempVar) = if (isRaw) {
("byte[] ", translator.doName(Identifier.ITERATOR2))
("byte[] ", translator.doName(Identifier.THIS_RAW))
} else {
("", translator.doName(Identifier.ITERATOR))
("", translator.doName(Identifier.THIS))
}
out.puts(s"$typeDecl$tempVar = $expr;")
out.puts(s"${privateMemberName(id)}.add($tempVar);")
Original file line number Diff line number Diff line change
@@ -339,7 +339,7 @@ class JavaScriptCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
}

override def handleAssignmentRepeatUntil(id: Identifier, expr: String, isRaw: Boolean): Unit = {
val tmpName = translator.doName(if (isRaw) Identifier.ITERATOR2 else Identifier.ITERATOR)
val tmpName = translator.doName(if (isRaw) Identifier.THIS_RAW else Identifier.THIS)
out.puts(s"var $tmpName = $expr;")
out.puts(s"${privateMemberName(id)}.push($tmpName);")
}
Original file line number Diff line number Diff line change
@@ -309,7 +309,7 @@ class LuaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
override def handleAssignmentRepeatExpr(id: Identifier, expr: String): Unit =
out.puts(s"${privateMemberName(id)}[i + 1] = $expr")
override def handleAssignmentRepeatUntil(id: Identifier, expr: String, isRaw: Boolean): Unit = {
val tmpName = translator.doName(if (isRaw) Identifier.ITERATOR2 else Identifier.ITERATOR)
val tmpName = translator.doName(if (isRaw) Identifier.THIS_RAW else Identifier.THIS)
out.puts(s"local $tmpName = $expr")
out.puts(s"${privateMemberName(id)}[i + 1] = $tmpName")
}
Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@ class NimCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
val ioName = s"${idToStr(id)}Io"
val arg = rep match {
case NoRepeat => idToStr(id) + "Expr"
case _ => translator.doName(Identifier.ITERATOR2)
case _ => translator.doName(Identifier.THIS_RAW)
}
out.puts(s"let $ioName = newKaitaiStream($arg)")
ioName
@@ -363,8 +363,8 @@ class NimCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
def handleAssignmentIterative(id: Identifier, expr: String): Unit = {
// Need better design for this XXX
val exprName = id match {
case _: RawIdentifier => translator.doName(Identifier.ITERATOR2)
case _ => translator.doName(Identifier.ITERATOR)
case _: RawIdentifier => translator.doName(Identifier.THIS_RAW)
case _ => translator.doName(Identifier.THIS)
}
out.puts(s"let $exprName = $expr")
out.puts(s"${privateMemberName(id)}.add($exprName)")
Original file line number Diff line number Diff line change
@@ -241,7 +241,7 @@ class PHPCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
val ioName = s"$$_io_${idToStr(id)}"

val args = rep match {
case RepeatUntil(_) => translator.doLocalName(Identifier.ITERATOR2)
case RepeatUntil(_) => translator.doLocalName(Identifier.THIS_RAW)
case _ => getRawIdExpr(id, rep)
}

@@ -313,7 +313,7 @@ class PHPCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
}

override def handleAssignmentRepeatUntil(id: Identifier, expr: String, isRaw: Boolean): Unit = {
val tmpName = translator.doLocalName(if (isRaw) Identifier.ITERATOR2 else Identifier.ITERATOR)
val tmpName = translator.doLocalName(if (isRaw) Identifier.THIS_RAW else Identifier.THIS)
out.puts(s"$tmpName = $expr;")
out.puts(s"${privateMemberName(id)}[] = $tmpName;")
}
Original file line number Diff line number Diff line change
@@ -202,7 +202,7 @@ class PerlCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
val memberName = privateMemberName(id)

val args = rep match {
case RepeatUntil(_) => translator.doName(Identifier.ITERATOR2)
case RepeatUntil(_) => translator.doName(Identifier.THIS_RAW)
case _ => getRawIdExpr(id, rep)
}

@@ -271,9 +271,9 @@ class PerlCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

override def handleAssignmentRepeatUntil(id: Identifier, expr: String, isRaw: Boolean): Unit = {
val (decl, tmpName) = if (isRaw) {
("my ", translator.doName(Identifier.ITERATOR2))
("my ", translator.doName(Identifier.THIS_RAW))
} else {
("", translator.doName(Identifier.ITERATOR))
("", translator.doName(Identifier.THIS))
}
out.puts(s"$decl$tmpName = $expr;")
out.puts(s"push @{${privateMemberName(id)}}, $tmpName;")
Original file line number Diff line number Diff line change
@@ -327,7 +327,7 @@ class PythonCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
}

override def handleAssignmentRepeatUntil(id: Identifier, expr: String, isRaw: Boolean): Unit = {
val tmpName = translator.doName(if (isRaw) Identifier.ITERATOR2 else Identifier.ITERATOR)
val tmpName = translator.doName(if (isRaw) Identifier.THIS_RAW else Identifier.THIS)
out.puts(s"$tmpName = $expr")
out.puts(s"${privateMemberName(id)}.append($tmpName)")
}
Original file line number Diff line number Diff line change
@@ -338,7 +338,7 @@ class RubyCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
}

override def handleAssignmentRepeatUntil(id: Identifier, expr: String, isRaw: Boolean): Unit = {
val tmpName = translator.doName(if (isRaw) Identifier.ITERATOR2 else Identifier.ITERATOR)
val tmpName = translator.doName(if (isRaw) Identifier.THIS_RAW else Identifier.THIS)
out.puts(s"$tmpName = $expr")
out.puts(s"${privateMemberName(id)} << $tmpName")
}
Original file line number Diff line number Diff line change
@@ -215,7 +215,7 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
val memberName = privateMemberName(id)

val args = rep match {
case RepeatUntil(_) => translator.doLocalName(Identifier.ITERATOR2)
case RepeatUntil(_) => translator.doLocalName(Identifier.THIS_RAW)
case _ => getRawIdExpr(id, rep)
}

@@ -284,9 +284,9 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

override def handleAssignmentRepeatUntil(id: Identifier, expr: String, isRaw: Boolean): Unit = {
val tempVar = if (isRaw) {
translator.doLocalName(Identifier.ITERATOR2)
translator.doLocalName(Identifier.THIS_RAW)
} else {
translator.doLocalName(Identifier.ITERATOR)
translator.doLocalName(Identifier.THIS)
}
out.puts(s"let $tempVar = $expr;")
out.puts(s"${privateMemberName(id)}.append($tempVar);")
Original file line number Diff line number Diff line change
@@ -57,7 +57,7 @@ trait ValidateOps extends ExceptionNames {
// using this variable
handleAssignmentTempVar(
attrTypeRef,
translator.translate(Ast.expr.Name(Ast.identifier(Identifier.ITERATOR))),
translator.translate(Ast.expr.Name(Ast.identifier(Identifier.THIS))),
translator.translate(itemValue)
)
attrValidateExpr(
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import io.kaitai.struct.ConstructClassCompiler
class ConstructTranslator(provider: TypeProvider, importList: ImportList) extends PythonTranslator(provider, importList, RuntimeConfig()) {
override def doLocalName(s: String) = {
s match {
case Identifier.ITERATOR => "obj_"
case Identifier.THIS => "obj_"
case Identifier.INDEX => "i"
case Identifier.ROOT => "this._root"
case Identifier.IO => "_io"
Original file line number Diff line number Diff line change
@@ -133,8 +133,8 @@ class CppTranslator(provider: TypeProvider, importListSrc: CppImportList, import
s"${translate(value)}->${doName(attrName)}"

override def doName(s: String) = s match {
case Identifier.ITERATOR => "_"
case Identifier.ITERATOR2 => "_buf"
case Identifier.THIS => "_"
case Identifier.THIS_RAW => "_buf"
case Identifier.INDEX => "i"
case _ => s"$s()"
}
Original file line number Diff line number Diff line change
@@ -208,8 +208,8 @@ class GoTranslator(out: StringLanguageOutputWriter, provider: TypeProvider, impo
ResultString(s"this.${specialName(s)}")

// These can be local only
case Identifier.ITERATOR |
Identifier.ITERATOR2 =>
case Identifier.THIS |
Identifier.THIS_RAW =>
ResultString(specialName(s))
case Identifier.INDEX => ResultString("i")

@@ -228,9 +228,9 @@ class GoTranslator(out: StringLanguageOutputWriter, provider: TypeProvider, impo
def specialName(id: String): String = id match {
case Identifier.ROOT | Identifier.PARENT | Identifier.IO =>
id
case Identifier.ITERATOR =>
case Identifier.THIS =>
"_it"
case Identifier.ITERATOR2 =>
case Identifier.THIS_RAW =>
"_buf"
}

Original file line number Diff line number Diff line change
@@ -56,8 +56,8 @@ class JavaTranslator(provider: TypeProvider, importList: ImportList) extends Bas

override def doName(s: String) =
s match {
case Identifier.ITERATOR => "_it"
case Identifier.ITERATOR2 => "_buf"
case Identifier.THIS => "_it"
case Identifier.THIS_RAW => "_buf"
case Identifier.SWITCH_ON => "on"
case Identifier.INDEX => "i"
case _ => s"${Utils.lowerCamelCase(s)}()"
Original file line number Diff line number Diff line change
@@ -73,7 +73,7 @@ class LuaTranslator(provider: TypeProvider, importList: ImportList) extends Base
"\"" + decEscapeByteArray(arr) + "\""

override def doLocalName(s: String) = s match {
case Identifier.ITERATOR => "_"
case Identifier.THIS => "_"
case Identifier.INDEX => "i"
case _ => s"self.${doName(s)}"
}
Original file line number Diff line number Diff line change
@@ -22,8 +22,8 @@ class NimTranslator(provider: TypeProvider, importList: ImportList) extends Base
case Identifier.ROOT => "root"
case Identifier.PARENT => "parent"
case Identifier.IO => "io"
case Identifier.ITERATOR => "it"
case Identifier.ITERATOR2 => "buf"
case Identifier.THIS => "it"
case Identifier.THIS_RAW => "buf"
case Identifier.INDEX => "i"
case Identifier.SWITCH_ON => "on"
case Identifier.IS_LE => "isLe"
@@ -32,7 +32,7 @@ class NimTranslator(provider: TypeProvider, importList: ImportList) extends Base
}
override def doLocalName(s: String): String =
s match {
case Identifier.ITERATOR => doName(s)
case Identifier.THIS => doName(s)
case Identifier.INDEX => doName(s)
case Identifier.ROOT => s"${ksToNim(provider.determineType(Identifier.ROOT))}(this.${doName(s)})"
case _ => s"this.${doName(s)}"
Original file line number Diff line number Diff line change
@@ -58,8 +58,8 @@ class PHPTranslator(provider: TypeProvider, config: RuntimeConfig) extends BaseT

override def doLocalName(s: String) = {
s match {
case Identifier.ITERATOR => "$_"
case Identifier.ITERATOR2 => "$_buf"
case Identifier.THIS => "$_"
case Identifier.THIS_RAW => "$_buf"
case Identifier.INDEX => "$i"
case _ => s"$$this->${doName(s)}"
}
Original file line number Diff line number Diff line change
@@ -66,8 +66,8 @@ class PerlTranslator(provider: TypeProvider, importList: ImportList) extends Bas

override def doName(s: String) = {
s match {
case Identifier.ITERATOR => "$_"
case Identifier.ITERATOR2 => "$_buf"
case Identifier.THIS => "$_"
case Identifier.THIS_RAW => "$_buf"
case Identifier.INDEX => "$i"
case _ => s"$s()"
}
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@ class PythonTranslator(provider: TypeProvider, importList: ImportList, config: R

override def doLocalName(s: String) = {
s match {
case Identifier.ITERATOR => "_"
case Identifier.THIS => "_"
case Identifier.INDEX => "i"
case _ => s"self.${doName(s)}"
}
Original file line number Diff line number Diff line change
@@ -28,8 +28,8 @@ class RustTranslator(provider: TypeProvider, config: RuntimeConfig) extends Base

override def doLocalName(s: String) = {
s match {
case Identifier.ITERATOR => "tmpa"
case Identifier.ITERATOR2 => "tmpb"
case Identifier.THIS => "tmpa"
case Identifier.THIS_RAW => "tmpb"
case Identifier.INDEX => "i"
case _ => s"self.${doName(s)}"
}

0 comments on commit a216e8e

Please sign in to comment.