Skip to content

Commit 91288b0

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

File tree

2 files changed

+28
-9
lines changed
  • src
    • main/kotlin/net/sjrx/intellij/plugins/systemdunitfiles/semanticdata/optionvalues/grammar
    • test/kotlin/net/sjrx/intellij/plugins/systemdunitfiles/semanticdata/optionvalues/grammar

2 files changed

+28
-9
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ interface Combinator {
4646

4747
interface TerminalCombinator : Combinator
4848

49-
data class MatchResult(val tokens: List<String>, val matchResult: Int, val terminals: List<TerminalCombinator> )
49+
data class MatchResult(val tokens: List<String>, val matchResult: Int, val terminals: List<TerminalCombinator> ) {
50+
init {
51+
if (tokens.size != terminals.size) {
52+
throw IllegalArgumentException("Tokens and terminals must be the same size, ${tokens.size} != ${terminals.size}")
53+
}
54+
}
55+
}
56+
5057

5158
val NoMatch = MatchResult(emptyList(), -1, emptyList())
5259

@@ -65,7 +72,7 @@ class RegexTerminal(val syntaticMatchStr : String, val semanticMatchStr: String
6572
override fun SemanticMatch(value: String, offset: Int): MatchResult {
6673
val matchResult = semanticMatch.matchAt(value, offset) ?: return NoMatch
6774

68-
return MatchResult(listOf(matchResult.value), offset + matchResult.value.length, listOf())
75+
return MatchResult(listOf(matchResult.value), offset + matchResult.value.length, listOf(this))
6976
}
7077
}
7178

@@ -200,9 +207,9 @@ class OneOrMore(val combinator : Combinator) : Combinator {
200207
while (match.matchResult != -1) {
201208
index = match.matchResult
202209
tokens.addAll(match.tokens)
210+
terminals.addAll(match.terminals)
203211

204212
match = f(value, index)
205-
terminals.addAll(match.terminals)
206213
}
207214

208215
return MatchResult(tokens, index, terminals)
@@ -237,9 +244,9 @@ class ZeroOrMore(val combinator : Combinator) : Combinator {
237244
while (match.matchResult != -1) {
238245
index = match.matchResult
239246
tokens.addAll(match.tokens)
247+
terminals.addAll(match.terminals)
240248

241249
match = f(value, index)
242-
terminals.addAll(match.terminals)
243250
}
244251

245252
return MatchResult(tokens, index, terminals)

src/test/kotlin/net/sjrx/intellij/plugins/systemdunitfiles/semanticdata/optionvalues/grammar/Grammar.kt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import junit.framework.TestCase
55
class GrammarTest : TestCase() {
66

77

8+
fun TerminalType(o : TerminalCombinator): String {
9+
return o.javaClass.simpleName
10+
}
11+
12+
fun TerminalTypes(os : List<TerminalCombinator>): List<String> {
13+
return os.map { o -> TerminalType(o) }
14+
}
15+
16+
817
fun testRegexTerminalMatches() {
918
/**
1019
* Fixture Setup
@@ -28,11 +37,14 @@ class GrammarTest : TestCase() {
2837
/**
2938
* Execute SUT & Verification
3039
*/
31-
assertEquals(semValid.length, regexTerminal.SemanticMatch(semValid, 0).matchResult)
32-
assertEquals(listOf("1K"), regexTerminal.SemanticMatch(semValid, 0).tokens)
33-
34-
assertEquals(semValid.length, regexTerminal.SyntacticMatch(semValid, 0).matchResult)
35-
assertEquals(listOf("1K"), regexTerminal.SyntacticMatch(semValid, 0).tokens)
40+
var match = regexTerminal.SemanticMatch(semValid, 0)
41+
assertEquals(semValid.length, match.matchResult)
42+
assertEquals(listOf("1K"), match.tokens)
43+
assertEquals(listOf("RegexTerminal"), TerminalTypes(match.terminals))
44+
45+
match = regexTerminal.SyntacticMatch(semValid, 0)
46+
assertEquals(semValid.length, match.matchResult)
47+
assertEquals(listOf("1K"), match.tokens)
3648

3749
assertEquals(NoMatch, regexTerminal.SemanticMatch(synValid, 0))
3850

0 commit comments

Comments
 (0)