MCPcopy Create free account
hub / github.com/hashintel/hash / unexpected_token

Function unexpected_token

libs/@local/hashql/syntax-jexpr/src/lexer/error.rs:150–194  ·  view source on GitHub ↗
(
    span: SpanId,
    found: SyntaxKind,
    expected: SyntaxKindSet,
)

Source from the content-addressed store, hash-verified

148}
149
150pub(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
196const INVALID_NUMBER_HELP: &str = "JSON numbers must contain digits and follow the format: \
197 `[-]digits[.digits][(e|E)[+|-]digits]`";

Callers 3

validate_tokenMethod · 0.85
visit_arrayFunction · 0.85
visit_objectFunction · 0.85

Calls 8

is_completeMethod · 0.80
primaryMethod · 0.80
contains_separatorMethod · 0.80
contains_valueMethod · 0.80
add_messageMethod · 0.80
is_emptyMethod · 0.45
containsMethod · 0.45

Tested by

no test coverage detected