Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate docs in Construct compiler #243

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.kaitai.struct.datatype.DataType._
import io.kaitai.struct.datatype._
import io.kaitai.struct.exprlang.Ast
import io.kaitai.struct.format._
import io.kaitai.struct.languages.components.{LanguageCompiler, LanguageCompilerStatic}
import io.kaitai.struct.languages.components.{LanguageCompiler, LanguageCompilerStatic, PythonOps}
import io.kaitai.struct.translators.ConstructTranslator

class ConstructClassCompiler(classSpecs: ClassSpecs, topClass: ClassSpec) extends AbstractCompiler {
Expand Down Expand Up @@ -37,7 +37,8 @@ class ConstructClassCompiler(classSpecs: ClassSpecs, topClass: ClassSpec) extend

cs.types.foreach { case (_, typeSpec) => compileClass(typeSpec) }

out.puts(s"${types2class(cs.name)} = Struct(")
val classname = types2class(cs.name)
out.puts(s"$classname = '$classname' / Struct(")
out.inc

provider.nowClass = cs
Expand All @@ -53,12 +54,24 @@ class ConstructClassCompiler(classSpecs: ClassSpecs, topClass: ClassSpec) extend
}

out.dec
out.puts(")")
val docStr = PythonOps.compileUniversalDocs(cs.doc)
out.puts(if (docStr.isEmpty) ")" else s") * '''$docStr'''")
out.puts
}

def compileAttr(attr: AttrLikeSpec): Unit = {
out.puts(s"'${idToStr(attr.id)}' / ${compileAttrBody(attr)},")
val attrStr = s"'${idToStr(attr.id)}' / ${compileAttrBody(attr)}"
val docStr = PythonOps.compileUniversalDocs(attr.doc)
if (docStr.isEmpty) {
out.puts(s"$attrStr,")
} else if (!docStr.contains('\n')) {
out.puts(s"$attrStr * '$docStr',")
} else {
out.puts(s"$attrStr * \\")
out.inc
out.putsLines("", s"'''$docStr''',")
out.dec
}
}

def compileValueInstance(id: Identifier, vis: ValueInstanceSpec): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import io.kaitai.struct.exprlang.Ast.expr
import io.kaitai.struct.format._
import io.kaitai.struct.languages.components._
import io.kaitai.struct.translators.PythonTranslator
import io.kaitai.struct.{ClassTypeProvider, RuntimeConfig, StringLanguageOutputWriter, Utils}
import io.kaitai.struct.{ClassTypeProvider, RuntimeConfig, Utils}

class PythonCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
extends LanguageCompiler(typeProvider, config)
Expand Down Expand Up @@ -147,31 +147,8 @@ class PythonCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
override def attributeReader(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = {}

override def universalDoc(doc: DocSpec): Unit = {
val docStr = doc.summary match {
case Some(summary) =>
val lastChar = summary.last
if (lastChar == '.' || lastChar == '\n') {
summary
} else {
summary + "."
}
case None =>
""
}

val extraNewline = if (docStr.isEmpty || docStr.last == '\n') "" else "\n"
val refStr = doc.ref.map {
case TextRef(text) =>
val seeAlso = new StringLanguageOutputWriter("")
seeAlso.putsLines(" ", text)
s"$extraNewline\n.. seealso::\n${seeAlso.result}"
case ref: UrlRef =>
val seeAlso = new StringLanguageOutputWriter("")
seeAlso.putsLines(" ", s"${ref.text} - ${ref.url}")
s"$extraNewline\n.. seealso::\n${seeAlso.result}"
}.mkString("\n")

out.putsLines("", "\"\"\"" + docStr + refStr + "\"\"\"")
val docStr = PythonOps.compileUniversalDocs(doc)
out.putsLines("", "\"\"\"" + docStr + "\"\"\"")
}

override def attrFixedContentsParse(attrName: Identifier, contents: String): Unit =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.kaitai.struct.languages.components

import io.kaitai.struct.StringLanguageOutputWriter
import io.kaitai.struct.format.{DocSpec, TextRef, UrlRef}

object PythonOps {
def compileUniversalDocs(doc: DocSpec): String = {
val docStr = doc.summary match {
case Some(summary) =>
val lastChar = summary.last
if (lastChar == '.' || lastChar == '\n') {
summary
} else {
summary + "."
}
case None =>
""
}

val extraNewline = if (docStr.isEmpty || docStr.last == '\n') "" else "\n"
val refStr = doc.ref.map {
case TextRef(text) =>
val seeAlso = new StringLanguageOutputWriter("")
seeAlso.putsLines(" ", text)
s"$extraNewline\n.. seealso::\n${seeAlso.result}"
case ref: UrlRef =>
val seeAlso = new StringLanguageOutputWriter("")
seeAlso.putsLines(" ", s"${ref.text} - ${ref.url}")
s"$extraNewline\n.. seealso::\n${seeAlso.result}"
}.mkString("\n")

docStr + refStr
}
}