Skip to content

Commit

Permalink
integrate fusible support
Browse files Browse the repository at this point in the history
  • Loading branch information
evanchooly committed Jun 23, 2023
1 parent 00dc599 commit b06d15a
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 11 deletions.
10 changes: 3 additions & 7 deletions src/main/kotlin/com/antwerkz/expression/types/AnyOf.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.antwerkz.expression.types

import java.util.function.Predicate

internal class AnyOf : Type("anyOf") {
companion object {
private fun fuseElements(elements: List<Type>): Pair<String, List<Type>> {
Expand All @@ -10,6 +8,8 @@ internal class AnyOf : Type("anyOf") {
fusables.joinToString("") { el ->
if (el is CharType || el is AnyOfChars) {
el.value.toString()
} else if (el !is RangeType) {
el.evaluate()
} else {
val value = el.value as List<*>
"${value[0]}-${value[1]}"
Expand All @@ -19,15 +19,11 @@ internal class AnyOf : Type("anyOf") {
}

private fun partition(elements: List<Type>): Pair<List<Type>, List<Type>> {
val predicate =
Predicate<Type> { type ->
type is RangeType || type is CharType || type is AnyOfChars
}
val fused = mutableListOf<Type>()
val rest = mutableListOf<Type>()

elements.forEach {
if (predicate.test(it)) {
if (it.fusible) {
fused += it
} else {
rest += it
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/com/antwerkz/expression/types/AnyOfChars.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.antwerkz.expression.types

internal class AnyOfChars(chars: String) : Type(chars) {
init {
fusible = true
}
override fun copy() = AnyOfChars(value as String).copy(this)

override fun evaluate() = "[${value}]"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.antwerkz.expression.types

internal object CarriageReturn : Type("carriageReturn") {
init {
fusible = true
}
override fun copy() = CarriageReturn

override fun evaluate() = "\\r"
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/com/antwerkz/expression/types/CharType.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.antwerkz.expression.types

internal class CharType(value: String) : Type(value) {
init {
fusible = true
}
override fun copy() = CharType(value as String).copy(this)

override fun evaluate() = value as String
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/com/antwerkz/expression/types/Digit.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.antwerkz.expression.types

internal object Digit : Type("digit") {
init {
fusible = true
}
override fun copy() = Digit

override fun evaluate() = "\\d"
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/com/antwerkz/expression/types/NewLine.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.antwerkz.expression.types

internal object NewLine : Type("newline") {
init {
fusible = true
}
override fun copy() = NewLine

override fun evaluate() = "\\n"
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/com/antwerkz/expression/types/NonDigit.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.antwerkz.expression.types
internal object NonDigit : Type("nonDigit") {
init {
fusible = true
}
override fun copy() = NonDigit

override fun evaluate() = "\\D"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.antwerkz.expression.types

internal object NonWhiteSpaceChar : Type("nonWhitespaceChar") {
init {
fusible = true
}
override fun copy() = NonWhiteSpaceChar

override fun evaluate() = "\\S"
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/com/antwerkz/expression/types/NonWord.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.antwerkz.expression.types

internal object NonWord : Type("nonWord") {
init {
fusible = true
}
override fun copy() = NonWord

override fun evaluate() = "\\W"
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/com/antwerkz/expression/types/NullByte.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.antwerkz.expression.types

internal object NullByte : Type("nullByte") {
init {
fusible = true
}
override fun copy() = NullByte

override fun evaluate() = "\\0"
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/com/antwerkz/expression/types/RangeType.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.antwerkz.expression.types

internal class RangeType(start: Char, end: Char) : Type(listOf(start, end)) {
init {
fusible = true
}
@Suppress("UNCHECKED_CAST")
override fun copy(): Type {
val list = value as List<Char>
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/com/antwerkz/expression/types/Tab.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.antwerkz.expression.types

internal object Tab : Type("tab") {
init {
fusible = true
}
override fun copy() = Tab

override fun evaluate() = "\\t"
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/com/antwerkz/expression/types/Type.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.antwerkz.expression.types

internal abstract class Type(var value: Any? = null) {
var containsChildren: Boolean = false
var quantifierRequiresGroup: Boolean = false
var containsChildren = false
var quantifierRequiresGroup = false
var fusible = false
var times: List<Int> = emptyList()
var name: String? = null
var index: Int = 0
var index = 0

constructor(original: Type) : this(original.value) {
this.name = original.name
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.antwerkz.expression.types

internal object WhitespaceChar : Type("whitespaceChar") {
init {
fusible = true
}
override fun copy() = WhitespaceChar

override fun evaluate() = "\\s"
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/com/antwerkz/expression/types/Word.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.antwerkz.expression.types

internal object Word : Type("word") {
init {
fusible = true
}
override fun copy() = Word

override fun evaluate() = "\\w"
Expand Down
10 changes: 9 additions & 1 deletion src/test/kotlin/com/antwerkz/expressive/SuperExpressiveTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class SuperExpressiveTest {
testRegexEquality("\\t", tab())

testRegexEquality(
"(?:hello|\\d|\\w|[\\.#])",
"""(?:hello|[\d\w\.#])""",
anyOf { string("hello").digit().word().char('.').char('#') }
)

Expand Down Expand Up @@ -187,6 +187,14 @@ class SuperExpressiveTest {
testRegexEquality("h", char('h'))
}

@Test
fun testFusing() {
testRegexEquality(
"""(?:hello|[\d\w\.#])""",
anyOf { string("hello").digit().word().char('.').char('#') }
)
}

@Test
fun simpleSubexpression() {
testRegexEquality(
Expand Down

0 comments on commit b06d15a

Please sign in to comment.