Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: kaitai-io/kaitai_struct_compiler
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: ffca8d7cc6bd465525e8e1b098cf287dd86cb89b
Choose a base ref
..
head repository: kaitai-io/kaitai_struct_compiler
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: ffdc8541ca887d63e3a15d17ab6429b7520f8a7c
Choose a head ref
Showing with 2,614 additions and 959 deletions.
  1. +1 −1 RELEASE_NOTES.md
  2. +1 −1 build.sbt
  3. +58 −22 jvm/src/main/scala/io/kaitai/struct/JavaMain.scala
  4. +15 −0 jvm/src/test/scala/io/kaitai/struct/exprlang/ExpressionsSpec.scala
  5. +26 −98 jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala
  6. +1 −1 project/build.properties
  7. +1 −1 shared/src/main/scala/io/kaitai/struct/ClassCompiler.scala
  8. +1 −0 shared/src/main/scala/io/kaitai/struct/ClassTypeProvider.scala
  9. +2 −4 shared/src/main/scala/io/kaitai/struct/ConstructClassCompiler.scala
  10. +35 −32 shared/src/main/scala/io/kaitai/struct/EncodingList.scala
  11. +3 −3 shared/src/main/scala/io/kaitai/struct/GoClassCompiler.scala
  12. +110 −19 shared/src/main/scala/io/kaitai/struct/GraphvizClassCompiler.scala
  13. +1 −1 shared/src/main/scala/io/kaitai/struct/HtmlClassCompiler.scala
  14. +1 −0 shared/src/main/scala/io/kaitai/struct/JSON.scala
  15. +1 −1 shared/src/main/scala/io/kaitai/struct/NimClassCompiler.scala
  16. +2 −2 shared/src/main/scala/io/kaitai/struct/RuntimeConfig.scala
  17. +34 −14 shared/src/main/scala/io/kaitai/struct/RustClassCompiler.scala
  18. +24 −5 shared/src/main/scala/io/kaitai/struct/datatype/DataType.scala
  19. +9 −0 shared/src/main/scala/io/kaitai/struct/datatype/KSError.scala
  20. +3 −3 shared/src/main/scala/io/kaitai/struct/format/AttrSpec.scala
  21. +13 −0 shared/src/main/scala/io/kaitai/struct/format/Identifier.scala
  22. +9 −0 shared/src/main/scala/io/kaitai/struct/format/ParseUtils.scala
  23. +25 −4 shared/src/main/scala/io/kaitai/struct/format/ValidationSpec.scala
  24. +59 −26 shared/src/main/scala/io/kaitai/struct/languages/CSharpCompiler.scala
  25. +74 −9 shared/src/main/scala/io/kaitai/struct/languages/CppCompiler.scala
  26. +41 −16 shared/src/main/scala/io/kaitai/struct/languages/GoCompiler.scala
  27. +35 −9 shared/src/main/scala/io/kaitai/struct/languages/JavaCompiler.scala
  28. +40 −15 shared/src/main/scala/io/kaitai/struct/languages/JavaScriptCompiler.scala
  29. +42 −15 shared/src/main/scala/io/kaitai/struct/languages/LuaCompiler.scala
  30. +14 −3 shared/src/main/scala/io/kaitai/struct/languages/NimCompiler.scala
  31. +50 −17 shared/src/main/scala/io/kaitai/struct/languages/PHPCompiler.scala
  32. +17 −6 shared/src/main/scala/io/kaitai/struct/languages/PerlCompiler.scala
  33. +38 −13 shared/src/main/scala/io/kaitai/struct/languages/PythonCompiler.scala
  34. +33 −9 shared/src/main/scala/io/kaitai/struct/languages/RubyCompiler.scala
  35. +1,153 −377 shared/src/main/scala/io/kaitai/struct/languages/RustCompiler.scala
  36. +24 −13 shared/src/main/scala/io/kaitai/struct/languages/components/CommonReads.scala
  37. +1 −18 shared/src/main/scala/io/kaitai/struct/languages/components/EveryReadIsExpression.scala
  38. +8 −2 shared/src/main/scala/io/kaitai/struct/languages/components/GoReads.scala
  39. +37 −24 shared/src/main/scala/io/kaitai/struct/languages/components/ValidateOps.scala
  40. +4 −2 shared/src/main/scala/io/kaitai/struct/problems/CompilationProblem.scala
  41. +2 −2 shared/src/main/scala/io/kaitai/struct/translators/CSharpTranslator.scala
  42. +7 −3 shared/src/main/scala/io/kaitai/struct/translators/CommonLiterals.scala
  43. +9 −21 shared/src/main/scala/io/kaitai/struct/translators/CppTranslator.scala
  44. +36 −15 shared/src/main/scala/io/kaitai/struct/translators/GoTranslator.scala
  45. +15 −2 shared/src/main/scala/io/kaitai/struct/translators/JavaScriptTranslator.scala
  46. +2 −17 shared/src/main/scala/io/kaitai/struct/translators/JavaTranslator.scala
  47. +15 −4 shared/src/main/scala/io/kaitai/struct/translators/LuaTranslator.scala
  48. +13 −32 shared/src/main/scala/io/kaitai/struct/translators/NimTranslator.scala
  49. +2 −2 shared/src/main/scala/io/kaitai/struct/translators/PHPTranslator.scala
  50. +2 −2 shared/src/main/scala/io/kaitai/struct/translators/PerlTranslator.scala
  51. +2 −2 shared/src/main/scala/io/kaitai/struct/translators/PythonTranslator.scala
  52. +3 −3 shared/src/main/scala/io/kaitai/struct/translators/RubyTranslator.scala
  53. +460 −68 shared/src/main/scala/io/kaitai/struct/translators/RustTranslator.scala
2 changes: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -71,7 +71,7 @@
* Implement parsed data validations using `valid` key ([#435](https://github.com/kaitai-io/kaitai_struct/issues/435))
* Implement compile-time `sizeof` and `bitsizeof` operators ([#84](https://github.com/kaitai-io/kaitai_struct/issues/84))
* Type-based: `sizeof<u4>`, `bitsizeof<b13>`, `sizeof<user_type>`
* Value-based: `file_header._sizeof`, `flags._bitsizeof` (`file_header`, `flags` are fields defined in the current type)
* Value-based: `file_header._sizeof` (`file_header` is a field defined in the current type)
* Implement little-endian bit-sized integers ([docs](https://doc.kaitai.io/user_guide.html#bit-ints-le))
* Support choosing endianness using `le` / `be` suffix: `type: b12le`, `type: b1be`
* Add `meta/bit-endian` key for selecting default bit endianness (`le` / `be`)
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ resolvers ++= Resolver.sonatypeOssRepos("public")

val NAME = "kaitai-struct-compiler"
val VERSION = "0.11-SNAPSHOT"
val TARGET_LANGS = "C++/STL, C#, Go, Java, JavaScript, Lua, Nim, Perl, PHP, Python, Ruby"
val TARGET_LANGS = "C++/STL, C#, Go, Java, JavaScript, Lua, Nim, Perl, PHP, Python, Ruby, Rust"
val UTF8 = Charset.forName("UTF-8")

lazy val root = project.in(file(".")).
80 changes: 58 additions & 22 deletions jvm/src/main/scala/io/kaitai/struct/JavaMain.scala
Original file line number Diff line number Diff line change
@@ -210,35 +210,71 @@ object JavaMain {
// Windows, custom install path with spaces and non-latin chars:
// /G:/%d0%b3%d0%b4%d0%b5-%d1%82%d0%be%20%d1%82%d0%b0%d0%bc/lib/io.kaitai.kaitai-struct-compiler-0.10-SNAPSHOT.jar

val fStr = classOf[JavaMain].getProtectionDomain.getCodeSource.getLocation.getPath
Log.importOps.info(() => s"home path: location = $fStr")

if (fStr.endsWith(".jar")) {
val fDec = URLDecoder.decode(fStr, "UTF-8")
Log.importOps.info(() => s"... URL-decoded = $fDec")

val homeFile = new File(fDec).getParentFile.getParentFile
Log.importOps.info(() => s"... home = $homeFile")

if (homeFile.exists) {
val homeFormat = new File(homeFile, "formats")
Log.importOps.info(() => s"... formats = $homeFormat")
if (homeFormat.exists) {
Some(homeFormat.toString)
try {
optionOrLog(
classOf[JavaMain].getProtectionDomain.getCodeSource,
"home path: unable to run getCodeSource(), got null"
).flatMap(sourceCode => optionOrLog(
sourceCode.getLocation,
"home path: unable to run getLocation(), got null"
)).flatMap(location => optionOrLog(
location.getPath,
"home path: unable to run getPath(), got null"
)).flatMap(fStr => {
Log.importOps.info(() => s"home path: location = $fStr")

if (fStr.endsWith(".jar")) {
val fDec = URLDecoder.decode(fStr, "UTF-8")
Log.importOps.info(() => s"... URL-decoded = $fDec")

val homeFile = new File(fDec).getParentFile.getParentFile
Log.importOps.info(() => s"... home = $homeFile")

if (homeFile.exists) {
val homeFormat = new File(homeFile, "formats")
Log.importOps.info(() => s"... formats = $homeFormat")
if (homeFormat.exists) {
Some(homeFormat.toString)
} else {
Log.importOps.info(() => "... home formats dir doesn't exist => fail")
None
}
} else {
Log.importOps.info(() => s"... home doesn't exist => no home import paths")
None
}
} else {
Log.importOps.info(() => "... home formats dir doesn't exist => fail")
Log.importOps.info(() => s"... not a jar, we're not running a packaged app => no home")
None
}
} else {
Log.importOps.info(() => s"... home doesn't exist => no home import paths")
})
} catch {
case se: SecurityException =>
Log.importOps.info(() => s"home path: unable to run getProtectionDomain(), got SecurityException $se")
None
}
} else {
Log.importOps.info(() => s"... not a jar, we're not running a packaged app => no home")
None
}
}

/**
* Helper method to wrap nullable value (coming from Java API) into Option.
* If it's null, we will bail out and won't process any longer due to a chain
* of flatMap(), but if we use this method, we'll also note in our logging which
* step failed, making it easier to diagnose.
* @param nullableValue value which is potentially null
* @param errMsg error message to show in case if it's null
* @tparam T type of potentially nullable value
* @return option-wrapped value
* @see [[scala.Option.apply()]]
*/
private def optionOrLog[T](nullableValue: T, errMsg: String): Option[T] =
Option(nullableValue) match {
case None =>
Log.importOps.info(() => errMsg)
None
case someValue =>
someValue
}

private def envPaths: List[String] =
sys.env.get("KSPATH").toList.flatMap((x) => x.split(File.pathSeparatorChar))

15 changes: 15 additions & 0 deletions jvm/src/test/scala/io/kaitai/struct/exprlang/ExpressionsSpec.scala
Original file line number Diff line number Diff line change
@@ -404,6 +404,21 @@ class ExpressionsSpec extends AnyFunSpec {
Expressions.parse("foo.bar") should be (Attribute(Name(identifier("foo")),identifier("bar")))
}

describe("strings") {
it("single-quoted") {
// \" -> \"
// \\ -> \\
Expressions.parse(""" ' \" \\ ' """) should be(Str(" \\\" \\\\ "))
Expressions.parse(""" 'ASCII\\x' """) should be(Str("ASCII\\\\x"))
}
it("double-quoted") {
// \" -> "
// \\ -> \
Expressions.parse(""" " \" \\ " """) should be(Str(" \" \\ "))
Expressions.parse(""" "ASCII\\'x" """) should be(Str("ASCII\\'x"))
}
}

describe("f-strings") {
it("parses f-string with just a string") {
Expressions.parse("f\"abc\"") should be(InterpolatedStr(Seq(
Loading