/
Directive.kt
176 lines (145 loc) · 5.84 KB
/
Directive.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.test.directives.model
import org.jetbrains.kotlin.test.util.joinToArrayString
// --------------------------- Directive declaration ---------------------------
enum class DirectiveApplicability(
val forGlobal: Boolean = false,
val forModule: Boolean = false,
val forFile: Boolean = false
) {
Any(forGlobal = true, forModule = true, forFile = true),
Global(forGlobal = true, forModule = true),
Module(forModule = true),
File(forFile = true)
}
sealed class Directive(val name: String, val description: String, val applicability: DirectiveApplicability) {
override fun toString(): String {
return name
}
}
class SimpleDirective(
name: String,
description: String,
applicability: DirectiveApplicability
) : Directive(name, description, applicability)
class StringDirective(
name: String,
description: String,
applicability: DirectiveApplicability,
val multiLine: Boolean
) : Directive(name, description, applicability)
class ValueDirective<T : Any>(
name: String,
description: String,
applicability: DirectiveApplicability,
val parser: (String) -> T?
) : Directive(name, description, applicability)
// --------------------------- Registered directive ---------------------------
abstract class RegisteredDirectives : Iterable<Directive> {
companion object {
val Empty = RegisteredDirectivesImpl(emptyList(), emptyMap(), emptyMap())
}
abstract operator fun contains(directive: Directive): Boolean
abstract operator fun get(directive: StringDirective): List<String>
abstract operator fun <T : Any> get(directive: ValueDirective<T>): List<T>
abstract fun isEmpty(): Boolean
}
class RegisteredDirectivesImpl(
private val simpleDirectives: List<SimpleDirective>,
private val stringDirectives: Map<StringDirective, List<String>>,
private val valueDirectives: Map<ValueDirective<*>, List<Any>>
) : RegisteredDirectives() {
override operator fun contains(directive: Directive): Boolean {
return when (directive) {
is SimpleDirective -> directive in simpleDirectives
is StringDirective -> directive in stringDirectives
is ValueDirective<*> -> directive in valueDirectives
}
}
override operator fun get(directive: StringDirective): List<String> {
return stringDirectives[directive] ?: emptyList()
}
override fun <T : Any> get(directive: ValueDirective<T>): List<T> {
@Suppress("UNCHECKED_CAST")
return valueDirectives[directive] as List<T>? ?: emptyList()
}
override fun isEmpty(): Boolean {
return simpleDirectives.isEmpty() && stringDirectives.isEmpty() && valueDirectives.isEmpty()
}
override fun toString(): String {
return buildString {
simpleDirectives.forEach { appendLine(" $it") }
stringDirectives.forEach { (d, v) -> appendLine(" $d: ${v.joinToArrayString()}") }
valueDirectives.forEach { (d, v) -> appendLine(" $d: ${v.joinToArrayString()}") }
}
}
@OptIn(ExperimentalStdlibApi::class)
override fun iterator(): Iterator<Directive> {
return buildList {
addAll(simpleDirectives)
addAll(stringDirectives.keys)
addAll(valueDirectives.keys)
}.iterator()
}
}
class ComposedRegisteredDirectives(
private val containers: List<RegisteredDirectives>
) : RegisteredDirectives() {
companion object {
operator fun invoke(vararg containers: RegisteredDirectives): RegisteredDirectives {
val notEmptyContainers = containers.filterNot { it.isEmpty() }
return when (notEmptyContainers.size) {
0 -> Empty
1 -> notEmptyContainers.single()
else -> ComposedRegisteredDirectives(notEmptyContainers)
}
}
}
override fun contains(directive: Directive): Boolean {
return containers.any { directive in it }
}
override fun get(directive: StringDirective): List<String> {
return containers.flatMap { it[directive] }
}
override fun <T : Any> get(directive: ValueDirective<T>): List<T> {
return containers.flatMap { it[directive] }
}
override fun isEmpty(): Boolean {
return containers.all { it.isEmpty() }
}
override fun iterator(): Iterator<Directive> {
return containers.flatten().iterator()
}
}
// --------------------------- Utils ---------------------------
fun RegisteredDirectives.singleValue(directive: StringDirective): String {
return singleOrZeroValue(directive) ?: error("No values passed to $directive")
}
fun RegisteredDirectives.singleOrZeroValue(directive: StringDirective): String? {
val values = this[directive]
return when (values.size) {
0 -> null
1 -> values.single()
else -> error("Too many values passed to $directive")
}
}
fun RegisteredDirectives.notEmptyValues(directive: StringDirective): List<String> = this[directive].ifEmpty {
error("No values passed to $directive")
}
fun <T : Any> RegisteredDirectives.singleValue(directive: ValueDirective<T>): T {
return singleOrZeroValue(directive) ?: error("No values passed to $directive")
}
fun <T : Any> RegisteredDirectives.singleOrZeroValue(directive: ValueDirective<T>): T? {
val values = this[directive]
return when (values.size) {
0 -> null
1 -> values.single()
else -> error("Too many values passed to $directive: ${values.joinToArrayString()}")
}
}
fun <T : Any> RegisteredDirectives.notEmptyValues(directive: ValueDirective<T>): List<T> = this[directive].ifEmpty {
error("No values passed to $directive")
}