Skip to content

Commit 645aae5

Browse files
author
Steve Ramage
committed
feat: add support for Network.Address
1 parent 8f55421 commit 645aae5

File tree

15 files changed

+125
-33
lines changed

15 files changed

+125
-33
lines changed

src/main/kotlin/net/sjrx/intellij/plugins/systemdunitfiles/actions/NewUnitFileAction.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,3 @@ class NewUnitFileAction : CreateFileFromTemplateAction(
2727
return "New Systemd Unit File"
2828
}
2929
}
30-
31-
32-
33-
// language=python
34-
val foo = """
35-
import foo
36-
""".trimIndent()

src/main/kotlin/net/sjrx/intellij/plugins/systemdunitfiles/actions/file.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,20 @@ open class AlternativeCombinator(vararg val tokens: Combinator) : Combinator {
3838
return match(value, offset, Combinator::SemanticMatch)
3939
}
4040

41-
override fun toString(): String {
42-
return "Alt(" + tokens.joinToString(",") { it.toString() } + ")"
41+
override fun toString(): String = toStringIndented(0)
42+
43+
override fun toStringIndented(indent: Int): String {
44+
val prefix = " ".repeat(indent)
45+
val sb = StringBuilder()
46+
sb.append(prefix).append("Alt(\n")
47+
for (token in tokens) {
48+
if (token is SequenceCombinator || token is AlternativeCombinator || token is Repeat || token is ZeroOrOne || token is ZeroOrMore || token is OneOrMore) {
49+
sb.append(token.toStringIndented(indent + 1)).append("\n")
50+
} else {
51+
sb.append(" ".repeat(indent + 1)).append(token.toString()).append("\n")
52+
}
53+
}
54+
sb.append(prefix).append(")")
55+
return sb.toString()
4356
}
4457
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ interface Combinator {
3232
*/
3333
fun SemanticMatch(value : String, offset: Int): MatchResult
3434

35+
fun toStringIndented(indent: Int): String
3536
}

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,23 @@ val IPV6_SEVEN_HEXTET_BEFORE_ZERO_COMP = SequenceCombinator(IPV6_HEXTET, COLON,I
2929

3030

3131
val IPV6_IPV4_SUFFIX_FULL = SequenceCombinator(IPV6_HEXTET, COLON, IPV6_HEXTET, COLON, IPV6_HEXTET, COLON, IPV6_HEXTET, COLON, IPV6_HEXTET, COLON, IPV6_HEXTET, COLON, IPV4_ADDR)
32-
val IPV6_IPV4_SUFFIX_ZERO_HEXTET_BEFORE_ZERO_COMP = SequenceCombinator(DOUBLE_COLON, ZeroOrOne(SequenceCombinator(Repeat(SequenceCombinator(IPV6_HEXTET, COLON), 0, 6), IPV4_ADDR)))
33-
val IPV6_IPV4_SUFFIX_ONE_HEXTET_BEFORE_ZERO_COMP = SequenceCombinator(IPV6_HEXTET, DOUBLE_COLON, ZeroOrOne(SequenceCombinator(Repeat(SequenceCombinator(IPV6_HEXTET, COLON), 0, 5), IPV4_ADDR)))
34-
val IPV6_IPV4_SUFFIX_TWO_HEXTET_BEFORE_ZERO_COMP = SequenceCombinator(IPV6_HEXTET, COLON, IPV6_HEXTET, DOUBLE_COLON, ZeroOrOne(SequenceCombinator(Repeat(SequenceCombinator(IPV6_HEXTET, COLON), 0, 4), IPV4_ADDR)))
32+
val IPV6_IPV4_SUFFIX_ZERO_HEXTET_BEFORE_ZERO_COMP = SequenceCombinator(DOUBLE_COLON,SequenceCombinator(Repeat(SequenceCombinator(IPV6_HEXTET, COLON), 0, 6), IPV4_ADDR))
33+
val IPV6_IPV4_SUFFIX_ONE_HEXTET_BEFORE_ZERO_COMP = SequenceCombinator(IPV6_HEXTET, DOUBLE_COLON, SequenceCombinator(Repeat(SequenceCombinator(IPV6_HEXTET, COLON), 0, 5), IPV4_ADDR))
34+
val IPV6_IPV4_SUFFIX_TWO_HEXTET_BEFORE_ZERO_COMP = SequenceCombinator(IPV6_HEXTET, COLON, IPV6_HEXTET, DOUBLE_COLON, SequenceCombinator(Repeat(SequenceCombinator(IPV6_HEXTET, COLON), 0, 4), IPV4_ADDR))
35+
val IPV6_IPV4_SUFFIX_THREE_HEXTET_BEFORE_ZERO_COMP = SequenceCombinator(IPV6_HEXTET, COLON, IPV6_HEXTET, COLON, IPV6_HEXTET, DOUBLE_COLON,SequenceCombinator(Repeat(SequenceCombinator(IPV6_HEXTET, COLON), 0, 3), IPV4_ADDR))
36+
val IPV6_IPV4_SUFFIX_FOUR_HEXTET_BEFORE_ZERO_COMP = SequenceCombinator(IPV6_HEXTET, COLON, IPV6_HEXTET, COLON, IPV6_HEXTET, COLON, IPV6_HEXTET, DOUBLE_COLON, SequenceCombinator(Repeat(SequenceCombinator(IPV6_HEXTET, COLON), 0, 2), IPV4_ADDR))
37+
val IPV6_IPV4_SUFFIX_FIVE_HEXTET_BEFORE_ZERO_COMP = SequenceCombinator(IPV6_HEXTET, COLON, IPV6_HEXTET, COLON, IPV6_HEXTET, COLON, IPV6_HEXTET, COLON, IPV6_HEXTET, DOUBLE_COLON, IPV4_ADDR)
3538

36-
val IPV6_ALL_ZEROS = DOUBLE_COLON
39+
//val IPV6_ALL_ZEROS = DOUBLE_COLON
3740

3841
val IPV6_ADDR = AlternativeCombinator(
42+
IPV6_IPV4_SUFFIX_FULL,
43+
IPV6_IPV4_SUFFIX_ZERO_HEXTET_BEFORE_ZERO_COMP,
44+
IPV6_IPV4_SUFFIX_ONE_HEXTET_BEFORE_ZERO_COMP,
45+
IPV6_IPV4_SUFFIX_TWO_HEXTET_BEFORE_ZERO_COMP,
46+
IPV6_IPV4_SUFFIX_THREE_HEXTET_BEFORE_ZERO_COMP,
47+
IPV6_IPV4_SUFFIX_FOUR_HEXTET_BEFORE_ZERO_COMP,
48+
IPV6_IPV4_SUFFIX_FIVE_HEXTET_BEFORE_ZERO_COMP,
3949
IPV6_FULL_SPECIFIED,
4050
IPV6_SEVEN_HEXTET_BEFORE_ZERO_COMP,
4151
IPV6_SIX_HEXTET_BEFORE_ZERO_COMP,
@@ -44,17 +54,16 @@ val IPV6_ADDR = AlternativeCombinator(
4454
IPV6_THREE_HEXTET_BEFORE_ZERO_COMP,
4555
IPV6_TWO_HEXTET_BEFORE_ZERO_COMP,
4656
IPV6_ONE_HEXTET_BEFORE_ZERO_COMP,
47-
// Must go last because it's the most general
57+
// Must go last because it's the most general and can match ::
4858
IPV6_ZERO_HEXTET_BEFORE_ZERO_COMP,
4959

50-
// IPV6_IPV4_SUFFIX_ONE_HEXTET_BEFORE_ZERO_COMP,
51-
// IPV6_IPV4_SUFFIX_TWO_HEXTET_BEFORE_ZERO_COMP,
52-
// IPV6_ALL_ZEROS,
60+
// I suspect maybe that this one is redundant
61+
//IPV6_ALL_ZEROS,
5362
)
5463

5564
val IPV6_ADDR_AND_PREFIX_LENGTH = SequenceCombinator(IPV6_ADDR, CIDR_SEPARATOR, IntegerTerminal(64, 129))
5665

5766

5867
var IP_ADDR_AND_PREFIX_LENGTH = AlternativeCombinator(
59-
//IPV4_ADDR_AND_PREFIX_LENGTH,
68+
IPV4_ADDR_AND_PREFIX_LENGTH,
6069
IPV6_ADDR_AND_PREFIX_LENGTH)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ class EOF : Combinator {
1616
NoMatch
1717
}
1818
}
19+
20+
override fun toStringIndented(indent: Int): String {
21+
return "EOF"
22+
}
1923
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,13 @@ class FlexibleLiteralChoiceTerminal(vararg val choices: String) : TerminalCombin
9393

9494
override fun toString(): String {
9595
return if (choices.size == 1) {
96-
"FlexLitChoice(\"${choices[0]}\")"
96+
"Literal(\"${choices[0]}\")"
9797
} else {
9898
"FlexLitChoice(" + choices.joinToString(",") { "\"$it\"" } + ")"
9999
}
100100
}
101+
102+
override fun toStringIndented(indent: Int): String {
103+
return toString()
104+
}
101105
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,18 @@ class OneOrMore(val combinator : Combinator) : Combinator {
4040
return match(value, offset, combinator::SemanticMatch)
4141
}
4242

43-
override fun toString(): String {
44-
return "OneOrMore(${combinator})"
43+
override fun toString(): String = toStringIndented(0)
44+
45+
override fun toStringIndented(indent: Int): String {
46+
val prefix = " ".repeat(indent)
47+
val sb = StringBuilder()
48+
sb.append(prefix).append("OneOrMore(\n")
49+
if (combinator is SequenceCombinator || combinator is AlternativeCombinator || combinator is Repeat || combinator is ZeroOrOne || combinator is ZeroOrMore || combinator is OneOrMore) {
50+
sb.append(combinator.toStringIndented(indent + 1)).append("\n")
51+
} else {
52+
sb.append(" ".repeat(indent + 1)).append(combinator.toString()).append("\n")
53+
}
54+
sb.append(prefix).append(")")
55+
return sb.toString()
4556
}
4657
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,18 @@ class Repeat(val combinator : Combinator, val minInclusive: Int, val maxExclusiv
6262
return match(value, offset, combinator::SemanticMatch)
6363
}
6464

65-
override fun toString(): String {
66-
return "Repeat(${combinator},$minInclusive,$maxExclusive)"
65+
override fun toString(): String = toStringIndented(0)
66+
67+
override fun toStringIndented(indent: Int): String {
68+
val prefix = " ".repeat(indent)
69+
val sb = StringBuilder()
70+
sb.append(prefix).append("Repeat($minInclusive,$maxExclusive\n")
71+
if (combinator is SequenceCombinator || combinator is AlternativeCombinator || combinator is Repeat || combinator is ZeroOrOne || combinator is ZeroOrMore || combinator is OneOrMore) {
72+
sb.append(combinator.toStringIndented(indent + 1)).append("\n")
73+
} else {
74+
sb.append(" ".repeat(indent + 1)).append(combinator.toString()).append("\n")
75+
}
76+
sb.append(prefix).append(")")
77+
return sb.toString()
6778
}
6879
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,20 @@ open class SequenceCombinator(vararg val tokens: Combinator) : Combinator {
5555
return MatchResult(resultTokens, index, resultTerminals, maxLength)
5656
}
5757

58-
override fun toString(): String {
59-
return "Seq(" + tokens.joinToString(",") { it.toString() } + ")"
58+
override fun toString(): String = toStringIndented(0)
59+
60+
override fun toStringIndented(indent: Int): String {
61+
val prefix = " ".repeat(indent)
62+
val sb = StringBuilder()
63+
sb.append(prefix).append("Seq(\n")
64+
for (token in tokens) {
65+
if (token is SequenceCombinator || token is AlternativeCombinator || token is Repeat || token is ZeroOrOne || token is ZeroOrMore || token is OneOrMore) {
66+
sb.append(token.toStringIndented(indent + 1)).append("\n")
67+
} else {
68+
sb.append(" ".repeat(indent + 1)).append(token.toString()).append("\n")
69+
}
70+
}
71+
sb.append(prefix).append(")")
72+
return sb.toString()
6073
}
6174
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
package net.sjrx.intellij.plugins.systemdunitfiles.semanticdata.optionvalues.grammar
22

3-
interface TerminalCombinator : Combinator
3+
interface TerminalCombinator : Combinator {
4+
override fun toStringIndented(indent: Int): String {
5+
return toString()
6+
}
7+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ class WhitespaceTerminal : TerminalCombinator {
3030
override fun toString(): String {
3131
return "\\s+"
3232
}
33+
34+
override fun toStringIndented(indent: Int): String {
35+
return toString()
36+
}
3337
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,18 @@ class ZeroOrMore(val combinator : Combinator) : Combinator {
4242
return match(value, offset, combinator::SemanticMatch)
4343
}
4444

45-
override fun toString(): String {
46-
return "ZeroOrMore(${combinator})"
45+
override fun toString(): String = toStringIndented(0)
46+
47+
override fun toStringIndented(indent: Int): String {
48+
val prefix = " ".repeat(indent)
49+
val sb = StringBuilder()
50+
sb.append(prefix).append("ZeroOrMore(\n")
51+
if (combinator is SequenceCombinator || combinator is AlternativeCombinator || combinator is Repeat || combinator is ZeroOrOne || combinator is ZeroOrMore || combinator is OneOrMore) {
52+
sb.append(combinator.toStringIndented(indent + 1)).append("\n")
53+
} else {
54+
sb.append(" ".repeat(indent + 1)).append(combinator.toString()).append("\n")
55+
}
56+
sb.append(prefix).append(")")
57+
return sb.toString()
4758
}
4859
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,18 @@ class ZeroOrOne(val combinator : Combinator) : Combinator {
4040
return match(value, offset, combinator::SemanticMatch)
4141
}
4242

43-
override fun toString(): String {
44-
return "ZeroOrOne(${combinator})"
43+
override fun toString(): String = toStringIndented(0)
44+
45+
override fun toStringIndented(indent: Int): String {
46+
val prefix = " ".repeat(indent)
47+
val sb = StringBuilder()
48+
sb.append(prefix).append("ZeroOrOne(\n")
49+
if (combinator is SequenceCombinator || combinator is AlternativeCombinator || combinator is Repeat || combinator is ZeroOrOne || combinator is ZeroOrMore || combinator is OneOrMore) {
50+
sb.append(combinator.toStringIndented(indent + 1)).append("\n")
51+
} else {
52+
sb.append(" ".repeat(indent + 1)).append(combinator.toString()).append("\n")
53+
}
54+
sb.append(prefix).append(")")
55+
return sb.toString()
4556
}
4657
}

src/test/kotlin/net/sjrx/intellij/plugins/systemdunitfiles/inspections/InvalidValueForNetworkAddressesTest.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class InvalidValueForNetworkAddressesTest : AbstractUnitFileTest() {
7575
Address=::1/127
7676
Address=2001:db8::/65
7777
Address=ff02::1/128
78+
# Honestly I don't know what matches this
79+
Address=2001:0db8:85a3:0000:0000:8a2e:192.168.0.1/96
7880
""".trimIndent()
7981

8082
// Execute SUT
@@ -91,7 +93,7 @@ class InvalidValueForNetworkAddressesTest : AbstractUnitFileTest() {
9193
// language="unit file (systemd)"
9294
val file="""
9395
[Network]
94-
# Too many hextets (9 instead of max 8)
96+
# Too many hextets (9 instead of max 8)
9597
Address=2001:0db8:85a3:0000:0000:8a2e:0370:7334:1234/64
9698
# Too few hextets (only 2)
9799
Address=abcd:1234/64
@@ -121,6 +123,8 @@ class InvalidValueForNetworkAddressesTest : AbstractUnitFileTest() {
121123
Address=2001:db8:12345::1/64
122124
# Non-hex character in hextet
123125
Address=2001:db8:zzzz::1/64
126+
# Invalid number of octets (missing one with no zero compression)
127+
Address=2001:0db8:85a3:0000:0000:192.168.0.1/96
124128
""".trimIndent()
125129

126130
// Execute SUT
@@ -129,7 +133,7 @@ class InvalidValueForNetworkAddressesTest : AbstractUnitFileTest() {
129133
val highlights = myFixture.doHighlighting()
130134

131135
// Verification
132-
assertSize(15, highlights)
136+
assertSize(16, highlights)
133137

134138
}
135139

0 commit comments

Comments
 (0)