From 699d10f9e45be72fe6cb39c7af63a506af4b31de Mon Sep 17 00:00:00 2001 From: Mingun Date: Sun, 23 Feb 2020 21:43:22 +0500 Subject: [PATCH] Java: Use new Span API --- .../kaitai/struct/languages/CppCompiler.scala | 2 +- .../struct/languages/JavaCompiler.scala | 47 +++++++++---------- .../struct/languages/JavaScriptCompiler.scala | 4 +- .../struct/languages/PythonCompiler.scala | 4 +- .../struct/languages/RubyCompiler.scala | 4 +- .../languages/components/CommonReads.scala | 8 ++-- .../components/EveryReadIsExpression.scala | 6 +-- 7 files changed, 36 insertions(+), 39 deletions(-) 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 3ce344cd7..35fffbf17 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala @@ -860,7 +860,7 @@ class CppCompiler( override def instanceCalculate(instName: Identifier, dataType: DataType, value: Ast.expr): Unit = { if (attrDebugNeeded(instName)) - attrDebugStart(instName, dataType, None, NoRepeat) + attrDebugStart(instName, dataType, NoRepeat, None, NoRepeat) val valExpr = expression(value) val isOwningInExpr = dataType match { case ct: ComplexDataType => ct.isOwningInExpr 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 d16922b80..41f8c4906 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/JavaCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/JavaCompiler.scala @@ -76,12 +76,10 @@ class JavaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) out.inc if (config.readStoresPos) { - out.puts("public Map _attrStart = new HashMap();") - out.puts("public Map _attrEnd = new HashMap();") - out.puts("public Map> _arrStart = new HashMap>();") - out.puts("public Map> _arrEnd = new HashMap>();") + out.puts("public final Map _spans = new HashMap();") out.puts + importList.add("io.kaitai.struct.Span") importList.add("java.util.ArrayList") importList.add("java.util.HashMap") importList.add("java.util.Map") @@ -314,14 +312,23 @@ class JavaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) override def alignToByte(io: String): Unit = out.puts(s"$io.alignToByte();") - override def attrDebugStart(attrId: Identifier, attrType: DataType, ios: Option[String], rep: RepeatSpec): Unit = { + override def attrDebugStart(attrId: Identifier, attrType: DataType, attrRep: RepeatSpec, ios: Option[String], rep: RepeatSpec): Unit = { ios.foreach { (io) => val name = idToStr(attrId) rep match { case NoRepeat => - out.puts("_attrStart.put(\"" + name + "\", " + io + ".pos());") + attrRep match { + case NoRepeat => + out.puts(s"final Span _s = new Span(${io});") + out.puts(s"""this._spans.put("${name}", _s);""") + importList.add("io.kaitai.struct.Span") + case _: RepeatExpr | RepeatEos | _: RepeatUntil => + out.puts(s"final ArraySpan _as = new ArraySpan(${io});") + out.puts(s"""this._spans.put("${name}", _as);""") + importList.add("io.kaitai.struct.ArraySpan") + } case _: RepeatExpr | RepeatEos | _: RepeatUntil => - getOrCreatePosList("_arrStart", name, io) + out.puts(s"final Span _is = _as.addItem(${io});") } } } @@ -330,31 +337,21 @@ class JavaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) // no _debug[$name]['arr'] initialization needed in Java } - override def attrDebugEnd(attrId: Identifier, attrType: DataType, io: String, rep: RepeatSpec): Unit = { + override def attrDebugEnd(attrId: Identifier, attrType: DataType, attrRep: RepeatSpec, io: String, rep: RepeatSpec): Unit = { val name = idToStr(attrId) rep match { case NoRepeat => - out.puts("_attrEnd.put(\"" + name + "\", " + io + ".pos());") + attrRep match { + case NoRepeat => + out.puts(s"_s.end = ${io}.pos();") + case _: RepeatExpr | RepeatEos | _: RepeatUntil => + out.puts(s"_as.end = ${io}.pos();") + } case _: RepeatExpr | RepeatEos | _: RepeatUntil => - getOrCreatePosList("_arrEnd", name, io) + out.puts(s"_is.end = ${io}.pos();") } } - def getOrCreatePosList(listName: String, varName: String, io: String): Unit = { - out.puts("{") - out.inc - out.puts("ArrayList _posList = " + listName + ".get(\"" + varName + "\");") - out.puts("if (_posList == null) {") - out.inc - out.puts("_posList = new ArrayList();") - out.puts(listName + ".put(\"" + varName + "\", _posList);") - out.dec - out.puts("}") - out.puts(s"_posList.add($io.pos());") - out.dec - out.puts("}") - } - override def condIfHeader(expr: expr): Unit = { out.puts(s"if (${expression(expr)}) {") out.inc 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 ff9adc063..caccde46a 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/JavaScriptCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/JavaScriptCompiler.scala @@ -259,7 +259,7 @@ class JavaScriptCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) override def alignToByte(io: String): Unit = out.puts(s"$io.alignToByte();") - override def attrDebugStart(attrId: Identifier, attrType: DataType, io: Option[String], rep: RepeatSpec): Unit = { + override def attrDebugStart(attrId: Identifier, attrType: DataType, attrRep: RepeatSpec, io: Option[String], rep: RepeatSpec): Unit = { val debugName = attrDebugName(attrId, rep, false) val ioProps = io match { @@ -278,7 +278,7 @@ class JavaScriptCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) override def attrDebugArrInit(id: Identifier, attrType: DataType): Unit = out.puts(s"this._debug.${idToStr(id)}.arr = [];") - override def attrDebugEnd(attrId: Identifier, attrType: DataType, io: String, rep: RepeatSpec): Unit = { + override def attrDebugEnd(attrId: Identifier, attrType: DataType, attrRep: RepeatSpec, io: String, rep: RepeatSpec): Unit = { val debugName = attrDebugName(attrId, rep, true) out.puts(s"$debugName.end = $io.pos;") 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 66ab783f0..e56ffba95 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/PythonCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/PythonCompiler.scala @@ -269,7 +269,7 @@ class PythonCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) override def alignToByte(io: String): Unit = out.puts(s"$io.align_to_byte()") - override def attrDebugStart(attrId: Identifier, attrType: DataType, ios: Option[String], rep: RepeatSpec): Unit = { + override def attrDebugStart(attrId: Identifier, attrType: DataType, attrRep: RepeatSpec, ios: Option[String], rep: RepeatSpec): Unit = { ios.foreach { (io) => val name = idToStr(attrId) rep match { @@ -284,7 +284,7 @@ class PythonCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) override def attrDebugArrInit(attrId: Identifier, attrType: DataType): Unit = out.puts(s"self._debug['${idToStr(attrId)}']['arr'] = []") - override def attrDebugEnd(attrId: Identifier, attrType: DataType, io: String, rep: RepeatSpec): Unit = { + override def attrDebugEnd(attrId: Identifier, attrType: DataType, attrRep: RepeatSpec, io: String, rep: RepeatSpec): Unit = { val name = idToStr(attrId) rep match { case NoRepeat => 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 6a4fa4ec7..32e4e54c9 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/RubyCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/RubyCompiler.scala @@ -241,7 +241,7 @@ class RubyCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) override def alignToByte(io: String): Unit = out.puts(s"$io.align_to_byte") - override def attrDebugStart(attrId: Identifier, attrType: DataType, ios: Option[String], rep: RepeatSpec): Unit = { + override def attrDebugStart(attrId: Identifier, attrType: DataType, attrRep: RepeatSpec, ios: Option[String], rep: RepeatSpec): Unit = { ios.foreach { (io) => val name = idToStr(attrId) rep match { @@ -258,7 +258,7 @@ class RubyCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig) override def attrDebugArrInit(attrId: Identifier, attrType: DataType): Unit = out.puts(s"@_debug['${idToStr(attrId)}'][:arr] = []") - override def attrDebugEnd(attrId: Identifier, attrType: DataType, io: String, rep: RepeatSpec): Unit = { + override def attrDebugEnd(attrId: Identifier, attrType: DataType, attrRep: RepeatSpec, io: String, rep: RepeatSpec): Unit = { val name = idToStr(attrId) rep match { case NoRepeat => diff --git a/shared/src/main/scala/io/kaitai/struct/languages/components/CommonReads.scala b/shared/src/main/scala/io/kaitai/struct/languages/components/CommonReads.scala index 0640b3af5..2fdfda17f 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/components/CommonReads.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/components/CommonReads.scala @@ -26,7 +26,7 @@ trait CommonReads extends LanguageCompiler { } if (attrDebugNeeded(id)) { - attrDebugStart(id, attr.dataType, Some(io), NoRepeat) + attrDebugStart(id, attr.dataType, attr.cond.repeat, Some(io), NoRepeat) if (attr.cond.repeat != NoRepeat) attrDebugArrInit(id, attr.dataType) } @@ -44,7 +44,7 @@ trait CommonReads extends LanguageCompiler { } if (config.readStoresPos) - attrDebugEnd(id, attr.dataType, io, NoRepeat) + attrDebugEnd(id, attr.dataType, attr.cond.repeat, io, NoRepeat) // More position management + set calculated flag after parsing for ParseInstanceSpecs attr match { @@ -90,9 +90,9 @@ trait CommonReads extends LanguageCompiler { def attrParse2(id: Identifier, dataType: DataType, io: String, rep: RepeatSpec, isRaw: Boolean, defEndian: Option[FixedEndian], assignType: Option[DataType] = None): Unit - def attrDebugStart(attrId: Identifier, attrType: DataType, io: Option[String], repeat: RepeatSpec): Unit = {} + def attrDebugStart(attrName: Identifier, attrType: DataType, attrRep: RepeatSpec, io: Option[String], repeat: RepeatSpec): Unit = {} def attrDebugArrInit(attrId: Identifier, attrType: DataType): Unit = {} - def attrDebugEnd(attrName: Identifier, attrType: DataType, io: String, repeat: RepeatSpec): Unit = {} + def attrDebugEnd(attrName: Identifier, attrType: DataType, attrRep: RepeatSpec, io: String, repeat: RepeatSpec): Unit = {} def attrDebugNeeded(attrId: Identifier): Boolean = false diff --git a/shared/src/main/scala/io/kaitai/struct/languages/components/EveryReadIsExpression.scala b/shared/src/main/scala/io/kaitai/struct/languages/components/EveryReadIsExpression.scala index 0d227a282..69e209d8d 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/components/EveryReadIsExpression.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/components/EveryReadIsExpression.scala @@ -34,7 +34,7 @@ trait EveryReadIsExpression val needsDebug = attrDebugNeeded(id) && rep != NoRepeat if (needsDebug) - attrDebugStart(id, dataType, Some(io), rep) + attrDebugStart(id, dataType, NoRepeat, Some(io), rep) dataType match { case t: UserType => @@ -63,7 +63,7 @@ trait EveryReadIsExpression } if (needsDebug) - attrDebugEnd(id, dataType, io, rep) + attrDebugEnd(id, dataType, NoRepeat, io, rep) } def attrBytesTypeParse( @@ -252,7 +252,7 @@ trait EveryReadIsExpression def instanceCalculate(instName: Identifier, dataType: DataType, value: Ast.expr): Unit = { if (attrDebugNeeded(instName)) - attrDebugStart(instName, dataType, None, NoRepeat) + attrDebugStart(instName, dataType, NoRepeat, None, NoRepeat) handleAssignmentSimple(instName, expression(value)) }