1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-08-15 20:02:56 +02:00

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.

This commit is contained in:
Tim Voronov
2025-08-12 13:18:49 -04:00
parent a12943633e
commit 3cd9c9a434
2 changed files with 66 additions and 0 deletions

View File

@@ -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]

View File

@@ -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