Skip to content

Commit

Permalink
Java: Use new Span API
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Sep 8, 2024
1 parent 6d34b9e commit a0ec4b2
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,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
Expand Down
47 changes: 22 additions & 25 deletions shared/src/main/scala/io/kaitai/struct/languages/JavaCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,10 @@ class JavaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
out.inc

if (config.readStoresPos) {
out.puts("public Map<String, Integer> _attrStart = new HashMap<String, Integer>();")
out.puts("public Map<String, Integer> _attrEnd = new HashMap<String, Integer>();")
out.puts("public Map<String, ArrayList<Integer>> _arrStart = new HashMap<String, ArrayList<Integer>>();")
out.puts("public Map<String, ArrayList<Integer>> _arrEnd = new HashMap<String, ArrayList<Integer>>();")
out.puts("public final Map<String, Span> _spans = new HashMap<String, Span>();")
out.puts

importList.add("io.kaitai.struct.Span")
importList.add("java.util.ArrayList")
importList.add("java.util.HashMap")
importList.add("java.util.Map")
Expand Down Expand Up @@ -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});")
}
}
}
Expand All @@ -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<Integer> _posList = " + listName + ".get(\"" + varName + "\");")
out.puts("if (_posList == null) {")
out.inc
out.puts("_posList = new ArrayList<Integer>();")
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,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 {
Expand All @@ -283,7 +283,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;")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,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 {
Expand All @@ -280,7 +280,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 =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,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 {
Expand All @@ -286,7 +286,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 =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ trait CommonReads extends LanguageCompiler {
val needsArrayDebug = attrDebugNeeded(id) && attr.cond.repeat != NoRepeat

if (needsArrayDebug) {
attrDebugStart(id, attr.dataType, Some(io), NoRepeat)
attrDebugStart(id, attr.dataType, attr.cond.repeat, Some(io), NoRepeat)
attrDebugArrInit(id, attr.dataType)
}

Expand All @@ -45,7 +45,7 @@ trait CommonReads extends LanguageCompiler {
}

if (needsArrayDebug)
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 {
Expand Down Expand Up @@ -74,12 +74,12 @@ trait CommonReads extends LanguageCompiler {

val needsDebug = attrDebugNeeded(id)
if (needsDebug)
attrDebugStart(id, attr.dataType, Some(io), attr.cond.repeat)
attrDebugStart(id, attr.dataType, attr.cond.repeat, Some(io), attr.cond.repeat)

attrParse2(id, attr.dataType, io, attr.cond.repeat, false, defEndian)

if (needsDebug)
attrDebugEnd(id, attr.dataType, io, attr.cond.repeat)
attrDebugEnd(id, attr.dataType, attr.cond.repeat, io, attr.cond.repeat)

attrValidateAll(attr)
attr.cond.repeat match {
Expand All @@ -95,9 +95,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 = {
if (!config.readStoresPos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,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))
}
}

0 comments on commit a0ec4b2

Please sign in to comment.