(
span: SpanId,
found: SyntaxKind,
expected: SyntaxKindSet,
)
| 148 | } |
| 149 | |
| 150 | pub(crate) fn unexpected_token( |
| 151 | span: SpanId, |
| 152 | found: SyntaxKind, |
| 153 | expected: SyntaxKindSet, |
| 154 | ) -> LexerDiagnostic { |
| 155 | // Create a specific label based on what was found vs what was expected |
| 156 | let label = if expected.is_empty() { |
| 157 | format!("Unexpected token {found}") |
| 158 | } else if expected.is_complete() { |
| 159 | format!("Invalid syntax found {found}") |
| 160 | } else { |
| 161 | format!( |
| 162 | "Unexpected {}, expected {}", |
| 163 | found, |
| 164 | expected.display(Some(Conjunction::Or)) |
| 165 | ) |
| 166 | }; |
| 167 | |
| 168 | let mut diagnostic = Diagnostic::new(LexerDiagnosticCategory::UnexpectedToken, Critical::ERROR) |
| 169 | .primary(Label::new(span, label)); |
| 170 | |
| 171 | // Provide specific help based on common syntax errors |
| 172 | let help = if expected.contains_closing_delimiter() |
| 173 | && SyntaxKindSet::CLOSING_DELIMITER.contains(found) |
| 174 | { |
| 175 | "Mismatched closing brackets. Ensure opening and closing brackets match correctly." |
| 176 | } else if expected.contains_closing_delimiter() { |
| 177 | "Missing closing bracket. Make sure all opening brackets have matching closing brackets." |
| 178 | } else if expected.contains_separator() && SyntaxKindSet::CLOSING_DELIMITER.contains(found) { |
| 179 | "You might be missing a comma between items or have an extra trailing comma." |
| 180 | } else if expected.contains_value() && SyntaxKindSet::SEPARATORS.contains(found) { |
| 181 | "Expected a value (string, number, object, array, true, false, or null) here." |
| 182 | } else if found == SyntaxKind::Colon && !expected.contains(SyntaxKind::Colon) { |
| 183 | "Colons are only used in objects to separate keys from values." |
| 184 | } else if found == SyntaxKind::Comma && !expected.contains(SyntaxKind::Comma) { |
| 185 | "Commas are only used to separate items in arrays and objects." |
| 186 | } else { |
| 187 | "Check your JSON syntax. Make sure brackets are balanced and all required punctuation is \ |
| 188 | present." |
| 189 | }; |
| 190 | |
| 191 | diagnostic.add_message(Message::help(Cow::Borrowed(help))); |
| 192 | |
| 193 | diagnostic |
| 194 | } |
| 195 | |
| 196 | const INVALID_NUMBER_HELP: &str = "JSON numbers must contain digits and follow the format: \ |
| 197 | `[-]digits[.digits][(e|E)[+|-]digits]`"; |
no test coverage detected