From e35037fbec38fc94bcf125f99f8d067538b05960 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Fri, 15 Mar 2024 08:25:55 +0100 Subject: [PATCH] [orx-fcurve] Fix regular expressions for Javascript, fix unit tests --- orx-fcurve/src/commonMain/kotlin/EFCurve.kt | 32 ++++++++----------- .../src/commonTest/kotlin/TestEFCurve.kt | 8 ++--- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/orx-fcurve/src/commonMain/kotlin/EFCurve.kt b/orx-fcurve/src/commonMain/kotlin/EFCurve.kt index 1f70d829..5a4f25b6 100644 --- a/orx-fcurve/src/commonMain/kotlin/EFCurve.kt +++ b/orx-fcurve/src/commonMain/kotlin/EFCurve.kt @@ -9,14 +9,21 @@ import org.openrndr.extra.expressions.evaluateExpression */ fun efcurve(ef: String, constants: Map = emptyMap()): String { val expression = Regex("_([^_]+)_") - val repetition = Regex("\\|([^|]+)\\|\\[([^\\[\\]]+)]") - val list = Regex("\\|([^|]+)\\|\\{([^\\[\\]]+)}") + + // IntelliJ falsely reports a redundant escape character. the escape character is required when running the regular + // expression on a javascript target. Removing the escape character will result in a `Lone quantifier brackets` + // syntax error. + + @Suppress("RegExpRedundantEscape") + val repetition = Regex("""\|([^|]+)\|\[([^\[\]]+)\]""") + + @Suppress("RegExpRedundantEscape") + val list = Regex("\\|([^|]+)\\|\\{([^\\[\\]]+)\\}") /** * perform comment substitution - * (?m) enables multiline mode */ - var curve = Regex("(?m)(#.*)$").replace(ef, "") + var curve = Regex("(#.*)$", RegexOption.MULTILINE).replace(ef, "") /** * Allow for nested repetitions and lists @@ -70,20 +77,7 @@ fun efcurve(ef: String, constants: Map = emptyMap()): String { /** * evaluate expression in expansion */ - return (expression.replace(curve) { ef -> - evaluateExpression(ef.groupValues[1], constants)?.toString() ?: error("parse error in '$curve") + return (expression.replace(curve) { exp -> + evaluateExpression(exp.groupValues[1], constants)?.toString() ?: error("parse error in '$curve") }) -} - -fun main() { - efcurve("""M1 |h5 m3|{ - |10.3 # toch wel handig zo'n comment - |11.2 - |14.5 - |} - """.trimMargin()) - - println(efcurve("|M0 |h4 m3|[2]|[5]")) - - println(efcurve("""M0|h4 m_it_|{|_cos(it * PI * 0.5)_ |[4]}""")) } \ No newline at end of file diff --git a/orx-fcurve/src/commonTest/kotlin/TestEFCurve.kt b/orx-fcurve/src/commonTest/kotlin/TestEFCurve.kt index 6efe9602..89da902c 100644 --- a/orx-fcurve/src/commonTest/kotlin/TestEFCurve.kt +++ b/orx-fcurve/src/commonTest/kotlin/TestEFCurve.kt @@ -16,18 +16,18 @@ class TestEFCurve { @Test fun expressions() { - assertEquals("M9.0", efcurve("M_4 + 5_")) + assertEquals("M${9.0}", efcurve("M_4 + 5_")) } @Test fun listExpansion() { - assertEquals("M0 L1.0, 3.0 L1.0, 6.0", efcurve("M0 |L1.0, _it_|{3, 6}")) + assertEquals("M0 L1.0, ${3.0} L1.0, ${6.0}", efcurve("M0 |L1.0, _it_|{3, 6}")) } @Test fun repetition() { assertEquals("M0 L1.0, 3.0 L1.0, 3.0", efcurve("M0 |L1.0, 3.0|[2]")) - assertEquals("M0 L1.0, 0.0 L1.0, 1.0", efcurve("M0 |L1.0, _it_|[2]")) - assertEquals("M0 L1.0, 0.0 L1.0, 1.0 L1.0, 0.0 L1.0, 1.0 L1.0, 0.0 L1.0, 1.0", efcurve("M0 ||L1.0, _it_|[2]|[3]")) + assertEquals("M0 L1.0, ${0.0} L1.0, ${1.0}", efcurve("M0 |L1.0, _it_|[2]")) + assertEquals("M0 L1.0, ${0.0} L1.0, ${1.0} L1.0, ${0.0} L1.0, ${1.0} L1.0, ${0.0} L1.0, ${1.0}", efcurve("M0 ||L1.0, _it_|[2]|[3]")) } } \ No newline at end of file