From 3cd9c9a4346f10fd1c772b8ecd4258d6df2ff901 Mon Sep 17 00:00:00 2001 From: Tim Voronov Date: Tue, 12 Aug 2025 13:18:49 -0400 Subject: [PATCH] Enhance diagnostics for incomplete ternary expressions: improve error messages and hints for missing expressions after '?' and ':' in ternary operator, and add comprehensive test cases for better clarity and coverage. --- .../diagnostics/match_common_errors.go | 30 ++++++++++++++++ .../compiler/compiler_errors_syntax_test.go | 36 +++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/pkg/compiler/internal/diagnostics/match_common_errors.go b/pkg/compiler/internal/diagnostics/match_common_errors.go index 41f519d8..00d764d0 100644 --- a/pkg/compiler/internal/diagnostics/match_common_errors.go +++ b/pkg/compiler/internal/diagnostics/match_common_errors.go @@ -39,6 +39,36 @@ func matchCommonErrors(src *file.Source, err *CompilationError, offending *Token return true } + // Ternary operator, incomplete expression + if is(offending.Prev(), "?") { + span := spanFromTokenSafe(offending.Prev().Token(), src) + span.Start++ + span.End++ + + err.Message = "Expected expression after '?' in ternary operator" + err.Hint = "Provide an expression after the question mark to complete the ternary operation." + err.Spans = []ErrorSpan{ + NewMainErrorSpan(span, "missing expression"), + } + + return true + } + + // Ternary operator, missing the right-hand expression + if is(offending.Prev(), ":") { + span := spanFromTokenSafe(offending.Prev().Token(), src) + span.Start++ + span.End++ + + err.Message = "Expected expression after ':' in ternary operator" + err.Hint = "Provide an expression after the colon to complete the ternary operation." + err.Spans = []ErrorSpan{ + NewMainErrorSpan(span, "missing expression"), + } + + return true + } + input := extractNoAlternativeInputs(err.Message) token := input[len(input)-1] diff --git a/test/integration/compiler/compiler_errors_syntax_test.go b/test/integration/compiler/compiler_errors_syntax_test.go index cb97d356..b490331f 100644 --- a/test/integration/compiler/compiler_errors_syntax_test.go +++ b/test/integration/compiler/compiler_errors_syntax_test.go @@ -302,6 +302,42 @@ func TestSyntaxErrors(t *testing.T) { Hint: "Provide an expression after the logical operator, e.g. (a AND b).", }, "Incomplete logical expression 4"), + ErrorCase( + ` + LET a = 1 + LET b = 2 + LET i = b > 1 ? a : + RETURN i + `, E{ + Kind: compiler.SyntaxError, + Message: "Expected expression after ':' in ternary operator", + Hint: "Provide an expression after the colon to complete the ternary operation.", + }, "Incomplete ternary expression"), + + ErrorCase( + ` + LET a = 1 + LET b = 2 + LET i = b > 1 ? 1 + 1 + 1 : + RETURN i + `, E{ + Kind: compiler.SyntaxError, + Message: "Expected expression after ':' in ternary operator", + Hint: "Provide an expression after the colon to complete the ternary operation.", + }, "Incomplete ternary expression 2"), + + ErrorCase( + ` + LET a = 1 + LET b = 2 + LET i = b > 1 ? + RETURN i + `, E{ + Kind: compiler.SyntaxError, + Message: "Expected expression after '?' in ternary operator", + Hint: "Provide an expression after the question mark to complete the ternary operation.", + }, "Incomplete ternary expression 3"), + SkipErrorCase( ` LET i = NONE