eventql_parser/error.rs
1//! Error types for lexical analysis and parsing.
2//!
3//! This module defines the error types that can occur during tokenization
4//! and parsing of EventQL queries. All errors include position information
5//! (line and column numbers) to help diagnose issues in query strings.
6
7use crate::{Type, token::Symbol};
8use serde::Serialize;
9use thiserror::Error;
10
11/// Top-level error type for the EventQL parser.
12///
13/// This enum wraps both lexer and parser errors, providing a unified
14/// error type for the entire parsing pipeline.
15#[derive(Debug, Error, Serialize)]
16pub enum Error {
17 /// Error during lexical analysis (tokenization).
18 #[error(transparent)]
19 Lexer(LexerError),
20
21 /// Error during syntactic analysis (parsing).
22 #[error(transparent)]
23 Parser(ParserError),
24
25 /// Error during static analysis.
26 #[error(transparent)]
27 Analysis(AnalysisError),
28}
29
30/// Errors that can occur during lexical analysis.
31///
32/// These errors are produced by the tokenizer when the input string
33/// contains invalid characters or ends unexpectedly.
34#[derive(Debug, Error, Serialize)]
35pub enum LexerError {
36 /// The input ended unexpectedly while parsing a token.
37 ///
38 /// This typically occurs when a string literal or other multi-character
39 /// token is not properly closed.
40 #[error("unexpected end of input")]
41 IncompleteInput,
42
43 /// An invalid character was encountered at the specified position.
44 ///
45 /// The tuple contains `(line_number, column_number)`.
46 #[error("{0}:{1}: invalid character")]
47 InvalidSymbol(u32, u32),
48}
49
50/// Errors that can occur during syntactic analysis.
51///
52/// These errors are produced by the parser when the token sequence
53/// does not match the expected grammar of EventQL.
54#[derive(Debug, Error, Serialize)]
55pub enum ParserError {
56 /// Expected an identifier but found something else.
57 ///
58 /// Fields: `(line, column, found_token)`
59 #[error("{0}:{1}: expected identifier but got {2}")]
60 ExpectedIdent(u32, u32, String),
61
62 #[error("{0}:{1}: missing FROM statement")]
63 MissingFromStatement(u32, u32),
64
65 /// Expected a specific keyword but found something else.
66 ///
67 /// Fields: `(line, column, expected_keyword, found_token)`
68 #[error("{0}:{1}: expected keyword {2} but got {3}")]
69 ExpectedKeyword(u32, u32, &'static str, String),
70
71 /// Expected a specific symbol but found something else.
72 ///
73 /// Fields: `(line, column, expected_symbol, found_token)`
74 #[error("{0}:{1}: expected {2} but got {3}")]
75 ExpectedSymbol(u32, u32, Symbol, String),
76
77 /// An unexpected token was encountered.
78 ///
79 /// Fields: `(line, column, found_token)`
80 ///
81 /// This is a general error for tokens that don't fit the current parse context.
82 #[error("{0}:{1}: unexpected token {2}")]
83 UnexpectedToken(u32, u32, String),
84
85 /// Expected a type name but found something else.
86 ///
87 /// Fields: `(line, column, found_token)`
88 ///
89 /// This occurs when defining a type conversion operation but the left side is
90 /// not a type.
91 #[error("{0}:{1}: expected a type")]
92 ExpectedType(u32, u32),
93
94 /// The input ended unexpectedly while parsing.
95 ///
96 /// This occurs when the parser expects more tokens but encounters
97 /// the end of the token stream.
98 #[error("unexpected end of file")]
99 UnexpectedEof,
100}
101
102/// Errors that can occur during static analysis.
103///
104/// These errors are produced by the type checker when it encounters
105/// type mismatches, undeclared variables, or other semantic issues
106/// in the query.
107#[derive(Debug, Error, Serialize)]
108pub enum AnalysisError {
109 /// A binding with the same name already exists in the current scope.
110 ///
111 /// Fields: `(line, column, binding_name)`
112 ///
113 /// This occurs when trying to declare a variable that shadows an existing
114 /// binding in the same scope, such as using the same alias for multiple
115 /// FROM sources.
116 #[error("{0}:{1}: binding '{2}' already exists")]
117 BindingAlreadyExists(u32, u32, String),
118
119 /// A variable was referenced but not declared in any accessible scope.
120 ///
121 /// Fields: `(line, column, variable_name)`
122 ///
123 /// This occurs when referencing a variable that hasn't been bound by a
124 /// FROM clause or defined in the default scope.
125 #[error("{0}:{1}: variable '{2}' is undeclared")]
126 VariableUndeclared(u32, u32, String),
127
128 /// A type mismatch occurred between expected and actual types.
129 ///
130 /// Fields: `(line, column, expected_type, actual_type)`
131 ///
132 /// This occurs when an expression has a different type than what is
133 /// required by its context (e.g., using a string where a number is expected).
134 #[error("{0}:{1}: type mismatch: expected {2} but got {3} ")]
135 TypeMismatch(u32, u32, Type, Type),
136
137 /// A record field was accessed but doesn't exist in the record type.
138 ///
139 /// Fields: `(line, column, field_name)`
140 ///
141 /// This occurs when trying to access a field that is not defined in the
142 /// record's type definition.
143 #[error("{0}:{1}: record field '{2}' is undeclared ")]
144 FieldUndeclared(u32, u32, String),
145
146 /// A function was called but is not declared in the scope.
147 ///
148 /// Fields: `(line, column, function_name)`
149 ///
150 /// This occurs when calling a function that is not defined in the default
151 /// scope or any accessible scope.
152 #[error("{0}:{1}: function '{2}' is undeclared ")]
153 FuncUndeclared(u32, u32, String),
154
155 /// Expected a record type but found a different type.
156 ///
157 /// Fields: `(line, column, actual_type)`
158 ///
159 /// This occurs when a record type is required (e.g., for field access)
160 /// but a different type was found.
161 #[error("{0}:{1}: expected record but got {2}")]
162 ExpectRecord(u32, u32, Type),
163
164 /// Expected an array type but found a different type.
165 ///
166 /// Fields: `(line, column, actual_type)`
167 ///
168 /// This occurs when an array type is required but a different type was found.
169 #[error("{0}:{1}: expected an array but got {2}")]
170 ExpectArray(u32, u32, Type),
171
172 /// Expected a field literal but found a different expression.
173 ///
174 /// Fields: `(line, column)`
175 ///
176 /// This occurs in contexts where only a simple field reference is allowed,
177 /// such as in GROUP BY or ORDER BY clauses.
178 #[error("{0}:{1}: expected a field")]
179 ExpectFieldLiteral(u32, u32),
180
181 /// Expected a record literal but found a different expression.
182 ///
183 /// Fields: `(line, column)`
184 ///
185 /// This occurs when a record literal is required, such as in the
186 /// PROJECT INTO clause.
187 #[error("{0}:{1}: expected a record")]
188 ExpectRecordLiteral(u32, u32),
189
190 /// When a custom type (meaning a type not supported by EventQL by default) is used but
191 /// not registered in the `AnalysisOptions` custom type set.
192 #[error("{0}:{1}: unsupported custom type '{2}'")]
193 UnsupportedCustomType(u32, u32, String),
194
195 /// A function was called with the wrong number of arguments.
196 ///
197 /// Fields: `(line, column, function_name)`
198 ///
199 /// This occurs when calling a function with a different number of arguments
200 /// than what the function signature requires.
201 #[error("{0}:{1}: incorrect number of arguments supplied to function '{2}'")]
202 FunWrongArgumentCount(u32, u32, String),
203
204 /// An aggregate function was used outside of a PROJECT INTO clause.
205 ///
206 /// Fields: `(line, column, function_name)`
207 ///
208 /// This occurs when an aggregate function (e.g., SUM, COUNT, AVG) is used
209 /// in a context where aggregation is not allowed, such as in WHERE, GROUP BY,
210 /// or ORDER BY clauses. Aggregate functions can only be used in the PROJECT INTO
211 /// clause to compute aggregated values over groups of events.
212 ///
213 /// # Example
214 ///
215 /// Invalid usage:
216 /// ```eql
217 /// FROM e IN events
218 /// WHERE COUNT() > 5 // Error: aggregate function in WHERE clause
219 /// PROJECT INTO e
220 /// ```
221 ///
222 /// Valid usage:
223 /// ```eql
224 /// FROM e IN events
225 /// PROJECT INTO { total: COUNT() }
226 /// ```
227 #[error("{0}:{1}: aggregate function '{2}' can only be used in a PROJECT INTO clause")]
228 WrongAggFunUsage(u32, u32, String),
229
230 /// An aggregate function was used together with source-bound fields.
231 ///
232 /// Fields: `(line, column)`
233 ///
234 /// This occurs when attempting to mix aggregate functions with fields that are
235 /// bound to source events within the same projection field. Aggregate functions
236 /// operate on groups of events, while source-bound fields refer to individual
237 /// event properties. These cannot be mixed in a single field expression.
238 ///
239 /// # Example
240 ///
241 /// Invalid usage:
242 /// ```eql
243 /// FROM e IN events
244 /// // Error: mixing aggregate (SUM) with source field (e.id)
245 /// PROJECT INTO { count: SUM(e.data.price), id: e.id }
246 /// ```
247 ///
248 /// Valid usage:
249 /// ```eql
250 /// FROM e IN events
251 /// PROJECT INTO { sum: SUM(e.data.price), label: "total" }
252 /// ```
253 #[error("{0}:{1}: aggregate functions cannot be used with source-bound fields")]
254 UnallowedAggFuncUsageWithSrcField(u32, u32),
255
256 /// An empty record literal was used in a context where it is not allowed.
257 ///
258 /// Fields: `(line, column)`
259 ///
260 /// This occurs when using an empty record `{}` as a projection, which would
261 /// result in a query that produces no output fields. Projections must contain
262 /// at least one field.
263 ///
264 /// # Example
265 ///
266 /// Invalid usage:
267 /// ```eql
268 /// FROM e IN events
269 /// PROJECT INTO {} // Error: empty record
270 /// ```
271 ///
272 /// Valid usage:
273 /// ```eql
274 /// FROM e IN events
275 /// PROJECT INTO { id: e.id }
276 /// ```
277 #[error("{0}:{1}: unexpected empty record")]
278 EmptyRecord(u32, u32),
279
280 /// An aggregate function was called with an argument that is not a source-bound field.
281 ///
282 /// Fields: `(line, column)`
283 ///
284 /// This occurs when an aggregate function (e.g., SUM, COUNT, AVG) is called with
285 /// an argument that is not derived from source event properties. Aggregate functions
286 /// must operate on fields that come from the source events being queried, not on
287 /// constants, literals, or results from other functions.
288 ///
289 /// # Example
290 ///
291 /// Invalid usage:
292 /// ```eql
293 /// FROM e IN events
294 /// // Error: RAND() is constant value
295 /// PROJECT INTO { sum: SUM(RAND()) }
296 /// ```
297 ///
298 /// Valid usage:
299 /// ```eql
300 /// FROM e IN events
301 /// PROJECT INTO { sum: SUM(e.data.price) }
302 /// ```
303 #[error("{0}:{1}: aggregate functions arguments must be source-bound fields")]
304 ExpectSourceBoundProperty(u32, u32),
305}
306
307impl From<LexerError> for Error {
308 fn from(value: LexerError) -> Self {
309 Self::Lexer(value)
310 }
311}
312
313impl From<ParserError> for Error {
314 fn from(value: ParserError) -> Self {
315 Self::Parser(value)
316 }
317}
318
319impl From<AnalysisError> for Error {
320 fn from(value: AnalysisError) -> Self {
321 Self::Analysis(value)
322 }
323}