Skip to content

Commit

Permalink
Graphviz: display contents in "type" column as in 0.8
Browse files Browse the repository at this point in the history
This fixes a regression in KS 0.9 where the `contents` key stopped
appearing on Graphviz diagrams.
  • Loading branch information
generalmimon committed Aug 11, 2024
1 parent 18fa14a commit 14630f5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
22 changes: 17 additions & 5 deletions shared/src/main/scala/io/kaitai/struct/GraphvizClassCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class GraphvizClassCompiler(classSpecs: ClassSpecs, topClass: ClassSpec) extends
compileSwitch(name, st)
s"switch (${expressionType(st.on, name)})"
case _ =>
dataTypeName(dataType)
dataTypeName(dataType, attr.valid)
}

out.puts("<TR>" +
Expand Down Expand Up @@ -450,11 +450,10 @@ object GraphvizClassCompiler extends LanguageCompilerStatic {
def type2class(name: List[String]) = name.last
def type2display(name: List[String]) = name.map(Utils.upperCamelCase).mkString("::")

def dataTypeName(dataType: DataType): String = {
def dataTypeName(dataType: DataType, valid: Option[ValidationSpec]): String = {
dataType match {
case rt: ReadableType => rt.apiCall(None) // FIXME
case ut: UserType => type2display(ut.name)
//case FixedBytesType(contents, _) => contents.map(_.formatted("%02X")).mkString(" ")
case BytesTerminatedType(terminator, include, consume, eosError, _) =>
val args = ListBuffer[String]()
val termStr = terminator.map(_ & 0xff).mkString(", ")
Expand All @@ -466,19 +465,32 @@ object GraphvizClassCompiler extends LanguageCompilerStatic {
if (!eosError)
args += "ignore EOS"
args.mkString(", ")
case blt: BytesLimitType =>
valid match {
case Some(ValidationEq(Ast.expr.List(contents))) if blt.size == Ast.expr.IntNum(contents.length) =>
fixedBytes(contents).getOrElse("")
case _ => ""
}
case _: BytesType => ""
case StrFromBytesType(basedOn, encoding, _) =>
val bytesStr = dataTypeName(basedOn)
val bytesStr = dataTypeName(basedOn, valid)
val comma = if (bytesStr.isEmpty) "" else ", "
s"str($bytesStr$comma$encoding)"
case EnumType(name, basedOn) =>
s"${dataTypeName(basedOn)}${type2display(name)}"
s"${dataTypeName(basedOn, valid)}${type2display(name)}"
case BitsType(width, bitEndian) => s"b$width${bitEndian.toSuffix}"
case BitsType1(bitEndian) => s"b1${bitEndian.toSuffix}→bool"
case _ => dataType.toString
}
}

private def fixedBytes(contents: Seq[Ast.expr]): Option[String] = {
Some(contents.map(_ match {
case Ast.expr.IntNum(byteVal) if byteVal >= 0x00 && byteVal <= 0xff => "%02X".format(byteVal)
case _ => return None
}).mkString(" "))
}

def htmlEscape(s: String): String = {
s.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\"", "&quot;")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,6 @@ object HtmlClassCompiler extends LanguageCompilerStatic {
def kaitaiType2NativeType(attrType: DataType): String = attrType match {
case ut: UserType =>
"<a href=\"#" + classSpec2Anchor(ut.classSpec.get) + "\">" + type2str(ut.name.last) + "</a>"
case _ => GraphvizClassCompiler.dataTypeName(attrType)
case _ => GraphvizClassCompiler.dataTypeName(attrType, None)
}
}

0 comments on commit 14630f5

Please sign in to comment.