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 Oct 24, 2023
1 parent 71d1ed3 commit 699d10f
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 @@ -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
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 @@ -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 {
Expand All @@ -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;")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down Expand Up @@ -63,7 +63,7 @@ trait EveryReadIsExpression
}

if (needsDebug)
attrDebugEnd(id, dataType, io, rep)
attrDebugEnd(id, dataType, NoRepeat, io, rep)
}

def attrBytesTypeParse(
Expand Down Expand Up @@ -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))
}

Expand Down

0 comments on commit 699d10f

Please sign in to comment.