IDL2K support static members
This commit is contained in:
@@ -44,10 +44,12 @@ fun generateFunction(repository: Repository, function: Operation, functionName:
|
||||
getterSetterNoImpl = false,
|
||||
override = false,
|
||||
readOnly = true,
|
||||
vararg = it.vararg
|
||||
vararg = it.vararg,
|
||||
static = it.static
|
||||
)
|
||||
},
|
||||
nativeGetterOrSetter = nativeGetterOrSetter
|
||||
nativeGetterOrSetter = nativeGetterOrSetter,
|
||||
static = function.static
|
||||
)
|
||||
}
|
||||
|
||||
@@ -90,7 +92,8 @@ fun generateAttribute(putNoImpl: Boolean, repository: Repository, attribute: Att
|
||||
getterSetterNoImpl = putNoImpl,
|
||||
readOnly = attribute.readOnly,
|
||||
override = false,
|
||||
vararg = attribute.vararg
|
||||
vararg = attribute.vararg,
|
||||
static = attribute.static
|
||||
)
|
||||
|
||||
private fun InterfaceDefinition.superTypes(repository: Repository) = superTypes.map { repository.interfaces[it] }.filterNotNull()
|
||||
@@ -103,7 +106,7 @@ private fun resolveDefinitionKind(repository: Repository, iface: InterfaceDefini
|
||||
|
||||
private fun InterfaceDefinition.mapAttributes(repository: Repository) = attributes.map { generateAttribute(!dictionary, repository, it) }
|
||||
private fun InterfaceDefinition.mapOperations(repository: Repository) = operations.flatMap { generateFunctions(repository, it) }
|
||||
private fun Constant.mapConstant(repository: Repository) = GenerateAttribute(name, mapType(repository, type), value, false, true, false, false)
|
||||
private fun Constant.mapConstant(repository : Repository) = GenerateAttribute(name, mapType(repository, type), value, false, true, false, false, true)
|
||||
private fun emptyConstructor() = ExtendedAttribute(null, "Constructor", emptyList())
|
||||
|
||||
fun generateTrait(repository: Repository, iface: InterfaceDefinition): GenerateTraitOrClass {
|
||||
@@ -158,7 +161,7 @@ fun generateTrait(repository: Repository, iface: InterfaceDefinition): GenerateT
|
||||
|
||||
fun generateConstructorAsFunction(repository: Repository, constructor: ExtendedAttribute) = generateFunction(
|
||||
repository,
|
||||
Operation("", "Unit", constructor.arguments, emptyList()),
|
||||
Operation("", "Unit", constructor.arguments, emptyList(), false),
|
||||
functionName = "",
|
||||
nativeGetterOrSetter = NativeGetterOrSetter.NONE)
|
||||
|
||||
|
||||
@@ -28,8 +28,8 @@ import org.antlr.webidl.WebIDLParser.*
|
||||
import java.util.ArrayList
|
||||
|
||||
data class ExtendedAttribute(val name: String?, val call: String, val arguments: List<Attribute>)
|
||||
data class Operation(val name: String, val returnType: String, val parameters: List<Attribute>, val attributes: List<ExtendedAttribute>)
|
||||
data class Attribute(val name: String, val type: String, val readOnly: Boolean = true, val defaultValue: String? = null, val vararg: Boolean)
|
||||
data class Operation(val name: String, val returnType: String, val parameters: List<Attribute>, val attributes: List<ExtendedAttribute>, val static: Boolean)
|
||||
data class Attribute(val name: String, val type: String, val readOnly: Boolean = true, val defaultValue: String? = null, val vararg: Boolean, val static: Boolean)
|
||||
data class Constant(val name: String, val type: String, val value: String?)
|
||||
|
||||
fun Attribute.formatFunctionTypePart() = if (vararg) "vararg $type" else type
|
||||
@@ -42,7 +42,7 @@ enum class DefinitionKind {
|
||||
DICTIONARY
|
||||
}
|
||||
|
||||
trait Definition
|
||||
interface Definition
|
||||
data class TypedefDefinition(val types: String, val namespace: String, val name: String) : Definition
|
||||
data class InterfaceDefinition(
|
||||
val name: String,
|
||||
@@ -66,7 +66,7 @@ class ExtendedAttributeArgumentsParser : WebIDLBaseVisitor<List<Attribute>>() {
|
||||
override fun defaultResult(): List<Attribute> = arguments
|
||||
|
||||
override fun visitOptionalOrRequiredArgument(ctx: WebIDLParser.OptionalOrRequiredArgumentContext): List<Attribute> {
|
||||
val attributeVisitor = AttributeVisitor(false)
|
||||
val attributeVisitor = AttributeVisitor()
|
||||
attributeVisitor.visit(ctx)
|
||||
val parameter = attributeVisitor.visitChildren(ctx)
|
||||
|
||||
@@ -104,7 +104,7 @@ class ExtendedAttributeParser : WebIDLBaseVisitor<ExtendedAttribute>() {
|
||||
object : WebIDLBaseVisitor<Unit>() {
|
||||
override fun visitTerminal(node: TerminalNode) {
|
||||
if (node.getSymbol().getType() == WebIDLLexer.IDENTIFIER_WEBIDL) {
|
||||
arguments.add(Attribute(node.getText(), "any", true, vararg = false))
|
||||
arguments.add(Attribute(node.getText(), "any", true, vararg = false, static = false))
|
||||
}
|
||||
}
|
||||
}.visitChildren(ctx)
|
||||
@@ -156,13 +156,13 @@ class TypeVisitor : WebIDLBaseVisitor<String>() {
|
||||
}
|
||||
}
|
||||
|
||||
class OperationVisitor(private val attributes: List<ExtendedAttribute>) : WebIDLBaseVisitor<Operation>() {
|
||||
class OperationVisitor(private val attributes: List<ExtendedAttribute>, private val static: Boolean) : WebIDLBaseVisitor<Operation>() {
|
||||
private var name: String = ""
|
||||
private var returnType: String = ""
|
||||
private val parameters = ArrayList<Attribute>()
|
||||
private val exts = ArrayList<ExtendedAttribute>()
|
||||
|
||||
override fun defaultResult() = Operation(name, returnType, parameters, attributes + exts)
|
||||
override fun defaultResult() = Operation(name, returnType, parameters, attributes + exts, static)
|
||||
|
||||
override fun visitOptionalIdentifier(ctx: OptionalIdentifierContext): Operation {
|
||||
name = ctx.getText()
|
||||
@@ -183,7 +183,7 @@ class OperationVisitor(private val attributes: List<ExtendedAttribute>) : WebIDL
|
||||
}
|
||||
|
||||
override fun visitOptionalOrRequiredArgument(ctx: WebIDLParser.OptionalOrRequiredArgumentContext): Operation {
|
||||
val attributeVisitor = AttributeVisitor()
|
||||
val attributeVisitor = AttributeVisitor(static = false)
|
||||
attributeVisitor.visit(ctx)
|
||||
val parameter = attributeVisitor.visitChildren(ctx)
|
||||
|
||||
@@ -193,13 +193,13 @@ class OperationVisitor(private val attributes: List<ExtendedAttribute>) : WebIDL
|
||||
}
|
||||
}
|
||||
|
||||
class AttributeVisitor(private val readOnly: Boolean = false) : WebIDLBaseVisitor<Attribute>() {
|
||||
class AttributeVisitor(private val readOnly: Boolean = false, private val static: Boolean = false) : WebIDLBaseVisitor<Attribute>() {
|
||||
private var type: String = ""
|
||||
private var name: String = ""
|
||||
private var defaultValue: String? = null
|
||||
private var vararg: Boolean = false
|
||||
|
||||
override fun defaultResult(): Attribute = Attribute(name, type, readOnly, defaultValue, vararg)
|
||||
override fun defaultResult(): Attribute = Attribute(name, type, readOnly, defaultValue, vararg, static)
|
||||
|
||||
override fun visitType(ctx: WebIDLParser.TypeContext): Attribute {
|
||||
type = TypeVisitor().visit(ctx)
|
||||
@@ -265,6 +265,7 @@ class DefinitionVisitor(val extendedAttributes: List<ExtendedAttribute>, val nam
|
||||
private val operations = ArrayList<Operation>()
|
||||
private val attributes = ArrayList<Attribute>()
|
||||
private var readOnly: Boolean = false
|
||||
private var static: Boolean = false
|
||||
private val inherited = ArrayList<String>()
|
||||
private var typedefType: String? = null
|
||||
private var implements: String? = null
|
||||
@@ -289,7 +290,7 @@ class DefinitionVisitor(val extendedAttributes: List<ExtendedAttribute>, val nam
|
||||
kind = DefinitionKind.TYPEDEF
|
||||
name = getName(ctx)
|
||||
|
||||
val function = OperationVisitor(memberAttributes.toList()).visit(ctx)
|
||||
val function = OperationVisitor(memberAttributes.toList(), static).visit(ctx)
|
||||
typedefType = "(${function.parameters.map { it.formatFunctionTypePart() }.join(", ")}) -> ${function.returnType}"
|
||||
|
||||
memberAttributes.clear()
|
||||
@@ -373,7 +374,7 @@ class DefinitionVisitor(val extendedAttributes: List<ExtendedAttribute>, val nam
|
||||
}
|
||||
}.visit(ctx)
|
||||
|
||||
attributes.add(Attribute(name ?: "", type, false, defaultValue, false))
|
||||
attributes.add(Attribute(name ?: "", type, false, defaultValue, false, static))
|
||||
|
||||
return defaultResult()
|
||||
}
|
||||
@@ -392,11 +393,15 @@ class DefinitionVisitor(val extendedAttributes: List<ExtendedAttribute>, val nam
|
||||
}
|
||||
|
||||
override fun visitOperation(ctx: OperationContext): Definition {
|
||||
operations.add(OperationVisitor(memberAttributes.toList()).visit(ctx))
|
||||
memberAttributes.clear()
|
||||
visitOperationImpl(ctx)
|
||||
return defaultResult()
|
||||
}
|
||||
|
||||
private fun visitOperationImpl(ctx: ParserRuleContext) {
|
||||
operations.add(OperationVisitor(memberAttributes.toList(), static).visit(ctx))
|
||||
memberAttributes.clear()
|
||||
}
|
||||
|
||||
override fun visitInheritance(ctx: WebIDLParser.InheritanceContext): Definition {
|
||||
if (ctx.children != null) {
|
||||
inherited.addAll(ctx.children.filterIdentifiers().map { it.getText().trim() }.filter { it != "" })
|
||||
@@ -412,8 +417,26 @@ class DefinitionVisitor(val extendedAttributes: List<ExtendedAttribute>, val nam
|
||||
return defaultResult()
|
||||
}
|
||||
|
||||
override fun visitStaticMember(ctx: WebIDLParser.StaticMemberContext): Definition {
|
||||
static = true
|
||||
visitChildren(ctx)
|
||||
static = false
|
||||
|
||||
return defaultResult()
|
||||
}
|
||||
|
||||
override fun visitStaticMemberRest(ctx: WebIDLParser.StaticMemberRestContext): Definition {
|
||||
if (ctx.children?.any { it is OperationRestContext } ?: false) {
|
||||
visitOperationImpl(ctx)
|
||||
} else {
|
||||
visitChildren(ctx)
|
||||
}
|
||||
|
||||
return defaultResult()
|
||||
}
|
||||
|
||||
override fun visitAttributeRest(ctx: WebIDLParser.AttributeRestContext): Definition {
|
||||
with(AttributeVisitor(readOnly)) {
|
||||
with(AttributeVisitor(readOnly, static)) {
|
||||
visit(ctx)
|
||||
this@DefinitionVisitor.attributes.add(visitChildren(ctx))
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ data class Repository(
|
||||
val enums: Map<String, EnumDefinition>
|
||||
)
|
||||
|
||||
data class GenerateAttribute(val name: String, val type: String, val initializer: String?, val getterSetterNoImpl: Boolean, val readOnly: Boolean, val override: Boolean, var vararg: Boolean)
|
||||
data class GenerateAttribute(val name: String, val type: String, val initializer: String?, val getterSetterNoImpl: Boolean, val readOnly: Boolean, val override: Boolean, var vararg: Boolean, val static: Boolean)
|
||||
|
||||
val GenerateAttribute.getterNoImpl: Boolean
|
||||
get() = getterSetterNoImpl
|
||||
@@ -70,7 +70,8 @@ data class GenerateFunction(
|
||||
val name: String,
|
||||
val returnType: String,
|
||||
val arguments: List<GenerateAttribute>,
|
||||
val nativeGetterOrSetter: NativeGetterOrSetter
|
||||
val nativeGetterOrSetter: NativeGetterOrSetter,
|
||||
val static: Boolean
|
||||
)
|
||||
|
||||
data class ConstructorWithSuperTypeCall(val constructor: GenerateFunction, val constructorAttribute: ExtendedAttribute, val initTypeCall: GenerateFunctionCall?)
|
||||
|
||||
@@ -64,12 +64,13 @@ private fun Appendable.renderArgumentsDeclaration(args: List<GenerateAttribute>,
|
||||
|
||||
private fun renderCall(call: GenerateFunctionCall) = "${call.name.replaceKeywords()}(${call.arguments.map { it.replaceKeywords() }.join(", ")})"
|
||||
|
||||
private fun Appendable.renderFunctionDeclaration(f: GenerateFunction, override: Boolean) {
|
||||
indent(1)
|
||||
private fun Appendable.renderFunctionDeclaration(f: GenerateFunction, override: Boolean, level: Int = 1) {
|
||||
indent(level)
|
||||
|
||||
when (f.nativeGetterOrSetter) {
|
||||
NativeGetterOrSetter.GETTER -> append("nativeGetter ")
|
||||
NativeGetterOrSetter.SETTER -> append("nativeSetter ")
|
||||
NativeGetterOrSetter.NONE -> {}
|
||||
}
|
||||
|
||||
if (override) {
|
||||
@@ -137,19 +138,29 @@ fun Appendable.render(allTypes: Map<String, GenerateTraitOrClass>, typeNamesToUn
|
||||
val superFunctions = allSuperTypes.flatMap { it.memberFunctions }.distinct()
|
||||
val superSignatures = superAttributes.map { it.signature } merge superFunctions.map { it.signature }
|
||||
|
||||
iface.memberAttributes.filter { it !in superAttributes }.map { it.dynamicIfUnknownType(allTypes.keySet()) }.groupBy { it.signature }.reduceValues().values().forEach { arg ->
|
||||
iface.memberAttributes.filter { it !in superAttributes && !it.static }.map { it.dynamicIfUnknownType(allTypes.keySet()) }.groupBy { it.signature }.reduceValues().values().forEach { arg ->
|
||||
renderAttributeDeclaration(arg, arg.signature in superSignatures)
|
||||
}
|
||||
iface.memberFunctions.filter { it !in superFunctions }.map { it.dynamicIfUnknownType(allTypes.keySet()) }.groupBy { it.signature }.reduceValues(::betterFunction).values().forEach {
|
||||
iface.memberFunctions.filter { it !in superFunctions && !it.static }.map { it.dynamicIfUnknownType(allTypes.keySet()) }.groupBy { it.signature }.reduceValues(::betterFunction).values().forEach {
|
||||
renderFunctionDeclaration(it, it.signature in superSignatures)
|
||||
}
|
||||
if (iface.constants.isNotEmpty()) {
|
||||
|
||||
val staticAttributes = iface.memberAttributes.filter { it.static }
|
||||
val staticFunctions = iface.memberFunctions.filter { it.static }
|
||||
|
||||
if (iface.constants.isNotEmpty() || staticAttributes.isNotEmpty() || staticFunctions.isNotEmpty()) {
|
||||
appendln()
|
||||
indent(1)
|
||||
appendln("companion object {")
|
||||
iface.constants.forEach {
|
||||
renderAttributeDeclaration(it, override = false, level = 2)
|
||||
}
|
||||
staticAttributes.forEach {
|
||||
renderAttributeDeclaration(it, override = false, level = 2)
|
||||
}
|
||||
staticFunctions.forEach {
|
||||
renderFunctionDeclaration(it, override = false, level = 2)
|
||||
}
|
||||
indent(1)
|
||||
appendln("}")
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ fun FunctionType(text : String) : FunctionType {
|
||||
parameterTypes = parameters.removeSurrounding("(", ")").split(',')
|
||||
.map { it.trim() }
|
||||
.filter { !it.isEmpty() }
|
||||
.map { Attribute(name = "", type = it.removePrefix("vararg "), vararg = it.startsWith("vararg ")) }
|
||||
.map { Attribute(name = "", type = it.removePrefix("vararg "), vararg = it.startsWith("vararg "), static = false) }
|
||||
.toList(),
|
||||
returnType = returnType
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user