AnalyzeTokenError produces a context-aware suggestion string for token-level parse errors. It inspects the actual and expected token types to provide specific guidance - for example, detecting when a quoted string is used where a number is expected, or when an unknown identifier looks like a misspel
(tokenType, tokenValue, expectedType string)
| 262 | // |
| 263 | // Returns a human-readable suggestion string; never returns empty string. |
| 264 | func AnalyzeTokenError(tokenType, tokenValue, expectedType string) string { |
| 265 | // String literal where number expected |
| 266 | if tokenType == "STRING" && (expectedType == "NUMBER" || expectedType == "INTEGER") { |
| 267 | return fmt.Sprintf("Expected a number but found a string literal '%s'. Remove the quotes if this should be numeric.", tokenValue) |
| 268 | } |
| 269 | |
| 270 | // Number where string expected |
| 271 | if tokenType == "NUMBER" && expectedType == "STRING" { |
| 272 | return fmt.Sprintf("Expected a string but found a number %s. Add quotes if this should be a string literal.", tokenValue) |
| 273 | } |
| 274 | |
| 275 | // Identifier issues |
| 276 | if tokenType == "IDENT" { |
| 277 | suggestion := SuggestKeyword(tokenValue) |
| 278 | if suggestion != "" && suggestion != strings.ToUpper(tokenValue) { |
| 279 | return fmt.Sprintf("Unknown identifier '%s'. Did you mean the keyword '%s'?", tokenValue, suggestion) |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // Missing operator between tokens |
| 284 | if tokenType == "IDENT" && expectedType == "OPERATOR" { |
| 285 | return "Expected an operator (=, <, >, AND, OR, etc.) between expressions." |
| 286 | } |
| 287 | |
| 288 | // Unclosed parenthesis |
| 289 | if tokenType == "EOF" && expectedType == "RPAREN" { |
| 290 | return "Unclosed parenthesis detected. Check that all opening parentheses have matching closing parentheses." |
| 291 | } |
| 292 | |
| 293 | // Generic suggestion |
| 294 | return fmt.Sprintf("Expected %s but found %s. Review the SQL syntax at this position.", expectedType, tokenType) |
| 295 | } |
| 296 | |
| 297 | // SuggestForIncompleteStatement returns a suggestion string explaining what tokens |
| 298 | // or clauses are expected to follow the given SQL keyword. This is used when the |