IDL2K code cleanup, add to build
This commit is contained in:
@@ -85,6 +85,7 @@
|
|||||||
<!--NB! kotlin-js-library should be built before kotlin-gradle-plugin-->
|
<!--NB! kotlin-js-library should be built before kotlin-gradle-plugin-->
|
||||||
<!--because it is used in tests but cannot be added as test-dependency-->
|
<!--because it is used in tests but cannot be added as test-dependency-->
|
||||||
<!--(kotlin-gradle-plugin module will be recognized as kotlin-js module)-->
|
<!--(kotlin-gradle-plugin module will be recognized as kotlin-js module)-->
|
||||||
|
<module>tools/idl2k</module>
|
||||||
<module>tools/kotlin-js-library</module>
|
<module>tools/kotlin-js-library</module>
|
||||||
<module>tools/kotlin-annotation-processing</module>
|
<module>tools/kotlin-annotation-processing</module>
|
||||||
<module>examples/annotation-processor-example</module>
|
<module>examples/annotation-processor-example</module>
|
||||||
|
|||||||
@@ -45,13 +45,13 @@ fun main(args: Array<String>) {
|
|||||||
val pkg = e.value.first().second
|
val pkg = e.value.first().second
|
||||||
|
|
||||||
File(dir, fileName).bufferedWriter().use { w ->
|
File(dir, fileName).bufferedWriter().use { w ->
|
||||||
w.appendln("namespace ${pkg};")
|
w.appendln("namespace $pkg;")
|
||||||
w.appendln()
|
w.appendln()
|
||||||
w.appendln()
|
w.appendln()
|
||||||
|
|
||||||
e.value.forEach { pair ->
|
e.value.forEach { pair ->
|
||||||
val (url) = pair
|
val (url) = pair
|
||||||
println("Loading ${url}...")
|
println("Loading $url...")
|
||||||
|
|
||||||
w.appendln("// Downloaded from $url")
|
w.appendln("// Downloaded from $url")
|
||||||
if (url.endsWith(".idl")) {
|
if (url.endsWith(".idl")) {
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
package org.jetbrains.idl2k
|
package org.jetbrains.idl2k
|
||||||
|
|
||||||
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ
|
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
private fun Operation.getterOrSetter() = this.attributes.map { it.call }.toSet().let { attributes ->
|
private fun Operation.getterOrSetter() = this.attributes.map { it.call }.toSet().let { attributes ->
|
||||||
when {
|
when {
|
||||||
"getter" in attributes -> NativeGetterOrSetter.GETTER
|
"getter" in attributes -> NativeGetterOrSetter.GETTER
|
||||||
@@ -45,7 +42,7 @@ fun generateFunctions(repository: Repository, function: Operation): List<Generat
|
|||||||
val interfaceType = repository.interfaces[parameterType?.type]
|
val interfaceType = repository.interfaces[parameterType?.type]
|
||||||
when {
|
when {
|
||||||
interfaceType == null -> it
|
interfaceType == null -> it
|
||||||
interfaceType.operations.size() != 1 -> it
|
interfaceType.operations.size != 1 -> it
|
||||||
interfaceType.callback -> interfaceType.operations.single().let { callbackFunction ->
|
interfaceType.callback -> interfaceType.operations.single().let { callbackFunction ->
|
||||||
it.copy(type = FunctionType(callbackFunction.parameters.map { it.copy(type = mapType(repository, it.type)) }, mapType(repository, callbackFunction.returnType), parameterType?.nullable ?: false))
|
it.copy(type = FunctionType(callbackFunction.parameters.map { it.copy(type = mapType(repository, it.type)) }, mapType(repository, callbackFunction.returnType), parameterType?.nullable ?: false))
|
||||||
}
|
}
|
||||||
@@ -92,7 +89,7 @@ fun generateTrait(repository: Repository, iface: InterfaceDefinition): GenerateT
|
|||||||
.filterNotNull()
|
.filterNotNull()
|
||||||
.filter { resolveDefinitionKind(repository, it) == GenerateDefinitionKind.CLASS }
|
.filter { resolveDefinitionKind(repository, it) == GenerateDefinitionKind.CLASS }
|
||||||
|
|
||||||
assert(superClasses.size() <= 1) { "Type ${iface.name} should have one or zero super classes but found ${superClasses.map { it.name }}" }
|
assert(superClasses.size <= 1) { "Type ${iface.name} should have one or zero super classes but found ${superClasses.map { it.name }}" }
|
||||||
val superClass = superClasses.singleOrNull()
|
val superClass = superClasses.singleOrNull()
|
||||||
val superConstructor = superClass?.findConstructors()?.firstOrNull() ?: EMPTY_CONSTRUCTOR
|
val superConstructor = superClass?.findConstructors()?.firstOrNull() ?: EMPTY_CONSTRUCTOR
|
||||||
|
|
||||||
@@ -101,7 +98,7 @@ fun generateTrait(repository: Repository, iface: InterfaceDefinition): GenerateT
|
|||||||
val extensions = repository.externals[iface.name]?.map { repository.interfaces[it] }?.filterNotNull() ?: emptyList()
|
val extensions = repository.externals[iface.name]?.map { repository.interfaces[it] }?.filterNotNull() ?: emptyList()
|
||||||
|
|
||||||
val primaryConstructor = when {
|
val primaryConstructor = when {
|
||||||
declaredConstructors.size() == 1 -> declaredConstructors.single()
|
declaredConstructors.size == 1 -> declaredConstructors.single()
|
||||||
declaredConstructors.isEmpty() && entityKind == GenerateDefinitionKind.CLASS -> EMPTY_CONSTRUCTOR
|
declaredConstructors.isEmpty() && entityKind == GenerateDefinitionKind.CLASS -> EMPTY_CONSTRUCTOR
|
||||||
else -> declaredConstructors.firstOrNull { it.arguments.isEmpty() }
|
else -> declaredConstructors.firstOrNull { it.arguments.isEmpty() }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ class ExtendedAttributeParser(private val namespace: String) : WebIDLBaseVisitor
|
|||||||
override fun defaultResult(): ExtendedAttribute = ExtendedAttribute(name, call, arguments)
|
override fun defaultResult(): ExtendedAttribute = ExtendedAttribute(name, call, arguments)
|
||||||
|
|
||||||
override fun visitExtendedAttribute(ctx: WebIDLParser.ExtendedAttributeContext): ExtendedAttribute {
|
override fun visitExtendedAttribute(ctx: WebIDLParser.ExtendedAttributeContext): ExtendedAttribute {
|
||||||
call = ctx.children.filterIdentifiers().firstOrNull()?.getText() ?: ""
|
call = ctx.children.filterIdentifiers().firstOrNull()?.text ?: ""
|
||||||
|
|
||||||
visitChildren(ctx)
|
visitChildren(ctx)
|
||||||
return defaultResult()
|
return defaultResult()
|
||||||
@@ -101,8 +101,8 @@ class ExtendedAttributeParser(private val namespace: String) : WebIDLBaseVisitor
|
|||||||
override fun visitIdentifierList(ctx: IdentifierListContext): ExtendedAttribute {
|
override fun visitIdentifierList(ctx: IdentifierListContext): ExtendedAttribute {
|
||||||
object : WebIDLBaseVisitor<Unit>() {
|
object : WebIDLBaseVisitor<Unit>() {
|
||||||
override fun visitTerminal(node: TerminalNode) {
|
override fun visitTerminal(node: TerminalNode) {
|
||||||
if (node.getSymbol().getType() == WebIDLLexer.IDENTIFIER_WEBIDL) {
|
if (node.symbol.type == WebIDLLexer.IDENTIFIER_WEBIDL) {
|
||||||
arguments.add(Attribute(node.getText(), AnyType(), true, vararg = false, static = false))
|
arguments.add(Attribute(node.text, AnyType(), true, vararg = false, static = false))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.visitChildren(ctx)
|
}.visitChildren(ctx)
|
||||||
@@ -134,7 +134,7 @@ class TypeVisitor(val namespace: String) : WebIDLBaseVisitor<Type>() {
|
|||||||
override fun defaultResult() = type
|
override fun defaultResult() = type
|
||||||
|
|
||||||
override fun visitNonAnyType(ctx: WebIDLParser.NonAnyTypeContext): Type {
|
override fun visitNonAnyType(ctx: WebIDLParser.NonAnyTypeContext): Type {
|
||||||
type = SimpleType(ctx.getText(), false)
|
type = SimpleType(ctx.text, false)
|
||||||
return type
|
return type
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,7 +144,7 @@ class TypeVisitor(val namespace: String) : WebIDLBaseVisitor<Type>() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun visitTypeSuffix(ctx: TypeSuffixContext): Type {
|
override fun visitTypeSuffix(ctx: TypeSuffixContext): Type {
|
||||||
when (ctx.getText()?.trim()) {
|
when (ctx.text?.trim()) {
|
||||||
"?" -> type = type.toNullable()
|
"?" -> type = type.toNullable()
|
||||||
"[]" -> type = ArrayType(type, false)
|
"[]" -> type = ArrayType(type, false)
|
||||||
"[]?" -> type = ArrayType(type, true)
|
"[]?" -> type = ArrayType(type, true)
|
||||||
@@ -155,7 +155,7 @@ class TypeVisitor(val namespace: String) : WebIDLBaseVisitor<Type>() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun visitTerminal(node: TerminalNode): Type {
|
override fun visitTerminal(node: TerminalNode): Type {
|
||||||
type = SimpleType(node.getText(), false)
|
type = SimpleType(node.text, false)
|
||||||
return type
|
return type
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -169,13 +169,13 @@ class OperationVisitor(private val attributes: List<ExtendedAttribute>, private
|
|||||||
override fun defaultResult() = Operation(name, returnType, parameters, attributes + exts, static)
|
override fun defaultResult() = Operation(name, returnType, parameters, attributes + exts, static)
|
||||||
|
|
||||||
override fun visitOptionalIdentifier(ctx: OptionalIdentifierContext): Operation {
|
override fun visitOptionalIdentifier(ctx: OptionalIdentifierContext): Operation {
|
||||||
name = ctx.getText()
|
name = ctx.text
|
||||||
return defaultResult()
|
return defaultResult()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitSpecial(ctx: WebIDLParser.SpecialContext): Operation {
|
override fun visitSpecial(ctx: WebIDLParser.SpecialContext): Operation {
|
||||||
if (ctx.children != null) {
|
if (ctx.children != null) {
|
||||||
exts.add(ExtendedAttribute(call = ctx.getText(), name = null, arguments = emptyList()))
|
exts.add(ExtendedAttribute(call = ctx.text, name = null, arguments = emptyList()))
|
||||||
}
|
}
|
||||||
|
|
||||||
return defaultResult()
|
return defaultResult()
|
||||||
@@ -211,29 +211,29 @@ class AttributeVisitor(private val readOnly: Boolean = false, private val static
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun visitOptionalOrRequiredArgument(ctx: WebIDLParser.OptionalOrRequiredArgumentContext): Attribute {
|
override fun visitOptionalOrRequiredArgument(ctx: WebIDLParser.OptionalOrRequiredArgumentContext): Attribute {
|
||||||
if (ctx.children?.any { it is TerminalNode && it.getText() == "optional" } ?: false) {
|
if (ctx.children?.any { it is TerminalNode && it.text == "optional" } ?: false) {
|
||||||
defaultValue = "noImpl"
|
defaultValue = "noImpl"
|
||||||
}
|
}
|
||||||
return visitChildren(ctx)
|
return visitChildren(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitAttributeRest(ctx: WebIDLParser.AttributeRestContext): Attribute {
|
override fun visitAttributeRest(ctx: WebIDLParser.AttributeRestContext): Attribute {
|
||||||
name = getNameOrNull(ctx) ?: ctx.children.filter { it is TerminalNode }.filter { it.getText() != ";" }.last().getText()
|
name = getNameOrNull(ctx) ?: ctx.children.filter { it is TerminalNode }.filter { it.text != ";" }.last().text
|
||||||
return defaultResult()
|
return defaultResult()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitArgumentName(ctx: WebIDLParser.ArgumentNameContext): Attribute {
|
override fun visitArgumentName(ctx: WebIDLParser.ArgumentNameContext): Attribute {
|
||||||
name = getNameOrNull(ctx) ?: ctx.getText()
|
name = getNameOrNull(ctx) ?: ctx.text
|
||||||
return defaultResult()
|
return defaultResult()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitDefaultValue(ctx: WebIDLParser.DefaultValueContext): Attribute {
|
override fun visitDefaultValue(ctx: WebIDLParser.DefaultValueContext): Attribute {
|
||||||
defaultValue = ctx.getText()
|
defaultValue = ctx.text
|
||||||
return defaultResult()
|
return defaultResult()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitEllipsis(ctx: WebIDLParser.EllipsisContext): Attribute {
|
override fun visitEllipsis(ctx: WebIDLParser.EllipsisContext): Attribute {
|
||||||
vararg = vararg || "..." in ctx.getText()
|
vararg = vararg || "..." in ctx.text
|
||||||
return defaultResult()
|
return defaultResult()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -252,12 +252,12 @@ class ConstantVisitor : WebIDLBaseVisitor<Constant>() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun visitConstType(ctx: WebIDLParser.ConstTypeContext): Constant {
|
override fun visitConstType(ctx: WebIDLParser.ConstTypeContext): Constant {
|
||||||
type = SimpleType(ctx.getText(), false)
|
type = SimpleType(ctx.text, false)
|
||||||
return defaultResult()
|
return defaultResult()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitConstValue(ctx: WebIDLParser.ConstValueContext): Constant {
|
override fun visitConstValue(ctx: WebIDLParser.ConstValueContext): Constant {
|
||||||
value = ctx.getText()
|
value = ctx.text
|
||||||
return defaultResult()
|
return defaultResult()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -363,8 +363,8 @@ class DefinitionVisitor(val extendedAttributes: List<ExtendedAttribute>, val nam
|
|||||||
override fun visitDictionaryMember(ctx: DictionaryMemberContext): Definition {
|
override fun visitDictionaryMember(ctx: DictionaryMemberContext): Definition {
|
||||||
val name = ctx.children
|
val name = ctx.children
|
||||||
.filterIdentifiers()
|
.filterIdentifiers()
|
||||||
.firstOrNull { it.getText() != "" }
|
.firstOrNull { it.text != "" }
|
||||||
?.getText()
|
?.text
|
||||||
|
|
||||||
val type = TypeVisitor(namespace).visit(ctx.children.first { it is TypeContext })
|
val type = TypeVisitor(namespace).visit(ctx.children.first { it is TypeContext })
|
||||||
val defaultValue = object : WebIDLBaseVisitor<String?>() {
|
val defaultValue = object : WebIDLBaseVisitor<String?>() {
|
||||||
@@ -373,7 +373,7 @@ class DefinitionVisitor(val extendedAttributes: List<ExtendedAttribute>, val nam
|
|||||||
override fun defaultResult() = value
|
override fun defaultResult() = value
|
||||||
|
|
||||||
override fun visitDefaultValue(ctx2: DefaultValueContext): String? {
|
override fun visitDefaultValue(ctx2: DefaultValueContext): String? {
|
||||||
value = ctx2.getText()
|
value = ctx2.text
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
}.visit(ctx)
|
}.visit(ctx)
|
||||||
@@ -384,9 +384,9 @@ class DefinitionVisitor(val extendedAttributes: List<ExtendedAttribute>, val nam
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun visitImplementsStatement(ctx: ImplementsStatementContext): Definition {
|
override fun visitImplementsStatement(ctx: ImplementsStatementContext): Definition {
|
||||||
val identifiers = ctx.children.filterIdentifiers().map { it.getText() }
|
val identifiers = ctx.children.filterIdentifiers().map { it.text }
|
||||||
|
|
||||||
if (identifiers.size() == 2) {
|
if (identifiers.size == 2) {
|
||||||
kind = DefinitionKind.EXTENSION_INTERFACE
|
kind = DefinitionKind.EXTENSION_INTERFACE
|
||||||
name = identifiers[0]
|
name = identifiers[0]
|
||||||
implements = identifiers[1]
|
implements = identifiers[1]
|
||||||
@@ -408,7 +408,7 @@ class DefinitionVisitor(val extendedAttributes: List<ExtendedAttribute>, val nam
|
|||||||
|
|
||||||
override fun visitInheritance(ctx: WebIDLParser.InheritanceContext): Definition {
|
override fun visitInheritance(ctx: WebIDLParser.InheritanceContext): Definition {
|
||||||
if (ctx.children != null) {
|
if (ctx.children != null) {
|
||||||
inherited.addAll(ctx.children.filterIdentifiers().map { it.getText().trim() }.filter { it != "" })
|
inherited.addAll(ctx.children.filterIdentifiers().map { it.text.trim() }.filter { it != "" })
|
||||||
}
|
}
|
||||||
return defaultResult()
|
return defaultResult()
|
||||||
}
|
}
|
||||||
@@ -488,13 +488,13 @@ class ModuleVisitor(val declarations: MutableList<Definition>, var namespace: St
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun visitNamespaceRest(ctx: NamespaceRestContext) {
|
override fun visitNamespaceRest(ctx: NamespaceRestContext) {
|
||||||
this.namespace = ctx.getText()
|
this.namespace = ctx.text
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun List<ParseTree>?.filterIdentifiers(): List<ParseTree> = this?.filter { it is TerminalNode && it.getSymbol().getType() == WebIDLLexer.IDENTIFIER_WEBIDL } ?: emptyList()
|
private fun List<ParseTree>?.filterIdentifiers(): List<ParseTree> = this?.filter { it is TerminalNode && it.symbol.type == WebIDLLexer.IDENTIFIER_WEBIDL } ?: emptyList()
|
||||||
private fun getName(ctx: ParserRuleContext) = ctx.children.filterIdentifiers().first().getText()
|
private fun getName(ctx: ParserRuleContext) = ctx.children.filterIdentifiers().first().text
|
||||||
private fun getNameOrNull(ctx: ParserRuleContext) = ctx.children.filterIdentifiers().firstOrNull()?.getText()
|
private fun getNameOrNull(ctx: ParserRuleContext) = ctx.children.filterIdentifiers().firstOrNull()?.text
|
||||||
|
|
||||||
fun parseIDL(reader: CharStream): Repository {
|
fun parseIDL(reader: CharStream): Repository {
|
||||||
val ll = WebIDLLexer(reader)
|
val ll = WebIDLLexer(reader)
|
||||||
|
|||||||
@@ -2,20 +2,19 @@ package org.jetbrains.idl2k
|
|||||||
|
|
||||||
import org.antlr.v4.runtime.ANTLRFileStream
|
import org.antlr.v4.runtime.ANTLRFileStream
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.StringReader
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
val outDir = File("../../../js/js.libraries/src/generated")
|
val outDir = File("../../../js/js.libraries/src/generated")
|
||||||
val srcDir = File("../../idl")
|
val srcDir = File("../../idl")
|
||||||
if (!srcDir.exists()) {
|
if (!srcDir.exists()) {
|
||||||
System.err?.println("Directory ${srcDir.getAbsolutePath()} doesn't exist")
|
System.err?.println("Directory ${srcDir.absolutePath} doesn't exist")
|
||||||
System.exit(1)
|
System.exit(1)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val repositoryPre = srcDir.walkTopDown().filter { it.isDirectory() || it.extension == "idl" }.asSequence().filter { it.isFile() }.toList().sortBy { it.getAbsolutePath() }.fold(Repository(emptyMap(), emptyMap(), emptyMap(), emptyMap())) { acc, e ->
|
val repositoryPre = srcDir.walkTopDown().filter { it.isDirectory || it.extension == "idl" }.asSequence().filter { it.isFile }.toList().sortedBy { it.absolutePath }.fold(Repository(emptyMap(), emptyMap(), emptyMap(), emptyMap())) { acc, e ->
|
||||||
val fileRepository = parseIDL(ANTLRFileStream(e.getAbsolutePath(), "UTF-8"))
|
val fileRepository = parseIDL(ANTLRFileStream(e.absolutePath, "UTF-8"))
|
||||||
|
|
||||||
Repository(
|
Repository(
|
||||||
interfaces = acc.interfaces.mergeReduce(fileRepository.interfaces, ::merge),
|
interfaces = acc.interfaces.mergeReduce(fileRepository.interfaces, ::merge),
|
||||||
@@ -37,7 +36,7 @@ fun main(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
val unions = generateUnions(definitions, repository.typeDefs.values())
|
val unions = generateUnions(definitions, repository.typeDefs.values())
|
||||||
val allPackages = definitions.map { it.namespace }.distinct().sort()
|
val allPackages = definitions.map { it.namespace }.distinct().sorted()
|
||||||
|
|
||||||
outDir.deleteRecursively()
|
outDir.deleteRecursively()
|
||||||
outDir.mkdirs()
|
outDir.mkdirs()
|
||||||
@@ -52,11 +51,11 @@ fun main(args: Array<String>) {
|
|||||||
w.appendln(" */")
|
w.appendln(" */")
|
||||||
|
|
||||||
w.appendln()
|
w.appendln()
|
||||||
w.appendln("package ${pkg}")
|
w.appendln("package $pkg")
|
||||||
w.appendln()
|
w.appendln()
|
||||||
|
|
||||||
allPackages.filter { it != pkg }.forEach { import ->
|
allPackages.filter { it != pkg }.forEach { import ->
|
||||||
w.appendln("import ${import}.*")
|
w.appendln("import $import.*")
|
||||||
}
|
}
|
||||||
w.appendln()
|
w.appendln()
|
||||||
|
|
||||||
@@ -65,10 +64,10 @@ fun main(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun <K, V> Map<K, List<V>>.reduceValues(reduce: (V, V) -> V = { a, b -> b }): Map<K, V> = mapValues { it.value.reduce(reduce) }
|
internal fun <K, V> Map<K, List<V>>.reduceValues(reduce: (V, V) -> V = { a, b -> b }): Map<K, V> = mapValues { it.value.reduce(reduce) }
|
||||||
|
|
||||||
private fun <K, V> Map<K, V>.mergeReduce(other: Map<K, V>, reduce: (V, V) -> V = { a, b -> b }): Map<K, V> {
|
internal fun <K, V> Map<K, V>.mergeReduce(other: Map<K, V>, reduce: (V, V) -> V = { a, b -> b }): Map<K, V> {
|
||||||
val result = LinkedHashMap<K, V>(this.size() + other.size())
|
val result = LinkedHashMap<K, V>(this.size + other.size)
|
||||||
result.putAll(this)
|
result.putAll(this)
|
||||||
other.forEach { e ->
|
other.forEach { e ->
|
||||||
val existing = result[e.key]
|
val existing = result[e.key]
|
||||||
@@ -84,8 +83,8 @@ private fun <K, V> Map<K, V>.mergeReduce(other: Map<K, V>, reduce: (V, V) -> V =
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun <K, V> Map<K, List<V>>.merge(other: Map<K, List<V>>): Map<K, List<V>> {
|
internal fun <K, V> Map<K, List<V>>.merge(other: Map<K, List<V>>): Map<K, List<V>> {
|
||||||
val result = LinkedHashMap<K, MutableList<V>>(size() + other.size())
|
val result = LinkedHashMap<K, MutableList<V>>(size + other.size)
|
||||||
this.forEach {
|
this.forEach {
|
||||||
result[it.key] = ArrayList(it.value)
|
result[it.key] = ArrayList(it.value)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ private fun List<GenerateAttribute>.hasNoVars() = none { it.isVar }
|
|||||||
|
|
||||||
private fun GenerateAttribute.isCommented(parent: String) = "$parent.$name" in commentOutDeclarations || "$parent.$name: ${type.render()}" in commentOutDeclarations
|
private fun GenerateAttribute.isCommented(parent: String) = "$parent.$name" in commentOutDeclarations || "$parent.$name: ${type.render()}" in commentOutDeclarations
|
||||||
private fun GenerateFunction.isCommented(parent: String) =
|
private fun GenerateFunction.isCommented(parent: String) =
|
||||||
"$parent.$name" in commentOutDeclarations || "$parent.$name(${arguments.size()})" in commentOutDeclarations
|
"$parent.$name" in commentOutDeclarations || "$parent.$name(${arguments.size})" in commentOutDeclarations
|
||||||
private fun GenerateAttribute.isRequiredFunctionArgument(owner: String, functionName: String) = "$owner.$functionName.$name" in requiredArguments
|
private fun GenerateAttribute.isRequiredFunctionArgument(owner: String, functionName: String) = "$owner.$functionName.$name" in requiredArguments
|
||||||
private fun GenerateFunction.fixRequiredArguments(parent: String) = copy(arguments = arguments.map { arg -> arg.copy(initializer = if (arg.isRequiredFunctionArgument(parent, name)) null else arg.initializer) })
|
private fun GenerateFunction.fixRequiredArguments(parent: String) = copy(arguments = arguments.map { arg -> arg.copy(initializer = if (arg.isRequiredFunctionArgument(parent, name)) null else arg.initializer) })
|
||||||
|
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ fun Type.dynamicIfUnknownType(allTypes: Set<String>, standardTypes: Set<Type> =
|
|||||||
|
|
||||||
private fun Type.dynamicIfAnyType(): Type = if (this is AnyType && this.nullable) DynamicType else this
|
private fun Type.dynamicIfAnyType(): Type = if (this is AnyType && this.nullable) DynamicType else this
|
||||||
|
|
||||||
private fun mapType(repository: Repository, type: Type): Type = when (type) {
|
internal fun mapType(repository: Repository, type: Type): Type = when (type) {
|
||||||
is SimpleType -> {
|
is SimpleType -> {
|
||||||
val typeName = type.type
|
val typeName = type.type
|
||||||
when {
|
when {
|
||||||
@@ -101,7 +101,7 @@ private fun mapTypedef(repository: Repository, type: SimpleType): Type {
|
|||||||
val typedef = repository.typeDefs[type.type]!!
|
val typedef = repository.typeDefs[type.type]!!
|
||||||
|
|
||||||
return when {
|
return when {
|
||||||
typedef.types is UnionType && typedef.types.memberTypes.size() == 1 -> mapType(repository, typedef.types.memberTypes.single().withNullability(type.nullable))
|
typedef.types is UnionType && typedef.types.memberTypes.size == 1 -> mapType(repository, typedef.types.memberTypes.single().withNullability(type.nullable))
|
||||||
typedef.types is UnionType -> SimpleType(typedef.name, type.nullable)
|
typedef.types is UnionType -> SimpleType(typedef.name, type.nullable)
|
||||||
else -> mapType(repository, typedef.types.withNullability(type.nullable))
|
else -> mapType(repository, typedef.types.withNullability(type.nullable))
|
||||||
}
|
}
|
||||||
@@ -109,7 +109,7 @@ private fun mapTypedef(repository: Repository, type: SimpleType): Type {
|
|||||||
|
|
||||||
private fun GenerateFunction?.allTypes() = if (this != null) sequenceOf(returnType) + arguments.asSequence().map { it.type } else emptySequence()
|
private fun GenerateFunction?.allTypes() = if (this != null) sequenceOf(returnType) + arguments.asSequence().map { it.type } else emptySequence()
|
||||||
|
|
||||||
private fun collectUnionTypes(allTypes: Map<String, GenerateTraitOrClass>) =
|
internal fun collectUnionTypes(allTypes: Map<String, GenerateTraitOrClass>) =
|
||||||
allTypes.values().asSequence()
|
allTypes.values().asSequence()
|
||||||
.flatMap {
|
.flatMap {
|
||||||
it.secondaryConstructors.asSequence().flatMap { it.constructor.allTypes() } +
|
it.secondaryConstructors.asSequence().flatMap { it.constructor.allTypes() } +
|
||||||
@@ -129,4 +129,4 @@ private fun guessPackage(types : List<String>, allTypes: Map<String, GenerateTra
|
|||||||
.filterNotNull()
|
.filterNotNull()
|
||||||
.filter { it.isNotEmpty() }
|
.filter { it.isNotEmpty() }
|
||||||
.distinct()
|
.distinct()
|
||||||
.minBy { it.split('.').size() } ?: ""
|
.minBy { it.split('.').size } ?: ""
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ interface Type {
|
|||||||
val nullable: Boolean
|
val nullable: Boolean
|
||||||
fun render(): String
|
fun render(): String
|
||||||
|
|
||||||
protected fun String.withSuffix(): String = if (nullable) "$this?" else this
|
fun String.withSuffix(): String = if (nullable) "$this?" else this
|
||||||
}
|
}
|
||||||
|
|
||||||
object UnitType : Type {
|
object UnitType : Type {
|
||||||
@@ -33,20 +33,23 @@ data class FunctionType(val parameterTypes : List<Attribute>, val returnType : T
|
|||||||
}
|
}
|
||||||
|
|
||||||
val FunctionType.arity : Int
|
val FunctionType.arity : Int
|
||||||
get() = parameterTypes.size()
|
get() = parameterTypes.size
|
||||||
|
|
||||||
data class UnionType(val namespace: String, types: Collection<Type>, override val nullable: Boolean, val memberTypes: Set<Type> = LinkedHashSet(types.sortBy { it.toString() })) : Type {
|
class UnionType(val namespace: String, types: Collection<Type>, override val nullable: Boolean, val memberTypes: Set<Type> = LinkedHashSet(types.sortedBy { it.toString() })) : Type {
|
||||||
val name = "Union${this.memberTypes.map { it.render() }.joinToString("Or")}"
|
val name = "Union${this.memberTypes.map { it.render() }.joinToString("Or")}"
|
||||||
|
|
||||||
fun contains(type: Type) = type in memberTypes
|
operator fun contains(type: Type) = type in memberTypes
|
||||||
override fun equals(other: Any?): Boolean = other is UnionType && memberTypes == other.memberTypes
|
override fun equals(other: Any?): Boolean = other is UnionType && memberTypes == other.memberTypes
|
||||||
override fun hashCode(): Int = memberTypes.hashCode()
|
override fun hashCode(): Int = memberTypes.hashCode()
|
||||||
override fun toString(): String = memberTypes.map { it.toString() }.join(", ", "Union<", ">")
|
override fun toString(): String = memberTypes.map { it.toString() }.join(", ", "Union<", ">")
|
||||||
|
|
||||||
override fun render(): String = name.withSuffix()
|
override fun render(): String = name.withSuffix()
|
||||||
|
|
||||||
|
fun copy(namespace: String = this.namespace, types: Collection<Type> = this.memberTypes, nullable: Boolean = this.nullable) =
|
||||||
|
UnionType(namespace, types, nullable)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun UnionType.toSingleTypeIfPossible() = if (this.memberTypes.size() == 1) this.memberTypes.single().withNullability(nullable) else this
|
fun UnionType.toSingleTypeIfPossible() = if (this.memberTypes.size == 1) this.memberTypes.single().withNullability(nullable) else this
|
||||||
|
|
||||||
data class ArrayType(val memberType: Type, override val nullable: Boolean) : Type {
|
data class ArrayType(val memberType: Type, override val nullable: Boolean) : Type {
|
||||||
override fun render(): String = "Array<${memberType.render()}>".withSuffix()
|
override fun render(): String = "Array<${memberType.render()}>".withSuffix()
|
||||||
|
|||||||
@@ -2,16 +2,16 @@ package org.jetbrains.idl2k.util
|
|||||||
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
fun List<List<*>>.mutationsCount() = if (isEmpty()) 0 else fold(1) { acc, e -> acc * e.size() }
|
fun List<List<*>>.mutationsCount() = if (isEmpty()) 0 else fold(1) { acc, e -> acc * e.size }
|
||||||
|
|
||||||
fun <T> List<List<T>>.mutations() : List<List<T>> {
|
fun <T> List<List<T>>.mutations() : List<List<T>> {
|
||||||
val indices = IntArray(size())
|
val indices = IntArray(size)
|
||||||
val sizes = map { it.size() }
|
val sizes = map { it.size }
|
||||||
|
|
||||||
fun next() : Boolean {
|
fun next() : Boolean {
|
||||||
var carry = 1
|
var carry = 1
|
||||||
|
|
||||||
for (pos in size() - 1 downTo 0) {
|
for (pos in size - 1 downTo 0) {
|
||||||
var index = indices[pos]
|
var index = indices[pos]
|
||||||
val size = sizes[pos]
|
val size = sizes[pos]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user