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 May 11, 2021
1 parent 9e8570d commit 185eaef
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 38 deletions.
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 @@ -316,49 +314,48 @@ 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 = attrId match {
case _: RawIdentifier | _: SpecialIdentifier => return
case _ => 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});")
}
}
}

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 = attrId match {
case _: RawIdentifier | _: SpecialIdentifier => return
case _ => 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 = {
if (!attrDebugNeeded(attrId))
return

Expand All @@ -278,7 +278,7 @@ class JavaScriptCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
out.puts(s"$debugName = { $ioProps${if (ioProps != "" && enumNameProps != "") ", " else ""}$enumNameProps };")
}

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 = {
if (!attrDebugNeeded(attrId))
return
val debugName = attrDebugName(attrId, rep, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,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 = attrId match {
case _: RawIdentifier | _: SpecialIdentifier => return
Expand All @@ -277,7 +277,7 @@ class PythonCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
}
}

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 = attrId match {
case _: RawIdentifier | _: SpecialIdentifier => return
case _ => idToStr(attrId)
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 = attrId match {
case _: RawIdentifier | _: SpecialIdentifier => return
Expand All @@ -258,7 +258,7 @@ class RubyCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
}
}

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 = attrId match {
case _: RawIdentifier | _: SpecialIdentifier => return
case _ => idToStr(attrId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ trait CommonReads extends LanguageCompiler {
}

if (config.readStoresPos)
attrDebugStart(id, attr.dataType, Some(io), NoRepeat)
attrDebugStart(id, attr.dataType, attr.cond.repeat, Some(io), NoRepeat)

defEndian match {
case Some(_: CalcEndian) | Some(InheritedEndian) =>
Expand All @@ -42,7 +42,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 @@ -102,8 +102,8 @@ trait CommonReads extends LanguageCompiler {
}
}

def attrDebugStart(attrName: Identifier, attrType: DataType, io: Option[String], repeat: RepeatSpec): Unit = {}
def attrDebugEnd(attrName: Identifier, attrType: DataType, io: String, repeat: RepeatSpec): Unit = {}
def attrDebugStart(attrName: Identifier, attrType: DataType, attrRep: RepeatSpec, io: Option[String], repeat: RepeatSpec): Unit = {}
def attrDebugEnd(attrName: Identifier, attrType: DataType, attrRep: RepeatSpec, io: String, repeat: RepeatSpec): Unit = {}

/**
* Runs all validation procedures requested for an attribute.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ trait EveryReadIsExpression
val assignType = assignTypeOpt.getOrElse(dataType)

if (config.readStoresPos && rep != NoRepeat)
attrDebugStart(id, dataType, Some(io), rep)
attrDebugStart(id, dataType, NoRepeat, Some(io), rep)

dataType match {
case t: UserType =>
Expand Down Expand Up @@ -62,7 +62,7 @@ trait EveryReadIsExpression
}

if (config.readStoresPos && rep != NoRepeat)
attrDebugEnd(id, dataType, io, rep)
attrDebugEnd(id, dataType, NoRepeat, io, rep)
}

def attrBytesTypeParse(
Expand Down Expand Up @@ -197,7 +197,7 @@ trait EveryReadIsExpression

def instanceCalculate(instName: Identifier, dataType: DataType, value: Ast.expr): Unit = {
if (config.readStoresPos)
attrDebugStart(instName, dataType, None, NoRepeat)
attrDebugStart(instName, dataType, NoRepeat, None, NoRepeat)
handleAssignmentSimple(instName, expression(value))
}
}

0 comments on commit 185eaef

Please sign in to comment.