IDL2K code cleanup, add to build

This commit is contained in:
Sergey Mashkov
2015-10-07 18:32:58 +03:00
parent e8e0f2d7e1
commit 4d8bd683eb
9 changed files with 59 additions and 59 deletions
+1
View File
@@ -85,6 +85,7 @@
<!--NB! kotlin-js-library should be built before kotlin-gradle-plugin-->
<!--because it is used in tests but cannot be added as test-dependency-->
<!--(kotlin-gradle-plugin module will be recognized as kotlin-js module)-->
<module>tools/idl2k</module>
<module>tools/kotlin-js-library</module>
<module>tools/kotlin-annotation-processing</module>
<module>examples/annotation-processor-example</module>
@@ -45,13 +45,13 @@ fun main(args: Array<String>) {
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")) {
+3 -6
View File
@@ -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<Generat
val interfaceType = repository.interfaces[parameterType?.type]
when {
interfaceType == null -> 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() }
}
+25 -25
View File
@@ -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<Unit>() {
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<Type>() {
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<Type>() {
}
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<Type>() {
}
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<ExtendedAttribute>, 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<Constant>() {
}
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<ExtendedAttribute>, 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<String?>() {
@@ -373,7 +373,7 @@ class DefinitionVisitor(val extendedAttributes: List<ExtendedAttribute>, 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<ExtendedAttribute>, 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<ExtendedAttribute>, 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<Definition>, var namespace: St
}
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 getName(ctx: ParserRuleContext) = ctx.children.filterIdentifiers().first().getText()
private fun getNameOrNull(ctx: ParserRuleContext) = ctx.children.filterIdentifiers().firstOrNull()?.getText()
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().text
private fun getNameOrNull(ctx: ParserRuleContext) = ctx.children.filterIdentifiers().firstOrNull()?.text
fun parseIDL(reader: CharStream): Repository {
val ll = WebIDLLexer(reader)
+11 -12
View File
@@ -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<String>) {
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<String>) {
}
}
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<String>) {
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<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> {
val result = LinkedHashMap<K, V>(this.size() + other.size())
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)
result.putAll(this)
other.forEach { e ->
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
}
private 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())
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)
this.forEach {
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 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) })
@@ -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 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<String, GenerateTraitOrClass>) =
internal fun collectUnionTypes(allTypes: Map<String, GenerateTraitOrClass>) =
allTypes.values().asSequence()
.flatMap {
it.secondaryConstructors.asSequence().flatMap { it.constructor.allTypes() } +
@@ -129,4 +129,4 @@ private fun guessPackage(types : List<String>, allTypes: Map<String, GenerateTra
.filterNotNull()
.filter { it.isNotEmpty() }
.distinct()
.minBy { it.split('.').size() } ?: ""
.minBy { it.split('.').size } ?: ""
@@ -6,7 +6,7 @@ interface Type {
val nullable: Boolean
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 {
@@ -33,20 +33,23 @@ data class FunctionType(val parameterTypes : List<Attribute>, val returnType : T
}
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")}"
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<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 {
override fun render(): String = "Array<${memberType.render()}>".withSuffix()
@@ -2,16 +2,16 @@ package org.jetbrains.idl2k.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>> {
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]