mirror of
https://github.com/MontFerret/ferret.git
synced 2025-08-13 19:52:52 +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:
@@ -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]
|
||||
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user