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 Oct 4, 2024
1 parent e236c71 commit 8026573
Show file tree
Hide file tree
Showing 25 changed files with 48 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

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

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

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

Expand Down Expand Up @@ -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);")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -369,8 +369,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)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

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

Expand Down Expand Up @@ -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;")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
}
val t = localTemporaryName(id)
out.puts(s"let $t = ${privateMemberName(id)};")
out.puts(s"let ${translator.doLocalName(Identifier.ITERATOR)} = $copy_type$t.last().unwrap();")
out.puts(s"let ${translator.doLocalName(Identifier.THIS)} = $copy_type$t.last().unwrap();")
}

override def condRepeatUntilFooter(id: Identifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,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()"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)}()"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class LuaTranslator(provider: TypeProvider, importList: ImportList) extends Base
"string.char(" + values.map(translate).mkString(", ") + ")"

override def doLocalName(s: String) = s match {
case Identifier.ITERATOR => "_"
case Identifier.THIS => "_"
case Identifier.INDEX => "i"
case _ => s"self.${doName(s)}"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ class RustTranslator(provider: TypeProvider, config: RuntimeConfig)
}

override def doLocalName(s: String): 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 Identifier.IO => s"${RustCompiler.privateMemberName(IoIdentifier)}"
case Identifier.ROOT => "_r"
Expand Down

0 comments on commit 8026573

Please sign in to comment.