-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from ygdrasil-io/feature/add-dictionary-parsing
Add support for parsing and modeling WebIDL dictionaries
- Loading branch information
Showing
12 changed files
with
232 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
name: Tests | ||
|
||
on: | ||
push: | ||
branches: [ main, 'release/**', 'feature/**', 'fix/**' ] | ||
paths-ignore: | ||
- '*.md' | ||
- '*.yml' | ||
- '.github/workflows/**' | ||
pull_request: | ||
branches: [ main, 'release/**', 'feature/**', 'fix/**' ] | ||
workflow_dispatch: | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
tests: | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
# add macos-latest when upgrading to JDK 17 | ||
os: [ ubuntu-latest, windows-latest ] | ||
runs-on: ${{ matrix.os }} | ||
env: | ||
CI: "true" | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Set up JDK | ||
uses: actions/setup-java@v4 | ||
with: | ||
distribution: 'temurin' | ||
java-version: 8 | ||
cache: 'gradle' | ||
- name: Cache Gradle packages | ||
uses: actions/cache@v4 | ||
with: | ||
path: ~/.gradle/caches | ||
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle.kts') }} | ||
restore-keys: ${{ runner.os }}-gradle | ||
- name: assemble | ||
run: ./gradlew assemble | ||
- name: build | ||
run: ./gradlew build | ||
- name: check | ||
run: ./gradlew check |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
webidl-util/src/main/kotlin/de/fabmax/webidl/model/IdlDictionary.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package de.fabmax.webidl.model | ||
|
||
class IdlDictionary(builder: Builder) : IdlDecoratedElement(builder) { | ||
val members = List(builder.members.size) { builder.members[it].build() } | ||
val superDictionaries = builder.superDictionaries.toList() | ||
val sourcePackage = builder.sourcePackage | ||
|
||
fun finishModel(parentModel: IdlModel) { | ||
this.parentModel = parentModel | ||
members.forEach { it.finishModel(this) } | ||
} | ||
|
||
override fun toString(indent: String): String { | ||
val subIndent = "$indent " | ||
val str = StringBuilder() | ||
str.append(decoratorsToStringOrEmpty(indent, "\n")) | ||
str.append("${indent}dictionary $name { ") | ||
|
||
if (members.isNotEmpty()) { | ||
str.append("\n") | ||
str.append(members.joinToString("\n", postfix = "\n", transform = { it.toString(subIndent) })) | ||
} | ||
str.append("$indent};") | ||
superDictionaries.forEach { str.append("\n$indent$name implements $it;") } | ||
return str.toString() | ||
} | ||
|
||
class Builder(name: String) : IdlDecoratedElement.Builder(name) { | ||
val members = mutableListOf<IdlMember.Builder>() | ||
val superDictionaries = mutableSetOf<String>() | ||
var sourcePackage = "" | ||
|
||
fun addAttribute(attribute: IdlMember.Builder) { members += attribute } | ||
fun build() = IdlDictionary(this) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
webidl-util/src/main/kotlin/de/fabmax/webidl/model/IdlMember.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package de.fabmax.webidl.model | ||
|
||
class IdlMember private constructor(builder: Builder) : IdlDecoratedElement(builder) { | ||
val type = builder.type | ||
val isRequired = builder.isRequired | ||
var defaultValue: String? = builder.defaultValue | ||
|
||
var parentDictionary: IdlDictionary? = null | ||
private set | ||
|
||
fun finishModel(parentDictionary: IdlDictionary?) { | ||
this.parentDictionary = parentDictionary | ||
this.parentModel = parentDictionary?.parentModel ?: error("Parent model missing") | ||
} | ||
|
||
override fun toString(indent: String): String { | ||
val str = StringBuilder(indent) | ||
str.append(decoratorsToStringOrEmpty(postfix = " ")) | ||
if (isRequired) { | ||
str.append("required ") | ||
} | ||
str.append(type).append(" ").append(name) | ||
if (defaultValue != null) { str.append(" = $defaultValue")} | ||
str.append(";") | ||
return str.toString() | ||
} | ||
|
||
class Builder(name: String, var type: IdlType) : IdlDecoratedElement.Builder(name) { | ||
var isRequired = false | ||
var defaultValue: String? = null | ||
|
||
fun build() = IdlMember(this) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
webidl-util/src/main/kotlin/de/fabmax/webidl/parser/DictionaryParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package de.fabmax.webidl.parser | ||
|
||
import de.fabmax.webidl.model.IdlDictionary | ||
|
||
class DictionaryParser(parserState: WebIdlParser.ParserState) : ElementParser(parserState, | ||
WebIdlParserType.Dictionary | ||
) { | ||
lateinit var builder: IdlDictionary.Builder | ||
|
||
override suspend fun parse(): String { | ||
popToken("dictionary") | ||
|
||
val interfaceName = popUntilPattern("\\{") ?: parserException("Failed parsing dictionary name") | ||
builder = IdlDictionary.Builder(interfaceName.first) | ||
builder.sourcePackage = parserState.sourcePackage | ||
parserState.popDecorators(builder) | ||
parserState.parentParser<RootParser>().builder.addDictionary(builder) | ||
|
||
parseChildren("}") | ||
|
||
popToken(";") | ||
parserState.popParser() | ||
return interfaceName.second | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
webidl-util/src/main/kotlin/de/fabmax/webidl/parser/MemberParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package de.fabmax.webidl.parser | ||
|
||
import de.fabmax.webidl.model.IdlMember | ||
|
||
class MemberParser(parserState: WebIdlParser.ParserState) : ElementParser(parserState, | ||
WebIdlParserType.Dictionary | ||
) { | ||
lateinit var builder: IdlMember.Builder | ||
|
||
override suspend fun parse(): String { | ||
val isRequired = popIfPresent("required") | ||
val type = parseType() | ||
val tokens = popUntilPattern(";") ?: parserException("Failed parsing member") | ||
builder = if (tokens.first.contains("=")) { | ||
val name = tokens.first.substringBefore("=").trim() | ||
IdlMember.Builder(name, type).also { | ||
it.defaultValue = tokens.first.substringAfter("=").trim() | ||
} | ||
} else IdlMember.Builder(tokens.first, type) | ||
builder.isRequired = isRequired | ||
parserState.popDecorators(builder) | ||
parserState.parentParser<DictionaryParser>().builder.addAttribute(builder) | ||
|
||
parserState.popParser() | ||
return tokens.second | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters