From 4d8bd683eb92870d2ec89fe21164093913dcc119 Mon Sep 17 00:00:00 2001 From: Sergey Mashkov Date: Wed, 7 Oct 2015 18:32:58 +0300 Subject: [PATCH] IDL2K code cleanup, add to build --- libraries/pom.xml | 1 + .../tools/idl2k/src/main/kotlin/download.kt | 4 +- libraries/tools/idl2k/src/main/kotlin/gen.kt | 9 ++-- libraries/tools/idl2k/src/main/kotlin/idl.kt | 50 +++++++++---------- libraries/tools/idl2k/src/main/kotlin/main.kt | 23 ++++----- .../tools/idl2k/src/main/kotlin/render.kt | 2 +- .../idl2k/src/main/kotlin/typeMappings.kt | 8 +-- .../tools/idl2k/src/main/kotlin/types.kt | 13 +++-- .../tools/idl2k/src/main/kotlin/utils.kt | 8 +-- 9 files changed, 59 insertions(+), 59 deletions(-) diff --git a/libraries/pom.xml b/libraries/pom.xml index ac138398668..93aee1de9c6 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -85,6 +85,7 @@ + tools/idl2k tools/kotlin-js-library tools/kotlin-annotation-processing examples/annotation-processor-example diff --git a/libraries/tools/idl2k/src/main/kotlin/download.kt b/libraries/tools/idl2k/src/main/kotlin/download.kt index 2347cd9b411..dddb0dab5e4 100644 --- a/libraries/tools/idl2k/src/main/kotlin/download.kt +++ b/libraries/tools/idl2k/src/main/kotlin/download.kt @@ -45,13 +45,13 @@ fun main(args: Array) { val pkg = e.value.first().second File(dir, fileName).bufferedWriter().use { w -> - w.appendln("namespace ${pkg};") + w.appendln("namespace $pkg;") w.appendln() w.appendln() e.value.forEach { pair -> val (url) = pair - println("Loading ${url}...") + println("Loading $url...") w.appendln("// Downloaded from $url") if (url.endsWith(".idl")) { diff --git a/libraries/tools/idl2k/src/main/kotlin/gen.kt b/libraries/tools/idl2k/src/main/kotlin/gen.kt index 7343a127f8f..648800f4d91 100644 --- a/libraries/tools/idl2k/src/main/kotlin/gen.kt +++ b/libraries/tools/idl2k/src/main/kotlin/gen.kt @@ -1,8 +1,5 @@ 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 -> when { "getter" in attributes -> NativeGetterOrSetter.GETTER @@ -45,7 +42,7 @@ fun generateFunctions(repository: Repository, function: Operation): List it - interfaceType.operations.size() != 1 -> it + interfaceType.operations.size != 1 -> it 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)) } @@ -92,7 +89,7 @@ fun generateTrait(repository: Repository, iface: InterfaceDefinition): GenerateT .filterNotNull() .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 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 primaryConstructor = when { - declaredConstructors.size() == 1 -> declaredConstructors.single() + declaredConstructors.size == 1 -> declaredConstructors.single() declaredConstructors.isEmpty() && entityKind == GenerateDefinitionKind.CLASS -> EMPTY_CONSTRUCTOR else -> declaredConstructors.firstOrNull { it.arguments.isEmpty() } } diff --git a/libraries/tools/idl2k/src/main/kotlin/idl.kt b/libraries/tools/idl2k/src/main/kotlin/idl.kt index dceaa8fae43..cfc349237fa 100644 --- a/libraries/tools/idl2k/src/main/kotlin/idl.kt +++ b/libraries/tools/idl2k/src/main/kotlin/idl.kt @@ -87,7 +87,7 @@ class ExtendedAttributeParser(private val namespace: String) : WebIDLBaseVisitor override fun defaultResult(): ExtendedAttribute = ExtendedAttribute(name, call, arguments) override fun visitExtendedAttribute(ctx: WebIDLParser.ExtendedAttributeContext): ExtendedAttribute { - call = ctx.children.filterIdentifiers().firstOrNull()?.getText() ?: "" + call = ctx.children.filterIdentifiers().firstOrNull()?.text ?: "" visitChildren(ctx) return defaultResult() @@ -101,8 +101,8 @@ class ExtendedAttributeParser(private val namespace: String) : WebIDLBaseVisitor override fun visitIdentifierList(ctx: IdentifierListContext): ExtendedAttribute { object : WebIDLBaseVisitor() { override fun visitTerminal(node: TerminalNode) { - if (node.getSymbol().getType() == WebIDLLexer.IDENTIFIER_WEBIDL) { - arguments.add(Attribute(node.getText(), AnyType(), true, vararg = false, static = false)) + if (node.symbol.type == WebIDLLexer.IDENTIFIER_WEBIDL) { + arguments.add(Attribute(node.text, AnyType(), true, vararg = false, static = false)) } } }.visitChildren(ctx) @@ -134,7 +134,7 @@ class TypeVisitor(val namespace: String) : WebIDLBaseVisitor() { override fun defaultResult() = type override fun visitNonAnyType(ctx: WebIDLParser.NonAnyTypeContext): Type { - type = SimpleType(ctx.getText(), false) + type = SimpleType(ctx.text, false) return type } @@ -144,7 +144,7 @@ class TypeVisitor(val namespace: String) : WebIDLBaseVisitor() { } override fun visitTypeSuffix(ctx: TypeSuffixContext): Type { - when (ctx.getText()?.trim()) { + when (ctx.text?.trim()) { "?" -> type = type.toNullable() "[]" -> type = ArrayType(type, false) "[]?" -> type = ArrayType(type, true) @@ -155,7 +155,7 @@ class TypeVisitor(val namespace: String) : WebIDLBaseVisitor() { } override fun visitTerminal(node: TerminalNode): Type { - type = SimpleType(node.getText(), false) + type = SimpleType(node.text, false) return type } } @@ -169,13 +169,13 @@ class OperationVisitor(private val attributes: List, private override fun defaultResult() = Operation(name, returnType, parameters, attributes + exts, static) override fun visitOptionalIdentifier(ctx: OptionalIdentifierContext): Operation { - name = ctx.getText() + name = ctx.text return defaultResult() } override fun visitSpecial(ctx: WebIDLParser.SpecialContext): Operation { 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() @@ -211,29 +211,29 @@ class AttributeVisitor(private val readOnly: Boolean = false, private val static } 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" } return visitChildren(ctx) } 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() } override fun visitArgumentName(ctx: WebIDLParser.ArgumentNameContext): Attribute { - name = getNameOrNull(ctx) ?: ctx.getText() + name = getNameOrNull(ctx) ?: ctx.text return defaultResult() } override fun visitDefaultValue(ctx: WebIDLParser.DefaultValueContext): Attribute { - defaultValue = ctx.getText() + defaultValue = ctx.text return defaultResult() } override fun visitEllipsis(ctx: WebIDLParser.EllipsisContext): Attribute { - vararg = vararg || "..." in ctx.getText() + vararg = vararg || "..." in ctx.text return defaultResult() } } @@ -252,12 +252,12 @@ class ConstantVisitor : WebIDLBaseVisitor() { } override fun visitConstType(ctx: WebIDLParser.ConstTypeContext): Constant { - type = SimpleType(ctx.getText(), false) + type = SimpleType(ctx.text, false) return defaultResult() } override fun visitConstValue(ctx: WebIDLParser.ConstValueContext): Constant { - value = ctx.getText() + value = ctx.text return defaultResult() } } @@ -363,8 +363,8 @@ class DefinitionVisitor(val extendedAttributes: List, val nam override fun visitDictionaryMember(ctx: DictionaryMemberContext): Definition { val name = ctx.children .filterIdentifiers() - .firstOrNull { it.getText() != "" } - ?.getText() + .firstOrNull { it.text != "" } + ?.text val type = TypeVisitor(namespace).visit(ctx.children.first { it is TypeContext }) val defaultValue = object : WebIDLBaseVisitor() { @@ -373,7 +373,7 @@ class DefinitionVisitor(val extendedAttributes: List, val nam override fun defaultResult() = value override fun visitDefaultValue(ctx2: DefaultValueContext): String? { - value = ctx2.getText() + value = ctx2.text return value } }.visit(ctx) @@ -384,9 +384,9 @@ class DefinitionVisitor(val extendedAttributes: List, val nam } 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 name = identifiers[0] implements = identifiers[1] @@ -408,7 +408,7 @@ class DefinitionVisitor(val extendedAttributes: List, val nam override fun visitInheritance(ctx: WebIDLParser.InheritanceContext): Definition { 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() } @@ -488,13 +488,13 @@ class ModuleVisitor(val declarations: MutableList, var namespace: St } override fun visitNamespaceRest(ctx: NamespaceRestContext) { - this.namespace = ctx.getText() + this.namespace = ctx.text } } -private fun List?.filterIdentifiers(): List = this?.filter { it is TerminalNode && it.getSymbol().getType() == WebIDLLexer.IDENTIFIER_WEBIDL } ?: emptyList() -private fun getName(ctx: ParserRuleContext) = ctx.children.filterIdentifiers().first().getText() -private fun getNameOrNull(ctx: ParserRuleContext) = ctx.children.filterIdentifiers().firstOrNull()?.getText() +private fun List?.filterIdentifiers(): List = this?.filter { it is TerminalNode && it.symbol.type == WebIDLLexer.IDENTIFIER_WEBIDL } ?: emptyList() +private fun getName(ctx: ParserRuleContext) = ctx.children.filterIdentifiers().first().text +private fun getNameOrNull(ctx: ParserRuleContext) = ctx.children.filterIdentifiers().firstOrNull()?.text fun parseIDL(reader: CharStream): Repository { val ll = WebIDLLexer(reader) diff --git a/libraries/tools/idl2k/src/main/kotlin/main.kt b/libraries/tools/idl2k/src/main/kotlin/main.kt index 4385e4ec362..a3c981a7994 100644 --- a/libraries/tools/idl2k/src/main/kotlin/main.kt +++ b/libraries/tools/idl2k/src/main/kotlin/main.kt @@ -2,20 +2,19 @@ package org.jetbrains.idl2k import org.antlr.v4.runtime.ANTLRFileStream import java.io.File -import java.io.StringReader import java.util.* fun main(args: Array) { val outDir = File("../../../js/js.libraries/src/generated") val srcDir = File("../../idl") if (!srcDir.exists()) { - System.err?.println("Directory ${srcDir.getAbsolutePath()} doesn't exist") + System.err?.println("Directory ${srcDir.absolutePath} doesn't exist") System.exit(1) 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 fileRepository = parseIDL(ANTLRFileStream(e.getAbsolutePath(), "UTF-8")) + 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.absolutePath, "UTF-8")) Repository( interfaces = acc.interfaces.mergeReduce(fileRepository.interfaces, ::merge), @@ -37,7 +36,7 @@ fun main(args: Array) { } } 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.mkdirs() @@ -52,11 +51,11 @@ fun main(args: Array) { w.appendln(" */") w.appendln() - w.appendln("package ${pkg}") + w.appendln("package $pkg") w.appendln() allPackages.filter { it != pkg }.forEach { import -> - w.appendln("import ${import}.*") + w.appendln("import $import.*") } w.appendln() @@ -65,10 +64,10 @@ fun main(args: Array) { } } -private fun Map>.reduceValues(reduce: (V, V) -> V = { a, b -> b }): Map = mapValues { it.value.reduce(reduce) } +internal fun Map>.reduceValues(reduce: (V, V) -> V = { a, b -> b }): Map = mapValues { it.value.reduce(reduce) } -private fun Map.mergeReduce(other: Map, reduce: (V, V) -> V = { a, b -> b }): Map { - val result = LinkedHashMap(this.size() + other.size()) +internal fun Map.mergeReduce(other: Map, reduce: (V, V) -> V = { a, b -> b }): Map { + val result = LinkedHashMap(this.size + other.size) result.putAll(this) other.forEach { e -> val existing = result[e.key] @@ -84,8 +83,8 @@ private fun Map.mergeReduce(other: Map, reduce: (V, V) -> V = return result } -private fun Map>.merge(other: Map>): Map> { - val result = LinkedHashMap>(size() + other.size()) +internal fun Map>.merge(other: Map>): Map> { + val result = LinkedHashMap>(size + other.size) this.forEach { result[it.key] = ArrayList(it.value) } diff --git a/libraries/tools/idl2k/src/main/kotlin/render.kt b/libraries/tools/idl2k/src/main/kotlin/render.kt index d8b00ea515b..c5ce1148cb2 100644 --- a/libraries/tools/idl2k/src/main/kotlin/render.kt +++ b/libraries/tools/idl2k/src/main/kotlin/render.kt @@ -92,7 +92,7 @@ private fun List.hasNoVars() = none { it.isVar } private fun GenerateAttribute.isCommented(parent: String) = "$parent.$name" in commentOutDeclarations || "$parent.$name: ${type.render()}" in commentOutDeclarations 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 GenerateFunction.fixRequiredArguments(parent: String) = copy(arguments = arguments.map { arg -> arg.copy(initializer = if (arg.isRequiredFunctionArgument(parent, name)) null else arg.initializer) }) diff --git a/libraries/tools/idl2k/src/main/kotlin/typeMappings.kt b/libraries/tools/idl2k/src/main/kotlin/typeMappings.kt index 2cd20009c58..fabe8011c45 100644 --- a/libraries/tools/idl2k/src/main/kotlin/typeMappings.kt +++ b/libraries/tools/idl2k/src/main/kotlin/typeMappings.kt @@ -69,7 +69,7 @@ fun Type.dynamicIfUnknownType(allTypes: Set, standardTypes: Set = 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 -> { val typeName = type.type when { @@ -101,7 +101,7 @@ private fun mapTypedef(repository: Repository, type: SimpleType): Type { val typedef = repository.typeDefs[type.type]!! 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) 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 collectUnionTypes(allTypes: Map) = +internal fun collectUnionTypes(allTypes: Map) = allTypes.values().asSequence() .flatMap { it.secondaryConstructors.asSequence().flatMap { it.constructor.allTypes() } + @@ -129,4 +129,4 @@ private fun guessPackage(types : List, allTypes: Map, val returnType : T } val FunctionType.arity : Int - get() = parameterTypes.size() + get() = parameterTypes.size -data class UnionType(val namespace: String, types: Collection, override val nullable: Boolean, val memberTypes: Set = LinkedHashSet(types.sortBy { it.toString() })) : Type { +class UnionType(val namespace: String, types: Collection, override val nullable: Boolean, val memberTypes: Set = LinkedHashSet(types.sortedBy { it.toString() })) : Type { 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 hashCode(): Int = memberTypes.hashCode() override fun toString(): String = memberTypes.map { it.toString() }.join(", ", "Union<", ">") override fun render(): String = name.withSuffix() + + fun copy(namespace: String = this.namespace, types: Collection = 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 { override fun render(): String = "Array<${memberType.render()}>".withSuffix() diff --git a/libraries/tools/idl2k/src/main/kotlin/utils.kt b/libraries/tools/idl2k/src/main/kotlin/utils.kt index c1cb9daf40b..3f4816a3b41 100644 --- a/libraries/tools/idl2k/src/main/kotlin/utils.kt +++ b/libraries/tools/idl2k/src/main/kotlin/utils.kt @@ -2,16 +2,16 @@ package org.jetbrains.idl2k.util import java.util.* -fun List>.mutationsCount() = if (isEmpty()) 0 else fold(1) { acc, e -> acc * e.size() } +fun List>.mutationsCount() = if (isEmpty()) 0 else fold(1) { acc, e -> acc * e.size } fun List>.mutations() : List> { - val indices = IntArray(size()) - val sizes = map { it.size() } + val indices = IntArray(size) + val sizes = map { it.size } fun next() : Boolean { var carry = 1 - for (pos in size() - 1 downTo 0) { + for (pos in size - 1 downTo 0) { var index = indices[pos] val size = sizes[pos]