From 0aff6a51746961fe2b53dfbca1a0fe361f3868aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonard=20Schu=CC=88tz?= Date: Tue, 30 May 2023 18:33:02 +0200 Subject: [PATCH 1/3] feat(semantic): Validate regex patterns --- .../tools/samt/semantic/ConstraintBuilder.kt | 15 ++++++++++++--- .../tools/samt/semantic/SemanticModelTest.kt | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/semantic/src/main/kotlin/tools/samt/semantic/ConstraintBuilder.kt b/semantic/src/main/kotlin/tools/samt/semantic/ConstraintBuilder.kt index e750bef9..d55b35cf 100644 --- a/semantic/src/main/kotlin/tools/samt/semantic/ConstraintBuilder.kt +++ b/semantic/src/main/kotlin/tools/samt/semantic/ConstraintBuilder.kt @@ -98,9 +98,18 @@ internal class ConstraintBuilder(private val controller: DiagnosticController) { private fun createPattern( expression: ExpressionNode, argument: StringNode, - ): ResolvedTypeReference.Constraint.Pattern { - // We will validate the pattern here in the future - return ResolvedTypeReference.Constraint.Pattern(expression, argument.value) + ): ResolvedTypeReference.Constraint.Pattern? { + val pattern = argument.value + + try { Regex(pattern) } catch (e: Exception) { + argument.reportError(controller) { + message("Invalid regex pattern") + highlight(argument.location) + } + return null + } + + return ResolvedTypeReference.Constraint.Pattern(expression, pattern) } private fun createValue( diff --git a/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt b/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt index 1c3073f1..1a31c8da 100644 --- a/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt +++ b/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt @@ -334,6 +334,22 @@ class SemanticModelTest { ) } + @Test + fun `pattern must be valid`() { + val source = """ + package complex + + record Foo { + name: String (pattern("fo/+++!hi")) + } + """.trimIndent() + parseAndCheck( + source to listOf( + "Error: Invalid regex pattern" + ) + ) + } + @Test fun `cannot use non-existent constraints`() { val source = """ From cd189fa00160021295c87a35ce15e2a7b94794e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonard=20Schu=CC=88tz?= Date: Tue, 30 May 2023 19:22:00 +0200 Subject: [PATCH 2/3] feat(diagnostic): Show regex error message --- .../src/main/kotlin/tools/samt/semantic/ConstraintBuilder.kt | 2 +- .../src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt | 4 +++- specification/examples/debug/debug.samt | 5 +++++ specification/examples/debug/samt.yaml | 5 +++++ 4 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 specification/examples/debug/debug.samt create mode 100644 specification/examples/debug/samt.yaml diff --git a/semantic/src/main/kotlin/tools/samt/semantic/ConstraintBuilder.kt b/semantic/src/main/kotlin/tools/samt/semantic/ConstraintBuilder.kt index d55b35cf..0bda554c 100644 --- a/semantic/src/main/kotlin/tools/samt/semantic/ConstraintBuilder.kt +++ b/semantic/src/main/kotlin/tools/samt/semantic/ConstraintBuilder.kt @@ -103,7 +103,7 @@ internal class ConstraintBuilder(private val controller: DiagnosticController) { try { Regex(pattern) } catch (e: Exception) { argument.reportError(controller) { - message("Invalid regex pattern") + message("Invalid regex pattern: '${e.message}'") highlight(argument.location) } return null diff --git a/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt b/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt index 1a31c8da..2baf6e80 100644 --- a/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt +++ b/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt @@ -345,7 +345,9 @@ class SemanticModelTest { """.trimIndent() parseAndCheck( source to listOf( - "Error: Invalid regex pattern" + "Error: Invalid regex pattern: 'Dangling meta character '+' near index 5\n" + + "fo/+++!hi\n" + + " ^'" ) ) } diff --git a/specification/examples/debug/debug.samt b/specification/examples/debug/debug.samt new file mode 100644 index 00000000..3b739358 --- /dev/null +++ b/specification/examples/debug/debug.samt @@ -0,0 +1,5 @@ +package debug.test + +record Foo { + name: String ("++") +} \ No newline at end of file diff --git a/specification/examples/debug/samt.yaml b/specification/examples/debug/samt.yaml new file mode 100644 index 00000000..96a38136 --- /dev/null +++ b/specification/examples/debug/samt.yaml @@ -0,0 +1,5 @@ +source: ./ + +generators: + - name: kotlin-ktor-consumer + output: ./out From 342e95019ff522dc8b6b5aaf2ed33763db11becf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonard=20Schu=CC=88tz?= Date: Wed, 31 May 2023 17:20:16 +0200 Subject: [PATCH 3/3] refactor(semantic): Replace newlines in test --- .../src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt b/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt index 2baf6e80..042405f4 100644 --- a/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt +++ b/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt @@ -345,8 +345,8 @@ class SemanticModelTest { """.trimIndent() parseAndCheck( source to listOf( - "Error: Invalid regex pattern: 'Dangling meta character '+' near index 5\n" + - "fo/+++!hi\n" + + "Error: Invalid regex pattern: 'Dangling meta character '+' near index 5${System.lineSeparator()}" + + "fo/+++!hi${System.lineSeparator()}" + " ^'" ) )