Skip to content

Commit 797b4dd

Browse files
author
Steve Ramage
committed
feat: Add support for Token Based validators (WIP #2)
1 parent cdcdb81 commit 797b4dd

File tree

1 file changed

+35
-22
lines changed
  • src/main/kotlin/net/sjrx/intellij/plugins/systemdunitfiles/semanticdata/optionvalues/grammar

1 file changed

+35
-22
lines changed

src/main/kotlin/net/sjrx/intellij/plugins/systemdunitfiles/semanticdata/optionvalues/grammar/GrammarOptionValue.kt

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.sjrx.intellij.plugins.systemdunitfiles.semanticdata.optionvalues.grammar
22

33
import com.intellij.openapi.project.Project
4+
import com.jediterm.terminal.Terminal
45
import net.sjrx.intellij.plugins.systemdunitfiles.semanticdata.Validator
56
import net.sjrx.intellij.plugins.systemdunitfiles.semanticdata.optionvalues.OptionValueInformation
67
import org.apache.ivy.plugins.version.Match
@@ -41,53 +42,54 @@ interface Combinator {
4142
*/
4243
fun SemanticMatch(value : String, offset: Int): MatchResult
4344

44-
4545
}
4646

47-
data class MatchResult(val tokens: List<String>, val matchResult: Int)
47+
interface TerminalCombinator : Combinator
48+
49+
data class MatchResult(val tokens: List<String>, val matchResult: Int, val terminals: List<TerminalCombinator> )
4850

49-
val NoMatch = MatchResult(emptyList(), -1)
51+
val NoMatch = MatchResult(emptyList(), -1, emptyList())
5052

51-
class RegexTerminal(val syntaticMatchStr : String, val semanticMatchStr: String ) : Combinator {
53+
class RegexTerminal(val syntaticMatchStr : String, val semanticMatchStr: String ) : TerminalCombinator {
5254

5355
val syntaticMatch = syntaticMatchStr.toRegex()
5456
val semanticMatch = semanticMatchStr.toRegex()
5557

5658
override fun SyntacticMatch(value: String, offset: Int): MatchResult {
5759
val matchResult = syntaticMatch.matchAt(value, offset) ?: return NoMatch
5860

59-
return MatchResult(listOf(matchResult.value), offset + matchResult.value.length)
61+
return MatchResult(listOf(matchResult.value), offset + matchResult.value.length, listOf(this))
6062

6163
}
6264

6365
override fun SemanticMatch(value: String, offset: Int): MatchResult {
6466
val matchResult = semanticMatch.matchAt(value, offset) ?: return NoMatch
6567

66-
return MatchResult(listOf(matchResult.value), offset + matchResult.value.length)
68+
return MatchResult(listOf(matchResult.value), offset + matchResult.value.length, listOf())
6769
}
6870
}
6971

7072
val BYTES = RegexTerminal("[0-9]+[a-zA-Z]*\\s*", "[0-9]+[KMGT]?\\s*")
7173
val DEVICE = RegexTerminal("\\S+\\s*", "/[^\\u0000.]+\\s*")
7274
val IOPS = RegexTerminal("[0-9]+[a-zA-Z]*\\s*", "[0-9]+[KMGT]?\\s*")
7375

74-
class LiteralChoiceTerminal(vararg val choices: String) : Combinator {
75-
override fun SyntacticMatch(value: String, offset: Int): MatchResult {
76+
class LiteralChoiceTerminal(vararg val choices: String) : TerminalCombinator {
77+
78+
private fun match(value: String, offset: Int): MatchResult {
7679
for (choice in choices) {
7780
if (value.substring(offset).startsWith(choice)) {
78-
return MatchResult(listOf(choice), offset + choice.length)
81+
return MatchResult(listOf(choice), offset + choice.length, listOf(this))
7982
}
8083
}
8184
return NoMatch
8285
}
8386

87+
override fun SyntacticMatch(value: String, offset: Int): MatchResult {
88+
return match(value, offset)
89+
}
90+
8491
override fun SemanticMatch(value: String, offset: Int): MatchResult {
85-
for (choice in choices) {
86-
if (value.substring(offset).startsWith(choice)) {
87-
return MatchResult(listOf(choice), offset + choice.length)
88-
}
89-
}
90-
return NoMatch
92+
return match(value, offset)
9193
}
9294
}
9395

@@ -114,6 +116,7 @@ class SequenceCombinator(vararg val tokens: Combinator) : Combinator{
114116
override fun SyntacticMatch(value: String, offset: Int): MatchResult {
115117
var index = offset
116118
val resultTokens = mutableListOf<String>()
119+
val resultTerminals = mutableListOf<TerminalCombinator>()
117120

118121
for (token in tokens) {
119122
val match = token.SyntacticMatch(value, index)
@@ -124,14 +127,17 @@ class SequenceCombinator(vararg val tokens: Combinator) : Combinator{
124127
}
125128
index = match.matchResult
126129
resultTokens.addAll(match.tokens)
130+
resultTerminals.addAll(match.terminals)
131+
127132
}
128-
return MatchResult(resultTokens, index)
133+
return MatchResult(resultTokens, index, resultTerminals)
129134
}
130135

131136
override fun SemanticMatch(value: String, offset: Int): MatchResult {
132137
var index = offset
133138

134139
val resultTokens = mutableListOf<String>()
140+
val resultTerminals = mutableListOf<TerminalCombinator>()
135141

136142
for (token in tokens) {
137143
val match = token.SemanticMatch(value, index)
@@ -141,9 +147,10 @@ class SequenceCombinator(vararg val tokens: Combinator) : Combinator{
141147
}
142148
index = match.matchResult
143149
resultTokens.addAll(match.tokens)
150+
resultTerminals.addAll(match.terminals)
144151

145152
}
146-
return MatchResult(resultTokens, index)
153+
return MatchResult(resultTokens, index, resultTerminals)
147154
}
148155
}
149156

@@ -184,6 +191,8 @@ class OneOrMore(val combinator : Combinator) : Combinator {
184191
var match = f(value, index)
185192

186193
val tokens = mutableListOf<String>()
194+
val terminals = mutableListOf<TerminalCombinator>()
195+
187196
if (match.matchResult == -1) {
188197
return NoMatch
189198
}
@@ -193,9 +202,10 @@ class OneOrMore(val combinator : Combinator) : Combinator {
193202
tokens.addAll(match.tokens)
194203

195204
match = f(value, index)
205+
terminals.addAll(match.terminals)
196206
}
197207

198-
return MatchResult(tokens, index)
208+
return MatchResult(tokens, index, terminals)
199209
}
200210

201211
override fun SyntacticMatch(value: String, offset: Int): MatchResult {
@@ -218,18 +228,21 @@ class ZeroOrMore(val combinator : Combinator) : Combinator {
218228
var match = f(value, index)
219229

220230
val tokens = mutableListOf<String>()
231+
val terminals = mutableListOf<TerminalCombinator>()
232+
221233
if (match.matchResult == -1) {
222-
return MatchResult(tokens, offset)
234+
return MatchResult(tokens, offset, terminals)
223235
}
224236

225237
while (match.matchResult != -1) {
226238
index = match.matchResult
227239
tokens.addAll(match.tokens)
228240

229241
match = f(value, index)
242+
terminals.addAll(match.terminals)
230243
}
231244

232-
return MatchResult(tokens, index)
245+
return MatchResult(tokens, index, terminals)
233246
}
234247

235248
override fun SyntacticMatch(value: String, offset: Int): MatchResult {
@@ -245,15 +258,15 @@ class ZeroOrMore(val combinator : Combinator) : Combinator {
245258
class EOF : Combinator {
246259
override fun SyntacticMatch(value: String, offset: Int): MatchResult {
247260
return if (offset == value.length) {
248-
MatchResult(emptyList(), offset)
261+
MatchResult(emptyList(), offset, emptyList())
249262
} else {
250263
NoMatch
251264
}
252265
}
253266

254267
override fun SemanticMatch(value: String, offset: Int): MatchResult {
255268
return if (offset == value.length) {
256-
MatchResult(emptyList(), offset)
269+
MatchResult(emptyList(), offset, emptyList())
257270
} else {
258271
NoMatch
259272
}

0 commit comments

Comments
 (0)