Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add setlike #6

Merged
merged 2 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions webidl-util/src/main/kotlin/de/fabmax/webidl/model/IdlInterface.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package de.fabmax.webidl.model

import de.fabmax.webidl.parser.ParserException

class IdlInterface(builder: Builder) : IdlDecoratedElement(builder) {
val attributes = List(builder.attributes.size) { builder.attributes[it].build() }
val functions: List<IdlFunction>
val functionsByName: Map<String, IdlFunction>
val functions: List<IdlFunction> = builder.functions.flatMap { it.build() }
val functionsByName: Map<String, IdlFunction> = functions.associateBy { it.name }
val superInterfaces = builder.superInterfaces.toList()
val sourcePackage = builder.sourcePackage
val isMixin = builder.isMixin
val setLike = builder.setLike?.build()

init {
functions = builder.functions.flatMap { it.build() }
functionsByName = functions.associateBy { it.name }
if (setLike != null) {
if (functions.isNotEmpty()) throw ParserException("functions and setlike are mutually exclusive")
if (attributes.isNotEmpty()) throw ParserException("attributes and setlike are mutually exclusive")
}
}

fun finishModel(parentModel: IdlModel) {
Expand Down Expand Up @@ -43,6 +48,7 @@ class IdlInterface(builder: Builder) : IdlDecoratedElement(builder) {
val superInterfaces = mutableSetOf<String>()
var sourcePackage = ""
var isMixin = false
var setLike: IdlSetLike.Builder? = null

fun addAttribute(attribute: IdlAttribute.Builder) {
attributes += attribute
Expand Down
25 changes: 25 additions & 0 deletions webidl-util/src/main/kotlin/de/fabmax/webidl/model/IdlSetLike.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package de.fabmax.webidl.model

class IdlSetLike private constructor(builder: Builder) : IdlElement(builder) {
val type = builder.type

var parentInterface: IdlInterface? = null
private set

fun finishModel(parentInterface: IdlInterface) {
this.parentModel = parentInterface.parentModel
this.parentInterface = parentInterface
}

override fun toString(indent: String): String {
val str = StringBuilder(indent)
str.append("readonly setlike<$type>").append(";")
return str.toString()
}

class Builder(var type: IdlType) : IdlElement.Builder("setlike") {

fun build() = IdlSetLike(this)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package de.fabmax.webidl.parser


import de.fabmax.webidl.model.IdlSetLike
import de.fabmax.webidl.model.IdlType

class SetLikeParser(parserState: WebIdlParser.ParserState) : ElementParser(parserState,
WebIdlParserType.Function
) {
lateinit var builder: IdlSetLike.Builder

override suspend fun parse(): String {
val type = (popUntilPattern(";")?.first ?: parserException("Failed parsing setlike"))
.substringAfter("<")
.substringBefore(">")
.let { IdlType(it, false) }

builder = IdlSetLike.Builder(type)
parserState.parentParser<InterfaceParser>().builder.setLike = builder
parserState.popParser()
return builder.name
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ enum class WebIdlParserType {
},

Interface {
override fun possibleChildren() = listOf(Decorators, LineComment, BlockComment, Attribute, Function)
override fun possibleChildren() = listOf(Decorators, LineComment, BlockComment, Attribute, Function, SetLike)
override suspend fun matches(stream: WebIdlStream) = stream.startsWith("interface")
override fun newParser(parserState: WebIdlParser.ParserState) = parserState.pushParser(
InterfaceParser(
Expand All @@ -46,6 +46,16 @@ enum class WebIdlParserType {
)
},

SetLike {
override fun possibleChildren(): List<WebIdlParserType> = emptyList()
override suspend fun matches(stream: WebIdlStream) = stream.startsWith("readonly setlike")
override fun newParser(parserState: WebIdlParser.ParserState) = parserState.pushParser(
SetLikeParser(
parserState
)
)
},

Attribute {
override fun possibleChildren(): List<WebIdlParserType> = emptyList()
override suspend fun matches(stream: WebIdlStream) = stream.startsWith("attribute")
Expand Down
13 changes: 12 additions & 1 deletion webidl-util/src/test/kotlin/ParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import kotlin.test.assertNotNull

class ParserTest {

Expand All @@ -22,7 +23,7 @@ class ParserTest {
}

assertTrue(model.dictionaries.size == 1)
assertTrue(model.interfaces.size == 5)
assertTrue(model.interfaces.size == 6)


assertEquals("AnDictionary", model.dictionaries[0].name)
Expand Down Expand Up @@ -68,6 +69,10 @@ class ParserTest {
assertEquals("JavaErrorCallback", model.interfaces[javaErrorCallbackIndex].name)
assertEquals("ErrorCallback", model.interfaces[javaErrorCallbackIndex].getDecoratorValue("JSImplementation", ""))

val setLikeIndex = 5
assertEquals("SetLikeInterface", model.interfaces[setLikeIndex].name)
assertNotNull(model.interfaces[setLikeIndex].setLike)
assertEquals("DOMString", model.interfaces[setLikeIndex].setLike?.type?.typeName)
}

@Test(expected = ParserException::class)
Expand All @@ -82,6 +87,12 @@ class ParserTest {
WebIdlParser.parseFromInputStream(inStream)
}

@Test(expected = ParserException::class)
fun parserTestBadSetLike() {
val inStream = ParserTest::class.java.classLoader.getResourceAsStream("bad-setlike.idl")!!
WebIdlParser.parseFromInputStream(inStream)
}

@Test
fun generatorJsTest() {
val inStream = ParserTest::class.java.classLoader.getResourceAsStream("test.idl")!!
Expand Down
5 changes: 5 additions & 0 deletions webidl-util/src/test/resources/bad-setlike.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

interface SetLikeInterface {
readonly setlike<DOMString>;
void JavaErrorCallback();
};
4 changes: 4 additions & 0 deletions webidl-util/src/test/resources/test.idl
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ dictionary AnDictionary {
required unsigned long long requiredMember;
USVString someMemberWithDefaultValue = "my string";
};

interface SetLikeInterface {
readonly setlike<DOMString>;
};
Loading