diff --git a/semantic/src/main/kotlin/tools/samt/semantic/ConstraintBuilder.kt b/semantic/src/main/kotlin/tools/samt/semantic/ConstraintBuilder.kt index e750bef9..0bda554c 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: '${e.message}'") + 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..042405f4 100644 --- a/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt +++ b/semantic/src/test/kotlin/tools/samt/semantic/SemanticModelTest.kt @@ -334,6 +334,24 @@ 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: 'Dangling meta character '+' near index 5${System.lineSeparator()}" + + "fo/+++!hi${System.lineSeparator()}" + + " ^'" + ) + ) + } + @Test fun `cannot use non-existent constraints`() { val source = """ 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