From b06d15ad2d4ecd7ec44c63576276fec68fb97052 Mon Sep 17 00:00:00 2001 From: evanchooly Date: Thu, 22 Jun 2023 23:13:33 -0400 Subject: [PATCH] integrate fusible support --- src/main/kotlin/com/antwerkz/expression/types/AnyOf.kt | 10 +++------- .../kotlin/com/antwerkz/expression/types/AnyOfChars.kt | 3 +++ .../com/antwerkz/expression/types/CarriageReturn.kt | 3 +++ .../kotlin/com/antwerkz/expression/types/CharType.kt | 3 +++ src/main/kotlin/com/antwerkz/expression/types/Digit.kt | 3 +++ .../kotlin/com/antwerkz/expression/types/NewLine.kt | 3 +++ .../kotlin/com/antwerkz/expression/types/NonDigit.kt | 3 +++ .../com/antwerkz/expression/types/NonWhiteSpaceChar.kt | 3 +++ .../kotlin/com/antwerkz/expression/types/NonWord.kt | 3 +++ .../kotlin/com/antwerkz/expression/types/NullByte.kt | 3 +++ .../kotlin/com/antwerkz/expression/types/RangeType.kt | 3 +++ src/main/kotlin/com/antwerkz/expression/types/Tab.kt | 3 +++ src/main/kotlin/com/antwerkz/expression/types/Type.kt | 7 ++++--- .../com/antwerkz/expression/types/WhitespaceChar.kt | 3 +++ src/main/kotlin/com/antwerkz/expression/types/Word.kt | 3 +++ .../com/antwerkz/expressive/SuperExpressiveTest.kt | 10 +++++++++- 16 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/com/antwerkz/expression/types/AnyOf.kt b/src/main/kotlin/com/antwerkz/expression/types/AnyOf.kt index 18caf3b..fab867a 100644 --- a/src/main/kotlin/com/antwerkz/expression/types/AnyOf.kt +++ b/src/main/kotlin/com/antwerkz/expression/types/AnyOf.kt @@ -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): Pair> { @@ -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]}" @@ -19,15 +19,11 @@ internal class AnyOf : Type("anyOf") { } private fun partition(elements: List): Pair, List> { - val predicate = - Predicate { type -> - type is RangeType || type is CharType || type is AnyOfChars - } val fused = mutableListOf() val rest = mutableListOf() elements.forEach { - if (predicate.test(it)) { + if (it.fusible) { fused += it } else { rest += it diff --git a/src/main/kotlin/com/antwerkz/expression/types/AnyOfChars.kt b/src/main/kotlin/com/antwerkz/expression/types/AnyOfChars.kt index b8c3718..51c7efb 100644 --- a/src/main/kotlin/com/antwerkz/expression/types/AnyOfChars.kt +++ b/src/main/kotlin/com/antwerkz/expression/types/AnyOfChars.kt @@ -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}]" diff --git a/src/main/kotlin/com/antwerkz/expression/types/CarriageReturn.kt b/src/main/kotlin/com/antwerkz/expression/types/CarriageReturn.kt index 934a108..19f8740 100644 --- a/src/main/kotlin/com/antwerkz/expression/types/CarriageReturn.kt +++ b/src/main/kotlin/com/antwerkz/expression/types/CarriageReturn.kt @@ -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" diff --git a/src/main/kotlin/com/antwerkz/expression/types/CharType.kt b/src/main/kotlin/com/antwerkz/expression/types/CharType.kt index 62ade65..45d56df 100644 --- a/src/main/kotlin/com/antwerkz/expression/types/CharType.kt +++ b/src/main/kotlin/com/antwerkz/expression/types/CharType.kt @@ -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 diff --git a/src/main/kotlin/com/antwerkz/expression/types/Digit.kt b/src/main/kotlin/com/antwerkz/expression/types/Digit.kt index 435ddbf..816cdda 100644 --- a/src/main/kotlin/com/antwerkz/expression/types/Digit.kt +++ b/src/main/kotlin/com/antwerkz/expression/types/Digit.kt @@ -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" diff --git a/src/main/kotlin/com/antwerkz/expression/types/NewLine.kt b/src/main/kotlin/com/antwerkz/expression/types/NewLine.kt index 2d096e0..1a694ea 100644 --- a/src/main/kotlin/com/antwerkz/expression/types/NewLine.kt +++ b/src/main/kotlin/com/antwerkz/expression/types/NewLine.kt @@ -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" diff --git a/src/main/kotlin/com/antwerkz/expression/types/NonDigit.kt b/src/main/kotlin/com/antwerkz/expression/types/NonDigit.kt index 7e0a938..3b1070f 100644 --- a/src/main/kotlin/com/antwerkz/expression/types/NonDigit.kt +++ b/src/main/kotlin/com/antwerkz/expression/types/NonDigit.kt @@ -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" diff --git a/src/main/kotlin/com/antwerkz/expression/types/NonWhiteSpaceChar.kt b/src/main/kotlin/com/antwerkz/expression/types/NonWhiteSpaceChar.kt index acdfa93..002523b 100644 --- a/src/main/kotlin/com/antwerkz/expression/types/NonWhiteSpaceChar.kt +++ b/src/main/kotlin/com/antwerkz/expression/types/NonWhiteSpaceChar.kt @@ -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" diff --git a/src/main/kotlin/com/antwerkz/expression/types/NonWord.kt b/src/main/kotlin/com/antwerkz/expression/types/NonWord.kt index d4ae74f..07f291f 100644 --- a/src/main/kotlin/com/antwerkz/expression/types/NonWord.kt +++ b/src/main/kotlin/com/antwerkz/expression/types/NonWord.kt @@ -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" diff --git a/src/main/kotlin/com/antwerkz/expression/types/NullByte.kt b/src/main/kotlin/com/antwerkz/expression/types/NullByte.kt index 4604485..73213c7 100644 --- a/src/main/kotlin/com/antwerkz/expression/types/NullByte.kt +++ b/src/main/kotlin/com/antwerkz/expression/types/NullByte.kt @@ -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" diff --git a/src/main/kotlin/com/antwerkz/expression/types/RangeType.kt b/src/main/kotlin/com/antwerkz/expression/types/RangeType.kt index 2acca68..ebb0817 100644 --- a/src/main/kotlin/com/antwerkz/expression/types/RangeType.kt +++ b/src/main/kotlin/com/antwerkz/expression/types/RangeType.kt @@ -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 diff --git a/src/main/kotlin/com/antwerkz/expression/types/Tab.kt b/src/main/kotlin/com/antwerkz/expression/types/Tab.kt index 5827a5d..78e013f 100644 --- a/src/main/kotlin/com/antwerkz/expression/types/Tab.kt +++ b/src/main/kotlin/com/antwerkz/expression/types/Tab.kt @@ -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" diff --git a/src/main/kotlin/com/antwerkz/expression/types/Type.kt b/src/main/kotlin/com/antwerkz/expression/types/Type.kt index d35850d..ff90bec 100644 --- a/src/main/kotlin/com/antwerkz/expression/types/Type.kt +++ b/src/main/kotlin/com/antwerkz/expression/types/Type.kt @@ -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 = emptyList() var name: String? = null - var index: Int = 0 + var index = 0 constructor(original: Type) : this(original.value) { this.name = original.name diff --git a/src/main/kotlin/com/antwerkz/expression/types/WhitespaceChar.kt b/src/main/kotlin/com/antwerkz/expression/types/WhitespaceChar.kt index f5f3859..0630494 100644 --- a/src/main/kotlin/com/antwerkz/expression/types/WhitespaceChar.kt +++ b/src/main/kotlin/com/antwerkz/expression/types/WhitespaceChar.kt @@ -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" diff --git a/src/main/kotlin/com/antwerkz/expression/types/Word.kt b/src/main/kotlin/com/antwerkz/expression/types/Word.kt index fcf4f5c..3ccdd24 100644 --- a/src/main/kotlin/com/antwerkz/expression/types/Word.kt +++ b/src/main/kotlin/com/antwerkz/expression/types/Word.kt @@ -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" diff --git a/src/test/kotlin/com/antwerkz/expressive/SuperExpressiveTest.kt b/src/test/kotlin/com/antwerkz/expressive/SuperExpressiveTest.kt index 06f8ada..ea56e46 100644 --- a/src/test/kotlin/com/antwerkz/expressive/SuperExpressiveTest.kt +++ b/src/test/kotlin/com/antwerkz/expressive/SuperExpressiveTest.kt @@ -101,7 +101,7 @@ class SuperExpressiveTest { testRegexEquality("\\t", tab()) testRegexEquality( - "(?:hello|\\d|\\w|[\\.#])", + """(?:hello|[\d\w\.#])""", anyOf { string("hello").digit().word().char('.').char('#') } ) @@ -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(